Error message cleanup
[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 Copyright (C) 2019-2020 Advanced Micro Devices, Inc. All rights reserved.
5
6 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
7 Inc. with support from Florida State University (under contract
8 with the Ada Joint Program Office), and Silicon Graphics, Inc.
9 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
10 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
11 support.
12
13 This file is part of GDB.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 3 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27
28 /* FIXME: Various die-reading functions need to be more careful with
29 reading off the end of the section.
30 E.g., load_partial_dies, read_partial_die. */
31
32 #include "defs.h"
33 #include "dwarf2read.h"
34 #include "dwarf-index-cache.h"
35 #include "dwarf-index-common.h"
36 #include "bfd.h"
37 #include "elf-bfd.h"
38 #include "symtab.h"
39 #include "gdbtypes.h"
40 #include "objfiles.h"
41 #include "dwarf2.h"
42 #include "buildsym.h"
43 #include "demangle.h"
44 #include "gdb-demangle.h"
45 #include "filenames.h" /* for DOSish file names */
46 #include "macrotab.h"
47 #include "language.h"
48 #include "complaints.h"
49 #include "dwarf2expr.h"
50 #include "dwarf2loc.h"
51 #include "cp-support.h"
52 #include "hashtab.h"
53 #include "command.h"
54 #include "gdbcmd.h"
55 #include "block.h"
56 #include "addrmap.h"
57 #include "typeprint.h"
58 #include "psympriv.h"
59 #include "c-lang.h"
60 #include "go-lang.h"
61 #include "valprint.h"
62 #include "gdbcore.h" /* for gnutarget */
63 #include "gdb/gdb-index.h"
64 #include "gdb_bfd.h"
65 #include "f-lang.h"
66 #include "source.h"
67 #include "build-id.h"
68 #include "namespace.h"
69 #include "gdbsupport/function-view.h"
70 #include "gdbsupport/gdb_optional.h"
71 #include "gdbsupport/underlying.h"
72 #include "gdbsupport/hash_enum.h"
73 #include "filename-seen-cache.h"
74 #include "producer.h"
75 #include <fcntl.h>
76 #include <algorithm>
77 #include <unordered_map>
78 #include "gdbsupport/selftest.h"
79 #include "rust-lang.h"
80 #include "gdbsupport/pathstuff.h"
81
82 /* When == 1, print basic high level tracing messages.
83 When > 1, be more verbose.
84 This is in contrast to the low level DIE reading of dwarf_die_debug. */
85 static unsigned int dwarf_read_debug = 0;
86
87 /* When non-zero, dump DIEs after they are read in. */
88 static unsigned int dwarf_die_debug = 0;
89
90 /* When non-zero, dump line number entries as they are read in. */
91 static unsigned int dwarf_line_debug = 0;
92
93 /* When true, cross-check physname against demangler. */
94 static bool check_physname = false;
95
96 /* When true, do not reject deprecated .gdb_index sections. */
97 static bool use_deprecated_index_sections = false;
98
99 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
100
101 /* The "aclass" indices for various kinds of computed DWARF symbols. */
102
103 static int dwarf2_locexpr_index;
104 static int dwarf2_loclist_index;
105 static int dwarf2_locexpr_block_index;
106 static int dwarf2_loclist_block_index;
107
108 /* An index into a (C++) symbol name component in a symbol name as
109 recorded in the mapped_index's symbol table. For each C++ symbol
110 in the symbol table, we record one entry for the start of each
111 component in the symbol in a table of name components, and then
112 sort the table, in order to be able to binary search symbol names,
113 ignoring leading namespaces, both completion and regular look up.
114 For example, for symbol "A::B::C", we'll have an entry that points
115 to "A::B::C", another that points to "B::C", and another for "C".
116 Note that function symbols in GDB index have no parameter
117 information, just the function/method names. You can convert a
118 name_component to a "const char *" using the
119 'mapped_index::symbol_name_at(offset_type)' method. */
120
121 struct name_component
122 {
123 /* Offset in the symbol name where the component starts. Stored as
124 a (32-bit) offset instead of a pointer to save memory and improve
125 locality on 64-bit architectures. */
126 offset_type name_offset;
127
128 /* The symbol's index in the symbol and constant pool tables of a
129 mapped_index. */
130 offset_type idx;
131 };
132
133 /* Base class containing bits shared by both .gdb_index and
134 .debug_name indexes. */
135
136 struct mapped_index_base
137 {
138 mapped_index_base () = default;
139 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
140
141 /* The name_component table (a sorted vector). See name_component's
142 description above. */
143 std::vector<name_component> name_components;
144
145 /* How NAME_COMPONENTS is sorted. */
146 enum case_sensitivity name_components_casing;
147
148 /* Return the number of names in the symbol table. */
149 virtual size_t symbol_name_count () const = 0;
150
151 /* Get the name of the symbol at IDX in the symbol table. */
152 virtual const char *symbol_name_at (offset_type idx) const = 0;
153
154 /* Return whether the name at IDX in the symbol table should be
155 ignored. */
156 virtual bool symbol_name_slot_invalid (offset_type idx) const
157 {
158 return false;
159 }
160
161 /* Build the symbol name component sorted vector, if we haven't
162 yet. */
163 void build_name_components ();
164
165 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
166 possible matches for LN_NO_PARAMS in the name component
167 vector. */
168 std::pair<std::vector<name_component>::const_iterator,
169 std::vector<name_component>::const_iterator>
170 find_name_components_bounds (const lookup_name_info &ln_no_params,
171 enum language lang) const;
172
173 /* Prevent deleting/destroying via a base class pointer. */
174 protected:
175 ~mapped_index_base() = default;
176 };
177
178 /* A description of the mapped index. The file format is described in
179 a comment by the code that writes the index. */
180 struct mapped_index final : public mapped_index_base
181 {
182 /* A slot/bucket in the symbol table hash. */
183 struct symbol_table_slot
184 {
185 const offset_type name;
186 const offset_type vec;
187 };
188
189 /* Index data format version. */
190 int version = 0;
191
192 /* The address table data. */
193 gdb::array_view<const gdb_byte> address_table;
194
195 /* The symbol table, implemented as a hash table. */
196 gdb::array_view<symbol_table_slot> symbol_table;
197
198 /* A pointer to the constant pool. */
199 const char *constant_pool = nullptr;
200
201 bool symbol_name_slot_invalid (offset_type idx) const override
202 {
203 const auto &bucket = this->symbol_table[idx];
204 return bucket.name == 0 && bucket.vec == 0;
205 }
206
207 /* Convenience method to get at the name of the symbol at IDX in the
208 symbol table. */
209 const char *symbol_name_at (offset_type idx) const override
210 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
211
212 size_t symbol_name_count () const override
213 { return this->symbol_table.size (); }
214 };
215
216 /* A description of the mapped .debug_names.
217 Uninitialized map has CU_COUNT 0. */
218 struct mapped_debug_names final : public mapped_index_base
219 {
220 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
221 : dwarf2_per_objfile (dwarf2_per_objfile_)
222 {}
223
224 struct dwarf2_per_objfile *dwarf2_per_objfile;
225 bfd_endian dwarf5_byte_order;
226 bool dwarf5_is_dwarf64;
227 bool augmentation_is_gdb;
228 uint8_t offset_size;
229 uint32_t cu_count = 0;
230 uint32_t tu_count, bucket_count, name_count;
231 const gdb_byte *cu_table_reordered, *tu_table_reordered;
232 const uint32_t *bucket_table_reordered, *hash_table_reordered;
233 const gdb_byte *name_table_string_offs_reordered;
234 const gdb_byte *name_table_entry_offs_reordered;
235 const gdb_byte *entry_pool;
236
237 struct index_val
238 {
239 ULONGEST dwarf_tag;
240 struct attr
241 {
242 /* Attribute name DW_IDX_*. */
243 ULONGEST dw_idx;
244
245 /* Attribute form DW_FORM_*. */
246 ULONGEST form;
247
248 /* Value if FORM is DW_FORM_implicit_const. */
249 LONGEST implicit_const;
250 };
251 std::vector<attr> attr_vec;
252 };
253
254 std::unordered_map<ULONGEST, index_val> abbrev_map;
255
256 const char *namei_to_name (uint32_t namei) const;
257
258 /* Implementation of the mapped_index_base virtual interface, for
259 the name_components cache. */
260
261 const char *symbol_name_at (offset_type idx) const override
262 { return namei_to_name (idx); }
263
264 size_t symbol_name_count () const override
265 { return this->name_count; }
266 };
267
268 /* See dwarf2read.h. */
269
270 dwarf2_per_objfile *
271 get_dwarf2_per_objfile (struct objfile *objfile)
272 {
273 return dwarf2_objfile_data_key.get (objfile);
274 }
275
276 /* Default names of the debugging sections. */
277
278 /* Note that if the debugging section has been compressed, it might
279 have a name like .zdebug_info. */
280
281 static const struct dwarf2_debug_sections dwarf2_elf_names =
282 {
283 { ".debug_info", ".zdebug_info" },
284 { ".debug_abbrev", ".zdebug_abbrev" },
285 { ".debug_line", ".zdebug_line" },
286 { ".debug_loc", ".zdebug_loc" },
287 { ".debug_loclists", ".zdebug_loclists" },
288 { ".debug_macinfo", ".zdebug_macinfo" },
289 { ".debug_macro", ".zdebug_macro" },
290 { ".debug_str", ".zdebug_str" },
291 { ".debug_line_str", ".zdebug_line_str" },
292 { ".debug_ranges", ".zdebug_ranges" },
293 { ".debug_rnglists", ".zdebug_rnglists" },
294 { ".debug_types", ".zdebug_types" },
295 { ".debug_addr", ".zdebug_addr" },
296 { ".debug_frame", ".zdebug_frame" },
297 { ".eh_frame", NULL },
298 { ".gdb_index", ".zgdb_index" },
299 { ".debug_names", ".zdebug_names" },
300 { ".debug_aranges", ".zdebug_aranges" },
301 23
302 };
303
304 /* List of DWO/DWP sections. */
305
306 static const struct dwop_section_names
307 {
308 struct dwarf2_section_names abbrev_dwo;
309 struct dwarf2_section_names info_dwo;
310 struct dwarf2_section_names line_dwo;
311 struct dwarf2_section_names loc_dwo;
312 struct dwarf2_section_names loclists_dwo;
313 struct dwarf2_section_names macinfo_dwo;
314 struct dwarf2_section_names macro_dwo;
315 struct dwarf2_section_names str_dwo;
316 struct dwarf2_section_names str_offsets_dwo;
317 struct dwarf2_section_names types_dwo;
318 struct dwarf2_section_names cu_index;
319 struct dwarf2_section_names tu_index;
320 }
321 dwop_section_names =
322 {
323 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
324 { ".debug_info.dwo", ".zdebug_info.dwo" },
325 { ".debug_line.dwo", ".zdebug_line.dwo" },
326 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
327 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
335 };
336
337 /* local data types */
338
339 /* The data in a compilation unit header, after target2host
340 translation, looks like this. */
341 struct comp_unit_head
342 {
343 unsigned int length;
344 short version;
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
347 sect_offset abbrev_sect_off;
348
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
351
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
354
355 enum dwarf_unit_type unit_type;
356
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
359 sect_offset sect_off;
360
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
363 cu_offset first_die_cu_offset;
364
365
366 /* 64-bit signature of this unit. For type units, it denotes the signature of
367 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
368 Also used in DWARF 5, to denote the dwo id when the unit type is
369 DW_UT_skeleton or DW_UT_split_compile. */
370 ULONGEST signature;
371
372 /* For types, offset in the type's DIE of the type defined by this TU. */
373 cu_offset type_cu_offset_in_tu;
374 };
375
376 /* Type used for delaying computation of method physnames.
377 See comments for compute_delayed_physnames. */
378 struct delayed_method_info
379 {
380 /* The type to which the method is attached, i.e., its parent class. */
381 struct type *type;
382
383 /* The index of the method in the type's function fieldlists. */
384 int fnfield_index;
385
386 /* The index of the method in the fieldlist. */
387 int index;
388
389 /* The name of the DIE. */
390 const char *name;
391
392 /* The DIE associated with this method. */
393 struct die_info *die;
394 };
395
396 /* Internal state when decoding a particular compilation unit. */
397 struct dwarf2_cu
398 {
399 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
400 ~dwarf2_cu ();
401
402 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
403
404 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
405 Create the set of symtabs used by this TU, or if this TU is sharing
406 symtabs with another TU and the symtabs have already been created
407 then restore those symtabs in the line header.
408 We don't need the pc/line-number mapping for type units. */
409 void setup_type_unit_groups (struct die_info *die);
410
411 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
412 buildsym_compunit constructor. */
413 struct compunit_symtab *start_symtab (const char *name,
414 const char *comp_dir,
415 CORE_ADDR low_pc);
416
417 /* Reset the builder. */
418 void reset_builder () { m_builder.reset (); }
419
420 /* The header of the compilation unit. */
421 struct comp_unit_head header {};
422
423 /* Base address of this compilation unit. */
424 CORE_ADDR base_address = 0;
425
426 /* Non-zero if base_address has been set. */
427 int base_known = 0;
428
429 /* The language we are debugging. */
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
432
433 const char *producer = nullptr;
434
435 private:
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
438 std::unique_ptr<buildsym_compunit> m_builder;
439
440 public:
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
450 struct pending **list_in_scope = nullptr;
451
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
454 htab_t partial_dies = nullptr;
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
458 auto_obstack comp_unit_obstack;
459
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
465
466 /* Backlink to our per_cu entry. */
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
470 int last_used = 0;
471
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
474 htab_t die_hash = nullptr;
475
476 /* Full DIEs if read in. */
477 struct die_info *dies = nullptr;
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
482 htab_t dependencies = nullptr;
483
484 /* Header data from the line table, during full symbol processing. */
485 struct line_header *line_header = nullptr;
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
491 die_info *line_header_die_owner = nullptr;
492
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
495 std::vector<delayed_method_info> method_list;
496
497 /* To be copied to symtab->call_site_htab. */
498 htab_t call_site_htab = nullptr;
499
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
504 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
509 struct dwo_unit *dwo_unit = nullptr;
510
511 /* The DW_AT_addr_base attribute if present, zero otherwise
512 (zero is a valid value though).
513 Note this value comes from the Fission stub CU/TU's DIE. */
514 ULONGEST addr_base = 0;
515
516 /* The DW_AT_ranges_base attribute if present, zero otherwise
517 (zero is a valid value though).
518 Note this value comes from the Fission stub CU/TU's DIE.
519 Also note that the value is zero in the non-DWO case so this value can
520 be used without needing to know whether DWO files are in use or not.
521 N.B. This does not apply to DW_AT_ranges appearing in
522 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
523 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
524 DW_AT_ranges_base *would* have to be applied, and we'd have to care
525 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
526 ULONGEST ranges_base = 0;
527
528 /* When reading debug info generated by older versions of rustc, we
529 have to rewrite some union types to be struct types with a
530 variant part. This rewriting must be done after the CU is fully
531 read in, because otherwise at the point of rewriting some struct
532 type might not have been fully processed. So, we keep a list of
533 all such types here and process them after expansion. */
534 std::vector<struct type *> rust_unions;
535
536 /* Mark used when releasing cached dies. */
537 bool mark : 1;
538
539 /* This CU references .debug_loc. See the symtab->locations_valid field.
540 This test is imperfect as there may exist optimized debug code not using
541 any location list and still facing inlining issues if handled as
542 unoptimized code. For a future better test see GCC PR other/32998. */
543 bool has_loclist : 1;
544
545 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
546 if all the producer_is_* fields are valid. This information is cached
547 because profiling CU expansion showed excessive time spent in
548 producer_is_gxx_lt_4_6. */
549 bool checked_producer : 1;
550 bool producer_is_gxx_lt_4_6 : 1;
551 bool producer_is_gcc_lt_4_3 : 1;
552 bool producer_is_icc : 1;
553 bool producer_is_icc_lt_14 : 1;
554 bool producer_is_codewarrior : 1;
555
556 /* When true, the file that we're processing is known to have
557 debugging info for C++ namespaces. GCC 3.3.x did not produce
558 this information, but later versions do. */
559
560 bool processing_has_namespace_info : 1;
561
562 struct partial_die_info *find_partial_die (sect_offset sect_off);
563
564 /* If this CU was inherited by another CU (via specification,
565 abstract_origin, etc), this is the ancestor CU. */
566 dwarf2_cu *ancestor;
567
568 /* Get the buildsym_compunit for this CU. */
569 buildsym_compunit *get_builder ()
570 {
571 /* If this CU has a builder associated with it, use that. */
572 if (m_builder != nullptr)
573 return m_builder.get ();
574
575 /* Otherwise, search ancestors for a valid builder. */
576 if (ancestor != nullptr)
577 return ancestor->get_builder ();
578
579 return nullptr;
580 }
581 };
582
583 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
584 This includes type_unit_group and quick_file_names. */
585
586 struct stmt_list_hash
587 {
588 /* The DWO unit this table is from or NULL if there is none. */
589 struct dwo_unit *dwo_unit;
590
591 /* Offset in .debug_line or .debug_line.dwo. */
592 sect_offset line_sect_off;
593 };
594
595 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
596 an object of this type. */
597
598 struct type_unit_group
599 {
600 /* dwarf2read.c's main "handle" on a TU symtab.
601 To simplify things we create an artificial CU that "includes" all the
602 type units using this stmt_list so that the rest of the code still has
603 a "per_cu" handle on the symtab.
604 This PER_CU is recognized by having no section. */
605 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
606 struct dwarf2_per_cu_data per_cu;
607
608 /* The TUs that share this DW_AT_stmt_list entry.
609 This is added to while parsing type units to build partial symtabs,
610 and is deleted afterwards and not used again. */
611 std::vector<signatured_type *> *tus;
612
613 /* The compunit symtab.
614 Type units in a group needn't all be defined in the same source file,
615 so we create an essentially anonymous symtab as the compunit symtab. */
616 struct compunit_symtab *compunit_symtab;
617
618 /* The data used to construct the hash key. */
619 struct stmt_list_hash hash;
620
621 /* The number of symtabs from the line header.
622 The value here must match line_header.num_file_names. */
623 unsigned int num_symtabs;
624
625 /* The symbol tables for this TU (obtained from the files listed in
626 DW_AT_stmt_list).
627 WARNING: The order of entries here must match the order of entries
628 in the line header. After the first TU using this type_unit_group, the
629 line header for the subsequent TUs is recreated from this. This is done
630 because we need to use the same symtabs for each TU using the same
631 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
632 there's no guarantee the line header doesn't have duplicate entries. */
633 struct symtab **symtabs;
634 };
635
636 /* These sections are what may appear in a (real or virtual) DWO file. */
637
638 struct dwo_sections
639 {
640 struct dwarf2_section_info abbrev;
641 struct dwarf2_section_info line;
642 struct dwarf2_section_info loc;
643 struct dwarf2_section_info loclists;
644 struct dwarf2_section_info macinfo;
645 struct dwarf2_section_info macro;
646 struct dwarf2_section_info str;
647 struct dwarf2_section_info str_offsets;
648 /* In the case of a virtual DWO file, these two are unused. */
649 struct dwarf2_section_info info;
650 std::vector<dwarf2_section_info> types;
651 };
652
653 /* CUs/TUs in DWP/DWO files. */
654
655 struct dwo_unit
656 {
657 /* Backlink to the containing struct dwo_file. */
658 struct dwo_file *dwo_file;
659
660 /* The "id" that distinguishes this CU/TU.
661 .debug_info calls this "dwo_id", .debug_types calls this "signature".
662 Since signatures came first, we stick with it for consistency. */
663 ULONGEST signature;
664
665 /* The section this CU/TU lives in, in the DWO file. */
666 struct dwarf2_section_info *section;
667
668 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
669 sect_offset sect_off;
670 unsigned int length;
671
672 /* For types, offset in the type's DIE of the type defined by this TU. */
673 cu_offset type_offset_in_tu;
674 };
675
676 /* include/dwarf2.h defines the DWP section codes.
677 It defines a max value but it doesn't define a min value, which we
678 use for error checking, so provide one. */
679
680 enum dwp_v2_section_ids
681 {
682 DW_SECT_MIN = 1
683 };
684
685 /* Data for one DWO file.
686
687 This includes virtual DWO files (a virtual DWO file is a DWO file as it
688 appears in a DWP file). DWP files don't really have DWO files per se -
689 comdat folding of types "loses" the DWO file they came from, and from
690 a high level view DWP files appear to contain a mass of random types.
691 However, to maintain consistency with the non-DWP case we pretend DWP
692 files contain virtual DWO files, and we assign each TU with one virtual
693 DWO file (generally based on the line and abbrev section offsets -
694 a heuristic that seems to work in practice). */
695
696 struct dwo_file
697 {
698 dwo_file () = default;
699 DISABLE_COPY_AND_ASSIGN (dwo_file);
700
701 /* The DW_AT_GNU_dwo_name attribute.
702 For virtual DWO files the name is constructed from the section offsets
703 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
704 from related CU+TUs. */
705 const char *dwo_name = nullptr;
706
707 /* The DW_AT_comp_dir attribute. */
708 const char *comp_dir = nullptr;
709
710 /* The bfd, when the file is open. Otherwise this is NULL.
711 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
712 gdb_bfd_ref_ptr dbfd;
713
714 /* The sections that make up this DWO file.
715 Remember that for virtual DWO files in DWP V2, these are virtual
716 sections (for lack of a better name). */
717 struct dwo_sections sections {};
718
719 /* The CUs in the file.
720 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
721 an extension to handle LLVM's Link Time Optimization output (where
722 multiple source files may be compiled into a single object/dwo pair). */
723 htab_t cus {};
724
725 /* Table of TUs in the file.
726 Each element is a struct dwo_unit. */
727 htab_t tus {};
728 };
729
730 /* These sections are what may appear in a DWP file. */
731
732 struct dwp_sections
733 {
734 /* These are used by both DWP version 1 and 2. */
735 struct dwarf2_section_info str;
736 struct dwarf2_section_info cu_index;
737 struct dwarf2_section_info tu_index;
738
739 /* These are only used by DWP version 2 files.
740 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
741 sections are referenced by section number, and are not recorded here.
742 In DWP version 2 there is at most one copy of all these sections, each
743 section being (effectively) comprised of the concatenation of all of the
744 individual sections that exist in the version 1 format.
745 To keep the code simple we treat each of these concatenated pieces as a
746 section itself (a virtual section?). */
747 struct dwarf2_section_info abbrev;
748 struct dwarf2_section_info info;
749 struct dwarf2_section_info line;
750 struct dwarf2_section_info loc;
751 struct dwarf2_section_info macinfo;
752 struct dwarf2_section_info macro;
753 struct dwarf2_section_info str_offsets;
754 struct dwarf2_section_info types;
755 };
756
757 /* These sections are what may appear in a virtual DWO file in DWP version 1.
758 A virtual DWO file is a DWO file as it appears in a DWP file. */
759
760 struct virtual_v1_dwo_sections
761 {
762 struct dwarf2_section_info abbrev;
763 struct dwarf2_section_info line;
764 struct dwarf2_section_info loc;
765 struct dwarf2_section_info macinfo;
766 struct dwarf2_section_info macro;
767 struct dwarf2_section_info str_offsets;
768 /* Each DWP hash table entry records one CU or one TU.
769 That is recorded here, and copied to dwo_unit.section. */
770 struct dwarf2_section_info info_or_types;
771 };
772
773 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
774 In version 2, the sections of the DWO files are concatenated together
775 and stored in one section of that name. Thus each ELF section contains
776 several "virtual" sections. */
777
778 struct virtual_v2_dwo_sections
779 {
780 bfd_size_type abbrev_offset;
781 bfd_size_type abbrev_size;
782
783 bfd_size_type line_offset;
784 bfd_size_type line_size;
785
786 bfd_size_type loc_offset;
787 bfd_size_type loc_size;
788
789 bfd_size_type macinfo_offset;
790 bfd_size_type macinfo_size;
791
792 bfd_size_type macro_offset;
793 bfd_size_type macro_size;
794
795 bfd_size_type str_offsets_offset;
796 bfd_size_type str_offsets_size;
797
798 /* Each DWP hash table entry records one CU or one TU.
799 That is recorded here, and copied to dwo_unit.section. */
800 bfd_size_type info_or_types_offset;
801 bfd_size_type info_or_types_size;
802 };
803
804 /* Contents of DWP hash tables. */
805
806 struct dwp_hash_table
807 {
808 uint32_t version, nr_columns;
809 uint32_t nr_units, nr_slots;
810 const gdb_byte *hash_table, *unit_table;
811 union
812 {
813 struct
814 {
815 const gdb_byte *indices;
816 } v1;
817 struct
818 {
819 /* This is indexed by column number and gives the id of the section
820 in that column. */
821 #define MAX_NR_V2_DWO_SECTIONS \
822 (1 /* .debug_info or .debug_types */ \
823 + 1 /* .debug_abbrev */ \
824 + 1 /* .debug_line */ \
825 + 1 /* .debug_loc */ \
826 + 1 /* .debug_str_offsets */ \
827 + 1 /* .debug_macro or .debug_macinfo */)
828 int section_ids[MAX_NR_V2_DWO_SECTIONS];
829 const gdb_byte *offsets;
830 const gdb_byte *sizes;
831 } v2;
832 } section_pool;
833 };
834
835 /* Data for one DWP file. */
836
837 struct dwp_file
838 {
839 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
840 : name (name_),
841 dbfd (std::move (abfd))
842 {
843 }
844
845 /* Name of the file. */
846 const char *name;
847
848 /* File format version. */
849 int version = 0;
850
851 /* The bfd. */
852 gdb_bfd_ref_ptr dbfd;
853
854 /* Section info for this file. */
855 struct dwp_sections sections {};
856
857 /* Table of CUs in the file. */
858 const struct dwp_hash_table *cus = nullptr;
859
860 /* Table of TUs in the file. */
861 const struct dwp_hash_table *tus = nullptr;
862
863 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
864 htab_t loaded_cus {};
865 htab_t loaded_tus {};
866
867 /* Table to map ELF section numbers to their sections.
868 This is only needed for the DWP V1 file format. */
869 unsigned int num_sections = 0;
870 asection **elf_sections = nullptr;
871 };
872
873 /* Struct used to pass misc. parameters to read_die_and_children, et
874 al. which are used for both .debug_info and .debug_types dies.
875 All parameters here are unchanging for the life of the call. This
876 struct exists to abstract away the constant parameters of die reading. */
877
878 struct die_reader_specs
879 {
880 /* The bfd of die_section. */
881 bfd* abfd;
882
883 /* The CU of the DIE we are parsing. */
884 struct dwarf2_cu *cu;
885
886 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
887 struct dwo_file *dwo_file;
888
889 /* The section the die comes from.
890 This is either .debug_info or .debug_types, or the .dwo variants. */
891 struct dwarf2_section_info *die_section;
892
893 /* die_section->buffer. */
894 const gdb_byte *buffer;
895
896 /* The end of the buffer. */
897 const gdb_byte *buffer_end;
898
899 /* The value of the DW_AT_comp_dir attribute. */
900 const char *comp_dir;
901
902 /* The abbreviation table to use when reading the DIEs. */
903 struct abbrev_table *abbrev_table;
904 };
905
906 /* Type of function passed to init_cutu_and_read_dies, et.al. */
907 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
908 const gdb_byte *info_ptr,
909 struct die_info *comp_unit_die,
910 int has_children,
911 void *data);
912
913 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
914 later. */
915 typedef int dir_index;
916
917 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
918 and later. */
919 typedef int file_name_index;
920
921 struct file_entry
922 {
923 file_entry () = default;
924
925 file_entry (const char *name_, dir_index d_index_,
926 unsigned int mod_time_, unsigned int length_)
927 : name (name_),
928 d_index (d_index_),
929 mod_time (mod_time_),
930 length (length_)
931 {}
932
933 /* Return the include directory at D_INDEX stored in LH. Returns
934 NULL if D_INDEX is out of bounds. */
935 const char *include_dir (const line_header *lh) const;
936
937 /* The file name. Note this is an observing pointer. The memory is
938 owned by debug_line_buffer. */
939 const char *name {};
940
941 /* The directory index (1-based). */
942 dir_index d_index {};
943
944 unsigned int mod_time {};
945
946 unsigned int length {};
947
948 /* True if referenced by the Line Number Program. */
949 bool included_p {};
950
951 /* The associated symbol table, if any. */
952 struct symtab *symtab {};
953 };
954
955 /* The line number information for a compilation unit (found in the
956 .debug_line section) begins with a "statement program header",
957 which contains the following information. */
958 struct line_header
959 {
960 line_header ()
961 : offset_in_dwz {}
962 {}
963
964 /* Add an entry to the include directory table. */
965 void add_include_dir (const char *include_dir);
966
967 /* Add an entry to the file name table. */
968 void add_file_name (const char *name, dir_index d_index,
969 unsigned int mod_time, unsigned int length);
970
971 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
972 Returns NULL if INDEX is out of bounds. */
973 const char *include_dir_at (dir_index index) const
974 {
975 int vec_index;
976 if (version >= 5)
977 vec_index = index;
978 else
979 vec_index = index - 1;
980 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
981 return NULL;
982 return m_include_dirs[vec_index];
983 }
984
985 bool is_valid_file_index (int file_index)
986 {
987 if (version >= 5)
988 return 0 <= file_index && file_index < file_names_size ();
989 return 1 <= file_index && file_index <= file_names_size ();
990 }
991
992 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
993 Returns NULL if INDEX is out of bounds. */
994 file_entry *file_name_at (file_name_index index)
995 {
996 int vec_index;
997 if (version >= 5)
998 vec_index = index;
999 else
1000 vec_index = index - 1;
1001 if (vec_index < 0 || vec_index >= m_file_names.size ())
1002 return NULL;
1003 return &m_file_names[vec_index];
1004 }
1005
1006 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1007 this method should only be used to iterate through all file entries in an
1008 index-agnostic manner. */
1009 std::vector<file_entry> &file_names ()
1010 { return m_file_names; }
1011
1012 /* Offset of line number information in .debug_line section. */
1013 sect_offset sect_off {};
1014
1015 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1016 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1017
1018 unsigned int total_length {};
1019 unsigned short version {};
1020 unsigned int header_length {};
1021 unsigned char minimum_instruction_length {};
1022 unsigned char maximum_ops_per_instruction {};
1023 unsigned char default_is_stmt {};
1024 int line_base {};
1025 unsigned char line_range {};
1026 unsigned char opcode_base {};
1027
1028 /* standard_opcode_lengths[i] is the number of operands for the
1029 standard opcode whose value is i. This means that
1030 standard_opcode_lengths[0] is unused, and the last meaningful
1031 element is standard_opcode_lengths[opcode_base - 1]. */
1032 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1033
1034 int file_names_size ()
1035 { return m_file_names.size(); }
1036
1037 /* The start and end of the statement program following this
1038 header. These point into dwarf2_per_objfile->line_buffer. */
1039 const gdb_byte *statement_program_start {}, *statement_program_end {};
1040
1041 private:
1042 /* The include_directories table. Note these are observing
1043 pointers. The memory is owned by debug_line_buffer. */
1044 std::vector<const char *> m_include_dirs;
1045
1046 /* The file_names table. This is private because the meaning of indexes
1047 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1048 before, and is 0 in DWARF 5 and later). So the client should use
1049 file_name_at method for access. */
1050 std::vector<file_entry> m_file_names;
1051 };
1052
1053 typedef std::unique_ptr<line_header> line_header_up;
1054
1055 const char *
1056 file_entry::include_dir (const line_header *lh) const
1057 {
1058 return lh->include_dir_at (d_index);
1059 }
1060
1061 /* When we construct a partial symbol table entry we only
1062 need this much information. */
1063 struct partial_die_info : public allocate_on_obstack
1064 {
1065 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1066
1067 /* Disable assign but still keep copy ctor, which is needed
1068 load_partial_dies. */
1069 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1070
1071 /* Adjust the partial die before generating a symbol for it. This
1072 function may set the is_external flag or change the DIE's
1073 name. */
1074 void fixup (struct dwarf2_cu *cu);
1075
1076 /* Read a minimal amount of information into the minimal die
1077 structure. */
1078 const gdb_byte *read (const struct die_reader_specs *reader,
1079 const struct abbrev_info &abbrev,
1080 const gdb_byte *info_ptr);
1081
1082 /* Offset of this DIE. */
1083 const sect_offset sect_off;
1084
1085 /* DWARF-2 tag for this DIE. */
1086 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1087
1088 /* Assorted flags describing the data found in this DIE. */
1089 const unsigned int has_children : 1;
1090
1091 unsigned int is_external : 1;
1092 unsigned int is_declaration : 1;
1093 unsigned int has_type : 1;
1094 unsigned int has_specification : 1;
1095 unsigned int has_pc_info : 1;
1096 unsigned int may_be_inlined : 1;
1097
1098 /* This DIE has been marked DW_AT_main_subprogram. */
1099 unsigned int main_subprogram : 1;
1100
1101 /* Flag set if the SCOPE field of this structure has been
1102 computed. */
1103 unsigned int scope_set : 1;
1104
1105 /* Flag set if the DIE has a byte_size attribute. */
1106 unsigned int has_byte_size : 1;
1107
1108 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1109 unsigned int has_const_value : 1;
1110
1111 /* Flag set if any of the DIE's children are template arguments. */
1112 unsigned int has_template_arguments : 1;
1113
1114 /* Flag set if fixup has been called on this die. */
1115 unsigned int fixup_called : 1;
1116
1117 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1118 unsigned int is_dwz : 1;
1119
1120 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1121 unsigned int spec_is_dwz : 1;
1122
1123 /* The name of this DIE. Normally the value of DW_AT_name, but
1124 sometimes a default name for unnamed DIEs. */
1125 const char *name = nullptr;
1126
1127 /* The linkage name, if present. */
1128 const char *linkage_name = nullptr;
1129
1130 /* The scope to prepend to our children. This is generally
1131 allocated on the comp_unit_obstack, so will disappear
1132 when this compilation unit leaves the cache. */
1133 const char *scope = nullptr;
1134
1135 /* Some data associated with the partial DIE. The tag determines
1136 which field is live. */
1137 union
1138 {
1139 /* The location description associated with this DIE, if any. */
1140 struct dwarf_block *locdesc;
1141 /* The offset of an import, for DW_TAG_imported_unit. */
1142 sect_offset sect_off;
1143 } d {};
1144
1145 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1146 CORE_ADDR lowpc = 0;
1147 CORE_ADDR highpc = 0;
1148
1149 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1150 DW_AT_sibling, if any. */
1151 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1152 could return DW_AT_sibling values to its caller load_partial_dies. */
1153 const gdb_byte *sibling = nullptr;
1154
1155 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1156 DW_AT_specification (or DW_AT_abstract_origin or
1157 DW_AT_extension). */
1158 sect_offset spec_offset {};
1159
1160 /* Pointers to this DIE's parent, first child, and next sibling,
1161 if any. */
1162 struct partial_die_info *die_parent = nullptr;
1163 struct partial_die_info *die_child = nullptr;
1164 struct partial_die_info *die_sibling = nullptr;
1165
1166 friend struct partial_die_info *
1167 dwarf2_cu::find_partial_die (sect_offset sect_off);
1168
1169 private:
1170 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1171 partial_die_info (sect_offset sect_off)
1172 : partial_die_info (sect_off, DW_TAG_padding, 0)
1173 {
1174 }
1175
1176 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1177 int has_children_)
1178 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1179 {
1180 is_external = 0;
1181 is_declaration = 0;
1182 has_type = 0;
1183 has_specification = 0;
1184 has_pc_info = 0;
1185 may_be_inlined = 0;
1186 main_subprogram = 0;
1187 scope_set = 0;
1188 has_byte_size = 0;
1189 has_const_value = 0;
1190 has_template_arguments = 0;
1191 fixup_called = 0;
1192 is_dwz = 0;
1193 spec_is_dwz = 0;
1194 }
1195 };
1196
1197 /* This data structure holds the information of an abbrev. */
1198 struct abbrev_info
1199 {
1200 unsigned int number; /* number identifying abbrev */
1201 enum dwarf_tag tag; /* dwarf tag */
1202 unsigned short has_children; /* boolean */
1203 unsigned short num_attrs; /* number of attributes */
1204 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1205 struct abbrev_info *next; /* next in chain */
1206 };
1207
1208 struct attr_abbrev
1209 {
1210 ENUM_BITFIELD(dwarf_attribute) name : 16;
1211 ENUM_BITFIELD(dwarf_form) form : 16;
1212
1213 /* It is valid only if FORM is DW_FORM_implicit_const. */
1214 LONGEST implicit_const;
1215 };
1216
1217 /* Size of abbrev_table.abbrev_hash_table. */
1218 #define ABBREV_HASH_SIZE 121
1219
1220 /* Top level data structure to contain an abbreviation table. */
1221
1222 struct abbrev_table
1223 {
1224 explicit abbrev_table (sect_offset off)
1225 : sect_off (off)
1226 {
1227 m_abbrevs =
1228 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1229 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1230 }
1231
1232 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1233
1234 /* Allocate space for a struct abbrev_info object in
1235 ABBREV_TABLE. */
1236 struct abbrev_info *alloc_abbrev ();
1237
1238 /* Add an abbreviation to the table. */
1239 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1240
1241 /* Look up an abbrev in the table.
1242 Returns NULL if the abbrev is not found. */
1243
1244 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1245
1246
1247 /* Where the abbrev table came from.
1248 This is used as a sanity check when the table is used. */
1249 const sect_offset sect_off;
1250
1251 /* Storage for the abbrev table. */
1252 auto_obstack abbrev_obstack;
1253
1254 private:
1255
1256 /* Hash table of abbrevs.
1257 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1258 It could be statically allocated, but the previous code didn't so we
1259 don't either. */
1260 struct abbrev_info **m_abbrevs;
1261 };
1262
1263 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1264
1265 /* Attributes have a name and a value. */
1266 struct attribute
1267 {
1268 ENUM_BITFIELD(dwarf_attribute) name : 16;
1269 ENUM_BITFIELD(dwarf_form) form : 15;
1270
1271 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1272 field should be in u.str (existing only for DW_STRING) but it is kept
1273 here for better struct attribute alignment. */
1274 unsigned int string_is_canonical : 1;
1275
1276 union
1277 {
1278 const char *str;
1279 struct dwarf_block *blk;
1280 ULONGEST unsnd;
1281 LONGEST snd;
1282 CORE_ADDR addr;
1283 ULONGEST signature;
1284 }
1285 u;
1286 };
1287
1288 /* This data structure holds a complete die structure. */
1289 struct die_info
1290 {
1291 /* DWARF-2 tag for this DIE. */
1292 ENUM_BITFIELD(dwarf_tag) tag : 16;
1293
1294 /* Number of attributes */
1295 unsigned char num_attrs;
1296
1297 /* True if we're presently building the full type name for the
1298 type derived from this DIE. */
1299 unsigned char building_fullname : 1;
1300
1301 /* True if this die is in process. PR 16581. */
1302 unsigned char in_process : 1;
1303
1304 /* Abbrev number */
1305 unsigned int abbrev;
1306
1307 /* Offset in .debug_info or .debug_types section. */
1308 sect_offset sect_off;
1309
1310 /* The dies in a compilation unit form an n-ary tree. PARENT
1311 points to this die's parent; CHILD points to the first child of
1312 this node; and all the children of a given node are chained
1313 together via their SIBLING fields. */
1314 struct die_info *child; /* Its first child, if any. */
1315 struct die_info *sibling; /* Its next sibling, if any. */
1316 struct die_info *parent; /* Its parent, if any. */
1317
1318 /* An array of attributes, with NUM_ATTRS elements. There may be
1319 zero, but it's not common and zero-sized arrays are not
1320 sufficiently portable C. */
1321 struct attribute attrs[1];
1322 };
1323
1324 /* Get at parts of an attribute structure. */
1325
1326 #define DW_STRING(attr) ((attr)->u.str)
1327 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1328 #define DW_UNSND(attr) ((attr)->u.unsnd)
1329 #define DW_BLOCK(attr) ((attr)->u.blk)
1330 #define DW_SND(attr) ((attr)->u.snd)
1331 #define DW_ADDR(attr) ((attr)->u.addr)
1332 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1333
1334 /* Blocks are a bunch of untyped bytes. */
1335 struct dwarf_block
1336 {
1337 size_t size;
1338
1339 /* Valid only if SIZE is not zero. */
1340 const gdb_byte *data;
1341 };
1342
1343 #ifndef ATTR_ALLOC_CHUNK
1344 #define ATTR_ALLOC_CHUNK 4
1345 #endif
1346
1347 /* Allocate fields for structs, unions and enums in this size. */
1348 #ifndef DW_FIELD_ALLOC_CHUNK
1349 #define DW_FIELD_ALLOC_CHUNK 4
1350 #endif
1351
1352 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1353 but this would require a corresponding change in unpack_field_as_long
1354 and friends. */
1355 static int bits_per_byte = 8;
1356
1357 /* When reading a variant or variant part, we track a bit more
1358 information about the field, and store it in an object of this
1359 type. */
1360
1361 struct variant_field
1362 {
1363 /* If we see a DW_TAG_variant, then this will be the discriminant
1364 value. */
1365 ULONGEST discriminant_value;
1366 /* If we see a DW_TAG_variant, then this will be set if this is the
1367 default branch. */
1368 bool default_branch;
1369 /* While reading a DW_TAG_variant_part, this will be set if this
1370 field is the discriminant. */
1371 bool is_discriminant;
1372 };
1373
1374 struct nextfield
1375 {
1376 int accessibility = 0;
1377 int virtuality = 0;
1378 /* Extra information to describe a variant or variant part. */
1379 struct variant_field variant {};
1380 struct field field {};
1381 };
1382
1383 struct fnfieldlist
1384 {
1385 const char *name = nullptr;
1386 std::vector<struct fn_field> fnfields;
1387 };
1388
1389 /* The routines that read and process dies for a C struct or C++ class
1390 pass lists of data member fields and lists of member function fields
1391 in an instance of a field_info structure, as defined below. */
1392 struct field_info
1393 {
1394 /* List of data member and baseclasses fields. */
1395 std::vector<struct nextfield> fields;
1396 std::vector<struct nextfield> baseclasses;
1397
1398 /* Number of fields (including baseclasses). */
1399 int nfields = 0;
1400
1401 /* Set if the accessibility of one of the fields is not public. */
1402 int non_public_fields = 0;
1403
1404 /* Member function fieldlist array, contains name of possibly overloaded
1405 member function, number of overloaded member functions and a pointer
1406 to the head of the member function field chain. */
1407 std::vector<struct fnfieldlist> fnfieldlists;
1408
1409 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1410 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1411 std::vector<struct decl_field> typedef_field_list;
1412
1413 /* Nested types defined by this class and the number of elements in this
1414 list. */
1415 std::vector<struct decl_field> nested_types_list;
1416 };
1417
1418 /* One item on the queue of compilation units to read in full symbols
1419 for. */
1420 struct dwarf2_queue_item
1421 {
1422 struct dwarf2_per_cu_data *per_cu;
1423 enum language pretend_language;
1424 struct dwarf2_queue_item *next;
1425 };
1426
1427 /* The current queue. */
1428 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1429
1430 /* Loaded secondary compilation units are kept in memory until they
1431 have not been referenced for the processing of this many
1432 compilation units. Set this to zero to disable caching. Cache
1433 sizes of up to at least twenty will improve startup time for
1434 typical inter-CU-reference binaries, at an obvious memory cost. */
1435 static int dwarf_max_cache_age = 5;
1436 static void
1437 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1438 struct cmd_list_element *c, const char *value)
1439 {
1440 fprintf_filtered (file, _("The upper bound on the age of cached "
1441 "DWARF compilation units is %s.\n"),
1442 value);
1443 }
1444 \f
1445 /* local function prototypes */
1446
1447 static const char *get_section_name (const struct dwarf2_section_info *);
1448
1449 static const char *get_section_file_name (const struct dwarf2_section_info *);
1450
1451 static void dwarf2_find_base_address (struct die_info *die,
1452 struct dwarf2_cu *cu);
1453
1454 static struct partial_symtab *create_partial_symtab
1455 (struct dwarf2_per_cu_data *per_cu, const char *name);
1456
1457 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1458 const gdb_byte *info_ptr,
1459 struct die_info *type_unit_die,
1460 int has_children, void *data);
1461
1462 static void dwarf2_build_psymtabs_hard
1463 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1464
1465 static void scan_partial_symbols (struct partial_die_info *,
1466 CORE_ADDR *, CORE_ADDR *,
1467 int, struct dwarf2_cu *);
1468
1469 static void add_partial_symbol (struct partial_die_info *,
1470 struct dwarf2_cu *);
1471
1472 static void add_partial_namespace (struct partial_die_info *pdi,
1473 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1474 int set_addrmap, struct dwarf2_cu *cu);
1475
1476 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1477 CORE_ADDR *highpc, int set_addrmap,
1478 struct dwarf2_cu *cu);
1479
1480 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1481 struct dwarf2_cu *cu);
1482
1483 static void add_partial_subprogram (struct partial_die_info *pdi,
1484 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1485 int need_pc, struct dwarf2_cu *cu);
1486
1487 static void dwarf2_read_symtab (struct partial_symtab *,
1488 struct objfile *);
1489
1490 static void psymtab_to_symtab_1 (struct partial_symtab *);
1491
1492 static abbrev_table_up abbrev_table_read_table
1493 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1494 sect_offset);
1495
1496 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1497
1498 static struct partial_die_info *load_partial_dies
1499 (const struct die_reader_specs *, const gdb_byte *, int);
1500
1501 /* A pair of partial_die_info and compilation unit. */
1502 struct cu_partial_die_info
1503 {
1504 /* The compilation unit of the partial_die_info. */
1505 struct dwarf2_cu *cu;
1506 /* A partial_die_info. */
1507 struct partial_die_info *pdi;
1508
1509 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1510 : cu (cu),
1511 pdi (pdi)
1512 { /* Nothing. */ }
1513
1514 private:
1515 cu_partial_die_info () = delete;
1516 };
1517
1518 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1519 struct dwarf2_cu *);
1520
1521 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1522 struct attribute *, struct attr_abbrev *,
1523 const gdb_byte *);
1524
1525 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1526
1527 static int read_1_signed_byte (bfd *, const gdb_byte *);
1528
1529 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1530
1531 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1532 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1533
1534 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1535
1536 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1537
1538 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1539 unsigned int *);
1540
1541 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1542
1543 static LONGEST read_checked_initial_length_and_offset
1544 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1545 unsigned int *, unsigned int *);
1546
1547 static LONGEST read_offset (bfd *, const gdb_byte *,
1548 const struct comp_unit_head *,
1549 unsigned int *);
1550
1551 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1552
1553 static sect_offset read_abbrev_offset
1554 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1555 struct dwarf2_section_info *, sect_offset);
1556
1557 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1558
1559 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1560
1561 static const char *read_indirect_string
1562 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1563 const struct comp_unit_head *, unsigned int *);
1564
1565 static const char *read_indirect_line_string
1566 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1567 const struct comp_unit_head *, unsigned int *);
1568
1569 static const char *read_indirect_string_at_offset
1570 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1571 LONGEST str_offset);
1572
1573 static const char *read_indirect_string_from_dwz
1574 (struct objfile *objfile, struct dwz_file *, LONGEST);
1575
1576 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1577
1578 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1579 const gdb_byte *,
1580 unsigned int *);
1581
1582 static const char *read_str_index (const struct die_reader_specs *reader,
1583 ULONGEST str_index);
1584
1585 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1586
1587 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1588 struct dwarf2_cu *);
1589
1590 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1591 unsigned int);
1592
1593 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1594 struct dwarf2_cu *cu);
1595
1596 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1597
1598 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1599 struct dwarf2_cu *cu);
1600
1601 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1602
1603 static struct die_info *die_specification (struct die_info *die,
1604 struct dwarf2_cu **);
1605
1606 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1607 struct dwarf2_cu *cu);
1608
1609 static void dwarf_decode_lines (struct line_header *, const char *,
1610 struct dwarf2_cu *, struct partial_symtab *,
1611 CORE_ADDR, int decode_mapping);
1612
1613 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1614 const char *);
1615
1616 static struct symbol *new_symbol (struct die_info *, struct type *,
1617 struct dwarf2_cu *, struct symbol * = NULL);
1618
1619 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1620 struct dwarf2_cu *);
1621
1622 static void dwarf2_const_value_attr (const struct attribute *attr,
1623 struct type *type,
1624 const char *name,
1625 struct obstack *obstack,
1626 struct dwarf2_cu *cu, LONGEST *value,
1627 const gdb_byte **bytes,
1628 struct dwarf2_locexpr_baton **baton);
1629
1630 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1631
1632 static int need_gnat_info (struct dwarf2_cu *);
1633
1634 static struct type *die_descriptive_type (struct die_info *,
1635 struct dwarf2_cu *);
1636
1637 static void set_descriptive_type (struct type *, struct die_info *,
1638 struct dwarf2_cu *);
1639
1640 static struct type *die_containing_type (struct die_info *,
1641 struct dwarf2_cu *);
1642
1643 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1644 struct dwarf2_cu *);
1645
1646 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1647
1648 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1649
1650 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1651
1652 static char *typename_concat (struct obstack *obs, const char *prefix,
1653 const char *suffix, int physname,
1654 struct dwarf2_cu *cu);
1655
1656 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1657
1658 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1659
1660 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1661
1662 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1663
1664 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1665
1666 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1667
1668 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1669 struct dwarf2_cu *, struct partial_symtab *);
1670
1671 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1672 values. Keep the items ordered with increasing constraints compliance. */
1673 enum pc_bounds_kind
1674 {
1675 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1676 PC_BOUNDS_NOT_PRESENT,
1677
1678 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1679 were present but they do not form a valid range of PC addresses. */
1680 PC_BOUNDS_INVALID,
1681
1682 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1683 PC_BOUNDS_RANGES,
1684
1685 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1686 PC_BOUNDS_HIGH_LOW,
1687 };
1688
1689 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1690 CORE_ADDR *, CORE_ADDR *,
1691 struct dwarf2_cu *,
1692 struct partial_symtab *);
1693
1694 static void get_scope_pc_bounds (struct die_info *,
1695 CORE_ADDR *, CORE_ADDR *,
1696 struct dwarf2_cu *);
1697
1698 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1699 CORE_ADDR, struct dwarf2_cu *);
1700
1701 static void dwarf2_add_field (struct field_info *, struct die_info *,
1702 struct dwarf2_cu *);
1703
1704 static void dwarf2_attach_fields_to_type (struct field_info *,
1705 struct type *, struct dwarf2_cu *);
1706
1707 static void dwarf2_add_member_fn (struct field_info *,
1708 struct die_info *, struct type *,
1709 struct dwarf2_cu *);
1710
1711 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1712 struct type *,
1713 struct dwarf2_cu *);
1714
1715 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1716
1717 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1718
1719 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1720
1721 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1722
1723 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1724
1725 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1726
1727 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1728
1729 static struct type *read_module_type (struct die_info *die,
1730 struct dwarf2_cu *cu);
1731
1732 static const char *namespace_name (struct die_info *die,
1733 int *is_anonymous, struct dwarf2_cu *);
1734
1735 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1736
1737 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1738
1739 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1740 struct dwarf2_cu *);
1741
1742 static struct die_info *read_die_and_siblings_1
1743 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1744 struct die_info *);
1745
1746 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1747 const gdb_byte *info_ptr,
1748 const gdb_byte **new_info_ptr,
1749 struct die_info *parent);
1750
1751 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1752 struct die_info **, const gdb_byte *,
1753 int *, int);
1754
1755 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1756 struct die_info **, const gdb_byte *,
1757 int *);
1758
1759 static void process_die (struct die_info *, struct dwarf2_cu *);
1760
1761 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1762 struct obstack *);
1763
1764 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1765
1766 static const char *dwarf2_full_name (const char *name,
1767 struct die_info *die,
1768 struct dwarf2_cu *cu);
1769
1770 static const char *dwarf2_physname (const char *name, struct die_info *die,
1771 struct dwarf2_cu *cu);
1772
1773 static struct die_info *dwarf2_extension (struct die_info *die,
1774 struct dwarf2_cu **);
1775
1776 static const char *dwarf_tag_name (unsigned int);
1777
1778 static const char *dwarf_attr_name (unsigned int);
1779
1780 static const char *dwarf_unit_type_name (int unit_type);
1781
1782 static const char *dwarf_form_name (unsigned int);
1783
1784 static const char *dwarf_bool_name (unsigned int);
1785
1786 static const char *dwarf_type_encoding_name (unsigned int);
1787
1788 static struct die_info *sibling_die (struct die_info *);
1789
1790 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1791
1792 static void dump_die_for_error (struct die_info *);
1793
1794 static void dump_die_1 (struct ui_file *, int level, int max_level,
1795 struct die_info *);
1796
1797 /*static*/ void dump_die (struct die_info *, int max_level);
1798
1799 static void store_in_ref_table (struct die_info *,
1800 struct dwarf2_cu *);
1801
1802 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1803
1804 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1805
1806 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1807 const struct attribute *,
1808 struct dwarf2_cu **);
1809
1810 static struct die_info *follow_die_ref (struct die_info *,
1811 const struct attribute *,
1812 struct dwarf2_cu **);
1813
1814 static struct die_info *follow_die_sig (struct die_info *,
1815 const struct attribute *,
1816 struct dwarf2_cu **);
1817
1818 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1819 struct dwarf2_cu *);
1820
1821 static struct type *get_DW_AT_signature_type (struct die_info *,
1822 const struct attribute *,
1823 struct dwarf2_cu *);
1824
1825 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1826
1827 static void read_signatured_type (struct signatured_type *);
1828
1829 static int attr_to_dynamic_prop (const struct attribute *attr,
1830 struct die_info *die, struct dwarf2_cu *cu,
1831 struct dynamic_prop *prop, struct type *type);
1832
1833 /* memory allocation interface */
1834
1835 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1836
1837 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1838
1839 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1840
1841 static int attr_form_is_block (const struct attribute *);
1842
1843 static int attr_form_is_section_offset (const struct attribute *);
1844
1845 static int attr_form_is_constant (const struct attribute *);
1846
1847 static int attr_form_is_ref (const struct attribute *);
1848
1849 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1850 struct dwarf2_loclist_baton *baton,
1851 const struct attribute *attr);
1852
1853 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1854 struct symbol *sym,
1855 struct dwarf2_cu *cu,
1856 int is_block);
1857
1858 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1859 const gdb_byte *info_ptr,
1860 struct abbrev_info *abbrev);
1861
1862 static hashval_t partial_die_hash (const void *item);
1863
1864 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1865
1866 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1867 (sect_offset sect_off, unsigned int offset_in_dwz,
1868 struct dwarf2_per_objfile *dwarf2_per_objfile);
1869
1870 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1871 struct die_info *comp_unit_die,
1872 enum language pretend_language);
1873
1874 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1875
1876 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1877
1878 static struct type *set_die_type (struct die_info *, struct type *,
1879 struct dwarf2_cu *);
1880
1881 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1882
1883 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1884
1885 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1886 enum language);
1887
1888 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1889 enum language);
1890
1891 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1892 enum language);
1893
1894 static void dwarf2_add_dependence (struct dwarf2_cu *,
1895 struct dwarf2_per_cu_data *);
1896
1897 static void dwarf2_mark (struct dwarf2_cu *);
1898
1899 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1900
1901 static struct type *get_die_type_at_offset (sect_offset,
1902 struct dwarf2_per_cu_data *);
1903
1904 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1905
1906 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1907 enum language pretend_language);
1908
1909 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1910
1911 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1912 static struct type *dwarf2_per_cu_addr_sized_int_type
1913 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1914 static struct type *dwarf2_per_cu_int_type
1915 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1916 bool unsigned_p);
1917
1918 /* Class, the destructor of which frees all allocated queue entries. This
1919 will only have work to do if an error was thrown while processing the
1920 dwarf. If no error was thrown then the queue entries should have all
1921 been processed, and freed, as we went along. */
1922
1923 class dwarf2_queue_guard
1924 {
1925 public:
1926 dwarf2_queue_guard () = default;
1927
1928 /* Free any entries remaining on the queue. There should only be
1929 entries left if we hit an error while processing the dwarf. */
1930 ~dwarf2_queue_guard ()
1931 {
1932 struct dwarf2_queue_item *item, *last;
1933
1934 item = dwarf2_queue;
1935 while (item)
1936 {
1937 /* Anything still marked queued is likely to be in an
1938 inconsistent state, so discard it. */
1939 if (item->per_cu->queued)
1940 {
1941 if (item->per_cu->cu != NULL)
1942 free_one_cached_comp_unit (item->per_cu);
1943 item->per_cu->queued = 0;
1944 }
1945
1946 last = item;
1947 item = item->next;
1948 xfree (last);
1949 }
1950
1951 dwarf2_queue = dwarf2_queue_tail = NULL;
1952 }
1953 };
1954
1955 /* The return type of find_file_and_directory. Note, the enclosed
1956 string pointers are only valid while this object is valid. */
1957
1958 struct file_and_directory
1959 {
1960 /* The filename. This is never NULL. */
1961 const char *name;
1962
1963 /* The compilation directory. NULL if not known. If we needed to
1964 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1965 points directly to the DW_AT_comp_dir string attribute owned by
1966 the obstack that owns the DIE. */
1967 const char *comp_dir;
1968
1969 /* If we needed to build a new string for comp_dir, this is what
1970 owns the storage. */
1971 std::string comp_dir_storage;
1972 };
1973
1974 static file_and_directory find_file_and_directory (struct die_info *die,
1975 struct dwarf2_cu *cu);
1976
1977 static char *file_full_name (int file, struct line_header *lh,
1978 const char *comp_dir);
1979
1980 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1981 enum class rcuh_kind { COMPILE, TYPE };
1982
1983 static const gdb_byte *read_and_check_comp_unit_head
1984 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1985 struct comp_unit_head *header,
1986 struct dwarf2_section_info *section,
1987 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1988 rcuh_kind section_kind);
1989
1990 static void init_cutu_and_read_dies
1991 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1992 int use_existing_cu, int keep, bool skip_partial,
1993 die_reader_func_ftype *die_reader_func, void *data);
1994
1995 static void init_cutu_and_read_dies_simple
1996 (struct dwarf2_per_cu_data *this_cu,
1997 die_reader_func_ftype *die_reader_func, void *data);
1998
1999 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2000
2001 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2002
2003 static struct dwo_unit *lookup_dwo_unit_in_dwp
2004 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2005 struct dwp_file *dwp_file, const char *comp_dir,
2006 ULONGEST signature, int is_debug_types);
2007
2008 static struct dwp_file *get_dwp_file
2009 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2010
2011 static struct dwo_unit *lookup_dwo_comp_unit
2012 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2013
2014 static struct dwo_unit *lookup_dwo_type_unit
2015 (struct signatured_type *, const char *, const char *);
2016
2017 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2018
2019 /* A unique pointer to a dwo_file. */
2020
2021 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2022
2023 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2024
2025 static void check_producer (struct dwarf2_cu *cu);
2026
2027 static void free_line_header_voidp (void *arg);
2028 \f
2029 /* Various complaints about symbol reading that don't abort the process. */
2030
2031 static void
2032 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2033 {
2034 complaint (_("statement list doesn't fit in .debug_line section"));
2035 }
2036
2037 static void
2038 dwarf2_debug_line_missing_file_complaint (void)
2039 {
2040 complaint (_(".debug_line section has line data without a file"));
2041 }
2042
2043 static void
2044 dwarf2_debug_line_missing_end_sequence_complaint (void)
2045 {
2046 complaint (_(".debug_line section has line "
2047 "program sequence without an end"));
2048 }
2049
2050 static void
2051 dwarf2_complex_location_expr_complaint (void)
2052 {
2053 complaint (_("location expression too complex"));
2054 }
2055
2056 static void
2057 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2058 int arg3)
2059 {
2060 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2061 arg1, arg2, arg3);
2062 }
2063
2064 static void
2065 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2066 {
2067 complaint (_("debug info runs off end of %s section"
2068 " [in module %s]"),
2069 get_section_name (section),
2070 get_section_file_name (section));
2071 }
2072
2073 static void
2074 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2075 {
2076 complaint (_("macro debug info contains a "
2077 "malformed macro definition:\n`%s'"),
2078 arg1);
2079 }
2080
2081 static void
2082 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2083 {
2084 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2085 arg1, arg2);
2086 }
2087
2088 /* Hash function for line_header_hash. */
2089
2090 static hashval_t
2091 line_header_hash (const struct line_header *ofs)
2092 {
2093 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2094 }
2095
2096 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2097
2098 static hashval_t
2099 line_header_hash_voidp (const void *item)
2100 {
2101 const struct line_header *ofs = (const struct line_header *) item;
2102
2103 return line_header_hash (ofs);
2104 }
2105
2106 /* Equality function for line_header_hash. */
2107
2108 static int
2109 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2110 {
2111 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2112 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2113
2114 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2115 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2116 }
2117
2118 \f
2119
2120 /* Read the given attribute value as an address, taking the attribute's
2121 form into account. */
2122
2123 static CORE_ADDR
2124 attr_value_as_address (struct attribute *attr)
2125 {
2126 CORE_ADDR addr;
2127
2128 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2129 && attr->form != DW_FORM_GNU_addr_index)
2130 {
2131 /* Aside from a few clearly defined exceptions, attributes that
2132 contain an address must always be in DW_FORM_addr form.
2133 Unfortunately, some compilers happen to be violating this
2134 requirement by encoding addresses using other forms, such
2135 as DW_FORM_data4 for example. For those broken compilers,
2136 we try to do our best, without any guarantee of success,
2137 to interpret the address correctly. It would also be nice
2138 to generate a complaint, but that would require us to maintain
2139 a list of legitimate cases where a non-address form is allowed,
2140 as well as update callers to pass in at least the CU's DWARF
2141 version. This is more overhead than what we're willing to
2142 expand for a pretty rare case. */
2143 addr = DW_UNSND (attr);
2144 }
2145 else
2146 addr = DW_ADDR (attr);
2147
2148 return addr;
2149 }
2150
2151 /* See declaration. */
2152
2153 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2154 const dwarf2_debug_sections *names,
2155 bool can_copy_)
2156 : objfile (objfile_),
2157 can_copy (can_copy_)
2158 {
2159 if (names == NULL)
2160 names = &dwarf2_elf_names;
2161
2162 bfd *obfd = objfile->obfd;
2163
2164 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2165 locate_sections (obfd, sec, *names);
2166 }
2167
2168 dwarf2_per_objfile::~dwarf2_per_objfile ()
2169 {
2170 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2171 free_cached_comp_units ();
2172
2173 if (quick_file_names_table)
2174 htab_delete (quick_file_names_table);
2175
2176 if (line_header_hash)
2177 htab_delete (line_header_hash);
2178
2179 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2180 per_cu->imported_symtabs_free ();
2181
2182 for (signatured_type *sig_type : all_type_units)
2183 sig_type->per_cu.imported_symtabs_free ();
2184
2185 /* Everything else should be on the objfile obstack. */
2186 }
2187
2188 /* See declaration. */
2189
2190 void
2191 dwarf2_per_objfile::free_cached_comp_units ()
2192 {
2193 dwarf2_per_cu_data *per_cu = read_in_chain;
2194 dwarf2_per_cu_data **last_chain = &read_in_chain;
2195 while (per_cu != NULL)
2196 {
2197 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2198
2199 delete per_cu->cu;
2200 *last_chain = next_cu;
2201 per_cu = next_cu;
2202 }
2203 }
2204
2205 /* A helper class that calls free_cached_comp_units on
2206 destruction. */
2207
2208 class free_cached_comp_units
2209 {
2210 public:
2211
2212 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2213 : m_per_objfile (per_objfile)
2214 {
2215 }
2216
2217 ~free_cached_comp_units ()
2218 {
2219 m_per_objfile->free_cached_comp_units ();
2220 }
2221
2222 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2223
2224 private:
2225
2226 dwarf2_per_objfile *m_per_objfile;
2227 };
2228
2229 /* Try to locate the sections we need for DWARF 2 debugging
2230 information and return true if we have enough to do something.
2231 NAMES points to the dwarf2 section names, or is NULL if the standard
2232 ELF names are used. CAN_COPY is true for formats where symbol
2233 interposition is possible and so symbol values must follow copy
2234 relocation rules. */
2235
2236 int
2237 dwarf2_has_info (struct objfile *objfile,
2238 const struct dwarf2_debug_sections *names,
2239 bool can_copy)
2240 {
2241 if (objfile->flags & OBJF_READNEVER)
2242 return 0;
2243
2244 struct dwarf2_per_objfile *dwarf2_per_objfile
2245 = get_dwarf2_per_objfile (objfile);
2246
2247 if (dwarf2_per_objfile == NULL)
2248 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2249 names,
2250 can_copy);
2251
2252 return (!dwarf2_per_objfile->info.is_virtual
2253 && dwarf2_per_objfile->info.s.section != NULL
2254 && !dwarf2_per_objfile->abbrev.is_virtual
2255 && dwarf2_per_objfile->abbrev.s.section != NULL);
2256 }
2257
2258 /* Return the containing section of virtual section SECTION. */
2259
2260 static struct dwarf2_section_info *
2261 get_containing_section (const struct dwarf2_section_info *section)
2262 {
2263 gdb_assert (section->is_virtual);
2264 return section->s.containing_section;
2265 }
2266
2267 /* Return the bfd owner of SECTION. */
2268
2269 static struct bfd *
2270 get_section_bfd_owner (const struct dwarf2_section_info *section)
2271 {
2272 if (section->is_virtual)
2273 {
2274 section = get_containing_section (section);
2275 gdb_assert (!section->is_virtual);
2276 }
2277 return section->s.section->owner;
2278 }
2279
2280 /* Return the bfd section of SECTION.
2281 Returns NULL if the section is not present. */
2282
2283 static asection *
2284 get_section_bfd_section (const struct dwarf2_section_info *section)
2285 {
2286 if (section->is_virtual)
2287 {
2288 section = get_containing_section (section);
2289 gdb_assert (!section->is_virtual);
2290 }
2291 return section->s.section;
2292 }
2293
2294 /* Return the name of SECTION. */
2295
2296 static const char *
2297 get_section_name (const struct dwarf2_section_info *section)
2298 {
2299 asection *sectp = get_section_bfd_section (section);
2300
2301 gdb_assert (sectp != NULL);
2302 return bfd_section_name (sectp);
2303 }
2304
2305 /* Return the name of the file SECTION is in. */
2306
2307 static const char *
2308 get_section_file_name (const struct dwarf2_section_info *section)
2309 {
2310 bfd *abfd = get_section_bfd_owner (section);
2311
2312 return bfd_get_filename (abfd);
2313 }
2314
2315 /* Return the id of SECTION.
2316 Returns 0 if SECTION doesn't exist. */
2317
2318 static int
2319 get_section_id (const struct dwarf2_section_info *section)
2320 {
2321 asection *sectp = get_section_bfd_section (section);
2322
2323 if (sectp == NULL)
2324 return 0;
2325 return sectp->id;
2326 }
2327
2328 /* Return the flags of SECTION.
2329 SECTION (or containing section if this is a virtual section) must exist. */
2330
2331 static int
2332 get_section_flags (const struct dwarf2_section_info *section)
2333 {
2334 asection *sectp = get_section_bfd_section (section);
2335
2336 gdb_assert (sectp != NULL);
2337 return bfd_section_flags (sectp);
2338 }
2339
2340 /* When loading sections, we look either for uncompressed section or for
2341 compressed section names. */
2342
2343 static int
2344 section_is_p (const char *section_name,
2345 const struct dwarf2_section_names *names)
2346 {
2347 if (names->normal != NULL
2348 && strcmp (section_name, names->normal) == 0)
2349 return 1;
2350 if (names->compressed != NULL
2351 && strcmp (section_name, names->compressed) == 0)
2352 return 1;
2353 return 0;
2354 }
2355
2356 /* See declaration. */
2357
2358 void
2359 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2360 const dwarf2_debug_sections &names)
2361 {
2362 flagword aflag = bfd_section_flags (sectp);
2363
2364 if ((aflag & SEC_HAS_CONTENTS) == 0)
2365 {
2366 }
2367 else if (elf_section_data (sectp)->this_hdr.sh_size
2368 > bfd_get_file_size (abfd))
2369 {
2370 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2371 warning (_("Discarding section %s which has a section size (%s"
2372 ") larger than the file size [in module %s]"),
2373 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2374 bfd_get_filename (abfd));
2375 }
2376 else if (section_is_p (sectp->name, &names.info))
2377 {
2378 this->info.s.section = sectp;
2379 this->info.size = bfd_section_size (sectp);
2380 }
2381 else if (section_is_p (sectp->name, &names.abbrev))
2382 {
2383 this->abbrev.s.section = sectp;
2384 this->abbrev.size = bfd_section_size (sectp);
2385 }
2386 else if (section_is_p (sectp->name, &names.line))
2387 {
2388 this->line.s.section = sectp;
2389 this->line.size = bfd_section_size (sectp);
2390 }
2391 else if (section_is_p (sectp->name, &names.loc))
2392 {
2393 this->loc.s.section = sectp;
2394 this->loc.size = bfd_section_size (sectp);
2395 }
2396 else if (section_is_p (sectp->name, &names.loclists))
2397 {
2398 this->loclists.s.section = sectp;
2399 this->loclists.size = bfd_section_size (sectp);
2400 }
2401 else if (section_is_p (sectp->name, &names.macinfo))
2402 {
2403 this->macinfo.s.section = sectp;
2404 this->macinfo.size = bfd_section_size (sectp);
2405 }
2406 else if (section_is_p (sectp->name, &names.macro))
2407 {
2408 this->macro.s.section = sectp;
2409 this->macro.size = bfd_section_size (sectp);
2410 }
2411 else if (section_is_p (sectp->name, &names.str))
2412 {
2413 this->str.s.section = sectp;
2414 this->str.size = bfd_section_size (sectp);
2415 }
2416 else if (section_is_p (sectp->name, &names.line_str))
2417 {
2418 this->line_str.s.section = sectp;
2419 this->line_str.size = bfd_section_size (sectp);
2420 }
2421 else if (section_is_p (sectp->name, &names.addr))
2422 {
2423 this->addr.s.section = sectp;
2424 this->addr.size = bfd_section_size (sectp);
2425 }
2426 else if (section_is_p (sectp->name, &names.frame))
2427 {
2428 this->frame.s.section = sectp;
2429 this->frame.size = bfd_section_size (sectp);
2430 }
2431 else if (section_is_p (sectp->name, &names.eh_frame))
2432 {
2433 this->eh_frame.s.section = sectp;
2434 this->eh_frame.size = bfd_section_size (sectp);
2435 }
2436 else if (section_is_p (sectp->name, &names.ranges))
2437 {
2438 this->ranges.s.section = sectp;
2439 this->ranges.size = bfd_section_size (sectp);
2440 }
2441 else if (section_is_p (sectp->name, &names.rnglists))
2442 {
2443 this->rnglists.s.section = sectp;
2444 this->rnglists.size = bfd_section_size (sectp);
2445 }
2446 else if (section_is_p (sectp->name, &names.types))
2447 {
2448 struct dwarf2_section_info type_section;
2449
2450 memset (&type_section, 0, sizeof (type_section));
2451 type_section.s.section = sectp;
2452 type_section.size = bfd_section_size (sectp);
2453
2454 this->types.push_back (type_section);
2455 }
2456 else if (section_is_p (sectp->name, &names.gdb_index))
2457 {
2458 this->gdb_index.s.section = sectp;
2459 this->gdb_index.size = bfd_section_size (sectp);
2460 }
2461 else if (section_is_p (sectp->name, &names.debug_names))
2462 {
2463 this->debug_names.s.section = sectp;
2464 this->debug_names.size = bfd_section_size (sectp);
2465 }
2466 else if (section_is_p (sectp->name, &names.debug_aranges))
2467 {
2468 this->debug_aranges.s.section = sectp;
2469 this->debug_aranges.size = bfd_section_size (sectp);
2470 }
2471
2472 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2473 && bfd_section_vma (sectp) == 0)
2474 this->has_section_at_zero = true;
2475 }
2476
2477 /* A helper function that decides whether a section is empty,
2478 or not present. */
2479
2480 static int
2481 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2482 {
2483 if (section->is_virtual)
2484 return section->size == 0;
2485 return section->s.section == NULL || section->size == 0;
2486 }
2487
2488 /* See dwarf2read.h. */
2489
2490 void
2491 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2492 {
2493 asection *sectp;
2494 bfd *abfd;
2495 gdb_byte *buf, *retbuf;
2496
2497 if (info->readin)
2498 return;
2499 info->buffer = NULL;
2500 info->readin = true;
2501
2502 if (dwarf2_section_empty_p (info))
2503 return;
2504
2505 sectp = get_section_bfd_section (info);
2506
2507 /* If this is a virtual section we need to read in the real one first. */
2508 if (info->is_virtual)
2509 {
2510 struct dwarf2_section_info *containing_section =
2511 get_containing_section (info);
2512
2513 gdb_assert (sectp != NULL);
2514 if ((sectp->flags & SEC_RELOC) != 0)
2515 {
2516 error (_("Dwarf Error: DWP format V2 with relocations is not"
2517 " supported in section %s [in module %s]"),
2518 get_section_name (info), get_section_file_name (info));
2519 }
2520 dwarf2_read_section (objfile, containing_section);
2521 /* Other code should have already caught virtual sections that don't
2522 fit. */
2523 gdb_assert (info->virtual_offset + info->size
2524 <= containing_section->size);
2525 /* If the real section is empty or there was a problem reading the
2526 section we shouldn't get here. */
2527 gdb_assert (containing_section->buffer != NULL);
2528 info->buffer = containing_section->buffer + info->virtual_offset;
2529 return;
2530 }
2531
2532 /* If the section has relocations, we must read it ourselves.
2533 Otherwise we attach it to the BFD. */
2534 if ((sectp->flags & SEC_RELOC) == 0)
2535 {
2536 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2537 return;
2538 }
2539
2540 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2541 info->buffer = buf;
2542
2543 /* When debugging .o files, we may need to apply relocations; see
2544 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2545 We never compress sections in .o files, so we only need to
2546 try this when the section is not compressed. */
2547 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2548 if (retbuf != NULL)
2549 {
2550 info->buffer = retbuf;
2551 return;
2552 }
2553
2554 abfd = get_section_bfd_owner (info);
2555 gdb_assert (abfd != NULL);
2556
2557 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2558 || bfd_bread (buf, info->size, abfd) != info->size)
2559 {
2560 error (_("Dwarf Error: Can't read DWARF data"
2561 " in section %s [in module %s]"),
2562 bfd_section_name (sectp), bfd_get_filename (abfd));
2563 }
2564 }
2565
2566 /* A helper function that returns the size of a section in a safe way.
2567 If you are positive that the section has been read before using the
2568 size, then it is safe to refer to the dwarf2_section_info object's
2569 "size" field directly. In other cases, you must call this
2570 function, because for compressed sections the size field is not set
2571 correctly until the section has been read. */
2572
2573 static bfd_size_type
2574 dwarf2_section_size (struct objfile *objfile,
2575 struct dwarf2_section_info *info)
2576 {
2577 if (!info->readin)
2578 dwarf2_read_section (objfile, info);
2579 return info->size;
2580 }
2581
2582 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2583 SECTION_NAME. */
2584
2585 void
2586 dwarf2_get_section_info (struct objfile *objfile,
2587 enum dwarf2_section_enum sect,
2588 asection **sectp, const gdb_byte **bufp,
2589 bfd_size_type *sizep)
2590 {
2591 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2592 struct dwarf2_section_info *info;
2593
2594 /* We may see an objfile without any DWARF, in which case we just
2595 return nothing. */
2596 if (data == NULL)
2597 {
2598 *sectp = NULL;
2599 *bufp = NULL;
2600 *sizep = 0;
2601 return;
2602 }
2603 switch (sect)
2604 {
2605 case DWARF2_DEBUG_FRAME:
2606 info = &data->frame;
2607 break;
2608 case DWARF2_EH_FRAME:
2609 info = &data->eh_frame;
2610 break;
2611 default:
2612 gdb_assert_not_reached ("unexpected section");
2613 }
2614
2615 dwarf2_read_section (objfile, info);
2616
2617 *sectp = get_section_bfd_section (info);
2618 *bufp = info->buffer;
2619 *sizep = info->size;
2620 }
2621
2622 /* A helper function to find the sections for a .dwz file. */
2623
2624 static void
2625 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2626 {
2627 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2628
2629 /* Note that we only support the standard ELF names, because .dwz
2630 is ELF-only (at the time of writing). */
2631 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2632 {
2633 dwz_file->abbrev.s.section = sectp;
2634 dwz_file->abbrev.size = bfd_section_size (sectp);
2635 }
2636 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2637 {
2638 dwz_file->info.s.section = sectp;
2639 dwz_file->info.size = bfd_section_size (sectp);
2640 }
2641 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2642 {
2643 dwz_file->str.s.section = sectp;
2644 dwz_file->str.size = bfd_section_size (sectp);
2645 }
2646 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2647 {
2648 dwz_file->line.s.section = sectp;
2649 dwz_file->line.size = bfd_section_size (sectp);
2650 }
2651 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2652 {
2653 dwz_file->macro.s.section = sectp;
2654 dwz_file->macro.size = bfd_section_size (sectp);
2655 }
2656 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2657 {
2658 dwz_file->gdb_index.s.section = sectp;
2659 dwz_file->gdb_index.size = bfd_section_size (sectp);
2660 }
2661 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2662 {
2663 dwz_file->debug_names.s.section = sectp;
2664 dwz_file->debug_names.size = bfd_section_size (sectp);
2665 }
2666 }
2667
2668 /* See dwarf2read.h. */
2669
2670 struct dwz_file *
2671 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2672 {
2673 const char *filename;
2674 bfd_size_type buildid_len_arg;
2675 size_t buildid_len;
2676 bfd_byte *buildid;
2677
2678 if (dwarf2_per_objfile->dwz_file != NULL)
2679 return dwarf2_per_objfile->dwz_file.get ();
2680
2681 bfd_set_error (bfd_error_no_error);
2682 gdb::unique_xmalloc_ptr<char> data
2683 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2684 &buildid_len_arg, &buildid));
2685 if (data == NULL)
2686 {
2687 if (bfd_get_error () == bfd_error_no_error)
2688 return NULL;
2689 error (_("could not read '.gnu_debugaltlink' section: %s"),
2690 bfd_errmsg (bfd_get_error ()));
2691 }
2692
2693 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2694
2695 buildid_len = (size_t) buildid_len_arg;
2696
2697 filename = data.get ();
2698
2699 std::string abs_storage;
2700 if (!IS_ABSOLUTE_PATH (filename))
2701 {
2702 gdb::unique_xmalloc_ptr<char> abs
2703 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2704
2705 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2706 filename = abs_storage.c_str ();
2707 }
2708
2709 /* First try the file name given in the section. If that doesn't
2710 work, try to use the build-id instead. */
2711 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2712 if (dwz_bfd != NULL)
2713 {
2714 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2715 dwz_bfd.reset (nullptr);
2716 }
2717
2718 if (dwz_bfd == NULL)
2719 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2720
2721 if (dwz_bfd == NULL)
2722 error (_("could not find '.gnu_debugaltlink' file for %s"),
2723 objfile_name (dwarf2_per_objfile->objfile));
2724
2725 std::unique_ptr<struct dwz_file> result
2726 (new struct dwz_file (std::move (dwz_bfd)));
2727
2728 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2729 result.get ());
2730
2731 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2732 result->dwz_bfd.get ());
2733 dwarf2_per_objfile->dwz_file = std::move (result);
2734 return dwarf2_per_objfile->dwz_file.get ();
2735 }
2736 \f
2737 /* DWARF quick_symbols_functions support. */
2738
2739 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2740 unique line tables, so we maintain a separate table of all .debug_line
2741 derived entries to support the sharing.
2742 All the quick functions need is the list of file names. We discard the
2743 line_header when we're done and don't need to record it here. */
2744 struct quick_file_names
2745 {
2746 /* The data used to construct the hash key. */
2747 struct stmt_list_hash hash;
2748
2749 /* The number of entries in file_names, real_names. */
2750 unsigned int num_file_names;
2751
2752 /* The file names from the line table, after being run through
2753 file_full_name. */
2754 const char **file_names;
2755
2756 /* The file names from the line table after being run through
2757 gdb_realpath. These are computed lazily. */
2758 const char **real_names;
2759 };
2760
2761 /* When using the index (and thus not using psymtabs), each CU has an
2762 object of this type. This is used to hold information needed by
2763 the various "quick" methods. */
2764 struct dwarf2_per_cu_quick_data
2765 {
2766 /* The file table. This can be NULL if there was no file table
2767 or it's currently not read in.
2768 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2769 struct quick_file_names *file_names;
2770
2771 /* The corresponding symbol table. This is NULL if symbols for this
2772 CU have not yet been read. */
2773 struct compunit_symtab *compunit_symtab;
2774
2775 /* A temporary mark bit used when iterating over all CUs in
2776 expand_symtabs_matching. */
2777 unsigned int mark : 1;
2778
2779 /* True if we've tried to read the file table and found there isn't one.
2780 There will be no point in trying to read it again next time. */
2781 unsigned int no_file_data : 1;
2782 };
2783
2784 /* Utility hash function for a stmt_list_hash. */
2785
2786 static hashval_t
2787 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2788 {
2789 hashval_t v = 0;
2790
2791 if (stmt_list_hash->dwo_unit != NULL)
2792 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2793 v += to_underlying (stmt_list_hash->line_sect_off);
2794 return v;
2795 }
2796
2797 /* Utility equality function for a stmt_list_hash. */
2798
2799 static int
2800 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2801 const struct stmt_list_hash *rhs)
2802 {
2803 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2804 return 0;
2805 if (lhs->dwo_unit != NULL
2806 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2807 return 0;
2808
2809 return lhs->line_sect_off == rhs->line_sect_off;
2810 }
2811
2812 /* Hash function for a quick_file_names. */
2813
2814 static hashval_t
2815 hash_file_name_entry (const void *e)
2816 {
2817 const struct quick_file_names *file_data
2818 = (const struct quick_file_names *) e;
2819
2820 return hash_stmt_list_entry (&file_data->hash);
2821 }
2822
2823 /* Equality function for a quick_file_names. */
2824
2825 static int
2826 eq_file_name_entry (const void *a, const void *b)
2827 {
2828 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2829 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2830
2831 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2832 }
2833
2834 /* Delete function for a quick_file_names. */
2835
2836 static void
2837 delete_file_name_entry (void *e)
2838 {
2839 struct quick_file_names *file_data = (struct quick_file_names *) e;
2840 int i;
2841
2842 for (i = 0; i < file_data->num_file_names; ++i)
2843 {
2844 xfree ((void*) file_data->file_names[i]);
2845 if (file_data->real_names)
2846 xfree ((void*) file_data->real_names[i]);
2847 }
2848
2849 /* The space for the struct itself lives on objfile_obstack,
2850 so we don't free it here. */
2851 }
2852
2853 /* Create a quick_file_names hash table. */
2854
2855 static htab_t
2856 create_quick_file_names_table (unsigned int nr_initial_entries)
2857 {
2858 return htab_create_alloc (nr_initial_entries,
2859 hash_file_name_entry, eq_file_name_entry,
2860 delete_file_name_entry, xcalloc, xfree);
2861 }
2862
2863 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2864 have to be created afterwards. You should call age_cached_comp_units after
2865 processing PER_CU->CU. dw2_setup must have been already called. */
2866
2867 static void
2868 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2869 {
2870 if (per_cu->is_debug_types)
2871 load_full_type_unit (per_cu);
2872 else
2873 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2874
2875 if (per_cu->cu == NULL)
2876 return; /* Dummy CU. */
2877
2878 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2879 }
2880
2881 /* Read in the symbols for PER_CU. */
2882
2883 static void
2884 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2885 {
2886 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2887
2888 /* Skip type_unit_groups, reading the type units they contain
2889 is handled elsewhere. */
2890 if (IS_TYPE_UNIT_GROUP (per_cu))
2891 return;
2892
2893 /* The destructor of dwarf2_queue_guard frees any entries left on
2894 the queue. After this point we're guaranteed to leave this function
2895 with the dwarf queue empty. */
2896 dwarf2_queue_guard q_guard;
2897
2898 if (dwarf2_per_objfile->using_index
2899 ? per_cu->v.quick->compunit_symtab == NULL
2900 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2901 {
2902 queue_comp_unit (per_cu, language_minimal);
2903 load_cu (per_cu, skip_partial);
2904
2905 /* If we just loaded a CU from a DWO, and we're working with an index
2906 that may badly handle TUs, load all the TUs in that DWO as well.
2907 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2908 if (!per_cu->is_debug_types
2909 && per_cu->cu != NULL
2910 && per_cu->cu->dwo_unit != NULL
2911 && dwarf2_per_objfile->index_table != NULL
2912 && dwarf2_per_objfile->index_table->version <= 7
2913 /* DWP files aren't supported yet. */
2914 && get_dwp_file (dwarf2_per_objfile) == NULL)
2915 queue_and_load_all_dwo_tus (per_cu);
2916 }
2917
2918 process_queue (dwarf2_per_objfile);
2919
2920 /* Age the cache, releasing compilation units that have not
2921 been used recently. */
2922 age_cached_comp_units (dwarf2_per_objfile);
2923 }
2924
2925 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2926 the objfile from which this CU came. Returns the resulting symbol
2927 table. */
2928
2929 static struct compunit_symtab *
2930 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2931 {
2932 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2933
2934 gdb_assert (dwarf2_per_objfile->using_index);
2935 if (!per_cu->v.quick->compunit_symtab)
2936 {
2937 free_cached_comp_units freer (dwarf2_per_objfile);
2938 scoped_restore decrementer = increment_reading_symtab ();
2939 dw2_do_instantiate_symtab (per_cu, skip_partial);
2940 process_cu_includes (dwarf2_per_objfile);
2941 }
2942
2943 return per_cu->v.quick->compunit_symtab;
2944 }
2945
2946 /* See declaration. */
2947
2948 dwarf2_per_cu_data *
2949 dwarf2_per_objfile::get_cutu (int index)
2950 {
2951 if (index >= this->all_comp_units.size ())
2952 {
2953 index -= this->all_comp_units.size ();
2954 gdb_assert (index < this->all_type_units.size ());
2955 return &this->all_type_units[index]->per_cu;
2956 }
2957
2958 return this->all_comp_units[index];
2959 }
2960
2961 /* See declaration. */
2962
2963 dwarf2_per_cu_data *
2964 dwarf2_per_objfile::get_cu (int index)
2965 {
2966 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2967
2968 return this->all_comp_units[index];
2969 }
2970
2971 /* See declaration. */
2972
2973 signatured_type *
2974 dwarf2_per_objfile::get_tu (int index)
2975 {
2976 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2977
2978 return this->all_type_units[index];
2979 }
2980
2981 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2982 objfile_obstack, and constructed with the specified field
2983 values. */
2984
2985 static dwarf2_per_cu_data *
2986 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2987 struct dwarf2_section_info *section,
2988 int is_dwz,
2989 sect_offset sect_off, ULONGEST length)
2990 {
2991 struct objfile *objfile = dwarf2_per_objfile->objfile;
2992 dwarf2_per_cu_data *the_cu
2993 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2994 struct dwarf2_per_cu_data);
2995 the_cu->sect_off = sect_off;
2996 the_cu->length = length;
2997 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2998 the_cu->section = section;
2999 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3000 struct dwarf2_per_cu_quick_data);
3001 the_cu->is_dwz = is_dwz;
3002 return the_cu;
3003 }
3004
3005 /* A helper for create_cus_from_index that handles a given list of
3006 CUs. */
3007
3008 static void
3009 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3010 const gdb_byte *cu_list, offset_type n_elements,
3011 struct dwarf2_section_info *section,
3012 int is_dwz)
3013 {
3014 for (offset_type i = 0; i < n_elements; i += 2)
3015 {
3016 gdb_static_assert (sizeof (ULONGEST) >= 8);
3017
3018 sect_offset sect_off
3019 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3020 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3021 cu_list += 2 * 8;
3022
3023 dwarf2_per_cu_data *per_cu
3024 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3025 sect_off, length);
3026 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3027 }
3028 }
3029
3030 /* Read the CU list from the mapped index, and use it to create all
3031 the CU objects for this objfile. */
3032
3033 static void
3034 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3035 const gdb_byte *cu_list, offset_type cu_list_elements,
3036 const gdb_byte *dwz_list, offset_type dwz_elements)
3037 {
3038 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3039 dwarf2_per_objfile->all_comp_units.reserve
3040 ((cu_list_elements + dwz_elements) / 2);
3041
3042 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3043 &dwarf2_per_objfile->info, 0);
3044
3045 if (dwz_elements == 0)
3046 return;
3047
3048 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3049 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3050 &dwz->info, 1);
3051 }
3052
3053 /* Create the signatured type hash table from the index. */
3054
3055 static void
3056 create_signatured_type_table_from_index
3057 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3058 struct dwarf2_section_info *section,
3059 const gdb_byte *bytes,
3060 offset_type elements)
3061 {
3062 struct objfile *objfile = dwarf2_per_objfile->objfile;
3063
3064 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3065 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3066
3067 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3068
3069 for (offset_type i = 0; i < elements; i += 3)
3070 {
3071 struct signatured_type *sig_type;
3072 ULONGEST signature;
3073 void **slot;
3074 cu_offset type_offset_in_tu;
3075
3076 gdb_static_assert (sizeof (ULONGEST) >= 8);
3077 sect_offset sect_off
3078 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3079 type_offset_in_tu
3080 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3081 BFD_ENDIAN_LITTLE);
3082 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3083 bytes += 3 * 8;
3084
3085 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3086 struct signatured_type);
3087 sig_type->signature = signature;
3088 sig_type->type_offset_in_tu = type_offset_in_tu;
3089 sig_type->per_cu.is_debug_types = 1;
3090 sig_type->per_cu.section = section;
3091 sig_type->per_cu.sect_off = sect_off;
3092 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3093 sig_type->per_cu.v.quick
3094 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3095 struct dwarf2_per_cu_quick_data);
3096
3097 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3098 *slot = sig_type;
3099
3100 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3101 }
3102
3103 dwarf2_per_objfile->signatured_types = sig_types_hash;
3104 }
3105
3106 /* Create the signatured type hash table from .debug_names. */
3107
3108 static void
3109 create_signatured_type_table_from_debug_names
3110 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3111 const mapped_debug_names &map,
3112 struct dwarf2_section_info *section,
3113 struct dwarf2_section_info *abbrev_section)
3114 {
3115 struct objfile *objfile = dwarf2_per_objfile->objfile;
3116
3117 dwarf2_read_section (objfile, section);
3118 dwarf2_read_section (objfile, abbrev_section);
3119
3120 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3121 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3122
3123 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3124
3125 for (uint32_t i = 0; i < map.tu_count; ++i)
3126 {
3127 struct signatured_type *sig_type;
3128 void **slot;
3129
3130 sect_offset sect_off
3131 = (sect_offset) (extract_unsigned_integer
3132 (map.tu_table_reordered + i * map.offset_size,
3133 map.offset_size,
3134 map.dwarf5_byte_order));
3135
3136 comp_unit_head cu_header;
3137 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3138 abbrev_section,
3139 section->buffer + to_underlying (sect_off),
3140 rcuh_kind::TYPE);
3141
3142 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3143 struct signatured_type);
3144 sig_type->signature = cu_header.signature;
3145 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3146 sig_type->per_cu.is_debug_types = 1;
3147 sig_type->per_cu.section = section;
3148 sig_type->per_cu.sect_off = sect_off;
3149 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3150 sig_type->per_cu.v.quick
3151 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3152 struct dwarf2_per_cu_quick_data);
3153
3154 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3155 *slot = sig_type;
3156
3157 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3158 }
3159
3160 dwarf2_per_objfile->signatured_types = sig_types_hash;
3161 }
3162
3163 /* Read the address map data from the mapped index, and use it to
3164 populate the objfile's psymtabs_addrmap. */
3165
3166 static void
3167 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3168 struct mapped_index *index)
3169 {
3170 struct objfile *objfile = dwarf2_per_objfile->objfile;
3171 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3172 const gdb_byte *iter, *end;
3173 struct addrmap *mutable_map;
3174 CORE_ADDR baseaddr;
3175
3176 auto_obstack temp_obstack;
3177
3178 mutable_map = addrmap_create_mutable (&temp_obstack);
3179
3180 iter = index->address_table.data ();
3181 end = iter + index->address_table.size ();
3182
3183 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3184
3185 while (iter < end)
3186 {
3187 ULONGEST hi, lo, cu_index;
3188 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3189 iter += 8;
3190 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3191 iter += 8;
3192 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3193 iter += 4;
3194
3195 if (lo > hi)
3196 {
3197 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3198 hex_string (lo), hex_string (hi));
3199 continue;
3200 }
3201
3202 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3203 {
3204 complaint (_(".gdb_index address table has invalid CU number %u"),
3205 (unsigned) cu_index);
3206 continue;
3207 }
3208
3209 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3210 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3211 addrmap_set_empty (mutable_map, lo, hi - 1,
3212 dwarf2_per_objfile->get_cu (cu_index));
3213 }
3214
3215 objfile->partial_symtabs->psymtabs_addrmap
3216 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3217 }
3218
3219 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3220 populate the objfile's psymtabs_addrmap. */
3221
3222 static void
3223 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3224 struct dwarf2_section_info *section)
3225 {
3226 struct objfile *objfile = dwarf2_per_objfile->objfile;
3227 bfd *abfd = objfile->obfd;
3228 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3229 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3230 SECT_OFF_TEXT (objfile));
3231
3232 auto_obstack temp_obstack;
3233 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3234
3235 std::unordered_map<sect_offset,
3236 dwarf2_per_cu_data *,
3237 gdb::hash_enum<sect_offset>>
3238 debug_info_offset_to_per_cu;
3239 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3240 {
3241 const auto insertpair
3242 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3243 if (!insertpair.second)
3244 {
3245 warning (_("Section .debug_aranges in %s has duplicate "
3246 "debug_info_offset %s, ignoring .debug_aranges."),
3247 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3248 return;
3249 }
3250 }
3251
3252 dwarf2_read_section (objfile, section);
3253
3254 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3255
3256 const gdb_byte *addr = section->buffer;
3257
3258 while (addr < section->buffer + section->size)
3259 {
3260 const gdb_byte *const entry_addr = addr;
3261 unsigned int bytes_read;
3262
3263 const LONGEST entry_length = read_initial_length (abfd, addr,
3264 &bytes_read);
3265 addr += bytes_read;
3266
3267 const gdb_byte *const entry_end = addr + entry_length;
3268 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3269 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3270 if (addr + entry_length > section->buffer + section->size)
3271 {
3272 warning (_("Section .debug_aranges in %s entry at offset %s "
3273 "length %s exceeds section length %s, "
3274 "ignoring .debug_aranges."),
3275 objfile_name (objfile),
3276 plongest (entry_addr - section->buffer),
3277 plongest (bytes_read + entry_length),
3278 pulongest (section->size));
3279 return;
3280 }
3281
3282 /* The version number. */
3283 const uint16_t version = read_2_bytes (abfd, addr);
3284 addr += 2;
3285 if (version != 2)
3286 {
3287 warning (_("Section .debug_aranges in %s entry at offset %s "
3288 "has unsupported version %d, ignoring .debug_aranges."),
3289 objfile_name (objfile),
3290 plongest (entry_addr - section->buffer), version);
3291 return;
3292 }
3293
3294 const uint64_t debug_info_offset
3295 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3296 addr += offset_size;
3297 const auto per_cu_it
3298 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3299 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3300 {
3301 warning (_("Section .debug_aranges in %s entry at offset %s "
3302 "debug_info_offset %s does not exists, "
3303 "ignoring .debug_aranges."),
3304 objfile_name (objfile),
3305 plongest (entry_addr - section->buffer),
3306 pulongest (debug_info_offset));
3307 return;
3308 }
3309 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3310
3311 const uint8_t address_size = *addr++;
3312 if (address_size < 1 || address_size > 8)
3313 {
3314 warning (_("Section .debug_aranges in %s entry at offset %s "
3315 "address_size %u is invalid, ignoring .debug_aranges."),
3316 objfile_name (objfile),
3317 plongest (entry_addr - section->buffer), address_size);
3318 return;
3319 }
3320
3321 const uint8_t segment_selector_size = *addr++;
3322 if (segment_selector_size != 0)
3323 {
3324 warning (_("Section .debug_aranges in %s entry at offset %s "
3325 "segment_selector_size %u is not supported, "
3326 "ignoring .debug_aranges."),
3327 objfile_name (objfile),
3328 plongest (entry_addr - section->buffer),
3329 segment_selector_size);
3330 return;
3331 }
3332
3333 /* Must pad to an alignment boundary that is twice the address
3334 size. It is undocumented by the DWARF standard but GCC does
3335 use it. */
3336 for (size_t padding = ((-(addr - section->buffer))
3337 & (2 * address_size - 1));
3338 padding > 0; padding--)
3339 if (*addr++ != 0)
3340 {
3341 warning (_("Section .debug_aranges in %s entry at offset %s "
3342 "padding is not zero, ignoring .debug_aranges."),
3343 objfile_name (objfile),
3344 plongest (entry_addr - section->buffer));
3345 return;
3346 }
3347
3348 for (;;)
3349 {
3350 if (addr + 2 * address_size > entry_end)
3351 {
3352 warning (_("Section .debug_aranges in %s entry at offset %s "
3353 "address list is not properly terminated, "
3354 "ignoring .debug_aranges."),
3355 objfile_name (objfile),
3356 plongest (entry_addr - section->buffer));
3357 return;
3358 }
3359 ULONGEST start = extract_unsigned_integer (addr, address_size,
3360 dwarf5_byte_order);
3361 addr += address_size;
3362 ULONGEST length = extract_unsigned_integer (addr, address_size,
3363 dwarf5_byte_order);
3364 addr += address_size;
3365 if (start == 0 && length == 0)
3366 break;
3367 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3368 {
3369 /* Symbol was eliminated due to a COMDAT group. */
3370 continue;
3371 }
3372 ULONGEST end = start + length;
3373 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3374 - baseaddr);
3375 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3376 - baseaddr);
3377 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3378 }
3379 }
3380
3381 objfile->partial_symtabs->psymtabs_addrmap
3382 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3383 }
3384
3385 /* Find a slot in the mapped index INDEX for the object named NAME.
3386 If NAME is found, set *VEC_OUT to point to the CU vector in the
3387 constant pool and return true. If NAME cannot be found, return
3388 false. */
3389
3390 static bool
3391 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3392 offset_type **vec_out)
3393 {
3394 offset_type hash;
3395 offset_type slot, step;
3396 int (*cmp) (const char *, const char *);
3397
3398 gdb::unique_xmalloc_ptr<char> without_params;
3399 if (current_language->la_language == language_cplus
3400 || current_language->la_language == language_fortran
3401 || current_language->la_language == language_d)
3402 {
3403 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3404 not contain any. */
3405
3406 if (strchr (name, '(') != NULL)
3407 {
3408 without_params = cp_remove_params (name);
3409
3410 if (without_params != NULL)
3411 name = without_params.get ();
3412 }
3413 }
3414
3415 /* Index version 4 did not support case insensitive searches. But the
3416 indices for case insensitive languages are built in lowercase, therefore
3417 simulate our NAME being searched is also lowercased. */
3418 hash = mapped_index_string_hash ((index->version == 4
3419 && case_sensitivity == case_sensitive_off
3420 ? 5 : index->version),
3421 name);
3422
3423 slot = hash & (index->symbol_table.size () - 1);
3424 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3425 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3426
3427 for (;;)
3428 {
3429 const char *str;
3430
3431 const auto &bucket = index->symbol_table[slot];
3432 if (bucket.name == 0 && bucket.vec == 0)
3433 return false;
3434
3435 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3436 if (!cmp (name, str))
3437 {
3438 *vec_out = (offset_type *) (index->constant_pool
3439 + MAYBE_SWAP (bucket.vec));
3440 return true;
3441 }
3442
3443 slot = (slot + step) & (index->symbol_table.size () - 1);
3444 }
3445 }
3446
3447 /* A helper function that reads the .gdb_index from BUFFER and fills
3448 in MAP. FILENAME is the name of the file containing the data;
3449 it is used for error reporting. DEPRECATED_OK is true if it is
3450 ok to use deprecated sections.
3451
3452 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3453 out parameters that are filled in with information about the CU and
3454 TU lists in the section.
3455
3456 Returns true if all went well, false otherwise. */
3457
3458 static bool
3459 read_gdb_index_from_buffer (struct objfile *objfile,
3460 const char *filename,
3461 bool deprecated_ok,
3462 gdb::array_view<const gdb_byte> buffer,
3463 struct mapped_index *map,
3464 const gdb_byte **cu_list,
3465 offset_type *cu_list_elements,
3466 const gdb_byte **types_list,
3467 offset_type *types_list_elements)
3468 {
3469 const gdb_byte *addr = &buffer[0];
3470
3471 /* Version check. */
3472 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3473 /* Versions earlier than 3 emitted every copy of a psymbol. This
3474 causes the index to behave very poorly for certain requests. Version 3
3475 contained incomplete addrmap. So, it seems better to just ignore such
3476 indices. */
3477 if (version < 4)
3478 {
3479 static int warning_printed = 0;
3480 if (!warning_printed)
3481 {
3482 warning (_("Skipping obsolete .gdb_index section in %s."),
3483 filename);
3484 warning_printed = 1;
3485 }
3486 return 0;
3487 }
3488 /* Index version 4 uses a different hash function than index version
3489 5 and later.
3490
3491 Versions earlier than 6 did not emit psymbols for inlined
3492 functions. Using these files will cause GDB not to be able to
3493 set breakpoints on inlined functions by name, so we ignore these
3494 indices unless the user has done
3495 "set use-deprecated-index-sections on". */
3496 if (version < 6 && !deprecated_ok)
3497 {
3498 static int warning_printed = 0;
3499 if (!warning_printed)
3500 {
3501 warning (_("\
3502 Skipping deprecated .gdb_index section in %s.\n\
3503 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3504 to use the section anyway."),
3505 filename);
3506 warning_printed = 1;
3507 }
3508 return 0;
3509 }
3510 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3511 of the TU (for symbols coming from TUs),
3512 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3513 Plus gold-generated indices can have duplicate entries for global symbols,
3514 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3515 These are just performance bugs, and we can't distinguish gdb-generated
3516 indices from gold-generated ones, so issue no warning here. */
3517
3518 /* Indexes with higher version than the one supported by GDB may be no
3519 longer backward compatible. */
3520 if (version > 8)
3521 return 0;
3522
3523 map->version = version;
3524
3525 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3526
3527 int i = 0;
3528 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3529 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3530 / 8);
3531 ++i;
3532
3533 *types_list = addr + MAYBE_SWAP (metadata[i]);
3534 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3535 - MAYBE_SWAP (metadata[i]))
3536 / 8);
3537 ++i;
3538
3539 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3540 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3541 map->address_table
3542 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3543 ++i;
3544
3545 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3546 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3547 map->symbol_table
3548 = gdb::array_view<mapped_index::symbol_table_slot>
3549 ((mapped_index::symbol_table_slot *) symbol_table,
3550 (mapped_index::symbol_table_slot *) symbol_table_end);
3551
3552 ++i;
3553 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3554
3555 return 1;
3556 }
3557
3558 /* Callback types for dwarf2_read_gdb_index. */
3559
3560 typedef gdb::function_view
3561 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3562 get_gdb_index_contents_ftype;
3563 typedef gdb::function_view
3564 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3565 get_gdb_index_contents_dwz_ftype;
3566
3567 /* Read .gdb_index. If everything went ok, initialize the "quick"
3568 elements of all the CUs and return 1. Otherwise, return 0. */
3569
3570 static int
3571 dwarf2_read_gdb_index
3572 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3573 get_gdb_index_contents_ftype get_gdb_index_contents,
3574 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3575 {
3576 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3577 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3578 struct dwz_file *dwz;
3579 struct objfile *objfile = dwarf2_per_objfile->objfile;
3580
3581 gdb::array_view<const gdb_byte> main_index_contents
3582 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3583
3584 if (main_index_contents.empty ())
3585 return 0;
3586
3587 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3588 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3589 use_deprecated_index_sections,
3590 main_index_contents, map.get (), &cu_list,
3591 &cu_list_elements, &types_list,
3592 &types_list_elements))
3593 return 0;
3594
3595 /* Don't use the index if it's empty. */
3596 if (map->symbol_table.empty ())
3597 return 0;
3598
3599 /* If there is a .dwz file, read it so we can get its CU list as
3600 well. */
3601 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3602 if (dwz != NULL)
3603 {
3604 struct mapped_index dwz_map;
3605 const gdb_byte *dwz_types_ignore;
3606 offset_type dwz_types_elements_ignore;
3607
3608 gdb::array_view<const gdb_byte> dwz_index_content
3609 = get_gdb_index_contents_dwz (objfile, dwz);
3610
3611 if (dwz_index_content.empty ())
3612 return 0;
3613
3614 if (!read_gdb_index_from_buffer (objfile,
3615 bfd_get_filename (dwz->dwz_bfd.get ()),
3616 1, dwz_index_content, &dwz_map,
3617 &dwz_list, &dwz_list_elements,
3618 &dwz_types_ignore,
3619 &dwz_types_elements_ignore))
3620 {
3621 warning (_("could not read '.gdb_index' section from %s; skipping"),
3622 bfd_get_filename (dwz->dwz_bfd.get ()));
3623 return 0;
3624 }
3625 }
3626
3627 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3628 dwz_list, dwz_list_elements);
3629
3630 if (types_list_elements)
3631 {
3632 /* We can only handle a single .debug_types when we have an
3633 index. */
3634 if (dwarf2_per_objfile->types.size () != 1)
3635 return 0;
3636
3637 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3638
3639 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3640 types_list, types_list_elements);
3641 }
3642
3643 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3644
3645 dwarf2_per_objfile->index_table = std::move (map);
3646 dwarf2_per_objfile->using_index = 1;
3647 dwarf2_per_objfile->quick_file_names_table =
3648 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3649
3650 return 1;
3651 }
3652
3653 /* die_reader_func for dw2_get_file_names. */
3654
3655 static void
3656 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3657 const gdb_byte *info_ptr,
3658 struct die_info *comp_unit_die,
3659 int has_children,
3660 void *data)
3661 {
3662 struct dwarf2_cu *cu = reader->cu;
3663 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3664 struct dwarf2_per_objfile *dwarf2_per_objfile
3665 = cu->per_cu->dwarf2_per_objfile;
3666 struct objfile *objfile = dwarf2_per_objfile->objfile;
3667 struct dwarf2_per_cu_data *lh_cu;
3668 struct attribute *attr;
3669 void **slot;
3670 struct quick_file_names *qfn;
3671
3672 gdb_assert (! this_cu->is_debug_types);
3673
3674 /* Our callers never want to match partial units -- instead they
3675 will match the enclosing full CU. */
3676 if (comp_unit_die->tag == DW_TAG_partial_unit)
3677 {
3678 this_cu->v.quick->no_file_data = 1;
3679 return;
3680 }
3681
3682 lh_cu = this_cu;
3683 slot = NULL;
3684
3685 line_header_up lh;
3686 sect_offset line_offset {};
3687
3688 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3689 if (attr != nullptr)
3690 {
3691 struct quick_file_names find_entry;
3692
3693 line_offset = (sect_offset) DW_UNSND (attr);
3694
3695 /* We may have already read in this line header (TU line header sharing).
3696 If we have we're done. */
3697 find_entry.hash.dwo_unit = cu->dwo_unit;
3698 find_entry.hash.line_sect_off = line_offset;
3699 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3700 &find_entry, INSERT);
3701 if (*slot != NULL)
3702 {
3703 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3704 return;
3705 }
3706
3707 lh = dwarf_decode_line_header (line_offset, cu);
3708 }
3709 if (lh == NULL)
3710 {
3711 lh_cu->v.quick->no_file_data = 1;
3712 return;
3713 }
3714
3715 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3716 qfn->hash.dwo_unit = cu->dwo_unit;
3717 qfn->hash.line_sect_off = line_offset;
3718 gdb_assert (slot != NULL);
3719 *slot = qfn;
3720
3721 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3722
3723 int offset = 0;
3724 if (strcmp (fnd.name, "<unknown>") != 0)
3725 ++offset;
3726
3727 qfn->num_file_names = offset + lh->file_names_size ();
3728 qfn->file_names =
3729 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3730 if (offset != 0)
3731 qfn->file_names[0] = xstrdup (fnd.name);
3732 for (int i = 0; i < lh->file_names_size (); ++i)
3733 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3734 qfn->real_names = NULL;
3735
3736 lh_cu->v.quick->file_names = qfn;
3737 }
3738
3739 /* A helper for the "quick" functions which attempts to read the line
3740 table for THIS_CU. */
3741
3742 static struct quick_file_names *
3743 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3744 {
3745 /* This should never be called for TUs. */
3746 gdb_assert (! this_cu->is_debug_types);
3747 /* Nor type unit groups. */
3748 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3749
3750 if (this_cu->v.quick->file_names != NULL)
3751 return this_cu->v.quick->file_names;
3752 /* If we know there is no line data, no point in looking again. */
3753 if (this_cu->v.quick->no_file_data)
3754 return NULL;
3755
3756 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3757
3758 if (this_cu->v.quick->no_file_data)
3759 return NULL;
3760 return this_cu->v.quick->file_names;
3761 }
3762
3763 /* A helper for the "quick" functions which computes and caches the
3764 real path for a given file name from the line table. */
3765
3766 static const char *
3767 dw2_get_real_path (struct objfile *objfile,
3768 struct quick_file_names *qfn, int index)
3769 {
3770 if (qfn->real_names == NULL)
3771 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3772 qfn->num_file_names, const char *);
3773
3774 if (qfn->real_names[index] == NULL)
3775 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3776
3777 return qfn->real_names[index];
3778 }
3779
3780 static struct symtab *
3781 dw2_find_last_source_symtab (struct objfile *objfile)
3782 {
3783 struct dwarf2_per_objfile *dwarf2_per_objfile
3784 = get_dwarf2_per_objfile (objfile);
3785 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3786 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3787
3788 if (cust == NULL)
3789 return NULL;
3790
3791 return compunit_primary_filetab (cust);
3792 }
3793
3794 /* Traversal function for dw2_forget_cached_source_info. */
3795
3796 static int
3797 dw2_free_cached_file_names (void **slot, void *info)
3798 {
3799 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3800
3801 if (file_data->real_names)
3802 {
3803 int i;
3804
3805 for (i = 0; i < file_data->num_file_names; ++i)
3806 {
3807 xfree ((void*) file_data->real_names[i]);
3808 file_data->real_names[i] = NULL;
3809 }
3810 }
3811
3812 return 1;
3813 }
3814
3815 static void
3816 dw2_forget_cached_source_info (struct objfile *objfile)
3817 {
3818 struct dwarf2_per_objfile *dwarf2_per_objfile
3819 = get_dwarf2_per_objfile (objfile);
3820
3821 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3822 dw2_free_cached_file_names, NULL);
3823 }
3824
3825 /* Helper function for dw2_map_symtabs_matching_filename that expands
3826 the symtabs and calls the iterator. */
3827
3828 static int
3829 dw2_map_expand_apply (struct objfile *objfile,
3830 struct dwarf2_per_cu_data *per_cu,
3831 const char *name, const char *real_path,
3832 gdb::function_view<bool (symtab *)> callback)
3833 {
3834 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3835
3836 /* Don't visit already-expanded CUs. */
3837 if (per_cu->v.quick->compunit_symtab)
3838 return 0;
3839
3840 /* This may expand more than one symtab, and we want to iterate over
3841 all of them. */
3842 dw2_instantiate_symtab (per_cu, false);
3843
3844 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3845 last_made, callback);
3846 }
3847
3848 /* Implementation of the map_symtabs_matching_filename method. */
3849
3850 static bool
3851 dw2_map_symtabs_matching_filename
3852 (struct objfile *objfile, const char *name, const char *real_path,
3853 gdb::function_view<bool (symtab *)> callback)
3854 {
3855 const char *name_basename = lbasename (name);
3856 struct dwarf2_per_objfile *dwarf2_per_objfile
3857 = get_dwarf2_per_objfile (objfile);
3858
3859 /* The rule is CUs specify all the files, including those used by
3860 any TU, so there's no need to scan TUs here. */
3861
3862 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3863 {
3864 /* We only need to look at symtabs not already expanded. */
3865 if (per_cu->v.quick->compunit_symtab)
3866 continue;
3867
3868 quick_file_names *file_data = dw2_get_file_names (per_cu);
3869 if (file_data == NULL)
3870 continue;
3871
3872 for (int j = 0; j < file_data->num_file_names; ++j)
3873 {
3874 const char *this_name = file_data->file_names[j];
3875 const char *this_real_name;
3876
3877 if (compare_filenames_for_search (this_name, name))
3878 {
3879 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3880 callback))
3881 return true;
3882 continue;
3883 }
3884
3885 /* Before we invoke realpath, which can get expensive when many
3886 files are involved, do a quick comparison of the basenames. */
3887 if (! basenames_may_differ
3888 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3889 continue;
3890
3891 this_real_name = dw2_get_real_path (objfile, file_data, j);
3892 if (compare_filenames_for_search (this_real_name, name))
3893 {
3894 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3895 callback))
3896 return true;
3897 continue;
3898 }
3899
3900 if (real_path != NULL)
3901 {
3902 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3903 gdb_assert (IS_ABSOLUTE_PATH (name));
3904 if (this_real_name != NULL
3905 && FILENAME_CMP (real_path, this_real_name) == 0)
3906 {
3907 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3908 callback))
3909 return true;
3910 continue;
3911 }
3912 }
3913 }
3914 }
3915
3916 return false;
3917 }
3918
3919 /* Struct used to manage iterating over all CUs looking for a symbol. */
3920
3921 struct dw2_symtab_iterator
3922 {
3923 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3924 struct dwarf2_per_objfile *dwarf2_per_objfile;
3925 /* If set, only look for symbols that match that block. Valid values are
3926 GLOBAL_BLOCK and STATIC_BLOCK. */
3927 gdb::optional<block_enum> block_index;
3928 /* The kind of symbol we're looking for. */
3929 domain_enum domain;
3930 /* The list of CUs from the index entry of the symbol,
3931 or NULL if not found. */
3932 offset_type *vec;
3933 /* The next element in VEC to look at. */
3934 int next;
3935 /* The number of elements in VEC, or zero if there is no match. */
3936 int length;
3937 /* Have we seen a global version of the symbol?
3938 If so we can ignore all further global instances.
3939 This is to work around gold/15646, inefficient gold-generated
3940 indices. */
3941 int global_seen;
3942 };
3943
3944 /* Initialize the index symtab iterator ITER. */
3945
3946 static void
3947 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3948 struct dwarf2_per_objfile *dwarf2_per_objfile,
3949 gdb::optional<block_enum> block_index,
3950 domain_enum domain,
3951 const char *name)
3952 {
3953 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3954 iter->block_index = block_index;
3955 iter->domain = domain;
3956 iter->next = 0;
3957 iter->global_seen = 0;
3958
3959 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3960
3961 /* index is NULL if OBJF_READNOW. */
3962 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3963 iter->length = MAYBE_SWAP (*iter->vec);
3964 else
3965 {
3966 iter->vec = NULL;
3967 iter->length = 0;
3968 }
3969 }
3970
3971 /* Return the next matching CU or NULL if there are no more. */
3972
3973 static struct dwarf2_per_cu_data *
3974 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3975 {
3976 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3977
3978 for ( ; iter->next < iter->length; ++iter->next)
3979 {
3980 offset_type cu_index_and_attrs =
3981 MAYBE_SWAP (iter->vec[iter->next + 1]);
3982 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3983 gdb_index_symbol_kind symbol_kind =
3984 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3985 /* Only check the symbol attributes if they're present.
3986 Indices prior to version 7 don't record them,
3987 and indices >= 7 may elide them for certain symbols
3988 (gold does this). */
3989 int attrs_valid =
3990 (dwarf2_per_objfile->index_table->version >= 7
3991 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3992
3993 /* Don't crash on bad data. */
3994 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3995 + dwarf2_per_objfile->all_type_units.size ()))
3996 {
3997 complaint (_(".gdb_index entry has bad CU index"
3998 " [in module %s]"),
3999 objfile_name (dwarf2_per_objfile->objfile));
4000 continue;
4001 }
4002
4003 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4004
4005 /* Skip if already read in. */
4006 if (per_cu->v.quick->compunit_symtab)
4007 continue;
4008
4009 /* Check static vs global. */
4010 if (attrs_valid)
4011 {
4012 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4013
4014 if (iter->block_index.has_value ())
4015 {
4016 bool want_static = *iter->block_index == STATIC_BLOCK;
4017
4018 if (is_static != want_static)
4019 continue;
4020 }
4021
4022 /* Work around gold/15646. */
4023 if (!is_static && iter->global_seen)
4024 continue;
4025 if (!is_static)
4026 iter->global_seen = 1;
4027 }
4028
4029 /* Only check the symbol's kind if it has one. */
4030 if (attrs_valid)
4031 {
4032 switch (iter->domain)
4033 {
4034 case VAR_DOMAIN:
4035 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4036 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4037 /* Some types are also in VAR_DOMAIN. */
4038 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4039 continue;
4040 break;
4041 case STRUCT_DOMAIN:
4042 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4043 continue;
4044 break;
4045 case LABEL_DOMAIN:
4046 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4047 continue;
4048 break;
4049 case MODULE_DOMAIN:
4050 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4051 continue;
4052 break;
4053 default:
4054 break;
4055 }
4056 }
4057
4058 ++iter->next;
4059 return per_cu;
4060 }
4061
4062 return NULL;
4063 }
4064
4065 static struct compunit_symtab *
4066 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4067 const char *name, domain_enum domain)
4068 {
4069 struct compunit_symtab *stab_best = NULL;
4070 struct dwarf2_per_objfile *dwarf2_per_objfile
4071 = get_dwarf2_per_objfile (objfile);
4072
4073 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4074
4075 struct dw2_symtab_iterator iter;
4076 struct dwarf2_per_cu_data *per_cu;
4077
4078 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4079
4080 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4081 {
4082 struct symbol *sym, *with_opaque = NULL;
4083 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4084 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4085 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4086
4087 sym = block_find_symbol (block, name, domain,
4088 block_find_non_opaque_type_preferred,
4089 &with_opaque);
4090
4091 /* Some caution must be observed with overloaded functions
4092 and methods, since the index will not contain any overload
4093 information (but NAME might contain it). */
4094
4095 if (sym != NULL
4096 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4097 return stab;
4098 if (with_opaque != NULL
4099 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4100 stab_best = stab;
4101
4102 /* Keep looking through other CUs. */
4103 }
4104
4105 return stab_best;
4106 }
4107
4108 static void
4109 dw2_print_stats (struct objfile *objfile)
4110 {
4111 struct dwarf2_per_objfile *dwarf2_per_objfile
4112 = get_dwarf2_per_objfile (objfile);
4113 int total = (dwarf2_per_objfile->all_comp_units.size ()
4114 + dwarf2_per_objfile->all_type_units.size ());
4115 int count = 0;
4116
4117 for (int i = 0; i < total; ++i)
4118 {
4119 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4120
4121 if (!per_cu->v.quick->compunit_symtab)
4122 ++count;
4123 }
4124 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4125 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4126 }
4127
4128 /* This dumps minimal information about the index.
4129 It is called via "mt print objfiles".
4130 One use is to verify .gdb_index has been loaded by the
4131 gdb.dwarf2/gdb-index.exp testcase. */
4132
4133 static void
4134 dw2_dump (struct objfile *objfile)
4135 {
4136 struct dwarf2_per_objfile *dwarf2_per_objfile
4137 = get_dwarf2_per_objfile (objfile);
4138
4139 gdb_assert (dwarf2_per_objfile->using_index);
4140 printf_filtered (".gdb_index:");
4141 if (dwarf2_per_objfile->index_table != NULL)
4142 {
4143 printf_filtered (" version %d\n",
4144 dwarf2_per_objfile->index_table->version);
4145 }
4146 else
4147 printf_filtered (" faked for \"readnow\"\n");
4148 printf_filtered ("\n");
4149 }
4150
4151 static void
4152 dw2_expand_symtabs_for_function (struct objfile *objfile,
4153 const char *func_name)
4154 {
4155 struct dwarf2_per_objfile *dwarf2_per_objfile
4156 = get_dwarf2_per_objfile (objfile);
4157
4158 struct dw2_symtab_iterator iter;
4159 struct dwarf2_per_cu_data *per_cu;
4160
4161 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4162
4163 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4164 dw2_instantiate_symtab (per_cu, false);
4165
4166 }
4167
4168 static void
4169 dw2_expand_all_symtabs (struct objfile *objfile)
4170 {
4171 struct dwarf2_per_objfile *dwarf2_per_objfile
4172 = get_dwarf2_per_objfile (objfile);
4173 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4174 + dwarf2_per_objfile->all_type_units.size ());
4175
4176 for (int i = 0; i < total_units; ++i)
4177 {
4178 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4179
4180 /* We don't want to directly expand a partial CU, because if we
4181 read it with the wrong language, then assertion failures can
4182 be triggered later on. See PR symtab/23010. So, tell
4183 dw2_instantiate_symtab to skip partial CUs -- any important
4184 partial CU will be read via DW_TAG_imported_unit anyway. */
4185 dw2_instantiate_symtab (per_cu, true);
4186 }
4187 }
4188
4189 static void
4190 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4191 const char *fullname)
4192 {
4193 struct dwarf2_per_objfile *dwarf2_per_objfile
4194 = get_dwarf2_per_objfile (objfile);
4195
4196 /* We don't need to consider type units here.
4197 This is only called for examining code, e.g. expand_line_sal.
4198 There can be an order of magnitude (or more) more type units
4199 than comp units, and we avoid them if we can. */
4200
4201 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4202 {
4203 /* We only need to look at symtabs not already expanded. */
4204 if (per_cu->v.quick->compunit_symtab)
4205 continue;
4206
4207 quick_file_names *file_data = dw2_get_file_names (per_cu);
4208 if (file_data == NULL)
4209 continue;
4210
4211 for (int j = 0; j < file_data->num_file_names; ++j)
4212 {
4213 const char *this_fullname = file_data->file_names[j];
4214
4215 if (filename_cmp (this_fullname, fullname) == 0)
4216 {
4217 dw2_instantiate_symtab (per_cu, false);
4218 break;
4219 }
4220 }
4221 }
4222 }
4223
4224 static void
4225 dw2_map_matching_symbols
4226 (struct objfile *objfile,
4227 const lookup_name_info &name, domain_enum domain,
4228 int global,
4229 gdb::function_view<symbol_found_callback_ftype> callback,
4230 symbol_compare_ftype *ordered_compare)
4231 {
4232 /* Currently unimplemented; used for Ada. The function can be called if the
4233 current language is Ada for a non-Ada objfile using GNU index. As Ada
4234 does not look for non-Ada symbols this function should just return. */
4235 }
4236
4237 /* Starting from a search name, return the string that finds the upper
4238 bound of all strings that start with SEARCH_NAME in a sorted name
4239 list. Returns the empty string to indicate that the upper bound is
4240 the end of the list. */
4241
4242 static std::string
4243 make_sort_after_prefix_name (const char *search_name)
4244 {
4245 /* When looking to complete "func", we find the upper bound of all
4246 symbols that start with "func" by looking for where we'd insert
4247 the closest string that would follow "func" in lexicographical
4248 order. Usually, that's "func"-with-last-character-incremented,
4249 i.e. "fund". Mind non-ASCII characters, though. Usually those
4250 will be UTF-8 multi-byte sequences, but we can't be certain.
4251 Especially mind the 0xff character, which is a valid character in
4252 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4253 rule out compilers allowing it in identifiers. Note that
4254 conveniently, strcmp/strcasecmp are specified to compare
4255 characters interpreted as unsigned char. So what we do is treat
4256 the whole string as a base 256 number composed of a sequence of
4257 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4258 to 0, and carries 1 to the following more-significant position.
4259 If the very first character in SEARCH_NAME ends up incremented
4260 and carries/overflows, then the upper bound is the end of the
4261 list. The string after the empty string is also the empty
4262 string.
4263
4264 Some examples of this operation:
4265
4266 SEARCH_NAME => "+1" RESULT
4267
4268 "abc" => "abd"
4269 "ab\xff" => "ac"
4270 "\xff" "a" "\xff" => "\xff" "b"
4271 "\xff" => ""
4272 "\xff\xff" => ""
4273 "" => ""
4274
4275 Then, with these symbols for example:
4276
4277 func
4278 func1
4279 fund
4280
4281 completing "func" looks for symbols between "func" and
4282 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4283 which finds "func" and "func1", but not "fund".
4284
4285 And with:
4286
4287 funcÿ (Latin1 'ÿ' [0xff])
4288 funcÿ1
4289 fund
4290
4291 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4292 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4293
4294 And with:
4295
4296 ÿÿ (Latin1 'ÿ' [0xff])
4297 ÿÿ1
4298
4299 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4300 the end of the list.
4301 */
4302 std::string after = search_name;
4303 while (!after.empty () && (unsigned char) after.back () == 0xff)
4304 after.pop_back ();
4305 if (!after.empty ())
4306 after.back () = (unsigned char) after.back () + 1;
4307 return after;
4308 }
4309
4310 /* See declaration. */
4311
4312 std::pair<std::vector<name_component>::const_iterator,
4313 std::vector<name_component>::const_iterator>
4314 mapped_index_base::find_name_components_bounds
4315 (const lookup_name_info &lookup_name_without_params, language lang) const
4316 {
4317 auto *name_cmp
4318 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4319
4320 const char *lang_name
4321 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4322
4323 /* Comparison function object for lower_bound that matches against a
4324 given symbol name. */
4325 auto lookup_compare_lower = [&] (const name_component &elem,
4326 const char *name)
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 (elem_name, name) < 0;
4331 };
4332
4333 /* Comparison function object for upper_bound that matches against a
4334 given symbol name. */
4335 auto lookup_compare_upper = [&] (const char *name,
4336 const name_component &elem)
4337 {
4338 const char *elem_qualified = this->symbol_name_at (elem.idx);
4339 const char *elem_name = elem_qualified + elem.name_offset;
4340 return name_cmp (name, elem_name) < 0;
4341 };
4342
4343 auto begin = this->name_components.begin ();
4344 auto end = this->name_components.end ();
4345
4346 /* Find the lower bound. */
4347 auto lower = [&] ()
4348 {
4349 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4350 return begin;
4351 else
4352 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4353 } ();
4354
4355 /* Find the upper bound. */
4356 auto upper = [&] ()
4357 {
4358 if (lookup_name_without_params.completion_mode ())
4359 {
4360 /* In completion mode, we want UPPER to point past all
4361 symbols names that have the same prefix. I.e., with
4362 these symbols, and completing "func":
4363
4364 function << lower bound
4365 function1
4366 other_function << upper bound
4367
4368 We find the upper bound by looking for the insertion
4369 point of "func"-with-last-character-incremented,
4370 i.e. "fund". */
4371 std::string after = make_sort_after_prefix_name (lang_name);
4372 if (after.empty ())
4373 return end;
4374 return std::lower_bound (lower, end, after.c_str (),
4375 lookup_compare_lower);
4376 }
4377 else
4378 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4379 } ();
4380
4381 return {lower, upper};
4382 }
4383
4384 /* See declaration. */
4385
4386 void
4387 mapped_index_base::build_name_components ()
4388 {
4389 if (!this->name_components.empty ())
4390 return;
4391
4392 this->name_components_casing = case_sensitivity;
4393 auto *name_cmp
4394 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4395
4396 /* The code below only knows how to break apart components of C++
4397 symbol names (and other languages that use '::' as
4398 namespace/module separator) and Ada symbol names. */
4399 auto count = this->symbol_name_count ();
4400 for (offset_type idx = 0; idx < count; idx++)
4401 {
4402 if (this->symbol_name_slot_invalid (idx))
4403 continue;
4404
4405 const char *name = this->symbol_name_at (idx);
4406
4407 /* Add each name component to the name component table. */
4408 unsigned int previous_len = 0;
4409
4410 if (strstr (name, "::") != nullptr)
4411 {
4412 for (unsigned int current_len = cp_find_first_component (name);
4413 name[current_len] != '\0';
4414 current_len += cp_find_first_component (name + current_len))
4415 {
4416 gdb_assert (name[current_len] == ':');
4417 this->name_components.push_back ({previous_len, idx});
4418 /* Skip the '::'. */
4419 current_len += 2;
4420 previous_len = current_len;
4421 }
4422 }
4423 else
4424 {
4425 /* Handle the Ada encoded (aka mangled) form here. */
4426 for (const char *iter = strstr (name, "__");
4427 iter != nullptr;
4428 iter = strstr (iter, "__"))
4429 {
4430 this->name_components.push_back ({previous_len, idx});
4431 iter += 2;
4432 previous_len = iter - name;
4433 }
4434 }
4435
4436 this->name_components.push_back ({previous_len, idx});
4437 }
4438
4439 /* Sort name_components elements by name. */
4440 auto name_comp_compare = [&] (const name_component &left,
4441 const name_component &right)
4442 {
4443 const char *left_qualified = this->symbol_name_at (left.idx);
4444 const char *right_qualified = this->symbol_name_at (right.idx);
4445
4446 const char *left_name = left_qualified + left.name_offset;
4447 const char *right_name = right_qualified + right.name_offset;
4448
4449 return name_cmp (left_name, right_name) < 0;
4450 };
4451
4452 std::sort (this->name_components.begin (),
4453 this->name_components.end (),
4454 name_comp_compare);
4455 }
4456
4457 /* Helper for dw2_expand_symtabs_matching that works with a
4458 mapped_index_base instead of the containing objfile. This is split
4459 to a separate function in order to be able to unit test the
4460 name_components matching using a mock mapped_index_base. For each
4461 symbol name that matches, calls MATCH_CALLBACK, passing it the
4462 symbol's index in the mapped_index_base symbol table. */
4463
4464 static void
4465 dw2_expand_symtabs_matching_symbol
4466 (mapped_index_base &index,
4467 const lookup_name_info &lookup_name_in,
4468 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4469 enum search_domain kind,
4470 gdb::function_view<bool (offset_type)> match_callback)
4471 {
4472 lookup_name_info lookup_name_without_params
4473 = lookup_name_in.make_ignore_params ();
4474
4475 /* Build the symbol name component sorted vector, if we haven't
4476 yet. */
4477 index.build_name_components ();
4478
4479 /* The same symbol may appear more than once in the range though.
4480 E.g., if we're looking for symbols that complete "w", and we have
4481 a symbol named "w1::w2", we'll find the two name components for
4482 that same symbol in the range. To be sure we only call the
4483 callback once per symbol, we first collect the symbol name
4484 indexes that matched in a temporary vector and ignore
4485 duplicates. */
4486 std::vector<offset_type> matches;
4487
4488 struct name_and_matcher
4489 {
4490 symbol_name_matcher_ftype *matcher;
4491 const std::string &name;
4492
4493 bool operator== (const name_and_matcher &other) const
4494 {
4495 return matcher == other.matcher && name == other.name;
4496 }
4497 };
4498
4499 /* A vector holding all the different symbol name matchers, for all
4500 languages. */
4501 std::vector<name_and_matcher> matchers;
4502
4503 for (int i = 0; i < nr_languages; i++)
4504 {
4505 enum language lang_e = (enum language) i;
4506
4507 const language_defn *lang = language_def (lang_e);
4508 symbol_name_matcher_ftype *name_matcher
4509 = get_symbol_name_matcher (lang, lookup_name_without_params);
4510
4511 name_and_matcher key {
4512 name_matcher,
4513 lookup_name_without_params.language_lookup_name (lang_e)
4514 };
4515
4516 /* Don't insert the same comparison routine more than once.
4517 Note that we do this linear walk. This is not a problem in
4518 practice because the number of supported languages is
4519 low. */
4520 if (std::find (matchers.begin (), matchers.end (), key)
4521 != matchers.end ())
4522 continue;
4523 matchers.push_back (std::move (key));
4524
4525 auto bounds
4526 = index.find_name_components_bounds (lookup_name_without_params,
4527 lang_e);
4528
4529 /* Now for each symbol name in range, check to see if we have a name
4530 match, and if so, call the MATCH_CALLBACK callback. */
4531
4532 for (; bounds.first != bounds.second; ++bounds.first)
4533 {
4534 const char *qualified = index.symbol_name_at (bounds.first->idx);
4535
4536 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4537 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4538 continue;
4539
4540 matches.push_back (bounds.first->idx);
4541 }
4542 }
4543
4544 std::sort (matches.begin (), matches.end ());
4545
4546 /* Finally call the callback, once per match. */
4547 ULONGEST prev = -1;
4548 for (offset_type idx : matches)
4549 {
4550 if (prev != idx)
4551 {
4552 if (!match_callback (idx))
4553 break;
4554 prev = idx;
4555 }
4556 }
4557
4558 /* Above we use a type wider than idx's for 'prev', since 0 and
4559 (offset_type)-1 are both possible values. */
4560 static_assert (sizeof (prev) > sizeof (offset_type), "");
4561 }
4562
4563 #if GDB_SELF_TEST
4564
4565 namespace selftests { namespace dw2_expand_symtabs_matching {
4566
4567 /* A mock .gdb_index/.debug_names-like name index table, enough to
4568 exercise dw2_expand_symtabs_matching_symbol, which works with the
4569 mapped_index_base interface. Builds an index from the symbol list
4570 passed as parameter to the constructor. */
4571 class mock_mapped_index : public mapped_index_base
4572 {
4573 public:
4574 mock_mapped_index (gdb::array_view<const char *> symbols)
4575 : m_symbol_table (symbols)
4576 {}
4577
4578 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4579
4580 /* Return the number of names in the symbol table. */
4581 size_t symbol_name_count () const override
4582 {
4583 return m_symbol_table.size ();
4584 }
4585
4586 /* Get the name of the symbol at IDX in the symbol table. */
4587 const char *symbol_name_at (offset_type idx) const override
4588 {
4589 return m_symbol_table[idx];
4590 }
4591
4592 private:
4593 gdb::array_view<const char *> m_symbol_table;
4594 };
4595
4596 /* Convenience function that converts a NULL pointer to a "<null>"
4597 string, to pass to print routines. */
4598
4599 static const char *
4600 string_or_null (const char *str)
4601 {
4602 return str != NULL ? str : "<null>";
4603 }
4604
4605 /* Check if a lookup_name_info built from
4606 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4607 index. EXPECTED_LIST is the list of expected matches, in expected
4608 matching order. If no match expected, then an empty list is
4609 specified. Returns true on success. On failure prints a warning
4610 indicating the file:line that failed, and returns false. */
4611
4612 static bool
4613 check_match (const char *file, int line,
4614 mock_mapped_index &mock_index,
4615 const char *name, symbol_name_match_type match_type,
4616 bool completion_mode,
4617 std::initializer_list<const char *> expected_list)
4618 {
4619 lookup_name_info lookup_name (name, match_type, completion_mode);
4620
4621 bool matched = true;
4622
4623 auto mismatch = [&] (const char *expected_str,
4624 const char *got)
4625 {
4626 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4627 "expected=\"%s\", got=\"%s\"\n"),
4628 file, line,
4629 (match_type == symbol_name_match_type::FULL
4630 ? "FULL" : "WILD"),
4631 name, string_or_null (expected_str), string_or_null (got));
4632 matched = false;
4633 };
4634
4635 auto expected_it = expected_list.begin ();
4636 auto expected_end = expected_list.end ();
4637
4638 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4639 NULL, ALL_DOMAIN,
4640 [&] (offset_type idx)
4641 {
4642 const char *matched_name = mock_index.symbol_name_at (idx);
4643 const char *expected_str
4644 = expected_it == expected_end ? NULL : *expected_it++;
4645
4646 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4647 mismatch (expected_str, matched_name);
4648 return true;
4649 });
4650
4651 const char *expected_str
4652 = expected_it == expected_end ? NULL : *expected_it++;
4653 if (expected_str != NULL)
4654 mismatch (expected_str, NULL);
4655
4656 return matched;
4657 }
4658
4659 /* The symbols added to the mock mapped_index for testing (in
4660 canonical form). */
4661 static const char *test_symbols[] = {
4662 "function",
4663 "std::bar",
4664 "std::zfunction",
4665 "std::zfunction2",
4666 "w1::w2",
4667 "ns::foo<char*>",
4668 "ns::foo<int>",
4669 "ns::foo<long>",
4670 "ns2::tmpl<int>::foo2",
4671 "(anonymous namespace)::A::B::C",
4672
4673 /* These are used to check that the increment-last-char in the
4674 matching algorithm for completion doesn't match "t1_fund" when
4675 completing "t1_func". */
4676 "t1_func",
4677 "t1_func1",
4678 "t1_fund",
4679 "t1_fund1",
4680
4681 /* A UTF-8 name with multi-byte sequences to make sure that
4682 cp-name-parser understands this as a single identifier ("função"
4683 is "function" in PT). */
4684 u8"u8função",
4685
4686 /* \377 (0xff) is Latin1 'ÿ'. */
4687 "yfunc\377",
4688
4689 /* \377 (0xff) is Latin1 'ÿ'. */
4690 "\377",
4691 "\377\377123",
4692
4693 /* A name with all sorts of complications. Starts with "z" to make
4694 it easier for the completion tests below. */
4695 #define Z_SYM_NAME \
4696 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4697 "::tuple<(anonymous namespace)::ui*, " \
4698 "std::default_delete<(anonymous namespace)::ui>, void>"
4699
4700 Z_SYM_NAME
4701 };
4702
4703 /* Returns true if the mapped_index_base::find_name_component_bounds
4704 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4705 in completion mode. */
4706
4707 static bool
4708 check_find_bounds_finds (mapped_index_base &index,
4709 const char *search_name,
4710 gdb::array_view<const char *> expected_syms)
4711 {
4712 lookup_name_info lookup_name (search_name,
4713 symbol_name_match_type::FULL, true);
4714
4715 auto bounds = index.find_name_components_bounds (lookup_name,
4716 language_cplus);
4717
4718 size_t distance = std::distance (bounds.first, bounds.second);
4719 if (distance != expected_syms.size ())
4720 return false;
4721
4722 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4723 {
4724 auto nc_elem = bounds.first + exp_elem;
4725 const char *qualified = index.symbol_name_at (nc_elem->idx);
4726 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4727 return false;
4728 }
4729
4730 return true;
4731 }
4732
4733 /* Test the lower-level mapped_index::find_name_component_bounds
4734 method. */
4735
4736 static void
4737 test_mapped_index_find_name_component_bounds ()
4738 {
4739 mock_mapped_index mock_index (test_symbols);
4740
4741 mock_index.build_name_components ();
4742
4743 /* Test the lower-level mapped_index::find_name_component_bounds
4744 method in completion mode. */
4745 {
4746 static const char *expected_syms[] = {
4747 "t1_func",
4748 "t1_func1",
4749 };
4750
4751 SELF_CHECK (check_find_bounds_finds (mock_index,
4752 "t1_func", expected_syms));
4753 }
4754
4755 /* Check that the increment-last-char in the name matching algorithm
4756 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4757 {
4758 static const char *expected_syms1[] = {
4759 "\377",
4760 "\377\377123",
4761 };
4762 SELF_CHECK (check_find_bounds_finds (mock_index,
4763 "\377", expected_syms1));
4764
4765 static const char *expected_syms2[] = {
4766 "\377\377123",
4767 };
4768 SELF_CHECK (check_find_bounds_finds (mock_index,
4769 "\377\377", expected_syms2));
4770 }
4771 }
4772
4773 /* Test dw2_expand_symtabs_matching_symbol. */
4774
4775 static void
4776 test_dw2_expand_symtabs_matching_symbol ()
4777 {
4778 mock_mapped_index mock_index (test_symbols);
4779
4780 /* We let all tests run until the end even if some fails, for debug
4781 convenience. */
4782 bool any_mismatch = false;
4783
4784 /* Create the expected symbols list (an initializer_list). Needed
4785 because lists have commas, and we need to pass them to CHECK,
4786 which is a macro. */
4787 #define EXPECT(...) { __VA_ARGS__ }
4788
4789 /* Wrapper for check_match that passes down the current
4790 __FILE__/__LINE__. */
4791 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4792 any_mismatch |= !check_match (__FILE__, __LINE__, \
4793 mock_index, \
4794 NAME, MATCH_TYPE, COMPLETION_MODE, \
4795 EXPECTED_LIST)
4796
4797 /* Identity checks. */
4798 for (const char *sym : test_symbols)
4799 {
4800 /* Should be able to match all existing symbols. */
4801 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4802 EXPECT (sym));
4803
4804 /* Should be able to match all existing symbols with
4805 parameters. */
4806 std::string with_params = std::string (sym) + "(int)";
4807 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4808 EXPECT (sym));
4809
4810 /* Should be able to match all existing symbols with
4811 parameters and qualifiers. */
4812 with_params = std::string (sym) + " ( int ) const";
4813 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4814 EXPECT (sym));
4815
4816 /* This should really find sym, but cp-name-parser.y doesn't
4817 know about lvalue/rvalue qualifiers yet. */
4818 with_params = std::string (sym) + " ( int ) &&";
4819 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4820 {});
4821 }
4822
4823 /* Check that the name matching algorithm for completion doesn't get
4824 confused with Latin1 'ÿ' / 0xff. */
4825 {
4826 static const char str[] = "\377";
4827 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4828 EXPECT ("\377", "\377\377123"));
4829 }
4830
4831 /* Check that the increment-last-char in the matching algorithm for
4832 completion doesn't match "t1_fund" when completing "t1_func". */
4833 {
4834 static const char str[] = "t1_func";
4835 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4836 EXPECT ("t1_func", "t1_func1"));
4837 }
4838
4839 /* Check that completion mode works at each prefix of the expected
4840 symbol name. */
4841 {
4842 static const char str[] = "function(int)";
4843 size_t len = strlen (str);
4844 std::string lookup;
4845
4846 for (size_t i = 1; i < len; i++)
4847 {
4848 lookup.assign (str, i);
4849 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4850 EXPECT ("function"));
4851 }
4852 }
4853
4854 /* While "w" is a prefix of both components, the match function
4855 should still only be called once. */
4856 {
4857 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4858 EXPECT ("w1::w2"));
4859 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4860 EXPECT ("w1::w2"));
4861 }
4862
4863 /* Same, with a "complicated" symbol. */
4864 {
4865 static const char str[] = Z_SYM_NAME;
4866 size_t len = strlen (str);
4867 std::string lookup;
4868
4869 for (size_t i = 1; i < len; i++)
4870 {
4871 lookup.assign (str, i);
4872 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4873 EXPECT (Z_SYM_NAME));
4874 }
4875 }
4876
4877 /* In FULL mode, an incomplete symbol doesn't match. */
4878 {
4879 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4880 {});
4881 }
4882
4883 /* A complete symbol with parameters matches any overload, since the
4884 index has no overload info. */
4885 {
4886 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4887 EXPECT ("std::zfunction", "std::zfunction2"));
4888 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4889 EXPECT ("std::zfunction", "std::zfunction2"));
4890 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4891 EXPECT ("std::zfunction", "std::zfunction2"));
4892 }
4893
4894 /* Check that whitespace is ignored appropriately. A symbol with a
4895 template argument list. */
4896 {
4897 static const char expected[] = "ns::foo<int>";
4898 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4899 EXPECT (expected));
4900 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4901 EXPECT (expected));
4902 }
4903
4904 /* Check that whitespace is ignored appropriately. A symbol with a
4905 template argument list that includes a pointer. */
4906 {
4907 static const char expected[] = "ns::foo<char*>";
4908 /* Try both completion and non-completion modes. */
4909 static const bool completion_mode[2] = {false, true};
4910 for (size_t i = 0; i < 2; i++)
4911 {
4912 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4913 completion_mode[i], EXPECT (expected));
4914 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4915 completion_mode[i], EXPECT (expected));
4916
4917 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4918 completion_mode[i], EXPECT (expected));
4919 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4920 completion_mode[i], EXPECT (expected));
4921 }
4922 }
4923
4924 {
4925 /* Check method qualifiers are ignored. */
4926 static const char expected[] = "ns::foo<char*>";
4927 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4928 symbol_name_match_type::FULL, true, EXPECT (expected));
4929 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4930 symbol_name_match_type::FULL, true, EXPECT (expected));
4931 CHECK_MATCH ("foo < char * > ( int ) const",
4932 symbol_name_match_type::WILD, true, EXPECT (expected));
4933 CHECK_MATCH ("foo < char * > ( int ) &&",
4934 symbol_name_match_type::WILD, true, EXPECT (expected));
4935 }
4936
4937 /* Test lookup names that don't match anything. */
4938 {
4939 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4940 {});
4941
4942 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4943 {});
4944 }
4945
4946 /* Some wild matching tests, exercising "(anonymous namespace)",
4947 which should not be confused with a parameter list. */
4948 {
4949 static const char *syms[] = {
4950 "A::B::C",
4951 "B::C",
4952 "C",
4953 "A :: B :: C ( int )",
4954 "B :: C ( int )",
4955 "C ( int )",
4956 };
4957
4958 for (const char *s : syms)
4959 {
4960 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4961 EXPECT ("(anonymous namespace)::A::B::C"));
4962 }
4963 }
4964
4965 {
4966 static const char expected[] = "ns2::tmpl<int>::foo2";
4967 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4968 EXPECT (expected));
4969 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4970 EXPECT (expected));
4971 }
4972
4973 SELF_CHECK (!any_mismatch);
4974
4975 #undef EXPECT
4976 #undef CHECK_MATCH
4977 }
4978
4979 static void
4980 run_test ()
4981 {
4982 test_mapped_index_find_name_component_bounds ();
4983 test_dw2_expand_symtabs_matching_symbol ();
4984 }
4985
4986 }} // namespace selftests::dw2_expand_symtabs_matching
4987
4988 #endif /* GDB_SELF_TEST */
4989
4990 /* If FILE_MATCHER is NULL or if PER_CU has
4991 dwarf2_per_cu_quick_data::MARK set (see
4992 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4993 EXPANSION_NOTIFY on it. */
4994
4995 static void
4996 dw2_expand_symtabs_matching_one
4997 (struct dwarf2_per_cu_data *per_cu,
4998 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4999 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5000 {
5001 if (file_matcher == NULL || per_cu->v.quick->mark)
5002 {
5003 bool symtab_was_null
5004 = (per_cu->v.quick->compunit_symtab == NULL);
5005
5006 dw2_instantiate_symtab (per_cu, false);
5007
5008 if (expansion_notify != NULL
5009 && symtab_was_null
5010 && per_cu->v.quick->compunit_symtab != NULL)
5011 expansion_notify (per_cu->v.quick->compunit_symtab);
5012 }
5013 }
5014
5015 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5016 matched, to expand corresponding CUs that were marked. IDX is the
5017 index of the symbol name that matched. */
5018
5019 static void
5020 dw2_expand_marked_cus
5021 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5022 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5023 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5024 search_domain kind)
5025 {
5026 offset_type *vec, vec_len, vec_idx;
5027 bool global_seen = false;
5028 mapped_index &index = *dwarf2_per_objfile->index_table;
5029
5030 vec = (offset_type *) (index.constant_pool
5031 + MAYBE_SWAP (index.symbol_table[idx].vec));
5032 vec_len = MAYBE_SWAP (vec[0]);
5033 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5034 {
5035 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5036 /* This value is only valid for index versions >= 7. */
5037 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5038 gdb_index_symbol_kind symbol_kind =
5039 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5040 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5041 /* Only check the symbol attributes if they're present.
5042 Indices prior to version 7 don't record them,
5043 and indices >= 7 may elide them for certain symbols
5044 (gold does this). */
5045 int attrs_valid =
5046 (index.version >= 7
5047 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5048
5049 /* Work around gold/15646. */
5050 if (attrs_valid)
5051 {
5052 if (!is_static && global_seen)
5053 continue;
5054 if (!is_static)
5055 global_seen = true;
5056 }
5057
5058 /* Only check the symbol's kind if it has one. */
5059 if (attrs_valid)
5060 {
5061 switch (kind)
5062 {
5063 case VARIABLES_DOMAIN:
5064 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5065 continue;
5066 break;
5067 case FUNCTIONS_DOMAIN:
5068 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5069 continue;
5070 break;
5071 case TYPES_DOMAIN:
5072 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5073 continue;
5074 break;
5075 case MODULES_DOMAIN:
5076 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5077 continue;
5078 break;
5079 default:
5080 break;
5081 }
5082 }
5083
5084 /* Don't crash on bad data. */
5085 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5086 + dwarf2_per_objfile->all_type_units.size ()))
5087 {
5088 complaint (_(".gdb_index entry has bad CU index"
5089 " [in module %s]"),
5090 objfile_name (dwarf2_per_objfile->objfile));
5091 continue;
5092 }
5093
5094 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5095 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5096 expansion_notify);
5097 }
5098 }
5099
5100 /* If FILE_MATCHER is non-NULL, set all the
5101 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5102 that match FILE_MATCHER. */
5103
5104 static void
5105 dw_expand_symtabs_matching_file_matcher
5106 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5107 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5108 {
5109 if (file_matcher == NULL)
5110 return;
5111
5112 objfile *const objfile = dwarf2_per_objfile->objfile;
5113
5114 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5115 htab_eq_pointer,
5116 NULL, xcalloc, xfree));
5117 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5118 htab_eq_pointer,
5119 NULL, xcalloc, xfree));
5120
5121 /* The rule is CUs specify all the files, including those used by
5122 any TU, so there's no need to scan TUs here. */
5123
5124 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5125 {
5126 QUIT;
5127
5128 per_cu->v.quick->mark = 0;
5129
5130 /* We only need to look at symtabs not already expanded. */
5131 if (per_cu->v.quick->compunit_symtab)
5132 continue;
5133
5134 quick_file_names *file_data = dw2_get_file_names (per_cu);
5135 if (file_data == NULL)
5136 continue;
5137
5138 if (htab_find (visited_not_found.get (), file_data) != NULL)
5139 continue;
5140 else if (htab_find (visited_found.get (), file_data) != NULL)
5141 {
5142 per_cu->v.quick->mark = 1;
5143 continue;
5144 }
5145
5146 for (int j = 0; j < file_data->num_file_names; ++j)
5147 {
5148 const char *this_real_name;
5149
5150 if (file_matcher (file_data->file_names[j], false))
5151 {
5152 per_cu->v.quick->mark = 1;
5153 break;
5154 }
5155
5156 /* Before we invoke realpath, which can get expensive when many
5157 files are involved, do a quick comparison of the basenames. */
5158 if (!basenames_may_differ
5159 && !file_matcher (lbasename (file_data->file_names[j]),
5160 true))
5161 continue;
5162
5163 this_real_name = dw2_get_real_path (objfile, file_data, j);
5164 if (file_matcher (this_real_name, false))
5165 {
5166 per_cu->v.quick->mark = 1;
5167 break;
5168 }
5169 }
5170
5171 void **slot = htab_find_slot (per_cu->v.quick->mark
5172 ? visited_found.get ()
5173 : visited_not_found.get (),
5174 file_data, INSERT);
5175 *slot = file_data;
5176 }
5177 }
5178
5179 static void
5180 dw2_expand_symtabs_matching
5181 (struct objfile *objfile,
5182 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5183 const lookup_name_info &lookup_name,
5184 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5185 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5186 enum search_domain kind)
5187 {
5188 struct dwarf2_per_objfile *dwarf2_per_objfile
5189 = get_dwarf2_per_objfile (objfile);
5190
5191 /* index_table is NULL if OBJF_READNOW. */
5192 if (!dwarf2_per_objfile->index_table)
5193 return;
5194
5195 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5196
5197 mapped_index &index = *dwarf2_per_objfile->index_table;
5198
5199 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5200 symbol_matcher,
5201 kind, [&] (offset_type idx)
5202 {
5203 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5204 expansion_notify, kind);
5205 return true;
5206 });
5207 }
5208
5209 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5210 symtab. */
5211
5212 static struct compunit_symtab *
5213 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5214 CORE_ADDR pc)
5215 {
5216 int i;
5217
5218 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5219 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5220 return cust;
5221
5222 if (cust->includes == NULL)
5223 return NULL;
5224
5225 for (i = 0; cust->includes[i]; ++i)
5226 {
5227 struct compunit_symtab *s = cust->includes[i];
5228
5229 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5230 if (s != NULL)
5231 return s;
5232 }
5233
5234 return NULL;
5235 }
5236
5237 static struct compunit_symtab *
5238 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5239 struct bound_minimal_symbol msymbol,
5240 CORE_ADDR pc,
5241 struct obj_section *section,
5242 int warn_if_readin)
5243 {
5244 struct dwarf2_per_cu_data *data;
5245 struct compunit_symtab *result;
5246
5247 if (!objfile->partial_symtabs->psymtabs_addrmap)
5248 return NULL;
5249
5250 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5251 SECT_OFF_TEXT (objfile));
5252 data = (struct dwarf2_per_cu_data *) addrmap_find
5253 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5254 if (!data)
5255 return NULL;
5256
5257 if (warn_if_readin && data->v.quick->compunit_symtab)
5258 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5259 paddress (get_objfile_arch (objfile), pc));
5260
5261 result
5262 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5263 false),
5264 pc);
5265 gdb_assert (result != NULL);
5266 return result;
5267 }
5268
5269 static void
5270 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5271 void *data, int need_fullname)
5272 {
5273 struct dwarf2_per_objfile *dwarf2_per_objfile
5274 = get_dwarf2_per_objfile (objfile);
5275
5276 if (!dwarf2_per_objfile->filenames_cache)
5277 {
5278 dwarf2_per_objfile->filenames_cache.emplace ();
5279
5280 htab_up visited (htab_create_alloc (10,
5281 htab_hash_pointer, htab_eq_pointer,
5282 NULL, xcalloc, xfree));
5283
5284 /* The rule is CUs specify all the files, including those used
5285 by any TU, so there's no need to scan TUs here. We can
5286 ignore file names coming from already-expanded CUs. */
5287
5288 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5289 {
5290 if (per_cu->v.quick->compunit_symtab)
5291 {
5292 void **slot = htab_find_slot (visited.get (),
5293 per_cu->v.quick->file_names,
5294 INSERT);
5295
5296 *slot = per_cu->v.quick->file_names;
5297 }
5298 }
5299
5300 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5301 {
5302 /* We only need to look at symtabs not already expanded. */
5303 if (per_cu->v.quick->compunit_symtab)
5304 continue;
5305
5306 quick_file_names *file_data = dw2_get_file_names (per_cu);
5307 if (file_data == NULL)
5308 continue;
5309
5310 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5311 if (*slot)
5312 {
5313 /* Already visited. */
5314 continue;
5315 }
5316 *slot = file_data;
5317
5318 for (int j = 0; j < file_data->num_file_names; ++j)
5319 {
5320 const char *filename = file_data->file_names[j];
5321 dwarf2_per_objfile->filenames_cache->seen (filename);
5322 }
5323 }
5324 }
5325
5326 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5327 {
5328 gdb::unique_xmalloc_ptr<char> this_real_name;
5329
5330 if (need_fullname)
5331 this_real_name = gdb_realpath (filename);
5332 (*fun) (filename, this_real_name.get (), data);
5333 });
5334 }
5335
5336 static int
5337 dw2_has_symbols (struct objfile *objfile)
5338 {
5339 return 1;
5340 }
5341
5342 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5343 {
5344 dw2_has_symbols,
5345 dw2_find_last_source_symtab,
5346 dw2_forget_cached_source_info,
5347 dw2_map_symtabs_matching_filename,
5348 dw2_lookup_symbol,
5349 dw2_print_stats,
5350 dw2_dump,
5351 dw2_expand_symtabs_for_function,
5352 dw2_expand_all_symtabs,
5353 dw2_expand_symtabs_with_fullname,
5354 dw2_map_matching_symbols,
5355 dw2_expand_symtabs_matching,
5356 dw2_find_pc_sect_compunit_symtab,
5357 NULL,
5358 dw2_map_symbol_filenames
5359 };
5360
5361 /* DWARF-5 debug_names reader. */
5362
5363 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5364 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5365
5366 /* A helper function that reads the .debug_names section in SECTION
5367 and fills in MAP. FILENAME is the name of the file containing the
5368 section; it is used for error reporting.
5369
5370 Returns true if all went well, false otherwise. */
5371
5372 static bool
5373 read_debug_names_from_section (struct objfile *objfile,
5374 const char *filename,
5375 struct dwarf2_section_info *section,
5376 mapped_debug_names &map)
5377 {
5378 if (dwarf2_section_empty_p (section))
5379 return false;
5380
5381 /* Older elfutils strip versions could keep the section in the main
5382 executable while splitting it for the separate debug info file. */
5383 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5384 return false;
5385
5386 dwarf2_read_section (objfile, section);
5387
5388 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5389
5390 const gdb_byte *addr = section->buffer;
5391
5392 bfd *const abfd = get_section_bfd_owner (section);
5393
5394 unsigned int bytes_read;
5395 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5396 addr += bytes_read;
5397
5398 map.dwarf5_is_dwarf64 = bytes_read != 4;
5399 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5400 if (bytes_read + length != section->size)
5401 {
5402 /* There may be multiple per-CU indices. */
5403 warning (_("Section .debug_names in %s length %s does not match "
5404 "section length %s, ignoring .debug_names."),
5405 filename, plongest (bytes_read + length),
5406 pulongest (section->size));
5407 return false;
5408 }
5409
5410 /* The version number. */
5411 uint16_t version = read_2_bytes (abfd, addr);
5412 addr += 2;
5413 if (version != 5)
5414 {
5415 warning (_("Section .debug_names in %s has unsupported version %d, "
5416 "ignoring .debug_names."),
5417 filename, version);
5418 return false;
5419 }
5420
5421 /* Padding. */
5422 uint16_t padding = read_2_bytes (abfd, addr);
5423 addr += 2;
5424 if (padding != 0)
5425 {
5426 warning (_("Section .debug_names in %s has unsupported padding %d, "
5427 "ignoring .debug_names."),
5428 filename, padding);
5429 return false;
5430 }
5431
5432 /* comp_unit_count - The number of CUs in the CU list. */
5433 map.cu_count = read_4_bytes (abfd, addr);
5434 addr += 4;
5435
5436 /* local_type_unit_count - The number of TUs in the local TU
5437 list. */
5438 map.tu_count = read_4_bytes (abfd, addr);
5439 addr += 4;
5440
5441 /* foreign_type_unit_count - The number of TUs in the foreign TU
5442 list. */
5443 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5444 addr += 4;
5445 if (foreign_tu_count != 0)
5446 {
5447 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5448 "ignoring .debug_names."),
5449 filename, static_cast<unsigned long> (foreign_tu_count));
5450 return false;
5451 }
5452
5453 /* bucket_count - The number of hash buckets in the hash lookup
5454 table. */
5455 map.bucket_count = read_4_bytes (abfd, addr);
5456 addr += 4;
5457
5458 /* name_count - The number of unique names in the index. */
5459 map.name_count = read_4_bytes (abfd, addr);
5460 addr += 4;
5461
5462 /* abbrev_table_size - The size in bytes of the abbreviations
5463 table. */
5464 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5465 addr += 4;
5466
5467 /* augmentation_string_size - The size in bytes of the augmentation
5468 string. This value is rounded up to a multiple of 4. */
5469 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5470 addr += 4;
5471 map.augmentation_is_gdb = ((augmentation_string_size
5472 == sizeof (dwarf5_augmentation))
5473 && memcmp (addr, dwarf5_augmentation,
5474 sizeof (dwarf5_augmentation)) == 0);
5475 augmentation_string_size += (-augmentation_string_size) & 3;
5476 addr += augmentation_string_size;
5477
5478 /* List of CUs */
5479 map.cu_table_reordered = addr;
5480 addr += map.cu_count * map.offset_size;
5481
5482 /* List of Local TUs */
5483 map.tu_table_reordered = addr;
5484 addr += map.tu_count * map.offset_size;
5485
5486 /* Hash Lookup Table */
5487 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5488 addr += map.bucket_count * 4;
5489 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5490 addr += map.name_count * 4;
5491
5492 /* Name Table */
5493 map.name_table_string_offs_reordered = addr;
5494 addr += map.name_count * map.offset_size;
5495 map.name_table_entry_offs_reordered = addr;
5496 addr += map.name_count * map.offset_size;
5497
5498 const gdb_byte *abbrev_table_start = addr;
5499 for (;;)
5500 {
5501 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5502 addr += bytes_read;
5503 if (index_num == 0)
5504 break;
5505
5506 const auto insertpair
5507 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5508 if (!insertpair.second)
5509 {
5510 warning (_("Section .debug_names in %s has duplicate index %s, "
5511 "ignoring .debug_names."),
5512 filename, pulongest (index_num));
5513 return false;
5514 }
5515 mapped_debug_names::index_val &indexval = insertpair.first->second;
5516 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5517 addr += bytes_read;
5518
5519 for (;;)
5520 {
5521 mapped_debug_names::index_val::attr attr;
5522 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5523 addr += bytes_read;
5524 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5525 addr += bytes_read;
5526 if (attr.form == DW_FORM_implicit_const)
5527 {
5528 attr.implicit_const = read_signed_leb128 (abfd, addr,
5529 &bytes_read);
5530 addr += bytes_read;
5531 }
5532 if (attr.dw_idx == 0 && attr.form == 0)
5533 break;
5534 indexval.attr_vec.push_back (std::move (attr));
5535 }
5536 }
5537 if (addr != abbrev_table_start + abbrev_table_size)
5538 {
5539 warning (_("Section .debug_names in %s has abbreviation_table "
5540 "of size %s vs. written as %u, ignoring .debug_names."),
5541 filename, plongest (addr - abbrev_table_start),
5542 abbrev_table_size);
5543 return false;
5544 }
5545 map.entry_pool = addr;
5546
5547 return true;
5548 }
5549
5550 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5551 list. */
5552
5553 static void
5554 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5555 const mapped_debug_names &map,
5556 dwarf2_section_info &section,
5557 bool is_dwz)
5558 {
5559 sect_offset sect_off_prev;
5560 for (uint32_t i = 0; i <= map.cu_count; ++i)
5561 {
5562 sect_offset sect_off_next;
5563 if (i < map.cu_count)
5564 {
5565 sect_off_next
5566 = (sect_offset) (extract_unsigned_integer
5567 (map.cu_table_reordered + i * map.offset_size,
5568 map.offset_size,
5569 map.dwarf5_byte_order));
5570 }
5571 else
5572 sect_off_next = (sect_offset) section.size;
5573 if (i >= 1)
5574 {
5575 const ULONGEST length = sect_off_next - sect_off_prev;
5576 dwarf2_per_cu_data *per_cu
5577 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5578 sect_off_prev, length);
5579 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5580 }
5581 sect_off_prev = sect_off_next;
5582 }
5583 }
5584
5585 /* Read the CU list from the mapped index, and use it to create all
5586 the CU objects for this dwarf2_per_objfile. */
5587
5588 static void
5589 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5590 const mapped_debug_names &map,
5591 const mapped_debug_names &dwz_map)
5592 {
5593 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5594 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5595
5596 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5597 dwarf2_per_objfile->info,
5598 false /* is_dwz */);
5599
5600 if (dwz_map.cu_count == 0)
5601 return;
5602
5603 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5604 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5605 true /* is_dwz */);
5606 }
5607
5608 /* Read .debug_names. If everything went ok, initialize the "quick"
5609 elements of all the CUs and return true. Otherwise, return false. */
5610
5611 static bool
5612 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5613 {
5614 std::unique_ptr<mapped_debug_names> map
5615 (new mapped_debug_names (dwarf2_per_objfile));
5616 mapped_debug_names dwz_map (dwarf2_per_objfile);
5617 struct objfile *objfile = dwarf2_per_objfile->objfile;
5618
5619 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5620 &dwarf2_per_objfile->debug_names,
5621 *map))
5622 return false;
5623
5624 /* Don't use the index if it's empty. */
5625 if (map->name_count == 0)
5626 return false;
5627
5628 /* If there is a .dwz file, read it so we can get its CU list as
5629 well. */
5630 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5631 if (dwz != NULL)
5632 {
5633 if (!read_debug_names_from_section (objfile,
5634 bfd_get_filename (dwz->dwz_bfd.get ()),
5635 &dwz->debug_names, dwz_map))
5636 {
5637 warning (_("could not read '.debug_names' section from %s; skipping"),
5638 bfd_get_filename (dwz->dwz_bfd.get ()));
5639 return false;
5640 }
5641 }
5642
5643 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5644
5645 if (map->tu_count != 0)
5646 {
5647 /* We can only handle a single .debug_types when we have an
5648 index. */
5649 if (dwarf2_per_objfile->types.size () != 1)
5650 return false;
5651
5652 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5653
5654 create_signatured_type_table_from_debug_names
5655 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5656 }
5657
5658 create_addrmap_from_aranges (dwarf2_per_objfile,
5659 &dwarf2_per_objfile->debug_aranges);
5660
5661 dwarf2_per_objfile->debug_names_table = std::move (map);
5662 dwarf2_per_objfile->using_index = 1;
5663 dwarf2_per_objfile->quick_file_names_table =
5664 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5665
5666 return true;
5667 }
5668
5669 /* Type used to manage iterating over all CUs looking for a symbol for
5670 .debug_names. */
5671
5672 class dw2_debug_names_iterator
5673 {
5674 public:
5675 dw2_debug_names_iterator (const mapped_debug_names &map,
5676 gdb::optional<block_enum> block_index,
5677 domain_enum domain,
5678 const char *name)
5679 : m_map (map), m_block_index (block_index), m_domain (domain),
5680 m_addr (find_vec_in_debug_names (map, name))
5681 {}
5682
5683 dw2_debug_names_iterator (const mapped_debug_names &map,
5684 search_domain search, uint32_t namei)
5685 : m_map (map),
5686 m_search (search),
5687 m_addr (find_vec_in_debug_names (map, namei))
5688 {}
5689
5690 dw2_debug_names_iterator (const mapped_debug_names &map,
5691 block_enum block_index, domain_enum domain,
5692 uint32_t namei)
5693 : m_map (map), m_block_index (block_index), m_domain (domain),
5694 m_addr (find_vec_in_debug_names (map, namei))
5695 {}
5696
5697 /* Return the next matching CU or NULL if there are no more. */
5698 dwarf2_per_cu_data *next ();
5699
5700 private:
5701 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5702 const char *name);
5703 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5704 uint32_t namei);
5705
5706 /* The internalized form of .debug_names. */
5707 const mapped_debug_names &m_map;
5708
5709 /* If set, only look for symbols that match that block. Valid values are
5710 GLOBAL_BLOCK and STATIC_BLOCK. */
5711 const gdb::optional<block_enum> m_block_index;
5712
5713 /* The kind of symbol we're looking for. */
5714 const domain_enum m_domain = UNDEF_DOMAIN;
5715 const search_domain m_search = ALL_DOMAIN;
5716
5717 /* The list of CUs from the index entry of the symbol, or NULL if
5718 not found. */
5719 const gdb_byte *m_addr;
5720 };
5721
5722 const char *
5723 mapped_debug_names::namei_to_name (uint32_t namei) const
5724 {
5725 const ULONGEST namei_string_offs
5726 = extract_unsigned_integer ((name_table_string_offs_reordered
5727 + namei * offset_size),
5728 offset_size,
5729 dwarf5_byte_order);
5730 return read_indirect_string_at_offset
5731 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5732 }
5733
5734 /* Find a slot in .debug_names for the object named NAME. If NAME is
5735 found, return pointer to its pool data. If NAME cannot be found,
5736 return NULL. */
5737
5738 const gdb_byte *
5739 dw2_debug_names_iterator::find_vec_in_debug_names
5740 (const mapped_debug_names &map, const char *name)
5741 {
5742 int (*cmp) (const char *, const char *);
5743
5744 gdb::unique_xmalloc_ptr<char> without_params;
5745 if (current_language->la_language == language_cplus
5746 || current_language->la_language == language_fortran
5747 || current_language->la_language == language_d)
5748 {
5749 /* NAME is already canonical. Drop any qualifiers as
5750 .debug_names does not contain any. */
5751
5752 if (strchr (name, '(') != NULL)
5753 {
5754 without_params = cp_remove_params (name);
5755 if (without_params != NULL)
5756 name = without_params.get ();
5757 }
5758 }
5759
5760 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5761
5762 const uint32_t full_hash = dwarf5_djb_hash (name);
5763 uint32_t namei
5764 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5765 (map.bucket_table_reordered
5766 + (full_hash % map.bucket_count)), 4,
5767 map.dwarf5_byte_order);
5768 if (namei == 0)
5769 return NULL;
5770 --namei;
5771 if (namei >= map.name_count)
5772 {
5773 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5774 "[in module %s]"),
5775 namei, map.name_count,
5776 objfile_name (map.dwarf2_per_objfile->objfile));
5777 return NULL;
5778 }
5779
5780 for (;;)
5781 {
5782 const uint32_t namei_full_hash
5783 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5784 (map.hash_table_reordered + namei), 4,
5785 map.dwarf5_byte_order);
5786 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5787 return NULL;
5788
5789 if (full_hash == namei_full_hash)
5790 {
5791 const char *const namei_string = map.namei_to_name (namei);
5792
5793 #if 0 /* An expensive sanity check. */
5794 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5795 {
5796 complaint (_("Wrong .debug_names hash for string at index %u "
5797 "[in module %s]"),
5798 namei, objfile_name (dwarf2_per_objfile->objfile));
5799 return NULL;
5800 }
5801 #endif
5802
5803 if (cmp (namei_string, name) == 0)
5804 {
5805 const ULONGEST namei_entry_offs
5806 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5807 + namei * map.offset_size),
5808 map.offset_size, map.dwarf5_byte_order);
5809 return map.entry_pool + namei_entry_offs;
5810 }
5811 }
5812
5813 ++namei;
5814 if (namei >= map.name_count)
5815 return NULL;
5816 }
5817 }
5818
5819 const gdb_byte *
5820 dw2_debug_names_iterator::find_vec_in_debug_names
5821 (const mapped_debug_names &map, uint32_t namei)
5822 {
5823 if (namei >= map.name_count)
5824 {
5825 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5826 "[in module %s]"),
5827 namei, map.name_count,
5828 objfile_name (map.dwarf2_per_objfile->objfile));
5829 return NULL;
5830 }
5831
5832 const ULONGEST namei_entry_offs
5833 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5834 + namei * map.offset_size),
5835 map.offset_size, map.dwarf5_byte_order);
5836 return map.entry_pool + namei_entry_offs;
5837 }
5838
5839 /* See dw2_debug_names_iterator. */
5840
5841 dwarf2_per_cu_data *
5842 dw2_debug_names_iterator::next ()
5843 {
5844 if (m_addr == NULL)
5845 return NULL;
5846
5847 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5848 struct objfile *objfile = dwarf2_per_objfile->objfile;
5849 bfd *const abfd = objfile->obfd;
5850
5851 again:
5852
5853 unsigned int bytes_read;
5854 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5855 m_addr += bytes_read;
5856 if (abbrev == 0)
5857 return NULL;
5858
5859 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5860 if (indexval_it == m_map.abbrev_map.cend ())
5861 {
5862 complaint (_("Wrong .debug_names undefined abbrev code %s "
5863 "[in module %s]"),
5864 pulongest (abbrev), objfile_name (objfile));
5865 return NULL;
5866 }
5867 const mapped_debug_names::index_val &indexval = indexval_it->second;
5868 enum class symbol_linkage {
5869 unknown,
5870 static_,
5871 extern_,
5872 } symbol_linkage_ = symbol_linkage::unknown;
5873 dwarf2_per_cu_data *per_cu = NULL;
5874 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5875 {
5876 ULONGEST ull;
5877 switch (attr.form)
5878 {
5879 case DW_FORM_implicit_const:
5880 ull = attr.implicit_const;
5881 break;
5882 case DW_FORM_flag_present:
5883 ull = 1;
5884 break;
5885 case DW_FORM_udata:
5886 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5887 m_addr += bytes_read;
5888 break;
5889 default:
5890 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5891 dwarf_form_name (attr.form),
5892 objfile_name (objfile));
5893 return NULL;
5894 }
5895 switch (attr.dw_idx)
5896 {
5897 case DW_IDX_compile_unit:
5898 /* Don't crash on bad data. */
5899 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5900 {
5901 complaint (_(".debug_names entry has bad CU index %s"
5902 " [in module %s]"),
5903 pulongest (ull),
5904 objfile_name (dwarf2_per_objfile->objfile));
5905 continue;
5906 }
5907 per_cu = dwarf2_per_objfile->get_cutu (ull);
5908 break;
5909 case DW_IDX_type_unit:
5910 /* Don't crash on bad data. */
5911 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5912 {
5913 complaint (_(".debug_names entry has bad TU index %s"
5914 " [in module %s]"),
5915 pulongest (ull),
5916 objfile_name (dwarf2_per_objfile->objfile));
5917 continue;
5918 }
5919 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5920 break;
5921 case DW_IDX_GNU_internal:
5922 if (!m_map.augmentation_is_gdb)
5923 break;
5924 symbol_linkage_ = symbol_linkage::static_;
5925 break;
5926 case DW_IDX_GNU_external:
5927 if (!m_map.augmentation_is_gdb)
5928 break;
5929 symbol_linkage_ = symbol_linkage::extern_;
5930 break;
5931 }
5932 }
5933
5934 /* Skip if already read in. */
5935 if (per_cu->v.quick->compunit_symtab)
5936 goto again;
5937
5938 /* Check static vs global. */
5939 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5940 {
5941 const bool want_static = *m_block_index == STATIC_BLOCK;
5942 const bool symbol_is_static =
5943 symbol_linkage_ == symbol_linkage::static_;
5944 if (want_static != symbol_is_static)
5945 goto again;
5946 }
5947
5948 /* Match dw2_symtab_iter_next, symbol_kind
5949 and debug_names::psymbol_tag. */
5950 switch (m_domain)
5951 {
5952 case VAR_DOMAIN:
5953 switch (indexval.dwarf_tag)
5954 {
5955 case DW_TAG_variable:
5956 case DW_TAG_subprogram:
5957 /* Some types are also in VAR_DOMAIN. */
5958 case DW_TAG_typedef:
5959 case DW_TAG_structure_type:
5960 break;
5961 default:
5962 goto again;
5963 }
5964 break;
5965 case STRUCT_DOMAIN:
5966 switch (indexval.dwarf_tag)
5967 {
5968 case DW_TAG_typedef:
5969 case DW_TAG_structure_type:
5970 break;
5971 default:
5972 goto again;
5973 }
5974 break;
5975 case LABEL_DOMAIN:
5976 switch (indexval.dwarf_tag)
5977 {
5978 case 0:
5979 case DW_TAG_variable:
5980 break;
5981 default:
5982 goto again;
5983 }
5984 break;
5985 case MODULE_DOMAIN:
5986 switch (indexval.dwarf_tag)
5987 {
5988 case DW_TAG_module:
5989 break;
5990 default:
5991 goto again;
5992 }
5993 break;
5994 default:
5995 break;
5996 }
5997
5998 /* Match dw2_expand_symtabs_matching, symbol_kind and
5999 debug_names::psymbol_tag. */
6000 switch (m_search)
6001 {
6002 case VARIABLES_DOMAIN:
6003 switch (indexval.dwarf_tag)
6004 {
6005 case DW_TAG_variable:
6006 break;
6007 default:
6008 goto again;
6009 }
6010 break;
6011 case FUNCTIONS_DOMAIN:
6012 switch (indexval.dwarf_tag)
6013 {
6014 case DW_TAG_subprogram:
6015 break;
6016 default:
6017 goto again;
6018 }
6019 break;
6020 case TYPES_DOMAIN:
6021 switch (indexval.dwarf_tag)
6022 {
6023 case DW_TAG_typedef:
6024 case DW_TAG_structure_type:
6025 break;
6026 default:
6027 goto again;
6028 }
6029 break;
6030 case MODULES_DOMAIN:
6031 switch (indexval.dwarf_tag)
6032 {
6033 case DW_TAG_module:
6034 break;
6035 default:
6036 goto again;
6037 }
6038 default:
6039 break;
6040 }
6041
6042 return per_cu;
6043 }
6044
6045 static struct compunit_symtab *
6046 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6047 const char *name, domain_enum domain)
6048 {
6049 struct dwarf2_per_objfile *dwarf2_per_objfile
6050 = get_dwarf2_per_objfile (objfile);
6051
6052 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6053 if (!mapp)
6054 {
6055 /* index is NULL if OBJF_READNOW. */
6056 return NULL;
6057 }
6058 const auto &map = *mapp;
6059
6060 dw2_debug_names_iterator iter (map, block_index, domain, name);
6061
6062 struct compunit_symtab *stab_best = NULL;
6063 struct dwarf2_per_cu_data *per_cu;
6064 while ((per_cu = iter.next ()) != NULL)
6065 {
6066 struct symbol *sym, *with_opaque = NULL;
6067 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6068 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6069 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6070
6071 sym = block_find_symbol (block, name, domain,
6072 block_find_non_opaque_type_preferred,
6073 &with_opaque);
6074
6075 /* Some caution must be observed with overloaded functions and
6076 methods, since the index will not contain any overload
6077 information (but NAME might contain it). */
6078
6079 if (sym != NULL
6080 && strcmp_iw (sym->search_name (), name) == 0)
6081 return stab;
6082 if (with_opaque != NULL
6083 && strcmp_iw (with_opaque->search_name (), name) == 0)
6084 stab_best = stab;
6085
6086 /* Keep looking through other CUs. */
6087 }
6088
6089 return stab_best;
6090 }
6091
6092 /* This dumps minimal information about .debug_names. It is called
6093 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6094 uses this to verify that .debug_names has been loaded. */
6095
6096 static void
6097 dw2_debug_names_dump (struct objfile *objfile)
6098 {
6099 struct dwarf2_per_objfile *dwarf2_per_objfile
6100 = get_dwarf2_per_objfile (objfile);
6101
6102 gdb_assert (dwarf2_per_objfile->using_index);
6103 printf_filtered (".debug_names:");
6104 if (dwarf2_per_objfile->debug_names_table)
6105 printf_filtered (" exists\n");
6106 else
6107 printf_filtered (" faked for \"readnow\"\n");
6108 printf_filtered ("\n");
6109 }
6110
6111 static void
6112 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6113 const char *func_name)
6114 {
6115 struct dwarf2_per_objfile *dwarf2_per_objfile
6116 = get_dwarf2_per_objfile (objfile);
6117
6118 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6119 if (dwarf2_per_objfile->debug_names_table)
6120 {
6121 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6122
6123 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6124
6125 struct dwarf2_per_cu_data *per_cu;
6126 while ((per_cu = iter.next ()) != NULL)
6127 dw2_instantiate_symtab (per_cu, false);
6128 }
6129 }
6130
6131 static void
6132 dw2_debug_names_map_matching_symbols
6133 (struct objfile *objfile,
6134 const lookup_name_info &name, domain_enum domain,
6135 int global,
6136 gdb::function_view<symbol_found_callback_ftype> callback,
6137 symbol_compare_ftype *ordered_compare)
6138 {
6139 struct dwarf2_per_objfile *dwarf2_per_objfile
6140 = get_dwarf2_per_objfile (objfile);
6141
6142 /* debug_names_table is NULL if OBJF_READNOW. */
6143 if (!dwarf2_per_objfile->debug_names_table)
6144 return;
6145
6146 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6147 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6148
6149 const char *match_name = name.ada ().lookup_name ().c_str ();
6150 auto matcher = [&] (const char *symname)
6151 {
6152 if (ordered_compare == nullptr)
6153 return true;
6154 return ordered_compare (symname, match_name) == 0;
6155 };
6156
6157 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6158 [&] (offset_type namei)
6159 {
6160 /* The name was matched, now expand corresponding CUs that were
6161 marked. */
6162 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6163
6164 struct dwarf2_per_cu_data *per_cu;
6165 while ((per_cu = iter.next ()) != NULL)
6166 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6167 return true;
6168 });
6169
6170 /* It's a shame we couldn't do this inside the
6171 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6172 that have already been expanded. Instead, this loop matches what
6173 the psymtab code does. */
6174 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6175 {
6176 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6177 if (cust != nullptr)
6178 {
6179 const struct block *block
6180 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6181 if (!iterate_over_symbols_terminated (block, name,
6182 domain, callback))
6183 break;
6184 }
6185 }
6186 }
6187
6188 static void
6189 dw2_debug_names_expand_symtabs_matching
6190 (struct objfile *objfile,
6191 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6192 const lookup_name_info &lookup_name,
6193 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6194 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6195 enum search_domain kind)
6196 {
6197 struct dwarf2_per_objfile *dwarf2_per_objfile
6198 = get_dwarf2_per_objfile (objfile);
6199
6200 /* debug_names_table is NULL if OBJF_READNOW. */
6201 if (!dwarf2_per_objfile->debug_names_table)
6202 return;
6203
6204 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6205
6206 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6207
6208 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6209 symbol_matcher,
6210 kind, [&] (offset_type namei)
6211 {
6212 /* The name was matched, now expand corresponding CUs that were
6213 marked. */
6214 dw2_debug_names_iterator iter (map, kind, namei);
6215
6216 struct dwarf2_per_cu_data *per_cu;
6217 while ((per_cu = iter.next ()) != NULL)
6218 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6219 expansion_notify);
6220 return true;
6221 });
6222 }
6223
6224 const struct quick_symbol_functions dwarf2_debug_names_functions =
6225 {
6226 dw2_has_symbols,
6227 dw2_find_last_source_symtab,
6228 dw2_forget_cached_source_info,
6229 dw2_map_symtabs_matching_filename,
6230 dw2_debug_names_lookup_symbol,
6231 dw2_print_stats,
6232 dw2_debug_names_dump,
6233 dw2_debug_names_expand_symtabs_for_function,
6234 dw2_expand_all_symtabs,
6235 dw2_expand_symtabs_with_fullname,
6236 dw2_debug_names_map_matching_symbols,
6237 dw2_debug_names_expand_symtabs_matching,
6238 dw2_find_pc_sect_compunit_symtab,
6239 NULL,
6240 dw2_map_symbol_filenames
6241 };
6242
6243 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6244 to either a dwarf2_per_objfile or dwz_file object. */
6245
6246 template <typename T>
6247 static gdb::array_view<const gdb_byte>
6248 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6249 {
6250 dwarf2_section_info *section = &section_owner->gdb_index;
6251
6252 if (dwarf2_section_empty_p (section))
6253 return {};
6254
6255 /* Older elfutils strip versions could keep the section in the main
6256 executable while splitting it for the separate debug info file. */
6257 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6258 return {};
6259
6260 dwarf2_read_section (obj, section);
6261
6262 /* dwarf2_section_info::size is a bfd_size_type, while
6263 gdb::array_view works with size_t. On 32-bit hosts, with
6264 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6265 is 32-bit. So we need an explicit narrowing conversion here.
6266 This is fine, because it's impossible to allocate or mmap an
6267 array/buffer larger than what size_t can represent. */
6268 return gdb::make_array_view (section->buffer, section->size);
6269 }
6270
6271 /* Lookup the index cache for the contents of the index associated to
6272 DWARF2_OBJ. */
6273
6274 static gdb::array_view<const gdb_byte>
6275 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6276 {
6277 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6278 if (build_id == nullptr)
6279 return {};
6280
6281 return global_index_cache.lookup_gdb_index (build_id,
6282 &dwarf2_obj->index_cache_res);
6283 }
6284
6285 /* Same as the above, but for DWZ. */
6286
6287 static gdb::array_view<const gdb_byte>
6288 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6289 {
6290 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6291 if (build_id == nullptr)
6292 return {};
6293
6294 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6295 }
6296
6297 /* See symfile.h. */
6298
6299 bool
6300 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6301 {
6302 struct dwarf2_per_objfile *dwarf2_per_objfile
6303 = get_dwarf2_per_objfile (objfile);
6304
6305 /* If we're about to read full symbols, don't bother with the
6306 indices. In this case we also don't care if some other debug
6307 format is making psymtabs, because they are all about to be
6308 expanded anyway. */
6309 if ((objfile->flags & OBJF_READNOW))
6310 {
6311 dwarf2_per_objfile->using_index = 1;
6312 create_all_comp_units (dwarf2_per_objfile);
6313 create_all_type_units (dwarf2_per_objfile);
6314 dwarf2_per_objfile->quick_file_names_table
6315 = create_quick_file_names_table
6316 (dwarf2_per_objfile->all_comp_units.size ());
6317
6318 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6319 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6320 {
6321 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6322
6323 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6324 struct dwarf2_per_cu_quick_data);
6325 }
6326
6327 /* Return 1 so that gdb sees the "quick" functions. However,
6328 these functions will be no-ops because we will have expanded
6329 all symtabs. */
6330 *index_kind = dw_index_kind::GDB_INDEX;
6331 return true;
6332 }
6333
6334 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6335 {
6336 *index_kind = dw_index_kind::DEBUG_NAMES;
6337 return true;
6338 }
6339
6340 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6341 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6342 get_gdb_index_contents_from_section<dwz_file>))
6343 {
6344 *index_kind = dw_index_kind::GDB_INDEX;
6345 return true;
6346 }
6347
6348 /* ... otherwise, try to find the index in the index cache. */
6349 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6350 get_gdb_index_contents_from_cache,
6351 get_gdb_index_contents_from_cache_dwz))
6352 {
6353 global_index_cache.hit ();
6354 *index_kind = dw_index_kind::GDB_INDEX;
6355 return true;
6356 }
6357
6358 global_index_cache.miss ();
6359 return false;
6360 }
6361
6362 \f
6363
6364 /* Build a partial symbol table. */
6365
6366 void
6367 dwarf2_build_psymtabs (struct objfile *objfile)
6368 {
6369 struct dwarf2_per_objfile *dwarf2_per_objfile
6370 = get_dwarf2_per_objfile (objfile);
6371
6372 init_psymbol_list (objfile, 1024);
6373
6374 try
6375 {
6376 /* This isn't really ideal: all the data we allocate on the
6377 objfile's obstack is still uselessly kept around. However,
6378 freeing it seems unsafe. */
6379 psymtab_discarder psymtabs (objfile);
6380 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6381 psymtabs.keep ();
6382
6383 /* (maybe) store an index in the cache. */
6384 global_index_cache.store (dwarf2_per_objfile);
6385 }
6386 catch (const gdb_exception_error &except)
6387 {
6388 exception_print (gdb_stderr, except);
6389 }
6390 }
6391
6392 /* Return the total length of the CU described by HEADER. */
6393
6394 static unsigned int
6395 get_cu_length (const struct comp_unit_head *header)
6396 {
6397 return header->initial_length_size + header->length;
6398 }
6399
6400 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6401
6402 static inline bool
6403 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6404 {
6405 sect_offset bottom = cu_header->sect_off;
6406 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6407
6408 return sect_off >= bottom && sect_off < top;
6409 }
6410
6411 /* Find the base address of the compilation unit for range lists and
6412 location lists. It will normally be specified by DW_AT_low_pc.
6413 In DWARF-3 draft 4, the base address could be overridden by
6414 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6415 compilation units with discontinuous ranges. */
6416
6417 static void
6418 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6419 {
6420 struct attribute *attr;
6421
6422 cu->base_known = 0;
6423 cu->base_address = 0;
6424
6425 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6426 if (attr != nullptr)
6427 {
6428 cu->base_address = attr_value_as_address (attr);
6429 cu->base_known = 1;
6430 }
6431 else
6432 {
6433 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6434 if (attr != nullptr)
6435 {
6436 cu->base_address = attr_value_as_address (attr);
6437 cu->base_known = 1;
6438 }
6439 }
6440 }
6441
6442 /* Read in the comp unit header information from the debug_info at info_ptr.
6443 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6444 NOTE: This leaves members offset, first_die_offset to be filled in
6445 by the caller. */
6446
6447 static const gdb_byte *
6448 read_comp_unit_head (struct comp_unit_head *cu_header,
6449 const gdb_byte *info_ptr,
6450 struct dwarf2_section_info *section,
6451 rcuh_kind section_kind)
6452 {
6453 int signed_addr;
6454 unsigned int bytes_read;
6455 const char *filename = get_section_file_name (section);
6456 bfd *abfd = get_section_bfd_owner (section);
6457
6458 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6459 cu_header->initial_length_size = bytes_read;
6460 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6461 info_ptr += bytes_read;
6462 cu_header->version = read_2_bytes (abfd, info_ptr);
6463 if (cu_header->version < 2 || cu_header->version > 5)
6464 error (_("Dwarf Error: wrong version in compilation unit header "
6465 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6466 cu_header->version, filename);
6467 info_ptr += 2;
6468 if (cu_header->version < 5)
6469 switch (section_kind)
6470 {
6471 case rcuh_kind::COMPILE:
6472 cu_header->unit_type = DW_UT_compile;
6473 break;
6474 case rcuh_kind::TYPE:
6475 cu_header->unit_type = DW_UT_type;
6476 break;
6477 default:
6478 internal_error (__FILE__, __LINE__,
6479 _("read_comp_unit_head: invalid section_kind"));
6480 }
6481 else
6482 {
6483 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6484 (read_1_byte (abfd, info_ptr));
6485 info_ptr += 1;
6486 switch (cu_header->unit_type)
6487 {
6488 case DW_UT_compile:
6489 case DW_UT_partial:
6490 case DW_UT_skeleton:
6491 case DW_UT_split_compile:
6492 if (section_kind != rcuh_kind::COMPILE)
6493 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6494 "(is %s, should be %s) [in module %s]"),
6495 dwarf_unit_type_name (cu_header->unit_type),
6496 dwarf_unit_type_name (DW_UT_type), filename);
6497 break;
6498 case DW_UT_type:
6499 case DW_UT_split_type:
6500 section_kind = rcuh_kind::TYPE;
6501 break;
6502 default:
6503 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6504 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6505 "[in module %s]"), cu_header->unit_type,
6506 dwarf_unit_type_name (DW_UT_compile),
6507 dwarf_unit_type_name (DW_UT_skeleton),
6508 dwarf_unit_type_name (DW_UT_split_compile),
6509 dwarf_unit_type_name (DW_UT_type),
6510 dwarf_unit_type_name (DW_UT_split_type), filename);
6511 }
6512
6513 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6514 info_ptr += 1;
6515 }
6516 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6517 cu_header,
6518 &bytes_read);
6519 info_ptr += bytes_read;
6520 if (cu_header->version < 5)
6521 {
6522 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6523 info_ptr += 1;
6524 }
6525 signed_addr = bfd_get_sign_extend_vma (abfd);
6526 if (signed_addr < 0)
6527 internal_error (__FILE__, __LINE__,
6528 _("read_comp_unit_head: dwarf from non elf file"));
6529 cu_header->signed_addr_p = signed_addr;
6530
6531 bool header_has_signature = section_kind == rcuh_kind::TYPE
6532 || cu_header->unit_type == DW_UT_skeleton
6533 || cu_header->unit_type == DW_UT_split_compile;
6534
6535 if (header_has_signature)
6536 {
6537 cu_header->signature = read_8_bytes (abfd, info_ptr);
6538 info_ptr += 8;
6539 }
6540
6541 if (section_kind == rcuh_kind::TYPE)
6542 {
6543 LONGEST type_offset;
6544 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6545 info_ptr += bytes_read;
6546 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6547 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6548 error (_("Dwarf Error: Too big type_offset in compilation unit "
6549 "header (is %s) [in module %s]"), plongest (type_offset),
6550 filename);
6551 }
6552
6553 return info_ptr;
6554 }
6555
6556 /* Helper function that returns the proper abbrev section for
6557 THIS_CU. */
6558
6559 static struct dwarf2_section_info *
6560 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6561 {
6562 struct dwarf2_section_info *abbrev;
6563 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6564
6565 if (this_cu->is_dwz)
6566 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6567 else
6568 abbrev = &dwarf2_per_objfile->abbrev;
6569
6570 return abbrev;
6571 }
6572
6573 /* Subroutine of read_and_check_comp_unit_head and
6574 read_and_check_type_unit_head to simplify them.
6575 Perform various error checking on the header. */
6576
6577 static void
6578 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6579 struct comp_unit_head *header,
6580 struct dwarf2_section_info *section,
6581 struct dwarf2_section_info *abbrev_section)
6582 {
6583 const char *filename = get_section_file_name (section);
6584
6585 if (to_underlying (header->abbrev_sect_off)
6586 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6587 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6588 "(offset %s + 6) [in module %s]"),
6589 sect_offset_str (header->abbrev_sect_off),
6590 sect_offset_str (header->sect_off),
6591 filename);
6592
6593 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6594 avoid potential 32-bit overflow. */
6595 if (((ULONGEST) header->sect_off + get_cu_length (header))
6596 > section->size)
6597 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6598 "(offset %s + 0) [in module %s]"),
6599 header->length, sect_offset_str (header->sect_off),
6600 filename);
6601 }
6602
6603 /* Read in a CU/TU header and perform some basic error checking.
6604 The contents of the header are stored in HEADER.
6605 The result is a pointer to the start of the first DIE. */
6606
6607 static const gdb_byte *
6608 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6609 struct comp_unit_head *header,
6610 struct dwarf2_section_info *section,
6611 struct dwarf2_section_info *abbrev_section,
6612 const gdb_byte *info_ptr,
6613 rcuh_kind section_kind)
6614 {
6615 const gdb_byte *beg_of_comp_unit = info_ptr;
6616
6617 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6618
6619 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6620
6621 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6622
6623 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6624 abbrev_section);
6625
6626 return info_ptr;
6627 }
6628
6629 /* Fetch the abbreviation table offset from a comp or type unit header. */
6630
6631 static sect_offset
6632 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6633 struct dwarf2_section_info *section,
6634 sect_offset sect_off)
6635 {
6636 bfd *abfd = get_section_bfd_owner (section);
6637 const gdb_byte *info_ptr;
6638 unsigned int initial_length_size, offset_size;
6639 uint16_t version;
6640
6641 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6642 info_ptr = section->buffer + to_underlying (sect_off);
6643 read_initial_length (abfd, info_ptr, &initial_length_size);
6644 offset_size = initial_length_size == 4 ? 4 : 8;
6645 info_ptr += initial_length_size;
6646
6647 version = read_2_bytes (abfd, info_ptr);
6648 info_ptr += 2;
6649 if (version >= 5)
6650 {
6651 /* Skip unit type and address size. */
6652 info_ptr += 2;
6653 }
6654
6655 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6656 }
6657
6658 /* Allocate a new partial symtab for file named NAME and mark this new
6659 partial symtab as being an include of PST. */
6660
6661 static void
6662 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6663 struct objfile *objfile)
6664 {
6665 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6666
6667 if (!IS_ABSOLUTE_PATH (subpst->filename))
6668 {
6669 /* It shares objfile->objfile_obstack. */
6670 subpst->dirname = pst->dirname;
6671 }
6672
6673 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6674 subpst->dependencies[0] = pst;
6675 subpst->number_of_dependencies = 1;
6676
6677 subpst->read_symtab = pst->read_symtab;
6678
6679 /* No private part is necessary for include psymtabs. This property
6680 can be used to differentiate between such include psymtabs and
6681 the regular ones. */
6682 subpst->read_symtab_private = NULL;
6683 }
6684
6685 /* Read the Line Number Program data and extract the list of files
6686 included by the source file represented by PST. Build an include
6687 partial symtab for each of these included files. */
6688
6689 static void
6690 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6691 struct die_info *die,
6692 struct partial_symtab *pst)
6693 {
6694 line_header_up lh;
6695 struct attribute *attr;
6696
6697 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6698 if (attr != nullptr)
6699 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6700 if (lh == NULL)
6701 return; /* No linetable, so no includes. */
6702
6703 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6704 that we pass in the raw text_low here; that is ok because we're
6705 only decoding the line table to make include partial symtabs, and
6706 so the addresses aren't really used. */
6707 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6708 pst->raw_text_low (), 1);
6709 }
6710
6711 static hashval_t
6712 hash_signatured_type (const void *item)
6713 {
6714 const struct signatured_type *sig_type
6715 = (const struct signatured_type *) item;
6716
6717 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6718 return sig_type->signature;
6719 }
6720
6721 static int
6722 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6723 {
6724 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6725 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6726
6727 return lhs->signature == rhs->signature;
6728 }
6729
6730 /* Allocate a hash table for signatured types. */
6731
6732 static htab_t
6733 allocate_signatured_type_table (struct objfile *objfile)
6734 {
6735 return htab_create_alloc_ex (41,
6736 hash_signatured_type,
6737 eq_signatured_type,
6738 NULL,
6739 &objfile->objfile_obstack,
6740 hashtab_obstack_allocate,
6741 dummy_obstack_deallocate);
6742 }
6743
6744 /* A helper function to add a signatured type CU to a table. */
6745
6746 static int
6747 add_signatured_type_cu_to_table (void **slot, void *datum)
6748 {
6749 struct signatured_type *sigt = (struct signatured_type *) *slot;
6750 std::vector<signatured_type *> *all_type_units
6751 = (std::vector<signatured_type *> *) datum;
6752
6753 all_type_units->push_back (sigt);
6754
6755 return 1;
6756 }
6757
6758 /* A helper for create_debug_types_hash_table. Read types from SECTION
6759 and fill them into TYPES_HTAB. It will process only type units,
6760 therefore DW_UT_type. */
6761
6762 static void
6763 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6764 struct dwo_file *dwo_file,
6765 dwarf2_section_info *section, htab_t &types_htab,
6766 rcuh_kind section_kind)
6767 {
6768 struct objfile *objfile = dwarf2_per_objfile->objfile;
6769 struct dwarf2_section_info *abbrev_section;
6770 bfd *abfd;
6771 const gdb_byte *info_ptr, *end_ptr;
6772
6773 abbrev_section = (dwo_file != NULL
6774 ? &dwo_file->sections.abbrev
6775 : &dwarf2_per_objfile->abbrev);
6776
6777 if (dwarf_read_debug)
6778 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6779 get_section_name (section),
6780 get_section_file_name (abbrev_section));
6781
6782 dwarf2_read_section (objfile, section);
6783 info_ptr = section->buffer;
6784
6785 if (info_ptr == NULL)
6786 return;
6787
6788 /* We can't set abfd until now because the section may be empty or
6789 not present, in which case the bfd is unknown. */
6790 abfd = get_section_bfd_owner (section);
6791
6792 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6793 because we don't need to read any dies: the signature is in the
6794 header. */
6795
6796 end_ptr = info_ptr + section->size;
6797 while (info_ptr < end_ptr)
6798 {
6799 struct signatured_type *sig_type;
6800 struct dwo_unit *dwo_tu;
6801 void **slot;
6802 const gdb_byte *ptr = info_ptr;
6803 struct comp_unit_head header;
6804 unsigned int length;
6805
6806 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6807
6808 /* Initialize it due to a false compiler warning. */
6809 header.signature = -1;
6810 header.type_cu_offset_in_tu = (cu_offset) -1;
6811
6812 /* We need to read the type's signature in order to build the hash
6813 table, but we don't need anything else just yet. */
6814
6815 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6816 abbrev_section, ptr, section_kind);
6817
6818 length = get_cu_length (&header);
6819
6820 /* Skip dummy type units. */
6821 if (ptr >= info_ptr + length
6822 || peek_abbrev_code (abfd, ptr) == 0
6823 || header.unit_type != DW_UT_type)
6824 {
6825 info_ptr += length;
6826 continue;
6827 }
6828
6829 if (types_htab == NULL)
6830 {
6831 if (dwo_file)
6832 types_htab = allocate_dwo_unit_table (objfile);
6833 else
6834 types_htab = allocate_signatured_type_table (objfile);
6835 }
6836
6837 if (dwo_file)
6838 {
6839 sig_type = NULL;
6840 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6841 struct dwo_unit);
6842 dwo_tu->dwo_file = dwo_file;
6843 dwo_tu->signature = header.signature;
6844 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6845 dwo_tu->section = section;
6846 dwo_tu->sect_off = sect_off;
6847 dwo_tu->length = length;
6848 }
6849 else
6850 {
6851 /* N.B.: type_offset is not usable if this type uses a DWO file.
6852 The real type_offset is in the DWO file. */
6853 dwo_tu = NULL;
6854 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6855 struct signatured_type);
6856 sig_type->signature = header.signature;
6857 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6858 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6859 sig_type->per_cu.is_debug_types = 1;
6860 sig_type->per_cu.section = section;
6861 sig_type->per_cu.sect_off = sect_off;
6862 sig_type->per_cu.length = length;
6863 }
6864
6865 slot = htab_find_slot (types_htab,
6866 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6867 INSERT);
6868 gdb_assert (slot != NULL);
6869 if (*slot != NULL)
6870 {
6871 sect_offset dup_sect_off;
6872
6873 if (dwo_file)
6874 {
6875 const struct dwo_unit *dup_tu
6876 = (const struct dwo_unit *) *slot;
6877
6878 dup_sect_off = dup_tu->sect_off;
6879 }
6880 else
6881 {
6882 const struct signatured_type *dup_tu
6883 = (const struct signatured_type *) *slot;
6884
6885 dup_sect_off = dup_tu->per_cu.sect_off;
6886 }
6887
6888 complaint (_("debug type entry at offset %s is duplicate to"
6889 " the entry at offset %s, signature %s"),
6890 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6891 hex_string (header.signature));
6892 }
6893 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6894
6895 if (dwarf_read_debug > 1)
6896 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6897 sect_offset_str (sect_off),
6898 hex_string (header.signature));
6899
6900 info_ptr += length;
6901 }
6902 }
6903
6904 /* Create the hash table of all entries in the .debug_types
6905 (or .debug_types.dwo) section(s).
6906 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6907 otherwise it is NULL.
6908
6909 The result is a pointer to the hash table or NULL if there are no types.
6910
6911 Note: This function processes DWO files only, not DWP files. */
6912
6913 static void
6914 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6915 struct dwo_file *dwo_file,
6916 gdb::array_view<dwarf2_section_info> type_sections,
6917 htab_t &types_htab)
6918 {
6919 for (dwarf2_section_info &section : type_sections)
6920 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6921 types_htab, rcuh_kind::TYPE);
6922 }
6923
6924 /* Create the hash table of all entries in the .debug_types section,
6925 and initialize all_type_units.
6926 The result is zero if there is an error (e.g. missing .debug_types section),
6927 otherwise non-zero. */
6928
6929 static int
6930 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6931 {
6932 htab_t types_htab = NULL;
6933
6934 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6935 &dwarf2_per_objfile->info, types_htab,
6936 rcuh_kind::COMPILE);
6937 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6938 dwarf2_per_objfile->types, types_htab);
6939 if (types_htab == NULL)
6940 {
6941 dwarf2_per_objfile->signatured_types = NULL;
6942 return 0;
6943 }
6944
6945 dwarf2_per_objfile->signatured_types = types_htab;
6946
6947 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6948 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6949
6950 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6951 &dwarf2_per_objfile->all_type_units);
6952
6953 return 1;
6954 }
6955
6956 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6957 If SLOT is non-NULL, it is the entry to use in the hash table.
6958 Otherwise we find one. */
6959
6960 static struct signatured_type *
6961 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6962 void **slot)
6963 {
6964 struct objfile *objfile = dwarf2_per_objfile->objfile;
6965
6966 if (dwarf2_per_objfile->all_type_units.size ()
6967 == dwarf2_per_objfile->all_type_units.capacity ())
6968 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6969
6970 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6971 struct signatured_type);
6972
6973 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6974 sig_type->signature = sig;
6975 sig_type->per_cu.is_debug_types = 1;
6976 if (dwarf2_per_objfile->using_index)
6977 {
6978 sig_type->per_cu.v.quick =
6979 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6980 struct dwarf2_per_cu_quick_data);
6981 }
6982
6983 if (slot == NULL)
6984 {
6985 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6986 sig_type, INSERT);
6987 }
6988 gdb_assert (*slot == NULL);
6989 *slot = sig_type;
6990 /* The rest of sig_type must be filled in by the caller. */
6991 return sig_type;
6992 }
6993
6994 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6995 Fill in SIG_ENTRY with DWO_ENTRY. */
6996
6997 static void
6998 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6999 struct signatured_type *sig_entry,
7000 struct dwo_unit *dwo_entry)
7001 {
7002 /* Make sure we're not clobbering something we don't expect to. */
7003 gdb_assert (! sig_entry->per_cu.queued);
7004 gdb_assert (sig_entry->per_cu.cu == NULL);
7005 if (dwarf2_per_objfile->using_index)
7006 {
7007 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7008 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7009 }
7010 else
7011 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7012 gdb_assert (sig_entry->signature == dwo_entry->signature);
7013 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7014 gdb_assert (sig_entry->type_unit_group == NULL);
7015 gdb_assert (sig_entry->dwo_unit == NULL);
7016
7017 sig_entry->per_cu.section = dwo_entry->section;
7018 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7019 sig_entry->per_cu.length = dwo_entry->length;
7020 sig_entry->per_cu.reading_dwo_directly = 1;
7021 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7022 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7023 sig_entry->dwo_unit = dwo_entry;
7024 }
7025
7026 /* Subroutine of lookup_signatured_type.
7027 If we haven't read the TU yet, create the signatured_type data structure
7028 for a TU to be read in directly from a DWO file, bypassing the stub.
7029 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7030 using .gdb_index, then when reading a CU we want to stay in the DWO file
7031 containing that CU. Otherwise we could end up reading several other DWO
7032 files (due to comdat folding) to process the transitive closure of all the
7033 mentioned TUs, and that can be slow. The current DWO file will have every
7034 type signature that it needs.
7035 We only do this for .gdb_index because in the psymtab case we already have
7036 to read all the DWOs to build the type unit groups. */
7037
7038 static struct signatured_type *
7039 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7040 {
7041 struct dwarf2_per_objfile *dwarf2_per_objfile
7042 = cu->per_cu->dwarf2_per_objfile;
7043 struct objfile *objfile = dwarf2_per_objfile->objfile;
7044 struct dwo_file *dwo_file;
7045 struct dwo_unit find_dwo_entry, *dwo_entry;
7046 struct signatured_type find_sig_entry, *sig_entry;
7047 void **slot;
7048
7049 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7050
7051 /* If TU skeletons have been removed then we may not have read in any
7052 TUs yet. */
7053 if (dwarf2_per_objfile->signatured_types == NULL)
7054 {
7055 dwarf2_per_objfile->signatured_types
7056 = allocate_signatured_type_table (objfile);
7057 }
7058
7059 /* We only ever need to read in one copy of a signatured type.
7060 Use the global signatured_types array to do our own comdat-folding
7061 of types. If this is the first time we're reading this TU, and
7062 the TU has an entry in .gdb_index, replace the recorded data from
7063 .gdb_index with this TU. */
7064
7065 find_sig_entry.signature = sig;
7066 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7067 &find_sig_entry, INSERT);
7068 sig_entry = (struct signatured_type *) *slot;
7069
7070 /* We can get here with the TU already read, *or* in the process of being
7071 read. Don't reassign the global entry to point to this DWO if that's
7072 the case. Also note that if the TU is already being read, it may not
7073 have come from a DWO, the program may be a mix of Fission-compiled
7074 code and non-Fission-compiled code. */
7075
7076 /* Have we already tried to read this TU?
7077 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7078 needn't exist in the global table yet). */
7079 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7080 return sig_entry;
7081
7082 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7083 dwo_unit of the TU itself. */
7084 dwo_file = cu->dwo_unit->dwo_file;
7085
7086 /* Ok, this is the first time we're reading this TU. */
7087 if (dwo_file->tus == NULL)
7088 return NULL;
7089 find_dwo_entry.signature = sig;
7090 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7091 if (dwo_entry == NULL)
7092 return NULL;
7093
7094 /* If the global table doesn't have an entry for this TU, add one. */
7095 if (sig_entry == NULL)
7096 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7097
7098 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7099 sig_entry->per_cu.tu_read = 1;
7100 return sig_entry;
7101 }
7102
7103 /* Subroutine of lookup_signatured_type.
7104 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7105 then try the DWP file. If the TU stub (skeleton) has been removed then
7106 it won't be in .gdb_index. */
7107
7108 static struct signatured_type *
7109 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7110 {
7111 struct dwarf2_per_objfile *dwarf2_per_objfile
7112 = cu->per_cu->dwarf2_per_objfile;
7113 struct objfile *objfile = dwarf2_per_objfile->objfile;
7114 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7115 struct dwo_unit *dwo_entry;
7116 struct signatured_type find_sig_entry, *sig_entry;
7117 void **slot;
7118
7119 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7120 gdb_assert (dwp_file != NULL);
7121
7122 /* If TU skeletons have been removed then we may not have read in any
7123 TUs yet. */
7124 if (dwarf2_per_objfile->signatured_types == NULL)
7125 {
7126 dwarf2_per_objfile->signatured_types
7127 = allocate_signatured_type_table (objfile);
7128 }
7129
7130 find_sig_entry.signature = sig;
7131 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7132 &find_sig_entry, INSERT);
7133 sig_entry = (struct signatured_type *) *slot;
7134
7135 /* Have we already tried to read this TU?
7136 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7137 needn't exist in the global table yet). */
7138 if (sig_entry != NULL)
7139 return sig_entry;
7140
7141 if (dwp_file->tus == NULL)
7142 return NULL;
7143 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7144 sig, 1 /* is_debug_types */);
7145 if (dwo_entry == NULL)
7146 return NULL;
7147
7148 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7149 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7150
7151 return sig_entry;
7152 }
7153
7154 /* Lookup a signature based type for DW_FORM_ref_sig8.
7155 Returns NULL if signature SIG is not present in the table.
7156 It is up to the caller to complain about this. */
7157
7158 static struct signatured_type *
7159 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7160 {
7161 struct dwarf2_per_objfile *dwarf2_per_objfile
7162 = cu->per_cu->dwarf2_per_objfile;
7163
7164 if (cu->dwo_unit
7165 && dwarf2_per_objfile->using_index)
7166 {
7167 /* We're in a DWO/DWP file, and we're using .gdb_index.
7168 These cases require special processing. */
7169 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7170 return lookup_dwo_signatured_type (cu, sig);
7171 else
7172 return lookup_dwp_signatured_type (cu, sig);
7173 }
7174 else
7175 {
7176 struct signatured_type find_entry, *entry;
7177
7178 if (dwarf2_per_objfile->signatured_types == NULL)
7179 return NULL;
7180 find_entry.signature = sig;
7181 entry = ((struct signatured_type *)
7182 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7183 return entry;
7184 }
7185 }
7186 \f
7187 /* Low level DIE reading support. */
7188
7189 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7190
7191 static void
7192 init_cu_die_reader (struct die_reader_specs *reader,
7193 struct dwarf2_cu *cu,
7194 struct dwarf2_section_info *section,
7195 struct dwo_file *dwo_file,
7196 struct abbrev_table *abbrev_table)
7197 {
7198 gdb_assert (section->readin && section->buffer != NULL);
7199 reader->abfd = get_section_bfd_owner (section);
7200 reader->cu = cu;
7201 reader->dwo_file = dwo_file;
7202 reader->die_section = section;
7203 reader->buffer = section->buffer;
7204 reader->buffer_end = section->buffer + section->size;
7205 reader->comp_dir = NULL;
7206 reader->abbrev_table = abbrev_table;
7207 }
7208
7209 /* Subroutine of init_cutu_and_read_dies to simplify it.
7210 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7211 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7212 already.
7213
7214 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7215 from it to the DIE in the DWO. If NULL we are skipping the stub.
7216 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7217 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7218 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7219 STUB_COMP_DIR may be non-NULL.
7220 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7221 are filled in with the info of the DIE from the DWO file.
7222 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7223 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7224 kept around for at least as long as *RESULT_READER.
7225
7226 The result is non-zero if a valid (non-dummy) DIE was found. */
7227
7228 static int
7229 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7230 struct dwo_unit *dwo_unit,
7231 struct die_info *stub_comp_unit_die,
7232 const char *stub_comp_dir,
7233 struct die_reader_specs *result_reader,
7234 const gdb_byte **result_info_ptr,
7235 struct die_info **result_comp_unit_die,
7236 int *result_has_children,
7237 abbrev_table_up *result_dwo_abbrev_table)
7238 {
7239 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7240 struct objfile *objfile = dwarf2_per_objfile->objfile;
7241 struct dwarf2_cu *cu = this_cu->cu;
7242 bfd *abfd;
7243 const gdb_byte *begin_info_ptr, *info_ptr;
7244 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7245 int i,num_extra_attrs;
7246 struct dwarf2_section_info *dwo_abbrev_section;
7247 struct attribute *attr;
7248 struct die_info *comp_unit_die;
7249
7250 /* At most one of these may be provided. */
7251 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7252
7253 /* These attributes aren't processed until later:
7254 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7255 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7256 referenced later. However, these attributes are found in the stub
7257 which we won't have later. In order to not impose this complication
7258 on the rest of the code, we read them here and copy them to the
7259 DWO CU/TU die. */
7260
7261 stmt_list = NULL;
7262 low_pc = NULL;
7263 high_pc = NULL;
7264 ranges = NULL;
7265 comp_dir = NULL;
7266
7267 if (stub_comp_unit_die != NULL)
7268 {
7269 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7270 DWO file. */
7271 if (! this_cu->is_debug_types)
7272 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7273 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7274 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7275 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7276 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7277
7278 /* There should be a DW_AT_addr_base attribute here (if needed).
7279 We need the value before we can process DW_FORM_GNU_addr_index
7280 or DW_FORM_addrx. */
7281 cu->addr_base = 0;
7282 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7283 if (attr != nullptr)
7284 cu->addr_base = DW_UNSND (attr);
7285
7286 /* There should be a DW_AT_ranges_base attribute here (if needed).
7287 We need the value before we can process DW_AT_ranges. */
7288 cu->ranges_base = 0;
7289 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7290 if (attr != nullptr)
7291 cu->ranges_base = DW_UNSND (attr);
7292 }
7293 else if (stub_comp_dir != NULL)
7294 {
7295 /* Reconstruct the comp_dir attribute to simplify the code below. */
7296 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7297 comp_dir->name = DW_AT_comp_dir;
7298 comp_dir->form = DW_FORM_string;
7299 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7300 DW_STRING (comp_dir) = stub_comp_dir;
7301 }
7302
7303 /* Set up for reading the DWO CU/TU. */
7304 cu->dwo_unit = dwo_unit;
7305 dwarf2_section_info *section = dwo_unit->section;
7306 dwarf2_read_section (objfile, section);
7307 abfd = get_section_bfd_owner (section);
7308 begin_info_ptr = info_ptr = (section->buffer
7309 + to_underlying (dwo_unit->sect_off));
7310 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7311
7312 if (this_cu->is_debug_types)
7313 {
7314 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7315
7316 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7317 &cu->header, section,
7318 dwo_abbrev_section,
7319 info_ptr, rcuh_kind::TYPE);
7320 /* This is not an assert because it can be caused by bad debug info. */
7321 if (sig_type->signature != cu->header.signature)
7322 {
7323 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7324 " TU at offset %s [in module %s]"),
7325 hex_string (sig_type->signature),
7326 hex_string (cu->header.signature),
7327 sect_offset_str (dwo_unit->sect_off),
7328 bfd_get_filename (abfd));
7329 }
7330 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7331 /* For DWOs coming from DWP files, we don't know the CU length
7332 nor the type's offset in the TU until now. */
7333 dwo_unit->length = get_cu_length (&cu->header);
7334 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7335
7336 /* Establish the type offset that can be used to lookup the type.
7337 For DWO files, we don't know it until now. */
7338 sig_type->type_offset_in_section
7339 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7340 }
7341 else
7342 {
7343 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7344 &cu->header, section,
7345 dwo_abbrev_section,
7346 info_ptr, rcuh_kind::COMPILE);
7347 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7348 /* For DWOs coming from DWP files, we don't know the CU length
7349 until now. */
7350 dwo_unit->length = get_cu_length (&cu->header);
7351 }
7352
7353 *result_dwo_abbrev_table
7354 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7355 cu->header.abbrev_sect_off);
7356 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7357 result_dwo_abbrev_table->get ());
7358
7359 /* Read in the die, but leave space to copy over the attributes
7360 from the stub. This has the benefit of simplifying the rest of
7361 the code - all the work to maintain the illusion of a single
7362 DW_TAG_{compile,type}_unit DIE is done here. */
7363 num_extra_attrs = ((stmt_list != NULL)
7364 + (low_pc != NULL)
7365 + (high_pc != NULL)
7366 + (ranges != NULL)
7367 + (comp_dir != NULL));
7368 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7369 result_has_children, num_extra_attrs);
7370
7371 /* Copy over the attributes from the stub to the DIE we just read in. */
7372 comp_unit_die = *result_comp_unit_die;
7373 i = comp_unit_die->num_attrs;
7374 if (stmt_list != NULL)
7375 comp_unit_die->attrs[i++] = *stmt_list;
7376 if (low_pc != NULL)
7377 comp_unit_die->attrs[i++] = *low_pc;
7378 if (high_pc != NULL)
7379 comp_unit_die->attrs[i++] = *high_pc;
7380 if (ranges != NULL)
7381 comp_unit_die->attrs[i++] = *ranges;
7382 if (comp_dir != NULL)
7383 comp_unit_die->attrs[i++] = *comp_dir;
7384 comp_unit_die->num_attrs += num_extra_attrs;
7385
7386 if (dwarf_die_debug)
7387 {
7388 fprintf_unfiltered (gdb_stdlog,
7389 "Read die from %s@0x%x of %s:\n",
7390 get_section_name (section),
7391 (unsigned) (begin_info_ptr - section->buffer),
7392 bfd_get_filename (abfd));
7393 dump_die (comp_unit_die, dwarf_die_debug);
7394 }
7395
7396 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7397 TUs by skipping the stub and going directly to the entry in the DWO file.
7398 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7399 to get it via circuitous means. Blech. */
7400 if (comp_dir != NULL)
7401 result_reader->comp_dir = DW_STRING (comp_dir);
7402
7403 /* Skip dummy compilation units. */
7404 if (info_ptr >= begin_info_ptr + dwo_unit->length
7405 || peek_abbrev_code (abfd, info_ptr) == 0)
7406 return 0;
7407
7408 *result_info_ptr = info_ptr;
7409 return 1;
7410 }
7411
7412 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7413 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7414 signature is part of the header. */
7415 static gdb::optional<ULONGEST>
7416 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7417 {
7418 if (cu->header.version >= 5)
7419 return cu->header.signature;
7420 struct attribute *attr;
7421 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7422 if (attr == nullptr)
7423 return gdb::optional<ULONGEST> ();
7424 return DW_UNSND (attr);
7425 }
7426
7427 /* Subroutine of init_cutu_and_read_dies to simplify it.
7428 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7429 Returns NULL if the specified DWO unit cannot be found. */
7430
7431 static struct dwo_unit *
7432 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7433 struct die_info *comp_unit_die)
7434 {
7435 struct dwarf2_cu *cu = this_cu->cu;
7436 struct dwo_unit *dwo_unit;
7437 const char *comp_dir, *dwo_name;
7438
7439 gdb_assert (cu != NULL);
7440
7441 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7442 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7443 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7444
7445 if (this_cu->is_debug_types)
7446 {
7447 struct signatured_type *sig_type;
7448
7449 /* Since this_cu is the first member of struct signatured_type,
7450 we can go from a pointer to one to a pointer to the other. */
7451 sig_type = (struct signatured_type *) this_cu;
7452 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7453 }
7454 else
7455 {
7456 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7457 if (!signature.has_value ())
7458 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7459 " [in module %s]"),
7460 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7461 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7462 *signature);
7463 }
7464
7465 return dwo_unit;
7466 }
7467
7468 /* Subroutine of init_cutu_and_read_dies to simplify it.
7469 See it for a description of the parameters.
7470 Read a TU directly from a DWO file, bypassing the stub. */
7471
7472 static void
7473 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7474 int use_existing_cu, int keep,
7475 die_reader_func_ftype *die_reader_func,
7476 void *data)
7477 {
7478 std::unique_ptr<dwarf2_cu> new_cu;
7479 struct signatured_type *sig_type;
7480 struct die_reader_specs reader;
7481 const gdb_byte *info_ptr;
7482 struct die_info *comp_unit_die;
7483 int has_children;
7484 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7485
7486 /* Verify we can do the following downcast, and that we have the
7487 data we need. */
7488 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7489 sig_type = (struct signatured_type *) this_cu;
7490 gdb_assert (sig_type->dwo_unit != NULL);
7491
7492 if (use_existing_cu && this_cu->cu != NULL)
7493 {
7494 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7495 /* There's no need to do the rereading_dwo_cu handling that
7496 init_cutu_and_read_dies does since we don't read the stub. */
7497 }
7498 else
7499 {
7500 /* If !use_existing_cu, this_cu->cu must be NULL. */
7501 gdb_assert (this_cu->cu == NULL);
7502 new_cu.reset (new dwarf2_cu (this_cu));
7503 }
7504
7505 /* A future optimization, if needed, would be to use an existing
7506 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7507 could share abbrev tables. */
7508
7509 /* The abbreviation table used by READER, this must live at least as long as
7510 READER. */
7511 abbrev_table_up dwo_abbrev_table;
7512
7513 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7514 NULL /* stub_comp_unit_die */,
7515 sig_type->dwo_unit->dwo_file->comp_dir,
7516 &reader, &info_ptr,
7517 &comp_unit_die, &has_children,
7518 &dwo_abbrev_table) == 0)
7519 {
7520 /* Dummy die. */
7521 return;
7522 }
7523
7524 /* All the "real" work is done here. */
7525 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7526
7527 /* This duplicates the code in init_cutu_and_read_dies,
7528 but the alternative is making the latter more complex.
7529 This function is only for the special case of using DWO files directly:
7530 no point in overly complicating the general case just to handle this. */
7531 if (new_cu != NULL && keep)
7532 {
7533 /* Link this CU into read_in_chain. */
7534 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7535 dwarf2_per_objfile->read_in_chain = this_cu;
7536 /* The chain owns it now. */
7537 new_cu.release ();
7538 }
7539 }
7540
7541 /* Initialize a CU (or TU) and read its DIEs.
7542 If the CU defers to a DWO file, read the DWO file as well.
7543
7544 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7545 Otherwise the table specified in the comp unit header is read in and used.
7546 This is an optimization for when we already have the abbrev table.
7547
7548 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7549 Otherwise, a new CU is allocated with xmalloc.
7550
7551 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7552 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7553
7554 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7555 linker) then DIE_READER_FUNC will not get called. */
7556
7557 static void
7558 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7559 struct abbrev_table *abbrev_table,
7560 int use_existing_cu, int keep,
7561 bool skip_partial,
7562 die_reader_func_ftype *die_reader_func,
7563 void *data)
7564 {
7565 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7566 struct objfile *objfile = dwarf2_per_objfile->objfile;
7567 struct dwarf2_section_info *section = this_cu->section;
7568 bfd *abfd = get_section_bfd_owner (section);
7569 struct dwarf2_cu *cu;
7570 const gdb_byte *begin_info_ptr, *info_ptr;
7571 struct die_reader_specs reader;
7572 struct die_info *comp_unit_die;
7573 int has_children;
7574 struct signatured_type *sig_type = NULL;
7575 struct dwarf2_section_info *abbrev_section;
7576 /* Non-zero if CU currently points to a DWO file and we need to
7577 reread it. When this happens we need to reread the skeleton die
7578 before we can reread the DWO file (this only applies to CUs, not TUs). */
7579 int rereading_dwo_cu = 0;
7580
7581 if (dwarf_die_debug)
7582 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7583 this_cu->is_debug_types ? "type" : "comp",
7584 sect_offset_str (this_cu->sect_off));
7585
7586 if (use_existing_cu)
7587 gdb_assert (keep);
7588
7589 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7590 file (instead of going through the stub), short-circuit all of this. */
7591 if (this_cu->reading_dwo_directly)
7592 {
7593 /* Narrow down the scope of possibilities to have to understand. */
7594 gdb_assert (this_cu->is_debug_types);
7595 gdb_assert (abbrev_table == NULL);
7596 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7597 die_reader_func, data);
7598 return;
7599 }
7600
7601 /* This is cheap if the section is already read in. */
7602 dwarf2_read_section (objfile, section);
7603
7604 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7605
7606 abbrev_section = get_abbrev_section_for_cu (this_cu);
7607
7608 std::unique_ptr<dwarf2_cu> new_cu;
7609 if (use_existing_cu && this_cu->cu != NULL)
7610 {
7611 cu = this_cu->cu;
7612 /* If this CU is from a DWO file we need to start over, we need to
7613 refetch the attributes from the skeleton CU.
7614 This could be optimized by retrieving those attributes from when we
7615 were here the first time: the previous comp_unit_die was stored in
7616 comp_unit_obstack. But there's no data yet that we need this
7617 optimization. */
7618 if (cu->dwo_unit != NULL)
7619 rereading_dwo_cu = 1;
7620 }
7621 else
7622 {
7623 /* If !use_existing_cu, this_cu->cu must be NULL. */
7624 gdb_assert (this_cu->cu == NULL);
7625 new_cu.reset (new dwarf2_cu (this_cu));
7626 cu = new_cu.get ();
7627 }
7628
7629 /* Get the header. */
7630 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7631 {
7632 /* We already have the header, there's no need to read it in again. */
7633 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7634 }
7635 else
7636 {
7637 if (this_cu->is_debug_types)
7638 {
7639 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7640 &cu->header, section,
7641 abbrev_section, info_ptr,
7642 rcuh_kind::TYPE);
7643
7644 /* Since per_cu is the first member of struct signatured_type,
7645 we can go from a pointer to one to a pointer to the other. */
7646 sig_type = (struct signatured_type *) this_cu;
7647 gdb_assert (sig_type->signature == cu->header.signature);
7648 gdb_assert (sig_type->type_offset_in_tu
7649 == cu->header.type_cu_offset_in_tu);
7650 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7651
7652 /* LENGTH has not been set yet for type units if we're
7653 using .gdb_index. */
7654 this_cu->length = get_cu_length (&cu->header);
7655
7656 /* Establish the type offset that can be used to lookup the type. */
7657 sig_type->type_offset_in_section =
7658 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7659
7660 this_cu->dwarf_version = cu->header.version;
7661 }
7662 else
7663 {
7664 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7665 &cu->header, section,
7666 abbrev_section,
7667 info_ptr,
7668 rcuh_kind::COMPILE);
7669
7670 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7671 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7672 this_cu->dwarf_version = cu->header.version;
7673 }
7674 }
7675
7676 /* Skip dummy compilation units. */
7677 if (info_ptr >= begin_info_ptr + this_cu->length
7678 || peek_abbrev_code (abfd, info_ptr) == 0)
7679 return;
7680
7681 /* If we don't have them yet, read the abbrevs for this compilation unit.
7682 And if we need to read them now, make sure they're freed when we're
7683 done (own the table through ABBREV_TABLE_HOLDER). */
7684 abbrev_table_up abbrev_table_holder;
7685 if (abbrev_table != NULL)
7686 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7687 else
7688 {
7689 abbrev_table_holder
7690 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7691 cu->header.abbrev_sect_off);
7692 abbrev_table = abbrev_table_holder.get ();
7693 }
7694
7695 /* Read the top level CU/TU die. */
7696 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7697 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7698
7699 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7700 return;
7701
7702 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7703 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7704 table from the DWO file and pass the ownership over to us. It will be
7705 referenced from READER, so we must make sure to free it after we're done
7706 with READER.
7707
7708 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7709 DWO CU, that this test will fail (the attribute will not be present). */
7710 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7711 abbrev_table_up dwo_abbrev_table;
7712 if (dwo_name != nullptr)
7713 {
7714 struct dwo_unit *dwo_unit;
7715 struct die_info *dwo_comp_unit_die;
7716
7717 if (has_children)
7718 {
7719 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7720 " has children (offset %s) [in module %s]"),
7721 sect_offset_str (this_cu->sect_off),
7722 bfd_get_filename (abfd));
7723 }
7724 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7725 if (dwo_unit != NULL)
7726 {
7727 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7728 comp_unit_die, NULL,
7729 &reader, &info_ptr,
7730 &dwo_comp_unit_die, &has_children,
7731 &dwo_abbrev_table) == 0)
7732 {
7733 /* Dummy die. */
7734 return;
7735 }
7736 comp_unit_die = dwo_comp_unit_die;
7737 }
7738 else
7739 {
7740 /* Yikes, we couldn't find the rest of the DIE, we only have
7741 the stub. A complaint has already been logged. There's
7742 not much more we can do except pass on the stub DIE to
7743 die_reader_func. We don't want to throw an error on bad
7744 debug info. */
7745 }
7746 }
7747
7748 /* All of the above is setup for this call. Yikes. */
7749 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7750
7751 /* Done, clean up. */
7752 if (new_cu != NULL && keep)
7753 {
7754 /* Link this CU into read_in_chain. */
7755 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7756 dwarf2_per_objfile->read_in_chain = this_cu;
7757 /* The chain owns it now. */
7758 new_cu.release ();
7759 }
7760 }
7761
7762 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7763 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7764 to have already done the lookup to find the DWO file).
7765
7766 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7767 THIS_CU->is_debug_types, but nothing else.
7768
7769 We fill in THIS_CU->length.
7770
7771 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7772 linker) then DIE_READER_FUNC will not get called.
7773
7774 THIS_CU->cu is always freed when done.
7775 This is done in order to not leave THIS_CU->cu in a state where we have
7776 to care whether it refers to the "main" CU or the DWO CU. */
7777
7778 static void
7779 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7780 struct dwo_file *dwo_file,
7781 die_reader_func_ftype *die_reader_func,
7782 void *data)
7783 {
7784 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7785 struct objfile *objfile = dwarf2_per_objfile->objfile;
7786 struct dwarf2_section_info *section = this_cu->section;
7787 bfd *abfd = get_section_bfd_owner (section);
7788 struct dwarf2_section_info *abbrev_section;
7789 const gdb_byte *begin_info_ptr, *info_ptr;
7790 struct die_reader_specs reader;
7791 struct die_info *comp_unit_die;
7792 int has_children;
7793
7794 if (dwarf_die_debug)
7795 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7796 this_cu->is_debug_types ? "type" : "comp",
7797 sect_offset_str (this_cu->sect_off));
7798
7799 gdb_assert (this_cu->cu == NULL);
7800
7801 abbrev_section = (dwo_file != NULL
7802 ? &dwo_file->sections.abbrev
7803 : get_abbrev_section_for_cu (this_cu));
7804
7805 /* This is cheap if the section is already read in. */
7806 dwarf2_read_section (objfile, section);
7807
7808 struct dwarf2_cu cu (this_cu);
7809
7810 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7811 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7812 &cu.header, section,
7813 abbrev_section, info_ptr,
7814 (this_cu->is_debug_types
7815 ? rcuh_kind::TYPE
7816 : rcuh_kind::COMPILE));
7817
7818 this_cu->length = get_cu_length (&cu.header);
7819
7820 /* Skip dummy compilation units. */
7821 if (info_ptr >= begin_info_ptr + this_cu->length
7822 || peek_abbrev_code (abfd, info_ptr) == 0)
7823 return;
7824
7825 abbrev_table_up abbrev_table
7826 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7827 cu.header.abbrev_sect_off);
7828
7829 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7830 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7831
7832 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7833 }
7834
7835 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7836 does not lookup the specified DWO file.
7837 This cannot be used to read DWO files.
7838
7839 THIS_CU->cu is always freed when done.
7840 This is done in order to not leave THIS_CU->cu in a state where we have
7841 to care whether it refers to the "main" CU or the DWO CU.
7842 We can revisit this if the data shows there's a performance issue. */
7843
7844 static void
7845 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7846 die_reader_func_ftype *die_reader_func,
7847 void *data)
7848 {
7849 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7850 }
7851 \f
7852 /* Type Unit Groups.
7853
7854 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7855 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7856 so that all types coming from the same compilation (.o file) are grouped
7857 together. A future step could be to put the types in the same symtab as
7858 the CU the types ultimately came from. */
7859
7860 static hashval_t
7861 hash_type_unit_group (const void *item)
7862 {
7863 const struct type_unit_group *tu_group
7864 = (const struct type_unit_group *) item;
7865
7866 return hash_stmt_list_entry (&tu_group->hash);
7867 }
7868
7869 static int
7870 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7871 {
7872 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7873 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7874
7875 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7876 }
7877
7878 /* Allocate a hash table for type unit groups. */
7879
7880 static htab_t
7881 allocate_type_unit_groups_table (struct objfile *objfile)
7882 {
7883 return htab_create_alloc_ex (3,
7884 hash_type_unit_group,
7885 eq_type_unit_group,
7886 NULL,
7887 &objfile->objfile_obstack,
7888 hashtab_obstack_allocate,
7889 dummy_obstack_deallocate);
7890 }
7891
7892 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7893 partial symtabs. We combine several TUs per psymtab to not let the size
7894 of any one psymtab grow too big. */
7895 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7896 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7897
7898 /* Helper routine for get_type_unit_group.
7899 Create the type_unit_group object used to hold one or more TUs. */
7900
7901 static struct type_unit_group *
7902 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7903 {
7904 struct dwarf2_per_objfile *dwarf2_per_objfile
7905 = cu->per_cu->dwarf2_per_objfile;
7906 struct objfile *objfile = dwarf2_per_objfile->objfile;
7907 struct dwarf2_per_cu_data *per_cu;
7908 struct type_unit_group *tu_group;
7909
7910 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7911 struct type_unit_group);
7912 per_cu = &tu_group->per_cu;
7913 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7914
7915 if (dwarf2_per_objfile->using_index)
7916 {
7917 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7918 struct dwarf2_per_cu_quick_data);
7919 }
7920 else
7921 {
7922 unsigned int line_offset = to_underlying (line_offset_struct);
7923 struct partial_symtab *pst;
7924 std::string name;
7925
7926 /* Give the symtab a useful name for debug purposes. */
7927 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7928 name = string_printf ("<type_units_%d>",
7929 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7930 else
7931 name = string_printf ("<type_units_at_0x%x>", line_offset);
7932
7933 pst = create_partial_symtab (per_cu, name.c_str ());
7934 pst->anonymous = 1;
7935 }
7936
7937 tu_group->hash.dwo_unit = cu->dwo_unit;
7938 tu_group->hash.line_sect_off = line_offset_struct;
7939
7940 return tu_group;
7941 }
7942
7943 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7944 STMT_LIST is a DW_AT_stmt_list attribute. */
7945
7946 static struct type_unit_group *
7947 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7948 {
7949 struct dwarf2_per_objfile *dwarf2_per_objfile
7950 = cu->per_cu->dwarf2_per_objfile;
7951 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7952 struct type_unit_group *tu_group;
7953 void **slot;
7954 unsigned int line_offset;
7955 struct type_unit_group type_unit_group_for_lookup;
7956
7957 if (dwarf2_per_objfile->type_unit_groups == NULL)
7958 {
7959 dwarf2_per_objfile->type_unit_groups =
7960 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7961 }
7962
7963 /* Do we need to create a new group, or can we use an existing one? */
7964
7965 if (stmt_list)
7966 {
7967 line_offset = DW_UNSND (stmt_list);
7968 ++tu_stats->nr_symtab_sharers;
7969 }
7970 else
7971 {
7972 /* Ugh, no stmt_list. Rare, but we have to handle it.
7973 We can do various things here like create one group per TU or
7974 spread them over multiple groups to split up the expansion work.
7975 To avoid worst case scenarios (too many groups or too large groups)
7976 we, umm, group them in bunches. */
7977 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7978 | (tu_stats->nr_stmt_less_type_units
7979 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7980 ++tu_stats->nr_stmt_less_type_units;
7981 }
7982
7983 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7984 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7985 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7986 &type_unit_group_for_lookup, INSERT);
7987 if (*slot != NULL)
7988 {
7989 tu_group = (struct type_unit_group *) *slot;
7990 gdb_assert (tu_group != NULL);
7991 }
7992 else
7993 {
7994 sect_offset line_offset_struct = (sect_offset) line_offset;
7995 tu_group = create_type_unit_group (cu, line_offset_struct);
7996 *slot = tu_group;
7997 ++tu_stats->nr_symtabs;
7998 }
7999
8000 return tu_group;
8001 }
8002 \f
8003 /* Partial symbol tables. */
8004
8005 /* Create a psymtab named NAME and assign it to PER_CU.
8006
8007 The caller must fill in the following details:
8008 dirname, textlow, texthigh. */
8009
8010 static struct partial_symtab *
8011 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8012 {
8013 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8014 struct partial_symtab *pst;
8015
8016 pst = start_psymtab_common (objfile, name, 0);
8017
8018 pst->psymtabs_addrmap_supported = 1;
8019
8020 /* This is the glue that links PST into GDB's symbol API. */
8021 pst->read_symtab_private = per_cu;
8022 pst->read_symtab = dwarf2_read_symtab;
8023 per_cu->v.psymtab = pst;
8024
8025 return pst;
8026 }
8027
8028 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8029 type. */
8030
8031 struct process_psymtab_comp_unit_data
8032 {
8033 /* True if we are reading a DW_TAG_partial_unit. */
8034
8035 int want_partial_unit;
8036
8037 /* The "pretend" language that is used if the CU doesn't declare a
8038 language. */
8039
8040 enum language pretend_language;
8041 };
8042
8043 /* die_reader_func for process_psymtab_comp_unit. */
8044
8045 static void
8046 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8047 const gdb_byte *info_ptr,
8048 struct die_info *comp_unit_die,
8049 int has_children,
8050 void *data)
8051 {
8052 struct dwarf2_cu *cu = reader->cu;
8053 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8054 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8055 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8056 CORE_ADDR baseaddr;
8057 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8058 struct partial_symtab *pst;
8059 enum pc_bounds_kind cu_bounds_kind;
8060 const char *filename;
8061 struct process_psymtab_comp_unit_data *info
8062 = (struct process_psymtab_comp_unit_data *) data;
8063
8064 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8065 return;
8066
8067 gdb_assert (! per_cu->is_debug_types);
8068
8069 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8070
8071 /* Allocate a new partial symbol table structure. */
8072 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8073 if (filename == NULL)
8074 filename = "";
8075
8076 pst = create_partial_symtab (per_cu, filename);
8077
8078 /* This must be done before calling dwarf2_build_include_psymtabs. */
8079 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8080
8081 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8082
8083 dwarf2_find_base_address (comp_unit_die, cu);
8084
8085 /* Possibly set the default values of LOWPC and HIGHPC from
8086 `DW_AT_ranges'. */
8087 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8088 &best_highpc, cu, pst);
8089 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8090 {
8091 CORE_ADDR low
8092 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8093 - baseaddr);
8094 CORE_ADDR high
8095 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8096 - baseaddr - 1);
8097 /* Store the contiguous range if it is not empty; it can be
8098 empty for CUs with no code. */
8099 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8100 low, high, pst);
8101 }
8102
8103 /* Check if comp unit has_children.
8104 If so, read the rest of the partial symbols from this comp unit.
8105 If not, there's no more debug_info for this comp unit. */
8106 if (has_children)
8107 {
8108 struct partial_die_info *first_die;
8109 CORE_ADDR lowpc, highpc;
8110
8111 lowpc = ((CORE_ADDR) -1);
8112 highpc = ((CORE_ADDR) 0);
8113
8114 first_die = load_partial_dies (reader, info_ptr, 1);
8115
8116 scan_partial_symbols (first_die, &lowpc, &highpc,
8117 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8118
8119 /* If we didn't find a lowpc, set it to highpc to avoid
8120 complaints from `maint check'. */
8121 if (lowpc == ((CORE_ADDR) -1))
8122 lowpc = highpc;
8123
8124 /* If the compilation unit didn't have an explicit address range,
8125 then use the information extracted from its child dies. */
8126 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8127 {
8128 best_lowpc = lowpc;
8129 best_highpc = highpc;
8130 }
8131 }
8132 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8133 best_lowpc + baseaddr)
8134 - baseaddr);
8135 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8136 best_highpc + baseaddr)
8137 - baseaddr);
8138
8139 end_psymtab_common (objfile, pst);
8140
8141 if (!cu->per_cu->imported_symtabs_empty ())
8142 {
8143 int i;
8144 int len = cu->per_cu->imported_symtabs_size ();
8145
8146 /* Fill in 'dependencies' here; we fill in 'users' in a
8147 post-pass. */
8148 pst->number_of_dependencies = len;
8149 pst->dependencies
8150 = objfile->partial_symtabs->allocate_dependencies (len);
8151 for (i = 0; i < len; ++i)
8152 {
8153 pst->dependencies[i]
8154 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8155 }
8156
8157 cu->per_cu->imported_symtabs_free ();
8158 }
8159
8160 /* Get the list of files included in the current compilation unit,
8161 and build a psymtab for each of them. */
8162 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8163
8164 if (dwarf_read_debug)
8165 fprintf_unfiltered (gdb_stdlog,
8166 "Psymtab for %s unit @%s: %s - %s"
8167 ", %d global, %d static syms\n",
8168 per_cu->is_debug_types ? "type" : "comp",
8169 sect_offset_str (per_cu->sect_off),
8170 paddress (gdbarch, pst->text_low (objfile)),
8171 paddress (gdbarch, pst->text_high (objfile)),
8172 pst->n_global_syms, pst->n_static_syms);
8173 }
8174
8175 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8176 Process compilation unit THIS_CU for a psymtab. */
8177
8178 static void
8179 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8180 int want_partial_unit,
8181 enum language pretend_language)
8182 {
8183 /* If this compilation unit was already read in, free the
8184 cached copy in order to read it in again. This is
8185 necessary because we skipped some symbols when we first
8186 read in the compilation unit (see load_partial_dies).
8187 This problem could be avoided, but the benefit is unclear. */
8188 if (this_cu->cu != NULL)
8189 free_one_cached_comp_unit (this_cu);
8190
8191 if (this_cu->is_debug_types)
8192 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8193 build_type_psymtabs_reader, NULL);
8194 else
8195 {
8196 process_psymtab_comp_unit_data info;
8197 info.want_partial_unit = want_partial_unit;
8198 info.pretend_language = pretend_language;
8199 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8200 process_psymtab_comp_unit_reader, &info);
8201 }
8202
8203 /* Age out any secondary CUs. */
8204 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8205 }
8206
8207 /* Reader function for build_type_psymtabs. */
8208
8209 static void
8210 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8211 const gdb_byte *info_ptr,
8212 struct die_info *type_unit_die,
8213 int has_children,
8214 void *data)
8215 {
8216 struct dwarf2_per_objfile *dwarf2_per_objfile
8217 = reader->cu->per_cu->dwarf2_per_objfile;
8218 struct objfile *objfile = dwarf2_per_objfile->objfile;
8219 struct dwarf2_cu *cu = reader->cu;
8220 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8221 struct signatured_type *sig_type;
8222 struct type_unit_group *tu_group;
8223 struct attribute *attr;
8224 struct partial_die_info *first_die;
8225 CORE_ADDR lowpc, highpc;
8226 struct partial_symtab *pst;
8227
8228 gdb_assert (data == NULL);
8229 gdb_assert (per_cu->is_debug_types);
8230 sig_type = (struct signatured_type *) per_cu;
8231
8232 if (! has_children)
8233 return;
8234
8235 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8236 tu_group = get_type_unit_group (cu, attr);
8237
8238 if (tu_group->tus == nullptr)
8239 tu_group->tus = new std::vector<signatured_type *>;
8240 tu_group->tus->push_back (sig_type);
8241
8242 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8243 pst = create_partial_symtab (per_cu, "");
8244 pst->anonymous = 1;
8245
8246 first_die = load_partial_dies (reader, info_ptr, 1);
8247
8248 lowpc = (CORE_ADDR) -1;
8249 highpc = (CORE_ADDR) 0;
8250 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8251
8252 end_psymtab_common (objfile, pst);
8253 }
8254
8255 /* Struct used to sort TUs by their abbreviation table offset. */
8256
8257 struct tu_abbrev_offset
8258 {
8259 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8260 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8261 {}
8262
8263 signatured_type *sig_type;
8264 sect_offset abbrev_offset;
8265 };
8266
8267 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8268
8269 static bool
8270 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8271 const struct tu_abbrev_offset &b)
8272 {
8273 return a.abbrev_offset < b.abbrev_offset;
8274 }
8275
8276 /* Efficiently read all the type units.
8277 This does the bulk of the work for build_type_psymtabs.
8278
8279 The efficiency is because we sort TUs by the abbrev table they use and
8280 only read each abbrev table once. In one program there are 200K TUs
8281 sharing 8K abbrev tables.
8282
8283 The main purpose of this function is to support building the
8284 dwarf2_per_objfile->type_unit_groups table.
8285 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8286 can collapse the search space by grouping them by stmt_list.
8287 The savings can be significant, in the same program from above the 200K TUs
8288 share 8K stmt_list tables.
8289
8290 FUNC is expected to call get_type_unit_group, which will create the
8291 struct type_unit_group if necessary and add it to
8292 dwarf2_per_objfile->type_unit_groups. */
8293
8294 static void
8295 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8296 {
8297 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8298 abbrev_table_up abbrev_table;
8299 sect_offset abbrev_offset;
8300
8301 /* It's up to the caller to not call us multiple times. */
8302 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8303
8304 if (dwarf2_per_objfile->all_type_units.empty ())
8305 return;
8306
8307 /* TUs typically share abbrev tables, and there can be way more TUs than
8308 abbrev tables. Sort by abbrev table to reduce the number of times we
8309 read each abbrev table in.
8310 Alternatives are to punt or to maintain a cache of abbrev tables.
8311 This is simpler and efficient enough for now.
8312
8313 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8314 symtab to use). Typically TUs with the same abbrev offset have the same
8315 stmt_list value too so in practice this should work well.
8316
8317 The basic algorithm here is:
8318
8319 sort TUs by abbrev table
8320 for each TU with same abbrev table:
8321 read abbrev table if first user
8322 read TU top level DIE
8323 [IWBN if DWO skeletons had DW_AT_stmt_list]
8324 call FUNC */
8325
8326 if (dwarf_read_debug)
8327 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8328
8329 /* Sort in a separate table to maintain the order of all_type_units
8330 for .gdb_index: TU indices directly index all_type_units. */
8331 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8332 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8333
8334 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8335 sorted_by_abbrev.emplace_back
8336 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8337 sig_type->per_cu.section,
8338 sig_type->per_cu.sect_off));
8339
8340 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8341 sort_tu_by_abbrev_offset);
8342
8343 abbrev_offset = (sect_offset) ~(unsigned) 0;
8344
8345 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8346 {
8347 /* Switch to the next abbrev table if necessary. */
8348 if (abbrev_table == NULL
8349 || tu.abbrev_offset != abbrev_offset)
8350 {
8351 abbrev_offset = tu.abbrev_offset;
8352 abbrev_table =
8353 abbrev_table_read_table (dwarf2_per_objfile,
8354 &dwarf2_per_objfile->abbrev,
8355 abbrev_offset);
8356 ++tu_stats->nr_uniq_abbrev_tables;
8357 }
8358
8359 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8360 0, 0, false, build_type_psymtabs_reader, NULL);
8361 }
8362 }
8363
8364 /* Print collected type unit statistics. */
8365
8366 static void
8367 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8368 {
8369 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8370
8371 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8372 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8373 dwarf2_per_objfile->all_type_units.size ());
8374 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8375 tu_stats->nr_uniq_abbrev_tables);
8376 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8377 tu_stats->nr_symtabs);
8378 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8379 tu_stats->nr_symtab_sharers);
8380 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8381 tu_stats->nr_stmt_less_type_units);
8382 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8383 tu_stats->nr_all_type_units_reallocs);
8384 }
8385
8386 /* Traversal function for build_type_psymtabs. */
8387
8388 static int
8389 build_type_psymtab_dependencies (void **slot, void *info)
8390 {
8391 struct dwarf2_per_objfile *dwarf2_per_objfile
8392 = (struct dwarf2_per_objfile *) info;
8393 struct objfile *objfile = dwarf2_per_objfile->objfile;
8394 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8395 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8396 struct partial_symtab *pst = per_cu->v.psymtab;
8397 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8398 int i;
8399
8400 gdb_assert (len > 0);
8401 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8402
8403 pst->number_of_dependencies = len;
8404 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8405 for (i = 0; i < len; ++i)
8406 {
8407 struct signatured_type *iter = tu_group->tus->at (i);
8408 gdb_assert (iter->per_cu.is_debug_types);
8409 pst->dependencies[i] = iter->per_cu.v.psymtab;
8410 iter->type_unit_group = tu_group;
8411 }
8412
8413 delete tu_group->tus;
8414 tu_group->tus = nullptr;
8415
8416 return 1;
8417 }
8418
8419 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8420 Build partial symbol tables for the .debug_types comp-units. */
8421
8422 static void
8423 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8424 {
8425 if (! create_all_type_units (dwarf2_per_objfile))
8426 return;
8427
8428 build_type_psymtabs_1 (dwarf2_per_objfile);
8429 }
8430
8431 /* Traversal function for process_skeletonless_type_unit.
8432 Read a TU in a DWO file and build partial symbols for it. */
8433
8434 static int
8435 process_skeletonless_type_unit (void **slot, void *info)
8436 {
8437 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8438 struct dwarf2_per_objfile *dwarf2_per_objfile
8439 = (struct dwarf2_per_objfile *) info;
8440 struct signatured_type find_entry, *entry;
8441
8442 /* If this TU doesn't exist in the global table, add it and read it in. */
8443
8444 if (dwarf2_per_objfile->signatured_types == NULL)
8445 {
8446 dwarf2_per_objfile->signatured_types
8447 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8448 }
8449
8450 find_entry.signature = dwo_unit->signature;
8451 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8452 INSERT);
8453 /* If we've already seen this type there's nothing to do. What's happening
8454 is we're doing our own version of comdat-folding here. */
8455 if (*slot != NULL)
8456 return 1;
8457
8458 /* This does the job that create_all_type_units would have done for
8459 this TU. */
8460 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8461 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8462 *slot = entry;
8463
8464 /* This does the job that build_type_psymtabs_1 would have done. */
8465 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8466 build_type_psymtabs_reader, NULL);
8467
8468 return 1;
8469 }
8470
8471 /* Traversal function for process_skeletonless_type_units. */
8472
8473 static int
8474 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8475 {
8476 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8477
8478 if (dwo_file->tus != NULL)
8479 {
8480 htab_traverse_noresize (dwo_file->tus,
8481 process_skeletonless_type_unit, info);
8482 }
8483
8484 return 1;
8485 }
8486
8487 /* Scan all TUs of DWO files, verifying we've processed them.
8488 This is needed in case a TU was emitted without its skeleton.
8489 Note: This can't be done until we know what all the DWO files are. */
8490
8491 static void
8492 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8493 {
8494 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8495 if (get_dwp_file (dwarf2_per_objfile) == NULL
8496 && dwarf2_per_objfile->dwo_files != NULL)
8497 {
8498 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8499 process_dwo_file_for_skeletonless_type_units,
8500 dwarf2_per_objfile);
8501 }
8502 }
8503
8504 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8505
8506 static void
8507 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8508 {
8509 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8510 {
8511 struct partial_symtab *pst = per_cu->v.psymtab;
8512
8513 if (pst == NULL)
8514 continue;
8515
8516 for (int j = 0; j < pst->number_of_dependencies; ++j)
8517 {
8518 /* Set the 'user' field only if it is not already set. */
8519 if (pst->dependencies[j]->user == NULL)
8520 pst->dependencies[j]->user = pst;
8521 }
8522 }
8523 }
8524
8525 /* Build the partial symbol table by doing a quick pass through the
8526 .debug_info and .debug_abbrev sections. */
8527
8528 static void
8529 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8530 {
8531 struct objfile *objfile = dwarf2_per_objfile->objfile;
8532
8533 if (dwarf_read_debug)
8534 {
8535 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8536 objfile_name (objfile));
8537 }
8538
8539 dwarf2_per_objfile->reading_partial_symbols = 1;
8540
8541 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8542
8543 /* Any cached compilation units will be linked by the per-objfile
8544 read_in_chain. Make sure to free them when we're done. */
8545 free_cached_comp_units freer (dwarf2_per_objfile);
8546
8547 build_type_psymtabs (dwarf2_per_objfile);
8548
8549 create_all_comp_units (dwarf2_per_objfile);
8550
8551 /* Create a temporary address map on a temporary obstack. We later
8552 copy this to the final obstack. */
8553 auto_obstack temp_obstack;
8554
8555 scoped_restore save_psymtabs_addrmap
8556 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8557 addrmap_create_mutable (&temp_obstack));
8558
8559 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8560 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8561
8562 /* This has to wait until we read the CUs, we need the list of DWOs. */
8563 process_skeletonless_type_units (dwarf2_per_objfile);
8564
8565 /* Now that all TUs have been processed we can fill in the dependencies. */
8566 if (dwarf2_per_objfile->type_unit_groups != NULL)
8567 {
8568 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8569 build_type_psymtab_dependencies, dwarf2_per_objfile);
8570 }
8571
8572 if (dwarf_read_debug)
8573 print_tu_stats (dwarf2_per_objfile);
8574
8575 set_partial_user (dwarf2_per_objfile);
8576
8577 objfile->partial_symtabs->psymtabs_addrmap
8578 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8579 objfile->partial_symtabs->obstack ());
8580 /* At this point we want to keep the address map. */
8581 save_psymtabs_addrmap.release ();
8582
8583 if (dwarf_read_debug)
8584 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8585 objfile_name (objfile));
8586 }
8587
8588 /* die_reader_func for load_partial_comp_unit. */
8589
8590 static void
8591 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8592 const gdb_byte *info_ptr,
8593 struct die_info *comp_unit_die,
8594 int has_children,
8595 void *data)
8596 {
8597 struct dwarf2_cu *cu = reader->cu;
8598
8599 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8600
8601 /* Check if comp unit has_children.
8602 If so, read the rest of the partial symbols from this comp unit.
8603 If not, there's no more debug_info for this comp unit. */
8604 if (has_children)
8605 load_partial_dies (reader, info_ptr, 0);
8606 }
8607
8608 /* Load the partial DIEs for a secondary CU into memory.
8609 This is also used when rereading a primary CU with load_all_dies. */
8610
8611 static void
8612 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8613 {
8614 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8615 load_partial_comp_unit_reader, NULL);
8616 }
8617
8618 static void
8619 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8620 struct dwarf2_section_info *section,
8621 struct dwarf2_section_info *abbrev_section,
8622 unsigned int is_dwz)
8623 {
8624 const gdb_byte *info_ptr;
8625 struct objfile *objfile = dwarf2_per_objfile->objfile;
8626
8627 if (dwarf_read_debug)
8628 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8629 get_section_name (section),
8630 get_section_file_name (section));
8631
8632 dwarf2_read_section (objfile, section);
8633
8634 info_ptr = section->buffer;
8635
8636 while (info_ptr < section->buffer + section->size)
8637 {
8638 struct dwarf2_per_cu_data *this_cu;
8639
8640 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8641
8642 comp_unit_head cu_header;
8643 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8644 abbrev_section, info_ptr,
8645 rcuh_kind::COMPILE);
8646
8647 /* Save the compilation unit for later lookup. */
8648 if (cu_header.unit_type != DW_UT_type)
8649 {
8650 this_cu = XOBNEW (&objfile->objfile_obstack,
8651 struct dwarf2_per_cu_data);
8652 memset (this_cu, 0, sizeof (*this_cu));
8653 }
8654 else
8655 {
8656 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8657 struct signatured_type);
8658 memset (sig_type, 0, sizeof (*sig_type));
8659 sig_type->signature = cu_header.signature;
8660 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8661 this_cu = &sig_type->per_cu;
8662 }
8663 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8664 this_cu->sect_off = sect_off;
8665 this_cu->length = cu_header.length + cu_header.initial_length_size;
8666 this_cu->is_dwz = is_dwz;
8667 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8668 this_cu->section = section;
8669
8670 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8671
8672 info_ptr = info_ptr + this_cu->length;
8673 }
8674 }
8675
8676 /* Create a list of all compilation units in OBJFILE.
8677 This is only done for -readnow and building partial symtabs. */
8678
8679 static void
8680 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8681 {
8682 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8683 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8684 &dwarf2_per_objfile->abbrev, 0);
8685
8686 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8687 if (dwz != NULL)
8688 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8689 1);
8690 }
8691
8692 /* Process all loaded DIEs for compilation unit CU, starting at
8693 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8694 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8695 DW_AT_ranges). See the comments of add_partial_subprogram on how
8696 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8697
8698 static void
8699 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8700 CORE_ADDR *highpc, int set_addrmap,
8701 struct dwarf2_cu *cu)
8702 {
8703 struct partial_die_info *pdi;
8704
8705 /* Now, march along the PDI's, descending into ones which have
8706 interesting children but skipping the children of the other ones,
8707 until we reach the end of the compilation unit. */
8708
8709 pdi = first_die;
8710
8711 while (pdi != NULL)
8712 {
8713 pdi->fixup (cu);
8714
8715 /* Anonymous namespaces or modules have no name but have interesting
8716 children, so we need to look at them. Ditto for anonymous
8717 enums. */
8718
8719 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8720 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8721 || pdi->tag == DW_TAG_imported_unit
8722 || pdi->tag == DW_TAG_inlined_subroutine)
8723 {
8724 switch (pdi->tag)
8725 {
8726 case DW_TAG_subprogram:
8727 case DW_TAG_inlined_subroutine:
8728 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8729 break;
8730 case DW_TAG_constant:
8731 case DW_TAG_variable:
8732 case DW_TAG_typedef:
8733 case DW_TAG_union_type:
8734 if (!pdi->is_declaration)
8735 {
8736 add_partial_symbol (pdi, cu);
8737 }
8738 break;
8739 case DW_TAG_class_type:
8740 case DW_TAG_interface_type:
8741 case DW_TAG_structure_type:
8742 if (!pdi->is_declaration)
8743 {
8744 add_partial_symbol (pdi, cu);
8745 }
8746 if ((cu->language == language_rust
8747 || cu->language == language_cplus) && pdi->has_children)
8748 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8749 set_addrmap, cu);
8750 break;
8751 case DW_TAG_enumeration_type:
8752 if (!pdi->is_declaration)
8753 add_partial_enumeration (pdi, cu);
8754 break;
8755 case DW_TAG_base_type:
8756 case DW_TAG_subrange_type:
8757 /* File scope base type definitions are added to the partial
8758 symbol table. */
8759 add_partial_symbol (pdi, cu);
8760 break;
8761 case DW_TAG_namespace:
8762 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8763 break;
8764 case DW_TAG_module:
8765 if (!pdi->is_declaration)
8766 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8767 break;
8768 case DW_TAG_imported_unit:
8769 {
8770 struct dwarf2_per_cu_data *per_cu;
8771
8772 /* For now we don't handle imported units in type units. */
8773 if (cu->per_cu->is_debug_types)
8774 {
8775 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8776 " supported in type units [in module %s]"),
8777 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8778 }
8779
8780 per_cu = dwarf2_find_containing_comp_unit
8781 (pdi->d.sect_off, pdi->is_dwz,
8782 cu->per_cu->dwarf2_per_objfile);
8783
8784 /* Go read the partial unit, if needed. */
8785 if (per_cu->v.psymtab == NULL)
8786 process_psymtab_comp_unit (per_cu, 1, cu->language);
8787
8788 cu->per_cu->imported_symtabs_push (per_cu);
8789 }
8790 break;
8791 case DW_TAG_imported_declaration:
8792 add_partial_symbol (pdi, cu);
8793 break;
8794 default:
8795 break;
8796 }
8797 }
8798
8799 /* If the die has a sibling, skip to the sibling. */
8800
8801 pdi = pdi->die_sibling;
8802 }
8803 }
8804
8805 /* Functions used to compute the fully scoped name of a partial DIE.
8806
8807 Normally, this is simple. For C++, the parent DIE's fully scoped
8808 name is concatenated with "::" and the partial DIE's name.
8809 Enumerators are an exception; they use the scope of their parent
8810 enumeration type, i.e. the name of the enumeration type is not
8811 prepended to the enumerator.
8812
8813 There are two complexities. One is DW_AT_specification; in this
8814 case "parent" means the parent of the target of the specification,
8815 instead of the direct parent of the DIE. The other is compilers
8816 which do not emit DW_TAG_namespace; in this case we try to guess
8817 the fully qualified name of structure types from their members'
8818 linkage names. This must be done using the DIE's children rather
8819 than the children of any DW_AT_specification target. We only need
8820 to do this for structures at the top level, i.e. if the target of
8821 any DW_AT_specification (if any; otherwise the DIE itself) does not
8822 have a parent. */
8823
8824 /* Compute the scope prefix associated with PDI's parent, in
8825 compilation unit CU. The result will be allocated on CU's
8826 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8827 field. NULL is returned if no prefix is necessary. */
8828 static const char *
8829 partial_die_parent_scope (struct partial_die_info *pdi,
8830 struct dwarf2_cu *cu)
8831 {
8832 const char *grandparent_scope;
8833 struct partial_die_info *parent, *real_pdi;
8834
8835 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8836 then this means the parent of the specification DIE. */
8837
8838 real_pdi = pdi;
8839 while (real_pdi->has_specification)
8840 {
8841 auto res = find_partial_die (real_pdi->spec_offset,
8842 real_pdi->spec_is_dwz, cu);
8843 real_pdi = res.pdi;
8844 cu = res.cu;
8845 }
8846
8847 parent = real_pdi->die_parent;
8848 if (parent == NULL)
8849 return NULL;
8850
8851 if (parent->scope_set)
8852 return parent->scope;
8853
8854 parent->fixup (cu);
8855
8856 grandparent_scope = partial_die_parent_scope (parent, cu);
8857
8858 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8859 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8860 Work around this problem here. */
8861 if (cu->language == language_cplus
8862 && parent->tag == DW_TAG_namespace
8863 && strcmp (parent->name, "::") == 0
8864 && grandparent_scope == NULL)
8865 {
8866 parent->scope = NULL;
8867 parent->scope_set = 1;
8868 return NULL;
8869 }
8870
8871 /* Nested subroutines in Fortran get a prefix. */
8872 if (pdi->tag == DW_TAG_enumerator)
8873 /* Enumerators should not get the name of the enumeration as a prefix. */
8874 parent->scope = grandparent_scope;
8875 else if (parent->tag == DW_TAG_namespace
8876 || parent->tag == DW_TAG_module
8877 || parent->tag == DW_TAG_structure_type
8878 || parent->tag == DW_TAG_class_type
8879 || parent->tag == DW_TAG_interface_type
8880 || parent->tag == DW_TAG_union_type
8881 || parent->tag == DW_TAG_enumeration_type
8882 || (cu->language == language_fortran
8883 && parent->tag == DW_TAG_subprogram
8884 && pdi->tag == DW_TAG_subprogram))
8885 {
8886 if (grandparent_scope == NULL)
8887 parent->scope = parent->name;
8888 else
8889 parent->scope = typename_concat (&cu->comp_unit_obstack,
8890 grandparent_scope,
8891 parent->name, 0, cu);
8892 }
8893 else
8894 {
8895 /* FIXME drow/2004-04-01: What should we be doing with
8896 function-local names? For partial symbols, we should probably be
8897 ignoring them. */
8898 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8899 dwarf_tag_name (parent->tag),
8900 sect_offset_str (pdi->sect_off));
8901 parent->scope = grandparent_scope;
8902 }
8903
8904 parent->scope_set = 1;
8905 return parent->scope;
8906 }
8907
8908 /* Return the fully scoped name associated with PDI, from compilation unit
8909 CU. The result will be allocated with malloc. */
8910
8911 static char *
8912 partial_die_full_name (struct partial_die_info *pdi,
8913 struct dwarf2_cu *cu)
8914 {
8915 const char *parent_scope;
8916
8917 /* If this is a template instantiation, we can not work out the
8918 template arguments from partial DIEs. So, unfortunately, we have
8919 to go through the full DIEs. At least any work we do building
8920 types here will be reused if full symbols are loaded later. */
8921 if (pdi->has_template_arguments)
8922 {
8923 pdi->fixup (cu);
8924
8925 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8926 {
8927 struct die_info *die;
8928 struct attribute attr;
8929 struct dwarf2_cu *ref_cu = cu;
8930
8931 /* DW_FORM_ref_addr is using section offset. */
8932 attr.name = (enum dwarf_attribute) 0;
8933 attr.form = DW_FORM_ref_addr;
8934 attr.u.unsnd = to_underlying (pdi->sect_off);
8935 die = follow_die_ref (NULL, &attr, &ref_cu);
8936
8937 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8938 }
8939 }
8940
8941 parent_scope = partial_die_parent_scope (pdi, cu);
8942 if (parent_scope == NULL)
8943 return NULL;
8944 else
8945 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8946 }
8947
8948 static void
8949 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8950 {
8951 struct dwarf2_per_objfile *dwarf2_per_objfile
8952 = cu->per_cu->dwarf2_per_objfile;
8953 struct objfile *objfile = dwarf2_per_objfile->objfile;
8954 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8955 CORE_ADDR addr = 0;
8956 const char *actual_name = NULL;
8957 CORE_ADDR baseaddr;
8958 char *built_actual_name;
8959
8960 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8961
8962 built_actual_name = partial_die_full_name (pdi, cu);
8963 if (built_actual_name != NULL)
8964 actual_name = built_actual_name;
8965
8966 if (actual_name == NULL)
8967 actual_name = pdi->name;
8968
8969 switch (pdi->tag)
8970 {
8971 case DW_TAG_inlined_subroutine:
8972 case DW_TAG_subprogram:
8973 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8974 - baseaddr);
8975 if (pdi->is_external
8976 || cu->language == language_ada
8977 || (cu->language == language_fortran
8978 && pdi->die_parent != NULL
8979 && pdi->die_parent->tag == DW_TAG_subprogram))
8980 {
8981 /* Normally, only "external" DIEs are part of the global scope.
8982 But in Ada and Fortran, we want to be able to access nested
8983 procedures globally. So all Ada and Fortran subprograms are
8984 stored in the global scope. */
8985 add_psymbol_to_list (actual_name,
8986 built_actual_name != NULL,
8987 VAR_DOMAIN, LOC_BLOCK,
8988 SECT_OFF_TEXT (objfile),
8989 psymbol_placement::GLOBAL,
8990 addr,
8991 cu->language, objfile);
8992 }
8993 else
8994 {
8995 add_psymbol_to_list (actual_name,
8996 built_actual_name != NULL,
8997 VAR_DOMAIN, LOC_BLOCK,
8998 SECT_OFF_TEXT (objfile),
8999 psymbol_placement::STATIC,
9000 addr, cu->language, objfile);
9001 }
9002
9003 if (pdi->main_subprogram && actual_name != NULL)
9004 set_objfile_main_name (objfile, actual_name, cu->language);
9005 break;
9006 case DW_TAG_constant:
9007 add_psymbol_to_list (actual_name,
9008 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9009 -1, (pdi->is_external
9010 ? psymbol_placement::GLOBAL
9011 : psymbol_placement::STATIC),
9012 0, cu->language, objfile);
9013 break;
9014 case DW_TAG_variable:
9015 if (pdi->d.locdesc)
9016 addr = decode_locdesc (pdi->d.locdesc, cu);
9017
9018 if (pdi->d.locdesc
9019 && addr == 0
9020 && !dwarf2_per_objfile->has_section_at_zero)
9021 {
9022 /* A global or static variable may also have been stripped
9023 out by the linker if unused, in which case its address
9024 will be nullified; do not add such variables into partial
9025 symbol table then. */
9026 }
9027 else if (pdi->is_external)
9028 {
9029 /* Global Variable.
9030 Don't enter into the minimal symbol tables as there is
9031 a minimal symbol table entry from the ELF symbols already.
9032 Enter into partial symbol table if it has a location
9033 descriptor or a type.
9034 If the location descriptor is missing, new_symbol will create
9035 a LOC_UNRESOLVED symbol, the address of the variable will then
9036 be determined from the minimal symbol table whenever the variable
9037 is referenced.
9038 The address for the partial symbol table entry is not
9039 used by GDB, but it comes in handy for debugging partial symbol
9040 table building. */
9041
9042 if (pdi->d.locdesc || pdi->has_type)
9043 add_psymbol_to_list (actual_name,
9044 built_actual_name != NULL,
9045 VAR_DOMAIN, LOC_STATIC,
9046 SECT_OFF_TEXT (objfile),
9047 psymbol_placement::GLOBAL,
9048 addr, cu->language, objfile);
9049 }
9050 else
9051 {
9052 int has_loc = pdi->d.locdesc != NULL;
9053
9054 /* Static Variable. Skip symbols whose value we cannot know (those
9055 without location descriptors or constant values). */
9056 if (!has_loc && !pdi->has_const_value)
9057 {
9058 xfree (built_actual_name);
9059 return;
9060 }
9061
9062 add_psymbol_to_list (actual_name,
9063 built_actual_name != NULL,
9064 VAR_DOMAIN, LOC_STATIC,
9065 SECT_OFF_TEXT (objfile),
9066 psymbol_placement::STATIC,
9067 has_loc ? addr : 0,
9068 cu->language, objfile);
9069 }
9070 break;
9071 case DW_TAG_typedef:
9072 case DW_TAG_base_type:
9073 case DW_TAG_subrange_type:
9074 add_psymbol_to_list (actual_name,
9075 built_actual_name != NULL,
9076 VAR_DOMAIN, LOC_TYPEDEF, -1,
9077 psymbol_placement::STATIC,
9078 0, cu->language, objfile);
9079 break;
9080 case DW_TAG_imported_declaration:
9081 case DW_TAG_namespace:
9082 add_psymbol_to_list (actual_name,
9083 built_actual_name != NULL,
9084 VAR_DOMAIN, LOC_TYPEDEF, -1,
9085 psymbol_placement::GLOBAL,
9086 0, cu->language, objfile);
9087 break;
9088 case DW_TAG_module:
9089 /* With Fortran 77 there might be a "BLOCK DATA" module
9090 available without any name. If so, we skip the module as it
9091 doesn't bring any value. */
9092 if (actual_name != nullptr)
9093 add_psymbol_to_list (actual_name,
9094 built_actual_name != NULL,
9095 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9096 psymbol_placement::GLOBAL,
9097 0, cu->language, objfile);
9098 break;
9099 case DW_TAG_class_type:
9100 case DW_TAG_interface_type:
9101 case DW_TAG_structure_type:
9102 case DW_TAG_union_type:
9103 case DW_TAG_enumeration_type:
9104 /* Skip external references. The DWARF standard says in the section
9105 about "Structure, Union, and Class Type Entries": "An incomplete
9106 structure, union or class type is represented by a structure,
9107 union or class entry that does not have a byte size attribute
9108 and that has a DW_AT_declaration attribute." */
9109 if (!pdi->has_byte_size && pdi->is_declaration)
9110 {
9111 xfree (built_actual_name);
9112 return;
9113 }
9114
9115 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9116 static vs. global. */
9117 add_psymbol_to_list (actual_name,
9118 built_actual_name != NULL,
9119 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9120 cu->language == language_cplus
9121 ? psymbol_placement::GLOBAL
9122 : psymbol_placement::STATIC,
9123 0, cu->language, objfile);
9124
9125 break;
9126 case DW_TAG_enumerator:
9127 add_psymbol_to_list (actual_name,
9128 built_actual_name != NULL,
9129 VAR_DOMAIN, LOC_CONST, -1,
9130 cu->language == language_cplus
9131 ? psymbol_placement::GLOBAL
9132 : psymbol_placement::STATIC,
9133 0, cu->language, objfile);
9134 break;
9135 default:
9136 break;
9137 }
9138
9139 xfree (built_actual_name);
9140 }
9141
9142 /* Read a partial die corresponding to a namespace; also, add a symbol
9143 corresponding to that namespace to the symbol table. NAMESPACE is
9144 the name of the enclosing namespace. */
9145
9146 static void
9147 add_partial_namespace (struct partial_die_info *pdi,
9148 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9149 int set_addrmap, struct dwarf2_cu *cu)
9150 {
9151 /* Add a symbol for the namespace. */
9152
9153 add_partial_symbol (pdi, cu);
9154
9155 /* Now scan partial symbols in that namespace. */
9156
9157 if (pdi->has_children)
9158 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9159 }
9160
9161 /* Read a partial die corresponding to a Fortran module. */
9162
9163 static void
9164 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9165 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9166 {
9167 /* Add a symbol for the namespace. */
9168
9169 add_partial_symbol (pdi, cu);
9170
9171 /* Now scan partial symbols in that module. */
9172
9173 if (pdi->has_children)
9174 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9175 }
9176
9177 /* Read a partial die corresponding to a subprogram or an inlined
9178 subprogram and create a partial symbol for that subprogram.
9179 When the CU language allows it, this routine also defines a partial
9180 symbol for each nested subprogram that this subprogram contains.
9181 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9182 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9183
9184 PDI may also be a lexical block, in which case we simply search
9185 recursively for subprograms defined inside that lexical block.
9186 Again, this is only performed when the CU language allows this
9187 type of definitions. */
9188
9189 static void
9190 add_partial_subprogram (struct partial_die_info *pdi,
9191 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9192 int set_addrmap, struct dwarf2_cu *cu)
9193 {
9194 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9195 {
9196 if (pdi->has_pc_info)
9197 {
9198 if (pdi->lowpc < *lowpc)
9199 *lowpc = pdi->lowpc;
9200 if (pdi->highpc > *highpc)
9201 *highpc = pdi->highpc;
9202 if (set_addrmap)
9203 {
9204 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9205 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9206 CORE_ADDR baseaddr;
9207 CORE_ADDR this_highpc;
9208 CORE_ADDR this_lowpc;
9209
9210 baseaddr = ANOFFSET (objfile->section_offsets,
9211 SECT_OFF_TEXT (objfile));
9212 this_lowpc
9213 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9214 pdi->lowpc + baseaddr)
9215 - baseaddr);
9216 this_highpc
9217 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9218 pdi->highpc + baseaddr)
9219 - baseaddr);
9220 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9221 this_lowpc, this_highpc - 1,
9222 cu->per_cu->v.psymtab);
9223 }
9224 }
9225
9226 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9227 {
9228 if (!pdi->is_declaration)
9229 /* Ignore subprogram DIEs that do not have a name, they are
9230 illegal. Do not emit a complaint at this point, we will
9231 do so when we convert this psymtab into a symtab. */
9232 if (pdi->name)
9233 add_partial_symbol (pdi, cu);
9234 }
9235 }
9236
9237 if (! pdi->has_children)
9238 return;
9239
9240 if (cu->language == language_ada || cu->language == language_fortran)
9241 {
9242 pdi = pdi->die_child;
9243 while (pdi != NULL)
9244 {
9245 pdi->fixup (cu);
9246 if (pdi->tag == DW_TAG_subprogram
9247 || pdi->tag == DW_TAG_inlined_subroutine
9248 || pdi->tag == DW_TAG_lexical_block)
9249 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9250 pdi = pdi->die_sibling;
9251 }
9252 }
9253 }
9254
9255 /* Read a partial die corresponding to an enumeration type. */
9256
9257 static void
9258 add_partial_enumeration (struct partial_die_info *enum_pdi,
9259 struct dwarf2_cu *cu)
9260 {
9261 struct partial_die_info *pdi;
9262
9263 if (enum_pdi->name != NULL)
9264 add_partial_symbol (enum_pdi, cu);
9265
9266 pdi = enum_pdi->die_child;
9267 while (pdi)
9268 {
9269 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9270 complaint (_("malformed enumerator DIE ignored"));
9271 else
9272 add_partial_symbol (pdi, cu);
9273 pdi = pdi->die_sibling;
9274 }
9275 }
9276
9277 /* Return the initial uleb128 in the die at INFO_PTR. */
9278
9279 static unsigned int
9280 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9281 {
9282 unsigned int bytes_read;
9283
9284 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9285 }
9286
9287 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9288 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9289
9290 Return the corresponding abbrev, or NULL if the number is zero (indicating
9291 an empty DIE). In either case *BYTES_READ will be set to the length of
9292 the initial number. */
9293
9294 static struct abbrev_info *
9295 peek_die_abbrev (const die_reader_specs &reader,
9296 const gdb_byte *info_ptr, unsigned int *bytes_read)
9297 {
9298 dwarf2_cu *cu = reader.cu;
9299 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9300 unsigned int abbrev_number
9301 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9302
9303 if (abbrev_number == 0)
9304 return NULL;
9305
9306 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9307 if (!abbrev)
9308 {
9309 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9310 " at offset %s [in module %s]"),
9311 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9312 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9313 }
9314
9315 return abbrev;
9316 }
9317
9318 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9319 Returns a pointer to the end of a series of DIEs, terminated by an empty
9320 DIE. Any children of the skipped DIEs will also be skipped. */
9321
9322 static const gdb_byte *
9323 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9324 {
9325 while (1)
9326 {
9327 unsigned int bytes_read;
9328 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9329
9330 if (abbrev == NULL)
9331 return info_ptr + bytes_read;
9332 else
9333 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9334 }
9335 }
9336
9337 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9338 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9339 abbrev corresponding to that skipped uleb128 should be passed in
9340 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9341 children. */
9342
9343 static const gdb_byte *
9344 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9345 struct abbrev_info *abbrev)
9346 {
9347 unsigned int bytes_read;
9348 struct attribute attr;
9349 bfd *abfd = reader->abfd;
9350 struct dwarf2_cu *cu = reader->cu;
9351 const gdb_byte *buffer = reader->buffer;
9352 const gdb_byte *buffer_end = reader->buffer_end;
9353 unsigned int form, i;
9354
9355 for (i = 0; i < abbrev->num_attrs; i++)
9356 {
9357 /* The only abbrev we care about is DW_AT_sibling. */
9358 if (abbrev->attrs[i].name == DW_AT_sibling)
9359 {
9360 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9361 if (attr.form == DW_FORM_ref_addr)
9362 complaint (_("ignoring absolute DW_AT_sibling"));
9363 else
9364 {
9365 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9366 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9367
9368 if (sibling_ptr < info_ptr)
9369 complaint (_("DW_AT_sibling points backwards"));
9370 else if (sibling_ptr > reader->buffer_end)
9371 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9372 else
9373 return sibling_ptr;
9374 }
9375 }
9376
9377 /* If it isn't DW_AT_sibling, skip this attribute. */
9378 form = abbrev->attrs[i].form;
9379 skip_attribute:
9380 switch (form)
9381 {
9382 case DW_FORM_ref_addr:
9383 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9384 and later it is offset sized. */
9385 if (cu->header.version == 2)
9386 info_ptr += cu->header.addr_size;
9387 else
9388 info_ptr += cu->header.offset_size;
9389 break;
9390 case DW_FORM_GNU_ref_alt:
9391 info_ptr += cu->header.offset_size;
9392 break;
9393 case DW_FORM_addr:
9394 info_ptr += cu->header.addr_size;
9395 break;
9396 case DW_FORM_data1:
9397 case DW_FORM_ref1:
9398 case DW_FORM_flag:
9399 case DW_FORM_strx1:
9400 info_ptr += 1;
9401 break;
9402 case DW_FORM_flag_present:
9403 case DW_FORM_implicit_const:
9404 break;
9405 case DW_FORM_data2:
9406 case DW_FORM_ref2:
9407 case DW_FORM_strx2:
9408 info_ptr += 2;
9409 break;
9410 case DW_FORM_strx3:
9411 info_ptr += 3;
9412 break;
9413 case DW_FORM_data4:
9414 case DW_FORM_ref4:
9415 case DW_FORM_strx4:
9416 info_ptr += 4;
9417 break;
9418 case DW_FORM_data8:
9419 case DW_FORM_ref8:
9420 case DW_FORM_ref_sig8:
9421 info_ptr += 8;
9422 break;
9423 case DW_FORM_data16:
9424 info_ptr += 16;
9425 break;
9426 case DW_FORM_string:
9427 read_direct_string (abfd, info_ptr, &bytes_read);
9428 info_ptr += bytes_read;
9429 break;
9430 case DW_FORM_sec_offset:
9431 case DW_FORM_strp:
9432 case DW_FORM_GNU_strp_alt:
9433 info_ptr += cu->header.offset_size;
9434 break;
9435 case DW_FORM_exprloc:
9436 case DW_FORM_block:
9437 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9438 info_ptr += bytes_read;
9439 break;
9440 case DW_FORM_block1:
9441 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9442 break;
9443 case DW_FORM_block2:
9444 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9445 break;
9446 case DW_FORM_block4:
9447 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9448 break;
9449 case DW_FORM_addrx:
9450 case DW_FORM_strx:
9451 case DW_FORM_sdata:
9452 case DW_FORM_udata:
9453 case DW_FORM_ref_udata:
9454 case DW_FORM_GNU_addr_index:
9455 case DW_FORM_GNU_str_index:
9456 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9457 break;
9458 case DW_FORM_indirect:
9459 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9460 info_ptr += bytes_read;
9461 /* We need to continue parsing from here, so just go back to
9462 the top. */
9463 goto skip_attribute;
9464
9465 default:
9466 error (_("Dwarf Error: Cannot handle %s "
9467 "in DWARF reader [in module %s]"),
9468 dwarf_form_name (form),
9469 bfd_get_filename (abfd));
9470 }
9471 }
9472
9473 if (abbrev->has_children)
9474 return skip_children (reader, info_ptr);
9475 else
9476 return info_ptr;
9477 }
9478
9479 /* Locate ORIG_PDI's sibling.
9480 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9481
9482 static const gdb_byte *
9483 locate_pdi_sibling (const struct die_reader_specs *reader,
9484 struct partial_die_info *orig_pdi,
9485 const gdb_byte *info_ptr)
9486 {
9487 /* Do we know the sibling already? */
9488
9489 if (orig_pdi->sibling)
9490 return orig_pdi->sibling;
9491
9492 /* Are there any children to deal with? */
9493
9494 if (!orig_pdi->has_children)
9495 return info_ptr;
9496
9497 /* Skip the children the long way. */
9498
9499 return skip_children (reader, info_ptr);
9500 }
9501
9502 /* Expand this partial symbol table into a full symbol table. SELF is
9503 not NULL. */
9504
9505 static void
9506 dwarf2_read_symtab (struct partial_symtab *self,
9507 struct objfile *objfile)
9508 {
9509 struct dwarf2_per_objfile *dwarf2_per_objfile
9510 = get_dwarf2_per_objfile (objfile);
9511
9512 if (self->readin)
9513 {
9514 warning (_("bug: psymtab for %s is already read in."),
9515 self->filename);
9516 }
9517 else
9518 {
9519 if (info_verbose)
9520 {
9521 printf_filtered (_("Reading in symbols for %s..."),
9522 self->filename);
9523 gdb_flush (gdb_stdout);
9524 }
9525
9526 /* If this psymtab is constructed from a debug-only objfile, the
9527 has_section_at_zero flag will not necessarily be correct. We
9528 can get the correct value for this flag by looking at the data
9529 associated with the (presumably stripped) associated objfile. */
9530 if (objfile->separate_debug_objfile_backlink)
9531 {
9532 struct dwarf2_per_objfile *dpo_backlink
9533 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9534
9535 dwarf2_per_objfile->has_section_at_zero
9536 = dpo_backlink->has_section_at_zero;
9537 }
9538
9539 dwarf2_per_objfile->reading_partial_symbols = 0;
9540
9541 psymtab_to_symtab_1 (self);
9542
9543 /* Finish up the debug error message. */
9544 if (info_verbose)
9545 printf_filtered (_("done.\n"));
9546 }
9547
9548 process_cu_includes (dwarf2_per_objfile);
9549 }
9550 \f
9551 /* Reading in full CUs. */
9552
9553 /* Add PER_CU to the queue. */
9554
9555 static void
9556 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9557 enum language pretend_language)
9558 {
9559 struct dwarf2_queue_item *item;
9560
9561 per_cu->queued = 1;
9562 item = XNEW (struct dwarf2_queue_item);
9563 item->per_cu = per_cu;
9564 item->pretend_language = pretend_language;
9565 item->next = NULL;
9566
9567 if (dwarf2_queue == NULL)
9568 dwarf2_queue = item;
9569 else
9570 dwarf2_queue_tail->next = item;
9571
9572 dwarf2_queue_tail = item;
9573 }
9574
9575 /* If PER_CU is not yet queued, add it to the queue.
9576 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9577 dependency.
9578 The result is non-zero if PER_CU was queued, otherwise the result is zero
9579 meaning either PER_CU is already queued or it is already loaded.
9580
9581 N.B. There is an invariant here that if a CU is queued then it is loaded.
9582 The caller is required to load PER_CU if we return non-zero. */
9583
9584 static int
9585 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9586 struct dwarf2_per_cu_data *per_cu,
9587 enum language pretend_language)
9588 {
9589 /* We may arrive here during partial symbol reading, if we need full
9590 DIEs to process an unusual case (e.g. template arguments). Do
9591 not queue PER_CU, just tell our caller to load its DIEs. */
9592 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9593 {
9594 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9595 return 1;
9596 return 0;
9597 }
9598
9599 /* Mark the dependence relation so that we don't flush PER_CU
9600 too early. */
9601 if (dependent_cu != NULL)
9602 dwarf2_add_dependence (dependent_cu, per_cu);
9603
9604 /* If it's already on the queue, we have nothing to do. */
9605 if (per_cu->queued)
9606 return 0;
9607
9608 /* If the compilation unit is already loaded, just mark it as
9609 used. */
9610 if (per_cu->cu != NULL)
9611 {
9612 per_cu->cu->last_used = 0;
9613 return 0;
9614 }
9615
9616 /* Add it to the queue. */
9617 queue_comp_unit (per_cu, pretend_language);
9618
9619 return 1;
9620 }
9621
9622 /* Process the queue. */
9623
9624 static void
9625 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9626 {
9627 struct dwarf2_queue_item *item, *next_item;
9628
9629 if (dwarf_read_debug)
9630 {
9631 fprintf_unfiltered (gdb_stdlog,
9632 "Expanding one or more symtabs of objfile %s ...\n",
9633 objfile_name (dwarf2_per_objfile->objfile));
9634 }
9635
9636 /* The queue starts out with one item, but following a DIE reference
9637 may load a new CU, adding it to the end of the queue. */
9638 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9639 {
9640 if ((dwarf2_per_objfile->using_index
9641 ? !item->per_cu->v.quick->compunit_symtab
9642 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9643 /* Skip dummy CUs. */
9644 && item->per_cu->cu != NULL)
9645 {
9646 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9647 unsigned int debug_print_threshold;
9648 char buf[100];
9649
9650 if (per_cu->is_debug_types)
9651 {
9652 struct signatured_type *sig_type =
9653 (struct signatured_type *) per_cu;
9654
9655 sprintf (buf, "TU %s at offset %s",
9656 hex_string (sig_type->signature),
9657 sect_offset_str (per_cu->sect_off));
9658 /* There can be 100s of TUs.
9659 Only print them in verbose mode. */
9660 debug_print_threshold = 2;
9661 }
9662 else
9663 {
9664 sprintf (buf, "CU at offset %s",
9665 sect_offset_str (per_cu->sect_off));
9666 debug_print_threshold = 1;
9667 }
9668
9669 if (dwarf_read_debug >= debug_print_threshold)
9670 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9671
9672 if (per_cu->is_debug_types)
9673 process_full_type_unit (per_cu, item->pretend_language);
9674 else
9675 process_full_comp_unit (per_cu, item->pretend_language);
9676
9677 if (dwarf_read_debug >= debug_print_threshold)
9678 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9679 }
9680
9681 item->per_cu->queued = 0;
9682 next_item = item->next;
9683 xfree (item);
9684 }
9685
9686 dwarf2_queue_tail = NULL;
9687
9688 if (dwarf_read_debug)
9689 {
9690 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9691 objfile_name (dwarf2_per_objfile->objfile));
9692 }
9693 }
9694
9695 /* Read in full symbols for PST, and anything it depends on. */
9696
9697 static void
9698 psymtab_to_symtab_1 (struct partial_symtab *pst)
9699 {
9700 struct dwarf2_per_cu_data *per_cu;
9701 int i;
9702
9703 if (pst->readin)
9704 return;
9705
9706 for (i = 0; i < pst->number_of_dependencies; i++)
9707 if (!pst->dependencies[i]->readin
9708 && pst->dependencies[i]->user == NULL)
9709 {
9710 /* Inform about additional files that need to be read in. */
9711 if (info_verbose)
9712 {
9713 /* FIXME: i18n: Need to make this a single string. */
9714 fputs_filtered (" ", gdb_stdout);
9715 wrap_here ("");
9716 fputs_filtered ("and ", gdb_stdout);
9717 wrap_here ("");
9718 printf_filtered ("%s...", pst->dependencies[i]->filename);
9719 wrap_here (""); /* Flush output. */
9720 gdb_flush (gdb_stdout);
9721 }
9722 psymtab_to_symtab_1 (pst->dependencies[i]);
9723 }
9724
9725 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9726
9727 if (per_cu == NULL)
9728 {
9729 /* It's an include file, no symbols to read for it.
9730 Everything is in the parent symtab. */
9731 pst->readin = 1;
9732 return;
9733 }
9734
9735 dw2_do_instantiate_symtab (per_cu, false);
9736 }
9737
9738 /* Trivial hash function for die_info: the hash value of a DIE
9739 is its offset in .debug_info for this objfile. */
9740
9741 static hashval_t
9742 die_hash (const void *item)
9743 {
9744 const struct die_info *die = (const struct die_info *) item;
9745
9746 return to_underlying (die->sect_off);
9747 }
9748
9749 /* Trivial comparison function for die_info structures: two DIEs
9750 are equal if they have the same offset. */
9751
9752 static int
9753 die_eq (const void *item_lhs, const void *item_rhs)
9754 {
9755 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9756 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9757
9758 return die_lhs->sect_off == die_rhs->sect_off;
9759 }
9760
9761 /* die_reader_func for load_full_comp_unit.
9762 This is identical to read_signatured_type_reader,
9763 but is kept separate for now. */
9764
9765 static void
9766 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9767 const gdb_byte *info_ptr,
9768 struct die_info *comp_unit_die,
9769 int has_children,
9770 void *data)
9771 {
9772 struct dwarf2_cu *cu = reader->cu;
9773 enum language *language_ptr = (enum language *) data;
9774
9775 gdb_assert (cu->die_hash == NULL);
9776 cu->die_hash =
9777 htab_create_alloc_ex (cu->header.length / 12,
9778 die_hash,
9779 die_eq,
9780 NULL,
9781 &cu->comp_unit_obstack,
9782 hashtab_obstack_allocate,
9783 dummy_obstack_deallocate);
9784
9785 if (has_children)
9786 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9787 &info_ptr, comp_unit_die);
9788 cu->dies = comp_unit_die;
9789 /* comp_unit_die is not stored in die_hash, no need. */
9790
9791 /* We try not to read any attributes in this function, because not
9792 all CUs needed for references have been loaded yet, and symbol
9793 table processing isn't initialized. But we have to set the CU language,
9794 or we won't be able to build types correctly.
9795 Similarly, if we do not read the producer, we can not apply
9796 producer-specific interpretation. */
9797 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9798 }
9799
9800 /* Load the DIEs associated with PER_CU into memory. */
9801
9802 static void
9803 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9804 bool skip_partial,
9805 enum language pretend_language)
9806 {
9807 gdb_assert (! this_cu->is_debug_types);
9808
9809 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9810 load_full_comp_unit_reader, &pretend_language);
9811 }
9812
9813 /* Add a DIE to the delayed physname list. */
9814
9815 static void
9816 add_to_method_list (struct type *type, int fnfield_index, int index,
9817 const char *name, struct die_info *die,
9818 struct dwarf2_cu *cu)
9819 {
9820 struct delayed_method_info mi;
9821 mi.type = type;
9822 mi.fnfield_index = fnfield_index;
9823 mi.index = index;
9824 mi.name = name;
9825 mi.die = die;
9826 cu->method_list.push_back (mi);
9827 }
9828
9829 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9830 "const" / "volatile". If so, decrements LEN by the length of the
9831 modifier and return true. Otherwise return false. */
9832
9833 template<size_t N>
9834 static bool
9835 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9836 {
9837 size_t mod_len = sizeof (mod) - 1;
9838 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9839 {
9840 len -= mod_len;
9841 return true;
9842 }
9843 return false;
9844 }
9845
9846 /* Compute the physnames of any methods on the CU's method list.
9847
9848 The computation of method physnames is delayed in order to avoid the
9849 (bad) condition that one of the method's formal parameters is of an as yet
9850 incomplete type. */
9851
9852 static void
9853 compute_delayed_physnames (struct dwarf2_cu *cu)
9854 {
9855 /* Only C++ delays computing physnames. */
9856 if (cu->method_list.empty ())
9857 return;
9858 gdb_assert (cu->language == language_cplus);
9859
9860 for (const delayed_method_info &mi : cu->method_list)
9861 {
9862 const char *physname;
9863 struct fn_fieldlist *fn_flp
9864 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9865 physname = dwarf2_physname (mi.name, mi.die, cu);
9866 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9867 = physname ? physname : "";
9868
9869 /* Since there's no tag to indicate whether a method is a
9870 const/volatile overload, extract that information out of the
9871 demangled name. */
9872 if (physname != NULL)
9873 {
9874 size_t len = strlen (physname);
9875
9876 while (1)
9877 {
9878 if (physname[len] == ')') /* shortcut */
9879 break;
9880 else if (check_modifier (physname, len, " const"))
9881 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9882 else if (check_modifier (physname, len, " volatile"))
9883 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9884 else
9885 break;
9886 }
9887 }
9888 }
9889
9890 /* The list is no longer needed. */
9891 cu->method_list.clear ();
9892 }
9893
9894 /* Go objects should be embedded in a DW_TAG_module DIE,
9895 and it's not clear if/how imported objects will appear.
9896 To keep Go support simple until that's worked out,
9897 go back through what we've read and create something usable.
9898 We could do this while processing each DIE, and feels kinda cleaner,
9899 but that way is more invasive.
9900 This is to, for example, allow the user to type "p var" or "b main"
9901 without having to specify the package name, and allow lookups
9902 of module.object to work in contexts that use the expression
9903 parser. */
9904
9905 static void
9906 fixup_go_packaging (struct dwarf2_cu *cu)
9907 {
9908 char *package_name = NULL;
9909 struct pending *list;
9910 int i;
9911
9912 for (list = *cu->get_builder ()->get_global_symbols ();
9913 list != NULL;
9914 list = list->next)
9915 {
9916 for (i = 0; i < list->nsyms; ++i)
9917 {
9918 struct symbol *sym = list->symbol[i];
9919
9920 if (SYMBOL_LANGUAGE (sym) == language_go
9921 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9922 {
9923 char *this_package_name = go_symbol_package_name (sym);
9924
9925 if (this_package_name == NULL)
9926 continue;
9927 if (package_name == NULL)
9928 package_name = this_package_name;
9929 else
9930 {
9931 struct objfile *objfile
9932 = cu->per_cu->dwarf2_per_objfile->objfile;
9933 if (strcmp (package_name, this_package_name) != 0)
9934 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9935 (symbol_symtab (sym) != NULL
9936 ? symtab_to_filename_for_display
9937 (symbol_symtab (sym))
9938 : objfile_name (objfile)),
9939 this_package_name, package_name);
9940 xfree (this_package_name);
9941 }
9942 }
9943 }
9944 }
9945
9946 if (package_name != NULL)
9947 {
9948 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9949 const char *saved_package_name
9950 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
9951 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9952 saved_package_name);
9953 struct symbol *sym;
9954
9955 sym = allocate_symbol (objfile);
9956 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9957 SYMBOL_SET_NAMES (sym, saved_package_name, false, objfile);
9958 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9959 e.g., "main" finds the "main" module and not C's main(). */
9960 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9961 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9962 SYMBOL_TYPE (sym) = type;
9963
9964 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9965
9966 xfree (package_name);
9967 }
9968 }
9969
9970 /* Allocate a fully-qualified name consisting of the two parts on the
9971 obstack. */
9972
9973 static const char *
9974 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9975 {
9976 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9977 }
9978
9979 /* A helper that allocates a struct discriminant_info to attach to a
9980 union type. */
9981
9982 static struct discriminant_info *
9983 alloc_discriminant_info (struct type *type, int discriminant_index,
9984 int default_index)
9985 {
9986 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9987 gdb_assert (discriminant_index == -1
9988 || (discriminant_index >= 0
9989 && discriminant_index < TYPE_NFIELDS (type)));
9990 gdb_assert (default_index == -1
9991 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9992
9993 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9994
9995 struct discriminant_info *disc
9996 = ((struct discriminant_info *)
9997 TYPE_ZALLOC (type,
9998 offsetof (struct discriminant_info, discriminants)
9999 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
10000 disc->default_index = default_index;
10001 disc->discriminant_index = discriminant_index;
10002
10003 struct dynamic_prop prop;
10004 prop.kind = PROP_UNDEFINED;
10005 prop.data.baton = disc;
10006
10007 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10008
10009 return disc;
10010 }
10011
10012 /* Some versions of rustc emitted enums in an unusual way.
10013
10014 Ordinary enums were emitted as unions. The first element of each
10015 structure in the union was named "RUST$ENUM$DISR". This element
10016 held the discriminant.
10017
10018 These versions of Rust also implemented the "non-zero"
10019 optimization. When the enum had two values, and one is empty and
10020 the other holds a pointer that cannot be zero, the pointer is used
10021 as the discriminant, with a zero value meaning the empty variant.
10022 Here, the union's first member is of the form
10023 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10024 where the fieldnos are the indices of the fields that should be
10025 traversed in order to find the field (which may be several fields deep)
10026 and the variantname is the name of the variant of the case when the
10027 field is zero.
10028
10029 This function recognizes whether TYPE is of one of these forms,
10030 and, if so, smashes it to be a variant type. */
10031
10032 static void
10033 quirk_rust_enum (struct type *type, struct objfile *objfile)
10034 {
10035 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10036
10037 /* We don't need to deal with empty enums. */
10038 if (TYPE_NFIELDS (type) == 0)
10039 return;
10040
10041 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10042 if (TYPE_NFIELDS (type) == 1
10043 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10044 {
10045 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10046
10047 /* Decode the field name to find the offset of the
10048 discriminant. */
10049 ULONGEST bit_offset = 0;
10050 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10051 while (name[0] >= '0' && name[0] <= '9')
10052 {
10053 char *tail;
10054 unsigned long index = strtoul (name, &tail, 10);
10055 name = tail;
10056 if (*name != '$'
10057 || index >= TYPE_NFIELDS (field_type)
10058 || (TYPE_FIELD_LOC_KIND (field_type, index)
10059 != FIELD_LOC_KIND_BITPOS))
10060 {
10061 complaint (_("Could not parse Rust enum encoding string \"%s\""
10062 "[in module %s]"),
10063 TYPE_FIELD_NAME (type, 0),
10064 objfile_name (objfile));
10065 return;
10066 }
10067 ++name;
10068
10069 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10070 field_type = TYPE_FIELD_TYPE (field_type, index);
10071 }
10072
10073 /* Make a union to hold the variants. */
10074 struct type *union_type = alloc_type (objfile);
10075 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10076 TYPE_NFIELDS (union_type) = 3;
10077 TYPE_FIELDS (union_type)
10078 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10079 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10080 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10081
10082 /* Put the discriminant must at index 0. */
10083 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10084 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10085 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10086 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10087
10088 /* The order of fields doesn't really matter, so put the real
10089 field at index 1 and the data-less field at index 2. */
10090 struct discriminant_info *disc
10091 = alloc_discriminant_info (union_type, 0, 1);
10092 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10093 TYPE_FIELD_NAME (union_type, 1)
10094 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10095 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10096 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10097 TYPE_FIELD_NAME (union_type, 1));
10098
10099 const char *dataless_name
10100 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10101 name);
10102 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10103 dataless_name);
10104 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10105 /* NAME points into the original discriminant name, which
10106 already has the correct lifetime. */
10107 TYPE_FIELD_NAME (union_type, 2) = name;
10108 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10109 disc->discriminants[2] = 0;
10110
10111 /* Smash this type to be a structure type. We have to do this
10112 because the type has already been recorded. */
10113 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10114 TYPE_NFIELDS (type) = 1;
10115 TYPE_FIELDS (type)
10116 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10117
10118 /* Install the variant part. */
10119 TYPE_FIELD_TYPE (type, 0) = union_type;
10120 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10121 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10122 }
10123 /* A union with a single anonymous field is probably an old-style
10124 univariant enum. */
10125 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10126 {
10127 /* Smash this type to be a structure type. We have to do this
10128 because the type has already been recorded. */
10129 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10130
10131 /* Make a union to hold the variants. */
10132 struct type *union_type = alloc_type (objfile);
10133 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10134 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10135 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10136 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10137 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10138
10139 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10140 const char *variant_name
10141 = rust_last_path_segment (TYPE_NAME (field_type));
10142 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10143 TYPE_NAME (field_type)
10144 = rust_fully_qualify (&objfile->objfile_obstack,
10145 TYPE_NAME (type), variant_name);
10146
10147 /* Install the union in the outer struct type. */
10148 TYPE_NFIELDS (type) = 1;
10149 TYPE_FIELDS (type)
10150 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10151 TYPE_FIELD_TYPE (type, 0) = union_type;
10152 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10153 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10154
10155 alloc_discriminant_info (union_type, -1, 0);
10156 }
10157 else
10158 {
10159 struct type *disr_type = nullptr;
10160 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10161 {
10162 disr_type = TYPE_FIELD_TYPE (type, i);
10163
10164 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10165 {
10166 /* All fields of a true enum will be structs. */
10167 return;
10168 }
10169 else if (TYPE_NFIELDS (disr_type) == 0)
10170 {
10171 /* Could be data-less variant, so keep going. */
10172 disr_type = nullptr;
10173 }
10174 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10175 "RUST$ENUM$DISR") != 0)
10176 {
10177 /* Not a Rust enum. */
10178 return;
10179 }
10180 else
10181 {
10182 /* Found one. */
10183 break;
10184 }
10185 }
10186
10187 /* If we got here without a discriminant, then it's probably
10188 just a union. */
10189 if (disr_type == nullptr)
10190 return;
10191
10192 /* Smash this type to be a structure type. We have to do this
10193 because the type has already been recorded. */
10194 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10195
10196 /* Make a union to hold the variants. */
10197 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10198 struct type *union_type = alloc_type (objfile);
10199 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10200 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10201 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10202 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10203 TYPE_FIELDS (union_type)
10204 = (struct field *) TYPE_ZALLOC (union_type,
10205 (TYPE_NFIELDS (union_type)
10206 * sizeof (struct field)));
10207
10208 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10209 TYPE_NFIELDS (type) * sizeof (struct field));
10210
10211 /* Install the discriminant at index 0 in the union. */
10212 TYPE_FIELD (union_type, 0) = *disr_field;
10213 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10214 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10215
10216 /* Install the union in the outer struct type. */
10217 TYPE_FIELD_TYPE (type, 0) = union_type;
10218 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10219 TYPE_NFIELDS (type) = 1;
10220
10221 /* Set the size and offset of the union type. */
10222 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10223
10224 /* We need a way to find the correct discriminant given a
10225 variant name. For convenience we build a map here. */
10226 struct type *enum_type = FIELD_TYPE (*disr_field);
10227 std::unordered_map<std::string, ULONGEST> discriminant_map;
10228 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10229 {
10230 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10231 {
10232 const char *name
10233 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10234 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10235 }
10236 }
10237
10238 int n_fields = TYPE_NFIELDS (union_type);
10239 struct discriminant_info *disc
10240 = alloc_discriminant_info (union_type, 0, -1);
10241 /* Skip the discriminant here. */
10242 for (int i = 1; i < n_fields; ++i)
10243 {
10244 /* Find the final word in the name of this variant's type.
10245 That name can be used to look up the correct
10246 discriminant. */
10247 const char *variant_name
10248 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10249 i)));
10250
10251 auto iter = discriminant_map.find (variant_name);
10252 if (iter != discriminant_map.end ())
10253 disc->discriminants[i] = iter->second;
10254
10255 /* Remove the discriminant field, if it exists. */
10256 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10257 if (TYPE_NFIELDS (sub_type) > 0)
10258 {
10259 --TYPE_NFIELDS (sub_type);
10260 ++TYPE_FIELDS (sub_type);
10261 }
10262 TYPE_FIELD_NAME (union_type, i) = variant_name;
10263 TYPE_NAME (sub_type)
10264 = rust_fully_qualify (&objfile->objfile_obstack,
10265 TYPE_NAME (type), variant_name);
10266 }
10267 }
10268 }
10269
10270 /* Rewrite some Rust unions to be structures with variants parts. */
10271
10272 static void
10273 rust_union_quirks (struct dwarf2_cu *cu)
10274 {
10275 gdb_assert (cu->language == language_rust);
10276 for (type *type_ : cu->rust_unions)
10277 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10278 /* We don't need this any more. */
10279 cu->rust_unions.clear ();
10280 }
10281
10282 /* Return the symtab for PER_CU. This works properly regardless of
10283 whether we're using the index or psymtabs. */
10284
10285 static struct compunit_symtab *
10286 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10287 {
10288 return (per_cu->dwarf2_per_objfile->using_index
10289 ? per_cu->v.quick->compunit_symtab
10290 : per_cu->v.psymtab->compunit_symtab);
10291 }
10292
10293 /* A helper function for computing the list of all symbol tables
10294 included by PER_CU. */
10295
10296 static void
10297 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10298 htab_t all_children, htab_t all_type_symtabs,
10299 struct dwarf2_per_cu_data *per_cu,
10300 struct compunit_symtab *immediate_parent)
10301 {
10302 void **slot;
10303 struct compunit_symtab *cust;
10304
10305 slot = htab_find_slot (all_children, per_cu, INSERT);
10306 if (*slot != NULL)
10307 {
10308 /* This inclusion and its children have been processed. */
10309 return;
10310 }
10311
10312 *slot = per_cu;
10313 /* Only add a CU if it has a symbol table. */
10314 cust = get_compunit_symtab (per_cu);
10315 if (cust != NULL)
10316 {
10317 /* If this is a type unit only add its symbol table if we haven't
10318 seen it yet (type unit per_cu's can share symtabs). */
10319 if (per_cu->is_debug_types)
10320 {
10321 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10322 if (*slot == NULL)
10323 {
10324 *slot = cust;
10325 result->push_back (cust);
10326 if (cust->user == NULL)
10327 cust->user = immediate_parent;
10328 }
10329 }
10330 else
10331 {
10332 result->push_back (cust);
10333 if (cust->user == NULL)
10334 cust->user = immediate_parent;
10335 }
10336 }
10337
10338 if (!per_cu->imported_symtabs_empty ())
10339 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10340 {
10341 recursively_compute_inclusions (result, all_children,
10342 all_type_symtabs, ptr, cust);
10343 }
10344 }
10345
10346 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10347 PER_CU. */
10348
10349 static void
10350 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10351 {
10352 gdb_assert (! per_cu->is_debug_types);
10353
10354 if (!per_cu->imported_symtabs_empty ())
10355 {
10356 int len;
10357 std::vector<compunit_symtab *> result_symtabs;
10358 htab_t all_children, all_type_symtabs;
10359 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10360
10361 /* If we don't have a symtab, we can just skip this case. */
10362 if (cust == NULL)
10363 return;
10364
10365 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10366 NULL, xcalloc, xfree);
10367 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10368 NULL, xcalloc, xfree);
10369
10370 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10371 {
10372 recursively_compute_inclusions (&result_symtabs, all_children,
10373 all_type_symtabs, ptr, cust);
10374 }
10375
10376 /* Now we have a transitive closure of all the included symtabs. */
10377 len = result_symtabs.size ();
10378 cust->includes
10379 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10380 struct compunit_symtab *, len + 1);
10381 memcpy (cust->includes, result_symtabs.data (),
10382 len * sizeof (compunit_symtab *));
10383 cust->includes[len] = NULL;
10384
10385 htab_delete (all_children);
10386 htab_delete (all_type_symtabs);
10387 }
10388 }
10389
10390 /* Compute the 'includes' field for the symtabs of all the CUs we just
10391 read. */
10392
10393 static void
10394 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10395 {
10396 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10397 {
10398 if (! iter->is_debug_types)
10399 compute_compunit_symtab_includes (iter);
10400 }
10401
10402 dwarf2_per_objfile->just_read_cus.clear ();
10403 }
10404
10405 /* Generate full symbol information for PER_CU, whose DIEs have
10406 already been loaded into memory. */
10407
10408 static void
10409 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10410 enum language pretend_language)
10411 {
10412 struct dwarf2_cu *cu = per_cu->cu;
10413 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10414 struct objfile *objfile = dwarf2_per_objfile->objfile;
10415 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10416 CORE_ADDR lowpc, highpc;
10417 struct compunit_symtab *cust;
10418 CORE_ADDR baseaddr;
10419 struct block *static_block;
10420 CORE_ADDR addr;
10421
10422 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10423
10424 /* Clear the list here in case something was left over. */
10425 cu->method_list.clear ();
10426
10427 cu->language = pretend_language;
10428 cu->language_defn = language_def (cu->language);
10429
10430 /* Do line number decoding in read_file_scope () */
10431 process_die (cu->dies, cu);
10432
10433 /* For now fudge the Go package. */
10434 if (cu->language == language_go)
10435 fixup_go_packaging (cu);
10436
10437 /* Now that we have processed all the DIEs in the CU, all the types
10438 should be complete, and it should now be safe to compute all of the
10439 physnames. */
10440 compute_delayed_physnames (cu);
10441
10442 if (cu->language == language_rust)
10443 rust_union_quirks (cu);
10444
10445 /* Some compilers don't define a DW_AT_high_pc attribute for the
10446 compilation unit. If the DW_AT_high_pc is missing, synthesize
10447 it, by scanning the DIE's below the compilation unit. */
10448 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10449
10450 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10451 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10452
10453 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10454 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10455 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10456 addrmap to help ensure it has an accurate map of pc values belonging to
10457 this comp unit. */
10458 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10459
10460 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10461 SECT_OFF_TEXT (objfile),
10462 0);
10463
10464 if (cust != NULL)
10465 {
10466 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10467
10468 /* Set symtab language to language from DW_AT_language. If the
10469 compilation is from a C file generated by language preprocessors, do
10470 not set the language if it was already deduced by start_subfile. */
10471 if (!(cu->language == language_c
10472 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10473 COMPUNIT_FILETABS (cust)->language = cu->language;
10474
10475 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10476 produce DW_AT_location with location lists but it can be possibly
10477 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10478 there were bugs in prologue debug info, fixed later in GCC-4.5
10479 by "unwind info for epilogues" patch (which is not directly related).
10480
10481 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10482 needed, it would be wrong due to missing DW_AT_producer there.
10483
10484 Still one can confuse GDB by using non-standard GCC compilation
10485 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10486 */
10487 if (cu->has_loclist && gcc_4_minor >= 5)
10488 cust->locations_valid = 1;
10489
10490 if (gcc_4_minor >= 5)
10491 cust->epilogue_unwind_valid = 1;
10492
10493 cust->call_site_htab = cu->call_site_htab;
10494 }
10495
10496 if (dwarf2_per_objfile->using_index)
10497 per_cu->v.quick->compunit_symtab = cust;
10498 else
10499 {
10500 struct partial_symtab *pst = per_cu->v.psymtab;
10501 pst->compunit_symtab = cust;
10502 pst->readin = 1;
10503 }
10504
10505 /* Push it for inclusion processing later. */
10506 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10507
10508 /* Not needed any more. */
10509 cu->reset_builder ();
10510 }
10511
10512 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10513 already been loaded into memory. */
10514
10515 static void
10516 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10517 enum language pretend_language)
10518 {
10519 struct dwarf2_cu *cu = per_cu->cu;
10520 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10521 struct objfile *objfile = dwarf2_per_objfile->objfile;
10522 struct compunit_symtab *cust;
10523 struct signatured_type *sig_type;
10524
10525 gdb_assert (per_cu->is_debug_types);
10526 sig_type = (struct signatured_type *) per_cu;
10527
10528 /* Clear the list here in case something was left over. */
10529 cu->method_list.clear ();
10530
10531 cu->language = pretend_language;
10532 cu->language_defn = language_def (cu->language);
10533
10534 /* The symbol tables are set up in read_type_unit_scope. */
10535 process_die (cu->dies, cu);
10536
10537 /* For now fudge the Go package. */
10538 if (cu->language == language_go)
10539 fixup_go_packaging (cu);
10540
10541 /* Now that we have processed all the DIEs in the CU, all the types
10542 should be complete, and it should now be safe to compute all of the
10543 physnames. */
10544 compute_delayed_physnames (cu);
10545
10546 if (cu->language == language_rust)
10547 rust_union_quirks (cu);
10548
10549 /* TUs share symbol tables.
10550 If this is the first TU to use this symtab, complete the construction
10551 of it with end_expandable_symtab. Otherwise, complete the addition of
10552 this TU's symbols to the existing symtab. */
10553 if (sig_type->type_unit_group->compunit_symtab == NULL)
10554 {
10555 buildsym_compunit *builder = cu->get_builder ();
10556 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10557 sig_type->type_unit_group->compunit_symtab = cust;
10558
10559 if (cust != NULL)
10560 {
10561 /* Set symtab language to language from DW_AT_language. If the
10562 compilation is from a C file generated by language preprocessors,
10563 do not set the language if it was already deduced by
10564 start_subfile. */
10565 if (!(cu->language == language_c
10566 && COMPUNIT_FILETABS (cust)->language != language_c))
10567 COMPUNIT_FILETABS (cust)->language = cu->language;
10568 }
10569 }
10570 else
10571 {
10572 cu->get_builder ()->augment_type_symtab ();
10573 cust = sig_type->type_unit_group->compunit_symtab;
10574 }
10575
10576 if (dwarf2_per_objfile->using_index)
10577 per_cu->v.quick->compunit_symtab = cust;
10578 else
10579 {
10580 struct partial_symtab *pst = per_cu->v.psymtab;
10581 pst->compunit_symtab = cust;
10582 pst->readin = 1;
10583 }
10584
10585 /* Not needed any more. */
10586 cu->reset_builder ();
10587 }
10588
10589 /* Process an imported unit DIE. */
10590
10591 static void
10592 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10593 {
10594 struct attribute *attr;
10595
10596 /* For now we don't handle imported units in type units. */
10597 if (cu->per_cu->is_debug_types)
10598 {
10599 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10600 " supported in type units [in module %s]"),
10601 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10602 }
10603
10604 attr = dwarf2_attr (die, DW_AT_import, cu);
10605 if (attr != NULL)
10606 {
10607 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10608 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10609 dwarf2_per_cu_data *per_cu
10610 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10611 cu->per_cu->dwarf2_per_objfile);
10612
10613 /* If necessary, add it to the queue and load its DIEs. */
10614 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10615 load_full_comp_unit (per_cu, false, cu->language);
10616
10617 cu->per_cu->imported_symtabs_push (per_cu);
10618 }
10619 }
10620
10621 /* RAII object that represents a process_die scope: i.e.,
10622 starts/finishes processing a DIE. */
10623 class process_die_scope
10624 {
10625 public:
10626 process_die_scope (die_info *die, dwarf2_cu *cu)
10627 : m_die (die), m_cu (cu)
10628 {
10629 /* We should only be processing DIEs not already in process. */
10630 gdb_assert (!m_die->in_process);
10631 m_die->in_process = true;
10632 }
10633
10634 ~process_die_scope ()
10635 {
10636 m_die->in_process = false;
10637
10638 /* If we're done processing the DIE for the CU that owns the line
10639 header, we don't need the line header anymore. */
10640 if (m_cu->line_header_die_owner == m_die)
10641 {
10642 delete m_cu->line_header;
10643 m_cu->line_header = NULL;
10644 m_cu->line_header_die_owner = NULL;
10645 }
10646 }
10647
10648 private:
10649 die_info *m_die;
10650 dwarf2_cu *m_cu;
10651 };
10652
10653 /* Process a die and its children. */
10654
10655 static void
10656 process_die (struct die_info *die, struct dwarf2_cu *cu)
10657 {
10658 process_die_scope scope (die, cu);
10659
10660 switch (die->tag)
10661 {
10662 case DW_TAG_padding:
10663 break;
10664 case DW_TAG_compile_unit:
10665 case DW_TAG_partial_unit:
10666 read_file_scope (die, cu);
10667 break;
10668 case DW_TAG_type_unit:
10669 read_type_unit_scope (die, cu);
10670 break;
10671 case DW_TAG_subprogram:
10672 /* Nested subprograms in Fortran get a prefix. */
10673 if (cu->language == language_fortran
10674 && die->parent != NULL
10675 && die->parent->tag == DW_TAG_subprogram)
10676 cu->processing_has_namespace_info = true;
10677 /* Fall through. */
10678 case DW_TAG_inlined_subroutine:
10679 read_func_scope (die, cu);
10680 break;
10681 case DW_TAG_lexical_block:
10682 case DW_TAG_try_block:
10683 case DW_TAG_catch_block:
10684 read_lexical_block_scope (die, cu);
10685 break;
10686 case DW_TAG_call_site:
10687 case DW_TAG_GNU_call_site:
10688 read_call_site_scope (die, cu);
10689 break;
10690 case DW_TAG_class_type:
10691 case DW_TAG_interface_type:
10692 case DW_TAG_structure_type:
10693 case DW_TAG_union_type:
10694 process_structure_scope (die, cu);
10695 break;
10696 case DW_TAG_enumeration_type:
10697 process_enumeration_scope (die, cu);
10698 break;
10699
10700 /* These dies have a type, but processing them does not create
10701 a symbol or recurse to process the children. Therefore we can
10702 read them on-demand through read_type_die. */
10703 case DW_TAG_subroutine_type:
10704 case DW_TAG_set_type:
10705 case DW_TAG_array_type:
10706 case DW_TAG_pointer_type:
10707 case DW_TAG_ptr_to_member_type:
10708 case DW_TAG_reference_type:
10709 case DW_TAG_rvalue_reference_type:
10710 case DW_TAG_string_type:
10711 break;
10712
10713 case DW_TAG_base_type:
10714 case DW_TAG_subrange_type:
10715 case DW_TAG_typedef:
10716 /* Add a typedef symbol for the type definition, if it has a
10717 DW_AT_name. */
10718 new_symbol (die, read_type_die (die, cu), cu);
10719 break;
10720 case DW_TAG_common_block:
10721 read_common_block (die, cu);
10722 break;
10723 case DW_TAG_common_inclusion:
10724 break;
10725 case DW_TAG_namespace:
10726 cu->processing_has_namespace_info = true;
10727 read_namespace (die, cu);
10728 break;
10729 case DW_TAG_module:
10730 cu->processing_has_namespace_info = true;
10731 read_module (die, cu);
10732 break;
10733 case DW_TAG_imported_declaration:
10734 cu->processing_has_namespace_info = true;
10735 if (read_namespace_alias (die, cu))
10736 break;
10737 /* The declaration is not a global namespace alias. */
10738 /* Fall through. */
10739 case DW_TAG_imported_module:
10740 cu->processing_has_namespace_info = true;
10741 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10742 || cu->language != language_fortran))
10743 complaint (_("Tag '%s' has unexpected children"),
10744 dwarf_tag_name (die->tag));
10745 read_import_statement (die, cu);
10746 break;
10747
10748 case DW_TAG_imported_unit:
10749 process_imported_unit_die (die, cu);
10750 break;
10751
10752 case DW_TAG_variable:
10753 read_variable (die, cu);
10754 break;
10755
10756 default:
10757 new_symbol (die, NULL, cu);
10758 break;
10759 }
10760 }
10761 \f
10762 /* DWARF name computation. */
10763
10764 /* A helper function for dwarf2_compute_name which determines whether DIE
10765 needs to have the name of the scope prepended to the name listed in the
10766 die. */
10767
10768 static int
10769 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10770 {
10771 struct attribute *attr;
10772
10773 switch (die->tag)
10774 {
10775 case DW_TAG_namespace:
10776 case DW_TAG_typedef:
10777 case DW_TAG_class_type:
10778 case DW_TAG_interface_type:
10779 case DW_TAG_structure_type:
10780 case DW_TAG_union_type:
10781 case DW_TAG_enumeration_type:
10782 case DW_TAG_enumerator:
10783 case DW_TAG_subprogram:
10784 case DW_TAG_inlined_subroutine:
10785 case DW_TAG_member:
10786 case DW_TAG_imported_declaration:
10787 return 1;
10788
10789 case DW_TAG_variable:
10790 case DW_TAG_constant:
10791 /* We only need to prefix "globally" visible variables. These include
10792 any variable marked with DW_AT_external or any variable that
10793 lives in a namespace. [Variables in anonymous namespaces
10794 require prefixing, but they are not DW_AT_external.] */
10795
10796 if (dwarf2_attr (die, DW_AT_specification, cu))
10797 {
10798 struct dwarf2_cu *spec_cu = cu;
10799
10800 return die_needs_namespace (die_specification (die, &spec_cu),
10801 spec_cu);
10802 }
10803
10804 attr = dwarf2_attr (die, DW_AT_external, cu);
10805 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10806 && die->parent->tag != DW_TAG_module)
10807 return 0;
10808 /* A variable in a lexical block of some kind does not need a
10809 namespace, even though in C++ such variables may be external
10810 and have a mangled name. */
10811 if (die->parent->tag == DW_TAG_lexical_block
10812 || die->parent->tag == DW_TAG_try_block
10813 || die->parent->tag == DW_TAG_catch_block
10814 || die->parent->tag == DW_TAG_subprogram)
10815 return 0;
10816 return 1;
10817
10818 default:
10819 return 0;
10820 }
10821 }
10822
10823 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10824 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10825 defined for the given DIE. */
10826
10827 static struct attribute *
10828 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10829 {
10830 struct attribute *attr;
10831
10832 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10833 if (attr == NULL)
10834 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10835
10836 return attr;
10837 }
10838
10839 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10840 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10841 defined for the given DIE. */
10842
10843 static const char *
10844 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10845 {
10846 const char *linkage_name;
10847
10848 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10849 if (linkage_name == NULL)
10850 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10851
10852 return linkage_name;
10853 }
10854
10855 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10856 compute the physname for the object, which include a method's:
10857 - formal parameters (C++),
10858 - receiver type (Go),
10859
10860 The term "physname" is a bit confusing.
10861 For C++, for example, it is the demangled name.
10862 For Go, for example, it's the mangled name.
10863
10864 For Ada, return the DIE's linkage name rather than the fully qualified
10865 name. PHYSNAME is ignored..
10866
10867 The result is allocated on the objfile_obstack and canonicalized. */
10868
10869 static const char *
10870 dwarf2_compute_name (const char *name,
10871 struct die_info *die, struct dwarf2_cu *cu,
10872 int physname)
10873 {
10874 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10875
10876 if (name == NULL)
10877 name = dwarf2_name (die, cu);
10878
10879 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10880 but otherwise compute it by typename_concat inside GDB.
10881 FIXME: Actually this is not really true, or at least not always true.
10882 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10883 Fortran names because there is no mangling standard. So new_symbol
10884 will set the demangled name to the result of dwarf2_full_name, and it is
10885 the demangled name that GDB uses if it exists. */
10886 if (cu->language == language_ada
10887 || (cu->language == language_fortran && physname))
10888 {
10889 /* For Ada unit, we prefer the linkage name over the name, as
10890 the former contains the exported name, which the user expects
10891 to be able to reference. Ideally, we want the user to be able
10892 to reference this entity using either natural or linkage name,
10893 but we haven't started looking at this enhancement yet. */
10894 const char *linkage_name = dw2_linkage_name (die, cu);
10895
10896 if (linkage_name != NULL)
10897 return linkage_name;
10898 }
10899
10900 /* These are the only languages we know how to qualify names in. */
10901 if (name != NULL
10902 && (cu->language == language_cplus
10903 || cu->language == language_fortran || cu->language == language_d
10904 || cu->language == language_rust))
10905 {
10906 if (die_needs_namespace (die, cu))
10907 {
10908 const char *prefix;
10909 const char *canonical_name = NULL;
10910
10911 string_file buf;
10912
10913 prefix = determine_prefix (die, cu);
10914 if (*prefix != '\0')
10915 {
10916 char *prefixed_name = typename_concat (NULL, prefix, name,
10917 physname, cu);
10918
10919 buf.puts (prefixed_name);
10920 xfree (prefixed_name);
10921 }
10922 else
10923 buf.puts (name);
10924
10925 /* Template parameters may be specified in the DIE's DW_AT_name, or
10926 as children with DW_TAG_template_type_param or
10927 DW_TAG_value_type_param. If the latter, add them to the name
10928 here. If the name already has template parameters, then
10929 skip this step; some versions of GCC emit both, and
10930 it is more efficient to use the pre-computed name.
10931
10932 Something to keep in mind about this process: it is very
10933 unlikely, or in some cases downright impossible, to produce
10934 something that will match the mangled name of a function.
10935 If the definition of the function has the same debug info,
10936 we should be able to match up with it anyway. But fallbacks
10937 using the minimal symbol, for instance to find a method
10938 implemented in a stripped copy of libstdc++, will not work.
10939 If we do not have debug info for the definition, we will have to
10940 match them up some other way.
10941
10942 When we do name matching there is a related problem with function
10943 templates; two instantiated function templates are allowed to
10944 differ only by their return types, which we do not add here. */
10945
10946 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10947 {
10948 struct attribute *attr;
10949 struct die_info *child;
10950 int first = 1;
10951
10952 die->building_fullname = 1;
10953
10954 for (child = die->child; child != NULL; child = child->sibling)
10955 {
10956 struct type *type;
10957 LONGEST value;
10958 const gdb_byte *bytes;
10959 struct dwarf2_locexpr_baton *baton;
10960 struct value *v;
10961
10962 if (child->tag != DW_TAG_template_type_param
10963 && child->tag != DW_TAG_template_value_param)
10964 continue;
10965
10966 if (first)
10967 {
10968 buf.puts ("<");
10969 first = 0;
10970 }
10971 else
10972 buf.puts (", ");
10973
10974 attr = dwarf2_attr (child, DW_AT_type, cu);
10975 if (attr == NULL)
10976 {
10977 complaint (_("template parameter missing DW_AT_type"));
10978 buf.puts ("UNKNOWN_TYPE");
10979 continue;
10980 }
10981 type = die_type (child, cu);
10982
10983 if (child->tag == DW_TAG_template_type_param)
10984 {
10985 c_print_type (type, "", &buf, -1, 0, cu->language,
10986 &type_print_raw_options);
10987 continue;
10988 }
10989
10990 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10991 if (attr == NULL)
10992 {
10993 complaint (_("template parameter missing "
10994 "DW_AT_const_value"));
10995 buf.puts ("UNKNOWN_VALUE");
10996 continue;
10997 }
10998
10999 dwarf2_const_value_attr (attr, type, name,
11000 &cu->comp_unit_obstack, cu,
11001 &value, &bytes, &baton);
11002
11003 if (TYPE_NOSIGN (type))
11004 /* GDB prints characters as NUMBER 'CHAR'. If that's
11005 changed, this can use value_print instead. */
11006 c_printchar (value, type, &buf);
11007 else
11008 {
11009 struct value_print_options opts;
11010
11011 if (baton != NULL)
11012 v = dwarf2_evaluate_loc_desc (type, NULL,
11013 baton->data,
11014 baton->size,
11015 baton->per_cu);
11016 else if (bytes != NULL)
11017 {
11018 v = allocate_value (type);
11019 memcpy (value_contents_writeable (v), bytes,
11020 TYPE_LENGTH (type));
11021 }
11022 else
11023 v = value_from_longest (type, value);
11024
11025 /* Specify decimal so that we do not depend on
11026 the radix. */
11027 get_formatted_print_options (&opts, 'd');
11028 opts.raw = 1;
11029 value_print (v, &buf, &opts);
11030 release_value (v);
11031 }
11032 }
11033
11034 die->building_fullname = 0;
11035
11036 if (!first)
11037 {
11038 /* Close the argument list, with a space if necessary
11039 (nested templates). */
11040 if (!buf.empty () && buf.string ().back () == '>')
11041 buf.puts (" >");
11042 else
11043 buf.puts (">");
11044 }
11045 }
11046
11047 /* For C++ methods, append formal parameter type
11048 information, if PHYSNAME. */
11049
11050 if (physname && die->tag == DW_TAG_subprogram
11051 && cu->language == language_cplus)
11052 {
11053 struct type *type = read_type_die (die, cu);
11054
11055 c_type_print_args (type, &buf, 1, cu->language,
11056 &type_print_raw_options);
11057
11058 if (cu->language == language_cplus)
11059 {
11060 /* Assume that an artificial first parameter is
11061 "this", but do not crash if it is not. RealView
11062 marks unnamed (and thus unused) parameters as
11063 artificial; there is no way to differentiate
11064 the two cases. */
11065 if (TYPE_NFIELDS (type) > 0
11066 && TYPE_FIELD_ARTIFICIAL (type, 0)
11067 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11068 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11069 0))))
11070 buf.puts (" const");
11071 }
11072 }
11073
11074 const std::string &intermediate_name = buf.string ();
11075
11076 if (cu->language == language_cplus)
11077 canonical_name
11078 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11079 &objfile->per_bfd->storage_obstack);
11080
11081 /* If we only computed INTERMEDIATE_NAME, or if
11082 INTERMEDIATE_NAME is already canonical, then we need to
11083 copy it to the appropriate obstack. */
11084 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11085 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11086 intermediate_name);
11087 else
11088 name = canonical_name;
11089 }
11090 }
11091
11092 return name;
11093 }
11094
11095 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11096 If scope qualifiers are appropriate they will be added. The result
11097 will be allocated on the storage_obstack, or NULL if the DIE does
11098 not have a name. NAME may either be from a previous call to
11099 dwarf2_name or NULL.
11100
11101 The output string will be canonicalized (if C++). */
11102
11103 static const char *
11104 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11105 {
11106 return dwarf2_compute_name (name, die, cu, 0);
11107 }
11108
11109 /* Construct a physname for the given DIE in CU. NAME may either be
11110 from a previous call to dwarf2_name or NULL. The result will be
11111 allocated on the objfile_objstack or NULL if the DIE does not have a
11112 name.
11113
11114 The output string will be canonicalized (if C++). */
11115
11116 static const char *
11117 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11118 {
11119 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11120 const char *retval, *mangled = NULL, *canon = NULL;
11121 int need_copy = 1;
11122
11123 /* In this case dwarf2_compute_name is just a shortcut not building anything
11124 on its own. */
11125 if (!die_needs_namespace (die, cu))
11126 return dwarf2_compute_name (name, die, cu, 1);
11127
11128 mangled = dw2_linkage_name (die, cu);
11129
11130 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11131 See https://github.com/rust-lang/rust/issues/32925. */
11132 if (cu->language == language_rust && mangled != NULL
11133 && strchr (mangled, '{') != NULL)
11134 mangled = NULL;
11135
11136 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11137 has computed. */
11138 gdb::unique_xmalloc_ptr<char> demangled;
11139 if (mangled != NULL)
11140 {
11141
11142 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11143 {
11144 /* Do nothing (do not demangle the symbol name). */
11145 }
11146 else if (cu->language == language_go)
11147 {
11148 /* This is a lie, but we already lie to the caller new_symbol.
11149 new_symbol assumes we return the mangled name.
11150 This just undoes that lie until things are cleaned up. */
11151 }
11152 else
11153 {
11154 /* Use DMGL_RET_DROP for C++ template functions to suppress
11155 their return type. It is easier for GDB users to search
11156 for such functions as `name(params)' than `long name(params)'.
11157 In such case the minimal symbol names do not match the full
11158 symbol names but for template functions there is never a need
11159 to look up their definition from their declaration so
11160 the only disadvantage remains the minimal symbol variant
11161 `long name(params)' does not have the proper inferior type. */
11162 demangled.reset (gdb_demangle (mangled,
11163 (DMGL_PARAMS | DMGL_ANSI
11164 | DMGL_RET_DROP)));
11165 }
11166 if (demangled)
11167 canon = demangled.get ();
11168 else
11169 {
11170 canon = mangled;
11171 need_copy = 0;
11172 }
11173 }
11174
11175 if (canon == NULL || check_physname)
11176 {
11177 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11178
11179 if (canon != NULL && strcmp (physname, canon) != 0)
11180 {
11181 /* It may not mean a bug in GDB. The compiler could also
11182 compute DW_AT_linkage_name incorrectly. But in such case
11183 GDB would need to be bug-to-bug compatible. */
11184
11185 complaint (_("Computed physname <%s> does not match demangled <%s> "
11186 "(from linkage <%s>) - DIE at %s [in module %s]"),
11187 physname, canon, mangled, sect_offset_str (die->sect_off),
11188 objfile_name (objfile));
11189
11190 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11191 is available here - over computed PHYSNAME. It is safer
11192 against both buggy GDB and buggy compilers. */
11193
11194 retval = canon;
11195 }
11196 else
11197 {
11198 retval = physname;
11199 need_copy = 0;
11200 }
11201 }
11202 else
11203 retval = canon;
11204
11205 if (need_copy)
11206 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11207
11208 return retval;
11209 }
11210
11211 /* Inspect DIE in CU for a namespace alias. If one exists, record
11212 a new symbol for it.
11213
11214 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11215
11216 static int
11217 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11218 {
11219 struct attribute *attr;
11220
11221 /* If the die does not have a name, this is not a namespace
11222 alias. */
11223 attr = dwarf2_attr (die, DW_AT_name, cu);
11224 if (attr != NULL)
11225 {
11226 int num;
11227 struct die_info *d = die;
11228 struct dwarf2_cu *imported_cu = cu;
11229
11230 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11231 keep inspecting DIEs until we hit the underlying import. */
11232 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11233 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11234 {
11235 attr = dwarf2_attr (d, DW_AT_import, cu);
11236 if (attr == NULL)
11237 break;
11238
11239 d = follow_die_ref (d, attr, &imported_cu);
11240 if (d->tag != DW_TAG_imported_declaration)
11241 break;
11242 }
11243
11244 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11245 {
11246 complaint (_("DIE at %s has too many recursively imported "
11247 "declarations"), sect_offset_str (d->sect_off));
11248 return 0;
11249 }
11250
11251 if (attr != NULL)
11252 {
11253 struct type *type;
11254 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11255
11256 type = get_die_type_at_offset (sect_off, cu->per_cu);
11257 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11258 {
11259 /* This declaration is a global namespace alias. Add
11260 a symbol for it whose type is the aliased namespace. */
11261 new_symbol (die, type, cu);
11262 return 1;
11263 }
11264 }
11265 }
11266
11267 return 0;
11268 }
11269
11270 /* Return the using directives repository (global or local?) to use in the
11271 current context for CU.
11272
11273 For Ada, imported declarations can materialize renamings, which *may* be
11274 global. However it is impossible (for now?) in DWARF to distinguish
11275 "external" imported declarations and "static" ones. As all imported
11276 declarations seem to be static in all other languages, make them all CU-wide
11277 global only in Ada. */
11278
11279 static struct using_direct **
11280 using_directives (struct dwarf2_cu *cu)
11281 {
11282 if (cu->language == language_ada
11283 && cu->get_builder ()->outermost_context_p ())
11284 return cu->get_builder ()->get_global_using_directives ();
11285 else
11286 return cu->get_builder ()->get_local_using_directives ();
11287 }
11288
11289 /* Read the import statement specified by the given die and record it. */
11290
11291 static void
11292 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11293 {
11294 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11295 struct attribute *import_attr;
11296 struct die_info *imported_die, *child_die;
11297 struct dwarf2_cu *imported_cu;
11298 const char *imported_name;
11299 const char *imported_name_prefix;
11300 const char *canonical_name;
11301 const char *import_alias;
11302 const char *imported_declaration = NULL;
11303 const char *import_prefix;
11304 std::vector<const char *> excludes;
11305
11306 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11307 if (import_attr == NULL)
11308 {
11309 complaint (_("Tag '%s' has no DW_AT_import"),
11310 dwarf_tag_name (die->tag));
11311 return;
11312 }
11313
11314 imported_cu = cu;
11315 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11316 imported_name = dwarf2_name (imported_die, imported_cu);
11317 if (imported_name == NULL)
11318 {
11319 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11320
11321 The import in the following code:
11322 namespace A
11323 {
11324 typedef int B;
11325 }
11326
11327 int main ()
11328 {
11329 using A::B;
11330 B b;
11331 return b;
11332 }
11333
11334 ...
11335 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11336 <52> DW_AT_decl_file : 1
11337 <53> DW_AT_decl_line : 6
11338 <54> DW_AT_import : <0x75>
11339 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11340 <59> DW_AT_name : B
11341 <5b> DW_AT_decl_file : 1
11342 <5c> DW_AT_decl_line : 2
11343 <5d> DW_AT_type : <0x6e>
11344 ...
11345 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11346 <76> DW_AT_byte_size : 4
11347 <77> DW_AT_encoding : 5 (signed)
11348
11349 imports the wrong die ( 0x75 instead of 0x58 ).
11350 This case will be ignored until the gcc bug is fixed. */
11351 return;
11352 }
11353
11354 /* Figure out the local name after import. */
11355 import_alias = dwarf2_name (die, cu);
11356
11357 /* Figure out where the statement is being imported to. */
11358 import_prefix = determine_prefix (die, cu);
11359
11360 /* Figure out what the scope of the imported die is and prepend it
11361 to the name of the imported die. */
11362 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11363
11364 if (imported_die->tag != DW_TAG_namespace
11365 && imported_die->tag != DW_TAG_module)
11366 {
11367 imported_declaration = imported_name;
11368 canonical_name = imported_name_prefix;
11369 }
11370 else if (strlen (imported_name_prefix) > 0)
11371 canonical_name = obconcat (&objfile->objfile_obstack,
11372 imported_name_prefix,
11373 (cu->language == language_d ? "." : "::"),
11374 imported_name, (char *) NULL);
11375 else
11376 canonical_name = imported_name;
11377
11378 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11379 for (child_die = die->child; child_die && child_die->tag;
11380 child_die = sibling_die (child_die))
11381 {
11382 /* DWARF-4: A Fortran use statement with a “rename list” may be
11383 represented by an imported module entry with an import attribute
11384 referring to the module and owned entries corresponding to those
11385 entities that are renamed as part of being imported. */
11386
11387 if (child_die->tag != DW_TAG_imported_declaration)
11388 {
11389 complaint (_("child DW_TAG_imported_declaration expected "
11390 "- DIE at %s [in module %s]"),
11391 sect_offset_str (child_die->sect_off),
11392 objfile_name (objfile));
11393 continue;
11394 }
11395
11396 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11397 if (import_attr == NULL)
11398 {
11399 complaint (_("Tag '%s' has no DW_AT_import"),
11400 dwarf_tag_name (child_die->tag));
11401 continue;
11402 }
11403
11404 imported_cu = cu;
11405 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11406 &imported_cu);
11407 imported_name = dwarf2_name (imported_die, imported_cu);
11408 if (imported_name == NULL)
11409 {
11410 complaint (_("child DW_TAG_imported_declaration has unknown "
11411 "imported name - DIE at %s [in module %s]"),
11412 sect_offset_str (child_die->sect_off),
11413 objfile_name (objfile));
11414 continue;
11415 }
11416
11417 excludes.push_back (imported_name);
11418
11419 process_die (child_die, cu);
11420 }
11421
11422 add_using_directive (using_directives (cu),
11423 import_prefix,
11424 canonical_name,
11425 import_alias,
11426 imported_declaration,
11427 excludes,
11428 0,
11429 &objfile->objfile_obstack);
11430 }
11431
11432 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11433 types, but gives them a size of zero. Starting with version 14,
11434 ICC is compatible with GCC. */
11435
11436 static bool
11437 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11438 {
11439 if (!cu->checked_producer)
11440 check_producer (cu);
11441
11442 return cu->producer_is_icc_lt_14;
11443 }
11444
11445 /* ICC generates a DW_AT_type for C void functions. This was observed on
11446 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11447 which says that void functions should not have a DW_AT_type. */
11448
11449 static bool
11450 producer_is_icc (struct dwarf2_cu *cu)
11451 {
11452 if (!cu->checked_producer)
11453 check_producer (cu);
11454
11455 return cu->producer_is_icc;
11456 }
11457
11458 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11459 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11460 this, it was first present in GCC release 4.3.0. */
11461
11462 static bool
11463 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11464 {
11465 if (!cu->checked_producer)
11466 check_producer (cu);
11467
11468 return cu->producer_is_gcc_lt_4_3;
11469 }
11470
11471 static file_and_directory
11472 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11473 {
11474 file_and_directory res;
11475
11476 /* Find the filename. Do not use dwarf2_name here, since the filename
11477 is not a source language identifier. */
11478 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11479 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11480
11481 if (res.comp_dir == NULL
11482 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11483 && IS_ABSOLUTE_PATH (res.name))
11484 {
11485 res.comp_dir_storage = ldirname (res.name);
11486 if (!res.comp_dir_storage.empty ())
11487 res.comp_dir = res.comp_dir_storage.c_str ();
11488 }
11489 if (res.comp_dir != NULL)
11490 {
11491 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11492 directory, get rid of it. */
11493 const char *cp = strchr (res.comp_dir, ':');
11494
11495 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11496 res.comp_dir = cp + 1;
11497 }
11498
11499 if (res.name == NULL)
11500 res.name = "<unknown>";
11501
11502 return res;
11503 }
11504
11505 /* Handle DW_AT_stmt_list for a compilation unit.
11506 DIE is the DW_TAG_compile_unit die for CU.
11507 COMP_DIR is the compilation directory. LOWPC is passed to
11508 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11509
11510 static void
11511 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11512 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11513 {
11514 struct dwarf2_per_objfile *dwarf2_per_objfile
11515 = cu->per_cu->dwarf2_per_objfile;
11516 struct objfile *objfile = dwarf2_per_objfile->objfile;
11517 struct attribute *attr;
11518 struct line_header line_header_local;
11519 hashval_t line_header_local_hash;
11520 void **slot;
11521 int decode_mapping;
11522
11523 gdb_assert (! cu->per_cu->is_debug_types);
11524
11525 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11526 if (attr == NULL)
11527 return;
11528
11529 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11530
11531 /* The line header hash table is only created if needed (it exists to
11532 prevent redundant reading of the line table for partial_units).
11533 If we're given a partial_unit, we'll need it. If we're given a
11534 compile_unit, then use the line header hash table if it's already
11535 created, but don't create one just yet. */
11536
11537 if (dwarf2_per_objfile->line_header_hash == NULL
11538 && die->tag == DW_TAG_partial_unit)
11539 {
11540 dwarf2_per_objfile->line_header_hash
11541 = htab_create_alloc_ex (127, line_header_hash_voidp,
11542 line_header_eq_voidp,
11543 free_line_header_voidp,
11544 &objfile->objfile_obstack,
11545 hashtab_obstack_allocate,
11546 dummy_obstack_deallocate);
11547 }
11548
11549 line_header_local.sect_off = line_offset;
11550 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11551 line_header_local_hash = line_header_hash (&line_header_local);
11552 if (dwarf2_per_objfile->line_header_hash != NULL)
11553 {
11554 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11555 &line_header_local,
11556 line_header_local_hash, NO_INSERT);
11557
11558 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11559 is not present in *SLOT (since if there is something in *SLOT then
11560 it will be for a partial_unit). */
11561 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11562 {
11563 gdb_assert (*slot != NULL);
11564 cu->line_header = (struct line_header *) *slot;
11565 return;
11566 }
11567 }
11568
11569 /* dwarf_decode_line_header does not yet provide sufficient information.
11570 We always have to call also dwarf_decode_lines for it. */
11571 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11572 if (lh == NULL)
11573 return;
11574
11575 cu->line_header = lh.release ();
11576 cu->line_header_die_owner = die;
11577
11578 if (dwarf2_per_objfile->line_header_hash == NULL)
11579 slot = NULL;
11580 else
11581 {
11582 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11583 &line_header_local,
11584 line_header_local_hash, INSERT);
11585 gdb_assert (slot != NULL);
11586 }
11587 if (slot != NULL && *slot == NULL)
11588 {
11589 /* This newly decoded line number information unit will be owned
11590 by line_header_hash hash table. */
11591 *slot = cu->line_header;
11592 cu->line_header_die_owner = NULL;
11593 }
11594 else
11595 {
11596 /* We cannot free any current entry in (*slot) as that struct line_header
11597 may be already used by multiple CUs. Create only temporary decoded
11598 line_header for this CU - it may happen at most once for each line
11599 number information unit. And if we're not using line_header_hash
11600 then this is what we want as well. */
11601 gdb_assert (die->tag != DW_TAG_partial_unit);
11602 }
11603 decode_mapping = (die->tag != DW_TAG_partial_unit);
11604 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11605 decode_mapping);
11606
11607 }
11608
11609 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11610
11611 static void
11612 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11613 {
11614 struct dwarf2_per_objfile *dwarf2_per_objfile
11615 = cu->per_cu->dwarf2_per_objfile;
11616 struct objfile *objfile = dwarf2_per_objfile->objfile;
11617 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11618 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11619 CORE_ADDR highpc = ((CORE_ADDR) 0);
11620 struct attribute *attr;
11621 struct die_info *child_die;
11622 CORE_ADDR baseaddr;
11623
11624 prepare_one_comp_unit (cu, die, cu->language);
11625 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11626
11627 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11628
11629 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11630 from finish_block. */
11631 if (lowpc == ((CORE_ADDR) -1))
11632 lowpc = highpc;
11633 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11634
11635 file_and_directory fnd = find_file_and_directory (die, cu);
11636
11637 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11638 standardised yet. As a workaround for the language detection we fall
11639 back to the DW_AT_producer string. */
11640 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11641 cu->language = language_opencl;
11642
11643 /* Similar hack for Go. */
11644 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11645 set_cu_language (DW_LANG_Go, cu);
11646
11647 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11648
11649 /* Decode line number information if present. We do this before
11650 processing child DIEs, so that the line header table is available
11651 for DW_AT_decl_file. */
11652 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11653
11654 /* Process all dies in compilation unit. */
11655 if (die->child != NULL)
11656 {
11657 child_die = die->child;
11658 while (child_die && child_die->tag)
11659 {
11660 process_die (child_die, cu);
11661 child_die = sibling_die (child_die);
11662 }
11663 }
11664
11665 /* Decode macro information, if present. Dwarf 2 macro information
11666 refers to information in the line number info statement program
11667 header, so we can only read it if we've read the header
11668 successfully. */
11669 attr = dwarf2_attr (die, DW_AT_macros, cu);
11670 if (attr == NULL)
11671 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11672 if (attr && cu->line_header)
11673 {
11674 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11675 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11676
11677 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11678 }
11679 else
11680 {
11681 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11682 if (attr && cu->line_header)
11683 {
11684 unsigned int macro_offset = DW_UNSND (attr);
11685
11686 dwarf_decode_macros (cu, macro_offset, 0);
11687 }
11688 }
11689 }
11690
11691 void
11692 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11693 {
11694 struct type_unit_group *tu_group;
11695 int first_time;
11696 struct attribute *attr;
11697 unsigned int i;
11698 struct signatured_type *sig_type;
11699
11700 gdb_assert (per_cu->is_debug_types);
11701 sig_type = (struct signatured_type *) per_cu;
11702
11703 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11704
11705 /* If we're using .gdb_index (includes -readnow) then
11706 per_cu->type_unit_group may not have been set up yet. */
11707 if (sig_type->type_unit_group == NULL)
11708 sig_type->type_unit_group = get_type_unit_group (this, attr);
11709 tu_group = sig_type->type_unit_group;
11710
11711 /* If we've already processed this stmt_list there's no real need to
11712 do it again, we could fake it and just recreate the part we need
11713 (file name,index -> symtab mapping). If data shows this optimization
11714 is useful we can do it then. */
11715 first_time = tu_group->compunit_symtab == NULL;
11716
11717 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11718 debug info. */
11719 line_header_up lh;
11720 if (attr != NULL)
11721 {
11722 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11723 lh = dwarf_decode_line_header (line_offset, this);
11724 }
11725 if (lh == NULL)
11726 {
11727 if (first_time)
11728 start_symtab ("", NULL, 0);
11729 else
11730 {
11731 gdb_assert (tu_group->symtabs == NULL);
11732 gdb_assert (m_builder == nullptr);
11733 struct compunit_symtab *cust = tu_group->compunit_symtab;
11734 m_builder.reset (new struct buildsym_compunit
11735 (COMPUNIT_OBJFILE (cust), "",
11736 COMPUNIT_DIRNAME (cust),
11737 compunit_language (cust),
11738 0, cust));
11739 }
11740 return;
11741 }
11742
11743 line_header = lh.release ();
11744 line_header_die_owner = die;
11745
11746 if (first_time)
11747 {
11748 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11749
11750 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11751 still initializing it, and our caller (a few levels up)
11752 process_full_type_unit still needs to know if this is the first
11753 time. */
11754
11755 tu_group->num_symtabs = line_header->file_names_size ();
11756 tu_group->symtabs = XNEWVEC (struct symtab *,
11757 line_header->file_names_size ());
11758
11759 auto &file_names = line_header->file_names ();
11760 for (i = 0; i < file_names.size (); ++i)
11761 {
11762 file_entry &fe = file_names[i];
11763 dwarf2_start_subfile (this, fe.name,
11764 fe.include_dir (line_header));
11765 buildsym_compunit *b = get_builder ();
11766 if (b->get_current_subfile ()->symtab == NULL)
11767 {
11768 /* NOTE: start_subfile will recognize when it's been
11769 passed a file it has already seen. So we can't
11770 assume there's a simple mapping from
11771 cu->line_header->file_names to subfiles, plus
11772 cu->line_header->file_names may contain dups. */
11773 b->get_current_subfile ()->symtab
11774 = allocate_symtab (cust, b->get_current_subfile ()->name);
11775 }
11776
11777 fe.symtab = b->get_current_subfile ()->symtab;
11778 tu_group->symtabs[i] = fe.symtab;
11779 }
11780 }
11781 else
11782 {
11783 gdb_assert (m_builder == nullptr);
11784 struct compunit_symtab *cust = tu_group->compunit_symtab;
11785 m_builder.reset (new struct buildsym_compunit
11786 (COMPUNIT_OBJFILE (cust), "",
11787 COMPUNIT_DIRNAME (cust),
11788 compunit_language (cust),
11789 0, cust));
11790
11791 auto &file_names = line_header->file_names ();
11792 for (i = 0; i < file_names.size (); ++i)
11793 {
11794 file_entry &fe = file_names[i];
11795 fe.symtab = tu_group->symtabs[i];
11796 }
11797 }
11798
11799 /* The main symtab is allocated last. Type units don't have DW_AT_name
11800 so they don't have a "real" (so to speak) symtab anyway.
11801 There is later code that will assign the main symtab to all symbols
11802 that don't have one. We need to handle the case of a symbol with a
11803 missing symtab (DW_AT_decl_file) anyway. */
11804 }
11805
11806 /* Process DW_TAG_type_unit.
11807 For TUs we want to skip the first top level sibling if it's not the
11808 actual type being defined by this TU. In this case the first top
11809 level sibling is there to provide context only. */
11810
11811 static void
11812 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11813 {
11814 struct die_info *child_die;
11815
11816 prepare_one_comp_unit (cu, die, language_minimal);
11817
11818 /* Initialize (or reinitialize) the machinery for building symtabs.
11819 We do this before processing child DIEs, so that the line header table
11820 is available for DW_AT_decl_file. */
11821 cu->setup_type_unit_groups (die);
11822
11823 if (die->child != NULL)
11824 {
11825 child_die = die->child;
11826 while (child_die && child_die->tag)
11827 {
11828 process_die (child_die, cu);
11829 child_die = sibling_die (child_die);
11830 }
11831 }
11832 }
11833 \f
11834 /* DWO/DWP files.
11835
11836 http://gcc.gnu.org/wiki/DebugFission
11837 http://gcc.gnu.org/wiki/DebugFissionDWP
11838
11839 To simplify handling of both DWO files ("object" files with the DWARF info)
11840 and DWP files (a file with the DWOs packaged up into one file), we treat
11841 DWP files as having a collection of virtual DWO files. */
11842
11843 static hashval_t
11844 hash_dwo_file (const void *item)
11845 {
11846 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11847 hashval_t hash;
11848
11849 hash = htab_hash_string (dwo_file->dwo_name);
11850 if (dwo_file->comp_dir != NULL)
11851 hash += htab_hash_string (dwo_file->comp_dir);
11852 return hash;
11853 }
11854
11855 static int
11856 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11857 {
11858 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11859 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11860
11861 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11862 return 0;
11863 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11864 return lhs->comp_dir == rhs->comp_dir;
11865 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11866 }
11867
11868 /* Allocate a hash table for DWO files. */
11869
11870 static htab_up
11871 allocate_dwo_file_hash_table (struct objfile *objfile)
11872 {
11873 auto delete_dwo_file = [] (void *item)
11874 {
11875 struct dwo_file *dwo_file = (struct dwo_file *) item;
11876
11877 delete dwo_file;
11878 };
11879
11880 return htab_up (htab_create_alloc_ex (41,
11881 hash_dwo_file,
11882 eq_dwo_file,
11883 delete_dwo_file,
11884 &objfile->objfile_obstack,
11885 hashtab_obstack_allocate,
11886 dummy_obstack_deallocate));
11887 }
11888
11889 /* Lookup DWO file DWO_NAME. */
11890
11891 static void **
11892 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11893 const char *dwo_name,
11894 const char *comp_dir)
11895 {
11896 struct dwo_file find_entry;
11897 void **slot;
11898
11899 if (dwarf2_per_objfile->dwo_files == NULL)
11900 dwarf2_per_objfile->dwo_files
11901 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11902
11903 find_entry.dwo_name = dwo_name;
11904 find_entry.comp_dir = comp_dir;
11905 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11906 INSERT);
11907
11908 return slot;
11909 }
11910
11911 static hashval_t
11912 hash_dwo_unit (const void *item)
11913 {
11914 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11915
11916 /* This drops the top 32 bits of the id, but is ok for a hash. */
11917 return dwo_unit->signature;
11918 }
11919
11920 static int
11921 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11922 {
11923 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11924 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11925
11926 /* The signature is assumed to be unique within the DWO file.
11927 So while object file CU dwo_id's always have the value zero,
11928 that's OK, assuming each object file DWO file has only one CU,
11929 and that's the rule for now. */
11930 return lhs->signature == rhs->signature;
11931 }
11932
11933 /* Allocate a hash table for DWO CUs,TUs.
11934 There is one of these tables for each of CUs,TUs for each DWO file. */
11935
11936 static htab_t
11937 allocate_dwo_unit_table (struct objfile *objfile)
11938 {
11939 /* Start out with a pretty small number.
11940 Generally DWO files contain only one CU and maybe some TUs. */
11941 return htab_create_alloc_ex (3,
11942 hash_dwo_unit,
11943 eq_dwo_unit,
11944 NULL,
11945 &objfile->objfile_obstack,
11946 hashtab_obstack_allocate,
11947 dummy_obstack_deallocate);
11948 }
11949
11950 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11951
11952 struct create_dwo_cu_data
11953 {
11954 struct dwo_file *dwo_file;
11955 struct dwo_unit dwo_unit;
11956 };
11957
11958 /* die_reader_func for create_dwo_cu. */
11959
11960 static void
11961 create_dwo_cu_reader (const struct die_reader_specs *reader,
11962 const gdb_byte *info_ptr,
11963 struct die_info *comp_unit_die,
11964 int has_children,
11965 void *datap)
11966 {
11967 struct dwarf2_cu *cu = reader->cu;
11968 sect_offset sect_off = cu->per_cu->sect_off;
11969 struct dwarf2_section_info *section = cu->per_cu->section;
11970 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11971 struct dwo_file *dwo_file = data->dwo_file;
11972 struct dwo_unit *dwo_unit = &data->dwo_unit;
11973
11974 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11975 if (!signature.has_value ())
11976 {
11977 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11978 " its dwo_id [in module %s]"),
11979 sect_offset_str (sect_off), dwo_file->dwo_name);
11980 return;
11981 }
11982
11983 dwo_unit->dwo_file = dwo_file;
11984 dwo_unit->signature = *signature;
11985 dwo_unit->section = section;
11986 dwo_unit->sect_off = sect_off;
11987 dwo_unit->length = cu->per_cu->length;
11988
11989 if (dwarf_read_debug)
11990 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11991 sect_offset_str (sect_off),
11992 hex_string (dwo_unit->signature));
11993 }
11994
11995 /* Create the dwo_units for the CUs in a DWO_FILE.
11996 Note: This function processes DWO files only, not DWP files. */
11997
11998 static void
11999 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12000 struct dwo_file &dwo_file, dwarf2_section_info &section,
12001 htab_t &cus_htab)
12002 {
12003 struct objfile *objfile = dwarf2_per_objfile->objfile;
12004 const gdb_byte *info_ptr, *end_ptr;
12005
12006 dwarf2_read_section (objfile, &section);
12007 info_ptr = section.buffer;
12008
12009 if (info_ptr == NULL)
12010 return;
12011
12012 if (dwarf_read_debug)
12013 {
12014 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12015 get_section_name (&section),
12016 get_section_file_name (&section));
12017 }
12018
12019 end_ptr = info_ptr + section.size;
12020 while (info_ptr < end_ptr)
12021 {
12022 struct dwarf2_per_cu_data per_cu;
12023 struct create_dwo_cu_data create_dwo_cu_data;
12024 struct dwo_unit *dwo_unit;
12025 void **slot;
12026 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12027
12028 memset (&create_dwo_cu_data.dwo_unit, 0,
12029 sizeof (create_dwo_cu_data.dwo_unit));
12030 memset (&per_cu, 0, sizeof (per_cu));
12031 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12032 per_cu.is_debug_types = 0;
12033 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12034 per_cu.section = &section;
12035 create_dwo_cu_data.dwo_file = &dwo_file;
12036
12037 init_cutu_and_read_dies_no_follow (
12038 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12039 info_ptr += per_cu.length;
12040
12041 // If the unit could not be parsed, skip it.
12042 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12043 continue;
12044
12045 if (cus_htab == NULL)
12046 cus_htab = allocate_dwo_unit_table (objfile);
12047
12048 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12049 *dwo_unit = create_dwo_cu_data.dwo_unit;
12050 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12051 gdb_assert (slot != NULL);
12052 if (*slot != NULL)
12053 {
12054 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12055 sect_offset dup_sect_off = dup_cu->sect_off;
12056
12057 complaint (_("debug cu entry at offset %s is duplicate to"
12058 " the entry at offset %s, signature %s"),
12059 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12060 hex_string (dwo_unit->signature));
12061 }
12062 *slot = (void *)dwo_unit;
12063 }
12064 }
12065
12066 /* DWP file .debug_{cu,tu}_index section format:
12067 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12068
12069 DWP Version 1:
12070
12071 Both index sections have the same format, and serve to map a 64-bit
12072 signature to a set of section numbers. Each section begins with a header,
12073 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12074 indexes, and a pool of 32-bit section numbers. The index sections will be
12075 aligned at 8-byte boundaries in the file.
12076
12077 The index section header consists of:
12078
12079 V, 32 bit version number
12080 -, 32 bits unused
12081 N, 32 bit number of compilation units or type units in the index
12082 M, 32 bit number of slots in the hash table
12083
12084 Numbers are recorded using the byte order of the application binary.
12085
12086 The hash table begins at offset 16 in the section, and consists of an array
12087 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12088 order of the application binary). Unused slots in the hash table are 0.
12089 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12090
12091 The parallel table begins immediately after the hash table
12092 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12093 array of 32-bit indexes (using the byte order of the application binary),
12094 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12095 table contains a 32-bit index into the pool of section numbers. For unused
12096 hash table slots, the corresponding entry in the parallel table will be 0.
12097
12098 The pool of section numbers begins immediately following the hash table
12099 (at offset 16 + 12 * M from the beginning of the section). The pool of
12100 section numbers consists of an array of 32-bit words (using the byte order
12101 of the application binary). Each item in the array is indexed starting
12102 from 0. The hash table entry provides the index of the first section
12103 number in the set. Additional section numbers in the set follow, and the
12104 set is terminated by a 0 entry (section number 0 is not used in ELF).
12105
12106 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12107 section must be the first entry in the set, and the .debug_abbrev.dwo must
12108 be the second entry. Other members of the set may follow in any order.
12109
12110 ---
12111
12112 DWP Version 2:
12113
12114 DWP Version 2 combines all the .debug_info, etc. sections into one,
12115 and the entries in the index tables are now offsets into these sections.
12116 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12117 section.
12118
12119 Index Section Contents:
12120 Header
12121 Hash Table of Signatures dwp_hash_table.hash_table
12122 Parallel Table of Indices dwp_hash_table.unit_table
12123 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12124 Table of Section Sizes dwp_hash_table.v2.sizes
12125
12126 The index section header consists of:
12127
12128 V, 32 bit version number
12129 L, 32 bit number of columns in the table of section offsets
12130 N, 32 bit number of compilation units or type units in the index
12131 M, 32 bit number of slots in the hash table
12132
12133 Numbers are recorded using the byte order of the application binary.
12134
12135 The hash table has the same format as version 1.
12136 The parallel table of indices has the same format as version 1,
12137 except that the entries are origin-1 indices into the table of sections
12138 offsets and the table of section sizes.
12139
12140 The table of offsets begins immediately following the parallel table
12141 (at offset 16 + 12 * M from the beginning of the section). The table is
12142 a two-dimensional array of 32-bit words (using the byte order of the
12143 application binary), with L columns and N+1 rows, in row-major order.
12144 Each row in the array is indexed starting from 0. The first row provides
12145 a key to the remaining rows: each column in this row provides an identifier
12146 for a debug section, and the offsets in the same column of subsequent rows
12147 refer to that section. The section identifiers are:
12148
12149 DW_SECT_INFO 1 .debug_info.dwo
12150 DW_SECT_TYPES 2 .debug_types.dwo
12151 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12152 DW_SECT_LINE 4 .debug_line.dwo
12153 DW_SECT_LOC 5 .debug_loc.dwo
12154 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12155 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12156 DW_SECT_MACRO 8 .debug_macro.dwo
12157
12158 The offsets provided by the CU and TU index sections are the base offsets
12159 for the contributions made by each CU or TU to the corresponding section
12160 in the package file. Each CU and TU header contains an abbrev_offset
12161 field, used to find the abbreviations table for that CU or TU within the
12162 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12163 be interpreted as relative to the base offset given in the index section.
12164 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12165 should be interpreted as relative to the base offset for .debug_line.dwo,
12166 and offsets into other debug sections obtained from DWARF attributes should
12167 also be interpreted as relative to the corresponding base offset.
12168
12169 The table of sizes begins immediately following the table of offsets.
12170 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12171 with L columns and N rows, in row-major order. Each row in the array is
12172 indexed starting from 1 (row 0 is shared by the two tables).
12173
12174 ---
12175
12176 Hash table lookup is handled the same in version 1 and 2:
12177
12178 We assume that N and M will not exceed 2^32 - 1.
12179 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12180
12181 Given a 64-bit compilation unit signature or a type signature S, an entry
12182 in the hash table is located as follows:
12183
12184 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12185 the low-order k bits all set to 1.
12186
12187 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12188
12189 3) If the hash table entry at index H matches the signature, use that
12190 entry. If the hash table entry at index H is unused (all zeroes),
12191 terminate the search: the signature is not present in the table.
12192
12193 4) Let H = (H + H') modulo M. Repeat at Step 3.
12194
12195 Because M > N and H' and M are relatively prime, the search is guaranteed
12196 to stop at an unused slot or find the match. */
12197
12198 /* Create a hash table to map DWO IDs to their CU/TU entry in
12199 .debug_{info,types}.dwo in DWP_FILE.
12200 Returns NULL if there isn't one.
12201 Note: This function processes DWP files only, not DWO files. */
12202
12203 static struct dwp_hash_table *
12204 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12205 struct dwp_file *dwp_file, int is_debug_types)
12206 {
12207 struct objfile *objfile = dwarf2_per_objfile->objfile;
12208 bfd *dbfd = dwp_file->dbfd.get ();
12209 const gdb_byte *index_ptr, *index_end;
12210 struct dwarf2_section_info *index;
12211 uint32_t version, nr_columns, nr_units, nr_slots;
12212 struct dwp_hash_table *htab;
12213
12214 if (is_debug_types)
12215 index = &dwp_file->sections.tu_index;
12216 else
12217 index = &dwp_file->sections.cu_index;
12218
12219 if (dwarf2_section_empty_p (index))
12220 return NULL;
12221 dwarf2_read_section (objfile, index);
12222
12223 index_ptr = index->buffer;
12224 index_end = index_ptr + index->size;
12225
12226 version = read_4_bytes (dbfd, index_ptr);
12227 index_ptr += 4;
12228 if (version == 2)
12229 nr_columns = read_4_bytes (dbfd, index_ptr);
12230 else
12231 nr_columns = 0;
12232 index_ptr += 4;
12233 nr_units = read_4_bytes (dbfd, index_ptr);
12234 index_ptr += 4;
12235 nr_slots = read_4_bytes (dbfd, index_ptr);
12236 index_ptr += 4;
12237
12238 if (version != 1 && version != 2)
12239 {
12240 error (_("Dwarf Error: unsupported DWP file version (%s)"
12241 " [in module %s]"),
12242 pulongest (version), dwp_file->name);
12243 }
12244 if (nr_slots != (nr_slots & -nr_slots))
12245 {
12246 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12247 " is not power of 2 [in module %s]"),
12248 pulongest (nr_slots), dwp_file->name);
12249 }
12250
12251 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12252 htab->version = version;
12253 htab->nr_columns = nr_columns;
12254 htab->nr_units = nr_units;
12255 htab->nr_slots = nr_slots;
12256 htab->hash_table = index_ptr;
12257 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12258
12259 /* Exit early if the table is empty. */
12260 if (nr_slots == 0 || nr_units == 0
12261 || (version == 2 && nr_columns == 0))
12262 {
12263 /* All must be zero. */
12264 if (nr_slots != 0 || nr_units != 0
12265 || (version == 2 && nr_columns != 0))
12266 {
12267 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12268 " all zero [in modules %s]"),
12269 dwp_file->name);
12270 }
12271 return htab;
12272 }
12273
12274 if (version == 1)
12275 {
12276 htab->section_pool.v1.indices =
12277 htab->unit_table + sizeof (uint32_t) * nr_slots;
12278 /* It's harder to decide whether the section is too small in v1.
12279 V1 is deprecated anyway so we punt. */
12280 }
12281 else
12282 {
12283 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12284 int *ids = htab->section_pool.v2.section_ids;
12285 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12286 /* Reverse map for error checking. */
12287 int ids_seen[DW_SECT_MAX + 1];
12288 int i;
12289
12290 if (nr_columns < 2)
12291 {
12292 error (_("Dwarf Error: bad DWP hash table, too few columns"
12293 " in section table [in module %s]"),
12294 dwp_file->name);
12295 }
12296 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12297 {
12298 error (_("Dwarf Error: bad DWP hash table, too many columns"
12299 " in section table [in module %s]"),
12300 dwp_file->name);
12301 }
12302 memset (ids, 255, sizeof_ids);
12303 memset (ids_seen, 255, sizeof (ids_seen));
12304 for (i = 0; i < nr_columns; ++i)
12305 {
12306 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12307
12308 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12309 {
12310 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12311 " in section table [in module %s]"),
12312 id, dwp_file->name);
12313 }
12314 if (ids_seen[id] != -1)
12315 {
12316 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12317 " id %d in section table [in module %s]"),
12318 id, dwp_file->name);
12319 }
12320 ids_seen[id] = i;
12321 ids[i] = id;
12322 }
12323 /* Must have exactly one info or types section. */
12324 if (((ids_seen[DW_SECT_INFO] != -1)
12325 + (ids_seen[DW_SECT_TYPES] != -1))
12326 != 1)
12327 {
12328 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12329 " DWO info/types section [in module %s]"),
12330 dwp_file->name);
12331 }
12332 /* Must have an abbrev section. */
12333 if (ids_seen[DW_SECT_ABBREV] == -1)
12334 {
12335 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12336 " section [in module %s]"),
12337 dwp_file->name);
12338 }
12339 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12340 htab->section_pool.v2.sizes =
12341 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12342 * nr_units * nr_columns);
12343 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12344 * nr_units * nr_columns))
12345 > index_end)
12346 {
12347 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12348 " [in module %s]"),
12349 dwp_file->name);
12350 }
12351 }
12352
12353 return htab;
12354 }
12355
12356 /* Update SECTIONS with the data from SECTP.
12357
12358 This function is like the other "locate" section routines that are
12359 passed to bfd_map_over_sections, but in this context the sections to
12360 read comes from the DWP V1 hash table, not the full ELF section table.
12361
12362 The result is non-zero for success, or zero if an error was found. */
12363
12364 static int
12365 locate_v1_virtual_dwo_sections (asection *sectp,
12366 struct virtual_v1_dwo_sections *sections)
12367 {
12368 const struct dwop_section_names *names = &dwop_section_names;
12369
12370 if (section_is_p (sectp->name, &names->abbrev_dwo))
12371 {
12372 /* There can be only one. */
12373 if (sections->abbrev.s.section != NULL)
12374 return 0;
12375 sections->abbrev.s.section = sectp;
12376 sections->abbrev.size = bfd_section_size (sectp);
12377 }
12378 else if (section_is_p (sectp->name, &names->info_dwo)
12379 || section_is_p (sectp->name, &names->types_dwo))
12380 {
12381 /* There can be only one. */
12382 if (sections->info_or_types.s.section != NULL)
12383 return 0;
12384 sections->info_or_types.s.section = sectp;
12385 sections->info_or_types.size = bfd_section_size (sectp);
12386 }
12387 else if (section_is_p (sectp->name, &names->line_dwo))
12388 {
12389 /* There can be only one. */
12390 if (sections->line.s.section != NULL)
12391 return 0;
12392 sections->line.s.section = sectp;
12393 sections->line.size = bfd_section_size (sectp);
12394 }
12395 else if (section_is_p (sectp->name, &names->loc_dwo))
12396 {
12397 /* There can be only one. */
12398 if (sections->loc.s.section != NULL)
12399 return 0;
12400 sections->loc.s.section = sectp;
12401 sections->loc.size = bfd_section_size (sectp);
12402 }
12403 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12404 {
12405 /* There can be only one. */
12406 if (sections->macinfo.s.section != NULL)
12407 return 0;
12408 sections->macinfo.s.section = sectp;
12409 sections->macinfo.size = bfd_section_size (sectp);
12410 }
12411 else if (section_is_p (sectp->name, &names->macro_dwo))
12412 {
12413 /* There can be only one. */
12414 if (sections->macro.s.section != NULL)
12415 return 0;
12416 sections->macro.s.section = sectp;
12417 sections->macro.size = bfd_section_size (sectp);
12418 }
12419 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12420 {
12421 /* There can be only one. */
12422 if (sections->str_offsets.s.section != NULL)
12423 return 0;
12424 sections->str_offsets.s.section = sectp;
12425 sections->str_offsets.size = bfd_section_size (sectp);
12426 }
12427 else
12428 {
12429 /* No other kind of section is valid. */
12430 return 0;
12431 }
12432
12433 return 1;
12434 }
12435
12436 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12437 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12438 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12439 This is for DWP version 1 files. */
12440
12441 static struct dwo_unit *
12442 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12443 struct dwp_file *dwp_file,
12444 uint32_t unit_index,
12445 const char *comp_dir,
12446 ULONGEST signature, int is_debug_types)
12447 {
12448 struct objfile *objfile = dwarf2_per_objfile->objfile;
12449 const struct dwp_hash_table *dwp_htab =
12450 is_debug_types ? dwp_file->tus : dwp_file->cus;
12451 bfd *dbfd = dwp_file->dbfd.get ();
12452 const char *kind = is_debug_types ? "TU" : "CU";
12453 struct dwo_file *dwo_file;
12454 struct dwo_unit *dwo_unit;
12455 struct virtual_v1_dwo_sections sections;
12456 void **dwo_file_slot;
12457 int i;
12458
12459 gdb_assert (dwp_file->version == 1);
12460
12461 if (dwarf_read_debug)
12462 {
12463 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12464 kind,
12465 pulongest (unit_index), hex_string (signature),
12466 dwp_file->name);
12467 }
12468
12469 /* Fetch the sections of this DWO unit.
12470 Put a limit on the number of sections we look for so that bad data
12471 doesn't cause us to loop forever. */
12472
12473 #define MAX_NR_V1_DWO_SECTIONS \
12474 (1 /* .debug_info or .debug_types */ \
12475 + 1 /* .debug_abbrev */ \
12476 + 1 /* .debug_line */ \
12477 + 1 /* .debug_loc */ \
12478 + 1 /* .debug_str_offsets */ \
12479 + 1 /* .debug_macro or .debug_macinfo */ \
12480 + 1 /* trailing zero */)
12481
12482 memset (&sections, 0, sizeof (sections));
12483
12484 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12485 {
12486 asection *sectp;
12487 uint32_t section_nr =
12488 read_4_bytes (dbfd,
12489 dwp_htab->section_pool.v1.indices
12490 + (unit_index + i) * sizeof (uint32_t));
12491
12492 if (section_nr == 0)
12493 break;
12494 if (section_nr >= dwp_file->num_sections)
12495 {
12496 error (_("Dwarf Error: bad DWP hash table, section number too large"
12497 " [in module %s]"),
12498 dwp_file->name);
12499 }
12500
12501 sectp = dwp_file->elf_sections[section_nr];
12502 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12503 {
12504 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12505 " [in module %s]"),
12506 dwp_file->name);
12507 }
12508 }
12509
12510 if (i < 2
12511 || dwarf2_section_empty_p (&sections.info_or_types)
12512 || dwarf2_section_empty_p (&sections.abbrev))
12513 {
12514 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12515 " [in module %s]"),
12516 dwp_file->name);
12517 }
12518 if (i == MAX_NR_V1_DWO_SECTIONS)
12519 {
12520 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12521 " [in module %s]"),
12522 dwp_file->name);
12523 }
12524
12525 /* It's easier for the rest of the code if we fake a struct dwo_file and
12526 have dwo_unit "live" in that. At least for now.
12527
12528 The DWP file can be made up of a random collection of CUs and TUs.
12529 However, for each CU + set of TUs that came from the same original DWO
12530 file, we can combine them back into a virtual DWO file to save space
12531 (fewer struct dwo_file objects to allocate). Remember that for really
12532 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12533
12534 std::string virtual_dwo_name =
12535 string_printf ("virtual-dwo/%d-%d-%d-%d",
12536 get_section_id (&sections.abbrev),
12537 get_section_id (&sections.line),
12538 get_section_id (&sections.loc),
12539 get_section_id (&sections.str_offsets));
12540 /* Can we use an existing virtual DWO file? */
12541 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12542 virtual_dwo_name.c_str (),
12543 comp_dir);
12544 /* Create one if necessary. */
12545 if (*dwo_file_slot == NULL)
12546 {
12547 if (dwarf_read_debug)
12548 {
12549 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12550 virtual_dwo_name.c_str ());
12551 }
12552 dwo_file = new struct dwo_file;
12553 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12554 virtual_dwo_name);
12555 dwo_file->comp_dir = comp_dir;
12556 dwo_file->sections.abbrev = sections.abbrev;
12557 dwo_file->sections.line = sections.line;
12558 dwo_file->sections.loc = sections.loc;
12559 dwo_file->sections.macinfo = sections.macinfo;
12560 dwo_file->sections.macro = sections.macro;
12561 dwo_file->sections.str_offsets = sections.str_offsets;
12562 /* The "str" section is global to the entire DWP file. */
12563 dwo_file->sections.str = dwp_file->sections.str;
12564 /* The info or types section is assigned below to dwo_unit,
12565 there's no need to record it in dwo_file.
12566 Also, we can't simply record type sections in dwo_file because
12567 we record a pointer into the vector in dwo_unit. As we collect more
12568 types we'll grow the vector and eventually have to reallocate space
12569 for it, invalidating all copies of pointers into the previous
12570 contents. */
12571 *dwo_file_slot = dwo_file;
12572 }
12573 else
12574 {
12575 if (dwarf_read_debug)
12576 {
12577 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12578 virtual_dwo_name.c_str ());
12579 }
12580 dwo_file = (struct dwo_file *) *dwo_file_slot;
12581 }
12582
12583 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12584 dwo_unit->dwo_file = dwo_file;
12585 dwo_unit->signature = signature;
12586 dwo_unit->section =
12587 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12588 *dwo_unit->section = sections.info_or_types;
12589 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12590
12591 return dwo_unit;
12592 }
12593
12594 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12595 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12596 piece within that section used by a TU/CU, return a virtual section
12597 of just that piece. */
12598
12599 static struct dwarf2_section_info
12600 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12601 struct dwarf2_section_info *section,
12602 bfd_size_type offset, bfd_size_type size)
12603 {
12604 struct dwarf2_section_info result;
12605 asection *sectp;
12606
12607 gdb_assert (section != NULL);
12608 gdb_assert (!section->is_virtual);
12609
12610 memset (&result, 0, sizeof (result));
12611 result.s.containing_section = section;
12612 result.is_virtual = true;
12613
12614 if (size == 0)
12615 return result;
12616
12617 sectp = get_section_bfd_section (section);
12618
12619 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12620 bounds of the real section. This is a pretty-rare event, so just
12621 flag an error (easier) instead of a warning and trying to cope. */
12622 if (sectp == NULL
12623 || offset + size > bfd_section_size (sectp))
12624 {
12625 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12626 " in section %s [in module %s]"),
12627 sectp ? bfd_section_name (sectp) : "<unknown>",
12628 objfile_name (dwarf2_per_objfile->objfile));
12629 }
12630
12631 result.virtual_offset = offset;
12632 result.size = size;
12633 return result;
12634 }
12635
12636 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12637 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12638 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12639 This is for DWP version 2 files. */
12640
12641 static struct dwo_unit *
12642 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12643 struct dwp_file *dwp_file,
12644 uint32_t unit_index,
12645 const char *comp_dir,
12646 ULONGEST signature, int is_debug_types)
12647 {
12648 struct objfile *objfile = dwarf2_per_objfile->objfile;
12649 const struct dwp_hash_table *dwp_htab =
12650 is_debug_types ? dwp_file->tus : dwp_file->cus;
12651 bfd *dbfd = dwp_file->dbfd.get ();
12652 const char *kind = is_debug_types ? "TU" : "CU";
12653 struct dwo_file *dwo_file;
12654 struct dwo_unit *dwo_unit;
12655 struct virtual_v2_dwo_sections sections;
12656 void **dwo_file_slot;
12657 int i;
12658
12659 gdb_assert (dwp_file->version == 2);
12660
12661 if (dwarf_read_debug)
12662 {
12663 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12664 kind,
12665 pulongest (unit_index), hex_string (signature),
12666 dwp_file->name);
12667 }
12668
12669 /* Fetch the section offsets of this DWO unit. */
12670
12671 memset (&sections, 0, sizeof (sections));
12672
12673 for (i = 0; i < dwp_htab->nr_columns; ++i)
12674 {
12675 uint32_t offset = read_4_bytes (dbfd,
12676 dwp_htab->section_pool.v2.offsets
12677 + (((unit_index - 1) * dwp_htab->nr_columns
12678 + i)
12679 * sizeof (uint32_t)));
12680 uint32_t size = read_4_bytes (dbfd,
12681 dwp_htab->section_pool.v2.sizes
12682 + (((unit_index - 1) * dwp_htab->nr_columns
12683 + i)
12684 * sizeof (uint32_t)));
12685
12686 switch (dwp_htab->section_pool.v2.section_ids[i])
12687 {
12688 case DW_SECT_INFO:
12689 case DW_SECT_TYPES:
12690 sections.info_or_types_offset = offset;
12691 sections.info_or_types_size = size;
12692 break;
12693 case DW_SECT_ABBREV:
12694 sections.abbrev_offset = offset;
12695 sections.abbrev_size = size;
12696 break;
12697 case DW_SECT_LINE:
12698 sections.line_offset = offset;
12699 sections.line_size = size;
12700 break;
12701 case DW_SECT_LOC:
12702 sections.loc_offset = offset;
12703 sections.loc_size = size;
12704 break;
12705 case DW_SECT_STR_OFFSETS:
12706 sections.str_offsets_offset = offset;
12707 sections.str_offsets_size = size;
12708 break;
12709 case DW_SECT_MACINFO:
12710 sections.macinfo_offset = offset;
12711 sections.macinfo_size = size;
12712 break;
12713 case DW_SECT_MACRO:
12714 sections.macro_offset = offset;
12715 sections.macro_size = size;
12716 break;
12717 }
12718 }
12719
12720 /* It's easier for the rest of the code if we fake a struct dwo_file and
12721 have dwo_unit "live" in that. At least for now.
12722
12723 The DWP file can be made up of a random collection of CUs and TUs.
12724 However, for each CU + set of TUs that came from the same original DWO
12725 file, we can combine them back into a virtual DWO file to save space
12726 (fewer struct dwo_file objects to allocate). Remember that for really
12727 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12728
12729 std::string virtual_dwo_name =
12730 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12731 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12732 (long) (sections.line_size ? sections.line_offset : 0),
12733 (long) (sections.loc_size ? sections.loc_offset : 0),
12734 (long) (sections.str_offsets_size
12735 ? sections.str_offsets_offset : 0));
12736 /* Can we use an existing virtual DWO file? */
12737 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12738 virtual_dwo_name.c_str (),
12739 comp_dir);
12740 /* Create one if necessary. */
12741 if (*dwo_file_slot == NULL)
12742 {
12743 if (dwarf_read_debug)
12744 {
12745 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12746 virtual_dwo_name.c_str ());
12747 }
12748 dwo_file = new struct dwo_file;
12749 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12750 virtual_dwo_name);
12751 dwo_file->comp_dir = comp_dir;
12752 dwo_file->sections.abbrev =
12753 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12754 sections.abbrev_offset, sections.abbrev_size);
12755 dwo_file->sections.line =
12756 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12757 sections.line_offset, sections.line_size);
12758 dwo_file->sections.loc =
12759 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12760 sections.loc_offset, sections.loc_size);
12761 dwo_file->sections.macinfo =
12762 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12763 sections.macinfo_offset, sections.macinfo_size);
12764 dwo_file->sections.macro =
12765 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12766 sections.macro_offset, sections.macro_size);
12767 dwo_file->sections.str_offsets =
12768 create_dwp_v2_section (dwarf2_per_objfile,
12769 &dwp_file->sections.str_offsets,
12770 sections.str_offsets_offset,
12771 sections.str_offsets_size);
12772 /* The "str" section is global to the entire DWP file. */
12773 dwo_file->sections.str = dwp_file->sections.str;
12774 /* The info or types section is assigned below to dwo_unit,
12775 there's no need to record it in dwo_file.
12776 Also, we can't simply record type sections in dwo_file because
12777 we record a pointer into the vector in dwo_unit. As we collect more
12778 types we'll grow the vector and eventually have to reallocate space
12779 for it, invalidating all copies of pointers into the previous
12780 contents. */
12781 *dwo_file_slot = dwo_file;
12782 }
12783 else
12784 {
12785 if (dwarf_read_debug)
12786 {
12787 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12788 virtual_dwo_name.c_str ());
12789 }
12790 dwo_file = (struct dwo_file *) *dwo_file_slot;
12791 }
12792
12793 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12794 dwo_unit->dwo_file = dwo_file;
12795 dwo_unit->signature = signature;
12796 dwo_unit->section =
12797 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12798 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12799 is_debug_types
12800 ? &dwp_file->sections.types
12801 : &dwp_file->sections.info,
12802 sections.info_or_types_offset,
12803 sections.info_or_types_size);
12804 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12805
12806 return dwo_unit;
12807 }
12808
12809 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12810 Returns NULL if the signature isn't found. */
12811
12812 static struct dwo_unit *
12813 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12814 struct dwp_file *dwp_file, const char *comp_dir,
12815 ULONGEST signature, int is_debug_types)
12816 {
12817 const struct dwp_hash_table *dwp_htab =
12818 is_debug_types ? dwp_file->tus : dwp_file->cus;
12819 bfd *dbfd = dwp_file->dbfd.get ();
12820 uint32_t mask = dwp_htab->nr_slots - 1;
12821 uint32_t hash = signature & mask;
12822 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12823 unsigned int i;
12824 void **slot;
12825 struct dwo_unit find_dwo_cu;
12826
12827 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12828 find_dwo_cu.signature = signature;
12829 slot = htab_find_slot (is_debug_types
12830 ? dwp_file->loaded_tus
12831 : dwp_file->loaded_cus,
12832 &find_dwo_cu, INSERT);
12833
12834 if (*slot != NULL)
12835 return (struct dwo_unit *) *slot;
12836
12837 /* Use a for loop so that we don't loop forever on bad debug info. */
12838 for (i = 0; i < dwp_htab->nr_slots; ++i)
12839 {
12840 ULONGEST signature_in_table;
12841
12842 signature_in_table =
12843 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12844 if (signature_in_table == signature)
12845 {
12846 uint32_t unit_index =
12847 read_4_bytes (dbfd,
12848 dwp_htab->unit_table + hash * sizeof (uint32_t));
12849
12850 if (dwp_file->version == 1)
12851 {
12852 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12853 dwp_file, unit_index,
12854 comp_dir, signature,
12855 is_debug_types);
12856 }
12857 else
12858 {
12859 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12860 dwp_file, unit_index,
12861 comp_dir, signature,
12862 is_debug_types);
12863 }
12864 return (struct dwo_unit *) *slot;
12865 }
12866 if (signature_in_table == 0)
12867 return NULL;
12868 hash = (hash + hash2) & mask;
12869 }
12870
12871 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12872 " [in module %s]"),
12873 dwp_file->name);
12874 }
12875
12876 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12877 Open the file specified by FILE_NAME and hand it off to BFD for
12878 preliminary analysis. Return a newly initialized bfd *, which
12879 includes a canonicalized copy of FILE_NAME.
12880 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12881 SEARCH_CWD is true if the current directory is to be searched.
12882 It will be searched before debug-file-directory.
12883 If successful, the file is added to the bfd include table of the
12884 objfile's bfd (see gdb_bfd_record_inclusion).
12885 If unable to find/open the file, return NULL.
12886 NOTE: This function is derived from symfile_bfd_open. */
12887
12888 static gdb_bfd_ref_ptr
12889 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12890 const char *file_name, int is_dwp, int search_cwd)
12891 {
12892 int desc;
12893 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12894 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12895 to debug_file_directory. */
12896 const char *search_path;
12897 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12898
12899 gdb::unique_xmalloc_ptr<char> search_path_holder;
12900 if (search_cwd)
12901 {
12902 if (*debug_file_directory != '\0')
12903 {
12904 search_path_holder.reset (concat (".", dirname_separator_string,
12905 debug_file_directory,
12906 (char *) NULL));
12907 search_path = search_path_holder.get ();
12908 }
12909 else
12910 search_path = ".";
12911 }
12912 else
12913 search_path = debug_file_directory;
12914
12915 openp_flags flags = OPF_RETURN_REALPATH;
12916 if (is_dwp)
12917 flags |= OPF_SEARCH_IN_PATH;
12918
12919 gdb::unique_xmalloc_ptr<char> absolute_name;
12920 desc = openp (search_path, flags, file_name,
12921 O_RDONLY | O_BINARY, &absolute_name);
12922 if (desc < 0)
12923 return NULL;
12924
12925 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12926 gnutarget, desc));
12927 if (sym_bfd == NULL)
12928 return NULL;
12929 bfd_set_cacheable (sym_bfd.get (), 1);
12930
12931 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12932 return NULL;
12933
12934 /* Success. Record the bfd as having been included by the objfile's bfd.
12935 This is important because things like demangled_names_hash lives in the
12936 objfile's per_bfd space and may have references to things like symbol
12937 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12938 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12939
12940 return sym_bfd;
12941 }
12942
12943 /* Try to open DWO file FILE_NAME.
12944 COMP_DIR is the DW_AT_comp_dir attribute.
12945 The result is the bfd handle of the file.
12946 If there is a problem finding or opening the file, return NULL.
12947 Upon success, the canonicalized path of the file is stored in the bfd,
12948 same as symfile_bfd_open. */
12949
12950 static gdb_bfd_ref_ptr
12951 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12952 const char *file_name, const char *comp_dir)
12953 {
12954 if (IS_ABSOLUTE_PATH (file_name))
12955 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12956 0 /*is_dwp*/, 0 /*search_cwd*/);
12957
12958 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12959
12960 if (comp_dir != NULL)
12961 {
12962 char *path_to_try = concat (comp_dir, SLASH_STRING,
12963 file_name, (char *) NULL);
12964
12965 /* NOTE: If comp_dir is a relative path, this will also try the
12966 search path, which seems useful. */
12967 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12968 path_to_try,
12969 0 /*is_dwp*/,
12970 1 /*search_cwd*/));
12971 xfree (path_to_try);
12972 if (abfd != NULL)
12973 return abfd;
12974 }
12975
12976 /* That didn't work, try debug-file-directory, which, despite its name,
12977 is a list of paths. */
12978
12979 if (*debug_file_directory == '\0')
12980 return NULL;
12981
12982 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12983 0 /*is_dwp*/, 1 /*search_cwd*/);
12984 }
12985
12986 /* This function is mapped across the sections and remembers the offset and
12987 size of each of the DWO debugging sections we are interested in. */
12988
12989 static void
12990 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12991 {
12992 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12993 const struct dwop_section_names *names = &dwop_section_names;
12994
12995 if (section_is_p (sectp->name, &names->abbrev_dwo))
12996 {
12997 dwo_sections->abbrev.s.section = sectp;
12998 dwo_sections->abbrev.size = bfd_section_size (sectp);
12999 }
13000 else if (section_is_p (sectp->name, &names->info_dwo))
13001 {
13002 dwo_sections->info.s.section = sectp;
13003 dwo_sections->info.size = bfd_section_size (sectp);
13004 }
13005 else if (section_is_p (sectp->name, &names->line_dwo))
13006 {
13007 dwo_sections->line.s.section = sectp;
13008 dwo_sections->line.size = bfd_section_size (sectp);
13009 }
13010 else if (section_is_p (sectp->name, &names->loc_dwo))
13011 {
13012 dwo_sections->loc.s.section = sectp;
13013 dwo_sections->loc.size = bfd_section_size (sectp);
13014 }
13015 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13016 {
13017 dwo_sections->macinfo.s.section = sectp;
13018 dwo_sections->macinfo.size = bfd_section_size (sectp);
13019 }
13020 else if (section_is_p (sectp->name, &names->macro_dwo))
13021 {
13022 dwo_sections->macro.s.section = sectp;
13023 dwo_sections->macro.size = bfd_section_size (sectp);
13024 }
13025 else if (section_is_p (sectp->name, &names->str_dwo))
13026 {
13027 dwo_sections->str.s.section = sectp;
13028 dwo_sections->str.size = bfd_section_size (sectp);
13029 }
13030 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13031 {
13032 dwo_sections->str_offsets.s.section = sectp;
13033 dwo_sections->str_offsets.size = bfd_section_size (sectp);
13034 }
13035 else if (section_is_p (sectp->name, &names->types_dwo))
13036 {
13037 struct dwarf2_section_info type_section;
13038
13039 memset (&type_section, 0, sizeof (type_section));
13040 type_section.s.section = sectp;
13041 type_section.size = bfd_section_size (sectp);
13042 dwo_sections->types.push_back (type_section);
13043 }
13044 }
13045
13046 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13047 by PER_CU. This is for the non-DWP case.
13048 The result is NULL if DWO_NAME can't be found. */
13049
13050 static struct dwo_file *
13051 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13052 const char *dwo_name, const char *comp_dir)
13053 {
13054 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13055
13056 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
13057 if (dbfd == NULL)
13058 {
13059 if (dwarf_read_debug)
13060 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13061 return NULL;
13062 }
13063
13064 dwo_file_up dwo_file (new struct dwo_file);
13065 dwo_file->dwo_name = dwo_name;
13066 dwo_file->comp_dir = comp_dir;
13067 dwo_file->dbfd = std::move (dbfd);
13068
13069 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13070 &dwo_file->sections);
13071
13072 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13073 dwo_file->cus);
13074
13075 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13076 dwo_file->sections.types, dwo_file->tus);
13077
13078 if (dwarf_read_debug)
13079 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13080
13081 return dwo_file.release ();
13082 }
13083
13084 /* This function is mapped across the sections and remembers the offset and
13085 size of each of the DWP debugging sections common to version 1 and 2 that
13086 we are interested in. */
13087
13088 static void
13089 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13090 void *dwp_file_ptr)
13091 {
13092 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13093 const struct dwop_section_names *names = &dwop_section_names;
13094 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13095
13096 /* Record the ELF section number for later lookup: this is what the
13097 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13098 gdb_assert (elf_section_nr < dwp_file->num_sections);
13099 dwp_file->elf_sections[elf_section_nr] = sectp;
13100
13101 /* Look for specific sections that we need. */
13102 if (section_is_p (sectp->name, &names->str_dwo))
13103 {
13104 dwp_file->sections.str.s.section = sectp;
13105 dwp_file->sections.str.size = bfd_section_size (sectp);
13106 }
13107 else if (section_is_p (sectp->name, &names->cu_index))
13108 {
13109 dwp_file->sections.cu_index.s.section = sectp;
13110 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13111 }
13112 else if (section_is_p (sectp->name, &names->tu_index))
13113 {
13114 dwp_file->sections.tu_index.s.section = sectp;
13115 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13116 }
13117 }
13118
13119 /* This function is mapped across the sections and remembers the offset and
13120 size of each of the DWP version 2 debugging sections that we are interested
13121 in. This is split into a separate function because we don't know if we
13122 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13123
13124 static void
13125 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13126 {
13127 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13128 const struct dwop_section_names *names = &dwop_section_names;
13129 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13130
13131 /* Record the ELF section number for later lookup: this is what the
13132 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13133 gdb_assert (elf_section_nr < dwp_file->num_sections);
13134 dwp_file->elf_sections[elf_section_nr] = sectp;
13135
13136 /* Look for specific sections that we need. */
13137 if (section_is_p (sectp->name, &names->abbrev_dwo))
13138 {
13139 dwp_file->sections.abbrev.s.section = sectp;
13140 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13141 }
13142 else if (section_is_p (sectp->name, &names->info_dwo))
13143 {
13144 dwp_file->sections.info.s.section = sectp;
13145 dwp_file->sections.info.size = bfd_section_size (sectp);
13146 }
13147 else if (section_is_p (sectp->name, &names->line_dwo))
13148 {
13149 dwp_file->sections.line.s.section = sectp;
13150 dwp_file->sections.line.size = bfd_section_size (sectp);
13151 }
13152 else if (section_is_p (sectp->name, &names->loc_dwo))
13153 {
13154 dwp_file->sections.loc.s.section = sectp;
13155 dwp_file->sections.loc.size = bfd_section_size (sectp);
13156 }
13157 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13158 {
13159 dwp_file->sections.macinfo.s.section = sectp;
13160 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13161 }
13162 else if (section_is_p (sectp->name, &names->macro_dwo))
13163 {
13164 dwp_file->sections.macro.s.section = sectp;
13165 dwp_file->sections.macro.size = bfd_section_size (sectp);
13166 }
13167 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13168 {
13169 dwp_file->sections.str_offsets.s.section = sectp;
13170 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13171 }
13172 else if (section_is_p (sectp->name, &names->types_dwo))
13173 {
13174 dwp_file->sections.types.s.section = sectp;
13175 dwp_file->sections.types.size = bfd_section_size (sectp);
13176 }
13177 }
13178
13179 /* Hash function for dwp_file loaded CUs/TUs. */
13180
13181 static hashval_t
13182 hash_dwp_loaded_cutus (const void *item)
13183 {
13184 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13185
13186 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13187 return dwo_unit->signature;
13188 }
13189
13190 /* Equality function for dwp_file loaded CUs/TUs. */
13191
13192 static int
13193 eq_dwp_loaded_cutus (const void *a, const void *b)
13194 {
13195 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13196 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13197
13198 return dua->signature == dub->signature;
13199 }
13200
13201 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13202
13203 static htab_t
13204 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13205 {
13206 return htab_create_alloc_ex (3,
13207 hash_dwp_loaded_cutus,
13208 eq_dwp_loaded_cutus,
13209 NULL,
13210 &objfile->objfile_obstack,
13211 hashtab_obstack_allocate,
13212 dummy_obstack_deallocate);
13213 }
13214
13215 /* Try to open DWP file FILE_NAME.
13216 The result is the bfd handle of the file.
13217 If there is a problem finding or opening the file, return NULL.
13218 Upon success, the canonicalized path of the file is stored in the bfd,
13219 same as symfile_bfd_open. */
13220
13221 static gdb_bfd_ref_ptr
13222 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13223 const char *file_name)
13224 {
13225 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13226 1 /*is_dwp*/,
13227 1 /*search_cwd*/));
13228 if (abfd != NULL)
13229 return abfd;
13230
13231 /* Work around upstream bug 15652.
13232 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13233 [Whether that's a "bug" is debatable, but it is getting in our way.]
13234 We have no real idea where the dwp file is, because gdb's realpath-ing
13235 of the executable's path may have discarded the needed info.
13236 [IWBN if the dwp file name was recorded in the executable, akin to
13237 .gnu_debuglink, but that doesn't exist yet.]
13238 Strip the directory from FILE_NAME and search again. */
13239 if (*debug_file_directory != '\0')
13240 {
13241 /* Don't implicitly search the current directory here.
13242 If the user wants to search "." to handle this case,
13243 it must be added to debug-file-directory. */
13244 return try_open_dwop_file (dwarf2_per_objfile,
13245 lbasename (file_name), 1 /*is_dwp*/,
13246 0 /*search_cwd*/);
13247 }
13248
13249 return NULL;
13250 }
13251
13252 /* Initialize the use of the DWP file for the current objfile.
13253 By convention the name of the DWP file is ${objfile}.dwp.
13254 The result is NULL if it can't be found. */
13255
13256 static std::unique_ptr<struct dwp_file>
13257 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13258 {
13259 struct objfile *objfile = dwarf2_per_objfile->objfile;
13260
13261 /* Try to find first .dwp for the binary file before any symbolic links
13262 resolving. */
13263
13264 /* If the objfile is a debug file, find the name of the real binary
13265 file and get the name of dwp file from there. */
13266 std::string dwp_name;
13267 if (objfile->separate_debug_objfile_backlink != NULL)
13268 {
13269 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13270 const char *backlink_basename = lbasename (backlink->original_name);
13271
13272 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13273 }
13274 else
13275 dwp_name = objfile->original_name;
13276
13277 dwp_name += ".dwp";
13278
13279 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13280 if (dbfd == NULL
13281 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13282 {
13283 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13284 dwp_name = objfile_name (objfile);
13285 dwp_name += ".dwp";
13286 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13287 }
13288
13289 if (dbfd == NULL)
13290 {
13291 if (dwarf_read_debug)
13292 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13293 return std::unique_ptr<dwp_file> ();
13294 }
13295
13296 const char *name = bfd_get_filename (dbfd.get ());
13297 std::unique_ptr<struct dwp_file> dwp_file
13298 (new struct dwp_file (name, std::move (dbfd)));
13299
13300 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13301 dwp_file->elf_sections =
13302 OBSTACK_CALLOC (&objfile->objfile_obstack,
13303 dwp_file->num_sections, asection *);
13304
13305 bfd_map_over_sections (dwp_file->dbfd.get (),
13306 dwarf2_locate_common_dwp_sections,
13307 dwp_file.get ());
13308
13309 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13310 0);
13311
13312 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13313 1);
13314
13315 /* The DWP file version is stored in the hash table. Oh well. */
13316 if (dwp_file->cus && dwp_file->tus
13317 && dwp_file->cus->version != dwp_file->tus->version)
13318 {
13319 /* Technically speaking, we should try to limp along, but this is
13320 pretty bizarre. We use pulongest here because that's the established
13321 portability solution (e.g, we cannot use %u for uint32_t). */
13322 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13323 " TU version %s [in DWP file %s]"),
13324 pulongest (dwp_file->cus->version),
13325 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13326 }
13327
13328 if (dwp_file->cus)
13329 dwp_file->version = dwp_file->cus->version;
13330 else if (dwp_file->tus)
13331 dwp_file->version = dwp_file->tus->version;
13332 else
13333 dwp_file->version = 2;
13334
13335 if (dwp_file->version == 2)
13336 bfd_map_over_sections (dwp_file->dbfd.get (),
13337 dwarf2_locate_v2_dwp_sections,
13338 dwp_file.get ());
13339
13340 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13341 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13342
13343 if (dwarf_read_debug)
13344 {
13345 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13346 fprintf_unfiltered (gdb_stdlog,
13347 " %s CUs, %s TUs\n",
13348 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13349 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13350 }
13351
13352 return dwp_file;
13353 }
13354
13355 /* Wrapper around open_and_init_dwp_file, only open it once. */
13356
13357 static struct dwp_file *
13358 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13359 {
13360 if (! dwarf2_per_objfile->dwp_checked)
13361 {
13362 dwarf2_per_objfile->dwp_file
13363 = open_and_init_dwp_file (dwarf2_per_objfile);
13364 dwarf2_per_objfile->dwp_checked = 1;
13365 }
13366 return dwarf2_per_objfile->dwp_file.get ();
13367 }
13368
13369 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13370 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13371 or in the DWP file for the objfile, referenced by THIS_UNIT.
13372 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13373 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13374
13375 This is called, for example, when wanting to read a variable with a
13376 complex location. Therefore we don't want to do file i/o for every call.
13377 Therefore we don't want to look for a DWO file on every call.
13378 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13379 then we check if we've already seen DWO_NAME, and only THEN do we check
13380 for a DWO file.
13381
13382 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13383 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13384
13385 static struct dwo_unit *
13386 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13387 const char *dwo_name, const char *comp_dir,
13388 ULONGEST signature, int is_debug_types)
13389 {
13390 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13391 struct objfile *objfile = dwarf2_per_objfile->objfile;
13392 const char *kind = is_debug_types ? "TU" : "CU";
13393 void **dwo_file_slot;
13394 struct dwo_file *dwo_file;
13395 struct dwp_file *dwp_file;
13396
13397 /* First see if there's a DWP file.
13398 If we have a DWP file but didn't find the DWO inside it, don't
13399 look for the original DWO file. It makes gdb behave differently
13400 depending on whether one is debugging in the build tree. */
13401
13402 dwp_file = get_dwp_file (dwarf2_per_objfile);
13403 if (dwp_file != NULL)
13404 {
13405 const struct dwp_hash_table *dwp_htab =
13406 is_debug_types ? dwp_file->tus : dwp_file->cus;
13407
13408 if (dwp_htab != NULL)
13409 {
13410 struct dwo_unit *dwo_cutu =
13411 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13412 signature, is_debug_types);
13413
13414 if (dwo_cutu != NULL)
13415 {
13416 if (dwarf_read_debug)
13417 {
13418 fprintf_unfiltered (gdb_stdlog,
13419 "Virtual DWO %s %s found: @%s\n",
13420 kind, hex_string (signature),
13421 host_address_to_string (dwo_cutu));
13422 }
13423 return dwo_cutu;
13424 }
13425 }
13426 }
13427 else
13428 {
13429 /* No DWP file, look for the DWO file. */
13430
13431 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13432 dwo_name, comp_dir);
13433 if (*dwo_file_slot == NULL)
13434 {
13435 /* Read in the file and build a table of the CUs/TUs it contains. */
13436 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13437 }
13438 /* NOTE: This will be NULL if unable to open the file. */
13439 dwo_file = (struct dwo_file *) *dwo_file_slot;
13440
13441 if (dwo_file != NULL)
13442 {
13443 struct dwo_unit *dwo_cutu = NULL;
13444
13445 if (is_debug_types && dwo_file->tus)
13446 {
13447 struct dwo_unit find_dwo_cutu;
13448
13449 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13450 find_dwo_cutu.signature = signature;
13451 dwo_cutu
13452 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13453 }
13454 else if (!is_debug_types && dwo_file->cus)
13455 {
13456 struct dwo_unit find_dwo_cutu;
13457
13458 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13459 find_dwo_cutu.signature = signature;
13460 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13461 &find_dwo_cutu);
13462 }
13463
13464 if (dwo_cutu != NULL)
13465 {
13466 if (dwarf_read_debug)
13467 {
13468 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13469 kind, dwo_name, hex_string (signature),
13470 host_address_to_string (dwo_cutu));
13471 }
13472 return dwo_cutu;
13473 }
13474 }
13475 }
13476
13477 /* We didn't find it. This could mean a dwo_id mismatch, or
13478 someone deleted the DWO/DWP file, or the search path isn't set up
13479 correctly to find the file. */
13480
13481 if (dwarf_read_debug)
13482 {
13483 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13484 kind, dwo_name, hex_string (signature));
13485 }
13486
13487 /* This is a warning and not a complaint because it can be caused by
13488 pilot error (e.g., user accidentally deleting the DWO). */
13489 {
13490 /* Print the name of the DWP file if we looked there, helps the user
13491 better diagnose the problem. */
13492 std::string dwp_text;
13493
13494 if (dwp_file != NULL)
13495 dwp_text = string_printf (" [in DWP file %s]",
13496 lbasename (dwp_file->name));
13497
13498 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13499 " [in module %s]"),
13500 kind, dwo_name, hex_string (signature),
13501 dwp_text.c_str (),
13502 this_unit->is_debug_types ? "TU" : "CU",
13503 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13504 }
13505 return NULL;
13506 }
13507
13508 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13509 See lookup_dwo_cutu_unit for details. */
13510
13511 static struct dwo_unit *
13512 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13513 const char *dwo_name, const char *comp_dir,
13514 ULONGEST signature)
13515 {
13516 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13517 }
13518
13519 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13520 See lookup_dwo_cutu_unit for details. */
13521
13522 static struct dwo_unit *
13523 lookup_dwo_type_unit (struct signatured_type *this_tu,
13524 const char *dwo_name, const char *comp_dir)
13525 {
13526 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13527 }
13528
13529 /* Traversal function for queue_and_load_all_dwo_tus. */
13530
13531 static int
13532 queue_and_load_dwo_tu (void **slot, void *info)
13533 {
13534 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13535 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13536 ULONGEST signature = dwo_unit->signature;
13537 struct signatured_type *sig_type =
13538 lookup_dwo_signatured_type (per_cu->cu, signature);
13539
13540 if (sig_type != NULL)
13541 {
13542 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13543
13544 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13545 a real dependency of PER_CU on SIG_TYPE. That is detected later
13546 while processing PER_CU. */
13547 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13548 load_full_type_unit (sig_cu);
13549 per_cu->imported_symtabs_push (sig_cu);
13550 }
13551
13552 return 1;
13553 }
13554
13555 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13556 The DWO may have the only definition of the type, though it may not be
13557 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13558 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13559
13560 static void
13561 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13562 {
13563 struct dwo_unit *dwo_unit;
13564 struct dwo_file *dwo_file;
13565
13566 gdb_assert (!per_cu->is_debug_types);
13567 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13568 gdb_assert (per_cu->cu != NULL);
13569
13570 dwo_unit = per_cu->cu->dwo_unit;
13571 gdb_assert (dwo_unit != NULL);
13572
13573 dwo_file = dwo_unit->dwo_file;
13574 if (dwo_file->tus != NULL)
13575 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13576 }
13577
13578 /* Read in various DIEs. */
13579
13580 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13581 Inherit only the children of the DW_AT_abstract_origin DIE not being
13582 already referenced by DW_AT_abstract_origin from the children of the
13583 current DIE. */
13584
13585 static void
13586 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13587 {
13588 struct die_info *child_die;
13589 sect_offset *offsetp;
13590 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13591 struct die_info *origin_die;
13592 /* Iterator of the ORIGIN_DIE children. */
13593 struct die_info *origin_child_die;
13594 struct attribute *attr;
13595 struct dwarf2_cu *origin_cu;
13596 struct pending **origin_previous_list_in_scope;
13597
13598 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13599 if (!attr)
13600 return;
13601
13602 /* Note that following die references may follow to a die in a
13603 different cu. */
13604
13605 origin_cu = cu;
13606 origin_die = follow_die_ref (die, attr, &origin_cu);
13607
13608 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13609 symbols in. */
13610 origin_previous_list_in_scope = origin_cu->list_in_scope;
13611 origin_cu->list_in_scope = cu->list_in_scope;
13612
13613 if (die->tag != origin_die->tag
13614 && !(die->tag == DW_TAG_inlined_subroutine
13615 && origin_die->tag == DW_TAG_subprogram))
13616 complaint (_("DIE %s and its abstract origin %s have different tags"),
13617 sect_offset_str (die->sect_off),
13618 sect_offset_str (origin_die->sect_off));
13619
13620 std::vector<sect_offset> offsets;
13621
13622 for (child_die = die->child;
13623 child_die && child_die->tag;
13624 child_die = sibling_die (child_die))
13625 {
13626 struct die_info *child_origin_die;
13627 struct dwarf2_cu *child_origin_cu;
13628
13629 /* We are trying to process concrete instance entries:
13630 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13631 it's not relevant to our analysis here. i.e. detecting DIEs that are
13632 present in the abstract instance but not referenced in the concrete
13633 one. */
13634 if (child_die->tag == DW_TAG_call_site
13635 || child_die->tag == DW_TAG_GNU_call_site)
13636 continue;
13637
13638 /* For each CHILD_DIE, find the corresponding child of
13639 ORIGIN_DIE. If there is more than one layer of
13640 DW_AT_abstract_origin, follow them all; there shouldn't be,
13641 but GCC versions at least through 4.4 generate this (GCC PR
13642 40573). */
13643 child_origin_die = child_die;
13644 child_origin_cu = cu;
13645 while (1)
13646 {
13647 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13648 child_origin_cu);
13649 if (attr == NULL)
13650 break;
13651 child_origin_die = follow_die_ref (child_origin_die, attr,
13652 &child_origin_cu);
13653 }
13654
13655 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13656 counterpart may exist. */
13657 if (child_origin_die != child_die)
13658 {
13659 if (child_die->tag != child_origin_die->tag
13660 && !(child_die->tag == DW_TAG_inlined_subroutine
13661 && child_origin_die->tag == DW_TAG_subprogram))
13662 complaint (_("Child DIE %s and its abstract origin %s have "
13663 "different tags"),
13664 sect_offset_str (child_die->sect_off),
13665 sect_offset_str (child_origin_die->sect_off));
13666 if (child_origin_die->parent != origin_die)
13667 complaint (_("Child DIE %s and its abstract origin %s have "
13668 "different parents"),
13669 sect_offset_str (child_die->sect_off),
13670 sect_offset_str (child_origin_die->sect_off));
13671 else
13672 offsets.push_back (child_origin_die->sect_off);
13673 }
13674 }
13675 std::sort (offsets.begin (), offsets.end ());
13676 sect_offset *offsets_end = offsets.data () + offsets.size ();
13677 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13678 if (offsetp[-1] == *offsetp)
13679 complaint (_("Multiple children of DIE %s refer "
13680 "to DIE %s as their abstract origin"),
13681 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13682
13683 offsetp = offsets.data ();
13684 origin_child_die = origin_die->child;
13685 while (origin_child_die && origin_child_die->tag)
13686 {
13687 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13688 while (offsetp < offsets_end
13689 && *offsetp < origin_child_die->sect_off)
13690 offsetp++;
13691 if (offsetp >= offsets_end
13692 || *offsetp > origin_child_die->sect_off)
13693 {
13694 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13695 Check whether we're already processing ORIGIN_CHILD_DIE.
13696 This can happen with mutually referenced abstract_origins.
13697 PR 16581. */
13698 if (!origin_child_die->in_process)
13699 process_die (origin_child_die, origin_cu);
13700 }
13701 origin_child_die = sibling_die (origin_child_die);
13702 }
13703 origin_cu->list_in_scope = origin_previous_list_in_scope;
13704
13705 if (cu != origin_cu)
13706 compute_delayed_physnames (origin_cu);
13707 }
13708
13709 static void
13710 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13711 {
13712 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13713 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13714 struct context_stack *newobj;
13715 CORE_ADDR lowpc;
13716 CORE_ADDR highpc;
13717 struct die_info *child_die;
13718 struct attribute *attr, *call_line, *call_file;
13719 const char *name;
13720 CORE_ADDR baseaddr;
13721 struct block *block;
13722 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13723 std::vector<struct symbol *> template_args;
13724 struct template_symbol *templ_func = NULL;
13725
13726 if (inlined_func)
13727 {
13728 /* If we do not have call site information, we can't show the
13729 caller of this inlined function. That's too confusing, so
13730 only use the scope for local variables. */
13731 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13732 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13733 if (call_line == NULL || call_file == NULL)
13734 {
13735 read_lexical_block_scope (die, cu);
13736 return;
13737 }
13738 }
13739
13740 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13741
13742 name = dwarf2_name (die, cu);
13743
13744 /* Ignore functions with missing or empty names. These are actually
13745 illegal according to the DWARF standard. */
13746 if (name == NULL)
13747 {
13748 complaint (_("missing name for subprogram DIE at %s"),
13749 sect_offset_str (die->sect_off));
13750 return;
13751 }
13752
13753 /* Ignore functions with missing or invalid low and high pc attributes. */
13754 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13755 <= PC_BOUNDS_INVALID)
13756 {
13757 attr = dwarf2_attr (die, DW_AT_external, cu);
13758 if (!attr || !DW_UNSND (attr))
13759 complaint (_("cannot get low and high bounds "
13760 "for subprogram DIE at %s"),
13761 sect_offset_str (die->sect_off));
13762 return;
13763 }
13764
13765 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13766 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13767
13768 /* If we have any template arguments, then we must allocate a
13769 different sort of symbol. */
13770 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13771 {
13772 if (child_die->tag == DW_TAG_template_type_param
13773 || child_die->tag == DW_TAG_template_value_param)
13774 {
13775 templ_func = allocate_template_symbol (objfile);
13776 templ_func->subclass = SYMBOL_TEMPLATE;
13777 break;
13778 }
13779 }
13780
13781 newobj = cu->get_builder ()->push_context (0, lowpc);
13782 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13783 (struct symbol *) templ_func);
13784
13785 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13786 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13787 cu->language);
13788
13789 /* If there is a location expression for DW_AT_frame_base, record
13790 it. */
13791 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13792 if (attr != nullptr)
13793 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13794
13795 /* If there is a location for the static link, record it. */
13796 newobj->static_link = NULL;
13797 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13798 if (attr != nullptr)
13799 {
13800 newobj->static_link
13801 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13802 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13803 dwarf2_per_cu_addr_type (cu->per_cu));
13804 }
13805
13806 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13807
13808 if (die->child != NULL)
13809 {
13810 child_die = die->child;
13811 while (child_die && child_die->tag)
13812 {
13813 if (child_die->tag == DW_TAG_template_type_param
13814 || child_die->tag == DW_TAG_template_value_param)
13815 {
13816 struct symbol *arg = new_symbol (child_die, NULL, cu);
13817
13818 if (arg != NULL)
13819 template_args.push_back (arg);
13820 }
13821 else
13822 process_die (child_die, cu);
13823 child_die = sibling_die (child_die);
13824 }
13825 }
13826
13827 inherit_abstract_dies (die, cu);
13828
13829 /* If we have a DW_AT_specification, we might need to import using
13830 directives from the context of the specification DIE. See the
13831 comment in determine_prefix. */
13832 if (cu->language == language_cplus
13833 && dwarf2_attr (die, DW_AT_specification, cu))
13834 {
13835 struct dwarf2_cu *spec_cu = cu;
13836 struct die_info *spec_die = die_specification (die, &spec_cu);
13837
13838 while (spec_die)
13839 {
13840 child_die = spec_die->child;
13841 while (child_die && child_die->tag)
13842 {
13843 if (child_die->tag == DW_TAG_imported_module)
13844 process_die (child_die, spec_cu);
13845 child_die = sibling_die (child_die);
13846 }
13847
13848 /* In some cases, GCC generates specification DIEs that
13849 themselves contain DW_AT_specification attributes. */
13850 spec_die = die_specification (spec_die, &spec_cu);
13851 }
13852 }
13853
13854 struct context_stack cstk = cu->get_builder ()->pop_context ();
13855 /* Make a block for the local symbols within. */
13856 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13857 cstk.static_link, lowpc, highpc);
13858
13859 /* For C++, set the block's scope. */
13860 if ((cu->language == language_cplus
13861 || cu->language == language_fortran
13862 || cu->language == language_d
13863 || cu->language == language_rust)
13864 && cu->processing_has_namespace_info)
13865 block_set_scope (block, determine_prefix (die, cu),
13866 &objfile->objfile_obstack);
13867
13868 /* If we have address ranges, record them. */
13869 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13870
13871 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13872
13873 /* Attach template arguments to function. */
13874 if (!template_args.empty ())
13875 {
13876 gdb_assert (templ_func != NULL);
13877
13878 templ_func->n_template_arguments = template_args.size ();
13879 templ_func->template_arguments
13880 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13881 templ_func->n_template_arguments);
13882 memcpy (templ_func->template_arguments,
13883 template_args.data (),
13884 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13885
13886 /* Make sure that the symtab is set on the new symbols. Even
13887 though they don't appear in this symtab directly, other parts
13888 of gdb assume that symbols do, and this is reasonably
13889 true. */
13890 for (symbol *sym : template_args)
13891 symbol_set_symtab (sym, symbol_symtab (templ_func));
13892 }
13893
13894 /* In C++, we can have functions nested inside functions (e.g., when
13895 a function declares a class that has methods). This means that
13896 when we finish processing a function scope, we may need to go
13897 back to building a containing block's symbol lists. */
13898 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13899 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13900
13901 /* If we've finished processing a top-level function, subsequent
13902 symbols go in the file symbol list. */
13903 if (cu->get_builder ()->outermost_context_p ())
13904 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13905 }
13906
13907 /* Process all the DIES contained within a lexical block scope. Start
13908 a new scope, process the dies, and then close the scope. */
13909
13910 static void
13911 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13912 {
13913 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13914 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13915 CORE_ADDR lowpc, highpc;
13916 struct die_info *child_die;
13917 CORE_ADDR baseaddr;
13918
13919 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13920
13921 /* Ignore blocks with missing or invalid low and high pc attributes. */
13922 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13923 as multiple lexical blocks? Handling children in a sane way would
13924 be nasty. Might be easier to properly extend generic blocks to
13925 describe ranges. */
13926 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13927 {
13928 case PC_BOUNDS_NOT_PRESENT:
13929 /* DW_TAG_lexical_block has no attributes, process its children as if
13930 there was no wrapping by that DW_TAG_lexical_block.
13931 GCC does no longer produces such DWARF since GCC r224161. */
13932 for (child_die = die->child;
13933 child_die != NULL && child_die->tag;
13934 child_die = sibling_die (child_die))
13935 process_die (child_die, cu);
13936 return;
13937 case PC_BOUNDS_INVALID:
13938 return;
13939 }
13940 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13941 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13942
13943 cu->get_builder ()->push_context (0, lowpc);
13944 if (die->child != NULL)
13945 {
13946 child_die = die->child;
13947 while (child_die && child_die->tag)
13948 {
13949 process_die (child_die, cu);
13950 child_die = sibling_die (child_die);
13951 }
13952 }
13953 inherit_abstract_dies (die, cu);
13954 struct context_stack cstk = cu->get_builder ()->pop_context ();
13955
13956 if (*cu->get_builder ()->get_local_symbols () != NULL
13957 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13958 {
13959 struct block *block
13960 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13961 cstk.start_addr, highpc);
13962
13963 /* Note that recording ranges after traversing children, as we
13964 do here, means that recording a parent's ranges entails
13965 walking across all its children's ranges as they appear in
13966 the address map, which is quadratic behavior.
13967
13968 It would be nicer to record the parent's ranges before
13969 traversing its children, simply overriding whatever you find
13970 there. But since we don't even decide whether to create a
13971 block until after we've traversed its children, that's hard
13972 to do. */
13973 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13974 }
13975 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13976 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13977 }
13978
13979 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13980
13981 static void
13982 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13983 {
13984 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13985 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13986 CORE_ADDR pc, baseaddr;
13987 struct attribute *attr;
13988 struct call_site *call_site, call_site_local;
13989 void **slot;
13990 int nparams;
13991 struct die_info *child_die;
13992
13993 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13994
13995 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13996 if (attr == NULL)
13997 {
13998 /* This was a pre-DWARF-5 GNU extension alias
13999 for DW_AT_call_return_pc. */
14000 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14001 }
14002 if (!attr)
14003 {
14004 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
14005 "DIE %s [in module %s]"),
14006 sect_offset_str (die->sect_off), objfile_name (objfile));
14007 return;
14008 }
14009 pc = attr_value_as_address (attr) + baseaddr;
14010 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14011
14012 if (cu->call_site_htab == NULL)
14013 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14014 NULL, &objfile->objfile_obstack,
14015 hashtab_obstack_allocate, NULL);
14016 call_site_local.pc = pc;
14017 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14018 if (*slot != NULL)
14019 {
14020 complaint (_("Duplicate PC %s for DW_TAG_call_site "
14021 "DIE %s [in module %s]"),
14022 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14023 objfile_name (objfile));
14024 return;
14025 }
14026
14027 /* Count parameters at the caller. */
14028
14029 nparams = 0;
14030 for (child_die = die->child; child_die && child_die->tag;
14031 child_die = sibling_die (child_die))
14032 {
14033 if (child_die->tag != DW_TAG_call_site_parameter
14034 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14035 {
14036 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
14037 "DW_TAG_call_site child DIE %s [in module %s]"),
14038 child_die->tag, sect_offset_str (child_die->sect_off),
14039 objfile_name (objfile));
14040 continue;
14041 }
14042
14043 nparams++;
14044 }
14045
14046 call_site
14047 = ((struct call_site *)
14048 obstack_alloc (&objfile->objfile_obstack,
14049 sizeof (*call_site)
14050 + (sizeof (*call_site->parameter) * (nparams - 1))));
14051 *slot = call_site;
14052 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14053 call_site->pc = pc;
14054
14055 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14056 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14057 {
14058 struct die_info *func_die;
14059
14060 /* Skip also over DW_TAG_inlined_subroutine. */
14061 for (func_die = die->parent;
14062 func_die && func_die->tag != DW_TAG_subprogram
14063 && func_die->tag != DW_TAG_subroutine_type;
14064 func_die = func_die->parent);
14065
14066 /* DW_AT_call_all_calls is a superset
14067 of DW_AT_call_all_tail_calls. */
14068 if (func_die
14069 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14070 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14071 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14072 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14073 {
14074 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14075 not complete. But keep CALL_SITE for look ups via call_site_htab,
14076 both the initial caller containing the real return address PC and
14077 the final callee containing the current PC of a chain of tail
14078 calls do not need to have the tail call list complete. But any
14079 function candidate for a virtual tail call frame searched via
14080 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14081 determined unambiguously. */
14082 }
14083 else
14084 {
14085 struct type *func_type = NULL;
14086
14087 if (func_die)
14088 func_type = get_die_type (func_die, cu);
14089 if (func_type != NULL)
14090 {
14091 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14092
14093 /* Enlist this call site to the function. */
14094 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14095 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14096 }
14097 else
14098 complaint (_("Cannot find function owning DW_TAG_call_site "
14099 "DIE %s [in module %s]"),
14100 sect_offset_str (die->sect_off), objfile_name (objfile));
14101 }
14102 }
14103
14104 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14105 if (attr == NULL)
14106 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14107 if (attr == NULL)
14108 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14109 if (attr == NULL)
14110 {
14111 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14112 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14113 }
14114 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14115 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14116 /* Keep NULL DWARF_BLOCK. */;
14117 else if (attr_form_is_block (attr))
14118 {
14119 struct dwarf2_locexpr_baton *dlbaton;
14120
14121 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14122 dlbaton->data = DW_BLOCK (attr)->data;
14123 dlbaton->size = DW_BLOCK (attr)->size;
14124 dlbaton->per_cu = cu->per_cu;
14125
14126 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14127 }
14128 else if (attr_form_is_ref (attr))
14129 {
14130 struct dwarf2_cu *target_cu = cu;
14131 struct die_info *target_die;
14132
14133 target_die = follow_die_ref (die, attr, &target_cu);
14134 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14135 if (die_is_declaration (target_die, target_cu))
14136 {
14137 const char *target_physname;
14138
14139 /* Prefer the mangled name; otherwise compute the demangled one. */
14140 target_physname = dw2_linkage_name (target_die, target_cu);
14141 if (target_physname == NULL)
14142 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14143 if (target_physname == NULL)
14144 complaint (_("DW_AT_call_target target DIE has invalid "
14145 "physname, for referencing DIE %s [in module %s]"),
14146 sect_offset_str (die->sect_off), objfile_name (objfile));
14147 else
14148 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14149 }
14150 else
14151 {
14152 CORE_ADDR lowpc;
14153
14154 /* DW_AT_entry_pc should be preferred. */
14155 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14156 <= PC_BOUNDS_INVALID)
14157 complaint (_("DW_AT_call_target target DIE has invalid "
14158 "low pc, for referencing DIE %s [in module %s]"),
14159 sect_offset_str (die->sect_off), objfile_name (objfile));
14160 else
14161 {
14162 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14163 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14164 }
14165 }
14166 }
14167 else
14168 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14169 "block nor reference, for DIE %s [in module %s]"),
14170 sect_offset_str (die->sect_off), objfile_name (objfile));
14171
14172 call_site->per_cu = cu->per_cu;
14173
14174 for (child_die = die->child;
14175 child_die && child_die->tag;
14176 child_die = sibling_die (child_die))
14177 {
14178 struct call_site_parameter *parameter;
14179 struct attribute *loc, *origin;
14180
14181 if (child_die->tag != DW_TAG_call_site_parameter
14182 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14183 {
14184 /* Already printed the complaint above. */
14185 continue;
14186 }
14187
14188 gdb_assert (call_site->parameter_count < nparams);
14189 parameter = &call_site->parameter[call_site->parameter_count];
14190
14191 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14192 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14193 register is contained in DW_AT_call_value. */
14194
14195 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14196 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14197 if (origin == NULL)
14198 {
14199 /* This was a pre-DWARF-5 GNU extension alias
14200 for DW_AT_call_parameter. */
14201 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14202 }
14203 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14204 {
14205 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14206
14207 sect_offset sect_off
14208 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14209 if (!offset_in_cu_p (&cu->header, sect_off))
14210 {
14211 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14212 binding can be done only inside one CU. Such referenced DIE
14213 therefore cannot be even moved to DW_TAG_partial_unit. */
14214 complaint (_("DW_AT_call_parameter offset is not in CU for "
14215 "DW_TAG_call_site child DIE %s [in module %s]"),
14216 sect_offset_str (child_die->sect_off),
14217 objfile_name (objfile));
14218 continue;
14219 }
14220 parameter->u.param_cu_off
14221 = (cu_offset) (sect_off - cu->header.sect_off);
14222 }
14223 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14224 {
14225 complaint (_("No DW_FORM_block* DW_AT_location for "
14226 "DW_TAG_call_site child DIE %s [in module %s]"),
14227 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14228 continue;
14229 }
14230 else
14231 {
14232 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14233 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14234 if (parameter->u.dwarf_reg != -1)
14235 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14236 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14237 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14238 &parameter->u.fb_offset))
14239 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14240 else
14241 {
14242 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14243 "for DW_FORM_block* DW_AT_location is supported for "
14244 "DW_TAG_call_site child DIE %s "
14245 "[in module %s]"),
14246 sect_offset_str (child_die->sect_off),
14247 objfile_name (objfile));
14248 continue;
14249 }
14250 }
14251
14252 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14253 if (attr == NULL)
14254 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14255 if (!attr_form_is_block (attr))
14256 {
14257 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14258 "DW_TAG_call_site child DIE %s [in module %s]"),
14259 sect_offset_str (child_die->sect_off),
14260 objfile_name (objfile));
14261 continue;
14262 }
14263 parameter->value = DW_BLOCK (attr)->data;
14264 parameter->value_size = DW_BLOCK (attr)->size;
14265
14266 /* Parameters are not pre-cleared by memset above. */
14267 parameter->data_value = NULL;
14268 parameter->data_value_size = 0;
14269 call_site->parameter_count++;
14270
14271 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14272 if (attr == NULL)
14273 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14274 if (attr != nullptr)
14275 {
14276 if (!attr_form_is_block (attr))
14277 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14278 "DW_TAG_call_site child DIE %s [in module %s]"),
14279 sect_offset_str (child_die->sect_off),
14280 objfile_name (objfile));
14281 else
14282 {
14283 parameter->data_value = DW_BLOCK (attr)->data;
14284 parameter->data_value_size = DW_BLOCK (attr)->size;
14285 }
14286 }
14287 }
14288 }
14289
14290 /* Helper function for read_variable. If DIE represents a virtual
14291 table, then return the type of the concrete object that is
14292 associated with the virtual table. Otherwise, return NULL. */
14293
14294 static struct type *
14295 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14296 {
14297 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14298 if (attr == NULL)
14299 return NULL;
14300
14301 /* Find the type DIE. */
14302 struct die_info *type_die = NULL;
14303 struct dwarf2_cu *type_cu = cu;
14304
14305 if (attr_form_is_ref (attr))
14306 type_die = follow_die_ref (die, attr, &type_cu);
14307 if (type_die == NULL)
14308 return NULL;
14309
14310 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14311 return NULL;
14312 return die_containing_type (type_die, type_cu);
14313 }
14314
14315 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14316
14317 static void
14318 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14319 {
14320 struct rust_vtable_symbol *storage = NULL;
14321
14322 if (cu->language == language_rust)
14323 {
14324 struct type *containing_type = rust_containing_type (die, cu);
14325
14326 if (containing_type != NULL)
14327 {
14328 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14329
14330 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
14331 initialize_objfile_symbol (storage);
14332 storage->concrete_type = containing_type;
14333 storage->subclass = SYMBOL_RUST_VTABLE;
14334 }
14335 }
14336
14337 struct symbol *res = new_symbol (die, NULL, cu, storage);
14338 struct attribute *abstract_origin
14339 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14340 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14341 if (res == NULL && loc && abstract_origin)
14342 {
14343 /* We have a variable without a name, but with a location and an abstract
14344 origin. This may be a concrete instance of an abstract variable
14345 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14346 later. */
14347 struct dwarf2_cu *origin_cu = cu;
14348 struct die_info *origin_die
14349 = follow_die_ref (die, abstract_origin, &origin_cu);
14350 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14351 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14352 }
14353 }
14354
14355 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14356 reading .debug_rnglists.
14357 Callback's type should be:
14358 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14359 Return true if the attributes are present and valid, otherwise,
14360 return false. */
14361
14362 template <typename Callback>
14363 static bool
14364 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14365 Callback &&callback)
14366 {
14367 struct dwarf2_per_objfile *dwarf2_per_objfile
14368 = cu->per_cu->dwarf2_per_objfile;
14369 struct objfile *objfile = dwarf2_per_objfile->objfile;
14370 bfd *obfd = objfile->obfd;
14371 /* Base address selection entry. */
14372 CORE_ADDR base;
14373 int found_base;
14374 const gdb_byte *buffer;
14375 CORE_ADDR baseaddr;
14376 bool overflow = false;
14377
14378 found_base = cu->base_known;
14379 base = cu->base_address;
14380
14381 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14382 if (offset >= dwarf2_per_objfile->rnglists.size)
14383 {
14384 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14385 offset);
14386 return false;
14387 }
14388 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14389
14390 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14391
14392 while (1)
14393 {
14394 /* Initialize it due to a false compiler warning. */
14395 CORE_ADDR range_beginning = 0, range_end = 0;
14396 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14397 + dwarf2_per_objfile->rnglists.size);
14398 unsigned int bytes_read;
14399
14400 if (buffer == buf_end)
14401 {
14402 overflow = true;
14403 break;
14404 }
14405 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14406 switch (rlet)
14407 {
14408 case DW_RLE_end_of_list:
14409 break;
14410 case DW_RLE_base_address:
14411 if (buffer + cu->header.addr_size > buf_end)
14412 {
14413 overflow = true;
14414 break;
14415 }
14416 base = read_address (obfd, buffer, cu, &bytes_read);
14417 found_base = 1;
14418 buffer += bytes_read;
14419 break;
14420 case DW_RLE_start_length:
14421 if (buffer + cu->header.addr_size > buf_end)
14422 {
14423 overflow = true;
14424 break;
14425 }
14426 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14427 buffer += bytes_read;
14428 range_end = (range_beginning
14429 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14430 buffer += bytes_read;
14431 if (buffer > buf_end)
14432 {
14433 overflow = true;
14434 break;
14435 }
14436 break;
14437 case DW_RLE_offset_pair:
14438 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14439 buffer += bytes_read;
14440 if (buffer > buf_end)
14441 {
14442 overflow = true;
14443 break;
14444 }
14445 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14446 buffer += bytes_read;
14447 if (buffer > buf_end)
14448 {
14449 overflow = true;
14450 break;
14451 }
14452 break;
14453 case DW_RLE_start_end:
14454 if (buffer + 2 * cu->header.addr_size > buf_end)
14455 {
14456 overflow = true;
14457 break;
14458 }
14459 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14460 buffer += bytes_read;
14461 range_end = read_address (obfd, buffer, cu, &bytes_read);
14462 buffer += bytes_read;
14463 break;
14464 default:
14465 complaint (_("Invalid .debug_rnglists data (no base address)"));
14466 return false;
14467 }
14468 if (rlet == DW_RLE_end_of_list || overflow)
14469 break;
14470 if (rlet == DW_RLE_base_address)
14471 continue;
14472
14473 if (!found_base)
14474 {
14475 /* We have no valid base address for the ranges
14476 data. */
14477 complaint (_("Invalid .debug_rnglists data (no base address)"));
14478 return false;
14479 }
14480
14481 if (range_beginning > range_end)
14482 {
14483 /* Inverted range entries are invalid. */
14484 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14485 return false;
14486 }
14487
14488 /* Empty range entries have no effect. */
14489 if (range_beginning == range_end)
14490 continue;
14491
14492 range_beginning += base;
14493 range_end += base;
14494
14495 /* A not-uncommon case of bad debug info.
14496 Don't pollute the addrmap with bad data. */
14497 if (range_beginning + baseaddr == 0
14498 && !dwarf2_per_objfile->has_section_at_zero)
14499 {
14500 complaint (_(".debug_rnglists entry has start address of zero"
14501 " [in module %s]"), objfile_name (objfile));
14502 continue;
14503 }
14504
14505 callback (range_beginning, range_end);
14506 }
14507
14508 if (overflow)
14509 {
14510 complaint (_("Offset %d is not terminated "
14511 "for DW_AT_ranges attribute"),
14512 offset);
14513 return false;
14514 }
14515
14516 return true;
14517 }
14518
14519 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14520 Callback's type should be:
14521 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14522 Return 1 if the attributes are present and valid, otherwise, return 0. */
14523
14524 template <typename Callback>
14525 static int
14526 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14527 Callback &&callback)
14528 {
14529 struct dwarf2_per_objfile *dwarf2_per_objfile
14530 = cu->per_cu->dwarf2_per_objfile;
14531 struct objfile *objfile = dwarf2_per_objfile->objfile;
14532 struct comp_unit_head *cu_header = &cu->header;
14533 bfd *obfd = objfile->obfd;
14534 unsigned int addr_size = cu_header->addr_size;
14535 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14536 /* Base address selection entry. */
14537 CORE_ADDR base;
14538 int found_base;
14539 unsigned int dummy;
14540 const gdb_byte *buffer;
14541 CORE_ADDR baseaddr;
14542
14543 if (cu_header->version >= 5)
14544 return dwarf2_rnglists_process (offset, cu, callback);
14545
14546 found_base = cu->base_known;
14547 base = cu->base_address;
14548
14549 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14550 if (offset >= dwarf2_per_objfile->ranges.size)
14551 {
14552 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14553 offset);
14554 return 0;
14555 }
14556 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14557
14558 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14559
14560 while (1)
14561 {
14562 CORE_ADDR range_beginning, range_end;
14563
14564 range_beginning = read_address (obfd, buffer, cu, &dummy);
14565 buffer += addr_size;
14566 range_end = read_address (obfd, buffer, cu, &dummy);
14567 buffer += addr_size;
14568 offset += 2 * addr_size;
14569
14570 /* An end of list marker is a pair of zero addresses. */
14571 if (range_beginning == 0 && range_end == 0)
14572 /* Found the end of list entry. */
14573 break;
14574
14575 /* Each base address selection entry is a pair of 2 values.
14576 The first is the largest possible address, the second is
14577 the base address. Check for a base address here. */
14578 if ((range_beginning & mask) == mask)
14579 {
14580 /* If we found the largest possible address, then we already
14581 have the base address in range_end. */
14582 base = range_end;
14583 found_base = 1;
14584 continue;
14585 }
14586
14587 if (!found_base)
14588 {
14589 /* We have no valid base address for the ranges
14590 data. */
14591 complaint (_("Invalid .debug_ranges data (no base address)"));
14592 return 0;
14593 }
14594
14595 if (range_beginning > range_end)
14596 {
14597 /* Inverted range entries are invalid. */
14598 complaint (_("Invalid .debug_ranges data (inverted range)"));
14599 return 0;
14600 }
14601
14602 /* Empty range entries have no effect. */
14603 if (range_beginning == range_end)
14604 continue;
14605
14606 range_beginning += base;
14607 range_end += base;
14608
14609 /* A not-uncommon case of bad debug info.
14610 Don't pollute the addrmap with bad data. */
14611 if (range_beginning + baseaddr == 0
14612 && !dwarf2_per_objfile->has_section_at_zero)
14613 {
14614 complaint (_(".debug_ranges entry has start address of zero"
14615 " [in module %s]"), objfile_name (objfile));
14616 continue;
14617 }
14618
14619 callback (range_beginning, range_end);
14620 }
14621
14622 return 1;
14623 }
14624
14625 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14626 Return 1 if the attributes are present and valid, otherwise, return 0.
14627 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14628
14629 static int
14630 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14631 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14632 struct partial_symtab *ranges_pst)
14633 {
14634 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14635 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14636 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14637 SECT_OFF_TEXT (objfile));
14638 int low_set = 0;
14639 CORE_ADDR low = 0;
14640 CORE_ADDR high = 0;
14641 int retval;
14642
14643 retval = dwarf2_ranges_process (offset, cu,
14644 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14645 {
14646 if (ranges_pst != NULL)
14647 {
14648 CORE_ADDR lowpc;
14649 CORE_ADDR highpc;
14650
14651 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14652 range_beginning + baseaddr)
14653 - baseaddr);
14654 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14655 range_end + baseaddr)
14656 - baseaddr);
14657 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14658 lowpc, highpc - 1, ranges_pst);
14659 }
14660
14661 /* FIXME: This is recording everything as a low-high
14662 segment of consecutive addresses. We should have a
14663 data structure for discontiguous block ranges
14664 instead. */
14665 if (! low_set)
14666 {
14667 low = range_beginning;
14668 high = range_end;
14669 low_set = 1;
14670 }
14671 else
14672 {
14673 if (range_beginning < low)
14674 low = range_beginning;
14675 if (range_end > high)
14676 high = range_end;
14677 }
14678 });
14679 if (!retval)
14680 return 0;
14681
14682 if (! low_set)
14683 /* If the first entry is an end-of-list marker, the range
14684 describes an empty scope, i.e. no instructions. */
14685 return 0;
14686
14687 if (low_return)
14688 *low_return = low;
14689 if (high_return)
14690 *high_return = high;
14691 return 1;
14692 }
14693
14694 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14695 definition for the return value. *LOWPC and *HIGHPC are set iff
14696 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14697
14698 static enum pc_bounds_kind
14699 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14700 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14701 struct partial_symtab *pst)
14702 {
14703 struct dwarf2_per_objfile *dwarf2_per_objfile
14704 = cu->per_cu->dwarf2_per_objfile;
14705 struct attribute *attr;
14706 struct attribute *attr_high;
14707 CORE_ADDR low = 0;
14708 CORE_ADDR high = 0;
14709 enum pc_bounds_kind ret;
14710
14711 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14712 if (attr_high)
14713 {
14714 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14715 if (attr != nullptr)
14716 {
14717 low = attr_value_as_address (attr);
14718 high = attr_value_as_address (attr_high);
14719 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14720 high += low;
14721 }
14722 else
14723 /* Found high w/o low attribute. */
14724 return PC_BOUNDS_INVALID;
14725
14726 /* Found consecutive range of addresses. */
14727 ret = PC_BOUNDS_HIGH_LOW;
14728 }
14729 else
14730 {
14731 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14732 if (attr != NULL)
14733 {
14734 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14735 We take advantage of the fact that DW_AT_ranges does not appear
14736 in DW_TAG_compile_unit of DWO files. */
14737 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14738 unsigned int ranges_offset = (DW_UNSND (attr)
14739 + (need_ranges_base
14740 ? cu->ranges_base
14741 : 0));
14742
14743 /* Value of the DW_AT_ranges attribute is the offset in the
14744 .debug_ranges section. */
14745 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14746 return PC_BOUNDS_INVALID;
14747 /* Found discontinuous range of addresses. */
14748 ret = PC_BOUNDS_RANGES;
14749 }
14750 else
14751 return PC_BOUNDS_NOT_PRESENT;
14752 }
14753
14754 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14755 if (high <= low)
14756 return PC_BOUNDS_INVALID;
14757
14758 /* When using the GNU linker, .gnu.linkonce. sections are used to
14759 eliminate duplicate copies of functions and vtables and such.
14760 The linker will arbitrarily choose one and discard the others.
14761 The AT_*_pc values for such functions refer to local labels in
14762 these sections. If the section from that file was discarded, the
14763 labels are not in the output, so the relocs get a value of 0.
14764 If this is a discarded function, mark the pc bounds as invalid,
14765 so that GDB will ignore it. */
14766 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14767 return PC_BOUNDS_INVALID;
14768
14769 *lowpc = low;
14770 if (highpc)
14771 *highpc = high;
14772 return ret;
14773 }
14774
14775 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14776 its low and high PC addresses. Do nothing if these addresses could not
14777 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14778 and HIGHPC to the high address if greater than HIGHPC. */
14779
14780 static void
14781 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14782 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14783 struct dwarf2_cu *cu)
14784 {
14785 CORE_ADDR low, high;
14786 struct die_info *child = die->child;
14787
14788 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14789 {
14790 *lowpc = std::min (*lowpc, low);
14791 *highpc = std::max (*highpc, high);
14792 }
14793
14794 /* If the language does not allow nested subprograms (either inside
14795 subprograms or lexical blocks), we're done. */
14796 if (cu->language != language_ada)
14797 return;
14798
14799 /* Check all the children of the given DIE. If it contains nested
14800 subprograms, then check their pc bounds. Likewise, we need to
14801 check lexical blocks as well, as they may also contain subprogram
14802 definitions. */
14803 while (child && child->tag)
14804 {
14805 if (child->tag == DW_TAG_subprogram
14806 || child->tag == DW_TAG_lexical_block)
14807 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14808 child = sibling_die (child);
14809 }
14810 }
14811
14812 /* Get the low and high pc's represented by the scope DIE, and store
14813 them in *LOWPC and *HIGHPC. If the correct values can't be
14814 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14815
14816 static void
14817 get_scope_pc_bounds (struct die_info *die,
14818 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14819 struct dwarf2_cu *cu)
14820 {
14821 CORE_ADDR best_low = (CORE_ADDR) -1;
14822 CORE_ADDR best_high = (CORE_ADDR) 0;
14823 CORE_ADDR current_low, current_high;
14824
14825 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14826 >= PC_BOUNDS_RANGES)
14827 {
14828 best_low = current_low;
14829 best_high = current_high;
14830 }
14831 else
14832 {
14833 struct die_info *child = die->child;
14834
14835 while (child && child->tag)
14836 {
14837 switch (child->tag) {
14838 case DW_TAG_subprogram:
14839 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14840 break;
14841 case DW_TAG_namespace:
14842 case DW_TAG_module:
14843 /* FIXME: carlton/2004-01-16: Should we do this for
14844 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14845 that current GCC's always emit the DIEs corresponding
14846 to definitions of methods of classes as children of a
14847 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14848 the DIEs giving the declarations, which could be
14849 anywhere). But I don't see any reason why the
14850 standards says that they have to be there. */
14851 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14852
14853 if (current_low != ((CORE_ADDR) -1))
14854 {
14855 best_low = std::min (best_low, current_low);
14856 best_high = std::max (best_high, current_high);
14857 }
14858 break;
14859 default:
14860 /* Ignore. */
14861 break;
14862 }
14863
14864 child = sibling_die (child);
14865 }
14866 }
14867
14868 *lowpc = best_low;
14869 *highpc = best_high;
14870 }
14871
14872 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14873 in DIE. */
14874
14875 static void
14876 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14877 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14878 {
14879 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14880 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14881 struct attribute *attr;
14882 struct attribute *attr_high;
14883
14884 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14885 if (attr_high)
14886 {
14887 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14888 if (attr != nullptr)
14889 {
14890 CORE_ADDR low = attr_value_as_address (attr);
14891 CORE_ADDR high = attr_value_as_address (attr_high);
14892
14893 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14894 high += low;
14895
14896 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14897 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14898 cu->get_builder ()->record_block_range (block, low, high - 1);
14899 }
14900 }
14901
14902 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14903 if (attr != nullptr)
14904 {
14905 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14906 We take advantage of the fact that DW_AT_ranges does not appear
14907 in DW_TAG_compile_unit of DWO files. */
14908 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14909
14910 /* The value of the DW_AT_ranges attribute is the offset of the
14911 address range list in the .debug_ranges section. */
14912 unsigned long offset = (DW_UNSND (attr)
14913 + (need_ranges_base ? cu->ranges_base : 0));
14914
14915 std::vector<blockrange> blockvec;
14916 dwarf2_ranges_process (offset, cu,
14917 [&] (CORE_ADDR start, CORE_ADDR end)
14918 {
14919 start += baseaddr;
14920 end += baseaddr;
14921 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14922 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14923 cu->get_builder ()->record_block_range (block, start, end - 1);
14924 blockvec.emplace_back (start, end);
14925 });
14926
14927 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14928 }
14929 }
14930
14931 /* Check whether the producer field indicates either of GCC < 4.6, or the
14932 Intel C/C++ compiler, and cache the result in CU. */
14933
14934 static void
14935 check_producer (struct dwarf2_cu *cu)
14936 {
14937 int major, minor;
14938
14939 if (cu->producer == NULL)
14940 {
14941 /* For unknown compilers expect their behavior is DWARF version
14942 compliant.
14943
14944 GCC started to support .debug_types sections by -gdwarf-4 since
14945 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14946 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14947 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14948 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14949 }
14950 else if (producer_is_gcc (cu->producer, &major, &minor))
14951 {
14952 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14953 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14954 }
14955 else if (producer_is_icc (cu->producer, &major, &minor))
14956 {
14957 cu->producer_is_icc = true;
14958 cu->producer_is_icc_lt_14 = major < 14;
14959 }
14960 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14961 cu->producer_is_codewarrior = true;
14962 else
14963 {
14964 /* For other non-GCC compilers, expect their behavior is DWARF version
14965 compliant. */
14966 }
14967
14968 cu->checked_producer = true;
14969 }
14970
14971 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14972 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14973 during 4.6.0 experimental. */
14974
14975 static bool
14976 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14977 {
14978 if (!cu->checked_producer)
14979 check_producer (cu);
14980
14981 return cu->producer_is_gxx_lt_4_6;
14982 }
14983
14984
14985 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14986 with incorrect is_stmt attributes. */
14987
14988 static bool
14989 producer_is_codewarrior (struct dwarf2_cu *cu)
14990 {
14991 if (!cu->checked_producer)
14992 check_producer (cu);
14993
14994 return cu->producer_is_codewarrior;
14995 }
14996
14997 /* Return the default accessibility type if it is not overridden by
14998 DW_AT_accessibility. */
14999
15000 static enum dwarf_access_attribute
15001 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15002 {
15003 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15004 {
15005 /* The default DWARF 2 accessibility for members is public, the default
15006 accessibility for inheritance is private. */
15007
15008 if (die->tag != DW_TAG_inheritance)
15009 return DW_ACCESS_public;
15010 else
15011 return DW_ACCESS_private;
15012 }
15013 else
15014 {
15015 /* DWARF 3+ defines the default accessibility a different way. The same
15016 rules apply now for DW_TAG_inheritance as for the members and it only
15017 depends on the container kind. */
15018
15019 if (die->parent->tag == DW_TAG_class_type)
15020 return DW_ACCESS_private;
15021 else
15022 return DW_ACCESS_public;
15023 }
15024 }
15025
15026 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15027 offset. If the attribute was not found return 0, otherwise return
15028 1. If it was found but could not properly be handled, set *OFFSET
15029 to 0. */
15030
15031 static int
15032 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15033 LONGEST *offset)
15034 {
15035 struct attribute *attr;
15036
15037 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15038 if (attr != NULL)
15039 {
15040 *offset = 0;
15041
15042 /* Note that we do not check for a section offset first here.
15043 This is because DW_AT_data_member_location is new in DWARF 4,
15044 so if we see it, we can assume that a constant form is really
15045 a constant and not a section offset. */
15046 if (attr_form_is_constant (attr))
15047 *offset = dwarf2_get_attr_constant_value (attr, 0);
15048 else if (attr_form_is_section_offset (attr))
15049 dwarf2_complex_location_expr_complaint ();
15050 else if (attr_form_is_block (attr))
15051 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15052 else
15053 dwarf2_complex_location_expr_complaint ();
15054
15055 return 1;
15056 }
15057
15058 return 0;
15059 }
15060
15061 /* Add an aggregate field to the field list. */
15062
15063 static void
15064 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15065 struct dwarf2_cu *cu)
15066 {
15067 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15068 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15069 struct nextfield *new_field;
15070 struct attribute *attr;
15071 struct field *fp;
15072 const char *fieldname = "";
15073
15074 if (die->tag == DW_TAG_inheritance)
15075 {
15076 fip->baseclasses.emplace_back ();
15077 new_field = &fip->baseclasses.back ();
15078 }
15079 else
15080 {
15081 fip->fields.emplace_back ();
15082 new_field = &fip->fields.back ();
15083 }
15084
15085 fip->nfields++;
15086
15087 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15088 if (attr != nullptr)
15089 new_field->accessibility = DW_UNSND (attr);
15090 else
15091 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15092 if (new_field->accessibility != DW_ACCESS_public)
15093 fip->non_public_fields = 1;
15094
15095 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15096 if (attr != nullptr)
15097 new_field->virtuality = DW_UNSND (attr);
15098 else
15099 new_field->virtuality = DW_VIRTUALITY_none;
15100
15101 fp = &new_field->field;
15102
15103 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15104 {
15105 LONGEST offset;
15106
15107 /* Data member other than a C++ static data member. */
15108
15109 /* Get type of field. */
15110 fp->type = die_type (die, cu);
15111
15112 SET_FIELD_BITPOS (*fp, 0);
15113
15114 /* Get bit size of field (zero if none). */
15115 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15116 if (attr != nullptr)
15117 {
15118 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15119 }
15120 else
15121 {
15122 FIELD_BITSIZE (*fp) = 0;
15123 }
15124
15125 /* Get bit offset of field. */
15126 if (handle_data_member_location (die, cu, &offset))
15127 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15128 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15129 if (attr != nullptr)
15130 {
15131 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15132 {
15133 /* For big endian bits, the DW_AT_bit_offset gives the
15134 additional bit offset from the MSB of the containing
15135 anonymous object to the MSB of the field. We don't
15136 have to do anything special since we don't need to
15137 know the size of the anonymous object. */
15138 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15139 }
15140 else
15141 {
15142 /* For little endian bits, compute the bit offset to the
15143 MSB of the anonymous object, subtract off the number of
15144 bits from the MSB of the field to the MSB of the
15145 object, and then subtract off the number of bits of
15146 the field itself. The result is the bit offset of
15147 the LSB of the field. */
15148 int anonymous_size;
15149 int bit_offset = DW_UNSND (attr);
15150
15151 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15152 if (attr != nullptr)
15153 {
15154 /* The size of the anonymous object containing
15155 the bit field is explicit, so use the
15156 indicated size (in bytes). */
15157 anonymous_size = DW_UNSND (attr);
15158 }
15159 else
15160 {
15161 /* The size of the anonymous object containing
15162 the bit field must be inferred from the type
15163 attribute of the data member containing the
15164 bit field. */
15165 anonymous_size = TYPE_LENGTH (fp->type);
15166 }
15167 SET_FIELD_BITPOS (*fp,
15168 (FIELD_BITPOS (*fp)
15169 + anonymous_size * bits_per_byte
15170 - bit_offset - FIELD_BITSIZE (*fp)));
15171 }
15172 }
15173 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15174 if (attr != NULL)
15175 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15176 + dwarf2_get_attr_constant_value (attr, 0)));
15177
15178 /* Get name of field. */
15179 fieldname = dwarf2_name (die, cu);
15180 if (fieldname == NULL)
15181 fieldname = "";
15182
15183 /* The name is already allocated along with this objfile, so we don't
15184 need to duplicate it for the type. */
15185 fp->name = fieldname;
15186
15187 /* Change accessibility for artificial fields (e.g. virtual table
15188 pointer or virtual base class pointer) to private. */
15189 if (dwarf2_attr (die, DW_AT_artificial, cu))
15190 {
15191 FIELD_ARTIFICIAL (*fp) = 1;
15192 new_field->accessibility = DW_ACCESS_private;
15193 fip->non_public_fields = 1;
15194 }
15195 }
15196 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15197 {
15198 /* C++ static member. */
15199
15200 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15201 is a declaration, but all versions of G++ as of this writing
15202 (so through at least 3.2.1) incorrectly generate
15203 DW_TAG_variable tags. */
15204
15205 const char *physname;
15206
15207 /* Get name of field. */
15208 fieldname = dwarf2_name (die, cu);
15209 if (fieldname == NULL)
15210 return;
15211
15212 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15213 if (attr
15214 /* Only create a symbol if this is an external value.
15215 new_symbol checks this and puts the value in the global symbol
15216 table, which we want. If it is not external, new_symbol
15217 will try to put the value in cu->list_in_scope which is wrong. */
15218 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15219 {
15220 /* A static const member, not much different than an enum as far as
15221 we're concerned, except that we can support more types. */
15222 new_symbol (die, NULL, cu);
15223 }
15224
15225 /* Get physical name. */
15226 physname = dwarf2_physname (fieldname, die, cu);
15227
15228 /* The name is already allocated along with this objfile, so we don't
15229 need to duplicate it for the type. */
15230 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15231 FIELD_TYPE (*fp) = die_type (die, cu);
15232 FIELD_NAME (*fp) = fieldname;
15233 }
15234 else if (die->tag == DW_TAG_inheritance)
15235 {
15236 LONGEST offset;
15237
15238 /* C++ base class field. */
15239 if (handle_data_member_location (die, cu, &offset))
15240 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15241 FIELD_BITSIZE (*fp) = 0;
15242 FIELD_TYPE (*fp) = die_type (die, cu);
15243 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15244 }
15245 else if (die->tag == DW_TAG_variant_part)
15246 {
15247 /* process_structure_scope will treat this DIE as a union. */
15248 process_structure_scope (die, cu);
15249
15250 /* The variant part is relative to the start of the enclosing
15251 structure. */
15252 SET_FIELD_BITPOS (*fp, 0);
15253 fp->type = get_die_type (die, cu);
15254 fp->artificial = 1;
15255 fp->name = "<<variant>>";
15256
15257 /* Normally a DW_TAG_variant_part won't have a size, but our
15258 representation requires one, so set it to the maximum of the
15259 child sizes, being sure to account for the offset at which
15260 each child is seen. */
15261 if (TYPE_LENGTH (fp->type) == 0)
15262 {
15263 unsigned max = 0;
15264 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15265 {
15266 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15267 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15268 if (len > max)
15269 max = len;
15270 }
15271 TYPE_LENGTH (fp->type) = max;
15272 }
15273 }
15274 else
15275 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15276 }
15277
15278 /* Can the type given by DIE define another type? */
15279
15280 static bool
15281 type_can_define_types (const struct die_info *die)
15282 {
15283 switch (die->tag)
15284 {
15285 case DW_TAG_typedef:
15286 case DW_TAG_class_type:
15287 case DW_TAG_structure_type:
15288 case DW_TAG_union_type:
15289 case DW_TAG_enumeration_type:
15290 return true;
15291
15292 default:
15293 return false;
15294 }
15295 }
15296
15297 /* Add a type definition defined in the scope of the FIP's class. */
15298
15299 static void
15300 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15301 struct dwarf2_cu *cu)
15302 {
15303 struct decl_field fp;
15304 memset (&fp, 0, sizeof (fp));
15305
15306 gdb_assert (type_can_define_types (die));
15307
15308 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15309 fp.name = dwarf2_name (die, cu);
15310 fp.type = read_type_die (die, cu);
15311
15312 /* Save accessibility. */
15313 enum dwarf_access_attribute accessibility;
15314 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15315 if (attr != NULL)
15316 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15317 else
15318 accessibility = dwarf2_default_access_attribute (die, cu);
15319 switch (accessibility)
15320 {
15321 case DW_ACCESS_public:
15322 /* The assumed value if neither private nor protected. */
15323 break;
15324 case DW_ACCESS_private:
15325 fp.is_private = 1;
15326 break;
15327 case DW_ACCESS_protected:
15328 fp.is_protected = 1;
15329 break;
15330 default:
15331 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15332 }
15333
15334 if (die->tag == DW_TAG_typedef)
15335 fip->typedef_field_list.push_back (fp);
15336 else
15337 fip->nested_types_list.push_back (fp);
15338 }
15339
15340 /* Create the vector of fields, and attach it to the type. */
15341
15342 static void
15343 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15344 struct dwarf2_cu *cu)
15345 {
15346 int nfields = fip->nfields;
15347
15348 /* Record the field count, allocate space for the array of fields,
15349 and create blank accessibility bitfields if necessary. */
15350 TYPE_NFIELDS (type) = nfields;
15351 TYPE_FIELDS (type) = (struct field *)
15352 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15353
15354 if (fip->non_public_fields && cu->language != language_ada)
15355 {
15356 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15357
15358 TYPE_FIELD_PRIVATE_BITS (type) =
15359 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15360 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15361
15362 TYPE_FIELD_PROTECTED_BITS (type) =
15363 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15364 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15365
15366 TYPE_FIELD_IGNORE_BITS (type) =
15367 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15368 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15369 }
15370
15371 /* If the type has baseclasses, allocate and clear a bit vector for
15372 TYPE_FIELD_VIRTUAL_BITS. */
15373 if (!fip->baseclasses.empty () && cu->language != language_ada)
15374 {
15375 int num_bytes = B_BYTES (fip->baseclasses.size ());
15376 unsigned char *pointer;
15377
15378 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15379 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15380 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15381 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15382 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15383 }
15384
15385 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15386 {
15387 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15388
15389 for (int index = 0; index < nfields; ++index)
15390 {
15391 struct nextfield &field = fip->fields[index];
15392
15393 if (field.variant.is_discriminant)
15394 di->discriminant_index = index;
15395 else if (field.variant.default_branch)
15396 di->default_index = index;
15397 else
15398 di->discriminants[index] = field.variant.discriminant_value;
15399 }
15400 }
15401
15402 /* Copy the saved-up fields into the field vector. */
15403 for (int i = 0; i < nfields; ++i)
15404 {
15405 struct nextfield &field
15406 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15407 : fip->fields[i - fip->baseclasses.size ()]);
15408
15409 TYPE_FIELD (type, i) = field.field;
15410 switch (field.accessibility)
15411 {
15412 case DW_ACCESS_private:
15413 if (cu->language != language_ada)
15414 SET_TYPE_FIELD_PRIVATE (type, i);
15415 break;
15416
15417 case DW_ACCESS_protected:
15418 if (cu->language != language_ada)
15419 SET_TYPE_FIELD_PROTECTED (type, i);
15420 break;
15421
15422 case DW_ACCESS_public:
15423 break;
15424
15425 default:
15426 /* Unknown accessibility. Complain and treat it as public. */
15427 {
15428 complaint (_("unsupported accessibility %d"),
15429 field.accessibility);
15430 }
15431 break;
15432 }
15433 if (i < fip->baseclasses.size ())
15434 {
15435 switch (field.virtuality)
15436 {
15437 case DW_VIRTUALITY_virtual:
15438 case DW_VIRTUALITY_pure_virtual:
15439 if (cu->language == language_ada)
15440 error (_("unexpected virtuality in component of Ada type"));
15441 SET_TYPE_FIELD_VIRTUAL (type, i);
15442 break;
15443 }
15444 }
15445 }
15446 }
15447
15448 /* Return true if this member function is a constructor, false
15449 otherwise. */
15450
15451 static int
15452 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15453 {
15454 const char *fieldname;
15455 const char *type_name;
15456 int len;
15457
15458 if (die->parent == NULL)
15459 return 0;
15460
15461 if (die->parent->tag != DW_TAG_structure_type
15462 && die->parent->tag != DW_TAG_union_type
15463 && die->parent->tag != DW_TAG_class_type)
15464 return 0;
15465
15466 fieldname = dwarf2_name (die, cu);
15467 type_name = dwarf2_name (die->parent, cu);
15468 if (fieldname == NULL || type_name == NULL)
15469 return 0;
15470
15471 len = strlen (fieldname);
15472 return (strncmp (fieldname, type_name, len) == 0
15473 && (type_name[len] == '\0' || type_name[len] == '<'));
15474 }
15475
15476 /* Add a member function to the proper fieldlist. */
15477
15478 static void
15479 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15480 struct type *type, struct dwarf2_cu *cu)
15481 {
15482 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15483 struct attribute *attr;
15484 int i;
15485 struct fnfieldlist *flp = nullptr;
15486 struct fn_field *fnp;
15487 const char *fieldname;
15488 struct type *this_type;
15489 enum dwarf_access_attribute accessibility;
15490
15491 if (cu->language == language_ada)
15492 error (_("unexpected member function in Ada type"));
15493
15494 /* Get name of member function. */
15495 fieldname = dwarf2_name (die, cu);
15496 if (fieldname == NULL)
15497 return;
15498
15499 /* Look up member function name in fieldlist. */
15500 for (i = 0; i < fip->fnfieldlists.size (); i++)
15501 {
15502 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15503 {
15504 flp = &fip->fnfieldlists[i];
15505 break;
15506 }
15507 }
15508
15509 /* Create a new fnfieldlist if necessary. */
15510 if (flp == nullptr)
15511 {
15512 fip->fnfieldlists.emplace_back ();
15513 flp = &fip->fnfieldlists.back ();
15514 flp->name = fieldname;
15515 i = fip->fnfieldlists.size () - 1;
15516 }
15517
15518 /* Create a new member function field and add it to the vector of
15519 fnfieldlists. */
15520 flp->fnfields.emplace_back ();
15521 fnp = &flp->fnfields.back ();
15522
15523 /* Delay processing of the physname until later. */
15524 if (cu->language == language_cplus)
15525 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15526 die, cu);
15527 else
15528 {
15529 const char *physname = dwarf2_physname (fieldname, die, cu);
15530 fnp->physname = physname ? physname : "";
15531 }
15532
15533 fnp->type = alloc_type (objfile);
15534 this_type = read_type_die (die, cu);
15535 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15536 {
15537 int nparams = TYPE_NFIELDS (this_type);
15538
15539 /* TYPE is the domain of this method, and THIS_TYPE is the type
15540 of the method itself (TYPE_CODE_METHOD). */
15541 smash_to_method_type (fnp->type, type,
15542 TYPE_TARGET_TYPE (this_type),
15543 TYPE_FIELDS (this_type),
15544 TYPE_NFIELDS (this_type),
15545 TYPE_VARARGS (this_type));
15546
15547 /* Handle static member functions.
15548 Dwarf2 has no clean way to discern C++ static and non-static
15549 member functions. G++ helps GDB by marking the first
15550 parameter for non-static member functions (which is the this
15551 pointer) as artificial. We obtain this information from
15552 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15553 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15554 fnp->voffset = VOFFSET_STATIC;
15555 }
15556 else
15557 complaint (_("member function type missing for '%s'"),
15558 dwarf2_full_name (fieldname, die, cu));
15559
15560 /* Get fcontext from DW_AT_containing_type if present. */
15561 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15562 fnp->fcontext = die_containing_type (die, cu);
15563
15564 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15565 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15566
15567 /* Get accessibility. */
15568 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15569 if (attr != nullptr)
15570 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15571 else
15572 accessibility = dwarf2_default_access_attribute (die, cu);
15573 switch (accessibility)
15574 {
15575 case DW_ACCESS_private:
15576 fnp->is_private = 1;
15577 break;
15578 case DW_ACCESS_protected:
15579 fnp->is_protected = 1;
15580 break;
15581 }
15582
15583 /* Check for artificial methods. */
15584 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15585 if (attr && DW_UNSND (attr) != 0)
15586 fnp->is_artificial = 1;
15587
15588 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15589
15590 /* Get index in virtual function table if it is a virtual member
15591 function. For older versions of GCC, this is an offset in the
15592 appropriate virtual table, as specified by DW_AT_containing_type.
15593 For everyone else, it is an expression to be evaluated relative
15594 to the object address. */
15595
15596 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15597 if (attr != nullptr)
15598 {
15599 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15600 {
15601 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15602 {
15603 /* Old-style GCC. */
15604 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15605 }
15606 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15607 || (DW_BLOCK (attr)->size > 1
15608 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15609 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15610 {
15611 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15612 if ((fnp->voffset % cu->header.addr_size) != 0)
15613 dwarf2_complex_location_expr_complaint ();
15614 else
15615 fnp->voffset /= cu->header.addr_size;
15616 fnp->voffset += 2;
15617 }
15618 else
15619 dwarf2_complex_location_expr_complaint ();
15620
15621 if (!fnp->fcontext)
15622 {
15623 /* If there is no `this' field and no DW_AT_containing_type,
15624 we cannot actually find a base class context for the
15625 vtable! */
15626 if (TYPE_NFIELDS (this_type) == 0
15627 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15628 {
15629 complaint (_("cannot determine context for virtual member "
15630 "function \"%s\" (offset %s)"),
15631 fieldname, sect_offset_str (die->sect_off));
15632 }
15633 else
15634 {
15635 fnp->fcontext
15636 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15637 }
15638 }
15639 }
15640 else if (attr_form_is_section_offset (attr))
15641 {
15642 dwarf2_complex_location_expr_complaint ();
15643 }
15644 else
15645 {
15646 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15647 fieldname);
15648 }
15649 }
15650 else
15651 {
15652 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15653 if (attr && DW_UNSND (attr))
15654 {
15655 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15656 complaint (_("Member function \"%s\" (offset %s) is virtual "
15657 "but the vtable offset is not specified"),
15658 fieldname, sect_offset_str (die->sect_off));
15659 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15660 TYPE_CPLUS_DYNAMIC (type) = 1;
15661 }
15662 }
15663 }
15664
15665 /* Create the vector of member function fields, and attach it to the type. */
15666
15667 static void
15668 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15669 struct dwarf2_cu *cu)
15670 {
15671 if (cu->language == language_ada)
15672 error (_("unexpected member functions in Ada type"));
15673
15674 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15675 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15676 TYPE_ALLOC (type,
15677 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15678
15679 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15680 {
15681 struct fnfieldlist &nf = fip->fnfieldlists[i];
15682 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15683
15684 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15685 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15686 fn_flp->fn_fields = (struct fn_field *)
15687 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15688
15689 for (int k = 0; k < nf.fnfields.size (); ++k)
15690 fn_flp->fn_fields[k] = nf.fnfields[k];
15691 }
15692
15693 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15694 }
15695
15696 /* Returns non-zero if NAME is the name of a vtable member in CU's
15697 language, zero otherwise. */
15698 static int
15699 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15700 {
15701 static const char vptr[] = "_vptr";
15702
15703 /* Look for the C++ form of the vtable. */
15704 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15705 return 1;
15706
15707 return 0;
15708 }
15709
15710 /* GCC outputs unnamed structures that are really pointers to member
15711 functions, with the ABI-specified layout. If TYPE describes
15712 such a structure, smash it into a member function type.
15713
15714 GCC shouldn't do this; it should just output pointer to member DIEs.
15715 This is GCC PR debug/28767. */
15716
15717 static void
15718 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15719 {
15720 struct type *pfn_type, *self_type, *new_type;
15721
15722 /* Check for a structure with no name and two children. */
15723 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15724 return;
15725
15726 /* Check for __pfn and __delta members. */
15727 if (TYPE_FIELD_NAME (type, 0) == NULL
15728 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15729 || TYPE_FIELD_NAME (type, 1) == NULL
15730 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15731 return;
15732
15733 /* Find the type of the method. */
15734 pfn_type = TYPE_FIELD_TYPE (type, 0);
15735 if (pfn_type == NULL
15736 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15737 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15738 return;
15739
15740 /* Look for the "this" argument. */
15741 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15742 if (TYPE_NFIELDS (pfn_type) == 0
15743 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15744 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15745 return;
15746
15747 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15748 new_type = alloc_type (objfile);
15749 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15750 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15751 TYPE_VARARGS (pfn_type));
15752 smash_to_methodptr_type (type, new_type);
15753 }
15754
15755 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15756 appropriate error checking and issuing complaints if there is a
15757 problem. */
15758
15759 static ULONGEST
15760 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15761 {
15762 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15763
15764 if (attr == nullptr)
15765 return 0;
15766
15767 if (!attr_form_is_constant (attr))
15768 {
15769 complaint (_("DW_AT_alignment must have constant form"
15770 " - DIE at %s [in module %s]"),
15771 sect_offset_str (die->sect_off),
15772 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15773 return 0;
15774 }
15775
15776 ULONGEST align;
15777 if (attr->form == DW_FORM_sdata)
15778 {
15779 LONGEST val = DW_SND (attr);
15780 if (val < 0)
15781 {
15782 complaint (_("DW_AT_alignment value must not be negative"
15783 " - DIE at %s [in module %s]"),
15784 sect_offset_str (die->sect_off),
15785 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15786 return 0;
15787 }
15788 align = val;
15789 }
15790 else
15791 align = DW_UNSND (attr);
15792
15793 if (align == 0)
15794 {
15795 complaint (_("DW_AT_alignment value must not be zero"
15796 " - DIE at %s [in module %s]"),
15797 sect_offset_str (die->sect_off),
15798 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15799 return 0;
15800 }
15801 if ((align & (align - 1)) != 0)
15802 {
15803 complaint (_("DW_AT_alignment value must be a power of 2"
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
15810 return align;
15811 }
15812
15813 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15814 the alignment for TYPE. */
15815
15816 static void
15817 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15818 struct type *type)
15819 {
15820 if (!set_type_align (type, get_alignment (cu, die)))
15821 complaint (_("DW_AT_alignment value too large"
15822 " - DIE at %s [in module %s]"),
15823 sect_offset_str (die->sect_off),
15824 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15825 }
15826
15827 /* Called when we find the DIE that starts a structure or union scope
15828 (definition) to create a type for the structure or union. Fill in
15829 the type's name and general properties; the members will not be
15830 processed until process_structure_scope. A symbol table entry for
15831 the type will also not be done until process_structure_scope (assuming
15832 the type has a name).
15833
15834 NOTE: we need to call these functions regardless of whether or not the
15835 DIE has a DW_AT_name attribute, since it might be an anonymous
15836 structure or union. This gets the type entered into our set of
15837 user defined types. */
15838
15839 static struct type *
15840 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15841 {
15842 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15843 struct type *type;
15844 struct attribute *attr;
15845 const char *name;
15846
15847 /* If the definition of this type lives in .debug_types, read that type.
15848 Don't follow DW_AT_specification though, that will take us back up
15849 the chain and we want to go down. */
15850 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15851 if (attr != nullptr)
15852 {
15853 type = get_DW_AT_signature_type (die, attr, cu);
15854
15855 /* The type's CU may not be the same as CU.
15856 Ensure TYPE is recorded with CU in die_type_hash. */
15857 return set_die_type (die, type, cu);
15858 }
15859
15860 type = alloc_type (objfile);
15861 INIT_CPLUS_SPECIFIC (type);
15862
15863 name = dwarf2_name (die, cu);
15864 if (name != NULL)
15865 {
15866 if (cu->language == language_cplus
15867 || cu->language == language_d
15868 || cu->language == language_rust)
15869 {
15870 const char *full_name = dwarf2_full_name (name, die, cu);
15871
15872 /* dwarf2_full_name might have already finished building the DIE's
15873 type. If so, there is no need to continue. */
15874 if (get_die_type (die, cu) != NULL)
15875 return get_die_type (die, cu);
15876
15877 TYPE_NAME (type) = full_name;
15878 }
15879 else
15880 {
15881 /* The name is already allocated along with this objfile, so
15882 we don't need to duplicate it for the type. */
15883 TYPE_NAME (type) = name;
15884 }
15885 }
15886
15887 if (die->tag == DW_TAG_structure_type)
15888 {
15889 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15890 }
15891 else if (die->tag == DW_TAG_union_type)
15892 {
15893 TYPE_CODE (type) = TYPE_CODE_UNION;
15894 }
15895 else if (die->tag == DW_TAG_variant_part)
15896 {
15897 TYPE_CODE (type) = TYPE_CODE_UNION;
15898 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15899 }
15900 else
15901 {
15902 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15903 }
15904
15905 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15906 TYPE_DECLARED_CLASS (type) = 1;
15907
15908 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15909 if (attr != nullptr)
15910 {
15911 if (attr_form_is_constant (attr))
15912 TYPE_LENGTH (type) = DW_UNSND (attr);
15913 else
15914 {
15915 /* For the moment, dynamic type sizes are not supported
15916 by GDB's struct type. The actual size is determined
15917 on-demand when resolving the type of a given object,
15918 so set the type's length to zero for now. Otherwise,
15919 we record an expression as the length, and that expression
15920 could lead to a very large value, which could eventually
15921 lead to us trying to allocate that much memory when creating
15922 a value of that type. */
15923 TYPE_LENGTH (type) = 0;
15924 }
15925 }
15926 else
15927 {
15928 TYPE_LENGTH (type) = 0;
15929 }
15930
15931 maybe_set_alignment (cu, die, type);
15932
15933 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15934 {
15935 /* ICC<14 does not output the required DW_AT_declaration on
15936 incomplete types, but gives them a size of zero. */
15937 TYPE_STUB (type) = 1;
15938 }
15939 else
15940 TYPE_STUB_SUPPORTED (type) = 1;
15941
15942 if (die_is_declaration (die, cu))
15943 TYPE_STUB (type) = 1;
15944 else if (attr == NULL && die->child == NULL
15945 && producer_is_realview (cu->producer))
15946 /* RealView does not output the required DW_AT_declaration
15947 on incomplete types. */
15948 TYPE_STUB (type) = 1;
15949
15950 /* We need to add the type field to the die immediately so we don't
15951 infinitely recurse when dealing with pointers to the structure
15952 type within the structure itself. */
15953 set_die_type (die, type, cu);
15954
15955 /* set_die_type should be already done. */
15956 set_descriptive_type (type, die, cu);
15957
15958 return type;
15959 }
15960
15961 /* A helper for process_structure_scope that handles a single member
15962 DIE. */
15963
15964 static void
15965 handle_struct_member_die (struct die_info *child_die, struct type *type,
15966 struct field_info *fi,
15967 std::vector<struct symbol *> *template_args,
15968 struct dwarf2_cu *cu)
15969 {
15970 if (child_die->tag == DW_TAG_member
15971 || child_die->tag == DW_TAG_variable
15972 || child_die->tag == DW_TAG_variant_part)
15973 {
15974 /* NOTE: carlton/2002-11-05: A C++ static data member
15975 should be a DW_TAG_member that is a declaration, but
15976 all versions of G++ as of this writing (so through at
15977 least 3.2.1) incorrectly generate DW_TAG_variable
15978 tags for them instead. */
15979 dwarf2_add_field (fi, child_die, cu);
15980 }
15981 else if (child_die->tag == DW_TAG_subprogram)
15982 {
15983 /* Rust doesn't have member functions in the C++ sense.
15984 However, it does emit ordinary functions as children
15985 of a struct DIE. */
15986 if (cu->language == language_rust)
15987 read_func_scope (child_die, cu);
15988 else
15989 {
15990 /* C++ member function. */
15991 dwarf2_add_member_fn (fi, child_die, type, cu);
15992 }
15993 }
15994 else if (child_die->tag == DW_TAG_inheritance)
15995 {
15996 /* C++ base class field. */
15997 dwarf2_add_field (fi, child_die, cu);
15998 }
15999 else if (type_can_define_types (child_die))
16000 dwarf2_add_type_defn (fi, child_die, cu);
16001 else if (child_die->tag == DW_TAG_template_type_param
16002 || child_die->tag == DW_TAG_template_value_param)
16003 {
16004 struct symbol *arg = new_symbol (child_die, NULL, cu);
16005
16006 if (arg != NULL)
16007 template_args->push_back (arg);
16008 }
16009 else if (child_die->tag == DW_TAG_variant)
16010 {
16011 /* In a variant we want to get the discriminant and also add a
16012 field for our sole member child. */
16013 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16014
16015 for (die_info *variant_child = child_die->child;
16016 variant_child != NULL;
16017 variant_child = sibling_die (variant_child))
16018 {
16019 if (variant_child->tag == DW_TAG_member)
16020 {
16021 handle_struct_member_die (variant_child, type, fi,
16022 template_args, cu);
16023 /* Only handle the one. */
16024 break;
16025 }
16026 }
16027
16028 /* We don't handle this but we might as well report it if we see
16029 it. */
16030 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16031 complaint (_("DW_AT_discr_list is not supported yet"
16032 " - DIE at %s [in module %s]"),
16033 sect_offset_str (child_die->sect_off),
16034 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16035
16036 /* The first field was just added, so we can stash the
16037 discriminant there. */
16038 gdb_assert (!fi->fields.empty ());
16039 if (discr == NULL)
16040 fi->fields.back ().variant.default_branch = true;
16041 else
16042 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16043 }
16044 }
16045
16046 /* Finish creating a structure or union type, including filling in
16047 its members and creating a symbol for it. */
16048
16049 static void
16050 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16051 {
16052 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16053 struct die_info *child_die;
16054 struct type *type;
16055
16056 type = get_die_type (die, cu);
16057 if (type == NULL)
16058 type = read_structure_type (die, cu);
16059
16060 /* When reading a DW_TAG_variant_part, we need to notice when we
16061 read the discriminant member, so we can record it later in the
16062 discriminant_info. */
16063 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16064 sect_offset discr_offset {};
16065 bool has_template_parameters = false;
16066
16067 if (is_variant_part)
16068 {
16069 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16070 if (discr == NULL)
16071 {
16072 /* Maybe it's a univariant form, an extension we support.
16073 In this case arrange not to check the offset. */
16074 is_variant_part = false;
16075 }
16076 else if (attr_form_is_ref (discr))
16077 {
16078 struct dwarf2_cu *target_cu = cu;
16079 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16080
16081 discr_offset = target_die->sect_off;
16082 }
16083 else
16084 {
16085 complaint (_("DW_AT_discr does not have DIE reference form"
16086 " - DIE at %s [in module %s]"),
16087 sect_offset_str (die->sect_off),
16088 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16089 is_variant_part = false;
16090 }
16091 }
16092
16093 if (die->child != NULL && ! die_is_declaration (die, cu))
16094 {
16095 struct field_info fi;
16096 std::vector<struct symbol *> template_args;
16097
16098 child_die = die->child;
16099
16100 while (child_die && child_die->tag)
16101 {
16102 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16103
16104 if (is_variant_part && discr_offset == child_die->sect_off)
16105 fi.fields.back ().variant.is_discriminant = true;
16106
16107 child_die = sibling_die (child_die);
16108 }
16109
16110 /* Attach template arguments to type. */
16111 if (!template_args.empty ())
16112 {
16113 has_template_parameters = true;
16114 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16115 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16116 TYPE_TEMPLATE_ARGUMENTS (type)
16117 = XOBNEWVEC (&objfile->objfile_obstack,
16118 struct symbol *,
16119 TYPE_N_TEMPLATE_ARGUMENTS (type));
16120 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16121 template_args.data (),
16122 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16123 * sizeof (struct symbol *)));
16124 }
16125
16126 /* Attach fields and member functions to the type. */
16127 if (fi.nfields)
16128 dwarf2_attach_fields_to_type (&fi, type, cu);
16129 if (!fi.fnfieldlists.empty ())
16130 {
16131 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16132
16133 /* Get the type which refers to the base class (possibly this
16134 class itself) which contains the vtable pointer for the current
16135 class from the DW_AT_containing_type attribute. This use of
16136 DW_AT_containing_type is a GNU extension. */
16137
16138 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16139 {
16140 struct type *t = die_containing_type (die, cu);
16141
16142 set_type_vptr_basetype (type, t);
16143 if (type == t)
16144 {
16145 int i;
16146
16147 /* Our own class provides vtbl ptr. */
16148 for (i = TYPE_NFIELDS (t) - 1;
16149 i >= TYPE_N_BASECLASSES (t);
16150 --i)
16151 {
16152 const char *fieldname = TYPE_FIELD_NAME (t, i);
16153
16154 if (is_vtable_name (fieldname, cu))
16155 {
16156 set_type_vptr_fieldno (type, i);
16157 break;
16158 }
16159 }
16160
16161 /* Complain if virtual function table field not found. */
16162 if (i < TYPE_N_BASECLASSES (t))
16163 complaint (_("virtual function table pointer "
16164 "not found when defining class '%s'"),
16165 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16166 }
16167 else
16168 {
16169 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16170 }
16171 }
16172 else if (cu->producer
16173 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16174 {
16175 /* The IBM XLC compiler does not provide direct indication
16176 of the containing type, but the vtable pointer is
16177 always named __vfp. */
16178
16179 int i;
16180
16181 for (i = TYPE_NFIELDS (type) - 1;
16182 i >= TYPE_N_BASECLASSES (type);
16183 --i)
16184 {
16185 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16186 {
16187 set_type_vptr_fieldno (type, i);
16188 set_type_vptr_basetype (type, type);
16189 break;
16190 }
16191 }
16192 }
16193 }
16194
16195 /* Copy fi.typedef_field_list linked list elements content into the
16196 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16197 if (!fi.typedef_field_list.empty ())
16198 {
16199 int count = fi.typedef_field_list.size ();
16200
16201 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16202 TYPE_TYPEDEF_FIELD_ARRAY (type)
16203 = ((struct decl_field *)
16204 TYPE_ALLOC (type,
16205 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16206 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16207
16208 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16209 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16210 }
16211
16212 /* Copy fi.nested_types_list linked list elements content into the
16213 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16214 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16215 {
16216 int count = fi.nested_types_list.size ();
16217
16218 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16219 TYPE_NESTED_TYPES_ARRAY (type)
16220 = ((struct decl_field *)
16221 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16222 TYPE_NESTED_TYPES_COUNT (type) = count;
16223
16224 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16225 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16226 }
16227 }
16228
16229 quirk_gcc_member_function_pointer (type, objfile);
16230 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16231 cu->rust_unions.push_back (type);
16232
16233 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16234 snapshots) has been known to create a die giving a declaration
16235 for a class that has, as a child, a die giving a definition for a
16236 nested class. So we have to process our children even if the
16237 current die is a declaration. Normally, of course, a declaration
16238 won't have any children at all. */
16239
16240 child_die = die->child;
16241
16242 while (child_die != NULL && child_die->tag)
16243 {
16244 if (child_die->tag == DW_TAG_member
16245 || child_die->tag == DW_TAG_variable
16246 || child_die->tag == DW_TAG_inheritance
16247 || child_die->tag == DW_TAG_template_value_param
16248 || child_die->tag == DW_TAG_template_type_param)
16249 {
16250 /* Do nothing. */
16251 }
16252 else
16253 process_die (child_die, cu);
16254
16255 child_die = sibling_die (child_die);
16256 }
16257
16258 /* Do not consider external references. According to the DWARF standard,
16259 these DIEs are identified by the fact that they have no byte_size
16260 attribute, and a declaration attribute. */
16261 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16262 || !die_is_declaration (die, cu))
16263 {
16264 struct symbol *sym = new_symbol (die, type, cu);
16265
16266 if (has_template_parameters)
16267 {
16268 struct symtab *symtab;
16269 if (sym != nullptr)
16270 symtab = symbol_symtab (sym);
16271 else if (cu->line_header != nullptr)
16272 {
16273 /* Any related symtab will do. */
16274 symtab
16275 = cu->line_header->file_names ()[0].symtab;
16276 }
16277 else
16278 {
16279 symtab = nullptr;
16280 complaint (_("could not find suitable "
16281 "symtab for template parameter"
16282 " - DIE at %s [in module %s]"),
16283 sect_offset_str (die->sect_off),
16284 objfile_name (objfile));
16285 }
16286
16287 if (symtab != nullptr)
16288 {
16289 /* Make sure that the symtab is set on the new symbols.
16290 Even though they don't appear in this symtab directly,
16291 other parts of gdb assume that symbols do, and this is
16292 reasonably true. */
16293 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16294 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16295 }
16296 }
16297 }
16298 }
16299
16300 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16301 update TYPE using some information only available in DIE's children. */
16302
16303 static void
16304 update_enumeration_type_from_children (struct die_info *die,
16305 struct type *type,
16306 struct dwarf2_cu *cu)
16307 {
16308 struct die_info *child_die;
16309 int unsigned_enum = 1;
16310 int flag_enum = 1;
16311 ULONGEST mask = 0;
16312
16313 auto_obstack obstack;
16314
16315 for (child_die = die->child;
16316 child_die != NULL && child_die->tag;
16317 child_die = sibling_die (child_die))
16318 {
16319 struct attribute *attr;
16320 LONGEST value;
16321 const gdb_byte *bytes;
16322 struct dwarf2_locexpr_baton *baton;
16323 const char *name;
16324
16325 if (child_die->tag != DW_TAG_enumerator)
16326 continue;
16327
16328 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16329 if (attr == NULL)
16330 continue;
16331
16332 name = dwarf2_name (child_die, cu);
16333 if (name == NULL)
16334 name = "<anonymous enumerator>";
16335
16336 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16337 &value, &bytes, &baton);
16338 if (value < 0)
16339 {
16340 unsigned_enum = 0;
16341 flag_enum = 0;
16342 }
16343 else if ((mask & value) != 0)
16344 flag_enum = 0;
16345 else
16346 mask |= value;
16347
16348 /* If we already know that the enum type is neither unsigned, nor
16349 a flag type, no need to look at the rest of the enumerates. */
16350 if (!unsigned_enum && !flag_enum)
16351 break;
16352 }
16353
16354 if (unsigned_enum)
16355 TYPE_UNSIGNED (type) = 1;
16356 if (flag_enum)
16357 TYPE_FLAG_ENUM (type) = 1;
16358 }
16359
16360 /* Given a DW_AT_enumeration_type die, set its type. We do not
16361 complete the type's fields yet, or create any symbols. */
16362
16363 static struct type *
16364 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16365 {
16366 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16367 struct type *type;
16368 struct attribute *attr;
16369 const char *name;
16370
16371 /* If the definition of this type lives in .debug_types, read that type.
16372 Don't follow DW_AT_specification though, that will take us back up
16373 the chain and we want to go down. */
16374 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16375 if (attr != nullptr)
16376 {
16377 type = get_DW_AT_signature_type (die, attr, cu);
16378
16379 /* The type's CU may not be the same as CU.
16380 Ensure TYPE is recorded with CU in die_type_hash. */
16381 return set_die_type (die, type, cu);
16382 }
16383
16384 type = alloc_type (objfile);
16385
16386 TYPE_CODE (type) = TYPE_CODE_ENUM;
16387 name = dwarf2_full_name (NULL, die, cu);
16388 if (name != NULL)
16389 TYPE_NAME (type) = name;
16390
16391 attr = dwarf2_attr (die, DW_AT_type, cu);
16392 if (attr != NULL)
16393 {
16394 struct type *underlying_type = die_type (die, cu);
16395
16396 TYPE_TARGET_TYPE (type) = underlying_type;
16397 }
16398
16399 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16400 if (attr != nullptr)
16401 {
16402 TYPE_LENGTH (type) = DW_UNSND (attr);
16403 }
16404 else
16405 {
16406 TYPE_LENGTH (type) = 0;
16407 }
16408
16409 maybe_set_alignment (cu, die, type);
16410
16411 /* The enumeration DIE can be incomplete. In Ada, any type can be
16412 declared as private in the package spec, and then defined only
16413 inside the package body. Such types are known as Taft Amendment
16414 Types. When another package uses such a type, an incomplete DIE
16415 may be generated by the compiler. */
16416 if (die_is_declaration (die, cu))
16417 TYPE_STUB (type) = 1;
16418
16419 /* Finish the creation of this type by using the enum's children.
16420 We must call this even when the underlying type has been provided
16421 so that we can determine if we're looking at a "flag" enum. */
16422 update_enumeration_type_from_children (die, type, cu);
16423
16424 /* If this type has an underlying type that is not a stub, then we
16425 may use its attributes. We always use the "unsigned" attribute
16426 in this situation, because ordinarily we guess whether the type
16427 is unsigned -- but the guess can be wrong and the underlying type
16428 can tell us the reality. However, we defer to a local size
16429 attribute if one exists, because this lets the compiler override
16430 the underlying type if needed. */
16431 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16432 {
16433 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16434 if (TYPE_LENGTH (type) == 0)
16435 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16436 if (TYPE_RAW_ALIGN (type) == 0
16437 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16438 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16439 }
16440
16441 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16442
16443 return set_die_type (die, type, cu);
16444 }
16445
16446 /* Given a pointer to a die which begins an enumeration, process all
16447 the dies that define the members of the enumeration, and create the
16448 symbol for the enumeration type.
16449
16450 NOTE: We reverse the order of the element list. */
16451
16452 static void
16453 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16454 {
16455 struct type *this_type;
16456
16457 this_type = get_die_type (die, cu);
16458 if (this_type == NULL)
16459 this_type = read_enumeration_type (die, cu);
16460
16461 if (die->child != NULL)
16462 {
16463 struct die_info *child_die;
16464 struct symbol *sym;
16465 struct field *fields = NULL;
16466 int num_fields = 0;
16467 const char *name;
16468
16469 child_die = die->child;
16470 while (child_die && child_die->tag)
16471 {
16472 if (child_die->tag != DW_TAG_enumerator)
16473 {
16474 process_die (child_die, cu);
16475 }
16476 else
16477 {
16478 name = dwarf2_name (child_die, cu);
16479 if (name)
16480 {
16481 sym = new_symbol (child_die, this_type, cu);
16482
16483 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16484 {
16485 fields = (struct field *)
16486 xrealloc (fields,
16487 (num_fields + DW_FIELD_ALLOC_CHUNK)
16488 * sizeof (struct field));
16489 }
16490
16491 FIELD_NAME (fields[num_fields]) = sym->linkage_name ();
16492 FIELD_TYPE (fields[num_fields]) = NULL;
16493 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16494 FIELD_BITSIZE (fields[num_fields]) = 0;
16495
16496 num_fields++;
16497 }
16498 }
16499
16500 child_die = sibling_die (child_die);
16501 }
16502
16503 if (num_fields)
16504 {
16505 TYPE_NFIELDS (this_type) = num_fields;
16506 TYPE_FIELDS (this_type) = (struct field *)
16507 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16508 memcpy (TYPE_FIELDS (this_type), fields,
16509 sizeof (struct field) * num_fields);
16510 xfree (fields);
16511 }
16512 }
16513
16514 /* If we are reading an enum from a .debug_types unit, and the enum
16515 is a declaration, and the enum is not the signatured type in the
16516 unit, then we do not want to add a symbol for it. Adding a
16517 symbol would in some cases obscure the true definition of the
16518 enum, giving users an incomplete type when the definition is
16519 actually available. Note that we do not want to do this for all
16520 enums which are just declarations, because C++0x allows forward
16521 enum declarations. */
16522 if (cu->per_cu->is_debug_types
16523 && die_is_declaration (die, cu))
16524 {
16525 struct signatured_type *sig_type;
16526
16527 sig_type = (struct signatured_type *) cu->per_cu;
16528 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16529 if (sig_type->type_offset_in_section != die->sect_off)
16530 return;
16531 }
16532
16533 new_symbol (die, this_type, cu);
16534 }
16535
16536 /* Extract all information from a DW_TAG_array_type DIE and put it in
16537 the DIE's type field. For now, this only handles one dimensional
16538 arrays. */
16539
16540 static struct type *
16541 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16542 {
16543 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16544 struct die_info *child_die;
16545 struct type *type;
16546 struct type *element_type, *range_type, *index_type;
16547 struct attribute *attr;
16548 const char *name;
16549 struct dynamic_prop *byte_stride_prop = NULL;
16550 unsigned int bit_stride = 0;
16551
16552 element_type = die_type (die, cu);
16553
16554 /* The die_type call above may have already set the type for this DIE. */
16555 type = get_die_type (die, cu);
16556 if (type)
16557 return type;
16558
16559 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16560 if (attr != NULL)
16561 {
16562 int stride_ok;
16563 struct type *prop_type
16564 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16565
16566 byte_stride_prop
16567 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16568 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16569 prop_type);
16570 if (!stride_ok)
16571 {
16572 complaint (_("unable to read array DW_AT_byte_stride "
16573 " - DIE at %s [in module %s]"),
16574 sect_offset_str (die->sect_off),
16575 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16576 /* Ignore this attribute. We will likely not be able to print
16577 arrays of this type correctly, but there is little we can do
16578 to help if we cannot read the attribute's value. */
16579 byte_stride_prop = NULL;
16580 }
16581 }
16582
16583 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16584 if (attr != NULL)
16585 bit_stride = DW_UNSND (attr);
16586
16587 /* Irix 6.2 native cc creates array types without children for
16588 arrays with unspecified length. */
16589 if (die->child == NULL)
16590 {
16591 index_type = objfile_type (objfile)->builtin_int;
16592 range_type = create_static_range_type (NULL, index_type, 0, -1);
16593 type = create_array_type_with_stride (NULL, element_type, range_type,
16594 byte_stride_prop, bit_stride);
16595 return set_die_type (die, type, cu);
16596 }
16597
16598 std::vector<struct type *> range_types;
16599 child_die = die->child;
16600 while (child_die && child_die->tag)
16601 {
16602 if (child_die->tag == DW_TAG_subrange_type)
16603 {
16604 struct type *child_type = read_type_die (child_die, cu);
16605
16606 if (child_type != NULL)
16607 {
16608 /* The range type was succesfully read. Save it for the
16609 array type creation. */
16610 range_types.push_back (child_type);
16611 }
16612 }
16613 child_die = sibling_die (child_die);
16614 }
16615
16616 /* Dwarf2 dimensions are output from left to right, create the
16617 necessary array types in backwards order. */
16618
16619 type = element_type;
16620
16621 if (read_array_order (die, cu) == DW_ORD_col_major)
16622 {
16623 int i = 0;
16624
16625 while (i < range_types.size ())
16626 type = create_array_type_with_stride (NULL, type, range_types[i++],
16627 byte_stride_prop, bit_stride);
16628 }
16629 else
16630 {
16631 size_t ndim = range_types.size ();
16632 while (ndim-- > 0)
16633 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16634 byte_stride_prop, bit_stride);
16635 }
16636
16637 /* Understand Dwarf2 support for vector types (like they occur on
16638 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16639 array type. This is not part of the Dwarf2/3 standard yet, but a
16640 custom vendor extension. The main difference between a regular
16641 array and the vector variant is that vectors are passed by value
16642 to functions. */
16643 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16644 if (attr != nullptr)
16645 make_vector_type (type);
16646
16647 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16648 implementation may choose to implement triple vectors using this
16649 attribute. */
16650 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16651 if (attr != nullptr)
16652 {
16653 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16654 TYPE_LENGTH (type) = DW_UNSND (attr);
16655 else
16656 complaint (_("DW_AT_byte_size for array type smaller "
16657 "than the total size of elements"));
16658 }
16659
16660 name = dwarf2_name (die, cu);
16661 if (name)
16662 TYPE_NAME (type) = name;
16663
16664 maybe_set_alignment (cu, die, type);
16665
16666 /* Install the type in the die. */
16667 set_die_type (die, type, cu);
16668
16669 /* set_die_type should be already done. */
16670 set_descriptive_type (type, die, cu);
16671
16672 return type;
16673 }
16674
16675 static enum dwarf_array_dim_ordering
16676 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16677 {
16678 struct attribute *attr;
16679
16680 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16681
16682 if (attr != nullptr)
16683 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16684
16685 /* GNU F77 is a special case, as at 08/2004 array type info is the
16686 opposite order to the dwarf2 specification, but data is still
16687 laid out as per normal fortran.
16688
16689 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16690 version checking. */
16691
16692 if (cu->language == language_fortran
16693 && cu->producer && strstr (cu->producer, "GNU F77"))
16694 {
16695 return DW_ORD_row_major;
16696 }
16697
16698 switch (cu->language_defn->la_array_ordering)
16699 {
16700 case array_column_major:
16701 return DW_ORD_col_major;
16702 case array_row_major:
16703 default:
16704 return DW_ORD_row_major;
16705 };
16706 }
16707
16708 /* Extract all information from a DW_TAG_set_type DIE and put it in
16709 the DIE's type field. */
16710
16711 static struct type *
16712 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16713 {
16714 struct type *domain_type, *set_type;
16715 struct attribute *attr;
16716
16717 domain_type = die_type (die, cu);
16718
16719 /* The die_type call above may have already set the type for this DIE. */
16720 set_type = get_die_type (die, cu);
16721 if (set_type)
16722 return set_type;
16723
16724 set_type = create_set_type (NULL, domain_type);
16725
16726 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16727 if (attr != nullptr)
16728 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16729
16730 maybe_set_alignment (cu, die, set_type);
16731
16732 return set_die_type (die, set_type, cu);
16733 }
16734
16735 /* A helper for read_common_block that creates a locexpr baton.
16736 SYM is the symbol which we are marking as computed.
16737 COMMON_DIE is the DIE for the common block.
16738 COMMON_LOC is the location expression attribute for the common
16739 block itself.
16740 MEMBER_LOC is the location expression attribute for the particular
16741 member of the common block that we are processing.
16742 CU is the CU from which the above come. */
16743
16744 static void
16745 mark_common_block_symbol_computed (struct symbol *sym,
16746 struct die_info *common_die,
16747 struct attribute *common_loc,
16748 struct attribute *member_loc,
16749 struct dwarf2_cu *cu)
16750 {
16751 struct dwarf2_per_objfile *dwarf2_per_objfile
16752 = cu->per_cu->dwarf2_per_objfile;
16753 struct objfile *objfile = dwarf2_per_objfile->objfile;
16754 struct dwarf2_locexpr_baton *baton;
16755 gdb_byte *ptr;
16756 unsigned int cu_off;
16757 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16758 LONGEST offset = 0;
16759
16760 gdb_assert (common_loc && member_loc);
16761 gdb_assert (attr_form_is_block (common_loc));
16762 gdb_assert (attr_form_is_block (member_loc)
16763 || attr_form_is_constant (member_loc));
16764
16765 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16766 baton->per_cu = cu->per_cu;
16767 gdb_assert (baton->per_cu);
16768
16769 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16770
16771 if (attr_form_is_constant (member_loc))
16772 {
16773 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16774 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16775 }
16776 else
16777 baton->size += DW_BLOCK (member_loc)->size;
16778
16779 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16780 baton->data = ptr;
16781
16782 *ptr++ = DW_OP_call4;
16783 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16784 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16785 ptr += 4;
16786
16787 if (attr_form_is_constant (member_loc))
16788 {
16789 *ptr++ = DW_OP_addr;
16790 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16791 ptr += cu->header.addr_size;
16792 }
16793 else
16794 {
16795 /* We have to copy the data here, because DW_OP_call4 will only
16796 use a DW_AT_location attribute. */
16797 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16798 ptr += DW_BLOCK (member_loc)->size;
16799 }
16800
16801 *ptr++ = DW_OP_plus;
16802 gdb_assert (ptr - baton->data == baton->size);
16803
16804 SYMBOL_LOCATION_BATON (sym) = baton;
16805 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16806 }
16807
16808 /* Create appropriate locally-scoped variables for all the
16809 DW_TAG_common_block entries. Also create a struct common_block
16810 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16811 is used to separate the common blocks name namespace from regular
16812 variable names. */
16813
16814 static void
16815 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16816 {
16817 struct attribute *attr;
16818
16819 attr = dwarf2_attr (die, DW_AT_location, cu);
16820 if (attr != nullptr)
16821 {
16822 /* Support the .debug_loc offsets. */
16823 if (attr_form_is_block (attr))
16824 {
16825 /* Ok. */
16826 }
16827 else if (attr_form_is_section_offset (attr))
16828 {
16829 dwarf2_complex_location_expr_complaint ();
16830 attr = NULL;
16831 }
16832 else
16833 {
16834 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16835 "common block member");
16836 attr = NULL;
16837 }
16838 }
16839
16840 if (die->child != NULL)
16841 {
16842 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16843 struct die_info *child_die;
16844 size_t n_entries = 0, size;
16845 struct common_block *common_block;
16846 struct symbol *sym;
16847
16848 for (child_die = die->child;
16849 child_die && child_die->tag;
16850 child_die = sibling_die (child_die))
16851 ++n_entries;
16852
16853 size = (sizeof (struct common_block)
16854 + (n_entries - 1) * sizeof (struct symbol *));
16855 common_block
16856 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16857 size);
16858 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16859 common_block->n_entries = 0;
16860
16861 for (child_die = die->child;
16862 child_die && child_die->tag;
16863 child_die = sibling_die (child_die))
16864 {
16865 /* Create the symbol in the DW_TAG_common_block block in the current
16866 symbol scope. */
16867 sym = new_symbol (child_die, NULL, cu);
16868 if (sym != NULL)
16869 {
16870 struct attribute *member_loc;
16871
16872 common_block->contents[common_block->n_entries++] = sym;
16873
16874 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16875 cu);
16876 if (member_loc)
16877 {
16878 /* GDB has handled this for a long time, but it is
16879 not specified by DWARF. It seems to have been
16880 emitted by gfortran at least as recently as:
16881 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16882 complaint (_("Variable in common block has "
16883 "DW_AT_data_member_location "
16884 "- DIE at %s [in module %s]"),
16885 sect_offset_str (child_die->sect_off),
16886 objfile_name (objfile));
16887
16888 if (attr_form_is_section_offset (member_loc))
16889 dwarf2_complex_location_expr_complaint ();
16890 else if (attr_form_is_constant (member_loc)
16891 || attr_form_is_block (member_loc))
16892 {
16893 if (attr != nullptr)
16894 mark_common_block_symbol_computed (sym, die, attr,
16895 member_loc, cu);
16896 }
16897 else
16898 dwarf2_complex_location_expr_complaint ();
16899 }
16900 }
16901 }
16902
16903 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16904 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16905 }
16906 }
16907
16908 /* Create a type for a C++ namespace. */
16909
16910 static struct type *
16911 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16912 {
16913 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16914 const char *previous_prefix, *name;
16915 int is_anonymous;
16916 struct type *type;
16917
16918 /* For extensions, reuse the type of the original namespace. */
16919 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16920 {
16921 struct die_info *ext_die;
16922 struct dwarf2_cu *ext_cu = cu;
16923
16924 ext_die = dwarf2_extension (die, &ext_cu);
16925 type = read_type_die (ext_die, ext_cu);
16926
16927 /* EXT_CU may not be the same as CU.
16928 Ensure TYPE is recorded with CU in die_type_hash. */
16929 return set_die_type (die, type, cu);
16930 }
16931
16932 name = namespace_name (die, &is_anonymous, cu);
16933
16934 /* Now build the name of the current namespace. */
16935
16936 previous_prefix = determine_prefix (die, cu);
16937 if (previous_prefix[0] != '\0')
16938 name = typename_concat (&objfile->objfile_obstack,
16939 previous_prefix, name, 0, cu);
16940
16941 /* Create the type. */
16942 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16943
16944 return set_die_type (die, type, cu);
16945 }
16946
16947 /* Read a namespace scope. */
16948
16949 static void
16950 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16951 {
16952 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16953 int is_anonymous;
16954
16955 /* Add a symbol associated to this if we haven't seen the namespace
16956 before. Also, add a using directive if it's an anonymous
16957 namespace. */
16958
16959 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16960 {
16961 struct type *type;
16962
16963 type = read_type_die (die, cu);
16964 new_symbol (die, type, cu);
16965
16966 namespace_name (die, &is_anonymous, cu);
16967 if (is_anonymous)
16968 {
16969 const char *previous_prefix = determine_prefix (die, cu);
16970
16971 std::vector<const char *> excludes;
16972 add_using_directive (using_directives (cu),
16973 previous_prefix, TYPE_NAME (type), NULL,
16974 NULL, excludes, 0, &objfile->objfile_obstack);
16975 }
16976 }
16977
16978 if (die->child != NULL)
16979 {
16980 struct die_info *child_die = die->child;
16981
16982 while (child_die && child_die->tag)
16983 {
16984 process_die (child_die, cu);
16985 child_die = sibling_die (child_die);
16986 }
16987 }
16988 }
16989
16990 /* Read a Fortran module as type. This DIE can be only a declaration used for
16991 imported module. Still we need that type as local Fortran "use ... only"
16992 declaration imports depend on the created type in determine_prefix. */
16993
16994 static struct type *
16995 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16996 {
16997 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16998 const char *module_name;
16999 struct type *type;
17000
17001 module_name = dwarf2_name (die, cu);
17002 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17003
17004 return set_die_type (die, type, cu);
17005 }
17006
17007 /* Read a Fortran module. */
17008
17009 static void
17010 read_module (struct die_info *die, struct dwarf2_cu *cu)
17011 {
17012 struct die_info *child_die = die->child;
17013 struct type *type;
17014
17015 type = read_type_die (die, cu);
17016 new_symbol (die, type, cu);
17017
17018 while (child_die && child_die->tag)
17019 {
17020 process_die (child_die, cu);
17021 child_die = sibling_die (child_die);
17022 }
17023 }
17024
17025 /* Return the name of the namespace represented by DIE. Set
17026 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17027 namespace. */
17028
17029 static const char *
17030 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17031 {
17032 struct die_info *current_die;
17033 const char *name = NULL;
17034
17035 /* Loop through the extensions until we find a name. */
17036
17037 for (current_die = die;
17038 current_die != NULL;
17039 current_die = dwarf2_extension (die, &cu))
17040 {
17041 /* We don't use dwarf2_name here so that we can detect the absence
17042 of a name -> anonymous namespace. */
17043 name = dwarf2_string_attr (die, DW_AT_name, cu);
17044
17045 if (name != NULL)
17046 break;
17047 }
17048
17049 /* Is it an anonymous namespace? */
17050
17051 *is_anonymous = (name == NULL);
17052 if (*is_anonymous)
17053 name = CP_ANONYMOUS_NAMESPACE_STR;
17054
17055 return name;
17056 }
17057
17058 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17059 the user defined type vector. */
17060
17061 static struct type *
17062 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17063 {
17064 struct gdbarch *gdbarch
17065 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17066 struct comp_unit_head *cu_header = &cu->header;
17067 struct type *type;
17068 struct attribute *attr_byte_size;
17069 struct attribute *attr_address_class;
17070 int byte_size, addr_class;
17071 struct type *target_type;
17072
17073 target_type = die_type (die, cu);
17074
17075 /* The die_type call above may have already set the type for this DIE. */
17076 type = get_die_type (die, cu);
17077 if (type)
17078 return type;
17079
17080 type = lookup_pointer_type (target_type);
17081
17082 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17083 if (attr_byte_size)
17084 byte_size = DW_UNSND (attr_byte_size);
17085 else
17086 byte_size = cu_header->addr_size;
17087
17088 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17089 if (attr_address_class)
17090 addr_class = DW_UNSND (attr_address_class);
17091 else
17092 addr_class = DW_ADDR_none;
17093
17094 ULONGEST alignment = get_alignment (cu, die);
17095
17096 /* If the pointer size, alignment, or address class is different
17097 than the default, create a type variant marked as such and set
17098 the length accordingly. */
17099 if (TYPE_LENGTH (type) != byte_size
17100 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17101 && alignment != TYPE_RAW_ALIGN (type))
17102 || addr_class != DW_ADDR_none)
17103 {
17104 if (gdbarch_address_class_type_flags_p (gdbarch))
17105 {
17106 int type_flags;
17107
17108 type_flags = gdbarch_address_class_type_flags
17109 (gdbarch, byte_size, addr_class);
17110 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17111 == 0);
17112 type = make_type_with_address_space (type, type_flags);
17113 }
17114 else if (TYPE_LENGTH (type) != byte_size)
17115 {
17116 complaint (_("invalid pointer size %d"), byte_size);
17117 }
17118 else if (TYPE_RAW_ALIGN (type) != alignment)
17119 {
17120 complaint (_("Invalid DW_AT_alignment"
17121 " - DIE at %s [in module %s]"),
17122 sect_offset_str (die->sect_off),
17123 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17124 }
17125 else
17126 {
17127 /* Should we also complain about unhandled address classes? */
17128 }
17129 }
17130
17131 TYPE_LENGTH (type) = byte_size;
17132 set_type_align (type, alignment);
17133 return set_die_type (die, type, cu);
17134 }
17135
17136 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17137 the user defined type vector. */
17138
17139 static struct type *
17140 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17141 {
17142 struct type *type;
17143 struct type *to_type;
17144 struct type *domain;
17145
17146 to_type = die_type (die, cu);
17147 domain = die_containing_type (die, cu);
17148
17149 /* The calls above may have already set the type for this DIE. */
17150 type = get_die_type (die, cu);
17151 if (type)
17152 return type;
17153
17154 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17155 type = lookup_methodptr_type (to_type);
17156 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17157 {
17158 struct type *new_type
17159 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17160
17161 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17162 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17163 TYPE_VARARGS (to_type));
17164 type = lookup_methodptr_type (new_type);
17165 }
17166 else
17167 type = lookup_memberptr_type (to_type, domain);
17168
17169 return set_die_type (die, type, cu);
17170 }
17171
17172 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17173 the user defined type vector. */
17174
17175 static struct type *
17176 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17177 enum type_code refcode)
17178 {
17179 struct comp_unit_head *cu_header = &cu->header;
17180 struct type *type, *target_type;
17181 struct attribute *attr;
17182
17183 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17184
17185 target_type = die_type (die, cu);
17186
17187 /* The die_type call above may have already set the type for this DIE. */
17188 type = get_die_type (die, cu);
17189 if (type)
17190 return type;
17191
17192 type = lookup_reference_type (target_type, refcode);
17193 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17194 if (attr != nullptr)
17195 {
17196 TYPE_LENGTH (type) = DW_UNSND (attr);
17197 }
17198 else
17199 {
17200 TYPE_LENGTH (type) = cu_header->addr_size;
17201 }
17202 maybe_set_alignment (cu, die, type);
17203 return set_die_type (die, type, cu);
17204 }
17205
17206 /* Add the given cv-qualifiers to the element type of the array. GCC
17207 outputs DWARF type qualifiers that apply to an array, not the
17208 element type. But GDB relies on the array element type to carry
17209 the cv-qualifiers. This mimics section 6.7.3 of the C99
17210 specification. */
17211
17212 static struct type *
17213 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17214 struct type *base_type, int cnst, int voltl)
17215 {
17216 struct type *el_type, *inner_array;
17217
17218 base_type = copy_type (base_type);
17219 inner_array = base_type;
17220
17221 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17222 {
17223 TYPE_TARGET_TYPE (inner_array) =
17224 copy_type (TYPE_TARGET_TYPE (inner_array));
17225 inner_array = TYPE_TARGET_TYPE (inner_array);
17226 }
17227
17228 el_type = TYPE_TARGET_TYPE (inner_array);
17229 cnst |= TYPE_CONST (el_type);
17230 voltl |= TYPE_VOLATILE (el_type);
17231 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17232
17233 return set_die_type (die, base_type, cu);
17234 }
17235
17236 static struct type *
17237 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17238 {
17239 struct type *base_type, *cv_type;
17240
17241 base_type = die_type (die, cu);
17242
17243 /* The die_type call above may have already set the type for this DIE. */
17244 cv_type = get_die_type (die, cu);
17245 if (cv_type)
17246 return cv_type;
17247
17248 /* In case the const qualifier is applied to an array type, the element type
17249 is so qualified, not the array type (section 6.7.3 of C99). */
17250 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17251 return add_array_cv_type (die, cu, base_type, 1, 0);
17252
17253 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17254 return set_die_type (die, cv_type, cu);
17255 }
17256
17257 static struct type *
17258 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17259 {
17260 struct type *base_type, *cv_type;
17261
17262 base_type = die_type (die, cu);
17263
17264 /* The die_type call above may have already set the type for this DIE. */
17265 cv_type = get_die_type (die, cu);
17266 if (cv_type)
17267 return cv_type;
17268
17269 /* In case the volatile qualifier is applied to an array type, the
17270 element type is so qualified, not the array type (section 6.7.3
17271 of C99). */
17272 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17273 return add_array_cv_type (die, cu, base_type, 0, 1);
17274
17275 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17276 return set_die_type (die, cv_type, cu);
17277 }
17278
17279 /* Handle DW_TAG_restrict_type. */
17280
17281 static struct type *
17282 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17283 {
17284 struct type *base_type, *cv_type;
17285
17286 base_type = die_type (die, cu);
17287
17288 /* The die_type call above may have already set the type for this DIE. */
17289 cv_type = get_die_type (die, cu);
17290 if (cv_type)
17291 return cv_type;
17292
17293 cv_type = make_restrict_type (base_type);
17294 return set_die_type (die, cv_type, cu);
17295 }
17296
17297 /* Handle DW_TAG_atomic_type. */
17298
17299 static struct type *
17300 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17301 {
17302 struct type *base_type, *cv_type;
17303
17304 base_type = die_type (die, cu);
17305
17306 /* The die_type call above may have already set the type for this DIE. */
17307 cv_type = get_die_type (die, cu);
17308 if (cv_type)
17309 return cv_type;
17310
17311 cv_type = make_atomic_type (base_type);
17312 return set_die_type (die, cv_type, cu);
17313 }
17314
17315 /* Extract all information from a DW_TAG_string_type DIE and add to
17316 the user defined type vector. It isn't really a user defined type,
17317 but it behaves like one, with other DIE's using an AT_user_def_type
17318 attribute to reference it. */
17319
17320 static struct type *
17321 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17322 {
17323 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17324 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17325 struct type *type, *range_type, *index_type, *char_type;
17326 struct attribute *attr;
17327 struct dynamic_prop prop;
17328 bool length_is_constant = true;
17329 LONGEST length;
17330
17331 /* There are a couple of places where bit sizes might be made use of
17332 when parsing a DW_TAG_string_type, however, no producer that we know
17333 of make use of these. Handling bit sizes that are a multiple of the
17334 byte size is easy enough, but what about other bit sizes? Lets deal
17335 with that problem when we have to. Warn about these attributes being
17336 unsupported, then parse the type and ignore them like we always
17337 have. */
17338 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17339 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17340 {
17341 static bool warning_printed = false;
17342 if (!warning_printed)
17343 {
17344 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17345 "currently supported on DW_TAG_string_type."));
17346 warning_printed = true;
17347 }
17348 }
17349
17350 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17351 if (attr != nullptr && !attr_form_is_constant (attr))
17352 {
17353 /* The string length describes the location at which the length of
17354 the string can be found. The size of the length field can be
17355 specified with one of the attributes below. */
17356 struct type *prop_type;
17357 struct attribute *len
17358 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17359 if (len == nullptr)
17360 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17361 if (len != nullptr && attr_form_is_constant (len))
17362 {
17363 /* Pass 0 as the default as we know this attribute is constant
17364 and the default value will not be returned. */
17365 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17366 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17367 }
17368 else
17369 {
17370 /* If the size is not specified then we assume it is the size of
17371 an address on this target. */
17372 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17373 }
17374
17375 /* Convert the attribute into a dynamic property. */
17376 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17377 length = 1;
17378 else
17379 length_is_constant = false;
17380 }
17381 else if (attr != nullptr)
17382 {
17383 /* This DW_AT_string_length just contains the length with no
17384 indirection. There's no need to create a dynamic property in this
17385 case. Pass 0 for the default value as we know it will not be
17386 returned in this case. */
17387 length = dwarf2_get_attr_constant_value (attr, 0);
17388 }
17389 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17390 {
17391 /* We don't currently support non-constant byte sizes for strings. */
17392 length = dwarf2_get_attr_constant_value (attr, 1);
17393 }
17394 else
17395 {
17396 /* Use 1 as a fallback length if we have nothing else. */
17397 length = 1;
17398 }
17399
17400 index_type = objfile_type (objfile)->builtin_int;
17401 if (length_is_constant)
17402 range_type = create_static_range_type (NULL, index_type, 1, length);
17403 else
17404 {
17405 struct dynamic_prop low_bound;
17406
17407 low_bound.kind = PROP_CONST;
17408 low_bound.data.const_val = 1;
17409 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17410 }
17411 char_type = language_string_char_type (cu->language_defn, gdbarch);
17412 type = create_string_type (NULL, char_type, range_type);
17413
17414 return set_die_type (die, type, cu);
17415 }
17416
17417 /* Assuming that DIE corresponds to a function, returns nonzero
17418 if the function is prototyped. */
17419
17420 static int
17421 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17422 {
17423 struct attribute *attr;
17424
17425 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17426 if (attr && (DW_UNSND (attr) != 0))
17427 return 1;
17428
17429 /* The DWARF standard implies that the DW_AT_prototyped attribute
17430 is only meaningful for C, but the concept also extends to other
17431 languages that allow unprototyped functions (Eg: Objective C).
17432 For all other languages, assume that functions are always
17433 prototyped. */
17434 if (cu->language != language_c
17435 && cu->language != language_objc
17436 && cu->language != language_opencl)
17437 return 1;
17438
17439 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17440 prototyped and unprototyped functions; default to prototyped,
17441 since that is more common in modern code (and RealView warns
17442 about unprototyped functions). */
17443 if (producer_is_realview (cu->producer))
17444 return 1;
17445
17446 return 0;
17447 }
17448
17449 /* Handle DIES due to C code like:
17450
17451 struct foo
17452 {
17453 int (*funcp)(int a, long l);
17454 int b;
17455 };
17456
17457 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17458
17459 static struct type *
17460 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17461 {
17462 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17463 struct type *type; /* Type that this function returns. */
17464 struct type *ftype; /* Function that returns above type. */
17465 struct attribute *attr;
17466
17467 type = die_type (die, cu);
17468
17469 /* The die_type call above may have already set the type for this DIE. */
17470 ftype = get_die_type (die, cu);
17471 if (ftype)
17472 return ftype;
17473
17474 ftype = lookup_function_type (type);
17475
17476 if (prototyped_function_p (die, cu))
17477 TYPE_PROTOTYPED (ftype) = 1;
17478
17479 /* Store the calling convention in the type if it's available in
17480 the subroutine die. Otherwise set the calling convention to
17481 the default value DW_CC_normal. */
17482 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17483 if (attr != nullptr)
17484 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17485 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17486 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17487 else
17488 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17489
17490 /* Record whether the function returns normally to its caller or not
17491 if the DWARF producer set that information. */
17492 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17493 if (attr && (DW_UNSND (attr) != 0))
17494 TYPE_NO_RETURN (ftype) = 1;
17495
17496 /* We need to add the subroutine type to the die immediately so
17497 we don't infinitely recurse when dealing with parameters
17498 declared as the same subroutine type. */
17499 set_die_type (die, ftype, cu);
17500
17501 if (die->child != NULL)
17502 {
17503 struct type *void_type = objfile_type (objfile)->builtin_void;
17504 struct die_info *child_die;
17505 int nparams, iparams;
17506
17507 /* Count the number of parameters.
17508 FIXME: GDB currently ignores vararg functions, but knows about
17509 vararg member functions. */
17510 nparams = 0;
17511 child_die = die->child;
17512 while (child_die && child_die->tag)
17513 {
17514 if (child_die->tag == DW_TAG_formal_parameter)
17515 nparams++;
17516 else if (child_die->tag == DW_TAG_unspecified_parameters)
17517 TYPE_VARARGS (ftype) = 1;
17518 child_die = sibling_die (child_die);
17519 }
17520
17521 /* Allocate storage for parameters and fill them in. */
17522 TYPE_NFIELDS (ftype) = nparams;
17523 TYPE_FIELDS (ftype) = (struct field *)
17524 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17525
17526 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17527 even if we error out during the parameters reading below. */
17528 for (iparams = 0; iparams < nparams; iparams++)
17529 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17530
17531 iparams = 0;
17532 child_die = die->child;
17533 while (child_die && child_die->tag)
17534 {
17535 if (child_die->tag == DW_TAG_formal_parameter)
17536 {
17537 struct type *arg_type;
17538
17539 /* DWARF version 2 has no clean way to discern C++
17540 static and non-static member functions. G++ helps
17541 GDB by marking the first parameter for non-static
17542 member functions (which is the this pointer) as
17543 artificial. We pass this information to
17544 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17545
17546 DWARF version 3 added DW_AT_object_pointer, which GCC
17547 4.5 does not yet generate. */
17548 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17549 if (attr != nullptr)
17550 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17551 else
17552 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17553 arg_type = die_type (child_die, cu);
17554
17555 /* RealView does not mark THIS as const, which the testsuite
17556 expects. GCC marks THIS as const in method definitions,
17557 but not in the class specifications (GCC PR 43053). */
17558 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17559 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17560 {
17561 int is_this = 0;
17562 struct dwarf2_cu *arg_cu = cu;
17563 const char *name = dwarf2_name (child_die, cu);
17564
17565 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17566 if (attr != nullptr)
17567 {
17568 /* If the compiler emits this, use it. */
17569 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17570 is_this = 1;
17571 }
17572 else if (name && strcmp (name, "this") == 0)
17573 /* Function definitions will have the argument names. */
17574 is_this = 1;
17575 else if (name == NULL && iparams == 0)
17576 /* Declarations may not have the names, so like
17577 elsewhere in GDB, assume an artificial first
17578 argument is "this". */
17579 is_this = 1;
17580
17581 if (is_this)
17582 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17583 arg_type, 0);
17584 }
17585
17586 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17587 iparams++;
17588 }
17589 child_die = sibling_die (child_die);
17590 }
17591 }
17592
17593 return ftype;
17594 }
17595
17596 static struct type *
17597 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17598 {
17599 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17600 const char *name = NULL;
17601 struct type *this_type, *target_type;
17602
17603 name = dwarf2_full_name (NULL, die, cu);
17604 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17605 TYPE_TARGET_STUB (this_type) = 1;
17606 set_die_type (die, this_type, cu);
17607 target_type = die_type (die, cu);
17608 if (target_type != this_type)
17609 TYPE_TARGET_TYPE (this_type) = target_type;
17610 else
17611 {
17612 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17613 spec and cause infinite loops in GDB. */
17614 complaint (_("Self-referential DW_TAG_typedef "
17615 "- DIE at %s [in module %s]"),
17616 sect_offset_str (die->sect_off), objfile_name (objfile));
17617 TYPE_TARGET_TYPE (this_type) = NULL;
17618 }
17619 return this_type;
17620 }
17621
17622 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17623 (which may be different from NAME) to the architecture back-end to allow
17624 it to guess the correct format if necessary. */
17625
17626 static struct type *
17627 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17628 const char *name_hint, enum bfd_endian byte_order)
17629 {
17630 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17631 const struct floatformat **format;
17632 struct type *type;
17633
17634 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17635 if (format)
17636 type = init_float_type (objfile, bits, name, format, byte_order);
17637 else
17638 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17639
17640 return type;
17641 }
17642
17643 /* Allocate an integer type of size BITS and name NAME. */
17644
17645 static struct type *
17646 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17647 int bits, int unsigned_p, const char *name)
17648 {
17649 struct type *type;
17650
17651 /* Versions of Intel's C Compiler generate an integer type called "void"
17652 instead of using DW_TAG_unspecified_type. This has been seen on
17653 at least versions 14, 17, and 18. */
17654 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17655 && strcmp (name, "void") == 0)
17656 type = objfile_type (objfile)->builtin_void;
17657 else
17658 type = init_integer_type (objfile, bits, unsigned_p, name);
17659
17660 return type;
17661 }
17662
17663 /* Initialise and return a floating point type of size BITS suitable for
17664 use as a component of a complex number. The NAME_HINT is passed through
17665 when initialising the floating point type and is the name of the complex
17666 type.
17667
17668 As DWARF doesn't currently provide an explicit name for the components
17669 of a complex number, but it can be helpful to have these components
17670 named, we try to select a suitable name based on the size of the
17671 component. */
17672 static struct type *
17673 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17674 struct objfile *objfile,
17675 int bits, const char *name_hint,
17676 enum bfd_endian byte_order)
17677 {
17678 gdbarch *gdbarch = get_objfile_arch (objfile);
17679 struct type *tt = nullptr;
17680
17681 /* Try to find a suitable floating point builtin type of size BITS.
17682 We're going to use the name of this type as the name for the complex
17683 target type that we are about to create. */
17684 switch (cu->language)
17685 {
17686 case language_fortran:
17687 switch (bits)
17688 {
17689 case 32:
17690 tt = builtin_f_type (gdbarch)->builtin_real;
17691 break;
17692 case 64:
17693 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17694 break;
17695 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17696 case 128:
17697 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17698 break;
17699 }
17700 break;
17701 default:
17702 switch (bits)
17703 {
17704 case 32:
17705 tt = builtin_type (gdbarch)->builtin_float;
17706 break;
17707 case 64:
17708 tt = builtin_type (gdbarch)->builtin_double;
17709 break;
17710 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17711 case 128:
17712 tt = builtin_type (gdbarch)->builtin_long_double;
17713 break;
17714 }
17715 break;
17716 }
17717
17718 /* If the type we found doesn't match the size we were looking for, then
17719 pretend we didn't find a type at all, the complex target type we
17720 create will then be nameless. */
17721 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17722 tt = nullptr;
17723
17724 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17725 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17726 }
17727
17728 /* Find a representation of a given base type and install
17729 it in the TYPE field of the die. */
17730
17731 static struct type *
17732 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17733 {
17734 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17735 struct type *type;
17736 struct attribute *attr;
17737 int encoding = 0, bits = 0;
17738 const char *name;
17739 gdbarch *arch;
17740
17741 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17742 if (attr != nullptr)
17743 encoding = DW_UNSND (attr);
17744 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17745 if (attr != nullptr)
17746 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17747 name = dwarf2_name (die, cu);
17748 if (!name)
17749 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17750
17751 arch = get_objfile_arch (objfile);
17752 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17753
17754 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17755 if (attr)
17756 {
17757 int endianity = DW_UNSND (attr);
17758
17759 switch (endianity)
17760 {
17761 case DW_END_big:
17762 byte_order = BFD_ENDIAN_BIG;
17763 break;
17764 case DW_END_little:
17765 byte_order = BFD_ENDIAN_LITTLE;
17766 break;
17767 default:
17768 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17769 break;
17770 }
17771 }
17772
17773 switch (encoding)
17774 {
17775 case DW_ATE_address:
17776 /* Turn DW_ATE_address into a void * pointer. */
17777 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17778 type = init_pointer_type (objfile, bits, name, type);
17779 break;
17780 case DW_ATE_boolean:
17781 type = init_boolean_type (objfile, bits, 1, name);
17782 break;
17783 case DW_ATE_complex_float:
17784 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17785 byte_order);
17786 type = init_complex_type (objfile, name, type);
17787 break;
17788 case DW_ATE_decimal_float:
17789 type = init_decfloat_type (objfile, bits, name);
17790 break;
17791 case DW_ATE_float:
17792 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17793 break;
17794 case DW_ATE_signed:
17795 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17796 break;
17797 case DW_ATE_unsigned:
17798 if (cu->language == language_fortran
17799 && name
17800 && startswith (name, "character("))
17801 type = init_character_type (objfile, bits, 1, name);
17802 else
17803 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17804 break;
17805 case DW_ATE_signed_char:
17806 if (cu->language == language_ada || cu->language == language_m2
17807 || cu->language == language_pascal
17808 || cu->language == language_fortran)
17809 type = init_character_type (objfile, bits, 0, name);
17810 else
17811 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17812 break;
17813 case DW_ATE_unsigned_char:
17814 if (cu->language == language_ada || cu->language == language_m2
17815 || cu->language == language_pascal
17816 || cu->language == language_fortran
17817 || cu->language == language_rust)
17818 type = init_character_type (objfile, bits, 1, name);
17819 else
17820 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17821 break;
17822 case DW_ATE_UTF:
17823 {
17824 if (bits == 16)
17825 type = builtin_type (arch)->builtin_char16;
17826 else if (bits == 32)
17827 type = builtin_type (arch)->builtin_char32;
17828 else
17829 {
17830 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17831 bits);
17832 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17833 }
17834 return set_die_type (die, type, cu);
17835 }
17836 break;
17837
17838 default:
17839 complaint (_("unsupported DW_AT_encoding: '%s'"),
17840 dwarf_type_encoding_name (encoding));
17841 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17842 break;
17843 }
17844
17845 if (name && strcmp (name, "char") == 0)
17846 TYPE_NOSIGN (type) = 1;
17847
17848 maybe_set_alignment (cu, die, type);
17849
17850 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17851
17852 return set_die_type (die, type, cu);
17853 }
17854
17855 /* Parse dwarf attribute if it's a block, reference or constant and put the
17856 resulting value of the attribute into struct bound_prop.
17857 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17858
17859 static int
17860 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17861 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17862 struct type *default_type)
17863 {
17864 struct dwarf2_property_baton *baton;
17865 struct obstack *obstack
17866 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17867
17868 gdb_assert (default_type != NULL);
17869
17870 if (attr == NULL || prop == NULL)
17871 return 0;
17872
17873 if (attr_form_is_block (attr))
17874 {
17875 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17876 baton->property_type = default_type;
17877 baton->locexpr.per_cu = cu->per_cu;
17878 baton->locexpr.size = DW_BLOCK (attr)->size;
17879 baton->locexpr.data = DW_BLOCK (attr)->data;
17880 switch (attr->name)
17881 {
17882 case DW_AT_string_length:
17883 baton->locexpr.is_reference = true;
17884 break;
17885 default:
17886 baton->locexpr.is_reference = false;
17887 break;
17888 }
17889 prop->data.baton = baton;
17890 prop->kind = PROP_LOCEXPR;
17891 gdb_assert (prop->data.baton != NULL);
17892 }
17893 else if (attr_form_is_ref (attr))
17894 {
17895 struct dwarf2_cu *target_cu = cu;
17896 struct die_info *target_die;
17897 struct attribute *target_attr;
17898
17899 target_die = follow_die_ref (die, attr, &target_cu);
17900 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17901 if (target_attr == NULL)
17902 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17903 target_cu);
17904 if (target_attr == NULL)
17905 return 0;
17906
17907 switch (target_attr->name)
17908 {
17909 case DW_AT_location:
17910 if (attr_form_is_section_offset (target_attr))
17911 {
17912 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17913 baton->property_type = die_type (target_die, target_cu);
17914 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17915 prop->data.baton = baton;
17916 prop->kind = PROP_LOCLIST;
17917 gdb_assert (prop->data.baton != NULL);
17918 }
17919 else if (attr_form_is_block (target_attr))
17920 {
17921 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17922 baton->property_type = die_type (target_die, target_cu);
17923 baton->locexpr.per_cu = cu->per_cu;
17924 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17925 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17926 baton->locexpr.is_reference = true;
17927 prop->data.baton = baton;
17928 prop->kind = PROP_LOCEXPR;
17929 gdb_assert (prop->data.baton != NULL);
17930 }
17931 else
17932 {
17933 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17934 "dynamic property");
17935 return 0;
17936 }
17937 break;
17938 case DW_AT_data_member_location:
17939 {
17940 LONGEST offset;
17941
17942 if (!handle_data_member_location (target_die, target_cu,
17943 &offset))
17944 return 0;
17945
17946 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17947 baton->property_type = read_type_die (target_die->parent,
17948 target_cu);
17949 baton->offset_info.offset = offset;
17950 baton->offset_info.type = die_type (target_die, target_cu);
17951 prop->data.baton = baton;
17952 prop->kind = PROP_ADDR_OFFSET;
17953 break;
17954 }
17955 }
17956 }
17957 else if (attr_form_is_constant (attr))
17958 {
17959 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17960 prop->kind = PROP_CONST;
17961 }
17962 else
17963 {
17964 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17965 dwarf2_name (die, cu));
17966 return 0;
17967 }
17968
17969 return 1;
17970 }
17971
17972 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17973 UNSIGNED_P controls if the integer is unsigned or not. */
17974
17975 static struct type *
17976 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17977 int size_in_bytes, bool unsigned_p)
17978 {
17979 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17980 struct type *int_type;
17981
17982 /* Helper macro to examine the various builtin types. */
17983 #define TRY_TYPE(F) \
17984 int_type = (unsigned_p \
17985 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17986 : objfile_type (objfile)->builtin_ ## F); \
17987 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17988 return int_type
17989
17990 TRY_TYPE (char);
17991 TRY_TYPE (short);
17992 TRY_TYPE (int);
17993 TRY_TYPE (long);
17994 TRY_TYPE (long_long);
17995
17996 #undef TRY_TYPE
17997
17998 gdb_assert_not_reached ("unable to find suitable integer type");
17999 }
18000
18001 /* Find an integer type the same size as the address size given in the
18002 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18003 is unsigned or not. */
18004
18005 static struct type *
18006 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18007 bool unsigned_p)
18008 {
18009 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18010 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18011 }
18012
18013 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18014 present (which is valid) then compute the default type based on the
18015 compilation units address size. */
18016
18017 static struct type *
18018 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18019 {
18020 struct type *index_type = die_type (die, cu);
18021
18022 /* Dwarf-2 specifications explicitly allows to create subrange types
18023 without specifying a base type.
18024 In that case, the base type must be set to the type of
18025 the lower bound, upper bound or count, in that order, if any of these
18026 three attributes references an object that has a type.
18027 If no base type is found, the Dwarf-2 specifications say that
18028 a signed integer type of size equal to the size of an address should
18029 be used.
18030 For the following C code: `extern char gdb_int [];'
18031 GCC produces an empty range DIE.
18032 FIXME: muller/2010-05-28: Possible references to object for low bound,
18033 high bound or count are not yet handled by this code. */
18034 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
18035 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18036
18037 return index_type;
18038 }
18039
18040 /* Read the given DW_AT_subrange DIE. */
18041
18042 static struct type *
18043 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18044 {
18045 struct type *base_type, *orig_base_type;
18046 struct type *range_type;
18047 struct attribute *attr;
18048 struct dynamic_prop low, high;
18049 int low_default_is_valid;
18050 int high_bound_is_count = 0;
18051 const char *name;
18052 ULONGEST negative_mask;
18053
18054 orig_base_type = read_subrange_index_type (die, cu);
18055
18056 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18057 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18058 creating the range type, but we use the result of check_typedef
18059 when examining properties of the type. */
18060 base_type = check_typedef (orig_base_type);
18061
18062 /* The die_type call above may have already set the type for this DIE. */
18063 range_type = get_die_type (die, cu);
18064 if (range_type)
18065 return range_type;
18066
18067 low.kind = PROP_CONST;
18068 high.kind = PROP_CONST;
18069 high.data.const_val = 0;
18070
18071 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18072 omitting DW_AT_lower_bound. */
18073 switch (cu->language)
18074 {
18075 case language_c:
18076 case language_cplus:
18077 low.data.const_val = 0;
18078 low_default_is_valid = 1;
18079 break;
18080 case language_fortran:
18081 low.data.const_val = 1;
18082 low_default_is_valid = 1;
18083 break;
18084 case language_d:
18085 case language_objc:
18086 case language_rust:
18087 low.data.const_val = 0;
18088 low_default_is_valid = (cu->header.version >= 4);
18089 break;
18090 case language_ada:
18091 case language_m2:
18092 case language_pascal:
18093 low.data.const_val = 1;
18094 low_default_is_valid = (cu->header.version >= 4);
18095 break;
18096 default:
18097 low.data.const_val = 0;
18098 low_default_is_valid = 0;
18099 break;
18100 }
18101
18102 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18103 if (attr != nullptr)
18104 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18105 else if (!low_default_is_valid)
18106 complaint (_("Missing DW_AT_lower_bound "
18107 "- DIE at %s [in module %s]"),
18108 sect_offset_str (die->sect_off),
18109 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18110
18111 struct attribute *attr_ub, *attr_count;
18112 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18113 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18114 {
18115 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18116 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18117 {
18118 /* If bounds are constant do the final calculation here. */
18119 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18120 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18121 else
18122 high_bound_is_count = 1;
18123 }
18124 else
18125 {
18126 if (attr_ub != NULL)
18127 complaint (_("Unresolved DW_AT_upper_bound "
18128 "- DIE at %s [in module %s]"),
18129 sect_offset_str (die->sect_off),
18130 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18131 if (attr_count != NULL)
18132 complaint (_("Unresolved DW_AT_count "
18133 "- DIE at %s [in module %s]"),
18134 sect_offset_str (die->sect_off),
18135 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18136 }
18137 }
18138
18139 LONGEST bias = 0;
18140 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18141 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18142 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18143
18144 /* Normally, the DWARF producers are expected to use a signed
18145 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18146 But this is unfortunately not always the case, as witnessed
18147 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18148 is used instead. To work around that ambiguity, we treat
18149 the bounds as signed, and thus sign-extend their values, when
18150 the base type is signed. */
18151 negative_mask =
18152 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18153 if (low.kind == PROP_CONST
18154 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18155 low.data.const_val |= negative_mask;
18156 if (high.kind == PROP_CONST
18157 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18158 high.data.const_val |= negative_mask;
18159
18160 /* Check for bit and byte strides. */
18161 struct dynamic_prop byte_stride_prop;
18162 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18163 if (attr_byte_stride != nullptr)
18164 {
18165 struct type *prop_type
18166 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18167 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18168 prop_type);
18169 }
18170
18171 struct dynamic_prop bit_stride_prop;
18172 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18173 if (attr_bit_stride != nullptr)
18174 {
18175 /* It only makes sense to have either a bit or byte stride. */
18176 if (attr_byte_stride != nullptr)
18177 {
18178 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18179 "- DIE at %s [in module %s]"),
18180 sect_offset_str (die->sect_off),
18181 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18182 attr_bit_stride = nullptr;
18183 }
18184 else
18185 {
18186 struct type *prop_type
18187 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18188 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18189 prop_type);
18190 }
18191 }
18192
18193 if (attr_byte_stride != nullptr
18194 || attr_bit_stride != nullptr)
18195 {
18196 bool byte_stride_p = (attr_byte_stride != nullptr);
18197 struct dynamic_prop *stride
18198 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18199
18200 range_type
18201 = create_range_type_with_stride (NULL, orig_base_type, &low,
18202 &high, bias, stride, byte_stride_p);
18203 }
18204 else
18205 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18206
18207 if (high_bound_is_count)
18208 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18209
18210 /* Ada expects an empty array on no boundary attributes. */
18211 if (attr == NULL && cu->language != language_ada)
18212 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18213
18214 name = dwarf2_name (die, cu);
18215 if (name)
18216 TYPE_NAME (range_type) = name;
18217
18218 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18219 if (attr != nullptr)
18220 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18221
18222 maybe_set_alignment (cu, die, range_type);
18223
18224 set_die_type (die, range_type, cu);
18225
18226 /* set_die_type should be already done. */
18227 set_descriptive_type (range_type, die, cu);
18228
18229 return range_type;
18230 }
18231
18232 static struct type *
18233 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18234 {
18235 struct type *type;
18236
18237 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18238 NULL);
18239 TYPE_NAME (type) = dwarf2_name (die, cu);
18240
18241 /* In Ada, an unspecified type is typically used when the description
18242 of the type is deferred to a different unit. When encountering
18243 such a type, we treat it as a stub, and try to resolve it later on,
18244 when needed. */
18245 if (cu->language == language_ada)
18246 TYPE_STUB (type) = 1;
18247
18248 return set_die_type (die, type, cu);
18249 }
18250
18251 /* Read a single die and all its descendents. Set the die's sibling
18252 field to NULL; set other fields in the die correctly, and set all
18253 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18254 location of the info_ptr after reading all of those dies. PARENT
18255 is the parent of the die in question. */
18256
18257 static struct die_info *
18258 read_die_and_children (const struct die_reader_specs *reader,
18259 const gdb_byte *info_ptr,
18260 const gdb_byte **new_info_ptr,
18261 struct die_info *parent)
18262 {
18263 struct die_info *die;
18264 const gdb_byte *cur_ptr;
18265 int has_children;
18266
18267 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18268 if (die == NULL)
18269 {
18270 *new_info_ptr = cur_ptr;
18271 return NULL;
18272 }
18273 store_in_ref_table (die, reader->cu);
18274
18275 if (has_children)
18276 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18277 else
18278 {
18279 die->child = NULL;
18280 *new_info_ptr = cur_ptr;
18281 }
18282
18283 die->sibling = NULL;
18284 die->parent = parent;
18285 return die;
18286 }
18287
18288 /* Read a die, all of its descendents, and all of its siblings; set
18289 all of the fields of all of the dies correctly. Arguments are as
18290 in read_die_and_children. */
18291
18292 static struct die_info *
18293 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18294 const gdb_byte *info_ptr,
18295 const gdb_byte **new_info_ptr,
18296 struct die_info *parent)
18297 {
18298 struct die_info *first_die, *last_sibling;
18299 const gdb_byte *cur_ptr;
18300
18301 cur_ptr = info_ptr;
18302 first_die = last_sibling = NULL;
18303
18304 while (1)
18305 {
18306 struct die_info *die
18307 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18308
18309 if (die == NULL)
18310 {
18311 *new_info_ptr = cur_ptr;
18312 return first_die;
18313 }
18314
18315 if (!first_die)
18316 first_die = die;
18317 else
18318 last_sibling->sibling = die;
18319
18320 last_sibling = die;
18321 }
18322 }
18323
18324 /* Read a die, all of its descendents, and all of its siblings; set
18325 all of the fields of all of the dies correctly. Arguments are as
18326 in read_die_and_children.
18327 This the main entry point for reading a DIE and all its children. */
18328
18329 static struct die_info *
18330 read_die_and_siblings (const struct die_reader_specs *reader,
18331 const gdb_byte *info_ptr,
18332 const gdb_byte **new_info_ptr,
18333 struct die_info *parent)
18334 {
18335 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18336 new_info_ptr, parent);
18337
18338 if (dwarf_die_debug)
18339 {
18340 fprintf_unfiltered (gdb_stdlog,
18341 "Read die from %s@0x%x of %s:\n",
18342 get_section_name (reader->die_section),
18343 (unsigned) (info_ptr - reader->die_section->buffer),
18344 bfd_get_filename (reader->abfd));
18345 dump_die (die, dwarf_die_debug);
18346 }
18347
18348 return die;
18349 }
18350
18351 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18352 attributes.
18353 The caller is responsible for filling in the extra attributes
18354 and updating (*DIEP)->num_attrs.
18355 Set DIEP to point to a newly allocated die with its information,
18356 except for its child, sibling, and parent fields.
18357 Set HAS_CHILDREN to tell whether the die has children or not. */
18358
18359 static const gdb_byte *
18360 read_full_die_1 (const struct die_reader_specs *reader,
18361 struct die_info **diep, const gdb_byte *info_ptr,
18362 int *has_children, int num_extra_attrs)
18363 {
18364 unsigned int abbrev_number, bytes_read, i;
18365 struct abbrev_info *abbrev;
18366 struct die_info *die;
18367 struct dwarf2_cu *cu = reader->cu;
18368 bfd *abfd = reader->abfd;
18369
18370 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18371 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18372 info_ptr += bytes_read;
18373 if (!abbrev_number)
18374 {
18375 *diep = NULL;
18376 *has_children = 0;
18377 return info_ptr;
18378 }
18379
18380 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18381 if (!abbrev)
18382 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18383 abbrev_number,
18384 bfd_get_filename (abfd));
18385
18386 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18387 die->sect_off = sect_off;
18388 die->tag = abbrev->tag;
18389 die->abbrev = abbrev_number;
18390
18391 /* Make the result usable.
18392 The caller needs to update num_attrs after adding the extra
18393 attributes. */
18394 die->num_attrs = abbrev->num_attrs;
18395
18396 for (i = 0; i < abbrev->num_attrs; ++i)
18397 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18398 info_ptr);
18399
18400 *diep = die;
18401 *has_children = abbrev->has_children;
18402 return info_ptr;
18403 }
18404
18405 /* Read a die and all its attributes.
18406 Set DIEP to point to a newly allocated die with its information,
18407 except for its child, sibling, and parent fields.
18408 Set HAS_CHILDREN to tell whether the die has children or not. */
18409
18410 static const gdb_byte *
18411 read_full_die (const struct die_reader_specs *reader,
18412 struct die_info **diep, const gdb_byte *info_ptr,
18413 int *has_children)
18414 {
18415 const gdb_byte *result;
18416
18417 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18418
18419 if (dwarf_die_debug)
18420 {
18421 fprintf_unfiltered (gdb_stdlog,
18422 "Read die from %s@0x%x of %s:\n",
18423 get_section_name (reader->die_section),
18424 (unsigned) (info_ptr - reader->die_section->buffer),
18425 bfd_get_filename (reader->abfd));
18426 dump_die (*diep, dwarf_die_debug);
18427 }
18428
18429 return result;
18430 }
18431 \f
18432 /* Abbreviation tables.
18433
18434 In DWARF version 2, the description of the debugging information is
18435 stored in a separate .debug_abbrev section. Before we read any
18436 dies from a section we read in all abbreviations and install them
18437 in a hash table. */
18438
18439 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18440
18441 struct abbrev_info *
18442 abbrev_table::alloc_abbrev ()
18443 {
18444 struct abbrev_info *abbrev;
18445
18446 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18447 memset (abbrev, 0, sizeof (struct abbrev_info));
18448
18449 return abbrev;
18450 }
18451
18452 /* Add an abbreviation to the table. */
18453
18454 void
18455 abbrev_table::add_abbrev (unsigned int abbrev_number,
18456 struct abbrev_info *abbrev)
18457 {
18458 unsigned int hash_number;
18459
18460 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18461 abbrev->next = m_abbrevs[hash_number];
18462 m_abbrevs[hash_number] = abbrev;
18463 }
18464
18465 /* Look up an abbrev in the table.
18466 Returns NULL if the abbrev is not found. */
18467
18468 struct abbrev_info *
18469 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18470 {
18471 unsigned int hash_number;
18472 struct abbrev_info *abbrev;
18473
18474 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18475 abbrev = m_abbrevs[hash_number];
18476
18477 while (abbrev)
18478 {
18479 if (abbrev->number == abbrev_number)
18480 return abbrev;
18481 abbrev = abbrev->next;
18482 }
18483 return NULL;
18484 }
18485
18486 /* Read in an abbrev table. */
18487
18488 static abbrev_table_up
18489 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18490 struct dwarf2_section_info *section,
18491 sect_offset sect_off)
18492 {
18493 struct objfile *objfile = dwarf2_per_objfile->objfile;
18494 bfd *abfd = get_section_bfd_owner (section);
18495 const gdb_byte *abbrev_ptr;
18496 struct abbrev_info *cur_abbrev;
18497 unsigned int abbrev_number, bytes_read, abbrev_name;
18498 unsigned int abbrev_form;
18499 struct attr_abbrev *cur_attrs;
18500 unsigned int allocated_attrs;
18501
18502 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18503
18504 dwarf2_read_section (objfile, section);
18505 abbrev_ptr = section->buffer + to_underlying (sect_off);
18506 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18507 abbrev_ptr += bytes_read;
18508
18509 allocated_attrs = ATTR_ALLOC_CHUNK;
18510 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18511
18512 /* Loop until we reach an abbrev number of 0. */
18513 while (abbrev_number)
18514 {
18515 cur_abbrev = abbrev_table->alloc_abbrev ();
18516
18517 /* read in abbrev header */
18518 cur_abbrev->number = abbrev_number;
18519 cur_abbrev->tag
18520 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18521 abbrev_ptr += bytes_read;
18522 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18523 abbrev_ptr += 1;
18524
18525 /* now read in declarations */
18526 for (;;)
18527 {
18528 LONGEST implicit_const;
18529
18530 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18531 abbrev_ptr += bytes_read;
18532 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18533 abbrev_ptr += bytes_read;
18534 if (abbrev_form == DW_FORM_implicit_const)
18535 {
18536 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18537 &bytes_read);
18538 abbrev_ptr += bytes_read;
18539 }
18540 else
18541 {
18542 /* Initialize it due to a false compiler warning. */
18543 implicit_const = -1;
18544 }
18545
18546 if (abbrev_name == 0)
18547 break;
18548
18549 if (cur_abbrev->num_attrs == allocated_attrs)
18550 {
18551 allocated_attrs += ATTR_ALLOC_CHUNK;
18552 cur_attrs
18553 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18554 }
18555
18556 cur_attrs[cur_abbrev->num_attrs].name
18557 = (enum dwarf_attribute) abbrev_name;
18558 cur_attrs[cur_abbrev->num_attrs].form
18559 = (enum dwarf_form) abbrev_form;
18560 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18561 ++cur_abbrev->num_attrs;
18562 }
18563
18564 cur_abbrev->attrs =
18565 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18566 cur_abbrev->num_attrs);
18567 memcpy (cur_abbrev->attrs, cur_attrs,
18568 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18569
18570 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18571
18572 /* Get next abbreviation.
18573 Under Irix6 the abbreviations for a compilation unit are not
18574 always properly terminated with an abbrev number of 0.
18575 Exit loop if we encounter an abbreviation which we have
18576 already read (which means we are about to read the abbreviations
18577 for the next compile unit) or if the end of the abbreviation
18578 table is reached. */
18579 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18580 break;
18581 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18582 abbrev_ptr += bytes_read;
18583 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18584 break;
18585 }
18586
18587 xfree (cur_attrs);
18588 return abbrev_table;
18589 }
18590
18591 /* Returns nonzero if TAG represents a type that we might generate a partial
18592 symbol for. */
18593
18594 static int
18595 is_type_tag_for_partial (int tag)
18596 {
18597 switch (tag)
18598 {
18599 #if 0
18600 /* Some types that would be reasonable to generate partial symbols for,
18601 that we don't at present. */
18602 case DW_TAG_array_type:
18603 case DW_TAG_file_type:
18604 case DW_TAG_ptr_to_member_type:
18605 case DW_TAG_set_type:
18606 case DW_TAG_string_type:
18607 case DW_TAG_subroutine_type:
18608 #endif
18609 case DW_TAG_base_type:
18610 case DW_TAG_class_type:
18611 case DW_TAG_interface_type:
18612 case DW_TAG_enumeration_type:
18613 case DW_TAG_structure_type:
18614 case DW_TAG_subrange_type:
18615 case DW_TAG_typedef:
18616 case DW_TAG_union_type:
18617 return 1;
18618 default:
18619 return 0;
18620 }
18621 }
18622
18623 /* Load all DIEs that are interesting for partial symbols into memory. */
18624
18625 static struct partial_die_info *
18626 load_partial_dies (const struct die_reader_specs *reader,
18627 const gdb_byte *info_ptr, int building_psymtab)
18628 {
18629 struct dwarf2_cu *cu = reader->cu;
18630 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18631 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18632 unsigned int bytes_read;
18633 unsigned int load_all = 0;
18634 int nesting_level = 1;
18635
18636 parent_die = NULL;
18637 last_die = NULL;
18638
18639 gdb_assert (cu->per_cu != NULL);
18640 if (cu->per_cu->load_all_dies)
18641 load_all = 1;
18642
18643 cu->partial_dies
18644 = htab_create_alloc_ex (cu->header.length / 12,
18645 partial_die_hash,
18646 partial_die_eq,
18647 NULL,
18648 &cu->comp_unit_obstack,
18649 hashtab_obstack_allocate,
18650 dummy_obstack_deallocate);
18651
18652 while (1)
18653 {
18654 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18655
18656 /* A NULL abbrev means the end of a series of children. */
18657 if (abbrev == NULL)
18658 {
18659 if (--nesting_level == 0)
18660 return first_die;
18661
18662 info_ptr += bytes_read;
18663 last_die = parent_die;
18664 parent_die = parent_die->die_parent;
18665 continue;
18666 }
18667
18668 /* Check for template arguments. We never save these; if
18669 they're seen, we just mark the parent, and go on our way. */
18670 if (parent_die != NULL
18671 && cu->language == language_cplus
18672 && (abbrev->tag == DW_TAG_template_type_param
18673 || abbrev->tag == DW_TAG_template_value_param))
18674 {
18675 parent_die->has_template_arguments = 1;
18676
18677 if (!load_all)
18678 {
18679 /* We don't need a partial DIE for the template argument. */
18680 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18681 continue;
18682 }
18683 }
18684
18685 /* We only recurse into c++ subprograms looking for template arguments.
18686 Skip their other children. */
18687 if (!load_all
18688 && cu->language == language_cplus
18689 && parent_die != NULL
18690 && parent_die->tag == DW_TAG_subprogram)
18691 {
18692 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18693 continue;
18694 }
18695
18696 /* Check whether this DIE is interesting enough to save. Normally
18697 we would not be interested in members here, but there may be
18698 later variables referencing them via DW_AT_specification (for
18699 static members). */
18700 if (!load_all
18701 && !is_type_tag_for_partial (abbrev->tag)
18702 && abbrev->tag != DW_TAG_constant
18703 && abbrev->tag != DW_TAG_enumerator
18704 && abbrev->tag != DW_TAG_subprogram
18705 && abbrev->tag != DW_TAG_inlined_subroutine
18706 && abbrev->tag != DW_TAG_lexical_block
18707 && abbrev->tag != DW_TAG_variable
18708 && abbrev->tag != DW_TAG_namespace
18709 && abbrev->tag != DW_TAG_module
18710 && abbrev->tag != DW_TAG_member
18711 && abbrev->tag != DW_TAG_imported_unit
18712 && abbrev->tag != DW_TAG_imported_declaration)
18713 {
18714 /* Otherwise we skip to the next sibling, if any. */
18715 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18716 continue;
18717 }
18718
18719 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18720 abbrev);
18721
18722 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18723
18724 /* This two-pass algorithm for processing partial symbols has a
18725 high cost in cache pressure. Thus, handle some simple cases
18726 here which cover the majority of C partial symbols. DIEs
18727 which neither have specification tags in them, nor could have
18728 specification tags elsewhere pointing at them, can simply be
18729 processed and discarded.
18730
18731 This segment is also optional; scan_partial_symbols and
18732 add_partial_symbol will handle these DIEs if we chain
18733 them in normally. When compilers which do not emit large
18734 quantities of duplicate debug information are more common,
18735 this code can probably be removed. */
18736
18737 /* Any complete simple types at the top level (pretty much all
18738 of them, for a language without namespaces), can be processed
18739 directly. */
18740 if (parent_die == NULL
18741 && pdi.has_specification == 0
18742 && pdi.is_declaration == 0
18743 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18744 || pdi.tag == DW_TAG_base_type
18745 || pdi.tag == DW_TAG_subrange_type))
18746 {
18747 if (building_psymtab && pdi.name != NULL)
18748 add_psymbol_to_list (pdi.name, false,
18749 VAR_DOMAIN, LOC_TYPEDEF, -1,
18750 psymbol_placement::STATIC,
18751 0, cu->language, objfile);
18752 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18753 continue;
18754 }
18755
18756 /* The exception for DW_TAG_typedef with has_children above is
18757 a workaround of GCC PR debug/47510. In the case of this complaint
18758 type_name_or_error will error on such types later.
18759
18760 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18761 it could not find the child DIEs referenced later, this is checked
18762 above. In correct DWARF DW_TAG_typedef should have no children. */
18763
18764 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18765 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18766 "- DIE at %s [in module %s]"),
18767 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18768
18769 /* If we're at the second level, and we're an enumerator, and
18770 our parent has no specification (meaning possibly lives in a
18771 namespace elsewhere), then we can add the partial symbol now
18772 instead of queueing it. */
18773 if (pdi.tag == DW_TAG_enumerator
18774 && parent_die != NULL
18775 && parent_die->die_parent == NULL
18776 && parent_die->tag == DW_TAG_enumeration_type
18777 && parent_die->has_specification == 0)
18778 {
18779 if (pdi.name == NULL)
18780 complaint (_("malformed enumerator DIE ignored"));
18781 else if (building_psymtab)
18782 add_psymbol_to_list (pdi.name, false,
18783 VAR_DOMAIN, LOC_CONST, -1,
18784 cu->language == language_cplus
18785 ? psymbol_placement::GLOBAL
18786 : psymbol_placement::STATIC,
18787 0, cu->language, objfile);
18788
18789 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18790 continue;
18791 }
18792
18793 struct partial_die_info *part_die
18794 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18795
18796 /* We'll save this DIE so link it in. */
18797 part_die->die_parent = parent_die;
18798 part_die->die_sibling = NULL;
18799 part_die->die_child = NULL;
18800
18801 if (last_die && last_die == parent_die)
18802 last_die->die_child = part_die;
18803 else if (last_die)
18804 last_die->die_sibling = part_die;
18805
18806 last_die = part_die;
18807
18808 if (first_die == NULL)
18809 first_die = part_die;
18810
18811 /* Maybe add the DIE to the hash table. Not all DIEs that we
18812 find interesting need to be in the hash table, because we
18813 also have the parent/sibling/child chains; only those that we
18814 might refer to by offset later during partial symbol reading.
18815
18816 For now this means things that might have be the target of a
18817 DW_AT_specification, DW_AT_abstract_origin, or
18818 DW_AT_extension. DW_AT_extension will refer only to
18819 namespaces; DW_AT_abstract_origin refers to functions (and
18820 many things under the function DIE, but we do not recurse
18821 into function DIEs during partial symbol reading) and
18822 possibly variables as well; DW_AT_specification refers to
18823 declarations. Declarations ought to have the DW_AT_declaration
18824 flag. It happens that GCC forgets to put it in sometimes, but
18825 only for functions, not for types.
18826
18827 Adding more things than necessary to the hash table is harmless
18828 except for the performance cost. Adding too few will result in
18829 wasted time in find_partial_die, when we reread the compilation
18830 unit with load_all_dies set. */
18831
18832 if (load_all
18833 || abbrev->tag == DW_TAG_constant
18834 || abbrev->tag == DW_TAG_subprogram
18835 || abbrev->tag == DW_TAG_variable
18836 || abbrev->tag == DW_TAG_namespace
18837 || part_die->is_declaration)
18838 {
18839 void **slot;
18840
18841 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18842 to_underlying (part_die->sect_off),
18843 INSERT);
18844 *slot = part_die;
18845 }
18846
18847 /* For some DIEs we want to follow their children (if any). For C
18848 we have no reason to follow the children of structures; for other
18849 languages we have to, so that we can get at method physnames
18850 to infer fully qualified class names, for DW_AT_specification,
18851 and for C++ template arguments. For C++, we also look one level
18852 inside functions to find template arguments (if the name of the
18853 function does not already contain the template arguments).
18854
18855 For Ada and Fortran, we need to scan the children of subprograms
18856 and lexical blocks as well because these languages allow the
18857 definition of nested entities that could be interesting for the
18858 debugger, such as nested subprograms for instance. */
18859 if (last_die->has_children
18860 && (load_all
18861 || last_die->tag == DW_TAG_namespace
18862 || last_die->tag == DW_TAG_module
18863 || last_die->tag == DW_TAG_enumeration_type
18864 || (cu->language == language_cplus
18865 && last_die->tag == DW_TAG_subprogram
18866 && (last_die->name == NULL
18867 || strchr (last_die->name, '<') == NULL))
18868 || (cu->language != language_c
18869 && (last_die->tag == DW_TAG_class_type
18870 || last_die->tag == DW_TAG_interface_type
18871 || last_die->tag == DW_TAG_structure_type
18872 || last_die->tag == DW_TAG_union_type))
18873 || ((cu->language == language_ada
18874 || cu->language == language_fortran)
18875 && (last_die->tag == DW_TAG_subprogram
18876 || last_die->tag == DW_TAG_lexical_block))))
18877 {
18878 nesting_level++;
18879 parent_die = last_die;
18880 continue;
18881 }
18882
18883 /* Otherwise we skip to the next sibling, if any. */
18884 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18885
18886 /* Back to the top, do it again. */
18887 }
18888 }
18889
18890 partial_die_info::partial_die_info (sect_offset sect_off_,
18891 struct abbrev_info *abbrev)
18892 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18893 {
18894 }
18895
18896 /* Read a minimal amount of information into the minimal die structure.
18897 INFO_PTR should point just after the initial uleb128 of a DIE. */
18898
18899 const gdb_byte *
18900 partial_die_info::read (const struct die_reader_specs *reader,
18901 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18902 {
18903 struct dwarf2_cu *cu = reader->cu;
18904 struct dwarf2_per_objfile *dwarf2_per_objfile
18905 = cu->per_cu->dwarf2_per_objfile;
18906 unsigned int i;
18907 int has_low_pc_attr = 0;
18908 int has_high_pc_attr = 0;
18909 int high_pc_relative = 0;
18910
18911 for (i = 0; i < abbrev.num_attrs; ++i)
18912 {
18913 struct attribute attr;
18914
18915 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18916
18917 /* Store the data if it is of an attribute we want to keep in a
18918 partial symbol table. */
18919 switch (attr.name)
18920 {
18921 case DW_AT_name:
18922 switch (tag)
18923 {
18924 case DW_TAG_compile_unit:
18925 case DW_TAG_partial_unit:
18926 case DW_TAG_type_unit:
18927 /* Compilation units have a DW_AT_name that is a filename, not
18928 a source language identifier. */
18929 case DW_TAG_enumeration_type:
18930 case DW_TAG_enumerator:
18931 /* These tags always have simple identifiers already; no need
18932 to canonicalize them. */
18933 name = DW_STRING (&attr);
18934 break;
18935 default:
18936 {
18937 struct objfile *objfile = dwarf2_per_objfile->objfile;
18938
18939 name
18940 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18941 &objfile->per_bfd->storage_obstack);
18942 }
18943 break;
18944 }
18945 break;
18946 case DW_AT_linkage_name:
18947 case DW_AT_MIPS_linkage_name:
18948 /* Note that both forms of linkage name might appear. We
18949 assume they will be the same, and we only store the last
18950 one we see. */
18951 linkage_name = DW_STRING (&attr);
18952 break;
18953 case DW_AT_low_pc:
18954 has_low_pc_attr = 1;
18955 lowpc = attr_value_as_address (&attr);
18956 break;
18957 case DW_AT_high_pc:
18958 has_high_pc_attr = 1;
18959 highpc = attr_value_as_address (&attr);
18960 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18961 high_pc_relative = 1;
18962 break;
18963 case DW_AT_location:
18964 /* Support the .debug_loc offsets. */
18965 if (attr_form_is_block (&attr))
18966 {
18967 d.locdesc = DW_BLOCK (&attr);
18968 }
18969 else if (attr_form_is_section_offset (&attr))
18970 {
18971 dwarf2_complex_location_expr_complaint ();
18972 }
18973 else
18974 {
18975 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18976 "partial symbol information");
18977 }
18978 break;
18979 case DW_AT_external:
18980 is_external = DW_UNSND (&attr);
18981 break;
18982 case DW_AT_declaration:
18983 is_declaration = DW_UNSND (&attr);
18984 break;
18985 case DW_AT_type:
18986 has_type = 1;
18987 break;
18988 case DW_AT_abstract_origin:
18989 case DW_AT_specification:
18990 case DW_AT_extension:
18991 has_specification = 1;
18992 spec_offset = dwarf2_get_ref_die_offset (&attr);
18993 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18994 || cu->per_cu->is_dwz);
18995 break;
18996 case DW_AT_sibling:
18997 /* Ignore absolute siblings, they might point outside of
18998 the current compile unit. */
18999 if (attr.form == DW_FORM_ref_addr)
19000 complaint (_("ignoring absolute DW_AT_sibling"));
19001 else
19002 {
19003 const gdb_byte *buffer = reader->buffer;
19004 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19005 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19006
19007 if (sibling_ptr < info_ptr)
19008 complaint (_("DW_AT_sibling points backwards"));
19009 else if (sibling_ptr > reader->buffer_end)
19010 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19011 else
19012 sibling = sibling_ptr;
19013 }
19014 break;
19015 case DW_AT_byte_size:
19016 has_byte_size = 1;
19017 break;
19018 case DW_AT_const_value:
19019 has_const_value = 1;
19020 break;
19021 case DW_AT_calling_convention:
19022 /* DWARF doesn't provide a way to identify a program's source-level
19023 entry point. DW_AT_calling_convention attributes are only meant
19024 to describe functions' calling conventions.
19025
19026 However, because it's a necessary piece of information in
19027 Fortran, and before DWARF 4 DW_CC_program was the only
19028 piece of debugging information whose definition refers to
19029 a 'main program' at all, several compilers marked Fortran
19030 main programs with DW_CC_program --- even when those
19031 functions use the standard calling conventions.
19032
19033 Although DWARF now specifies a way to provide this
19034 information, we support this practice for backward
19035 compatibility. */
19036 if (DW_UNSND (&attr) == DW_CC_program
19037 && cu->language == language_fortran)
19038 main_subprogram = 1;
19039 break;
19040 case DW_AT_inline:
19041 if (DW_UNSND (&attr) == DW_INL_inlined
19042 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19043 may_be_inlined = 1;
19044 break;
19045
19046 case DW_AT_import:
19047 if (tag == DW_TAG_imported_unit)
19048 {
19049 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19050 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19051 || cu->per_cu->is_dwz);
19052 }
19053 break;
19054
19055 case DW_AT_main_subprogram:
19056 main_subprogram = DW_UNSND (&attr);
19057 break;
19058
19059 case DW_AT_ranges:
19060 {
19061 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19062 but that requires a full DIE, so instead we just
19063 reimplement it. */
19064 int need_ranges_base = tag != DW_TAG_compile_unit;
19065 unsigned int ranges_offset = (DW_UNSND (&attr)
19066 + (need_ranges_base
19067 ? cu->ranges_base
19068 : 0));
19069
19070 /* Value of the DW_AT_ranges attribute is the offset in the
19071 .debug_ranges section. */
19072 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19073 nullptr))
19074 has_pc_info = 1;
19075 }
19076 break;
19077
19078 default:
19079 break;
19080 }
19081 }
19082
19083 /* For Ada, if both the name and the linkage name appear, we prefer
19084 the latter. This lets "catch exception" work better, regardless
19085 of the order in which the name and linkage name were emitted.
19086 Really, though, this is just a workaround for the fact that gdb
19087 doesn't store both the name and the linkage name. */
19088 if (cu->language == language_ada && linkage_name != nullptr)
19089 name = linkage_name;
19090
19091 if (high_pc_relative)
19092 highpc += lowpc;
19093
19094 if (has_low_pc_attr && has_high_pc_attr)
19095 {
19096 /* When using the GNU linker, .gnu.linkonce. sections are used to
19097 eliminate duplicate copies of functions and vtables and such.
19098 The linker will arbitrarily choose one and discard the others.
19099 The AT_*_pc values for such functions refer to local labels in
19100 these sections. If the section from that file was discarded, the
19101 labels are not in the output, so the relocs get a value of 0.
19102 If this is a discarded function, mark the pc bounds as invalid,
19103 so that GDB will ignore it. */
19104 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19105 {
19106 struct objfile *objfile = dwarf2_per_objfile->objfile;
19107 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19108
19109 complaint (_("DW_AT_low_pc %s is zero "
19110 "for DIE at %s [in module %s]"),
19111 paddress (gdbarch, lowpc),
19112 sect_offset_str (sect_off),
19113 objfile_name (objfile));
19114 }
19115 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19116 else if (lowpc >= highpc)
19117 {
19118 struct objfile *objfile = dwarf2_per_objfile->objfile;
19119 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19120
19121 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19122 "for DIE at %s [in module %s]"),
19123 paddress (gdbarch, lowpc),
19124 paddress (gdbarch, highpc),
19125 sect_offset_str (sect_off),
19126 objfile_name (objfile));
19127 }
19128 else
19129 has_pc_info = 1;
19130 }
19131
19132 return info_ptr;
19133 }
19134
19135 /* Find a cached partial DIE at OFFSET in CU. */
19136
19137 struct partial_die_info *
19138 dwarf2_cu::find_partial_die (sect_offset sect_off)
19139 {
19140 struct partial_die_info *lookup_die = NULL;
19141 struct partial_die_info part_die (sect_off);
19142
19143 lookup_die = ((struct partial_die_info *)
19144 htab_find_with_hash (partial_dies, &part_die,
19145 to_underlying (sect_off)));
19146
19147 return lookup_die;
19148 }
19149
19150 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19151 except in the case of .debug_types DIEs which do not reference
19152 outside their CU (they do however referencing other types via
19153 DW_FORM_ref_sig8). */
19154
19155 static const struct cu_partial_die_info
19156 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19157 {
19158 struct dwarf2_per_objfile *dwarf2_per_objfile
19159 = cu->per_cu->dwarf2_per_objfile;
19160 struct objfile *objfile = dwarf2_per_objfile->objfile;
19161 struct dwarf2_per_cu_data *per_cu = NULL;
19162 struct partial_die_info *pd = NULL;
19163
19164 if (offset_in_dwz == cu->per_cu->is_dwz
19165 && offset_in_cu_p (&cu->header, sect_off))
19166 {
19167 pd = cu->find_partial_die (sect_off);
19168 if (pd != NULL)
19169 return { cu, pd };
19170 /* We missed recording what we needed.
19171 Load all dies and try again. */
19172 per_cu = cu->per_cu;
19173 }
19174 else
19175 {
19176 /* TUs don't reference other CUs/TUs (except via type signatures). */
19177 if (cu->per_cu->is_debug_types)
19178 {
19179 error (_("Dwarf Error: Type Unit at offset %s contains"
19180 " external reference to offset %s [in module %s].\n"),
19181 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19182 bfd_get_filename (objfile->obfd));
19183 }
19184 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19185 dwarf2_per_objfile);
19186
19187 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19188 load_partial_comp_unit (per_cu);
19189
19190 per_cu->cu->last_used = 0;
19191 pd = per_cu->cu->find_partial_die (sect_off);
19192 }
19193
19194 /* If we didn't find it, and not all dies have been loaded,
19195 load them all and try again. */
19196
19197 if (pd == NULL && per_cu->load_all_dies == 0)
19198 {
19199 per_cu->load_all_dies = 1;
19200
19201 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19202 THIS_CU->cu may already be in use. So we can't just free it and
19203 replace its DIEs with the ones we read in. Instead, we leave those
19204 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19205 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19206 set. */
19207 load_partial_comp_unit (per_cu);
19208
19209 pd = per_cu->cu->find_partial_die (sect_off);
19210 }
19211
19212 if (pd == NULL)
19213 internal_error (__FILE__, __LINE__,
19214 _("could not find partial DIE %s "
19215 "in cache [from module %s]\n"),
19216 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19217 return { per_cu->cu, pd };
19218 }
19219
19220 /* See if we can figure out if the class lives in a namespace. We do
19221 this by looking for a member function; its demangled name will
19222 contain namespace info, if there is any. */
19223
19224 static void
19225 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19226 struct dwarf2_cu *cu)
19227 {
19228 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19229 what template types look like, because the demangler
19230 frequently doesn't give the same name as the debug info. We
19231 could fix this by only using the demangled name to get the
19232 prefix (but see comment in read_structure_type). */
19233
19234 struct partial_die_info *real_pdi;
19235 struct partial_die_info *child_pdi;
19236
19237 /* If this DIE (this DIE's specification, if any) has a parent, then
19238 we should not do this. We'll prepend the parent's fully qualified
19239 name when we create the partial symbol. */
19240
19241 real_pdi = struct_pdi;
19242 while (real_pdi->has_specification)
19243 {
19244 auto res = find_partial_die (real_pdi->spec_offset,
19245 real_pdi->spec_is_dwz, cu);
19246 real_pdi = res.pdi;
19247 cu = res.cu;
19248 }
19249
19250 if (real_pdi->die_parent != NULL)
19251 return;
19252
19253 for (child_pdi = struct_pdi->die_child;
19254 child_pdi != NULL;
19255 child_pdi = child_pdi->die_sibling)
19256 {
19257 if (child_pdi->tag == DW_TAG_subprogram
19258 && child_pdi->linkage_name != NULL)
19259 {
19260 char *actual_class_name
19261 = language_class_name_from_physname (cu->language_defn,
19262 child_pdi->linkage_name);
19263 if (actual_class_name != NULL)
19264 {
19265 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19266 struct_pdi->name
19267 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19268 actual_class_name);
19269 xfree (actual_class_name);
19270 }
19271 break;
19272 }
19273 }
19274 }
19275
19276 void
19277 partial_die_info::fixup (struct dwarf2_cu *cu)
19278 {
19279 /* Once we've fixed up a die, there's no point in doing so again.
19280 This also avoids a memory leak if we were to call
19281 guess_partial_die_structure_name multiple times. */
19282 if (fixup_called)
19283 return;
19284
19285 /* If we found a reference attribute and the DIE has no name, try
19286 to find a name in the referred to DIE. */
19287
19288 if (name == NULL && has_specification)
19289 {
19290 struct partial_die_info *spec_die;
19291
19292 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19293 spec_die = res.pdi;
19294 cu = res.cu;
19295
19296 spec_die->fixup (cu);
19297
19298 if (spec_die->name)
19299 {
19300 name = spec_die->name;
19301
19302 /* Copy DW_AT_external attribute if it is set. */
19303 if (spec_die->is_external)
19304 is_external = spec_die->is_external;
19305 }
19306 }
19307
19308 /* Set default names for some unnamed DIEs. */
19309
19310 if (name == NULL && tag == DW_TAG_namespace)
19311 name = CP_ANONYMOUS_NAMESPACE_STR;
19312
19313 /* If there is no parent die to provide a namespace, and there are
19314 children, see if we can determine the namespace from their linkage
19315 name. */
19316 if (cu->language == language_cplus
19317 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19318 && die_parent == NULL
19319 && has_children
19320 && (tag == DW_TAG_class_type
19321 || tag == DW_TAG_structure_type
19322 || tag == DW_TAG_union_type))
19323 guess_partial_die_structure_name (this, cu);
19324
19325 /* GCC might emit a nameless struct or union that has a linkage
19326 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19327 if (name == NULL
19328 && (tag == DW_TAG_class_type
19329 || tag == DW_TAG_interface_type
19330 || tag == DW_TAG_structure_type
19331 || tag == DW_TAG_union_type)
19332 && linkage_name != NULL)
19333 {
19334 char *demangled;
19335
19336 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19337 if (demangled)
19338 {
19339 const char *base;
19340
19341 /* Strip any leading namespaces/classes, keep only the base name.
19342 DW_AT_name for named DIEs does not contain the prefixes. */
19343 base = strrchr (demangled, ':');
19344 if (base && base > demangled && base[-1] == ':')
19345 base++;
19346 else
19347 base = demangled;
19348
19349 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19350 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19351 xfree (demangled);
19352 }
19353 }
19354
19355 fixup_called = 1;
19356 }
19357
19358 /* Read an attribute value described by an attribute form. */
19359
19360 static const gdb_byte *
19361 read_attribute_value (const struct die_reader_specs *reader,
19362 struct attribute *attr, unsigned form,
19363 LONGEST implicit_const, const gdb_byte *info_ptr)
19364 {
19365 struct dwarf2_cu *cu = reader->cu;
19366 struct dwarf2_per_objfile *dwarf2_per_objfile
19367 = cu->per_cu->dwarf2_per_objfile;
19368 struct objfile *objfile = dwarf2_per_objfile->objfile;
19369 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19370 bfd *abfd = reader->abfd;
19371 struct comp_unit_head *cu_header = &cu->header;
19372 unsigned int bytes_read;
19373 struct dwarf_block *blk;
19374
19375 attr->form = (enum dwarf_form) form;
19376 switch (form)
19377 {
19378 case DW_FORM_ref_addr:
19379 if (cu->header.version == 2)
19380 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19381 else
19382 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19383 &cu->header, &bytes_read);
19384 info_ptr += bytes_read;
19385 break;
19386 case DW_FORM_GNU_ref_alt:
19387 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19388 info_ptr += bytes_read;
19389 break;
19390 case DW_FORM_addr:
19391 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19392 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19393 info_ptr += bytes_read;
19394 break;
19395 case DW_FORM_block2:
19396 blk = dwarf_alloc_block (cu);
19397 blk->size = read_2_bytes (abfd, info_ptr);
19398 info_ptr += 2;
19399 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19400 info_ptr += blk->size;
19401 DW_BLOCK (attr) = blk;
19402 break;
19403 case DW_FORM_block4:
19404 blk = dwarf_alloc_block (cu);
19405 blk->size = read_4_bytes (abfd, info_ptr);
19406 info_ptr += 4;
19407 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19408 info_ptr += blk->size;
19409 DW_BLOCK (attr) = blk;
19410 break;
19411 case DW_FORM_data2:
19412 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19413 info_ptr += 2;
19414 break;
19415 case DW_FORM_data4:
19416 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19417 info_ptr += 4;
19418 break;
19419 case DW_FORM_data8:
19420 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19421 info_ptr += 8;
19422 break;
19423 case DW_FORM_data16:
19424 blk = dwarf_alloc_block (cu);
19425 blk->size = 16;
19426 blk->data = read_n_bytes (abfd, info_ptr, 16);
19427 info_ptr += 16;
19428 DW_BLOCK (attr) = blk;
19429 break;
19430 case DW_FORM_sec_offset:
19431 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19432 info_ptr += bytes_read;
19433 break;
19434 case DW_FORM_string:
19435 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19436 DW_STRING_IS_CANONICAL (attr) = 0;
19437 info_ptr += bytes_read;
19438 break;
19439 case DW_FORM_strp:
19440 if (!cu->per_cu->is_dwz)
19441 {
19442 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19443 abfd, info_ptr, cu_header,
19444 &bytes_read);
19445 DW_STRING_IS_CANONICAL (attr) = 0;
19446 info_ptr += bytes_read;
19447 break;
19448 }
19449 /* FALLTHROUGH */
19450 case DW_FORM_line_strp:
19451 if (!cu->per_cu->is_dwz)
19452 {
19453 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19454 abfd, info_ptr,
19455 cu_header, &bytes_read);
19456 DW_STRING_IS_CANONICAL (attr) = 0;
19457 info_ptr += bytes_read;
19458 break;
19459 }
19460 /* FALLTHROUGH */
19461 case DW_FORM_GNU_strp_alt:
19462 {
19463 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19464 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19465 &bytes_read);
19466
19467 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19468 dwz, str_offset);
19469 DW_STRING_IS_CANONICAL (attr) = 0;
19470 info_ptr += bytes_read;
19471 }
19472 break;
19473 case DW_FORM_exprloc:
19474 case DW_FORM_block:
19475 blk = dwarf_alloc_block (cu);
19476 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19477 info_ptr += bytes_read;
19478 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19479 info_ptr += blk->size;
19480 DW_BLOCK (attr) = blk;
19481 break;
19482 case DW_FORM_block1:
19483 blk = dwarf_alloc_block (cu);
19484 blk->size = read_1_byte (abfd, info_ptr);
19485 info_ptr += 1;
19486 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19487 info_ptr += blk->size;
19488 DW_BLOCK (attr) = blk;
19489 break;
19490 case DW_FORM_data1:
19491 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19492 info_ptr += 1;
19493 break;
19494 case DW_FORM_flag:
19495 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19496 info_ptr += 1;
19497 break;
19498 case DW_FORM_flag_present:
19499 DW_UNSND (attr) = 1;
19500 break;
19501 case DW_FORM_sdata:
19502 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19503 info_ptr += bytes_read;
19504 break;
19505 case DW_FORM_udata:
19506 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19507 info_ptr += bytes_read;
19508 break;
19509 case DW_FORM_ref1:
19510 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19511 + read_1_byte (abfd, info_ptr));
19512 info_ptr += 1;
19513 break;
19514 case DW_FORM_ref2:
19515 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19516 + read_2_bytes (abfd, info_ptr));
19517 info_ptr += 2;
19518 break;
19519 case DW_FORM_ref4:
19520 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19521 + read_4_bytes (abfd, info_ptr));
19522 info_ptr += 4;
19523 break;
19524 case DW_FORM_ref8:
19525 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19526 + read_8_bytes (abfd, info_ptr));
19527 info_ptr += 8;
19528 break;
19529 case DW_FORM_ref_sig8:
19530 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19531 info_ptr += 8;
19532 break;
19533 case DW_FORM_ref_udata:
19534 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19535 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19536 info_ptr += bytes_read;
19537 break;
19538 case DW_FORM_indirect:
19539 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19540 info_ptr += bytes_read;
19541 if (form == DW_FORM_implicit_const)
19542 {
19543 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19544 info_ptr += bytes_read;
19545 }
19546 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19547 info_ptr);
19548 break;
19549 case DW_FORM_implicit_const:
19550 DW_SND (attr) = implicit_const;
19551 break;
19552 case DW_FORM_addrx:
19553 case DW_FORM_GNU_addr_index:
19554 if (reader->dwo_file == NULL)
19555 {
19556 /* For now flag a hard error.
19557 Later we can turn this into a complaint. */
19558 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19559 dwarf_form_name (form),
19560 bfd_get_filename (abfd));
19561 }
19562 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19563 info_ptr += bytes_read;
19564 break;
19565 case DW_FORM_strx:
19566 case DW_FORM_strx1:
19567 case DW_FORM_strx2:
19568 case DW_FORM_strx3:
19569 case DW_FORM_strx4:
19570 case DW_FORM_GNU_str_index:
19571 if (reader->dwo_file == NULL)
19572 {
19573 /* For now flag a hard error.
19574 Later we can turn this into a complaint if warranted. */
19575 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19576 dwarf_form_name (form),
19577 bfd_get_filename (abfd));
19578 }
19579 {
19580 ULONGEST str_index;
19581 if (form == DW_FORM_strx1)
19582 {
19583 str_index = read_1_byte (abfd, info_ptr);
19584 info_ptr += 1;
19585 }
19586 else if (form == DW_FORM_strx2)
19587 {
19588 str_index = read_2_bytes (abfd, info_ptr);
19589 info_ptr += 2;
19590 }
19591 else if (form == DW_FORM_strx3)
19592 {
19593 str_index = read_3_bytes (abfd, info_ptr);
19594 info_ptr += 3;
19595 }
19596 else if (form == DW_FORM_strx4)
19597 {
19598 str_index = read_4_bytes (abfd, info_ptr);
19599 info_ptr += 4;
19600 }
19601 else
19602 {
19603 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19604 info_ptr += bytes_read;
19605 }
19606 DW_STRING (attr) = read_str_index (reader, str_index);
19607 DW_STRING_IS_CANONICAL (attr) = 0;
19608 }
19609 break;
19610 default:
19611 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19612 dwarf_form_name (form),
19613 bfd_get_filename (abfd));
19614 }
19615
19616 /* Super hack. */
19617 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19618 attr->form = DW_FORM_GNU_ref_alt;
19619
19620 /* We have seen instances where the compiler tried to emit a byte
19621 size attribute of -1 which ended up being encoded as an unsigned
19622 0xffffffff. Although 0xffffffff is technically a valid size value,
19623 an object of this size seems pretty unlikely so we can relatively
19624 safely treat these cases as if the size attribute was invalid and
19625 treat them as zero by default. */
19626 if (attr->name == DW_AT_byte_size
19627 && form == DW_FORM_data4
19628 && DW_UNSND (attr) >= 0xffffffff)
19629 {
19630 complaint
19631 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19632 hex_string (DW_UNSND (attr)));
19633 DW_UNSND (attr) = 0;
19634 }
19635
19636 return info_ptr;
19637 }
19638
19639 /* Read an attribute described by an abbreviated attribute. */
19640
19641 static const gdb_byte *
19642 read_attribute (const struct die_reader_specs *reader,
19643 struct attribute *attr, struct attr_abbrev *abbrev,
19644 const gdb_byte *info_ptr)
19645 {
19646 attr->name = abbrev->name;
19647 return read_attribute_value (reader, attr, abbrev->form,
19648 abbrev->implicit_const, info_ptr);
19649 }
19650
19651 /* Read dwarf information from a buffer. */
19652
19653 static unsigned int
19654 read_1_byte (bfd *abfd, const gdb_byte *buf)
19655 {
19656 return bfd_get_8 (abfd, buf);
19657 }
19658
19659 static int
19660 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19661 {
19662 return bfd_get_signed_8 (abfd, buf);
19663 }
19664
19665 static unsigned int
19666 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19667 {
19668 return bfd_get_16 (abfd, buf);
19669 }
19670
19671 static int
19672 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19673 {
19674 return bfd_get_signed_16 (abfd, buf);
19675 }
19676
19677 static unsigned int
19678 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19679 {
19680 unsigned int result = 0;
19681 for (int i = 0; i < 3; ++i)
19682 {
19683 unsigned char byte = bfd_get_8 (abfd, buf);
19684 buf++;
19685 result |= ((unsigned int) byte << (i * 8));
19686 }
19687 return result;
19688 }
19689
19690 static unsigned int
19691 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19692 {
19693 return bfd_get_32 (abfd, buf);
19694 }
19695
19696 static int
19697 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19698 {
19699 return bfd_get_signed_32 (abfd, buf);
19700 }
19701
19702 static ULONGEST
19703 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19704 {
19705 return bfd_get_64 (abfd, buf);
19706 }
19707
19708 static CORE_ADDR
19709 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19710 unsigned int *bytes_read)
19711 {
19712 struct comp_unit_head *cu_header = &cu->header;
19713 CORE_ADDR retval = 0;
19714
19715 if (cu_header->signed_addr_p)
19716 {
19717 switch (cu_header->addr_size)
19718 {
19719 case 2:
19720 retval = bfd_get_signed_16 (abfd, buf);
19721 break;
19722 case 4:
19723 retval = bfd_get_signed_32 (abfd, buf);
19724 break;
19725 case 8:
19726 retval = bfd_get_signed_64 (abfd, buf);
19727 break;
19728 default:
19729 internal_error (__FILE__, __LINE__,
19730 _("read_address: bad switch, signed [in module %s]"),
19731 bfd_get_filename (abfd));
19732 }
19733 }
19734 else
19735 {
19736 switch (cu_header->addr_size)
19737 {
19738 case 2:
19739 retval = bfd_get_16 (abfd, buf);
19740 break;
19741 case 4:
19742 retval = bfd_get_32 (abfd, buf);
19743 break;
19744 case 8:
19745 retval = bfd_get_64 (abfd, buf);
19746 break;
19747 default:
19748 internal_error (__FILE__, __LINE__,
19749 _("read_address: bad switch, "
19750 "unsigned [in module %s]"),
19751 bfd_get_filename (abfd));
19752 }
19753 }
19754
19755 *bytes_read = cu_header->addr_size;
19756 return retval;
19757 }
19758
19759 /* Read the initial length from a section. The (draft) DWARF 3
19760 specification allows the initial length to take up either 4 bytes
19761 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19762 bytes describe the length and all offsets will be 8 bytes in length
19763 instead of 4.
19764
19765 An older, non-standard 64-bit format is also handled by this
19766 function. The older format in question stores the initial length
19767 as an 8-byte quantity without an escape value. Lengths greater
19768 than 2^32 aren't very common which means that the initial 4 bytes
19769 is almost always zero. Since a length value of zero doesn't make
19770 sense for the 32-bit format, this initial zero can be considered to
19771 be an escape value which indicates the presence of the older 64-bit
19772 format. As written, the code can't detect (old format) lengths
19773 greater than 4GB. If it becomes necessary to handle lengths
19774 somewhat larger than 4GB, we could allow other small values (such
19775 as the non-sensical values of 1, 2, and 3) to also be used as
19776 escape values indicating the presence of the old format.
19777
19778 The value returned via bytes_read should be used to increment the
19779 relevant pointer after calling read_initial_length().
19780
19781 [ Note: read_initial_length() and read_offset() are based on the
19782 document entitled "DWARF Debugging Information Format", revision
19783 3, draft 8, dated November 19, 2001. This document was obtained
19784 from:
19785
19786 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19787
19788 This document is only a draft and is subject to change. (So beware.)
19789
19790 Details regarding the older, non-standard 64-bit format were
19791 determined empirically by examining 64-bit ELF files produced by
19792 the SGI toolchain on an IRIX 6.5 machine.
19793
19794 - Kevin, July 16, 2002
19795 ] */
19796
19797 static LONGEST
19798 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19799 {
19800 LONGEST length = bfd_get_32 (abfd, buf);
19801
19802 if (length == 0xffffffff)
19803 {
19804 length = bfd_get_64 (abfd, buf + 4);
19805 *bytes_read = 12;
19806 }
19807 else if (length == 0)
19808 {
19809 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19810 length = bfd_get_64 (abfd, buf);
19811 *bytes_read = 8;
19812 }
19813 else
19814 {
19815 *bytes_read = 4;
19816 }
19817
19818 return length;
19819 }
19820
19821 /* Cover function for read_initial_length.
19822 Returns the length of the object at BUF, and stores the size of the
19823 initial length in *BYTES_READ and stores the size that offsets will be in
19824 *OFFSET_SIZE.
19825 If the initial length size is not equivalent to that specified in
19826 CU_HEADER then issue a complaint.
19827 This is useful when reading non-comp-unit headers. */
19828
19829 static LONGEST
19830 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19831 const struct comp_unit_head *cu_header,
19832 unsigned int *bytes_read,
19833 unsigned int *offset_size)
19834 {
19835 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19836
19837 gdb_assert (cu_header->initial_length_size == 4
19838 || cu_header->initial_length_size == 8
19839 || cu_header->initial_length_size == 12);
19840
19841 if (cu_header->initial_length_size != *bytes_read)
19842 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19843
19844 *offset_size = (*bytes_read == 4) ? 4 : 8;
19845 return length;
19846 }
19847
19848 /* Read an offset from the data stream. The size of the offset is
19849 given by cu_header->offset_size. */
19850
19851 static LONGEST
19852 read_offset (bfd *abfd, const gdb_byte *buf,
19853 const struct comp_unit_head *cu_header,
19854 unsigned int *bytes_read)
19855 {
19856 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19857
19858 *bytes_read = cu_header->offset_size;
19859 return offset;
19860 }
19861
19862 /* Read an offset from the data stream. */
19863
19864 static LONGEST
19865 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19866 {
19867 LONGEST retval = 0;
19868
19869 switch (offset_size)
19870 {
19871 case 4:
19872 retval = bfd_get_32 (abfd, buf);
19873 break;
19874 case 8:
19875 retval = bfd_get_64 (abfd, buf);
19876 break;
19877 default:
19878 internal_error (__FILE__, __LINE__,
19879 _("read_offset_1: bad switch [in module %s]"),
19880 bfd_get_filename (abfd));
19881 }
19882
19883 return retval;
19884 }
19885
19886 static const gdb_byte *
19887 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19888 {
19889 /* If the size of a host char is 8 bits, we can return a pointer
19890 to the buffer, otherwise we have to copy the data to a buffer
19891 allocated on the temporary obstack. */
19892 gdb_assert (HOST_CHAR_BIT == 8);
19893 return buf;
19894 }
19895
19896 static const char *
19897 read_direct_string (bfd *abfd, const gdb_byte *buf,
19898 unsigned int *bytes_read_ptr)
19899 {
19900 /* If the size of a host char is 8 bits, we can return a pointer
19901 to the string, otherwise we have to copy the string to a buffer
19902 allocated on the temporary obstack. */
19903 gdb_assert (HOST_CHAR_BIT == 8);
19904 if (*buf == '\0')
19905 {
19906 *bytes_read_ptr = 1;
19907 return NULL;
19908 }
19909 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19910 return (const char *) buf;
19911 }
19912
19913 /* Return pointer to string at section SECT offset STR_OFFSET with error
19914 reporting strings FORM_NAME and SECT_NAME. */
19915
19916 static const char *
19917 read_indirect_string_at_offset_from (struct objfile *objfile,
19918 bfd *abfd, LONGEST str_offset,
19919 struct dwarf2_section_info *sect,
19920 const char *form_name,
19921 const char *sect_name)
19922 {
19923 dwarf2_read_section (objfile, sect);
19924 if (sect->buffer == NULL)
19925 error (_("%s used without %s section [in module %s]"),
19926 form_name, sect_name, bfd_get_filename (abfd));
19927 if (str_offset >= sect->size)
19928 error (_("%s pointing outside of %s section [in module %s]"),
19929 form_name, sect_name, bfd_get_filename (abfd));
19930 gdb_assert (HOST_CHAR_BIT == 8);
19931 if (sect->buffer[str_offset] == '\0')
19932 return NULL;
19933 return (const char *) (sect->buffer + str_offset);
19934 }
19935
19936 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19937
19938 static const char *
19939 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19940 bfd *abfd, LONGEST str_offset)
19941 {
19942 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19943 abfd, str_offset,
19944 &dwarf2_per_objfile->str,
19945 "DW_FORM_strp", ".debug_str");
19946 }
19947
19948 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19949
19950 static const char *
19951 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19952 bfd *abfd, LONGEST str_offset)
19953 {
19954 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19955 abfd, str_offset,
19956 &dwarf2_per_objfile->line_str,
19957 "DW_FORM_line_strp",
19958 ".debug_line_str");
19959 }
19960
19961 /* Read a string at offset STR_OFFSET in the .debug_str section from
19962 the .dwz file DWZ. Throw an error if the offset is too large. If
19963 the string consists of a single NUL byte, return NULL; otherwise
19964 return a pointer to the string. */
19965
19966 static const char *
19967 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19968 LONGEST str_offset)
19969 {
19970 dwarf2_read_section (objfile, &dwz->str);
19971
19972 if (dwz->str.buffer == NULL)
19973 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19974 "section [in module %s]"),
19975 bfd_get_filename (dwz->dwz_bfd.get ()));
19976 if (str_offset >= dwz->str.size)
19977 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19978 ".debug_str section [in module %s]"),
19979 bfd_get_filename (dwz->dwz_bfd.get ()));
19980 gdb_assert (HOST_CHAR_BIT == 8);
19981 if (dwz->str.buffer[str_offset] == '\0')
19982 return NULL;
19983 return (const char *) (dwz->str.buffer + str_offset);
19984 }
19985
19986 /* Return pointer to string at .debug_str offset as read from BUF.
19987 BUF is assumed to be in a compilation unit described by CU_HEADER.
19988 Return *BYTES_READ_PTR count of bytes read from BUF. */
19989
19990 static const char *
19991 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19992 const gdb_byte *buf,
19993 const struct comp_unit_head *cu_header,
19994 unsigned int *bytes_read_ptr)
19995 {
19996 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19997
19998 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19999 }
20000
20001 /* Return pointer to string at .debug_line_str offset as read from BUF.
20002 BUF is assumed to be in a compilation unit described by CU_HEADER.
20003 Return *BYTES_READ_PTR count of bytes read from BUF. */
20004
20005 static const char *
20006 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20007 bfd *abfd, const gdb_byte *buf,
20008 const struct comp_unit_head *cu_header,
20009 unsigned int *bytes_read_ptr)
20010 {
20011 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20012
20013 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20014 str_offset);
20015 }
20016
20017 ULONGEST
20018 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
20019 unsigned int *bytes_read_ptr)
20020 {
20021 ULONGEST result;
20022 unsigned int num_read;
20023 int shift;
20024 unsigned char byte;
20025
20026 result = 0;
20027 shift = 0;
20028 num_read = 0;
20029 while (1)
20030 {
20031 byte = bfd_get_8 (abfd, buf);
20032 buf++;
20033 num_read++;
20034 result |= ((ULONGEST) (byte & 127) << shift);
20035 if ((byte & 128) == 0)
20036 {
20037 break;
20038 }
20039 shift += 7;
20040 }
20041 *bytes_read_ptr = num_read;
20042 return result;
20043 }
20044
20045 static LONGEST
20046 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20047 unsigned int *bytes_read_ptr)
20048 {
20049 ULONGEST result;
20050 int shift, num_read;
20051 unsigned char byte;
20052
20053 result = 0;
20054 shift = 0;
20055 num_read = 0;
20056 while (1)
20057 {
20058 byte = bfd_get_8 (abfd, buf);
20059 buf++;
20060 num_read++;
20061 result |= ((ULONGEST) (byte & 127) << shift);
20062 shift += 7;
20063 if ((byte & 128) == 0)
20064 {
20065 break;
20066 }
20067 }
20068 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20069 result |= -(((ULONGEST) 1) << shift);
20070 *bytes_read_ptr = num_read;
20071 return result;
20072 }
20073
20074 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20075 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20076 ADDR_SIZE is the size of addresses from the CU header. */
20077
20078 static CORE_ADDR
20079 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20080 unsigned int addr_index, ULONGEST addr_base, int addr_size)
20081 {
20082 struct objfile *objfile = dwarf2_per_objfile->objfile;
20083 bfd *abfd = objfile->obfd;
20084 const gdb_byte *info_ptr;
20085
20086 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20087 if (dwarf2_per_objfile->addr.buffer == NULL)
20088 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20089 objfile_name (objfile));
20090 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20091 error (_("DW_FORM_addr_index pointing outside of "
20092 ".debug_addr section [in module %s]"),
20093 objfile_name (objfile));
20094 info_ptr = (dwarf2_per_objfile->addr.buffer
20095 + addr_base + addr_index * addr_size);
20096 if (addr_size == 4)
20097 return bfd_get_32 (abfd, info_ptr);
20098 else
20099 return bfd_get_64 (abfd, info_ptr);
20100 }
20101
20102 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20103
20104 static CORE_ADDR
20105 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20106 {
20107 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20108 cu->addr_base, cu->header.addr_size);
20109 }
20110
20111 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20112
20113 static CORE_ADDR
20114 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20115 unsigned int *bytes_read)
20116 {
20117 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20118 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20119
20120 return read_addr_index (cu, addr_index);
20121 }
20122
20123 /* Data structure to pass results from dwarf2_read_addr_index_reader
20124 back to dwarf2_read_addr_index. */
20125
20126 struct dwarf2_read_addr_index_data
20127 {
20128 ULONGEST addr_base;
20129 int addr_size;
20130 };
20131
20132 /* die_reader_func for dwarf2_read_addr_index. */
20133
20134 static void
20135 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
20136 const gdb_byte *info_ptr,
20137 struct die_info *comp_unit_die,
20138 int has_children,
20139 void *data)
20140 {
20141 struct dwarf2_cu *cu = reader->cu;
20142 struct dwarf2_read_addr_index_data *aidata =
20143 (struct dwarf2_read_addr_index_data *) data;
20144
20145 aidata->addr_base = cu->addr_base;
20146 aidata->addr_size = cu->header.addr_size;
20147 }
20148
20149 /* Given an index in .debug_addr, fetch the value.
20150 NOTE: This can be called during dwarf expression evaluation,
20151 long after the debug information has been read, and thus per_cu->cu
20152 may no longer exist. */
20153
20154 CORE_ADDR
20155 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20156 unsigned int addr_index)
20157 {
20158 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20159 struct dwarf2_cu *cu = per_cu->cu;
20160 ULONGEST addr_base;
20161 int addr_size;
20162
20163 /* We need addr_base and addr_size.
20164 If we don't have PER_CU->cu, we have to get it.
20165 Nasty, but the alternative is storing the needed info in PER_CU,
20166 which at this point doesn't seem justified: it's not clear how frequently
20167 it would get used and it would increase the size of every PER_CU.
20168 Entry points like dwarf2_per_cu_addr_size do a similar thing
20169 so we're not in uncharted territory here.
20170 Alas we need to be a bit more complicated as addr_base is contained
20171 in the DIE.
20172
20173 We don't need to read the entire CU(/TU).
20174 We just need the header and top level die.
20175
20176 IWBN to use the aging mechanism to let us lazily later discard the CU.
20177 For now we skip this optimization. */
20178
20179 if (cu != NULL)
20180 {
20181 addr_base = cu->addr_base;
20182 addr_size = cu->header.addr_size;
20183 }
20184 else
20185 {
20186 struct dwarf2_read_addr_index_data aidata;
20187
20188 /* Note: We can't use init_cutu_and_read_dies_simple here,
20189 we need addr_base. */
20190 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
20191 dwarf2_read_addr_index_reader, &aidata);
20192 addr_base = aidata.addr_base;
20193 addr_size = aidata.addr_size;
20194 }
20195
20196 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20197 addr_size);
20198 }
20199
20200 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
20201 This is only used by the Fission support. */
20202
20203 static const char *
20204 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20205 {
20206 struct dwarf2_cu *cu = reader->cu;
20207 struct dwarf2_per_objfile *dwarf2_per_objfile
20208 = cu->per_cu->dwarf2_per_objfile;
20209 struct objfile *objfile = dwarf2_per_objfile->objfile;
20210 const char *objf_name = objfile_name (objfile);
20211 bfd *abfd = objfile->obfd;
20212 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20213 struct dwarf2_section_info *str_offsets_section =
20214 &reader->dwo_file->sections.str_offsets;
20215 const gdb_byte *info_ptr;
20216 ULONGEST str_offset;
20217 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20218
20219 dwarf2_read_section (objfile, str_section);
20220 dwarf2_read_section (objfile, str_offsets_section);
20221 if (str_section->buffer == NULL)
20222 error (_("%s used without .debug_str.dwo section"
20223 " in CU at offset %s [in module %s]"),
20224 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20225 if (str_offsets_section->buffer == NULL)
20226 error (_("%s used without .debug_str_offsets.dwo section"
20227 " in CU at offset %s [in module %s]"),
20228 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20229 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20230 error (_("%s pointing outside of .debug_str_offsets.dwo"
20231 " section in CU at offset %s [in module %s]"),
20232 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20233 info_ptr = (str_offsets_section->buffer
20234 + str_index * cu->header.offset_size);
20235 if (cu->header.offset_size == 4)
20236 str_offset = bfd_get_32 (abfd, info_ptr);
20237 else
20238 str_offset = bfd_get_64 (abfd, info_ptr);
20239 if (str_offset >= str_section->size)
20240 error (_("Offset from %s pointing outside of"
20241 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20242 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20243 return (const char *) (str_section->buffer + str_offset);
20244 }
20245
20246 /* Return the length of an LEB128 number in BUF. */
20247
20248 static int
20249 leb128_size (const gdb_byte *buf)
20250 {
20251 const gdb_byte *begin = buf;
20252 gdb_byte byte;
20253
20254 while (1)
20255 {
20256 byte = *buf++;
20257 if ((byte & 128) == 0)
20258 return buf - begin;
20259 }
20260 }
20261
20262 static void
20263 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20264 {
20265 switch (lang)
20266 {
20267 case DW_LANG_C89:
20268 case DW_LANG_C99:
20269 case DW_LANG_C11:
20270 case DW_LANG_C:
20271 case DW_LANG_UPC:
20272 cu->language = language_c;
20273 break;
20274 case DW_LANG_Java:
20275 case DW_LANG_C_plus_plus:
20276 case DW_LANG_C_plus_plus_11:
20277 case DW_LANG_C_plus_plus_14:
20278 cu->language = language_cplus;
20279 break;
20280 case DW_LANG_D:
20281 cu->language = language_d;
20282 break;
20283 case DW_LANG_Fortran77:
20284 case DW_LANG_Fortran90:
20285 case DW_LANG_Fortran95:
20286 case DW_LANG_Fortran03:
20287 case DW_LANG_Fortran08:
20288 cu->language = language_fortran;
20289 break;
20290 case DW_LANG_Go:
20291 cu->language = language_go;
20292 break;
20293 case DW_LANG_Mips_Assembler:
20294 cu->language = language_asm;
20295 break;
20296 case DW_LANG_Ada83:
20297 case DW_LANG_Ada95:
20298 cu->language = language_ada;
20299 break;
20300 case DW_LANG_Modula2:
20301 cu->language = language_m2;
20302 break;
20303 case DW_LANG_Pascal83:
20304 cu->language = language_pascal;
20305 break;
20306 case DW_LANG_ObjC:
20307 cu->language = language_objc;
20308 break;
20309 case DW_LANG_Rust:
20310 case DW_LANG_Rust_old:
20311 cu->language = language_rust;
20312 break;
20313 case DW_LANG_Cobol74:
20314 case DW_LANG_Cobol85:
20315 default:
20316 cu->language = language_minimal;
20317 break;
20318 }
20319 cu->language_defn = language_def (cu->language);
20320 }
20321
20322 /* Return the named attribute or NULL if not there. */
20323
20324 static struct attribute *
20325 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20326 {
20327 for (;;)
20328 {
20329 unsigned int i;
20330 struct attribute *spec = NULL;
20331
20332 for (i = 0; i < die->num_attrs; ++i)
20333 {
20334 if (die->attrs[i].name == name)
20335 return &die->attrs[i];
20336 if (die->attrs[i].name == DW_AT_specification
20337 || die->attrs[i].name == DW_AT_abstract_origin)
20338 spec = &die->attrs[i];
20339 }
20340
20341 if (!spec)
20342 break;
20343
20344 die = follow_die_ref (die, spec, &cu);
20345 }
20346
20347 return NULL;
20348 }
20349
20350 /* Return the named attribute or NULL if not there,
20351 but do not follow DW_AT_specification, etc.
20352 This is for use in contexts where we're reading .debug_types dies.
20353 Following DW_AT_specification, DW_AT_abstract_origin will take us
20354 back up the chain, and we want to go down. */
20355
20356 static struct attribute *
20357 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20358 {
20359 unsigned int i;
20360
20361 for (i = 0; i < die->num_attrs; ++i)
20362 if (die->attrs[i].name == name)
20363 return &die->attrs[i];
20364
20365 return NULL;
20366 }
20367
20368 /* Return the string associated with a string-typed attribute, or NULL if it
20369 is either not found or is of an incorrect type. */
20370
20371 static const char *
20372 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20373 {
20374 struct attribute *attr;
20375 const char *str = NULL;
20376
20377 attr = dwarf2_attr (die, name, cu);
20378
20379 if (attr != NULL)
20380 {
20381 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20382 || attr->form == DW_FORM_string
20383 || attr->form == DW_FORM_strx
20384 || attr->form == DW_FORM_strx1
20385 || attr->form == DW_FORM_strx2
20386 || attr->form == DW_FORM_strx3
20387 || attr->form == DW_FORM_strx4
20388 || attr->form == DW_FORM_GNU_str_index
20389 || attr->form == DW_FORM_GNU_strp_alt)
20390 str = DW_STRING (attr);
20391 else
20392 complaint (_("string type expected for attribute %s for "
20393 "DIE at %s in module %s"),
20394 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20395 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20396 }
20397
20398 return str;
20399 }
20400
20401 /* Return the dwo name or NULL if not present. If present, it is in either
20402 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20403 static const char *
20404 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20405 {
20406 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20407 if (dwo_name == nullptr)
20408 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20409 return dwo_name;
20410 }
20411
20412 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20413 and holds a non-zero value. This function should only be used for
20414 DW_FORM_flag or DW_FORM_flag_present attributes. */
20415
20416 static int
20417 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20418 {
20419 struct attribute *attr = dwarf2_attr (die, name, cu);
20420
20421 return (attr && DW_UNSND (attr));
20422 }
20423
20424 static int
20425 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20426 {
20427 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20428 which value is non-zero. However, we have to be careful with
20429 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20430 (via dwarf2_flag_true_p) follows this attribute. So we may
20431 end up accidently finding a declaration attribute that belongs
20432 to a different DIE referenced by the specification attribute,
20433 even though the given DIE does not have a declaration attribute. */
20434 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20435 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20436 }
20437
20438 /* Return the die giving the specification for DIE, if there is
20439 one. *SPEC_CU is the CU containing DIE on input, and the CU
20440 containing the return value on output. If there is no
20441 specification, but there is an abstract origin, that is
20442 returned. */
20443
20444 static struct die_info *
20445 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20446 {
20447 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20448 *spec_cu);
20449
20450 if (spec_attr == NULL)
20451 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20452
20453 if (spec_attr == NULL)
20454 return NULL;
20455 else
20456 return follow_die_ref (die, spec_attr, spec_cu);
20457 }
20458
20459 /* Stub for free_line_header to match void * callback types. */
20460
20461 static void
20462 free_line_header_voidp (void *arg)
20463 {
20464 struct line_header *lh = (struct line_header *) arg;
20465
20466 delete lh;
20467 }
20468
20469 void
20470 line_header::add_include_dir (const char *include_dir)
20471 {
20472 if (dwarf_line_debug >= 2)
20473 {
20474 size_t new_size;
20475 if (version >= 5)
20476 new_size = m_include_dirs.size ();
20477 else
20478 new_size = m_include_dirs.size () + 1;
20479 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20480 new_size, include_dir);
20481 }
20482 m_include_dirs.push_back (include_dir);
20483 }
20484
20485 void
20486 line_header::add_file_name (const char *name,
20487 dir_index d_index,
20488 unsigned int mod_time,
20489 unsigned int length)
20490 {
20491 if (dwarf_line_debug >= 2)
20492 {
20493 size_t new_size;
20494 if (version >= 5)
20495 new_size = file_names_size ();
20496 else
20497 new_size = file_names_size () + 1;
20498 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20499 new_size, name);
20500 }
20501 m_file_names.emplace_back (name, d_index, mod_time, length);
20502 }
20503
20504 /* A convenience function to find the proper .debug_line section for a CU. */
20505
20506 static struct dwarf2_section_info *
20507 get_debug_line_section (struct dwarf2_cu *cu)
20508 {
20509 struct dwarf2_section_info *section;
20510 struct dwarf2_per_objfile *dwarf2_per_objfile
20511 = cu->per_cu->dwarf2_per_objfile;
20512
20513 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20514 DWO file. */
20515 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20516 section = &cu->dwo_unit->dwo_file->sections.line;
20517 else if (cu->per_cu->is_dwz)
20518 {
20519 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20520
20521 section = &dwz->line;
20522 }
20523 else
20524 section = &dwarf2_per_objfile->line;
20525
20526 return section;
20527 }
20528
20529 /* Read directory or file name entry format, starting with byte of
20530 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20531 entries count and the entries themselves in the described entry
20532 format. */
20533
20534 static void
20535 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20536 bfd *abfd, const gdb_byte **bufp,
20537 struct line_header *lh,
20538 const struct comp_unit_head *cu_header,
20539 void (*callback) (struct line_header *lh,
20540 const char *name,
20541 dir_index d_index,
20542 unsigned int mod_time,
20543 unsigned int length))
20544 {
20545 gdb_byte format_count, formati;
20546 ULONGEST data_count, datai;
20547 const gdb_byte *buf = *bufp;
20548 const gdb_byte *format_header_data;
20549 unsigned int bytes_read;
20550
20551 format_count = read_1_byte (abfd, buf);
20552 buf += 1;
20553 format_header_data = buf;
20554 for (formati = 0; formati < format_count; formati++)
20555 {
20556 read_unsigned_leb128 (abfd, buf, &bytes_read);
20557 buf += bytes_read;
20558 read_unsigned_leb128 (abfd, buf, &bytes_read);
20559 buf += bytes_read;
20560 }
20561
20562 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20563 buf += bytes_read;
20564 for (datai = 0; datai < data_count; datai++)
20565 {
20566 const gdb_byte *format = format_header_data;
20567 struct file_entry fe;
20568
20569 for (formati = 0; formati < format_count; formati++)
20570 {
20571 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20572 format += bytes_read;
20573
20574 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20575 format += bytes_read;
20576
20577 gdb::optional<const char *> string;
20578 gdb::optional<unsigned int> uint;
20579
20580 switch (form)
20581 {
20582 case DW_FORM_string:
20583 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20584 buf += bytes_read;
20585 break;
20586
20587 case DW_FORM_line_strp:
20588 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20589 abfd, buf,
20590 cu_header,
20591 &bytes_read));
20592 buf += bytes_read;
20593 break;
20594
20595 case DW_FORM_data1:
20596 uint.emplace (read_1_byte (abfd, buf));
20597 buf += 1;
20598 break;
20599
20600 case DW_FORM_data2:
20601 uint.emplace (read_2_bytes (abfd, buf));
20602 buf += 2;
20603 break;
20604
20605 case DW_FORM_data4:
20606 uint.emplace (read_4_bytes (abfd, buf));
20607 buf += 4;
20608 break;
20609
20610 case DW_FORM_data8:
20611 uint.emplace (read_8_bytes (abfd, buf));
20612 buf += 8;
20613 break;
20614
20615 case DW_FORM_data16:
20616 /* This is used for MD5, but file_entry does not record MD5s. */
20617 buf += 16;
20618 break;
20619
20620 case DW_FORM_udata:
20621 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20622 buf += bytes_read;
20623 break;
20624
20625 case DW_FORM_block:
20626 /* It is valid only for DW_LNCT_timestamp which is ignored by
20627 current GDB. */
20628 break;
20629 }
20630
20631 switch (content_type)
20632 {
20633 case DW_LNCT_path:
20634 if (string.has_value ())
20635 fe.name = *string;
20636 break;
20637 case DW_LNCT_directory_index:
20638 if (uint.has_value ())
20639 fe.d_index = (dir_index) *uint;
20640 break;
20641 case DW_LNCT_timestamp:
20642 if (uint.has_value ())
20643 fe.mod_time = *uint;
20644 break;
20645 case DW_LNCT_size:
20646 if (uint.has_value ())
20647 fe.length = *uint;
20648 break;
20649 case DW_LNCT_MD5:
20650 break;
20651 default:
20652 complaint (_("Unknown format content type %s"),
20653 pulongest (content_type));
20654 }
20655 }
20656
20657 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20658 }
20659
20660 *bufp = buf;
20661 }
20662
20663 /* Read the statement program header starting at OFFSET in
20664 .debug_line, or .debug_line.dwo. Return a pointer
20665 to a struct line_header, allocated using xmalloc.
20666 Returns NULL if there is a problem reading the header, e.g., if it
20667 has a version we don't understand.
20668
20669 NOTE: the strings in the include directory and file name tables of
20670 the returned object point into the dwarf line section buffer,
20671 and must not be freed. */
20672
20673 static line_header_up
20674 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20675 {
20676 const gdb_byte *line_ptr;
20677 unsigned int bytes_read, offset_size;
20678 int i;
20679 const char *cur_dir, *cur_file;
20680 struct dwarf2_section_info *section;
20681 bfd *abfd;
20682 struct dwarf2_per_objfile *dwarf2_per_objfile
20683 = cu->per_cu->dwarf2_per_objfile;
20684
20685 section = get_debug_line_section (cu);
20686 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20687 if (section->buffer == NULL)
20688 {
20689 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20690 complaint (_("missing .debug_line.dwo section"));
20691 else
20692 complaint (_("missing .debug_line section"));
20693 return 0;
20694 }
20695
20696 /* We can't do this until we know the section is non-empty.
20697 Only then do we know we have such a section. */
20698 abfd = get_section_bfd_owner (section);
20699
20700 /* Make sure that at least there's room for the total_length field.
20701 That could be 12 bytes long, but we're just going to fudge that. */
20702 if (to_underlying (sect_off) + 4 >= section->size)
20703 {
20704 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20705 return 0;
20706 }
20707
20708 line_header_up lh (new line_header ());
20709
20710 lh->sect_off = sect_off;
20711 lh->offset_in_dwz = cu->per_cu->is_dwz;
20712
20713 line_ptr = section->buffer + to_underlying (sect_off);
20714
20715 /* Read in the header. */
20716 lh->total_length =
20717 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20718 &bytes_read, &offset_size);
20719 line_ptr += bytes_read;
20720
20721 const gdb_byte *start_here = line_ptr;
20722
20723 if (line_ptr + lh->total_length > (section->buffer + section->size))
20724 {
20725 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20726 return 0;
20727 }
20728 lh->statement_program_end = start_here + lh->total_length;
20729 lh->version = read_2_bytes (abfd, line_ptr);
20730 line_ptr += 2;
20731 if (lh->version > 5)
20732 {
20733 /* This is a version we don't understand. The format could have
20734 changed in ways we don't handle properly so just punt. */
20735 complaint (_("unsupported version in .debug_line section"));
20736 return NULL;
20737 }
20738 if (lh->version >= 5)
20739 {
20740 gdb_byte segment_selector_size;
20741
20742 /* Skip address size. */
20743 read_1_byte (abfd, line_ptr);
20744 line_ptr += 1;
20745
20746 segment_selector_size = read_1_byte (abfd, line_ptr);
20747 line_ptr += 1;
20748 if (segment_selector_size != 0)
20749 {
20750 complaint (_("unsupported segment selector size %u "
20751 "in .debug_line section"),
20752 segment_selector_size);
20753 return NULL;
20754 }
20755 }
20756 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20757 line_ptr += offset_size;
20758 lh->statement_program_start = line_ptr + lh->header_length;
20759 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20760 line_ptr += 1;
20761 if (lh->version >= 4)
20762 {
20763 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20764 line_ptr += 1;
20765 }
20766 else
20767 lh->maximum_ops_per_instruction = 1;
20768
20769 if (lh->maximum_ops_per_instruction == 0)
20770 {
20771 lh->maximum_ops_per_instruction = 1;
20772 complaint (_("invalid maximum_ops_per_instruction "
20773 "in `.debug_line' section"));
20774 }
20775
20776 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20777 line_ptr += 1;
20778 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20779 line_ptr += 1;
20780 lh->line_range = read_1_byte (abfd, line_ptr);
20781 line_ptr += 1;
20782 lh->opcode_base = read_1_byte (abfd, line_ptr);
20783 line_ptr += 1;
20784 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20785
20786 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20787 for (i = 1; i < lh->opcode_base; ++i)
20788 {
20789 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20790 line_ptr += 1;
20791 }
20792
20793 if (lh->version >= 5)
20794 {
20795 /* Read directory table. */
20796 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20797 &cu->header,
20798 [] (struct line_header *header, const char *name,
20799 dir_index d_index, unsigned int mod_time,
20800 unsigned int length)
20801 {
20802 header->add_include_dir (name);
20803 });
20804
20805 /* Read file name table. */
20806 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20807 &cu->header,
20808 [] (struct line_header *header, const char *name,
20809 dir_index d_index, unsigned int mod_time,
20810 unsigned int length)
20811 {
20812 header->add_file_name (name, d_index, mod_time, length);
20813 });
20814 }
20815 else
20816 {
20817 /* Read directory table. */
20818 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20819 {
20820 line_ptr += bytes_read;
20821 lh->add_include_dir (cur_dir);
20822 }
20823 line_ptr += bytes_read;
20824
20825 /* Read file name table. */
20826 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20827 {
20828 unsigned int mod_time, length;
20829 dir_index d_index;
20830
20831 line_ptr += bytes_read;
20832 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20833 line_ptr += bytes_read;
20834 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20835 line_ptr += bytes_read;
20836 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20837 line_ptr += bytes_read;
20838
20839 lh->add_file_name (cur_file, d_index, mod_time, length);
20840 }
20841 line_ptr += bytes_read;
20842 }
20843
20844 if (line_ptr > (section->buffer + section->size))
20845 complaint (_("line number info header doesn't "
20846 "fit in `.debug_line' section"));
20847
20848 return lh;
20849 }
20850
20851 /* Subroutine of dwarf_decode_lines to simplify it.
20852 Return the file name of the psymtab for the given file_entry.
20853 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20854 If space for the result is malloc'd, *NAME_HOLDER will be set.
20855 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20856
20857 static const char *
20858 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20859 const struct partial_symtab *pst,
20860 const char *comp_dir,
20861 gdb::unique_xmalloc_ptr<char> *name_holder)
20862 {
20863 const char *include_name = fe.name;
20864 const char *include_name_to_compare = include_name;
20865 const char *pst_filename;
20866 int file_is_pst;
20867
20868 const char *dir_name = fe.include_dir (lh);
20869
20870 gdb::unique_xmalloc_ptr<char> hold_compare;
20871 if (!IS_ABSOLUTE_PATH (include_name)
20872 && (dir_name != NULL || comp_dir != NULL))
20873 {
20874 /* Avoid creating a duplicate psymtab for PST.
20875 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20876 Before we do the comparison, however, we need to account
20877 for DIR_NAME and COMP_DIR.
20878 First prepend dir_name (if non-NULL). If we still don't
20879 have an absolute path prepend comp_dir (if non-NULL).
20880 However, the directory we record in the include-file's
20881 psymtab does not contain COMP_DIR (to match the
20882 corresponding symtab(s)).
20883
20884 Example:
20885
20886 bash$ cd /tmp
20887 bash$ gcc -g ./hello.c
20888 include_name = "hello.c"
20889 dir_name = "."
20890 DW_AT_comp_dir = comp_dir = "/tmp"
20891 DW_AT_name = "./hello.c"
20892
20893 */
20894
20895 if (dir_name != NULL)
20896 {
20897 name_holder->reset (concat (dir_name, SLASH_STRING,
20898 include_name, (char *) NULL));
20899 include_name = name_holder->get ();
20900 include_name_to_compare = include_name;
20901 }
20902 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20903 {
20904 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20905 include_name, (char *) NULL));
20906 include_name_to_compare = hold_compare.get ();
20907 }
20908 }
20909
20910 pst_filename = pst->filename;
20911 gdb::unique_xmalloc_ptr<char> copied_name;
20912 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20913 {
20914 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20915 pst_filename, (char *) NULL));
20916 pst_filename = copied_name.get ();
20917 }
20918
20919 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20920
20921 if (file_is_pst)
20922 return NULL;
20923 return include_name;
20924 }
20925
20926 /* State machine to track the state of the line number program. */
20927
20928 class lnp_state_machine
20929 {
20930 public:
20931 /* Initialize a machine state for the start of a line number
20932 program. */
20933 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20934 bool record_lines_p);
20935
20936 file_entry *current_file ()
20937 {
20938 /* lh->file_names is 0-based, but the file name numbers in the
20939 statement program are 1-based. */
20940 return m_line_header->file_name_at (m_file);
20941 }
20942
20943 /* Record the line in the state machine. END_SEQUENCE is true if
20944 we're processing the end of a sequence. */
20945 void record_line (bool end_sequence);
20946
20947 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20948 nop-out rest of the lines in this sequence. */
20949 void check_line_address (struct dwarf2_cu *cu,
20950 const gdb_byte *line_ptr,
20951 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20952
20953 void handle_set_discriminator (unsigned int discriminator)
20954 {
20955 m_discriminator = discriminator;
20956 m_line_has_non_zero_discriminator |= discriminator != 0;
20957 }
20958
20959 /* Handle DW_LNE_set_address. */
20960 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20961 {
20962 m_op_index = 0;
20963 address += baseaddr;
20964 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20965 }
20966
20967 /* Handle DW_LNS_advance_pc. */
20968 void handle_advance_pc (CORE_ADDR adjust);
20969
20970 /* Handle a special opcode. */
20971 void handle_special_opcode (unsigned char op_code);
20972
20973 /* Handle DW_LNS_advance_line. */
20974 void handle_advance_line (int line_delta)
20975 {
20976 advance_line (line_delta);
20977 }
20978
20979 /* Handle DW_LNS_set_file. */
20980 void handle_set_file (file_name_index file);
20981
20982 /* Handle DW_LNS_negate_stmt. */
20983 void handle_negate_stmt ()
20984 {
20985 m_is_stmt = !m_is_stmt;
20986 }
20987
20988 /* Handle DW_LNS_const_add_pc. */
20989 void handle_const_add_pc ();
20990
20991 /* Handle DW_LNS_fixed_advance_pc. */
20992 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20993 {
20994 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20995 m_op_index = 0;
20996 }
20997
20998 /* Handle DW_LNS_copy. */
20999 void handle_copy ()
21000 {
21001 record_line (false);
21002 m_discriminator = 0;
21003 }
21004
21005 /* Handle DW_LNE_end_sequence. */
21006 void handle_end_sequence ()
21007 {
21008 m_currently_recording_lines = true;
21009 }
21010
21011 private:
21012 /* Advance the line by LINE_DELTA. */
21013 void advance_line (int line_delta)
21014 {
21015 m_line += line_delta;
21016
21017 if (line_delta != 0)
21018 m_line_has_non_zero_discriminator = m_discriminator != 0;
21019 }
21020
21021 struct dwarf2_cu *m_cu;
21022
21023 gdbarch *m_gdbarch;
21024
21025 /* True if we're recording lines.
21026 Otherwise we're building partial symtabs and are just interested in
21027 finding include files mentioned by the line number program. */
21028 bool m_record_lines_p;
21029
21030 /* The line number header. */
21031 line_header *m_line_header;
21032
21033 /* These are part of the standard DWARF line number state machine,
21034 and initialized according to the DWARF spec. */
21035
21036 unsigned char m_op_index = 0;
21037 /* The line table index of the current file. */
21038 file_name_index m_file = 1;
21039 unsigned int m_line = 1;
21040
21041 /* These are initialized in the constructor. */
21042
21043 CORE_ADDR m_address;
21044 bool m_is_stmt;
21045 unsigned int m_discriminator;
21046
21047 /* Additional bits of state we need to track. */
21048
21049 /* The last file that we called dwarf2_start_subfile for.
21050 This is only used for TLLs. */
21051 unsigned int m_last_file = 0;
21052 /* The last file a line number was recorded for. */
21053 struct subfile *m_last_subfile = NULL;
21054
21055 /* When true, record the lines we decode. */
21056 bool m_currently_recording_lines = false;
21057
21058 /* The last line number that was recorded, used to coalesce
21059 consecutive entries for the same line. This can happen, for
21060 example, when discriminators are present. PR 17276. */
21061 unsigned int m_last_line = 0;
21062 bool m_line_has_non_zero_discriminator = false;
21063 };
21064
21065 void
21066 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21067 {
21068 CORE_ADDR addr_adj = (((m_op_index + adjust)
21069 / m_line_header->maximum_ops_per_instruction)
21070 * m_line_header->minimum_instruction_length);
21071 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21072 m_op_index = ((m_op_index + adjust)
21073 % m_line_header->maximum_ops_per_instruction);
21074 }
21075
21076 void
21077 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21078 {
21079 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21080 CORE_ADDR addr_adj = (((m_op_index
21081 + (adj_opcode / m_line_header->line_range))
21082 / m_line_header->maximum_ops_per_instruction)
21083 * m_line_header->minimum_instruction_length);
21084 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21085 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21086 % m_line_header->maximum_ops_per_instruction);
21087
21088 int line_delta = (m_line_header->line_base
21089 + (adj_opcode % m_line_header->line_range));
21090 advance_line (line_delta);
21091 record_line (false);
21092 m_discriminator = 0;
21093 }
21094
21095 void
21096 lnp_state_machine::handle_set_file (file_name_index file)
21097 {
21098 m_file = file;
21099
21100 const file_entry *fe = current_file ();
21101 if (fe == NULL)
21102 dwarf2_debug_line_missing_file_complaint ();
21103 else if (m_record_lines_p)
21104 {
21105 const char *dir = fe->include_dir (m_line_header);
21106
21107 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21108 m_line_has_non_zero_discriminator = m_discriminator != 0;
21109 dwarf2_start_subfile (m_cu, fe->name, dir);
21110 }
21111 }
21112
21113 void
21114 lnp_state_machine::handle_const_add_pc ()
21115 {
21116 CORE_ADDR adjust
21117 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21118
21119 CORE_ADDR addr_adj
21120 = (((m_op_index + adjust)
21121 / m_line_header->maximum_ops_per_instruction)
21122 * m_line_header->minimum_instruction_length);
21123
21124 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21125 m_op_index = ((m_op_index + adjust)
21126 % m_line_header->maximum_ops_per_instruction);
21127 }
21128
21129 /* Return non-zero if we should add LINE to the line number table.
21130 LINE is the line to add, LAST_LINE is the last line that was added,
21131 LAST_SUBFILE is the subfile for LAST_LINE.
21132 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21133 had a non-zero discriminator.
21134
21135 We have to be careful in the presence of discriminators.
21136 E.g., for this line:
21137
21138 for (i = 0; i < 100000; i++);
21139
21140 clang can emit four line number entries for that one line,
21141 each with a different discriminator.
21142 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21143
21144 However, we want gdb to coalesce all four entries into one.
21145 Otherwise the user could stepi into the middle of the line and
21146 gdb would get confused about whether the pc really was in the
21147 middle of the line.
21148
21149 Things are further complicated by the fact that two consecutive
21150 line number entries for the same line is a heuristic used by gcc
21151 to denote the end of the prologue. So we can't just discard duplicate
21152 entries, we have to be selective about it. The heuristic we use is
21153 that we only collapse consecutive entries for the same line if at least
21154 one of those entries has a non-zero discriminator. PR 17276.
21155
21156 Note: Addresses in the line number state machine can never go backwards
21157 within one sequence, thus this coalescing is ok. */
21158
21159 static int
21160 dwarf_record_line_p (struct dwarf2_cu *cu,
21161 unsigned int line, unsigned int last_line,
21162 int line_has_non_zero_discriminator,
21163 struct subfile *last_subfile)
21164 {
21165 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21166 return 1;
21167 if (line != last_line)
21168 return 1;
21169 /* Same line for the same file that we've seen already.
21170 As a last check, for pr 17276, only record the line if the line
21171 has never had a non-zero discriminator. */
21172 if (!line_has_non_zero_discriminator)
21173 return 1;
21174 return 0;
21175 }
21176
21177 /* Use the CU's builder to record line number LINE beginning at
21178 address ADDRESS in the line table of subfile SUBFILE. */
21179
21180 static void
21181 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21182 unsigned int line, CORE_ADDR address,
21183 struct dwarf2_cu *cu)
21184 {
21185 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21186
21187 if (dwarf_line_debug)
21188 {
21189 fprintf_unfiltered (gdb_stdlog,
21190 "Recording line %u, file %s, address %s\n",
21191 line, lbasename (subfile->name),
21192 paddress (gdbarch, address));
21193 }
21194
21195 if (cu != nullptr)
21196 cu->get_builder ()->record_line (subfile, line, addr);
21197 }
21198
21199 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21200 Mark the end of a set of line number records.
21201 The arguments are the same as for dwarf_record_line_1.
21202 If SUBFILE is NULL the request is ignored. */
21203
21204 static void
21205 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21206 CORE_ADDR address, struct dwarf2_cu *cu)
21207 {
21208 if (subfile == NULL)
21209 return;
21210
21211 if (dwarf_line_debug)
21212 {
21213 fprintf_unfiltered (gdb_stdlog,
21214 "Finishing current line, file %s, address %s\n",
21215 lbasename (subfile->name),
21216 paddress (gdbarch, address));
21217 }
21218
21219 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21220 }
21221
21222 void
21223 lnp_state_machine::record_line (bool end_sequence)
21224 {
21225 if (dwarf_line_debug)
21226 {
21227 fprintf_unfiltered (gdb_stdlog,
21228 "Processing actual line %u: file %u,"
21229 " address %s, is_stmt %u, discrim %u\n",
21230 m_line, m_file,
21231 paddress (m_gdbarch, m_address),
21232 m_is_stmt, m_discriminator);
21233 }
21234
21235 file_entry *fe = current_file ();
21236
21237 if (fe == NULL)
21238 dwarf2_debug_line_missing_file_complaint ();
21239 /* For now we ignore lines not starting on an instruction boundary.
21240 But not when processing end_sequence for compatibility with the
21241 previous version of the code. */
21242 else if (m_op_index == 0 || end_sequence)
21243 {
21244 fe->included_p = 1;
21245 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
21246 {
21247 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21248 || end_sequence)
21249 {
21250 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21251 m_currently_recording_lines ? m_cu : nullptr);
21252 }
21253
21254 if (!end_sequence)
21255 {
21256 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21257 m_line_has_non_zero_discriminator,
21258 m_last_subfile))
21259 {
21260 buildsym_compunit *builder = m_cu->get_builder ();
21261 dwarf_record_line_1 (m_gdbarch,
21262 builder->get_current_subfile (),
21263 m_line, m_address,
21264 m_currently_recording_lines ? m_cu : nullptr);
21265 }
21266 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21267 m_last_line = m_line;
21268 }
21269 }
21270 }
21271 }
21272
21273 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21274 line_header *lh, bool record_lines_p)
21275 {
21276 m_cu = cu;
21277 m_gdbarch = arch;
21278 m_record_lines_p = record_lines_p;
21279 m_line_header = lh;
21280
21281 m_currently_recording_lines = true;
21282
21283 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21284 was a line entry for it so that the backend has a chance to adjust it
21285 and also record it in case it needs it. This is currently used by MIPS
21286 code, cf. `mips_adjust_dwarf2_line'. */
21287 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21288 m_is_stmt = lh->default_is_stmt;
21289 m_discriminator = 0;
21290 }
21291
21292 void
21293 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21294 const gdb_byte *line_ptr,
21295 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21296 {
21297 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21298 the pc range of the CU. However, we restrict the test to only ADDRESS
21299 values of zero to preserve GDB's previous behaviour which is to handle
21300 the specific case of a function being GC'd by the linker. */
21301
21302 if (address == 0 && address < unrelocated_lowpc)
21303 {
21304 /* This line table is for a function which has been
21305 GCd by the linker. Ignore it. PR gdb/12528 */
21306
21307 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21308 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21309
21310 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21311 line_offset, objfile_name (objfile));
21312 m_currently_recording_lines = false;
21313 /* Note: m_currently_recording_lines is left as false until we see
21314 DW_LNE_end_sequence. */
21315 }
21316 }
21317
21318 /* Subroutine of dwarf_decode_lines to simplify it.
21319 Process the line number information in LH.
21320 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21321 program in order to set included_p for every referenced header. */
21322
21323 static void
21324 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21325 const int decode_for_pst_p, CORE_ADDR lowpc)
21326 {
21327 const gdb_byte *line_ptr, *extended_end;
21328 const gdb_byte *line_end;
21329 unsigned int bytes_read, extended_len;
21330 unsigned char op_code, extended_op;
21331 CORE_ADDR baseaddr;
21332 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21333 bfd *abfd = objfile->obfd;
21334 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21335 /* True if we're recording line info (as opposed to building partial
21336 symtabs and just interested in finding include files mentioned by
21337 the line number program). */
21338 bool record_lines_p = !decode_for_pst_p;
21339
21340 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21341
21342 line_ptr = lh->statement_program_start;
21343 line_end = lh->statement_program_end;
21344
21345 /* Read the statement sequences until there's nothing left. */
21346 while (line_ptr < line_end)
21347 {
21348 /* The DWARF line number program state machine. Reset the state
21349 machine at the start of each sequence. */
21350 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21351 bool end_sequence = false;
21352
21353 if (record_lines_p)
21354 {
21355 /* Start a subfile for the current file of the state
21356 machine. */
21357 const file_entry *fe = state_machine.current_file ();
21358
21359 if (fe != NULL)
21360 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21361 }
21362
21363 /* Decode the table. */
21364 while (line_ptr < line_end && !end_sequence)
21365 {
21366 op_code = read_1_byte (abfd, line_ptr);
21367 line_ptr += 1;
21368
21369 if (op_code >= lh->opcode_base)
21370 {
21371 /* Special opcode. */
21372 state_machine.handle_special_opcode (op_code);
21373 }
21374 else switch (op_code)
21375 {
21376 case DW_LNS_extended_op:
21377 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21378 &bytes_read);
21379 line_ptr += bytes_read;
21380 extended_end = line_ptr + extended_len;
21381 extended_op = read_1_byte (abfd, line_ptr);
21382 line_ptr += 1;
21383 switch (extended_op)
21384 {
21385 case DW_LNE_end_sequence:
21386 state_machine.handle_end_sequence ();
21387 end_sequence = true;
21388 break;
21389 case DW_LNE_set_address:
21390 {
21391 CORE_ADDR address
21392 = read_address (abfd, line_ptr, cu, &bytes_read);
21393 line_ptr += bytes_read;
21394
21395 state_machine.check_line_address (cu, line_ptr,
21396 lowpc - baseaddr, address);
21397 state_machine.handle_set_address (baseaddr, address);
21398 }
21399 break;
21400 case DW_LNE_define_file:
21401 {
21402 const char *cur_file;
21403 unsigned int mod_time, length;
21404 dir_index dindex;
21405
21406 cur_file = read_direct_string (abfd, line_ptr,
21407 &bytes_read);
21408 line_ptr += bytes_read;
21409 dindex = (dir_index)
21410 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21411 line_ptr += bytes_read;
21412 mod_time =
21413 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21414 line_ptr += bytes_read;
21415 length =
21416 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21417 line_ptr += bytes_read;
21418 lh->add_file_name (cur_file, dindex, mod_time, length);
21419 }
21420 break;
21421 case DW_LNE_set_discriminator:
21422 {
21423 /* The discriminator is not interesting to the
21424 debugger; just ignore it. We still need to
21425 check its value though:
21426 if there are consecutive entries for the same
21427 (non-prologue) line we want to coalesce them.
21428 PR 17276. */
21429 unsigned int discr
21430 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21431 line_ptr += bytes_read;
21432
21433 state_machine.handle_set_discriminator (discr);
21434 }
21435 break;
21436 default:
21437 complaint (_("mangled .debug_line section"));
21438 return;
21439 }
21440 /* Make sure that we parsed the extended op correctly. If e.g.
21441 we expected a different address size than the producer used,
21442 we may have read the wrong number of bytes. */
21443 if (line_ptr != extended_end)
21444 {
21445 complaint (_("mangled .debug_line section"));
21446 return;
21447 }
21448 break;
21449 case DW_LNS_copy:
21450 state_machine.handle_copy ();
21451 break;
21452 case DW_LNS_advance_pc:
21453 {
21454 CORE_ADDR adjust
21455 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21456 line_ptr += bytes_read;
21457
21458 state_machine.handle_advance_pc (adjust);
21459 }
21460 break;
21461 case DW_LNS_advance_line:
21462 {
21463 int line_delta
21464 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21465 line_ptr += bytes_read;
21466
21467 state_machine.handle_advance_line (line_delta);
21468 }
21469 break;
21470 case DW_LNS_set_file:
21471 {
21472 file_name_index file
21473 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21474 &bytes_read);
21475 line_ptr += bytes_read;
21476
21477 state_machine.handle_set_file (file);
21478 }
21479 break;
21480 case DW_LNS_set_column:
21481 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21482 line_ptr += bytes_read;
21483 break;
21484 case DW_LNS_negate_stmt:
21485 state_machine.handle_negate_stmt ();
21486 break;
21487 case DW_LNS_set_basic_block:
21488 break;
21489 /* Add to the address register of the state machine the
21490 address increment value corresponding to special opcode
21491 255. I.e., this value is scaled by the minimum
21492 instruction length since special opcode 255 would have
21493 scaled the increment. */
21494 case DW_LNS_const_add_pc:
21495 state_machine.handle_const_add_pc ();
21496 break;
21497 case DW_LNS_fixed_advance_pc:
21498 {
21499 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21500 line_ptr += 2;
21501
21502 state_machine.handle_fixed_advance_pc (addr_adj);
21503 }
21504 break;
21505 default:
21506 {
21507 /* Unknown standard opcode, ignore it. */
21508 int i;
21509
21510 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21511 {
21512 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21513 line_ptr += bytes_read;
21514 }
21515 }
21516 }
21517 }
21518
21519 if (!end_sequence)
21520 dwarf2_debug_line_missing_end_sequence_complaint ();
21521
21522 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21523 in which case we still finish recording the last line). */
21524 state_machine.record_line (true);
21525 }
21526 }
21527
21528 /* Decode the Line Number Program (LNP) for the given line_header
21529 structure and CU. The actual information extracted and the type
21530 of structures created from the LNP depends on the value of PST.
21531
21532 1. If PST is NULL, then this procedure uses the data from the program
21533 to create all necessary symbol tables, and their linetables.
21534
21535 2. If PST is not NULL, this procedure reads the program to determine
21536 the list of files included by the unit represented by PST, and
21537 builds all the associated partial symbol tables.
21538
21539 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21540 It is used for relative paths in the line table.
21541 NOTE: When processing partial symtabs (pst != NULL),
21542 comp_dir == pst->dirname.
21543
21544 NOTE: It is important that psymtabs have the same file name (via strcmp)
21545 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21546 symtab we don't use it in the name of the psymtabs we create.
21547 E.g. expand_line_sal requires this when finding psymtabs to expand.
21548 A good testcase for this is mb-inline.exp.
21549
21550 LOWPC is the lowest address in CU (or 0 if not known).
21551
21552 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21553 for its PC<->lines mapping information. Otherwise only the filename
21554 table is read in. */
21555
21556 static void
21557 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21558 struct dwarf2_cu *cu, struct partial_symtab *pst,
21559 CORE_ADDR lowpc, int decode_mapping)
21560 {
21561 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21562 const int decode_for_pst_p = (pst != NULL);
21563
21564 if (decode_mapping)
21565 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21566
21567 if (decode_for_pst_p)
21568 {
21569 /* Now that we're done scanning the Line Header Program, we can
21570 create the psymtab of each included file. */
21571 for (auto &file_entry : lh->file_names ())
21572 if (file_entry.included_p == 1)
21573 {
21574 gdb::unique_xmalloc_ptr<char> name_holder;
21575 const char *include_name =
21576 psymtab_include_file_name (lh, file_entry, pst,
21577 comp_dir, &name_holder);
21578 if (include_name != NULL)
21579 dwarf2_create_include_psymtab (include_name, pst, objfile);
21580 }
21581 }
21582 else
21583 {
21584 /* Make sure a symtab is created for every file, even files
21585 which contain only variables (i.e. no code with associated
21586 line numbers). */
21587 buildsym_compunit *builder = cu->get_builder ();
21588 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21589
21590 for (auto &fe : lh->file_names ())
21591 {
21592 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21593 if (builder->get_current_subfile ()->symtab == NULL)
21594 {
21595 builder->get_current_subfile ()->symtab
21596 = allocate_symtab (cust,
21597 builder->get_current_subfile ()->name);
21598 }
21599 fe.symtab = builder->get_current_subfile ()->symtab;
21600 }
21601 }
21602 }
21603
21604 /* Start a subfile for DWARF. FILENAME is the name of the file and
21605 DIRNAME the name of the source directory which contains FILENAME
21606 or NULL if not known.
21607 This routine tries to keep line numbers from identical absolute and
21608 relative file names in a common subfile.
21609
21610 Using the `list' example from the GDB testsuite, which resides in
21611 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21612 of /srcdir/list0.c yields the following debugging information for list0.c:
21613
21614 DW_AT_name: /srcdir/list0.c
21615 DW_AT_comp_dir: /compdir
21616 files.files[0].name: list0.h
21617 files.files[0].dir: /srcdir
21618 files.files[1].name: list0.c
21619 files.files[1].dir: /srcdir
21620
21621 The line number information for list0.c has to end up in a single
21622 subfile, so that `break /srcdir/list0.c:1' works as expected.
21623 start_subfile will ensure that this happens provided that we pass the
21624 concatenation of files.files[1].dir and files.files[1].name as the
21625 subfile's name. */
21626
21627 static void
21628 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21629 const char *dirname)
21630 {
21631 char *copy = NULL;
21632
21633 /* In order not to lose the line information directory,
21634 we concatenate it to the filename when it makes sense.
21635 Note that the Dwarf3 standard says (speaking of filenames in line
21636 information): ``The directory index is ignored for file names
21637 that represent full path names''. Thus ignoring dirname in the
21638 `else' branch below isn't an issue. */
21639
21640 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21641 {
21642 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21643 filename = copy;
21644 }
21645
21646 cu->get_builder ()->start_subfile (filename);
21647
21648 if (copy != NULL)
21649 xfree (copy);
21650 }
21651
21652 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21653 buildsym_compunit constructor. */
21654
21655 struct compunit_symtab *
21656 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21657 CORE_ADDR low_pc)
21658 {
21659 gdb_assert (m_builder == nullptr);
21660
21661 m_builder.reset (new struct buildsym_compunit
21662 (per_cu->dwarf2_per_objfile->objfile,
21663 name, comp_dir, language, low_pc));
21664
21665 list_in_scope = get_builder ()->get_file_symbols ();
21666
21667 get_builder ()->record_debugformat (xstrprintf ("DWARF %d", this->header.version));
21668 get_builder ()->record_producer (producer);
21669
21670 processing_has_namespace_info = false;
21671
21672 return get_builder ()->get_compunit_symtab ();
21673 }
21674
21675 static void
21676 var_decode_location (struct attribute *attr, struct symbol *sym,
21677 struct dwarf2_cu *cu)
21678 {
21679 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21680 struct comp_unit_head *cu_header = &cu->header;
21681
21682 /* NOTE drow/2003-01-30: There used to be a comment and some special
21683 code here to turn a symbol with DW_AT_external and a
21684 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21685 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21686 with some versions of binutils) where shared libraries could have
21687 relocations against symbols in their debug information - the
21688 minimal symbol would have the right address, but the debug info
21689 would not. It's no longer necessary, because we will explicitly
21690 apply relocations when we read in the debug information now. */
21691
21692 /* A DW_AT_location attribute with no contents indicates that a
21693 variable has been optimized away. */
21694 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21695 {
21696 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21697 return;
21698 }
21699
21700 /* Handle one degenerate form of location expression specially, to
21701 preserve GDB's previous behavior when section offsets are
21702 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21703 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21704
21705 if (attr_form_is_block (attr)
21706 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21707 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21708 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21709 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21710 && (DW_BLOCK (attr)->size
21711 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21712 {
21713 unsigned int dummy;
21714
21715 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21716 SET_SYMBOL_VALUE_ADDRESS (sym,
21717 read_address (objfile->obfd,
21718 DW_BLOCK (attr)->data + 1,
21719 cu, &dummy));
21720 else
21721 SET_SYMBOL_VALUE_ADDRESS
21722 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21723 &dummy));
21724 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21725 fixup_symbol_section (sym, objfile);
21726 SET_SYMBOL_VALUE_ADDRESS (sym,
21727 SYMBOL_VALUE_ADDRESS (sym)
21728 + ANOFFSET (objfile->section_offsets,
21729 SYMBOL_SECTION (sym)));
21730 return;
21731 }
21732
21733 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21734 expression evaluator, and use LOC_COMPUTED only when necessary
21735 (i.e. when the value of a register or memory location is
21736 referenced, or a thread-local block, etc.). Then again, it might
21737 not be worthwhile. I'm assuming that it isn't unless performance
21738 or memory numbers show me otherwise. */
21739
21740 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21741
21742 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21743 cu->has_loclist = true;
21744 }
21745
21746 /* Given a pointer to a DWARF information entry, figure out if we need
21747 to make a symbol table entry for it, and if so, create a new entry
21748 and return a pointer to it.
21749 If TYPE is NULL, determine symbol type from the die, otherwise
21750 used the passed type.
21751 If SPACE is not NULL, use it to hold the new symbol. If it is
21752 NULL, allocate a new symbol on the objfile's obstack. */
21753
21754 static struct symbol *
21755 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21756 struct symbol *space)
21757 {
21758 struct dwarf2_per_objfile *dwarf2_per_objfile
21759 = cu->per_cu->dwarf2_per_objfile;
21760 struct objfile *objfile = dwarf2_per_objfile->objfile;
21761 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21762 struct symbol *sym = NULL;
21763 const char *name;
21764 struct attribute *attr = NULL;
21765 struct attribute *attr2 = NULL;
21766 CORE_ADDR baseaddr;
21767 struct pending **list_to_add = NULL;
21768
21769 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21770
21771 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21772
21773 name = dwarf2_name (die, cu);
21774 if (name)
21775 {
21776 const char *linkagename;
21777 int suppress_add = 0;
21778
21779 if (space)
21780 sym = space;
21781 else
21782 sym = allocate_symbol (objfile);
21783 OBJSTAT (objfile, n_syms++);
21784
21785 /* Cache this symbol's name and the name's demangled form (if any). */
21786 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21787 linkagename = dwarf2_physname (name, die, cu);
21788 SYMBOL_SET_NAMES (sym, linkagename, false, objfile);
21789
21790 /* Fortran does not have mangling standard and the mangling does differ
21791 between gfortran, iFort etc. */
21792 if (cu->language == language_fortran
21793 && symbol_get_demangled_name (sym) == NULL)
21794 symbol_set_demangled_name (sym,
21795 dwarf2_full_name (name, die, cu),
21796 NULL);
21797
21798 /* Default assumptions.
21799 Use the passed type or decode it from the die. */
21800 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21801 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21802 if (type != NULL)
21803 SYMBOL_TYPE (sym) = type;
21804 else
21805 SYMBOL_TYPE (sym) = die_type (die, cu);
21806 attr = dwarf2_attr (die,
21807 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21808 cu);
21809 if (attr != nullptr)
21810 {
21811 SYMBOL_LINE (sym) = DW_UNSND (attr);
21812 }
21813
21814 attr = dwarf2_attr (die,
21815 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21816 cu);
21817 if (attr != nullptr)
21818 {
21819 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21820 struct file_entry *fe;
21821
21822 if (cu->line_header != NULL)
21823 fe = cu->line_header->file_name_at (file_index);
21824 else
21825 fe = NULL;
21826
21827 if (fe == NULL)
21828 complaint (_("file index out of range"));
21829 else
21830 symbol_set_symtab (sym, fe->symtab);
21831 }
21832
21833 switch (die->tag)
21834 {
21835 case DW_TAG_label:
21836 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21837 if (attr != nullptr)
21838 {
21839 CORE_ADDR addr;
21840
21841 addr = attr_value_as_address (attr);
21842 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21843 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21844 }
21845 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21846 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21847 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21848 add_symbol_to_list (sym, cu->list_in_scope);
21849 break;
21850 case DW_TAG_subprogram:
21851 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21852 finish_block. */
21853 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21854 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21855 if ((attr2 && (DW_UNSND (attr2) != 0))
21856 || cu->language == language_ada
21857 || cu->language == language_fortran)
21858 {
21859 /* Subprograms marked external are stored as a global symbol.
21860 Ada and Fortran subprograms, whether marked external or
21861 not, are always stored as a global symbol, because we want
21862 to be able to access them globally. For instance, we want
21863 to be able to break on a nested subprogram without having
21864 to specify the context. */
21865 list_to_add = cu->get_builder ()->get_global_symbols ();
21866 }
21867 else
21868 {
21869 list_to_add = cu->list_in_scope;
21870 }
21871 break;
21872 case DW_TAG_inlined_subroutine:
21873 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21874 finish_block. */
21875 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21876 SYMBOL_INLINED (sym) = 1;
21877 list_to_add = cu->list_in_scope;
21878 break;
21879 case DW_TAG_template_value_param:
21880 suppress_add = 1;
21881 /* Fall through. */
21882 case DW_TAG_constant:
21883 case DW_TAG_variable:
21884 case DW_TAG_member:
21885 /* Compilation with minimal debug info may result in
21886 variables with missing type entries. Change the
21887 misleading `void' type to something sensible. */
21888 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21889 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21890
21891 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21892 /* In the case of DW_TAG_member, we should only be called for
21893 static const members. */
21894 if (die->tag == DW_TAG_member)
21895 {
21896 /* dwarf2_add_field uses die_is_declaration,
21897 so we do the same. */
21898 gdb_assert (die_is_declaration (die, cu));
21899 gdb_assert (attr);
21900 }
21901 if (attr != nullptr)
21902 {
21903 dwarf2_const_value (attr, sym, cu);
21904 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21905 if (!suppress_add)
21906 {
21907 if (attr2 && (DW_UNSND (attr2) != 0))
21908 list_to_add = cu->get_builder ()->get_global_symbols ();
21909 else
21910 list_to_add = cu->list_in_scope;
21911 }
21912 break;
21913 }
21914 attr = dwarf2_attr (die, DW_AT_location, cu);
21915 if (attr != nullptr)
21916 {
21917 var_decode_location (attr, sym, cu);
21918 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21919
21920 /* Fortran explicitly imports any global symbols to the local
21921 scope by DW_TAG_common_block. */
21922 if (cu->language == language_fortran && die->parent
21923 && die->parent->tag == DW_TAG_common_block)
21924 attr2 = NULL;
21925
21926 if (SYMBOL_CLASS (sym) == LOC_STATIC
21927 && SYMBOL_VALUE_ADDRESS (sym) == 0
21928 && !dwarf2_per_objfile->has_section_at_zero)
21929 {
21930 /* When a static variable is eliminated by the linker,
21931 the corresponding debug information is not stripped
21932 out, but the variable address is set to null;
21933 do not add such variables into symbol table. */
21934 }
21935 else if (attr2 && (DW_UNSND (attr2) != 0))
21936 {
21937 if (SYMBOL_CLASS (sym) == LOC_STATIC
21938 && (objfile->flags & OBJF_MAINLINE) == 0
21939 && dwarf2_per_objfile->can_copy)
21940 {
21941 /* A global static variable might be subject to
21942 copy relocation. We first check for a local
21943 minsym, though, because maybe the symbol was
21944 marked hidden, in which case this would not
21945 apply. */
21946 bound_minimal_symbol found
21947 = (lookup_minimal_symbol_linkage
21948 (sym->linkage_name (), objfile));
21949 if (found.minsym != nullptr)
21950 sym->maybe_copied = 1;
21951 }
21952
21953 /* A variable with DW_AT_external is never static,
21954 but it may be block-scoped. */
21955 list_to_add
21956 = ((cu->list_in_scope
21957 == cu->get_builder ()->get_file_symbols ())
21958 ? cu->get_builder ()->get_global_symbols ()
21959 : cu->list_in_scope);
21960 }
21961 else
21962 list_to_add = cu->list_in_scope;
21963 }
21964 else
21965 {
21966 /* We do not know the address of this symbol.
21967 If it is an external symbol and we have type information
21968 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21969 The address of the variable will then be determined from
21970 the minimal symbol table whenever the variable is
21971 referenced. */
21972 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21973
21974 /* Fortran explicitly imports any global symbols to the local
21975 scope by DW_TAG_common_block. */
21976 if (cu->language == language_fortran && die->parent
21977 && die->parent->tag == DW_TAG_common_block)
21978 {
21979 /* SYMBOL_CLASS doesn't matter here because
21980 read_common_block is going to reset it. */
21981 if (!suppress_add)
21982 list_to_add = cu->list_in_scope;
21983 }
21984 else if (attr2 && (DW_UNSND (attr2) != 0)
21985 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21986 {
21987 /* A variable with DW_AT_external is never static, but it
21988 may be block-scoped. */
21989 list_to_add
21990 = ((cu->list_in_scope
21991 == cu->get_builder ()->get_file_symbols ())
21992 ? cu->get_builder ()->get_global_symbols ()
21993 : cu->list_in_scope);
21994
21995 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21996 }
21997 else if (!die_is_declaration (die, cu))
21998 {
21999 /* Use the default LOC_OPTIMIZED_OUT class. */
22000 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22001 if (!suppress_add)
22002 list_to_add = cu->list_in_scope;
22003 }
22004 }
22005 break;
22006 case DW_TAG_formal_parameter:
22007 {
22008 /* If we are inside a function, mark this as an argument. If
22009 not, we might be looking at an argument to an inlined function
22010 when we do not have enough information to show inlined frames;
22011 pretend it's a local variable in that case so that the user can
22012 still see it. */
22013 struct context_stack *curr
22014 = cu->get_builder ()->get_current_context_stack ();
22015 if (curr != nullptr && curr->name != nullptr)
22016 SYMBOL_IS_ARGUMENT (sym) = 1;
22017 attr = dwarf2_attr (die, DW_AT_location, cu);
22018 if (attr != nullptr)
22019 {
22020 var_decode_location (attr, sym, cu);
22021 }
22022 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22023 if (attr != nullptr)
22024 {
22025 dwarf2_const_value (attr, sym, cu);
22026 }
22027
22028 list_to_add = cu->list_in_scope;
22029 }
22030 break;
22031 case DW_TAG_unspecified_parameters:
22032 /* From varargs functions; gdb doesn't seem to have any
22033 interest in this information, so just ignore it for now.
22034 (FIXME?) */
22035 break;
22036 case DW_TAG_template_type_param:
22037 suppress_add = 1;
22038 /* Fall through. */
22039 case DW_TAG_class_type:
22040 case DW_TAG_interface_type:
22041 case DW_TAG_structure_type:
22042 case DW_TAG_union_type:
22043 case DW_TAG_set_type:
22044 case DW_TAG_enumeration_type:
22045 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22046 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22047
22048 {
22049 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22050 really ever be static objects: otherwise, if you try
22051 to, say, break of a class's method and you're in a file
22052 which doesn't mention that class, it won't work unless
22053 the check for all static symbols in lookup_symbol_aux
22054 saves you. See the OtherFileClass tests in
22055 gdb.c++/namespace.exp. */
22056
22057 if (!suppress_add)
22058 {
22059 buildsym_compunit *builder = cu->get_builder ();
22060 list_to_add
22061 = (cu->list_in_scope == builder->get_file_symbols ()
22062 && cu->language == language_cplus
22063 ? builder->get_global_symbols ()
22064 : cu->list_in_scope);
22065
22066 /* The semantics of C++ state that "struct foo {
22067 ... }" also defines a typedef for "foo". */
22068 if (cu->language == language_cplus
22069 || cu->language == language_ada
22070 || cu->language == language_d
22071 || cu->language == language_rust)
22072 {
22073 /* The symbol's name is already allocated along
22074 with this objfile, so we don't need to
22075 duplicate it for the type. */
22076 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22077 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
22078 }
22079 }
22080 }
22081 break;
22082 case DW_TAG_typedef:
22083 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22084 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22085 list_to_add = cu->list_in_scope;
22086 break;
22087 case DW_TAG_base_type:
22088 case DW_TAG_subrange_type:
22089 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22090 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22091 list_to_add = cu->list_in_scope;
22092 break;
22093 case DW_TAG_enumerator:
22094 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22095 if (attr != nullptr)
22096 {
22097 dwarf2_const_value (attr, sym, cu);
22098 }
22099 {
22100 /* NOTE: carlton/2003-11-10: See comment above in the
22101 DW_TAG_class_type, etc. block. */
22102
22103 list_to_add
22104 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22105 && cu->language == language_cplus
22106 ? cu->get_builder ()->get_global_symbols ()
22107 : cu->list_in_scope);
22108 }
22109 break;
22110 case DW_TAG_imported_declaration:
22111 case DW_TAG_namespace:
22112 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22113 list_to_add = cu->get_builder ()->get_global_symbols ();
22114 break;
22115 case DW_TAG_module:
22116 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22117 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22118 list_to_add = cu->get_builder ()->get_global_symbols ();
22119 break;
22120 case DW_TAG_common_block:
22121 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22122 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22123 add_symbol_to_list (sym, cu->list_in_scope);
22124 break;
22125 default:
22126 /* Not a tag we recognize. Hopefully we aren't processing
22127 trash data, but since we must specifically ignore things
22128 we don't recognize, there is nothing else we should do at
22129 this point. */
22130 complaint (_("unsupported tag: '%s'"),
22131 dwarf_tag_name (die->tag));
22132 break;
22133 }
22134
22135 if (suppress_add)
22136 {
22137 sym->hash_next = objfile->template_symbols;
22138 objfile->template_symbols = sym;
22139 list_to_add = NULL;
22140 }
22141
22142 if (list_to_add != NULL)
22143 add_symbol_to_list (sym, list_to_add);
22144
22145 /* For the benefit of old versions of GCC, check for anonymous
22146 namespaces based on the demangled name. */
22147 if (!cu->processing_has_namespace_info
22148 && cu->language == language_cplus)
22149 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22150 }
22151 return (sym);
22152 }
22153
22154 /* Given an attr with a DW_FORM_dataN value in host byte order,
22155 zero-extend it as appropriate for the symbol's type. The DWARF
22156 standard (v4) is not entirely clear about the meaning of using
22157 DW_FORM_dataN for a constant with a signed type, where the type is
22158 wider than the data. The conclusion of a discussion on the DWARF
22159 list was that this is unspecified. We choose to always zero-extend
22160 because that is the interpretation long in use by GCC. */
22161
22162 static gdb_byte *
22163 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22164 struct dwarf2_cu *cu, LONGEST *value, int bits)
22165 {
22166 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22167 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22168 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22169 LONGEST l = DW_UNSND (attr);
22170
22171 if (bits < sizeof (*value) * 8)
22172 {
22173 l &= ((LONGEST) 1 << bits) - 1;
22174 *value = l;
22175 }
22176 else if (bits == sizeof (*value) * 8)
22177 *value = l;
22178 else
22179 {
22180 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22181 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22182 return bytes;
22183 }
22184
22185 return NULL;
22186 }
22187
22188 /* Read a constant value from an attribute. Either set *VALUE, or if
22189 the value does not fit in *VALUE, set *BYTES - either already
22190 allocated on the objfile obstack, or newly allocated on OBSTACK,
22191 or, set *BATON, if we translated the constant to a location
22192 expression. */
22193
22194 static void
22195 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22196 const char *name, struct obstack *obstack,
22197 struct dwarf2_cu *cu,
22198 LONGEST *value, const gdb_byte **bytes,
22199 struct dwarf2_locexpr_baton **baton)
22200 {
22201 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22202 struct comp_unit_head *cu_header = &cu->header;
22203 struct dwarf_block *blk;
22204 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22205 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22206
22207 *value = 0;
22208 *bytes = NULL;
22209 *baton = NULL;
22210
22211 switch (attr->form)
22212 {
22213 case DW_FORM_addr:
22214 case DW_FORM_addrx:
22215 case DW_FORM_GNU_addr_index:
22216 {
22217 gdb_byte *data;
22218
22219 if (TYPE_LENGTH (type) != cu_header->addr_size)
22220 dwarf2_const_value_length_mismatch_complaint (name,
22221 cu_header->addr_size,
22222 TYPE_LENGTH (type));
22223 /* Symbols of this form are reasonably rare, so we just
22224 piggyback on the existing location code rather than writing
22225 a new implementation of symbol_computed_ops. */
22226 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22227 (*baton)->per_cu = cu->per_cu;
22228 gdb_assert ((*baton)->per_cu);
22229
22230 (*baton)->size = 2 + cu_header->addr_size;
22231 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22232 (*baton)->data = data;
22233
22234 data[0] = DW_OP_addr;
22235 store_unsigned_integer (&data[1], cu_header->addr_size,
22236 byte_order, DW_ADDR (attr));
22237 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22238 }
22239 break;
22240 case DW_FORM_string:
22241 case DW_FORM_strp:
22242 case DW_FORM_strx:
22243 case DW_FORM_GNU_str_index:
22244 case DW_FORM_GNU_strp_alt:
22245 /* DW_STRING is already allocated on the objfile obstack, point
22246 directly to it. */
22247 *bytes = (const gdb_byte *) DW_STRING (attr);
22248 break;
22249 case DW_FORM_block1:
22250 case DW_FORM_block2:
22251 case DW_FORM_block4:
22252 case DW_FORM_block:
22253 case DW_FORM_exprloc:
22254 case DW_FORM_data16:
22255 blk = DW_BLOCK (attr);
22256 if (TYPE_LENGTH (type) != blk->size)
22257 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22258 TYPE_LENGTH (type));
22259 *bytes = blk->data;
22260 break;
22261
22262 /* The DW_AT_const_value attributes are supposed to carry the
22263 symbol's value "represented as it would be on the target
22264 architecture." By the time we get here, it's already been
22265 converted to host endianness, so we just need to sign- or
22266 zero-extend it as appropriate. */
22267 case DW_FORM_data1:
22268 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22269 break;
22270 case DW_FORM_data2:
22271 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22272 break;
22273 case DW_FORM_data4:
22274 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22275 break;
22276 case DW_FORM_data8:
22277 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22278 break;
22279
22280 case DW_FORM_sdata:
22281 case DW_FORM_implicit_const:
22282 *value = DW_SND (attr);
22283 break;
22284
22285 case DW_FORM_udata:
22286 *value = DW_UNSND (attr);
22287 break;
22288
22289 default:
22290 complaint (_("unsupported const value attribute form: '%s'"),
22291 dwarf_form_name (attr->form));
22292 *value = 0;
22293 break;
22294 }
22295 }
22296
22297
22298 /* Copy constant value from an attribute to a symbol. */
22299
22300 static void
22301 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22302 struct dwarf2_cu *cu)
22303 {
22304 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22305 LONGEST value;
22306 const gdb_byte *bytes;
22307 struct dwarf2_locexpr_baton *baton;
22308
22309 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22310 sym->print_name (),
22311 &objfile->objfile_obstack, cu,
22312 &value, &bytes, &baton);
22313
22314 if (baton != NULL)
22315 {
22316 SYMBOL_LOCATION_BATON (sym) = baton;
22317 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22318 }
22319 else if (bytes != NULL)
22320 {
22321 SYMBOL_VALUE_BYTES (sym) = bytes;
22322 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22323 }
22324 else
22325 {
22326 SYMBOL_VALUE (sym) = value;
22327 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22328 }
22329 }
22330
22331 /* Return the type of the die in question using its DW_AT_type attribute. */
22332
22333 static struct type *
22334 die_type (struct die_info *die, struct dwarf2_cu *cu)
22335 {
22336 struct attribute *type_attr;
22337
22338 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22339 if (!type_attr)
22340 {
22341 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22342 /* A missing DW_AT_type represents a void type. */
22343 return objfile_type (objfile)->builtin_void;
22344 }
22345
22346 return lookup_die_type (die, type_attr, cu);
22347 }
22348
22349 /* True iff CU's producer generates GNAT Ada auxiliary information
22350 that allows to find parallel types through that information instead
22351 of having to do expensive parallel lookups by type name. */
22352
22353 static int
22354 need_gnat_info (struct dwarf2_cu *cu)
22355 {
22356 /* Assume that the Ada compiler was GNAT, which always produces
22357 the auxiliary information. */
22358 return (cu->language == language_ada);
22359 }
22360
22361 /* Return the auxiliary type of the die in question using its
22362 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22363 attribute is not present. */
22364
22365 static struct type *
22366 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22367 {
22368 struct attribute *type_attr;
22369
22370 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22371 if (!type_attr)
22372 return NULL;
22373
22374 return lookup_die_type (die, type_attr, cu);
22375 }
22376
22377 /* If DIE has a descriptive_type attribute, then set the TYPE's
22378 descriptive type accordingly. */
22379
22380 static void
22381 set_descriptive_type (struct type *type, struct die_info *die,
22382 struct dwarf2_cu *cu)
22383 {
22384 struct type *descriptive_type = die_descriptive_type (die, cu);
22385
22386 if (descriptive_type)
22387 {
22388 ALLOCATE_GNAT_AUX_TYPE (type);
22389 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22390 }
22391 }
22392
22393 /* Return the containing type of the die in question using its
22394 DW_AT_containing_type attribute. */
22395
22396 static struct type *
22397 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22398 {
22399 struct attribute *type_attr;
22400 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22401
22402 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22403 if (!type_attr)
22404 error (_("Dwarf Error: Problem turning containing type into gdb type "
22405 "[in module %s]"), objfile_name (objfile));
22406
22407 return lookup_die_type (die, type_attr, cu);
22408 }
22409
22410 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22411
22412 static struct type *
22413 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22414 {
22415 struct dwarf2_per_objfile *dwarf2_per_objfile
22416 = cu->per_cu->dwarf2_per_objfile;
22417 struct objfile *objfile = dwarf2_per_objfile->objfile;
22418 char *saved;
22419
22420 std::string message
22421 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22422 objfile_name (objfile),
22423 sect_offset_str (cu->header.sect_off),
22424 sect_offset_str (die->sect_off));
22425 saved = obstack_strdup (&objfile->objfile_obstack, message);
22426
22427 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22428 }
22429
22430 /* Look up the type of DIE in CU using its type attribute ATTR.
22431 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22432 DW_AT_containing_type.
22433 If there is no type substitute an error marker. */
22434
22435 static struct type *
22436 lookup_die_type (struct die_info *die, const struct attribute *attr,
22437 struct dwarf2_cu *cu)
22438 {
22439 struct dwarf2_per_objfile *dwarf2_per_objfile
22440 = cu->per_cu->dwarf2_per_objfile;
22441 struct objfile *objfile = dwarf2_per_objfile->objfile;
22442 struct type *this_type;
22443
22444 gdb_assert (attr->name == DW_AT_type
22445 || attr->name == DW_AT_GNAT_descriptive_type
22446 || attr->name == DW_AT_containing_type);
22447
22448 /* First see if we have it cached. */
22449
22450 if (attr->form == DW_FORM_GNU_ref_alt)
22451 {
22452 struct dwarf2_per_cu_data *per_cu;
22453 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22454
22455 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22456 dwarf2_per_objfile);
22457 this_type = get_die_type_at_offset (sect_off, per_cu);
22458 }
22459 else if (attr_form_is_ref (attr))
22460 {
22461 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22462
22463 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22464 }
22465 else if (attr->form == DW_FORM_ref_sig8)
22466 {
22467 ULONGEST signature = DW_SIGNATURE (attr);
22468
22469 return get_signatured_type (die, signature, cu);
22470 }
22471 else
22472 {
22473 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22474 " at %s [in module %s]"),
22475 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22476 objfile_name (objfile));
22477 return build_error_marker_type (cu, die);
22478 }
22479
22480 /* If not cached we need to read it in. */
22481
22482 if (this_type == NULL)
22483 {
22484 struct die_info *type_die = NULL;
22485 struct dwarf2_cu *type_cu = cu;
22486
22487 if (attr_form_is_ref (attr))
22488 type_die = follow_die_ref (die, attr, &type_cu);
22489 if (type_die == NULL)
22490 return build_error_marker_type (cu, die);
22491 /* If we find the type now, it's probably because the type came
22492 from an inter-CU reference and the type's CU got expanded before
22493 ours. */
22494 this_type = read_type_die (type_die, type_cu);
22495 }
22496
22497 /* If we still don't have a type use an error marker. */
22498
22499 if (this_type == NULL)
22500 return build_error_marker_type (cu, die);
22501
22502 return this_type;
22503 }
22504
22505 /* Return the type in DIE, CU.
22506 Returns NULL for invalid types.
22507
22508 This first does a lookup in die_type_hash,
22509 and only reads the die in if necessary.
22510
22511 NOTE: This can be called when reading in partial or full symbols. */
22512
22513 static struct type *
22514 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22515 {
22516 struct type *this_type;
22517
22518 this_type = get_die_type (die, cu);
22519 if (this_type)
22520 return this_type;
22521
22522 return read_type_die_1 (die, cu);
22523 }
22524
22525 /* Read the type in DIE, CU.
22526 Returns NULL for invalid types. */
22527
22528 static struct type *
22529 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22530 {
22531 struct type *this_type = NULL;
22532
22533 switch (die->tag)
22534 {
22535 case DW_TAG_class_type:
22536 case DW_TAG_interface_type:
22537 case DW_TAG_structure_type:
22538 case DW_TAG_union_type:
22539 this_type = read_structure_type (die, cu);
22540 break;
22541 case DW_TAG_enumeration_type:
22542 this_type = read_enumeration_type (die, cu);
22543 break;
22544 case DW_TAG_subprogram:
22545 case DW_TAG_subroutine_type:
22546 case DW_TAG_inlined_subroutine:
22547 this_type = read_subroutine_type (die, cu);
22548 break;
22549 case DW_TAG_array_type:
22550 this_type = read_array_type (die, cu);
22551 break;
22552 case DW_TAG_set_type:
22553 this_type = read_set_type (die, cu);
22554 break;
22555 case DW_TAG_pointer_type:
22556 this_type = read_tag_pointer_type (die, cu);
22557 break;
22558 case DW_TAG_ptr_to_member_type:
22559 this_type = read_tag_ptr_to_member_type (die, cu);
22560 break;
22561 case DW_TAG_reference_type:
22562 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22563 break;
22564 case DW_TAG_rvalue_reference_type:
22565 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22566 break;
22567 case DW_TAG_const_type:
22568 this_type = read_tag_const_type (die, cu);
22569 break;
22570 case DW_TAG_volatile_type:
22571 this_type = read_tag_volatile_type (die, cu);
22572 break;
22573 case DW_TAG_restrict_type:
22574 this_type = read_tag_restrict_type (die, cu);
22575 break;
22576 case DW_TAG_string_type:
22577 this_type = read_tag_string_type (die, cu);
22578 break;
22579 case DW_TAG_typedef:
22580 this_type = read_typedef (die, cu);
22581 break;
22582 case DW_TAG_subrange_type:
22583 this_type = read_subrange_type (die, cu);
22584 break;
22585 case DW_TAG_base_type:
22586 this_type = read_base_type (die, cu);
22587 break;
22588 case DW_TAG_unspecified_type:
22589 this_type = read_unspecified_type (die, cu);
22590 break;
22591 case DW_TAG_namespace:
22592 this_type = read_namespace_type (die, cu);
22593 break;
22594 case DW_TAG_module:
22595 this_type = read_module_type (die, cu);
22596 break;
22597 case DW_TAG_atomic_type:
22598 this_type = read_tag_atomic_type (die, cu);
22599 break;
22600 default:
22601 complaint (_("unexpected tag in read_type_die: '%s'"),
22602 dwarf_tag_name (die->tag));
22603 break;
22604 }
22605
22606 return this_type;
22607 }
22608
22609 /* See if we can figure out if the class lives in a namespace. We do
22610 this by looking for a member function; its demangled name will
22611 contain namespace info, if there is any.
22612 Return the computed name or NULL.
22613 Space for the result is allocated on the objfile's obstack.
22614 This is the full-die version of guess_partial_die_structure_name.
22615 In this case we know DIE has no useful parent. */
22616
22617 static char *
22618 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22619 {
22620 struct die_info *spec_die;
22621 struct dwarf2_cu *spec_cu;
22622 struct die_info *child;
22623 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22624
22625 spec_cu = cu;
22626 spec_die = die_specification (die, &spec_cu);
22627 if (spec_die != NULL)
22628 {
22629 die = spec_die;
22630 cu = spec_cu;
22631 }
22632
22633 for (child = die->child;
22634 child != NULL;
22635 child = child->sibling)
22636 {
22637 if (child->tag == DW_TAG_subprogram)
22638 {
22639 const char *linkage_name = dw2_linkage_name (child, cu);
22640
22641 if (linkage_name != NULL)
22642 {
22643 char *actual_name
22644 = language_class_name_from_physname (cu->language_defn,
22645 linkage_name);
22646 char *name = NULL;
22647
22648 if (actual_name != NULL)
22649 {
22650 const char *die_name = dwarf2_name (die, cu);
22651
22652 if (die_name != NULL
22653 && strcmp (die_name, actual_name) != 0)
22654 {
22655 /* Strip off the class name from the full name.
22656 We want the prefix. */
22657 int die_name_len = strlen (die_name);
22658 int actual_name_len = strlen (actual_name);
22659
22660 /* Test for '::' as a sanity check. */
22661 if (actual_name_len > die_name_len + 2
22662 && actual_name[actual_name_len
22663 - die_name_len - 1] == ':')
22664 name = obstack_strndup (
22665 &objfile->per_bfd->storage_obstack,
22666 actual_name, actual_name_len - die_name_len - 2);
22667 }
22668 }
22669 xfree (actual_name);
22670 return name;
22671 }
22672 }
22673 }
22674
22675 return NULL;
22676 }
22677
22678 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22679 prefix part in such case. See
22680 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22681
22682 static const char *
22683 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22684 {
22685 struct attribute *attr;
22686 const char *base;
22687
22688 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22689 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22690 return NULL;
22691
22692 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22693 return NULL;
22694
22695 attr = dw2_linkage_name_attr (die, cu);
22696 if (attr == NULL || DW_STRING (attr) == NULL)
22697 return NULL;
22698
22699 /* dwarf2_name had to be already called. */
22700 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22701
22702 /* Strip the base name, keep any leading namespaces/classes. */
22703 base = strrchr (DW_STRING (attr), ':');
22704 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22705 return "";
22706
22707 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22708 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22709 DW_STRING (attr),
22710 &base[-1] - DW_STRING (attr));
22711 }
22712
22713 /* Return the name of the namespace/class that DIE is defined within,
22714 or "" if we can't tell. The caller should not xfree the result.
22715
22716 For example, if we're within the method foo() in the following
22717 code:
22718
22719 namespace N {
22720 class C {
22721 void foo () {
22722 }
22723 };
22724 }
22725
22726 then determine_prefix on foo's die will return "N::C". */
22727
22728 static const char *
22729 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22730 {
22731 struct dwarf2_per_objfile *dwarf2_per_objfile
22732 = cu->per_cu->dwarf2_per_objfile;
22733 struct die_info *parent, *spec_die;
22734 struct dwarf2_cu *spec_cu;
22735 struct type *parent_type;
22736 const char *retval;
22737
22738 if (cu->language != language_cplus
22739 && cu->language != language_fortran && cu->language != language_d
22740 && cu->language != language_rust)
22741 return "";
22742
22743 retval = anonymous_struct_prefix (die, cu);
22744 if (retval)
22745 return retval;
22746
22747 /* We have to be careful in the presence of DW_AT_specification.
22748 For example, with GCC 3.4, given the code
22749
22750 namespace N {
22751 void foo() {
22752 // Definition of N::foo.
22753 }
22754 }
22755
22756 then we'll have a tree of DIEs like this:
22757
22758 1: DW_TAG_compile_unit
22759 2: DW_TAG_namespace // N
22760 3: DW_TAG_subprogram // declaration of N::foo
22761 4: DW_TAG_subprogram // definition of N::foo
22762 DW_AT_specification // refers to die #3
22763
22764 Thus, when processing die #4, we have to pretend that we're in
22765 the context of its DW_AT_specification, namely the contex of die
22766 #3. */
22767 spec_cu = cu;
22768 spec_die = die_specification (die, &spec_cu);
22769 if (spec_die == NULL)
22770 parent = die->parent;
22771 else
22772 {
22773 parent = spec_die->parent;
22774 cu = spec_cu;
22775 }
22776
22777 if (parent == NULL)
22778 return "";
22779 else if (parent->building_fullname)
22780 {
22781 const char *name;
22782 const char *parent_name;
22783
22784 /* It has been seen on RealView 2.2 built binaries,
22785 DW_TAG_template_type_param types actually _defined_ as
22786 children of the parent class:
22787
22788 enum E {};
22789 template class <class Enum> Class{};
22790 Class<enum E> class_e;
22791
22792 1: DW_TAG_class_type (Class)
22793 2: DW_TAG_enumeration_type (E)
22794 3: DW_TAG_enumerator (enum1:0)
22795 3: DW_TAG_enumerator (enum2:1)
22796 ...
22797 2: DW_TAG_template_type_param
22798 DW_AT_type DW_FORM_ref_udata (E)
22799
22800 Besides being broken debug info, it can put GDB into an
22801 infinite loop. Consider:
22802
22803 When we're building the full name for Class<E>, we'll start
22804 at Class, and go look over its template type parameters,
22805 finding E. We'll then try to build the full name of E, and
22806 reach here. We're now trying to build the full name of E,
22807 and look over the parent DIE for containing scope. In the
22808 broken case, if we followed the parent DIE of E, we'd again
22809 find Class, and once again go look at its template type
22810 arguments, etc., etc. Simply don't consider such parent die
22811 as source-level parent of this die (it can't be, the language
22812 doesn't allow it), and break the loop here. */
22813 name = dwarf2_name (die, cu);
22814 parent_name = dwarf2_name (parent, cu);
22815 complaint (_("template param type '%s' defined within parent '%s'"),
22816 name ? name : "<unknown>",
22817 parent_name ? parent_name : "<unknown>");
22818 return "";
22819 }
22820 else
22821 switch (parent->tag)
22822 {
22823 case DW_TAG_namespace:
22824 parent_type = read_type_die (parent, cu);
22825 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22826 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22827 Work around this problem here. */
22828 if (cu->language == language_cplus
22829 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22830 return "";
22831 /* We give a name to even anonymous namespaces. */
22832 return TYPE_NAME (parent_type);
22833 case DW_TAG_class_type:
22834 case DW_TAG_interface_type:
22835 case DW_TAG_structure_type:
22836 case DW_TAG_union_type:
22837 case DW_TAG_module:
22838 parent_type = read_type_die (parent, cu);
22839 if (TYPE_NAME (parent_type) != NULL)
22840 return TYPE_NAME (parent_type);
22841 else
22842 /* An anonymous structure is only allowed non-static data
22843 members; no typedefs, no member functions, et cetera.
22844 So it does not need a prefix. */
22845 return "";
22846 case DW_TAG_compile_unit:
22847 case DW_TAG_partial_unit:
22848 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22849 if (cu->language == language_cplus
22850 && !dwarf2_per_objfile->types.empty ()
22851 && die->child != NULL
22852 && (die->tag == DW_TAG_class_type
22853 || die->tag == DW_TAG_structure_type
22854 || die->tag == DW_TAG_union_type))
22855 {
22856 char *name = guess_full_die_structure_name (die, cu);
22857 if (name != NULL)
22858 return name;
22859 }
22860 return "";
22861 case DW_TAG_subprogram:
22862 /* Nested subroutines in Fortran get a prefix with the name
22863 of the parent's subroutine. */
22864 if (cu->language == language_fortran)
22865 {
22866 if ((die->tag == DW_TAG_subprogram)
22867 && (dwarf2_name (parent, cu) != NULL))
22868 return dwarf2_name (parent, cu);
22869 }
22870 return determine_prefix (parent, cu);
22871 case DW_TAG_enumeration_type:
22872 parent_type = read_type_die (parent, cu);
22873 if (TYPE_DECLARED_CLASS (parent_type))
22874 {
22875 if (TYPE_NAME (parent_type) != NULL)
22876 return TYPE_NAME (parent_type);
22877 return "";
22878 }
22879 /* Fall through. */
22880 default:
22881 return determine_prefix (parent, cu);
22882 }
22883 }
22884
22885 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22886 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22887 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22888 an obconcat, otherwise allocate storage for the result. The CU argument is
22889 used to determine the language and hence, the appropriate separator. */
22890
22891 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22892
22893 static char *
22894 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22895 int physname, struct dwarf2_cu *cu)
22896 {
22897 const char *lead = "";
22898 const char *sep;
22899
22900 if (suffix == NULL || suffix[0] == '\0'
22901 || prefix == NULL || prefix[0] == '\0')
22902 sep = "";
22903 else if (cu->language == language_d)
22904 {
22905 /* For D, the 'main' function could be defined in any module, but it
22906 should never be prefixed. */
22907 if (strcmp (suffix, "D main") == 0)
22908 {
22909 prefix = "";
22910 sep = "";
22911 }
22912 else
22913 sep = ".";
22914 }
22915 else if (cu->language == language_fortran && physname)
22916 {
22917 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22918 DW_AT_MIPS_linkage_name is preferred and used instead. */
22919
22920 lead = "__";
22921 sep = "_MOD_";
22922 }
22923 else
22924 sep = "::";
22925
22926 if (prefix == NULL)
22927 prefix = "";
22928 if (suffix == NULL)
22929 suffix = "";
22930
22931 if (obs == NULL)
22932 {
22933 char *retval
22934 = ((char *)
22935 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22936
22937 strcpy (retval, lead);
22938 strcat (retval, prefix);
22939 strcat (retval, sep);
22940 strcat (retval, suffix);
22941 return retval;
22942 }
22943 else
22944 {
22945 /* We have an obstack. */
22946 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22947 }
22948 }
22949
22950 /* Return sibling of die, NULL if no sibling. */
22951
22952 static struct die_info *
22953 sibling_die (struct die_info *die)
22954 {
22955 return die->sibling;
22956 }
22957
22958 /* Get name of a die, return NULL if not found. */
22959
22960 static const char *
22961 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22962 struct obstack *obstack)
22963 {
22964 if (name && cu->language == language_cplus)
22965 {
22966 std::string canon_name = cp_canonicalize_string (name);
22967
22968 if (!canon_name.empty ())
22969 {
22970 if (canon_name != name)
22971 name = obstack_strdup (obstack, canon_name);
22972 }
22973 }
22974
22975 return name;
22976 }
22977
22978 /* Get name of a die, return NULL if not found.
22979 Anonymous namespaces are converted to their magic string. */
22980
22981 static const char *
22982 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22983 {
22984 struct attribute *attr;
22985 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22986
22987 attr = dwarf2_attr (die, DW_AT_name, cu);
22988 if ((!attr || !DW_STRING (attr))
22989 && die->tag != DW_TAG_namespace
22990 && die->tag != DW_TAG_class_type
22991 && die->tag != DW_TAG_interface_type
22992 && die->tag != DW_TAG_structure_type
22993 && die->tag != DW_TAG_union_type)
22994 return NULL;
22995
22996 switch (die->tag)
22997 {
22998 case DW_TAG_compile_unit:
22999 case DW_TAG_partial_unit:
23000 /* Compilation units have a DW_AT_name that is a filename, not
23001 a source language identifier. */
23002 case DW_TAG_enumeration_type:
23003 case DW_TAG_enumerator:
23004 /* These tags always have simple identifiers already; no need
23005 to canonicalize them. */
23006 return DW_STRING (attr);
23007
23008 case DW_TAG_namespace:
23009 if (attr != NULL && DW_STRING (attr) != NULL)
23010 return DW_STRING (attr);
23011 return CP_ANONYMOUS_NAMESPACE_STR;
23012
23013 case DW_TAG_class_type:
23014 case DW_TAG_interface_type:
23015 case DW_TAG_structure_type:
23016 case DW_TAG_union_type:
23017 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23018 structures or unions. These were of the form "._%d" in GCC 4.1,
23019 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23020 and GCC 4.4. We work around this problem by ignoring these. */
23021 if (attr && DW_STRING (attr)
23022 && (startswith (DW_STRING (attr), "._")
23023 || startswith (DW_STRING (attr), "<anonymous")))
23024 return NULL;
23025
23026 /* GCC might emit a nameless typedef that has a linkage name. See
23027 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23028 if (!attr || DW_STRING (attr) == NULL)
23029 {
23030 char *demangled = NULL;
23031
23032 attr = dw2_linkage_name_attr (die, cu);
23033 if (attr == NULL || DW_STRING (attr) == NULL)
23034 return NULL;
23035
23036 /* Avoid demangling DW_STRING (attr) the second time on a second
23037 call for the same DIE. */
23038 if (!DW_STRING_IS_CANONICAL (attr))
23039 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
23040
23041 if (demangled)
23042 {
23043 const char *base;
23044
23045 /* FIXME: we already did this for the partial symbol... */
23046 DW_STRING (attr)
23047 = obstack_strdup (&objfile->per_bfd->storage_obstack,
23048 demangled);
23049 DW_STRING_IS_CANONICAL (attr) = 1;
23050 xfree (demangled);
23051
23052 /* Strip any leading namespaces/classes, keep only the base name.
23053 DW_AT_name for named DIEs does not contain the prefixes. */
23054 base = strrchr (DW_STRING (attr), ':');
23055 if (base && base > DW_STRING (attr) && base[-1] == ':')
23056 return &base[1];
23057 else
23058 return DW_STRING (attr);
23059 }
23060 }
23061 break;
23062
23063 default:
23064 break;
23065 }
23066
23067 if (!DW_STRING_IS_CANONICAL (attr))
23068 {
23069 DW_STRING (attr)
23070 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
23071 &objfile->per_bfd->storage_obstack);
23072 DW_STRING_IS_CANONICAL (attr) = 1;
23073 }
23074 return DW_STRING (attr);
23075 }
23076
23077 /* Return the die that this die in an extension of, or NULL if there
23078 is none. *EXT_CU is the CU containing DIE on input, and the CU
23079 containing the return value on output. */
23080
23081 static struct die_info *
23082 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23083 {
23084 struct attribute *attr;
23085
23086 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23087 if (attr == NULL)
23088 return NULL;
23089
23090 return follow_die_ref (die, attr, ext_cu);
23091 }
23092
23093 /* A convenience function that returns an "unknown" DWARF name,
23094 including the value of V. STR is the name of the entity being
23095 printed, e.g., "TAG". */
23096
23097 static const char *
23098 dwarf_unknown (const char *str, unsigned v)
23099 {
23100 char *cell = get_print_cell ();
23101 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23102 return cell;
23103 }
23104
23105 /* Convert a DIE tag into its string name. */
23106
23107 static const char *
23108 dwarf_tag_name (unsigned tag)
23109 {
23110 const char *name = get_DW_TAG_name (tag);
23111
23112 if (name == NULL)
23113 return dwarf_unknown ("TAG", tag);
23114
23115 return name;
23116 }
23117
23118 /* Convert a DWARF attribute code into its string name. */
23119
23120 static const char *
23121 dwarf_attr_name (unsigned attr)
23122 {
23123 const char *name;
23124
23125 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23126 if (attr == DW_AT_MIPS_fde)
23127 return "DW_AT_MIPS_fde";
23128 #else
23129 if (attr == DW_AT_HP_block_index)
23130 return "DW_AT_HP_block_index";
23131 #endif
23132
23133 name = get_DW_AT_name (attr);
23134
23135 if (name == NULL)
23136 return dwarf_unknown ("AT", attr);
23137
23138 return name;
23139 }
23140
23141 /* Convert a unit type to corresponding DW_UT name. */
23142
23143 static const char *
23144 dwarf_unit_type_name (int unit_type) {
23145 switch (unit_type)
23146 {
23147 case 0x01:
23148 return "DW_UT_compile (0x01)";
23149 case 0x02:
23150 return "DW_UT_type (0x02)";
23151 case 0x03:
23152 return "DW_UT_partial (0x03)";
23153 case 0x04:
23154 return "DW_UT_skeleton (0x04)";
23155 case 0x05:
23156 return "DW_UT_split_compile (0x05)";
23157 case 0x06:
23158 return "DW_UT_split_type (0x06)";
23159 case 0x80:
23160 return "DW_UT_lo_user (0x80)";
23161 case 0xff:
23162 return "DW_UT_hi_user (0xff)";
23163 default:
23164 return nullptr;
23165 }
23166 }
23167
23168 /* Convert a DWARF value form code into its string name. */
23169
23170 static const char *
23171 dwarf_form_name (unsigned form)
23172 {
23173 const char *name = get_DW_FORM_name (form);
23174
23175 if (name == NULL)
23176 return dwarf_unknown ("FORM", form);
23177
23178 return name;
23179 }
23180
23181 static const char *
23182 dwarf_bool_name (unsigned mybool)
23183 {
23184 if (mybool)
23185 return "TRUE";
23186 else
23187 return "FALSE";
23188 }
23189
23190 /* Convert a DWARF type code into its string name. */
23191
23192 static const char *
23193 dwarf_type_encoding_name (unsigned enc)
23194 {
23195 const char *name = get_DW_ATE_name (enc);
23196
23197 if (name == NULL)
23198 return dwarf_unknown ("ATE", enc);
23199
23200 return name;
23201 }
23202
23203 static void
23204 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23205 {
23206 unsigned int i;
23207
23208 print_spaces (indent, f);
23209 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23210 dwarf_tag_name (die->tag), die->abbrev,
23211 sect_offset_str (die->sect_off));
23212
23213 if (die->parent != NULL)
23214 {
23215 print_spaces (indent, f);
23216 fprintf_unfiltered (f, " parent at offset: %s\n",
23217 sect_offset_str (die->parent->sect_off));
23218 }
23219
23220 print_spaces (indent, f);
23221 fprintf_unfiltered (f, " has children: %s\n",
23222 dwarf_bool_name (die->child != NULL));
23223
23224 print_spaces (indent, f);
23225 fprintf_unfiltered (f, " attributes:\n");
23226
23227 for (i = 0; i < die->num_attrs; ++i)
23228 {
23229 print_spaces (indent, f);
23230 fprintf_unfiltered (f, " %s (%s) ",
23231 dwarf_attr_name (die->attrs[i].name),
23232 dwarf_form_name (die->attrs[i].form));
23233
23234 switch (die->attrs[i].form)
23235 {
23236 case DW_FORM_addr:
23237 case DW_FORM_addrx:
23238 case DW_FORM_GNU_addr_index:
23239 fprintf_unfiltered (f, "address: ");
23240 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23241 break;
23242 case DW_FORM_block2:
23243 case DW_FORM_block4:
23244 case DW_FORM_block:
23245 case DW_FORM_block1:
23246 fprintf_unfiltered (f, "block: size %s",
23247 pulongest (DW_BLOCK (&die->attrs[i])->size));
23248 break;
23249 case DW_FORM_exprloc:
23250 fprintf_unfiltered (f, "expression: size %s",
23251 pulongest (DW_BLOCK (&die->attrs[i])->size));
23252 break;
23253 case DW_FORM_data16:
23254 fprintf_unfiltered (f, "constant of 16 bytes");
23255 break;
23256 case DW_FORM_ref_addr:
23257 fprintf_unfiltered (f, "ref address: ");
23258 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23259 break;
23260 case DW_FORM_GNU_ref_alt:
23261 fprintf_unfiltered (f, "alt ref address: ");
23262 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23263 break;
23264 case DW_FORM_ref1:
23265 case DW_FORM_ref2:
23266 case DW_FORM_ref4:
23267 case DW_FORM_ref8:
23268 case DW_FORM_ref_udata:
23269 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23270 (long) (DW_UNSND (&die->attrs[i])));
23271 break;
23272 case DW_FORM_data1:
23273 case DW_FORM_data2:
23274 case DW_FORM_data4:
23275 case DW_FORM_data8:
23276 case DW_FORM_udata:
23277 case DW_FORM_sdata:
23278 fprintf_unfiltered (f, "constant: %s",
23279 pulongest (DW_UNSND (&die->attrs[i])));
23280 break;
23281 case DW_FORM_sec_offset:
23282 fprintf_unfiltered (f, "section offset: %s",
23283 pulongest (DW_UNSND (&die->attrs[i])));
23284 break;
23285 case DW_FORM_ref_sig8:
23286 fprintf_unfiltered (f, "signature: %s",
23287 hex_string (DW_SIGNATURE (&die->attrs[i])));
23288 break;
23289 case DW_FORM_string:
23290 case DW_FORM_strp:
23291 case DW_FORM_line_strp:
23292 case DW_FORM_strx:
23293 case DW_FORM_GNU_str_index:
23294 case DW_FORM_GNU_strp_alt:
23295 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23296 DW_STRING (&die->attrs[i])
23297 ? DW_STRING (&die->attrs[i]) : "",
23298 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23299 break;
23300 case DW_FORM_flag:
23301 if (DW_UNSND (&die->attrs[i]))
23302 fprintf_unfiltered (f, "flag: TRUE");
23303 else
23304 fprintf_unfiltered (f, "flag: FALSE");
23305 break;
23306 case DW_FORM_flag_present:
23307 fprintf_unfiltered (f, "flag: TRUE");
23308 break;
23309 case DW_FORM_indirect:
23310 /* The reader will have reduced the indirect form to
23311 the "base form" so this form should not occur. */
23312 fprintf_unfiltered (f,
23313 "unexpected attribute form: DW_FORM_indirect");
23314 break;
23315 case DW_FORM_implicit_const:
23316 fprintf_unfiltered (f, "constant: %s",
23317 plongest (DW_SND (&die->attrs[i])));
23318 break;
23319 default:
23320 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23321 die->attrs[i].form);
23322 break;
23323 }
23324 fprintf_unfiltered (f, "\n");
23325 }
23326 }
23327
23328 static void
23329 dump_die_for_error (struct die_info *die)
23330 {
23331 dump_die_shallow (gdb_stderr, 0, die);
23332 }
23333
23334 static void
23335 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23336 {
23337 int indent = level * 4;
23338
23339 gdb_assert (die != NULL);
23340
23341 if (level >= max_level)
23342 return;
23343
23344 dump_die_shallow (f, indent, die);
23345
23346 if (die->child != NULL)
23347 {
23348 print_spaces (indent, f);
23349 fprintf_unfiltered (f, " Children:");
23350 if (level + 1 < max_level)
23351 {
23352 fprintf_unfiltered (f, "\n");
23353 dump_die_1 (f, level + 1, max_level, die->child);
23354 }
23355 else
23356 {
23357 fprintf_unfiltered (f,
23358 " [not printed, max nesting level reached]\n");
23359 }
23360 }
23361
23362 if (die->sibling != NULL && level > 0)
23363 {
23364 dump_die_1 (f, level, max_level, die->sibling);
23365 }
23366 }
23367
23368 /* This is called from the pdie macro in gdbinit.in.
23369 It's not static so gcc will keep a copy callable from gdb. */
23370
23371 void
23372 dump_die (struct die_info *die, int max_level)
23373 {
23374 dump_die_1 (gdb_stdlog, 0, max_level, die);
23375 }
23376
23377 static void
23378 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23379 {
23380 void **slot;
23381
23382 slot = htab_find_slot_with_hash (cu->die_hash, die,
23383 to_underlying (die->sect_off),
23384 INSERT);
23385
23386 *slot = die;
23387 }
23388
23389 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23390 required kind. */
23391
23392 static sect_offset
23393 dwarf2_get_ref_die_offset (const struct attribute *attr)
23394 {
23395 if (attr_form_is_ref (attr))
23396 return (sect_offset) DW_UNSND (attr);
23397
23398 complaint (_("unsupported die ref attribute form: '%s'"),
23399 dwarf_form_name (attr->form));
23400 return {};
23401 }
23402
23403 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23404 * the value held by the attribute is not constant. */
23405
23406 static LONGEST
23407 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23408 {
23409 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23410 return DW_SND (attr);
23411 else if (attr->form == DW_FORM_udata
23412 || attr->form == DW_FORM_data1
23413 || attr->form == DW_FORM_data2
23414 || attr->form == DW_FORM_data4
23415 || attr->form == DW_FORM_data8)
23416 return DW_UNSND (attr);
23417 else
23418 {
23419 /* For DW_FORM_data16 see attr_form_is_constant. */
23420 complaint (_("Attribute value is not a constant (%s)"),
23421 dwarf_form_name (attr->form));
23422 return default_value;
23423 }
23424 }
23425
23426 /* Follow reference or signature attribute ATTR of SRC_DIE.
23427 On entry *REF_CU is the CU of SRC_DIE.
23428 On exit *REF_CU is the CU of the result. */
23429
23430 static struct die_info *
23431 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23432 struct dwarf2_cu **ref_cu)
23433 {
23434 struct die_info *die;
23435
23436 if (attr_form_is_ref (attr))
23437 die = follow_die_ref (src_die, attr, ref_cu);
23438 else if (attr->form == DW_FORM_ref_sig8)
23439 die = follow_die_sig (src_die, attr, ref_cu);
23440 else
23441 {
23442 dump_die_for_error (src_die);
23443 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23444 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23445 }
23446
23447 return die;
23448 }
23449
23450 /* Follow reference OFFSET.
23451 On entry *REF_CU is the CU of the source die referencing OFFSET.
23452 On exit *REF_CU is the CU of the result.
23453 Returns NULL if OFFSET is invalid. */
23454
23455 static struct die_info *
23456 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23457 struct dwarf2_cu **ref_cu)
23458 {
23459 struct die_info temp_die;
23460 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23461 struct dwarf2_per_objfile *dwarf2_per_objfile
23462 = cu->per_cu->dwarf2_per_objfile;
23463
23464 gdb_assert (cu->per_cu != NULL);
23465
23466 target_cu = cu;
23467
23468 if (cu->per_cu->is_debug_types)
23469 {
23470 /* .debug_types CUs cannot reference anything outside their CU.
23471 If they need to, they have to reference a signatured type via
23472 DW_FORM_ref_sig8. */
23473 if (!offset_in_cu_p (&cu->header, sect_off))
23474 return NULL;
23475 }
23476 else if (offset_in_dwz != cu->per_cu->is_dwz
23477 || !offset_in_cu_p (&cu->header, sect_off))
23478 {
23479 struct dwarf2_per_cu_data *per_cu;
23480
23481 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23482 dwarf2_per_objfile);
23483
23484 /* If necessary, add it to the queue and load its DIEs. */
23485 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23486 load_full_comp_unit (per_cu, false, cu->language);
23487
23488 target_cu = per_cu->cu;
23489 }
23490 else if (cu->dies == NULL)
23491 {
23492 /* We're loading full DIEs during partial symbol reading. */
23493 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23494 load_full_comp_unit (cu->per_cu, false, language_minimal);
23495 }
23496
23497 *ref_cu = target_cu;
23498 temp_die.sect_off = sect_off;
23499
23500 if (target_cu != cu)
23501 target_cu->ancestor = cu;
23502
23503 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23504 &temp_die,
23505 to_underlying (sect_off));
23506 }
23507
23508 /* Follow reference attribute ATTR of SRC_DIE.
23509 On entry *REF_CU is the CU of SRC_DIE.
23510 On exit *REF_CU is the CU of the result. */
23511
23512 static struct die_info *
23513 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23514 struct dwarf2_cu **ref_cu)
23515 {
23516 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23517 struct dwarf2_cu *cu = *ref_cu;
23518 struct die_info *die;
23519
23520 die = follow_die_offset (sect_off,
23521 (attr->form == DW_FORM_GNU_ref_alt
23522 || cu->per_cu->is_dwz),
23523 ref_cu);
23524 if (!die)
23525 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23526 "at %s [in module %s]"),
23527 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23528 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23529
23530 return die;
23531 }
23532
23533 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23534 Returned value is intended for DW_OP_call*. Returned
23535 dwarf2_locexpr_baton->data has lifetime of
23536 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23537
23538 struct dwarf2_locexpr_baton
23539 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23540 struct dwarf2_per_cu_data *per_cu,
23541 CORE_ADDR (*get_frame_pc) (void *baton),
23542 void *baton, bool resolve_abstract_p)
23543 {
23544 struct dwarf2_cu *cu;
23545 struct die_info *die;
23546 struct attribute *attr;
23547 struct dwarf2_locexpr_baton retval;
23548 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23549 struct objfile *objfile = dwarf2_per_objfile->objfile;
23550
23551 if (per_cu->cu == NULL)
23552 load_cu (per_cu, false);
23553 cu = per_cu->cu;
23554 if (cu == NULL)
23555 {
23556 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23557 Instead just throw an error, not much else we can do. */
23558 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23559 sect_offset_str (sect_off), objfile_name (objfile));
23560 }
23561
23562 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23563 if (!die)
23564 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23565 sect_offset_str (sect_off), objfile_name (objfile));
23566
23567 attr = dwarf2_attr (die, DW_AT_location, cu);
23568 if (!attr && resolve_abstract_p
23569 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23570 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23571 {
23572 CORE_ADDR pc = (*get_frame_pc) (baton);
23573 CORE_ADDR baseaddr
23574 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23575 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23576
23577 for (const auto &cand_off
23578 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23579 {
23580 struct dwarf2_cu *cand_cu = cu;
23581 struct die_info *cand
23582 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23583 if (!cand
23584 || !cand->parent
23585 || cand->parent->tag != DW_TAG_subprogram)
23586 continue;
23587
23588 CORE_ADDR pc_low, pc_high;
23589 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23590 if (pc_low == ((CORE_ADDR) -1))
23591 continue;
23592 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23593 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23594 if (!(pc_low <= pc && pc < pc_high))
23595 continue;
23596
23597 die = cand;
23598 attr = dwarf2_attr (die, DW_AT_location, cu);
23599 break;
23600 }
23601 }
23602
23603 if (!attr)
23604 {
23605 /* DWARF: "If there is no such attribute, then there is no effect.".
23606 DATA is ignored if SIZE is 0. */
23607
23608 retval.data = NULL;
23609 retval.size = 0;
23610 }
23611 else if (attr_form_is_section_offset (attr))
23612 {
23613 struct dwarf2_loclist_baton loclist_baton;
23614 CORE_ADDR pc = (*get_frame_pc) (baton);
23615 size_t size;
23616
23617 fill_in_loclist_baton (cu, &loclist_baton, attr);
23618
23619 retval.data = dwarf2_find_location_expression (&loclist_baton,
23620 &size, pc);
23621 retval.size = size;
23622 }
23623 else
23624 {
23625 if (!attr_form_is_block (attr))
23626 error (_("Dwarf Error: DIE at %s referenced in module %s "
23627 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23628 sect_offset_str (sect_off), objfile_name (objfile));
23629
23630 retval.data = DW_BLOCK (attr)->data;
23631 retval.size = DW_BLOCK (attr)->size;
23632 }
23633 retval.per_cu = cu->per_cu;
23634
23635 age_cached_comp_units (dwarf2_per_objfile);
23636
23637 return retval;
23638 }
23639
23640 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23641 offset. */
23642
23643 struct dwarf2_locexpr_baton
23644 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23645 struct dwarf2_per_cu_data *per_cu,
23646 CORE_ADDR (*get_frame_pc) (void *baton),
23647 void *baton)
23648 {
23649 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23650
23651 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23652 }
23653
23654 /* Write a constant of a given type as target-ordered bytes into
23655 OBSTACK. */
23656
23657 static const gdb_byte *
23658 write_constant_as_bytes (struct obstack *obstack,
23659 enum bfd_endian byte_order,
23660 struct type *type,
23661 ULONGEST value,
23662 LONGEST *len)
23663 {
23664 gdb_byte *result;
23665
23666 *len = TYPE_LENGTH (type);
23667 result = (gdb_byte *) obstack_alloc (obstack, *len);
23668 store_unsigned_integer (result, *len, byte_order, value);
23669
23670 return result;
23671 }
23672
23673 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23674 pointer to the constant bytes and set LEN to the length of the
23675 data. If memory is needed, allocate it on OBSTACK. If the DIE
23676 does not have a DW_AT_const_value, return NULL. */
23677
23678 const gdb_byte *
23679 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23680 struct dwarf2_per_cu_data *per_cu,
23681 struct obstack *obstack,
23682 LONGEST *len)
23683 {
23684 struct dwarf2_cu *cu;
23685 struct die_info *die;
23686 struct attribute *attr;
23687 const gdb_byte *result = NULL;
23688 struct type *type;
23689 LONGEST value;
23690 enum bfd_endian byte_order;
23691 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23692
23693 if (per_cu->cu == NULL)
23694 load_cu (per_cu, false);
23695 cu = per_cu->cu;
23696 if (cu == NULL)
23697 {
23698 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23699 Instead just throw an error, not much else we can do. */
23700 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23701 sect_offset_str (sect_off), objfile_name (objfile));
23702 }
23703
23704 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23705 if (!die)
23706 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23707 sect_offset_str (sect_off), objfile_name (objfile));
23708
23709 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23710 if (attr == NULL)
23711 return NULL;
23712
23713 byte_order = (bfd_big_endian (objfile->obfd)
23714 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23715
23716 switch (attr->form)
23717 {
23718 case DW_FORM_addr:
23719 case DW_FORM_addrx:
23720 case DW_FORM_GNU_addr_index:
23721 {
23722 gdb_byte *tem;
23723
23724 *len = cu->header.addr_size;
23725 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23726 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23727 result = tem;
23728 }
23729 break;
23730 case DW_FORM_string:
23731 case DW_FORM_strp:
23732 case DW_FORM_strx:
23733 case DW_FORM_GNU_str_index:
23734 case DW_FORM_GNU_strp_alt:
23735 /* DW_STRING is already allocated on the objfile obstack, point
23736 directly to it. */
23737 result = (const gdb_byte *) DW_STRING (attr);
23738 *len = strlen (DW_STRING (attr));
23739 break;
23740 case DW_FORM_block1:
23741 case DW_FORM_block2:
23742 case DW_FORM_block4:
23743 case DW_FORM_block:
23744 case DW_FORM_exprloc:
23745 case DW_FORM_data16:
23746 result = DW_BLOCK (attr)->data;
23747 *len = DW_BLOCK (attr)->size;
23748 break;
23749
23750 /* The DW_AT_const_value attributes are supposed to carry the
23751 symbol's value "represented as it would be on the target
23752 architecture." By the time we get here, it's already been
23753 converted to host endianness, so we just need to sign- or
23754 zero-extend it as appropriate. */
23755 case DW_FORM_data1:
23756 type = die_type (die, cu);
23757 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23758 if (result == NULL)
23759 result = write_constant_as_bytes (obstack, byte_order,
23760 type, value, len);
23761 break;
23762 case DW_FORM_data2:
23763 type = die_type (die, cu);
23764 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23765 if (result == NULL)
23766 result = write_constant_as_bytes (obstack, byte_order,
23767 type, value, len);
23768 break;
23769 case DW_FORM_data4:
23770 type = die_type (die, cu);
23771 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23772 if (result == NULL)
23773 result = write_constant_as_bytes (obstack, byte_order,
23774 type, value, len);
23775 break;
23776 case DW_FORM_data8:
23777 type = die_type (die, cu);
23778 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23779 if (result == NULL)
23780 result = write_constant_as_bytes (obstack, byte_order,
23781 type, value, len);
23782 break;
23783
23784 case DW_FORM_sdata:
23785 case DW_FORM_implicit_const:
23786 type = die_type (die, cu);
23787 result = write_constant_as_bytes (obstack, byte_order,
23788 type, DW_SND (attr), len);
23789 break;
23790
23791 case DW_FORM_udata:
23792 type = die_type (die, cu);
23793 result = write_constant_as_bytes (obstack, byte_order,
23794 type, DW_UNSND (attr), len);
23795 break;
23796
23797 default:
23798 complaint (_("unsupported const value attribute form: '%s'"),
23799 dwarf_form_name (attr->form));
23800 break;
23801 }
23802
23803 return result;
23804 }
23805
23806 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23807 valid type for this die is found. */
23808
23809 struct type *
23810 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23811 struct dwarf2_per_cu_data *per_cu)
23812 {
23813 struct dwarf2_cu *cu;
23814 struct die_info *die;
23815
23816 if (per_cu->cu == NULL)
23817 load_cu (per_cu, false);
23818 cu = per_cu->cu;
23819 if (!cu)
23820 return NULL;
23821
23822 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23823 if (!die)
23824 return NULL;
23825
23826 return die_type (die, cu);
23827 }
23828
23829 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23830 PER_CU. */
23831
23832 struct type *
23833 dwarf2_get_die_type (cu_offset die_offset,
23834 struct dwarf2_per_cu_data *per_cu)
23835 {
23836 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23837 return get_die_type_at_offset (die_offset_sect, per_cu);
23838 }
23839
23840 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23841 On entry *REF_CU is the CU of SRC_DIE.
23842 On exit *REF_CU is the CU of the result.
23843 Returns NULL if the referenced DIE isn't found. */
23844
23845 static struct die_info *
23846 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23847 struct dwarf2_cu **ref_cu)
23848 {
23849 struct die_info temp_die;
23850 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23851 struct die_info *die;
23852
23853 /* While it might be nice to assert sig_type->type == NULL here,
23854 we can get here for DW_AT_imported_declaration where we need
23855 the DIE not the type. */
23856
23857 /* If necessary, add it to the queue and load its DIEs. */
23858
23859 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23860 read_signatured_type (sig_type);
23861
23862 sig_cu = sig_type->per_cu.cu;
23863 gdb_assert (sig_cu != NULL);
23864 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23865 temp_die.sect_off = sig_type->type_offset_in_section;
23866 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23867 to_underlying (temp_die.sect_off));
23868 if (die)
23869 {
23870 struct dwarf2_per_objfile *dwarf2_per_objfile
23871 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23872
23873 /* For .gdb_index version 7 keep track of included TUs.
23874 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23875 if (dwarf2_per_objfile->index_table != NULL
23876 && dwarf2_per_objfile->index_table->version <= 7)
23877 {
23878 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23879 }
23880
23881 *ref_cu = sig_cu;
23882 if (sig_cu != cu)
23883 sig_cu->ancestor = cu;
23884
23885 return die;
23886 }
23887
23888 return NULL;
23889 }
23890
23891 /* Follow signatured type referenced by ATTR in SRC_DIE.
23892 On entry *REF_CU is the CU of SRC_DIE.
23893 On exit *REF_CU is the CU of the result.
23894 The result is the DIE of the type.
23895 If the referenced type cannot be found an error is thrown. */
23896
23897 static struct die_info *
23898 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23899 struct dwarf2_cu **ref_cu)
23900 {
23901 ULONGEST signature = DW_SIGNATURE (attr);
23902 struct signatured_type *sig_type;
23903 struct die_info *die;
23904
23905 gdb_assert (attr->form == DW_FORM_ref_sig8);
23906
23907 sig_type = lookup_signatured_type (*ref_cu, signature);
23908 /* sig_type will be NULL if the signatured type is missing from
23909 the debug info. */
23910 if (sig_type == NULL)
23911 {
23912 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23913 " from DIE at %s [in module %s]"),
23914 hex_string (signature), sect_offset_str (src_die->sect_off),
23915 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23916 }
23917
23918 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23919 if (die == NULL)
23920 {
23921 dump_die_for_error (src_die);
23922 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23923 " from DIE at %s [in module %s]"),
23924 hex_string (signature), sect_offset_str (src_die->sect_off),
23925 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23926 }
23927
23928 return die;
23929 }
23930
23931 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23932 reading in and processing the type unit if necessary. */
23933
23934 static struct type *
23935 get_signatured_type (struct die_info *die, ULONGEST signature,
23936 struct dwarf2_cu *cu)
23937 {
23938 struct dwarf2_per_objfile *dwarf2_per_objfile
23939 = cu->per_cu->dwarf2_per_objfile;
23940 struct signatured_type *sig_type;
23941 struct dwarf2_cu *type_cu;
23942 struct die_info *type_die;
23943 struct type *type;
23944
23945 sig_type = lookup_signatured_type (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 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23951 " from DIE at %s [in module %s]"),
23952 hex_string (signature), sect_offset_str (die->sect_off),
23953 objfile_name (dwarf2_per_objfile->objfile));
23954 return build_error_marker_type (cu, die);
23955 }
23956
23957 /* If we already know the type we're done. */
23958 if (sig_type->type != NULL)
23959 return sig_type->type;
23960
23961 type_cu = cu;
23962 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23963 if (type_die != NULL)
23964 {
23965 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23966 is created. This is important, for example, because for c++ classes
23967 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23968 type = read_type_die (type_die, type_cu);
23969 if (type == NULL)
23970 {
23971 complaint (_("Dwarf Error: Cannot build signatured type %s"
23972 " referenced from DIE at %s [in module %s]"),
23973 hex_string (signature), sect_offset_str (die->sect_off),
23974 objfile_name (dwarf2_per_objfile->objfile));
23975 type = build_error_marker_type (cu, die);
23976 }
23977 }
23978 else
23979 {
23980 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23981 " from DIE at %s [in module %s]"),
23982 hex_string (signature), sect_offset_str (die->sect_off),
23983 objfile_name (dwarf2_per_objfile->objfile));
23984 type = build_error_marker_type (cu, die);
23985 }
23986 sig_type->type = type;
23987
23988 return type;
23989 }
23990
23991 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23992 reading in and processing the type unit if necessary. */
23993
23994 static struct type *
23995 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23996 struct dwarf2_cu *cu) /* ARI: editCase function */
23997 {
23998 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23999 if (attr_form_is_ref (attr))
24000 {
24001 struct dwarf2_cu *type_cu = cu;
24002 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24003
24004 return read_type_die (type_die, type_cu);
24005 }
24006 else if (attr->form == DW_FORM_ref_sig8)
24007 {
24008 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24009 }
24010 else
24011 {
24012 struct dwarf2_per_objfile *dwarf2_per_objfile
24013 = cu->per_cu->dwarf2_per_objfile;
24014
24015 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24016 " at %s [in module %s]"),
24017 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24018 objfile_name (dwarf2_per_objfile->objfile));
24019 return build_error_marker_type (cu, die);
24020 }
24021 }
24022
24023 /* Load the DIEs associated with type unit PER_CU into memory. */
24024
24025 static void
24026 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
24027 {
24028 struct signatured_type *sig_type;
24029
24030 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24031 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24032
24033 /* We have the per_cu, but we need the signatured_type.
24034 Fortunately this is an easy translation. */
24035 gdb_assert (per_cu->is_debug_types);
24036 sig_type = (struct signatured_type *) per_cu;
24037
24038 gdb_assert (per_cu->cu == NULL);
24039
24040 read_signatured_type (sig_type);
24041
24042 gdb_assert (per_cu->cu != NULL);
24043 }
24044
24045 /* die_reader_func for read_signatured_type.
24046 This is identical to load_full_comp_unit_reader,
24047 but is kept separate for now. */
24048
24049 static void
24050 read_signatured_type_reader (const struct die_reader_specs *reader,
24051 const gdb_byte *info_ptr,
24052 struct die_info *comp_unit_die,
24053 int has_children,
24054 void *data)
24055 {
24056 struct dwarf2_cu *cu = reader->cu;
24057
24058 gdb_assert (cu->die_hash == NULL);
24059 cu->die_hash =
24060 htab_create_alloc_ex (cu->header.length / 12,
24061 die_hash,
24062 die_eq,
24063 NULL,
24064 &cu->comp_unit_obstack,
24065 hashtab_obstack_allocate,
24066 dummy_obstack_deallocate);
24067
24068 if (has_children)
24069 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
24070 &info_ptr, comp_unit_die);
24071 cu->dies = comp_unit_die;
24072 /* comp_unit_die is not stored in die_hash, no need. */
24073
24074 /* We try not to read any attributes in this function, because not
24075 all CUs needed for references have been loaded yet, and symbol
24076 table processing isn't initialized. But we have to set the CU language,
24077 or we won't be able to build types correctly.
24078 Similarly, if we do not read the producer, we can not apply
24079 producer-specific interpretation. */
24080 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24081 }
24082
24083 /* Read in a signatured type and build its CU and DIEs.
24084 If the type is a stub for the real type in a DWO file,
24085 read in the real type from the DWO file as well. */
24086
24087 static void
24088 read_signatured_type (struct signatured_type *sig_type)
24089 {
24090 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24091
24092 gdb_assert (per_cu->is_debug_types);
24093 gdb_assert (per_cu->cu == NULL);
24094
24095 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
24096 read_signatured_type_reader, NULL);
24097 sig_type->per_cu.tu_read = 1;
24098 }
24099
24100 /* Decode simple location descriptions.
24101 Given a pointer to a dwarf block that defines a location, compute
24102 the location and return the value.
24103
24104 NOTE drow/2003-11-18: This function is called in two situations
24105 now: for the address of static or global variables (partial symbols
24106 only) and for offsets into structures which are expected to be
24107 (more or less) constant. The partial symbol case should go away,
24108 and only the constant case should remain. That will let this
24109 function complain more accurately. A few special modes are allowed
24110 without complaint for global variables (for instance, global
24111 register values and thread-local values).
24112
24113 A location description containing no operations indicates that the
24114 object is optimized out. The return value is 0 for that case.
24115 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24116 callers will only want a very basic result and this can become a
24117 complaint.
24118
24119 Note that stack[0] is unused except as a default error return. */
24120
24121 static CORE_ADDR
24122 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24123 {
24124 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24125 size_t i;
24126 size_t size = blk->size;
24127 const gdb_byte *data = blk->data;
24128 CORE_ADDR stack[64];
24129 int stacki;
24130 unsigned int bytes_read, unsnd;
24131 gdb_byte op;
24132
24133 i = 0;
24134 stacki = 0;
24135 stack[stacki] = 0;
24136 stack[++stacki] = 0;
24137
24138 while (i < size)
24139 {
24140 op = data[i++];
24141 switch (op)
24142 {
24143 case DW_OP_lit0:
24144 case DW_OP_lit1:
24145 case DW_OP_lit2:
24146 case DW_OP_lit3:
24147 case DW_OP_lit4:
24148 case DW_OP_lit5:
24149 case DW_OP_lit6:
24150 case DW_OP_lit7:
24151 case DW_OP_lit8:
24152 case DW_OP_lit9:
24153 case DW_OP_lit10:
24154 case DW_OP_lit11:
24155 case DW_OP_lit12:
24156 case DW_OP_lit13:
24157 case DW_OP_lit14:
24158 case DW_OP_lit15:
24159 case DW_OP_lit16:
24160 case DW_OP_lit17:
24161 case DW_OP_lit18:
24162 case DW_OP_lit19:
24163 case DW_OP_lit20:
24164 case DW_OP_lit21:
24165 case DW_OP_lit22:
24166 case DW_OP_lit23:
24167 case DW_OP_lit24:
24168 case DW_OP_lit25:
24169 case DW_OP_lit26:
24170 case DW_OP_lit27:
24171 case DW_OP_lit28:
24172 case DW_OP_lit29:
24173 case DW_OP_lit30:
24174 case DW_OP_lit31:
24175 stack[++stacki] = op - DW_OP_lit0;
24176 break;
24177
24178 case DW_OP_reg0:
24179 case DW_OP_reg1:
24180 case DW_OP_reg2:
24181 case DW_OP_reg3:
24182 case DW_OP_reg4:
24183 case DW_OP_reg5:
24184 case DW_OP_reg6:
24185 case DW_OP_reg7:
24186 case DW_OP_reg8:
24187 case DW_OP_reg9:
24188 case DW_OP_reg10:
24189 case DW_OP_reg11:
24190 case DW_OP_reg12:
24191 case DW_OP_reg13:
24192 case DW_OP_reg14:
24193 case DW_OP_reg15:
24194 case DW_OP_reg16:
24195 case DW_OP_reg17:
24196 case DW_OP_reg18:
24197 case DW_OP_reg19:
24198 case DW_OP_reg20:
24199 case DW_OP_reg21:
24200 case DW_OP_reg22:
24201 case DW_OP_reg23:
24202 case DW_OP_reg24:
24203 case DW_OP_reg25:
24204 case DW_OP_reg26:
24205 case DW_OP_reg27:
24206 case DW_OP_reg28:
24207 case DW_OP_reg29:
24208 case DW_OP_reg30:
24209 case DW_OP_reg31:
24210 stack[++stacki] = op - DW_OP_reg0;
24211 if (i < size)
24212 dwarf2_complex_location_expr_complaint ();
24213 break;
24214
24215 case DW_OP_regx:
24216 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24217 i += bytes_read;
24218 stack[++stacki] = unsnd;
24219 if (i < size)
24220 dwarf2_complex_location_expr_complaint ();
24221 break;
24222
24223 case DW_OP_addr:
24224 stack[++stacki] = read_address (objfile->obfd, &data[i],
24225 cu, &bytes_read);
24226 i += bytes_read;
24227 break;
24228
24229 case DW_OP_const1u:
24230 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24231 i += 1;
24232 break;
24233
24234 case DW_OP_const1s:
24235 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24236 i += 1;
24237 break;
24238
24239 case DW_OP_const2u:
24240 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24241 i += 2;
24242 break;
24243
24244 case DW_OP_const2s:
24245 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24246 i += 2;
24247 break;
24248
24249 case DW_OP_const4u:
24250 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24251 i += 4;
24252 break;
24253
24254 case DW_OP_const4s:
24255 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24256 i += 4;
24257 break;
24258
24259 case DW_OP_const8u:
24260 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24261 i += 8;
24262 break;
24263
24264 case DW_OP_constu:
24265 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24266 &bytes_read);
24267 i += bytes_read;
24268 break;
24269
24270 case DW_OP_consts:
24271 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24272 i += bytes_read;
24273 break;
24274
24275 case DW_OP_dup:
24276 stack[stacki + 1] = stack[stacki];
24277 stacki++;
24278 break;
24279
24280 case DW_OP_plus:
24281 stack[stacki - 1] += stack[stacki];
24282 stacki--;
24283 break;
24284
24285 case DW_OP_plus_uconst:
24286 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24287 &bytes_read);
24288 i += bytes_read;
24289 break;
24290
24291 case DW_OP_minus:
24292 stack[stacki - 1] -= stack[stacki];
24293 stacki--;
24294 break;
24295
24296 case DW_OP_deref:
24297 /* If we're not the last op, then we definitely can't encode
24298 this using GDB's address_class enum. This is valid for partial
24299 global symbols, although the variable's address will be bogus
24300 in the psymtab. */
24301 if (i < size)
24302 dwarf2_complex_location_expr_complaint ();
24303 break;
24304
24305 case DW_OP_GNU_push_tls_address:
24306 case DW_OP_form_tls_address:
24307 /* The top of the stack has the offset from the beginning
24308 of the thread control block at which the variable is located. */
24309 /* Nothing should follow this operator, so the top of stack would
24310 be returned. */
24311 /* This is valid for partial global symbols, but the variable's
24312 address will be bogus in the psymtab. Make it always at least
24313 non-zero to not look as a variable garbage collected by linker
24314 which have DW_OP_addr 0. */
24315 if (i < size)
24316 dwarf2_complex_location_expr_complaint ();
24317 stack[stacki]++;
24318 break;
24319
24320 case DW_OP_GNU_uninit:
24321 break;
24322
24323 case DW_OP_addrx:
24324 case DW_OP_GNU_addr_index:
24325 case DW_OP_GNU_const_index:
24326 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24327 &bytes_read);
24328 i += bytes_read;
24329 break;
24330
24331 default:
24332 {
24333 const char *name = get_DW_OP_name (op);
24334
24335 if (name)
24336 complaint (_("unsupported stack op: '%s'"),
24337 name);
24338 else
24339 complaint (_("unsupported stack op: '%02x'"),
24340 op);
24341 }
24342
24343 return (stack[stacki]);
24344 }
24345
24346 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24347 outside of the allocated space. Also enforce minimum>0. */
24348 if (stacki >= ARRAY_SIZE (stack) - 1)
24349 {
24350 complaint (_("location description stack overflow"));
24351 return 0;
24352 }
24353
24354 if (stacki <= 0)
24355 {
24356 complaint (_("location description stack underflow"));
24357 return 0;
24358 }
24359 }
24360 return (stack[stacki]);
24361 }
24362
24363 /* memory allocation interface */
24364
24365 static struct dwarf_block *
24366 dwarf_alloc_block (struct dwarf2_cu *cu)
24367 {
24368 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24369 }
24370
24371 static struct die_info *
24372 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24373 {
24374 struct die_info *die;
24375 size_t size = sizeof (struct die_info);
24376
24377 if (num_attrs > 1)
24378 size += (num_attrs - 1) * sizeof (struct attribute);
24379
24380 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24381 memset (die, 0, sizeof (struct die_info));
24382 return (die);
24383 }
24384
24385 \f
24386 /* Macro support. */
24387
24388 /* Return file name relative to the compilation directory of file number I in
24389 *LH's file name table. The result is allocated using xmalloc; the caller is
24390 responsible for freeing it. */
24391
24392 static char *
24393 file_file_name (int file, struct line_header *lh)
24394 {
24395 /* Is the file number a valid index into the line header's file name
24396 table? Remember that file numbers start with one, not zero. */
24397 if (lh->is_valid_file_index (file))
24398 {
24399 const file_entry *fe = lh->file_name_at (file);
24400
24401 if (!IS_ABSOLUTE_PATH (fe->name))
24402 {
24403 const char *dir = fe->include_dir (lh);
24404 if (dir != NULL)
24405 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
24406 }
24407 return xstrdup (fe->name);
24408 }
24409 else
24410 {
24411 /* The compiler produced a bogus file number. We can at least
24412 record the macro definitions made in the file, even if we
24413 won't be able to find the file by name. */
24414 char fake_name[80];
24415
24416 xsnprintf (fake_name, sizeof (fake_name),
24417 "<bad macro file number %d>", file);
24418
24419 complaint (_("bad file number in macro information (%d)"),
24420 file);
24421
24422 return xstrdup (fake_name);
24423 }
24424 }
24425
24426 /* Return the full name of file number I in *LH's file name table.
24427 Use COMP_DIR as the name of the current directory of the
24428 compilation. The result is allocated using xmalloc; the caller is
24429 responsible for freeing it. */
24430 static char *
24431 file_full_name (int file, struct line_header *lh, const char *comp_dir)
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 char *relative = file_file_name (file, lh);
24438
24439 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24440 return relative;
24441 return reconcat (relative, comp_dir, SLASH_STRING,
24442 relative, (char *) NULL);
24443 }
24444 else
24445 return file_file_name (file, lh);
24446 }
24447
24448
24449 static struct macro_source_file *
24450 macro_start_file (struct dwarf2_cu *cu,
24451 int file, int line,
24452 struct macro_source_file *current_file,
24453 struct line_header *lh)
24454 {
24455 /* File name relative to the compilation directory of this source file. */
24456 char *file_name = file_file_name (file, lh);
24457
24458 if (! current_file)
24459 {
24460 /* Note: We don't create a macro table for this compilation unit
24461 at all until we actually get a filename. */
24462 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24463
24464 /* If we have no current file, then this must be the start_file
24465 directive for the compilation unit's main source file. */
24466 current_file = macro_set_main (macro_table, file_name);
24467 macro_define_special (macro_table);
24468 }
24469 else
24470 current_file = macro_include (current_file, line, file_name);
24471
24472 xfree (file_name);
24473
24474 return current_file;
24475 }
24476
24477 static const char *
24478 consume_improper_spaces (const char *p, const char *body)
24479 {
24480 if (*p == ' ')
24481 {
24482 complaint (_("macro definition contains spaces "
24483 "in formal argument list:\n`%s'"),
24484 body);
24485
24486 while (*p == ' ')
24487 p++;
24488 }
24489
24490 return p;
24491 }
24492
24493
24494 static void
24495 parse_macro_definition (struct macro_source_file *file, int line,
24496 const char *body)
24497 {
24498 const char *p;
24499
24500 /* The body string takes one of two forms. For object-like macro
24501 definitions, it should be:
24502
24503 <macro name> " " <definition>
24504
24505 For function-like macro definitions, it should be:
24506
24507 <macro name> "() " <definition>
24508 or
24509 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24510
24511 Spaces may appear only where explicitly indicated, and in the
24512 <definition>.
24513
24514 The Dwarf 2 spec says that an object-like macro's name is always
24515 followed by a space, but versions of GCC around March 2002 omit
24516 the space when the macro's definition is the empty string.
24517
24518 The Dwarf 2 spec says that there should be no spaces between the
24519 formal arguments in a function-like macro's formal argument list,
24520 but versions of GCC around March 2002 include spaces after the
24521 commas. */
24522
24523
24524 /* Find the extent of the macro name. The macro name is terminated
24525 by either a space or null character (for an object-like macro) or
24526 an opening paren (for a function-like macro). */
24527 for (p = body; *p; p++)
24528 if (*p == ' ' || *p == '(')
24529 break;
24530
24531 if (*p == ' ' || *p == '\0')
24532 {
24533 /* It's an object-like macro. */
24534 int name_len = p - body;
24535 char *name = savestring (body, name_len);
24536 const char *replacement;
24537
24538 if (*p == ' ')
24539 replacement = body + name_len + 1;
24540 else
24541 {
24542 dwarf2_macro_malformed_definition_complaint (body);
24543 replacement = body + name_len;
24544 }
24545
24546 macro_define_object (file, line, name, replacement);
24547
24548 xfree (name);
24549 }
24550 else if (*p == '(')
24551 {
24552 /* It's a function-like macro. */
24553 char *name = savestring (body, p - body);
24554 int argc = 0;
24555 int argv_size = 1;
24556 char **argv = XNEWVEC (char *, argv_size);
24557
24558 p++;
24559
24560 p = consume_improper_spaces (p, body);
24561
24562 /* Parse the formal argument list. */
24563 while (*p && *p != ')')
24564 {
24565 /* Find the extent of the current argument name. */
24566 const char *arg_start = p;
24567
24568 while (*p && *p != ',' && *p != ')' && *p != ' ')
24569 p++;
24570
24571 if (! *p || p == arg_start)
24572 dwarf2_macro_malformed_definition_complaint (body);
24573 else
24574 {
24575 /* Make sure argv has room for the new argument. */
24576 if (argc >= argv_size)
24577 {
24578 argv_size *= 2;
24579 argv = XRESIZEVEC (char *, argv, argv_size);
24580 }
24581
24582 argv[argc++] = savestring (arg_start, p - arg_start);
24583 }
24584
24585 p = consume_improper_spaces (p, body);
24586
24587 /* Consume the comma, if present. */
24588 if (*p == ',')
24589 {
24590 p++;
24591
24592 p = consume_improper_spaces (p, body);
24593 }
24594 }
24595
24596 if (*p == ')')
24597 {
24598 p++;
24599
24600 if (*p == ' ')
24601 /* Perfectly formed definition, no complaints. */
24602 macro_define_function (file, line, name,
24603 argc, (const char **) argv,
24604 p + 1);
24605 else if (*p == '\0')
24606 {
24607 /* Complain, but do define it. */
24608 dwarf2_macro_malformed_definition_complaint (body);
24609 macro_define_function (file, line, name,
24610 argc, (const char **) argv,
24611 p);
24612 }
24613 else
24614 /* Just complain. */
24615 dwarf2_macro_malformed_definition_complaint (body);
24616 }
24617 else
24618 /* Just complain. */
24619 dwarf2_macro_malformed_definition_complaint (body);
24620
24621 xfree (name);
24622 {
24623 int i;
24624
24625 for (i = 0; i < argc; i++)
24626 xfree (argv[i]);
24627 }
24628 xfree (argv);
24629 }
24630 else
24631 dwarf2_macro_malformed_definition_complaint (body);
24632 }
24633
24634 /* Skip some bytes from BYTES according to the form given in FORM.
24635 Returns the new pointer. */
24636
24637 static const gdb_byte *
24638 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24639 enum dwarf_form form,
24640 unsigned int offset_size,
24641 struct dwarf2_section_info *section)
24642 {
24643 unsigned int bytes_read;
24644
24645 switch (form)
24646 {
24647 case DW_FORM_data1:
24648 case DW_FORM_flag:
24649 ++bytes;
24650 break;
24651
24652 case DW_FORM_data2:
24653 bytes += 2;
24654 break;
24655
24656 case DW_FORM_data4:
24657 bytes += 4;
24658 break;
24659
24660 case DW_FORM_data8:
24661 bytes += 8;
24662 break;
24663
24664 case DW_FORM_data16:
24665 bytes += 16;
24666 break;
24667
24668 case DW_FORM_string:
24669 read_direct_string (abfd, bytes, &bytes_read);
24670 bytes += bytes_read;
24671 break;
24672
24673 case DW_FORM_sec_offset:
24674 case DW_FORM_strp:
24675 case DW_FORM_GNU_strp_alt:
24676 bytes += offset_size;
24677 break;
24678
24679 case DW_FORM_block:
24680 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24681 bytes += bytes_read;
24682 break;
24683
24684 case DW_FORM_block1:
24685 bytes += 1 + read_1_byte (abfd, bytes);
24686 break;
24687 case DW_FORM_block2:
24688 bytes += 2 + read_2_bytes (abfd, bytes);
24689 break;
24690 case DW_FORM_block4:
24691 bytes += 4 + read_4_bytes (abfd, bytes);
24692 break;
24693
24694 case DW_FORM_addrx:
24695 case DW_FORM_sdata:
24696 case DW_FORM_strx:
24697 case DW_FORM_udata:
24698 case DW_FORM_GNU_addr_index:
24699 case DW_FORM_GNU_str_index:
24700 bytes = gdb_skip_leb128 (bytes, buffer_end);
24701 if (bytes == NULL)
24702 {
24703 dwarf2_section_buffer_overflow_complaint (section);
24704 return NULL;
24705 }
24706 break;
24707
24708 case DW_FORM_implicit_const:
24709 break;
24710
24711 default:
24712 {
24713 complaint (_("invalid form 0x%x in `%s'"),
24714 form, get_section_name (section));
24715 return NULL;
24716 }
24717 }
24718
24719 return bytes;
24720 }
24721
24722 /* A helper for dwarf_decode_macros that handles skipping an unknown
24723 opcode. Returns an updated pointer to the macro data buffer; or,
24724 on error, issues a complaint and returns NULL. */
24725
24726 static const gdb_byte *
24727 skip_unknown_opcode (unsigned int opcode,
24728 const gdb_byte **opcode_definitions,
24729 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24730 bfd *abfd,
24731 unsigned int offset_size,
24732 struct dwarf2_section_info *section)
24733 {
24734 unsigned int bytes_read, i;
24735 unsigned long arg;
24736 const gdb_byte *defn;
24737
24738 if (opcode_definitions[opcode] == NULL)
24739 {
24740 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24741 opcode);
24742 return NULL;
24743 }
24744
24745 defn = opcode_definitions[opcode];
24746 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24747 defn += bytes_read;
24748
24749 for (i = 0; i < arg; ++i)
24750 {
24751 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24752 (enum dwarf_form) defn[i], offset_size,
24753 section);
24754 if (mac_ptr == NULL)
24755 {
24756 /* skip_form_bytes already issued the complaint. */
24757 return NULL;
24758 }
24759 }
24760
24761 return mac_ptr;
24762 }
24763
24764 /* A helper function which parses the header of a macro section.
24765 If the macro section is the extended (for now called "GNU") type,
24766 then this updates *OFFSET_SIZE. Returns a pointer to just after
24767 the header, or issues a complaint and returns NULL on error. */
24768
24769 static const gdb_byte *
24770 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24771 bfd *abfd,
24772 const gdb_byte *mac_ptr,
24773 unsigned int *offset_size,
24774 int section_is_gnu)
24775 {
24776 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24777
24778 if (section_is_gnu)
24779 {
24780 unsigned int version, flags;
24781
24782 version = read_2_bytes (abfd, mac_ptr);
24783 if (version != 4 && version != 5)
24784 {
24785 complaint (_("unrecognized version `%d' in .debug_macro section"),
24786 version);
24787 return NULL;
24788 }
24789 mac_ptr += 2;
24790
24791 flags = read_1_byte (abfd, mac_ptr);
24792 ++mac_ptr;
24793 *offset_size = (flags & 1) ? 8 : 4;
24794
24795 if ((flags & 2) != 0)
24796 /* We don't need the line table offset. */
24797 mac_ptr += *offset_size;
24798
24799 /* Vendor opcode descriptions. */
24800 if ((flags & 4) != 0)
24801 {
24802 unsigned int i, count;
24803
24804 count = read_1_byte (abfd, mac_ptr);
24805 ++mac_ptr;
24806 for (i = 0; i < count; ++i)
24807 {
24808 unsigned int opcode, bytes_read;
24809 unsigned long arg;
24810
24811 opcode = read_1_byte (abfd, mac_ptr);
24812 ++mac_ptr;
24813 opcode_definitions[opcode] = mac_ptr;
24814 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24815 mac_ptr += bytes_read;
24816 mac_ptr += arg;
24817 }
24818 }
24819 }
24820
24821 return mac_ptr;
24822 }
24823
24824 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24825 including DW_MACRO_import. */
24826
24827 static void
24828 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24829 bfd *abfd,
24830 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24831 struct macro_source_file *current_file,
24832 struct line_header *lh,
24833 struct dwarf2_section_info *section,
24834 int section_is_gnu, int section_is_dwz,
24835 unsigned int offset_size,
24836 htab_t include_hash)
24837 {
24838 struct dwarf2_per_objfile *dwarf2_per_objfile
24839 = cu->per_cu->dwarf2_per_objfile;
24840 struct objfile *objfile = dwarf2_per_objfile->objfile;
24841 enum dwarf_macro_record_type macinfo_type;
24842 int at_commandline;
24843 const gdb_byte *opcode_definitions[256];
24844
24845 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24846 &offset_size, section_is_gnu);
24847 if (mac_ptr == NULL)
24848 {
24849 /* We already issued a complaint. */
24850 return;
24851 }
24852
24853 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24854 GDB is still reading the definitions from command line. First
24855 DW_MACINFO_start_file will need to be ignored as it was already executed
24856 to create CURRENT_FILE for the main source holding also the command line
24857 definitions. On first met DW_MACINFO_start_file this flag is reset to
24858 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24859
24860 at_commandline = 1;
24861
24862 do
24863 {
24864 /* Do we at least have room for a macinfo type byte? */
24865 if (mac_ptr >= mac_end)
24866 {
24867 dwarf2_section_buffer_overflow_complaint (section);
24868 break;
24869 }
24870
24871 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24872 mac_ptr++;
24873
24874 /* Note that we rely on the fact that the corresponding GNU and
24875 DWARF constants are the same. */
24876 DIAGNOSTIC_PUSH
24877 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24878 switch (macinfo_type)
24879 {
24880 /* A zero macinfo type indicates the end of the macro
24881 information. */
24882 case 0:
24883 break;
24884
24885 case DW_MACRO_define:
24886 case DW_MACRO_undef:
24887 case DW_MACRO_define_strp:
24888 case DW_MACRO_undef_strp:
24889 case DW_MACRO_define_sup:
24890 case DW_MACRO_undef_sup:
24891 {
24892 unsigned int bytes_read;
24893 int line;
24894 const char *body;
24895 int is_define;
24896
24897 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24898 mac_ptr += bytes_read;
24899
24900 if (macinfo_type == DW_MACRO_define
24901 || macinfo_type == DW_MACRO_undef)
24902 {
24903 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24904 mac_ptr += bytes_read;
24905 }
24906 else
24907 {
24908 LONGEST str_offset;
24909
24910 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24911 mac_ptr += offset_size;
24912
24913 if (macinfo_type == DW_MACRO_define_sup
24914 || macinfo_type == DW_MACRO_undef_sup
24915 || section_is_dwz)
24916 {
24917 struct dwz_file *dwz
24918 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24919
24920 body = read_indirect_string_from_dwz (objfile,
24921 dwz, str_offset);
24922 }
24923 else
24924 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24925 abfd, str_offset);
24926 }
24927
24928 is_define = (macinfo_type == DW_MACRO_define
24929 || macinfo_type == DW_MACRO_define_strp
24930 || macinfo_type == DW_MACRO_define_sup);
24931 if (! current_file)
24932 {
24933 /* DWARF violation as no main source is present. */
24934 complaint (_("debug info with no main source gives macro %s "
24935 "on line %d: %s"),
24936 is_define ? _("definition") : _("undefinition"),
24937 line, body);
24938 break;
24939 }
24940 if ((line == 0 && !at_commandline)
24941 || (line != 0 && at_commandline))
24942 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24943 at_commandline ? _("command-line") : _("in-file"),
24944 is_define ? _("definition") : _("undefinition"),
24945 line == 0 ? _("zero") : _("non-zero"), line, body);
24946
24947 if (body == NULL)
24948 {
24949 /* Fedora's rpm-build's "debugedit" binary
24950 corrupted .debug_macro sections.
24951
24952 For more info, see
24953 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24954 complaint (_("debug info gives %s invalid macro %s "
24955 "without body (corrupted?) at line %d "
24956 "on file %s"),
24957 at_commandline ? _("command-line") : _("in-file"),
24958 is_define ? _("definition") : _("undefinition"),
24959 line, current_file->filename);
24960 }
24961 else if (is_define)
24962 parse_macro_definition (current_file, line, body);
24963 else
24964 {
24965 gdb_assert (macinfo_type == DW_MACRO_undef
24966 || macinfo_type == DW_MACRO_undef_strp
24967 || macinfo_type == DW_MACRO_undef_sup);
24968 macro_undef (current_file, line, body);
24969 }
24970 }
24971 break;
24972
24973 case DW_MACRO_start_file:
24974 {
24975 unsigned int bytes_read;
24976 int line, file;
24977
24978 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24979 mac_ptr += bytes_read;
24980 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24981 mac_ptr += bytes_read;
24982
24983 if ((line == 0 && !at_commandline)
24984 || (line != 0 && at_commandline))
24985 complaint (_("debug info gives source %d included "
24986 "from %s at %s line %d"),
24987 file, at_commandline ? _("command-line") : _("file"),
24988 line == 0 ? _("zero") : _("non-zero"), line);
24989
24990 if (at_commandline)
24991 {
24992 /* This DW_MACRO_start_file was executed in the
24993 pass one. */
24994 at_commandline = 0;
24995 }
24996 else
24997 current_file = macro_start_file (cu, file, line, current_file,
24998 lh);
24999 }
25000 break;
25001
25002 case DW_MACRO_end_file:
25003 if (! current_file)
25004 complaint (_("macro debug info has an unmatched "
25005 "`close_file' directive"));
25006 else
25007 {
25008 current_file = current_file->included_by;
25009 if (! current_file)
25010 {
25011 enum dwarf_macro_record_type next_type;
25012
25013 /* GCC circa March 2002 doesn't produce the zero
25014 type byte marking the end of the compilation
25015 unit. Complain if it's not there, but exit no
25016 matter what. */
25017
25018 /* Do we at least have room for a macinfo type byte? */
25019 if (mac_ptr >= mac_end)
25020 {
25021 dwarf2_section_buffer_overflow_complaint (section);
25022 return;
25023 }
25024
25025 /* We don't increment mac_ptr here, so this is just
25026 a look-ahead. */
25027 next_type
25028 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25029 mac_ptr);
25030 if (next_type != 0)
25031 complaint (_("no terminating 0-type entry for "
25032 "macros in `.debug_macinfo' section"));
25033
25034 return;
25035 }
25036 }
25037 break;
25038
25039 case DW_MACRO_import:
25040 case DW_MACRO_import_sup:
25041 {
25042 LONGEST offset;
25043 void **slot;
25044 bfd *include_bfd = abfd;
25045 struct dwarf2_section_info *include_section = section;
25046 const gdb_byte *include_mac_end = mac_end;
25047 int is_dwz = section_is_dwz;
25048 const gdb_byte *new_mac_ptr;
25049
25050 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25051 mac_ptr += offset_size;
25052
25053 if (macinfo_type == DW_MACRO_import_sup)
25054 {
25055 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
25056
25057 dwarf2_read_section (objfile, &dwz->macro);
25058
25059 include_section = &dwz->macro;
25060 include_bfd = get_section_bfd_owner (include_section);
25061 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25062 is_dwz = 1;
25063 }
25064
25065 new_mac_ptr = include_section->buffer + offset;
25066 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25067
25068 if (*slot != NULL)
25069 {
25070 /* This has actually happened; see
25071 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
25072 complaint (_("recursive DW_MACRO_import in "
25073 ".debug_macro section"));
25074 }
25075 else
25076 {
25077 *slot = (void *) new_mac_ptr;
25078
25079 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
25080 include_mac_end, current_file, lh,
25081 section, section_is_gnu, is_dwz,
25082 offset_size, include_hash);
25083
25084 htab_remove_elt (include_hash, (void *) new_mac_ptr);
25085 }
25086 }
25087 break;
25088
25089 case DW_MACINFO_vendor_ext:
25090 if (!section_is_gnu)
25091 {
25092 unsigned int bytes_read;
25093
25094 /* This reads the constant, but since we don't recognize
25095 any vendor extensions, we ignore it. */
25096 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25097 mac_ptr += bytes_read;
25098 read_direct_string (abfd, mac_ptr, &bytes_read);
25099 mac_ptr += bytes_read;
25100
25101 /* We don't recognize any vendor extensions. */
25102 break;
25103 }
25104 /* FALLTHROUGH */
25105
25106 default:
25107 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25108 mac_ptr, mac_end, abfd, offset_size,
25109 section);
25110 if (mac_ptr == NULL)
25111 return;
25112 break;
25113 }
25114 DIAGNOSTIC_POP
25115 } while (macinfo_type != 0);
25116 }
25117
25118 static void
25119 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25120 int section_is_gnu)
25121 {
25122 struct dwarf2_per_objfile *dwarf2_per_objfile
25123 = cu->per_cu->dwarf2_per_objfile;
25124 struct objfile *objfile = dwarf2_per_objfile->objfile;
25125 struct line_header *lh = cu->line_header;
25126 bfd *abfd;
25127 const gdb_byte *mac_ptr, *mac_end;
25128 struct macro_source_file *current_file = 0;
25129 enum dwarf_macro_record_type macinfo_type;
25130 unsigned int offset_size = cu->header.offset_size;
25131 const gdb_byte *opcode_definitions[256];
25132 void **slot;
25133 struct dwarf2_section_info *section;
25134 const char *section_name;
25135
25136 if (cu->dwo_unit != NULL)
25137 {
25138 if (section_is_gnu)
25139 {
25140 section = &cu->dwo_unit->dwo_file->sections.macro;
25141 section_name = ".debug_macro.dwo";
25142 }
25143 else
25144 {
25145 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25146 section_name = ".debug_macinfo.dwo";
25147 }
25148 }
25149 else
25150 {
25151 if (section_is_gnu)
25152 {
25153 section = &dwarf2_per_objfile->macro;
25154 section_name = ".debug_macro";
25155 }
25156 else
25157 {
25158 section = &dwarf2_per_objfile->macinfo;
25159 section_name = ".debug_macinfo";
25160 }
25161 }
25162
25163 dwarf2_read_section (objfile, section);
25164 if (section->buffer == NULL)
25165 {
25166 complaint (_("missing %s section"), section_name);
25167 return;
25168 }
25169 abfd = get_section_bfd_owner (section);
25170
25171 /* First pass: Find the name of the base filename.
25172 This filename is needed in order to process all macros whose definition
25173 (or undefinition) comes from the command line. These macros are defined
25174 before the first DW_MACINFO_start_file entry, and yet still need to be
25175 associated to the base file.
25176
25177 To determine the base file name, we scan the macro definitions until we
25178 reach the first DW_MACINFO_start_file entry. We then initialize
25179 CURRENT_FILE accordingly so that any macro definition found before the
25180 first DW_MACINFO_start_file can still be associated to the base file. */
25181
25182 mac_ptr = section->buffer + offset;
25183 mac_end = section->buffer + section->size;
25184
25185 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25186 &offset_size, section_is_gnu);
25187 if (mac_ptr == NULL)
25188 {
25189 /* We already issued a complaint. */
25190 return;
25191 }
25192
25193 do
25194 {
25195 /* Do we at least have room for a macinfo type byte? */
25196 if (mac_ptr >= mac_end)
25197 {
25198 /* Complaint is printed during the second pass as GDB will probably
25199 stop the first pass earlier upon finding
25200 DW_MACINFO_start_file. */
25201 break;
25202 }
25203
25204 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25205 mac_ptr++;
25206
25207 /* Note that we rely on the fact that the corresponding GNU and
25208 DWARF constants are the same. */
25209 DIAGNOSTIC_PUSH
25210 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25211 switch (macinfo_type)
25212 {
25213 /* A zero macinfo type indicates the end of the macro
25214 information. */
25215 case 0:
25216 break;
25217
25218 case DW_MACRO_define:
25219 case DW_MACRO_undef:
25220 /* Only skip the data by MAC_PTR. */
25221 {
25222 unsigned int bytes_read;
25223
25224 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25225 mac_ptr += bytes_read;
25226 read_direct_string (abfd, mac_ptr, &bytes_read);
25227 mac_ptr += bytes_read;
25228 }
25229 break;
25230
25231 case DW_MACRO_start_file:
25232 {
25233 unsigned int bytes_read;
25234 int line, file;
25235
25236 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25237 mac_ptr += bytes_read;
25238 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25239 mac_ptr += bytes_read;
25240
25241 current_file = macro_start_file (cu, file, line, current_file, lh);
25242 }
25243 break;
25244
25245 case DW_MACRO_end_file:
25246 /* No data to skip by MAC_PTR. */
25247 break;
25248
25249 case DW_MACRO_define_strp:
25250 case DW_MACRO_undef_strp:
25251 case DW_MACRO_define_sup:
25252 case DW_MACRO_undef_sup:
25253 {
25254 unsigned int bytes_read;
25255
25256 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25257 mac_ptr += bytes_read;
25258 mac_ptr += offset_size;
25259 }
25260 break;
25261
25262 case DW_MACRO_import:
25263 case DW_MACRO_import_sup:
25264 /* Note that, according to the spec, a transparent include
25265 chain cannot call DW_MACRO_start_file. So, we can just
25266 skip this opcode. */
25267 mac_ptr += offset_size;
25268 break;
25269
25270 case DW_MACINFO_vendor_ext:
25271 /* Only skip the data by MAC_PTR. */
25272 if (!section_is_gnu)
25273 {
25274 unsigned int bytes_read;
25275
25276 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25277 mac_ptr += bytes_read;
25278 read_direct_string (abfd, mac_ptr, &bytes_read);
25279 mac_ptr += bytes_read;
25280 }
25281 /* FALLTHROUGH */
25282
25283 default:
25284 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25285 mac_ptr, mac_end, abfd, offset_size,
25286 section);
25287 if (mac_ptr == NULL)
25288 return;
25289 break;
25290 }
25291 DIAGNOSTIC_POP
25292 } while (macinfo_type != 0 && current_file == NULL);
25293
25294 /* Second pass: Process all entries.
25295
25296 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25297 command-line macro definitions/undefinitions. This flag is unset when we
25298 reach the first DW_MACINFO_start_file entry. */
25299
25300 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25301 htab_eq_pointer,
25302 NULL, xcalloc, xfree));
25303 mac_ptr = section->buffer + offset;
25304 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25305 *slot = (void *) mac_ptr;
25306 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25307 current_file, lh, section,
25308 section_is_gnu, 0, offset_size,
25309 include_hash.get ());
25310 }
25311
25312 /* Check if the attribute's form is a DW_FORM_block*
25313 if so return true else false. */
25314
25315 static int
25316 attr_form_is_block (const struct attribute *attr)
25317 {
25318 return (attr == NULL ? 0 :
25319 attr->form == DW_FORM_block1
25320 || attr->form == DW_FORM_block2
25321 || attr->form == DW_FORM_block4
25322 || attr->form == DW_FORM_block
25323 || attr->form == DW_FORM_exprloc);
25324 }
25325
25326 /* Return non-zero if ATTR's value is a section offset --- classes
25327 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25328 You may use DW_UNSND (attr) to retrieve such offsets.
25329
25330 Section 7.5.4, "Attribute Encodings", explains that no attribute
25331 may have a value that belongs to more than one of these classes; it
25332 would be ambiguous if we did, because we use the same forms for all
25333 of them. */
25334
25335 static int
25336 attr_form_is_section_offset (const struct attribute *attr)
25337 {
25338 return (attr->form == DW_FORM_data4
25339 || attr->form == DW_FORM_data8
25340 || attr->form == DW_FORM_sec_offset);
25341 }
25342
25343 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25344 zero otherwise. When this function returns true, you can apply
25345 dwarf2_get_attr_constant_value to it.
25346
25347 However, note that for some attributes you must check
25348 attr_form_is_section_offset before using this test. DW_FORM_data4
25349 and DW_FORM_data8 are members of both the constant class, and of
25350 the classes that contain offsets into other debug sections
25351 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25352 that, if an attribute's can be either a constant or one of the
25353 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25354 taken as section offsets, not constants.
25355
25356 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25357 cannot handle that. */
25358
25359 static int
25360 attr_form_is_constant (const struct attribute *attr)
25361 {
25362 switch (attr->form)
25363 {
25364 case DW_FORM_sdata:
25365 case DW_FORM_udata:
25366 case DW_FORM_data1:
25367 case DW_FORM_data2:
25368 case DW_FORM_data4:
25369 case DW_FORM_data8:
25370 case DW_FORM_implicit_const:
25371 return 1;
25372 default:
25373 return 0;
25374 }
25375 }
25376
25377
25378 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25379 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25380
25381 static int
25382 attr_form_is_ref (const struct attribute *attr)
25383 {
25384 switch (attr->form)
25385 {
25386 case DW_FORM_ref_addr:
25387 case DW_FORM_ref1:
25388 case DW_FORM_ref2:
25389 case DW_FORM_ref4:
25390 case DW_FORM_ref8:
25391 case DW_FORM_ref_udata:
25392 case DW_FORM_GNU_ref_alt:
25393 return 1;
25394 default:
25395 return 0;
25396 }
25397 }
25398
25399 /* Return the .debug_loc section to use for CU.
25400 For DWO files use .debug_loc.dwo. */
25401
25402 static struct dwarf2_section_info *
25403 cu_debug_loc_section (struct dwarf2_cu *cu)
25404 {
25405 struct dwarf2_per_objfile *dwarf2_per_objfile
25406 = cu->per_cu->dwarf2_per_objfile;
25407
25408 if (cu->dwo_unit)
25409 {
25410 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25411
25412 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25413 }
25414 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25415 : &dwarf2_per_objfile->loc);
25416 }
25417
25418 /* A helper function that fills in a dwarf2_loclist_baton. */
25419
25420 static void
25421 fill_in_loclist_baton (struct dwarf2_cu *cu,
25422 struct dwarf2_loclist_baton *baton,
25423 const struct attribute *attr)
25424 {
25425 struct dwarf2_per_objfile *dwarf2_per_objfile
25426 = cu->per_cu->dwarf2_per_objfile;
25427 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25428
25429 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25430
25431 baton->per_cu = cu->per_cu;
25432 gdb_assert (baton->per_cu);
25433 /* We don't know how long the location list is, but make sure we
25434 don't run off the edge of the section. */
25435 baton->size = section->size - DW_UNSND (attr);
25436 baton->data = section->buffer + DW_UNSND (attr);
25437 baton->base_address = cu->base_address;
25438 baton->from_dwo = cu->dwo_unit != NULL;
25439 }
25440
25441 static void
25442 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25443 struct dwarf2_cu *cu, int is_block)
25444 {
25445 struct dwarf2_per_objfile *dwarf2_per_objfile
25446 = cu->per_cu->dwarf2_per_objfile;
25447 struct objfile *objfile = dwarf2_per_objfile->objfile;
25448 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25449
25450 if (attr_form_is_section_offset (attr)
25451 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25452 the section. If so, fall through to the complaint in the
25453 other branch. */
25454 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25455 {
25456 struct dwarf2_loclist_baton *baton;
25457
25458 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25459
25460 fill_in_loclist_baton (cu, baton, attr);
25461
25462 if (cu->base_known == 0)
25463 complaint (_("Location list used without "
25464 "specifying the CU base address."));
25465
25466 SYMBOL_ACLASS_INDEX (sym) = (is_block
25467 ? dwarf2_loclist_block_index
25468 : dwarf2_loclist_index);
25469 SYMBOL_LOCATION_BATON (sym) = baton;
25470 }
25471 else
25472 {
25473 struct dwarf2_locexpr_baton *baton;
25474
25475 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25476 baton->per_cu = cu->per_cu;
25477 gdb_assert (baton->per_cu);
25478
25479 if (attr_form_is_block (attr))
25480 {
25481 /* Note that we're just copying the block's data pointer
25482 here, not the actual data. We're still pointing into the
25483 info_buffer for SYM's objfile; right now we never release
25484 that buffer, but when we do clean up properly this may
25485 need to change. */
25486 baton->size = DW_BLOCK (attr)->size;
25487 baton->data = DW_BLOCK (attr)->data;
25488 }
25489 else
25490 {
25491 dwarf2_invalid_attrib_class_complaint ("location description",
25492 sym->natural_name ());
25493 baton->size = 0;
25494 }
25495
25496 SYMBOL_ACLASS_INDEX (sym) = (is_block
25497 ? dwarf2_locexpr_block_index
25498 : dwarf2_locexpr_index);
25499 SYMBOL_LOCATION_BATON (sym) = baton;
25500 }
25501 }
25502
25503 /* Return the OBJFILE associated with the compilation unit CU. If CU
25504 came from a separate debuginfo file, then the master objfile is
25505 returned. */
25506
25507 struct objfile *
25508 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25509 {
25510 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25511
25512 /* Return the master objfile, so that we can report and look up the
25513 correct file containing this variable. */
25514 if (objfile->separate_debug_objfile_backlink)
25515 objfile = objfile->separate_debug_objfile_backlink;
25516
25517 return objfile;
25518 }
25519
25520 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25521 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25522 CU_HEADERP first. */
25523
25524 static const struct comp_unit_head *
25525 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25526 struct dwarf2_per_cu_data *per_cu)
25527 {
25528 const gdb_byte *info_ptr;
25529
25530 if (per_cu->cu)
25531 return &per_cu->cu->header;
25532
25533 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25534
25535 memset (cu_headerp, 0, sizeof (*cu_headerp));
25536 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25537 rcuh_kind::COMPILE);
25538
25539 return cu_headerp;
25540 }
25541
25542 /* Return the address size given in the compilation unit header for CU. */
25543
25544 int
25545 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25546 {
25547 struct comp_unit_head cu_header_local;
25548 const struct comp_unit_head *cu_headerp;
25549
25550 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25551
25552 return cu_headerp->addr_size;
25553 }
25554
25555 /* Return the offset size given in the compilation unit header for CU. */
25556
25557 int
25558 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25559 {
25560 struct comp_unit_head cu_header_local;
25561 const struct comp_unit_head *cu_headerp;
25562
25563 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25564
25565 return cu_headerp->offset_size;
25566 }
25567
25568 /* See its dwarf2loc.h declaration. */
25569
25570 int
25571 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25572 {
25573 struct comp_unit_head cu_header_local;
25574 const struct comp_unit_head *cu_headerp;
25575
25576 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25577
25578 if (cu_headerp->version == 2)
25579 return cu_headerp->addr_size;
25580 else
25581 return cu_headerp->offset_size;
25582 }
25583
25584 /* Return the text offset of the CU. The returned offset comes from
25585 this CU's objfile. If this objfile came from a separate debuginfo
25586 file, then the offset may be different from the corresponding
25587 offset in the parent objfile. */
25588
25589 CORE_ADDR
25590 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25591 {
25592 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25593
25594 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25595 }
25596
25597 /* Return a type that is a generic pointer type, the size of which matches
25598 the address size given in the compilation unit header for PER_CU. */
25599 static struct type *
25600 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25601 {
25602 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25603 struct type *void_type = objfile_type (objfile)->builtin_void;
25604 struct type *addr_type = lookup_pointer_type (void_type);
25605 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25606
25607 if (TYPE_LENGTH (addr_type) == addr_size)
25608 return addr_type;
25609
25610 addr_type
25611 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25612 return addr_type;
25613 }
25614
25615 /* Return DWARF version number of PER_CU. */
25616
25617 short
25618 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25619 {
25620 return per_cu->dwarf_version;
25621 }
25622
25623 /* Locate the .debug_info compilation unit from CU's objfile which contains
25624 the DIE at OFFSET. Raises an error on failure. */
25625
25626 static struct dwarf2_per_cu_data *
25627 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25628 unsigned int offset_in_dwz,
25629 struct dwarf2_per_objfile *dwarf2_per_objfile)
25630 {
25631 struct dwarf2_per_cu_data *this_cu;
25632 int low, high;
25633
25634 low = 0;
25635 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25636 while (high > low)
25637 {
25638 struct dwarf2_per_cu_data *mid_cu;
25639 int mid = low + (high - low) / 2;
25640
25641 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25642 if (mid_cu->is_dwz > offset_in_dwz
25643 || (mid_cu->is_dwz == offset_in_dwz
25644 && mid_cu->sect_off + mid_cu->length >= sect_off))
25645 high = mid;
25646 else
25647 low = mid + 1;
25648 }
25649 gdb_assert (low == high);
25650 this_cu = dwarf2_per_objfile->all_comp_units[low];
25651 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25652 {
25653 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25654 error (_("Dwarf Error: could not find partial DIE containing "
25655 "offset %s [in module %s]"),
25656 sect_offset_str (sect_off),
25657 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25658
25659 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25660 <= sect_off);
25661 return dwarf2_per_objfile->all_comp_units[low-1];
25662 }
25663 else
25664 {
25665 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25666 && sect_off >= this_cu->sect_off + this_cu->length)
25667 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25668 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25669 return this_cu;
25670 }
25671 }
25672
25673 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25674
25675 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25676 : per_cu (per_cu_),
25677 mark (false),
25678 has_loclist (false),
25679 checked_producer (false),
25680 producer_is_gxx_lt_4_6 (false),
25681 producer_is_gcc_lt_4_3 (false),
25682 producer_is_icc (false),
25683 producer_is_icc_lt_14 (false),
25684 producer_is_codewarrior (false),
25685 processing_has_namespace_info (false)
25686 {
25687 per_cu->cu = this;
25688 }
25689
25690 /* Destroy a dwarf2_cu. */
25691
25692 dwarf2_cu::~dwarf2_cu ()
25693 {
25694 per_cu->cu = NULL;
25695 }
25696
25697 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25698
25699 static void
25700 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25701 enum language pretend_language)
25702 {
25703 struct attribute *attr;
25704
25705 /* Set the language we're debugging. */
25706 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25707 if (attr != nullptr)
25708 set_cu_language (DW_UNSND (attr), cu);
25709 else
25710 {
25711 cu->language = pretend_language;
25712 cu->language_defn = language_def (cu->language);
25713 }
25714
25715 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25716 }
25717
25718 /* Increase the age counter on each cached compilation unit, and free
25719 any that are too old. */
25720
25721 static void
25722 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25723 {
25724 struct dwarf2_per_cu_data *per_cu, **last_chain;
25725
25726 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25727 per_cu = dwarf2_per_objfile->read_in_chain;
25728 while (per_cu != NULL)
25729 {
25730 per_cu->cu->last_used ++;
25731 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25732 dwarf2_mark (per_cu->cu);
25733 per_cu = per_cu->cu->read_in_chain;
25734 }
25735
25736 per_cu = dwarf2_per_objfile->read_in_chain;
25737 last_chain = &dwarf2_per_objfile->read_in_chain;
25738 while (per_cu != NULL)
25739 {
25740 struct dwarf2_per_cu_data *next_cu;
25741
25742 next_cu = per_cu->cu->read_in_chain;
25743
25744 if (!per_cu->cu->mark)
25745 {
25746 delete per_cu->cu;
25747 *last_chain = next_cu;
25748 }
25749 else
25750 last_chain = &per_cu->cu->read_in_chain;
25751
25752 per_cu = next_cu;
25753 }
25754 }
25755
25756 /* Remove a single compilation unit from the cache. */
25757
25758 static void
25759 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25760 {
25761 struct dwarf2_per_cu_data *per_cu, **last_chain;
25762 struct dwarf2_per_objfile *dwarf2_per_objfile
25763 = target_per_cu->dwarf2_per_objfile;
25764
25765 per_cu = dwarf2_per_objfile->read_in_chain;
25766 last_chain = &dwarf2_per_objfile->read_in_chain;
25767 while (per_cu != NULL)
25768 {
25769 struct dwarf2_per_cu_data *next_cu;
25770
25771 next_cu = per_cu->cu->read_in_chain;
25772
25773 if (per_cu == target_per_cu)
25774 {
25775 delete per_cu->cu;
25776 per_cu->cu = NULL;
25777 *last_chain = next_cu;
25778 break;
25779 }
25780 else
25781 last_chain = &per_cu->cu->read_in_chain;
25782
25783 per_cu = next_cu;
25784 }
25785 }
25786
25787 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25788 We store these in a hash table separate from the DIEs, and preserve them
25789 when the DIEs are flushed out of cache.
25790
25791 The CU "per_cu" pointer is needed because offset alone is not enough to
25792 uniquely identify the type. A file may have multiple .debug_types sections,
25793 or the type may come from a DWO file. Furthermore, while it's more logical
25794 to use per_cu->section+offset, with Fission the section with the data is in
25795 the DWO file but we don't know that section at the point we need it.
25796 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25797 because we can enter the lookup routine, get_die_type_at_offset, from
25798 outside this file, and thus won't necessarily have PER_CU->cu.
25799 Fortunately, PER_CU is stable for the life of the objfile. */
25800
25801 struct dwarf2_per_cu_offset_and_type
25802 {
25803 const struct dwarf2_per_cu_data *per_cu;
25804 sect_offset sect_off;
25805 struct type *type;
25806 };
25807
25808 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25809
25810 static hashval_t
25811 per_cu_offset_and_type_hash (const void *item)
25812 {
25813 const struct dwarf2_per_cu_offset_and_type *ofs
25814 = (const struct dwarf2_per_cu_offset_and_type *) item;
25815
25816 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25817 }
25818
25819 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25820
25821 static int
25822 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25823 {
25824 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25825 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25826 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25827 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25828
25829 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25830 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25831 }
25832
25833 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25834 table if necessary. For convenience, return TYPE.
25835
25836 The DIEs reading must have careful ordering to:
25837 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25838 reading current DIE.
25839 * Not trying to dereference contents of still incompletely read in types
25840 while reading in other DIEs.
25841 * Enable referencing still incompletely read in types just by a pointer to
25842 the type without accessing its fields.
25843
25844 Therefore caller should follow these rules:
25845 * Try to fetch any prerequisite types we may need to build this DIE type
25846 before building the type and calling set_die_type.
25847 * After building type call set_die_type for current DIE as soon as
25848 possible before fetching more types to complete the current type.
25849 * Make the type as complete as possible before fetching more types. */
25850
25851 static struct type *
25852 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25853 {
25854 struct dwarf2_per_objfile *dwarf2_per_objfile
25855 = cu->per_cu->dwarf2_per_objfile;
25856 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25857 struct objfile *objfile = dwarf2_per_objfile->objfile;
25858 struct attribute *attr;
25859 struct dynamic_prop prop;
25860
25861 /* For Ada types, make sure that the gnat-specific data is always
25862 initialized (if not already set). There are a few types where
25863 we should not be doing so, because the type-specific area is
25864 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25865 where the type-specific area is used to store the floatformat).
25866 But this is not a problem, because the gnat-specific information
25867 is actually not needed for these types. */
25868 if (need_gnat_info (cu)
25869 && TYPE_CODE (type) != TYPE_CODE_FUNC
25870 && TYPE_CODE (type) != TYPE_CODE_FLT
25871 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25872 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25873 && TYPE_CODE (type) != TYPE_CODE_METHOD
25874 && !HAVE_GNAT_AUX_INFO (type))
25875 INIT_GNAT_SPECIFIC (type);
25876
25877 /* Read DW_AT_allocated and set in type. */
25878 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25879 if (attr_form_is_block (attr))
25880 {
25881 struct type *prop_type
25882 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25883 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25884 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25885 }
25886 else if (attr != NULL)
25887 {
25888 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25889 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25890 sect_offset_str (die->sect_off));
25891 }
25892
25893 /* Read DW_AT_associated and set in type. */
25894 attr = dwarf2_attr (die, DW_AT_associated, cu);
25895 if (attr_form_is_block (attr))
25896 {
25897 struct type *prop_type
25898 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25899 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25900 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25901 }
25902 else if (attr != NULL)
25903 {
25904 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25905 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25906 sect_offset_str (die->sect_off));
25907 }
25908
25909 /* Read DW_AT_data_location and set in type. */
25910 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25911 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25912 dwarf2_per_cu_addr_type (cu->per_cu)))
25913 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25914
25915 if (dwarf2_per_objfile->die_type_hash == NULL)
25916 {
25917 dwarf2_per_objfile->die_type_hash =
25918 htab_create_alloc_ex (127,
25919 per_cu_offset_and_type_hash,
25920 per_cu_offset_and_type_eq,
25921 NULL,
25922 &objfile->objfile_obstack,
25923 hashtab_obstack_allocate,
25924 dummy_obstack_deallocate);
25925 }
25926
25927 ofs.per_cu = cu->per_cu;
25928 ofs.sect_off = die->sect_off;
25929 ofs.type = type;
25930 slot = (struct dwarf2_per_cu_offset_and_type **)
25931 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25932 if (*slot)
25933 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25934 sect_offset_str (die->sect_off));
25935 *slot = XOBNEW (&objfile->objfile_obstack,
25936 struct dwarf2_per_cu_offset_and_type);
25937 **slot = ofs;
25938 return type;
25939 }
25940
25941 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25942 or return NULL if the die does not have a saved type. */
25943
25944 static struct type *
25945 get_die_type_at_offset (sect_offset sect_off,
25946 struct dwarf2_per_cu_data *per_cu)
25947 {
25948 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25949 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25950
25951 if (dwarf2_per_objfile->die_type_hash == NULL)
25952 return NULL;
25953
25954 ofs.per_cu = per_cu;
25955 ofs.sect_off = sect_off;
25956 slot = ((struct dwarf2_per_cu_offset_and_type *)
25957 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25958 if (slot)
25959 return slot->type;
25960 else
25961 return NULL;
25962 }
25963
25964 /* Look up the type for DIE in CU in die_type_hash,
25965 or return NULL if DIE does not have a saved type. */
25966
25967 static struct type *
25968 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25969 {
25970 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25971 }
25972
25973 /* Add a dependence relationship from CU to REF_PER_CU. */
25974
25975 static void
25976 dwarf2_add_dependence (struct dwarf2_cu *cu,
25977 struct dwarf2_per_cu_data *ref_per_cu)
25978 {
25979 void **slot;
25980
25981 if (cu->dependencies == NULL)
25982 cu->dependencies
25983 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25984 NULL, &cu->comp_unit_obstack,
25985 hashtab_obstack_allocate,
25986 dummy_obstack_deallocate);
25987
25988 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25989 if (*slot == NULL)
25990 *slot = ref_per_cu;
25991 }
25992
25993 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25994 Set the mark field in every compilation unit in the
25995 cache that we must keep because we are keeping CU. */
25996
25997 static int
25998 dwarf2_mark_helper (void **slot, void *data)
25999 {
26000 struct dwarf2_per_cu_data *per_cu;
26001
26002 per_cu = (struct dwarf2_per_cu_data *) *slot;
26003
26004 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26005 reading of the chain. As such dependencies remain valid it is not much
26006 useful to track and undo them during QUIT cleanups. */
26007 if (per_cu->cu == NULL)
26008 return 1;
26009
26010 if (per_cu->cu->mark)
26011 return 1;
26012 per_cu->cu->mark = true;
26013
26014 if (per_cu->cu->dependencies != NULL)
26015 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26016
26017 return 1;
26018 }
26019
26020 /* Set the mark field in CU and in every other compilation unit in the
26021 cache that we must keep because we are keeping CU. */
26022
26023 static void
26024 dwarf2_mark (struct dwarf2_cu *cu)
26025 {
26026 if (cu->mark)
26027 return;
26028 cu->mark = true;
26029 if (cu->dependencies != NULL)
26030 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
26031 }
26032
26033 static void
26034 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26035 {
26036 while (per_cu)
26037 {
26038 per_cu->cu->mark = false;
26039 per_cu = per_cu->cu->read_in_chain;
26040 }
26041 }
26042
26043 /* Trivial hash function for partial_die_info: the hash value of a DIE
26044 is its offset in .debug_info for this objfile. */
26045
26046 static hashval_t
26047 partial_die_hash (const void *item)
26048 {
26049 const struct partial_die_info *part_die
26050 = (const struct partial_die_info *) item;
26051
26052 return to_underlying (part_die->sect_off);
26053 }
26054
26055 /* Trivial comparison function for partial_die_info structures: two DIEs
26056 are equal if they have the same offset. */
26057
26058 static int
26059 partial_die_eq (const void *item_lhs, const void *item_rhs)
26060 {
26061 const struct partial_die_info *part_die_lhs
26062 = (const struct partial_die_info *) item_lhs;
26063 const struct partial_die_info *part_die_rhs
26064 = (const struct partial_die_info *) item_rhs;
26065
26066 return part_die_lhs->sect_off == part_die_rhs->sect_off;
26067 }
26068
26069 struct cmd_list_element *set_dwarf_cmdlist;
26070 struct cmd_list_element *show_dwarf_cmdlist;
26071
26072 static void
26073 set_dwarf_cmd (const char *args, int from_tty)
26074 {
26075 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
26076 gdb_stdout);
26077 }
26078
26079 static void
26080 show_dwarf_cmd (const char *args, int from_tty)
26081 {
26082 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
26083 }
26084
26085 bool dwarf_always_disassemble;
26086
26087 static void
26088 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26089 struct cmd_list_element *c, const char *value)
26090 {
26091 fprintf_filtered (file,
26092 _("Whether to always disassemble "
26093 "DWARF expressions is %s.\n"),
26094 value);
26095 }
26096
26097 static void
26098 show_check_physname (struct ui_file *file, int from_tty,
26099 struct cmd_list_element *c, const char *value)
26100 {
26101 fprintf_filtered (file,
26102 _("Whether to check \"physname\" is %s.\n"),
26103 value);
26104 }
26105
26106 void
26107 _initialize_dwarf2_read (void)
26108 {
26109 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26110 Set DWARF specific variables.\n\
26111 Configure DWARF variables such as the cache size."),
26112 &set_dwarf_cmdlist, "maintenance set dwarf ",
26113 0/*allow-unknown*/, &maintenance_set_cmdlist);
26114
26115 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26116 Show DWARF specific variables.\n\
26117 Show DWARF variables such as the cache size."),
26118 &show_dwarf_cmdlist, "maintenance show dwarf ",
26119 0/*allow-unknown*/, &maintenance_show_cmdlist);
26120
26121 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26122 &dwarf_max_cache_age, _("\
26123 Set the upper bound on the age of cached DWARF compilation units."), _("\
26124 Show the upper bound on the age of cached DWARF compilation units."), _("\
26125 A higher limit means that cached compilation units will be stored\n\
26126 in memory longer, and more total memory will be used. Zero disables\n\
26127 caching, which can slow down startup."),
26128 NULL,
26129 show_dwarf_max_cache_age,
26130 &set_dwarf_cmdlist,
26131 &show_dwarf_cmdlist);
26132
26133 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26134 &dwarf_always_disassemble, _("\
26135 Set whether `info address' always disassembles DWARF expressions."), _("\
26136 Show whether `info address' always disassembles DWARF expressions."), _("\
26137 When enabled, DWARF expressions are always printed in an assembly-like\n\
26138 syntax. When disabled, expressions will be printed in a more\n\
26139 conversational style, when possible."),
26140 NULL,
26141 show_dwarf_always_disassemble,
26142 &set_dwarf_cmdlist,
26143 &show_dwarf_cmdlist);
26144
26145 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26146 Set debugging of the DWARF reader."), _("\
26147 Show debugging of the DWARF reader."), _("\
26148 When enabled (non-zero), debugging messages are printed during DWARF\n\
26149 reading and symtab expansion. A value of 1 (one) provides basic\n\
26150 information. A value greater than 1 provides more verbose information."),
26151 NULL,
26152 NULL,
26153 &setdebuglist, &showdebuglist);
26154
26155 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26156 Set debugging of the DWARF DIE reader."), _("\
26157 Show debugging of the DWARF DIE reader."), _("\
26158 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26159 The value is the maximum depth to print."),
26160 NULL,
26161 NULL,
26162 &setdebuglist, &showdebuglist);
26163
26164 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26165 Set debugging of the dwarf line reader."), _("\
26166 Show debugging of the dwarf line reader."), _("\
26167 When enabled (non-zero), line number entries are dumped as they are read in.\n\
26168 A value of 1 (one) provides basic information.\n\
26169 A value greater than 1 provides more verbose information."),
26170 NULL,
26171 NULL,
26172 &setdebuglist, &showdebuglist);
26173
26174 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26175 Set cross-checking of \"physname\" code against demangler."), _("\
26176 Show cross-checking of \"physname\" code against demangler."), _("\
26177 When enabled, GDB's internal \"physname\" code is checked against\n\
26178 the demangler."),
26179 NULL, show_check_physname,
26180 &setdebuglist, &showdebuglist);
26181
26182 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26183 no_class, &use_deprecated_index_sections, _("\
26184 Set whether to use deprecated gdb_index sections."), _("\
26185 Show whether to use deprecated gdb_index sections."), _("\
26186 When enabled, deprecated .gdb_index sections are used anyway.\n\
26187 Normally they are ignored either because of a missing feature or\n\
26188 performance issue.\n\
26189 Warning: This option must be enabled before gdb reads the file."),
26190 NULL,
26191 NULL,
26192 &setlist, &showlist);
26193
26194 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26195 &dwarf2_locexpr_funcs);
26196 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26197 &dwarf2_loclist_funcs);
26198
26199 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26200 &dwarf2_block_frame_base_locexpr_funcs);
26201 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26202 &dwarf2_block_frame_base_loclist_funcs);
26203
26204 #if GDB_SELF_TEST
26205 selftests::register_test ("dw2_expand_symtabs_matching",
26206 selftests::dw2_expand_symtabs_matching::run_test);
26207 #endif
26208 }
This page took 0.57209 seconds and 4 git commands to generate.