Sort includes for files gdb/[a-f]*.[chyl].
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2019 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32
33 /* Standard C includes. */
34 #include <ctype.h>
35 #include <fcntl.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38
39 /* Standard C++ includes. */
40 #include <algorithm>
41 #include <cmath>
42 #include <forward_list>
43 #include <set>
44 #include <unordered_map>
45 #include <unordered_set>
46
47 /* Local non-gdb includes. */
48 #include "addrmap.h"
49 #include "bfd.h"
50 #include "block.h"
51 #include "build-id.h"
52 #include "buildsym.h"
53 #include "c-lang.h"
54 #include "command.h"
55 #include "common/byte-vector.h"
56 #include "common/filestuff.h"
57 #include "common/function-view.h"
58 #include "common/gdb_optional.h"
59 #include "common/gdb_unlinker.h"
60 #include "common/hash_enum.h"
61 #include "common/pathstuff.h"
62 #include "common/selftest.h"
63 #include "common/underlying.h"
64 #include "common/vec.h"
65 #include "complaints.h"
66 #include "completer.h"
67 #include "cp-support.h"
68 #include "demangle.h"
69 #include "dwarf-index-cache.h"
70 #include "dwarf-index-common.h"
71 #include "dwarf2.h"
72 #include "dwarf2expr.h"
73 #include "dwarf2loc.h"
74 #include "dwarf2read.h"
75 #include "elf-bfd.h"
76 #include "expression.h"
77 #include "f-lang.h"
78 #include "filename-seen-cache.h"
79 #include "filenames.h"
80 #include "gdb-demangle.h"
81 #include "gdb/gdb-index.h"
82 #include "gdb_bfd.h"
83 #include "gdbcmd.h"
84 #include "gdbcore.h"
85 #include "gdbtypes.h"
86 #include "go-lang.h"
87 #include "hashtab.h"
88 #include "language.h"
89 #include "macrotab.h"
90 #include "namespace.h"
91 #include "objfiles.h"
92 #include "producer.h"
93 #include "psympriv.h"
94 #include "rust-lang.h"
95 #include "source.h"
96 #include "symtab.h"
97 #include "typeprint.h"
98 #include "valprint.h"
99
100 /* When == 1, print basic high level tracing messages.
101 When > 1, be more verbose.
102 This is in contrast to the low level DIE reading of dwarf_die_debug. */
103 static unsigned int dwarf_read_debug = 0;
104
105 /* When non-zero, dump DIEs after they are read in. */
106 static unsigned int dwarf_die_debug = 0;
107
108 /* When non-zero, dump line number entries as they are read in. */
109 static unsigned int dwarf_line_debug = 0;
110
111 /* When non-zero, cross-check physname against demangler. */
112 static int check_physname = 0;
113
114 /* When non-zero, do not reject deprecated .gdb_index sections. */
115 static int use_deprecated_index_sections = 0;
116
117 static const struct objfile_data *dwarf2_objfile_data_key;
118
119 /* The "aclass" indices for various kinds of computed DWARF symbols. */
120
121 static int dwarf2_locexpr_index;
122 static int dwarf2_loclist_index;
123 static int dwarf2_locexpr_block_index;
124 static int dwarf2_loclist_block_index;
125
126 /* An index into a (C++) symbol name component in a symbol name as
127 recorded in the mapped_index's symbol table. For each C++ symbol
128 in the symbol table, we record one entry for the start of each
129 component in the symbol in a table of name components, and then
130 sort the table, in order to be able to binary search symbol names,
131 ignoring leading namespaces, both completion and regular look up.
132 For example, for symbol "A::B::C", we'll have an entry that points
133 to "A::B::C", another that points to "B::C", and another for "C".
134 Note that function symbols in GDB index have no parameter
135 information, just the function/method names. You can convert a
136 name_component to a "const char *" using the
137 'mapped_index::symbol_name_at(offset_type)' method. */
138
139 struct name_component
140 {
141 /* Offset in the symbol name where the component starts. Stored as
142 a (32-bit) offset instead of a pointer to save memory and improve
143 locality on 64-bit architectures. */
144 offset_type name_offset;
145
146 /* The symbol's index in the symbol and constant pool tables of a
147 mapped_index. */
148 offset_type idx;
149 };
150
151 /* Base class containing bits shared by both .gdb_index and
152 .debug_name indexes. */
153
154 struct mapped_index_base
155 {
156 mapped_index_base () = default;
157 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
158
159 /* The name_component table (a sorted vector). See name_component's
160 description above. */
161 std::vector<name_component> name_components;
162
163 /* How NAME_COMPONENTS is sorted. */
164 enum case_sensitivity name_components_casing;
165
166 /* Return the number of names in the symbol table. */
167 virtual size_t symbol_name_count () const = 0;
168
169 /* Get the name of the symbol at IDX in the symbol table. */
170 virtual const char *symbol_name_at (offset_type idx) const = 0;
171
172 /* Return whether the name at IDX in the symbol table should be
173 ignored. */
174 virtual bool symbol_name_slot_invalid (offset_type idx) const
175 {
176 return false;
177 }
178
179 /* Build the symbol name component sorted vector, if we haven't
180 yet. */
181 void build_name_components ();
182
183 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
184 possible matches for LN_NO_PARAMS in the name component
185 vector. */
186 std::pair<std::vector<name_component>::const_iterator,
187 std::vector<name_component>::const_iterator>
188 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
189
190 /* Prevent deleting/destroying via a base class pointer. */
191 protected:
192 ~mapped_index_base() = default;
193 };
194
195 /* A description of the mapped index. The file format is described in
196 a comment by the code that writes the index. */
197 struct mapped_index final : public mapped_index_base
198 {
199 /* A slot/bucket in the symbol table hash. */
200 struct symbol_table_slot
201 {
202 const offset_type name;
203 const offset_type vec;
204 };
205
206 /* Index data format version. */
207 int version = 0;
208
209 /* The address table data. */
210 gdb::array_view<const gdb_byte> address_table;
211
212 /* The symbol table, implemented as a hash table. */
213 gdb::array_view<symbol_table_slot> symbol_table;
214
215 /* A pointer to the constant pool. */
216 const char *constant_pool = nullptr;
217
218 bool symbol_name_slot_invalid (offset_type idx) const override
219 {
220 const auto &bucket = this->symbol_table[idx];
221 return bucket.name == 0 && bucket.vec;
222 }
223
224 /* Convenience method to get at the name of the symbol at IDX in the
225 symbol table. */
226 const char *symbol_name_at (offset_type idx) const override
227 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
228
229 size_t symbol_name_count () const override
230 { return this->symbol_table.size (); }
231 };
232
233 /* A description of the mapped .debug_names.
234 Uninitialized map has CU_COUNT 0. */
235 struct mapped_debug_names final : public mapped_index_base
236 {
237 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
238 : dwarf2_per_objfile (dwarf2_per_objfile_)
239 {}
240
241 struct dwarf2_per_objfile *dwarf2_per_objfile;
242 bfd_endian dwarf5_byte_order;
243 bool dwarf5_is_dwarf64;
244 bool augmentation_is_gdb;
245 uint8_t offset_size;
246 uint32_t cu_count = 0;
247 uint32_t tu_count, bucket_count, name_count;
248 const gdb_byte *cu_table_reordered, *tu_table_reordered;
249 const uint32_t *bucket_table_reordered, *hash_table_reordered;
250 const gdb_byte *name_table_string_offs_reordered;
251 const gdb_byte *name_table_entry_offs_reordered;
252 const gdb_byte *entry_pool;
253
254 struct index_val
255 {
256 ULONGEST dwarf_tag;
257 struct attr
258 {
259 /* Attribute name DW_IDX_*. */
260 ULONGEST dw_idx;
261
262 /* Attribute form DW_FORM_*. */
263 ULONGEST form;
264
265 /* Value if FORM is DW_FORM_implicit_const. */
266 LONGEST implicit_const;
267 };
268 std::vector<attr> attr_vec;
269 };
270
271 std::unordered_map<ULONGEST, index_val> abbrev_map;
272
273 const char *namei_to_name (uint32_t namei) const;
274
275 /* Implementation of the mapped_index_base virtual interface, for
276 the name_components cache. */
277
278 const char *symbol_name_at (offset_type idx) const override
279 { return namei_to_name (idx); }
280
281 size_t symbol_name_count () const override
282 { return this->name_count; }
283 };
284
285 /* See dwarf2read.h. */
286
287 dwarf2_per_objfile *
288 get_dwarf2_per_objfile (struct objfile *objfile)
289 {
290 return ((struct dwarf2_per_objfile *)
291 objfile_data (objfile, dwarf2_objfile_data_key));
292 }
293
294 /* Set the dwarf2_per_objfile associated to OBJFILE. */
295
296 void
297 set_dwarf2_per_objfile (struct objfile *objfile,
298 struct dwarf2_per_objfile *dwarf2_per_objfile)
299 {
300 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
301 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
302 }
303
304 /* Default names of the debugging sections. */
305
306 /* Note that if the debugging section has been compressed, it might
307 have a name like .zdebug_info. */
308
309 static const struct dwarf2_debug_sections dwarf2_elf_names =
310 {
311 { ".debug_info", ".zdebug_info" },
312 { ".debug_abbrev", ".zdebug_abbrev" },
313 { ".debug_line", ".zdebug_line" },
314 { ".debug_loc", ".zdebug_loc" },
315 { ".debug_loclists", ".zdebug_loclists" },
316 { ".debug_macinfo", ".zdebug_macinfo" },
317 { ".debug_macro", ".zdebug_macro" },
318 { ".debug_str", ".zdebug_str" },
319 { ".debug_line_str", ".zdebug_line_str" },
320 { ".debug_ranges", ".zdebug_ranges" },
321 { ".debug_rnglists", ".zdebug_rnglists" },
322 { ".debug_types", ".zdebug_types" },
323 { ".debug_addr", ".zdebug_addr" },
324 { ".debug_frame", ".zdebug_frame" },
325 { ".eh_frame", NULL },
326 { ".gdb_index", ".zgdb_index" },
327 { ".debug_names", ".zdebug_names" },
328 { ".debug_aranges", ".zdebug_aranges" },
329 23
330 };
331
332 /* List of DWO/DWP sections. */
333
334 static const struct dwop_section_names
335 {
336 struct dwarf2_section_names abbrev_dwo;
337 struct dwarf2_section_names info_dwo;
338 struct dwarf2_section_names line_dwo;
339 struct dwarf2_section_names loc_dwo;
340 struct dwarf2_section_names loclists_dwo;
341 struct dwarf2_section_names macinfo_dwo;
342 struct dwarf2_section_names macro_dwo;
343 struct dwarf2_section_names str_dwo;
344 struct dwarf2_section_names str_offsets_dwo;
345 struct dwarf2_section_names types_dwo;
346 struct dwarf2_section_names cu_index;
347 struct dwarf2_section_names tu_index;
348 }
349 dwop_section_names =
350 {
351 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
352 { ".debug_info.dwo", ".zdebug_info.dwo" },
353 { ".debug_line.dwo", ".zdebug_line.dwo" },
354 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
355 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
356 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
357 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
358 { ".debug_str.dwo", ".zdebug_str.dwo" },
359 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
360 { ".debug_types.dwo", ".zdebug_types.dwo" },
361 { ".debug_cu_index", ".zdebug_cu_index" },
362 { ".debug_tu_index", ".zdebug_tu_index" },
363 };
364
365 /* local data types */
366
367 /* The data in a compilation unit header, after target2host
368 translation, looks like this. */
369 struct comp_unit_head
370 {
371 unsigned int length;
372 short version;
373 unsigned char addr_size;
374 unsigned char signed_addr_p;
375 sect_offset abbrev_sect_off;
376
377 /* Size of file offsets; either 4 or 8. */
378 unsigned int offset_size;
379
380 /* Size of the length field; either 4 or 12. */
381 unsigned int initial_length_size;
382
383 enum dwarf_unit_type unit_type;
384
385 /* Offset to the first byte of this compilation unit header in the
386 .debug_info section, for resolving relative reference dies. */
387 sect_offset sect_off;
388
389 /* Offset to first die in this cu from the start of the cu.
390 This will be the first byte following the compilation unit header. */
391 cu_offset first_die_cu_offset;
392
393 /* 64-bit signature of this type unit - it is valid only for
394 UNIT_TYPE DW_UT_type. */
395 ULONGEST signature;
396
397 /* For types, offset in the type's DIE of the type defined by this TU. */
398 cu_offset type_cu_offset_in_tu;
399 };
400
401 /* Type used for delaying computation of method physnames.
402 See comments for compute_delayed_physnames. */
403 struct delayed_method_info
404 {
405 /* The type to which the method is attached, i.e., its parent class. */
406 struct type *type;
407
408 /* The index of the method in the type's function fieldlists. */
409 int fnfield_index;
410
411 /* The index of the method in the fieldlist. */
412 int index;
413
414 /* The name of the DIE. */
415 const char *name;
416
417 /* The DIE associated with this method. */
418 struct die_info *die;
419 };
420
421 /* Internal state when decoding a particular compilation unit. */
422 struct dwarf2_cu
423 {
424 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
425 ~dwarf2_cu ();
426
427 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
428
429 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
430 Create the set of symtabs used by this TU, or if this TU is sharing
431 symtabs with another TU and the symtabs have already been created
432 then restore those symtabs in the line header.
433 We don't need the pc/line-number mapping for type units. */
434 void setup_type_unit_groups (struct die_info *die);
435
436 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
437 buildsym_compunit constructor. */
438 struct compunit_symtab *start_symtab (const char *name,
439 const char *comp_dir,
440 CORE_ADDR low_pc);
441
442 /* Reset the builder. */
443 void reset_builder () { m_builder.reset (); }
444
445 /* The header of the compilation unit. */
446 struct comp_unit_head header {};
447
448 /* Base address of this compilation unit. */
449 CORE_ADDR base_address = 0;
450
451 /* Non-zero if base_address has been set. */
452 int base_known = 0;
453
454 /* The language we are debugging. */
455 enum language language = language_unknown;
456 const struct language_defn *language_defn = nullptr;
457
458 const char *producer = nullptr;
459
460 private:
461 /* The symtab builder for this CU. This is only non-NULL when full
462 symbols are being read. */
463 std::unique_ptr<buildsym_compunit> m_builder;
464
465 public:
466 /* The generic symbol table building routines have separate lists for
467 file scope symbols and all all other scopes (local scopes). So
468 we need to select the right one to pass to add_symbol_to_list().
469 We do it by keeping a pointer to the correct list in list_in_scope.
470
471 FIXME: The original dwarf code just treated the file scope as the
472 first local scope, and all other local scopes as nested local
473 scopes, and worked fine. Check to see if we really need to
474 distinguish these in buildsym.c. */
475 struct pending **list_in_scope = nullptr;
476
477 /* Hash table holding all the loaded partial DIEs
478 with partial_die->offset.SECT_OFF as hash. */
479 htab_t partial_dies = nullptr;
480
481 /* Storage for things with the same lifetime as this read-in compilation
482 unit, including partial DIEs. */
483 auto_obstack comp_unit_obstack;
484
485 /* When multiple dwarf2_cu structures are living in memory, this field
486 chains them all together, so that they can be released efficiently.
487 We will probably also want a generation counter so that most-recently-used
488 compilation units are cached... */
489 struct dwarf2_per_cu_data *read_in_chain = nullptr;
490
491 /* Backlink to our per_cu entry. */
492 struct dwarf2_per_cu_data *per_cu;
493
494 /* How many compilation units ago was this CU last referenced? */
495 int last_used = 0;
496
497 /* A hash table of DIE cu_offset for following references with
498 die_info->offset.sect_off as hash. */
499 htab_t die_hash = nullptr;
500
501 /* Full DIEs if read in. */
502 struct die_info *dies = nullptr;
503
504 /* A set of pointers to dwarf2_per_cu_data objects for compilation
505 units referenced by this one. Only set during full symbol processing;
506 partial symbol tables do not have dependencies. */
507 htab_t dependencies = nullptr;
508
509 /* Header data from the line table, during full symbol processing. */
510 struct line_header *line_header = nullptr;
511 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
512 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
513 this is the DW_TAG_compile_unit die for this CU. We'll hold on
514 to the line header as long as this DIE is being processed. See
515 process_die_scope. */
516 die_info *line_header_die_owner = nullptr;
517
518 /* A list of methods which need to have physnames computed
519 after all type information has been read. */
520 std::vector<delayed_method_info> method_list;
521
522 /* To be copied to symtab->call_site_htab. */
523 htab_t call_site_htab = nullptr;
524
525 /* Non-NULL if this CU came from a DWO file.
526 There is an invariant here that is important to remember:
527 Except for attributes copied from the top level DIE in the "main"
528 (or "stub") file in preparation for reading the DWO file
529 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
530 Either there isn't a DWO file (in which case this is NULL and the point
531 is moot), or there is and either we're not going to read it (in which
532 case this is NULL) or there is and we are reading it (in which case this
533 is non-NULL). */
534 struct dwo_unit *dwo_unit = nullptr;
535
536 /* The DW_AT_addr_base attribute if present, zero otherwise
537 (zero is a valid value though).
538 Note this value comes from the Fission stub CU/TU's DIE. */
539 ULONGEST addr_base = 0;
540
541 /* The DW_AT_ranges_base attribute if present, zero otherwise
542 (zero is a valid value though).
543 Note this value comes from the Fission stub CU/TU's DIE.
544 Also note that the value is zero in the non-DWO case so this value can
545 be used without needing to know whether DWO files are in use or not.
546 N.B. This does not apply to DW_AT_ranges appearing in
547 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
548 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
549 DW_AT_ranges_base *would* have to be applied, and we'd have to care
550 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
551 ULONGEST ranges_base = 0;
552
553 /* When reading debug info generated by older versions of rustc, we
554 have to rewrite some union types to be struct types with a
555 variant part. This rewriting must be done after the CU is fully
556 read in, because otherwise at the point of rewriting some struct
557 type might not have been fully processed. So, we keep a list of
558 all such types here and process them after expansion. */
559 std::vector<struct type *> rust_unions;
560
561 /* Mark used when releasing cached dies. */
562 bool mark : 1;
563
564 /* This CU references .debug_loc. See the symtab->locations_valid field.
565 This test is imperfect as there may exist optimized debug code not using
566 any location list and still facing inlining issues if handled as
567 unoptimized code. For a future better test see GCC PR other/32998. */
568 bool has_loclist : 1;
569
570 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
571 if all the producer_is_* fields are valid. This information is cached
572 because profiling CU expansion showed excessive time spent in
573 producer_is_gxx_lt_4_6. */
574 bool checked_producer : 1;
575 bool producer_is_gxx_lt_4_6 : 1;
576 bool producer_is_gcc_lt_4_3 : 1;
577 bool producer_is_icc : 1;
578 bool producer_is_icc_lt_14 : 1;
579 bool producer_is_codewarrior : 1;
580
581 /* When true, the file that we're processing is known to have
582 debugging info for C++ namespaces. GCC 3.3.x did not produce
583 this information, but later versions do. */
584
585 bool processing_has_namespace_info : 1;
586
587 struct partial_die_info *find_partial_die (sect_offset sect_off);
588
589 /* If this CU was inherited by another CU (via specification,
590 abstract_origin, etc), this is the ancestor CU. */
591 dwarf2_cu *ancestor;
592
593 /* Get the buildsym_compunit for this CU. */
594 buildsym_compunit *get_builder ()
595 {
596 /* If this CU has a builder associated with it, use that. */
597 if (m_builder != nullptr)
598 return m_builder.get ();
599
600 /* Otherwise, search ancestors for a valid builder. */
601 if (ancestor != nullptr)
602 return ancestor->get_builder ();
603
604 return nullptr;
605 }
606 };
607
608 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
609 This includes type_unit_group and quick_file_names. */
610
611 struct stmt_list_hash
612 {
613 /* The DWO unit this table is from or NULL if there is none. */
614 struct dwo_unit *dwo_unit;
615
616 /* Offset in .debug_line or .debug_line.dwo. */
617 sect_offset line_sect_off;
618 };
619
620 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
621 an object of this type. */
622
623 struct type_unit_group
624 {
625 /* dwarf2read.c's main "handle" on a TU symtab.
626 To simplify things we create an artificial CU that "includes" all the
627 type units using this stmt_list so that the rest of the code still has
628 a "per_cu" handle on the symtab.
629 This PER_CU is recognized by having no section. */
630 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
631 struct dwarf2_per_cu_data per_cu;
632
633 /* The TUs that share this DW_AT_stmt_list entry.
634 This is added to while parsing type units to build partial symtabs,
635 and is deleted afterwards and not used again. */
636 VEC (sig_type_ptr) *tus;
637
638 /* The compunit symtab.
639 Type units in a group needn't all be defined in the same source file,
640 so we create an essentially anonymous symtab as the compunit symtab. */
641 struct compunit_symtab *compunit_symtab;
642
643 /* The data used to construct the hash key. */
644 struct stmt_list_hash hash;
645
646 /* The number of symtabs from the line header.
647 The value here must match line_header.num_file_names. */
648 unsigned int num_symtabs;
649
650 /* The symbol tables for this TU (obtained from the files listed in
651 DW_AT_stmt_list).
652 WARNING: The order of entries here must match the order of entries
653 in the line header. After the first TU using this type_unit_group, the
654 line header for the subsequent TUs is recreated from this. This is done
655 because we need to use the same symtabs for each TU using the same
656 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
657 there's no guarantee the line header doesn't have duplicate entries. */
658 struct symtab **symtabs;
659 };
660
661 /* These sections are what may appear in a (real or virtual) DWO file. */
662
663 struct dwo_sections
664 {
665 struct dwarf2_section_info abbrev;
666 struct dwarf2_section_info line;
667 struct dwarf2_section_info loc;
668 struct dwarf2_section_info loclists;
669 struct dwarf2_section_info macinfo;
670 struct dwarf2_section_info macro;
671 struct dwarf2_section_info str;
672 struct dwarf2_section_info str_offsets;
673 /* In the case of a virtual DWO file, these two are unused. */
674 struct dwarf2_section_info info;
675 VEC (dwarf2_section_info_def) *types;
676 };
677
678 /* CUs/TUs in DWP/DWO files. */
679
680 struct dwo_unit
681 {
682 /* Backlink to the containing struct dwo_file. */
683 struct dwo_file *dwo_file;
684
685 /* The "id" that distinguishes this CU/TU.
686 .debug_info calls this "dwo_id", .debug_types calls this "signature".
687 Since signatures came first, we stick with it for consistency. */
688 ULONGEST signature;
689
690 /* The section this CU/TU lives in, in the DWO file. */
691 struct dwarf2_section_info *section;
692
693 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
694 sect_offset sect_off;
695 unsigned int length;
696
697 /* For types, offset in the type's DIE of the type defined by this TU. */
698 cu_offset type_offset_in_tu;
699 };
700
701 /* include/dwarf2.h defines the DWP section codes.
702 It defines a max value but it doesn't define a min value, which we
703 use for error checking, so provide one. */
704
705 enum dwp_v2_section_ids
706 {
707 DW_SECT_MIN = 1
708 };
709
710 /* Data for one DWO file.
711
712 This includes virtual DWO files (a virtual DWO file is a DWO file as it
713 appears in a DWP file). DWP files don't really have DWO files per se -
714 comdat folding of types "loses" the DWO file they came from, and from
715 a high level view DWP files appear to contain a mass of random types.
716 However, to maintain consistency with the non-DWP case we pretend DWP
717 files contain virtual DWO files, and we assign each TU with one virtual
718 DWO file (generally based on the line and abbrev section offsets -
719 a heuristic that seems to work in practice). */
720
721 struct dwo_file
722 {
723 /* The DW_AT_GNU_dwo_name attribute.
724 For virtual DWO files the name is constructed from the section offsets
725 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
726 from related CU+TUs. */
727 const char *dwo_name;
728
729 /* The DW_AT_comp_dir attribute. */
730 const char *comp_dir;
731
732 /* The bfd, when the file is open. Otherwise this is NULL.
733 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
734 bfd *dbfd;
735
736 /* The sections that make up this DWO file.
737 Remember that for virtual DWO files in DWP V2, these are virtual
738 sections (for lack of a better name). */
739 struct dwo_sections sections;
740
741 /* The CUs in the file.
742 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
743 an extension to handle LLVM's Link Time Optimization output (where
744 multiple source files may be compiled into a single object/dwo pair). */
745 htab_t cus;
746
747 /* Table of TUs in the file.
748 Each element is a struct dwo_unit. */
749 htab_t tus;
750 };
751
752 /* These sections are what may appear in a DWP file. */
753
754 struct dwp_sections
755 {
756 /* These are used by both DWP version 1 and 2. */
757 struct dwarf2_section_info str;
758 struct dwarf2_section_info cu_index;
759 struct dwarf2_section_info tu_index;
760
761 /* These are only used by DWP version 2 files.
762 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
763 sections are referenced by section number, and are not recorded here.
764 In DWP version 2 there is at most one copy of all these sections, each
765 section being (effectively) comprised of the concatenation of all of the
766 individual sections that exist in the version 1 format.
767 To keep the code simple we treat each of these concatenated pieces as a
768 section itself (a virtual section?). */
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info info;
771 struct dwarf2_section_info line;
772 struct dwarf2_section_info loc;
773 struct dwarf2_section_info macinfo;
774 struct dwarf2_section_info macro;
775 struct dwarf2_section_info str_offsets;
776 struct dwarf2_section_info types;
777 };
778
779 /* These sections are what may appear in a virtual DWO file in DWP version 1.
780 A virtual DWO file is a DWO file as it appears in a DWP file. */
781
782 struct virtual_v1_dwo_sections
783 {
784 struct dwarf2_section_info abbrev;
785 struct dwarf2_section_info line;
786 struct dwarf2_section_info loc;
787 struct dwarf2_section_info macinfo;
788 struct dwarf2_section_info macro;
789 struct dwarf2_section_info str_offsets;
790 /* Each DWP hash table entry records one CU or one TU.
791 That is recorded here, and copied to dwo_unit.section. */
792 struct dwarf2_section_info info_or_types;
793 };
794
795 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
796 In version 2, the sections of the DWO files are concatenated together
797 and stored in one section of that name. Thus each ELF section contains
798 several "virtual" sections. */
799
800 struct virtual_v2_dwo_sections
801 {
802 bfd_size_type abbrev_offset;
803 bfd_size_type abbrev_size;
804
805 bfd_size_type line_offset;
806 bfd_size_type line_size;
807
808 bfd_size_type loc_offset;
809 bfd_size_type loc_size;
810
811 bfd_size_type macinfo_offset;
812 bfd_size_type macinfo_size;
813
814 bfd_size_type macro_offset;
815 bfd_size_type macro_size;
816
817 bfd_size_type str_offsets_offset;
818 bfd_size_type str_offsets_size;
819
820 /* Each DWP hash table entry records one CU or one TU.
821 That is recorded here, and copied to dwo_unit.section. */
822 bfd_size_type info_or_types_offset;
823 bfd_size_type info_or_types_size;
824 };
825
826 /* Contents of DWP hash tables. */
827
828 struct dwp_hash_table
829 {
830 uint32_t version, nr_columns;
831 uint32_t nr_units, nr_slots;
832 const gdb_byte *hash_table, *unit_table;
833 union
834 {
835 struct
836 {
837 const gdb_byte *indices;
838 } v1;
839 struct
840 {
841 /* This is indexed by column number and gives the id of the section
842 in that column. */
843 #define MAX_NR_V2_DWO_SECTIONS \
844 (1 /* .debug_info or .debug_types */ \
845 + 1 /* .debug_abbrev */ \
846 + 1 /* .debug_line */ \
847 + 1 /* .debug_loc */ \
848 + 1 /* .debug_str_offsets */ \
849 + 1 /* .debug_macro or .debug_macinfo */)
850 int section_ids[MAX_NR_V2_DWO_SECTIONS];
851 const gdb_byte *offsets;
852 const gdb_byte *sizes;
853 } v2;
854 } section_pool;
855 };
856
857 /* Data for one DWP file. */
858
859 struct dwp_file
860 {
861 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
862 : name (name_),
863 dbfd (std::move (abfd))
864 {
865 }
866
867 /* Name of the file. */
868 const char *name;
869
870 /* File format version. */
871 int version = 0;
872
873 /* The bfd. */
874 gdb_bfd_ref_ptr dbfd;
875
876 /* Section info for this file. */
877 struct dwp_sections sections {};
878
879 /* Table of CUs in the file. */
880 const struct dwp_hash_table *cus = nullptr;
881
882 /* Table of TUs in the file. */
883 const struct dwp_hash_table *tus = nullptr;
884
885 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
886 htab_t loaded_cus {};
887 htab_t loaded_tus {};
888
889 /* Table to map ELF section numbers to their sections.
890 This is only needed for the DWP V1 file format. */
891 unsigned int num_sections = 0;
892 asection **elf_sections = nullptr;
893 };
894
895 /* This represents a '.dwz' file. */
896
897 struct dwz_file
898 {
899 dwz_file (gdb_bfd_ref_ptr &&bfd)
900 : dwz_bfd (std::move (bfd))
901 {
902 }
903
904 /* A dwz file can only contain a few sections. */
905 struct dwarf2_section_info abbrev {};
906 struct dwarf2_section_info info {};
907 struct dwarf2_section_info str {};
908 struct dwarf2_section_info line {};
909 struct dwarf2_section_info macro {};
910 struct dwarf2_section_info gdb_index {};
911 struct dwarf2_section_info debug_names {};
912
913 /* The dwz's BFD. */
914 gdb_bfd_ref_ptr dwz_bfd;
915
916 /* If we loaded the index from an external file, this contains the
917 resources associated to the open file, memory mapping, etc. */
918 std::unique_ptr<index_cache_resource> index_cache_res;
919 };
920
921 /* Struct used to pass misc. parameters to read_die_and_children, et
922 al. which are used for both .debug_info and .debug_types dies.
923 All parameters here are unchanging for the life of the call. This
924 struct exists to abstract away the constant parameters of die reading. */
925
926 struct die_reader_specs
927 {
928 /* The bfd of die_section. */
929 bfd* abfd;
930
931 /* The CU of the DIE we are parsing. */
932 struct dwarf2_cu *cu;
933
934 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
935 struct dwo_file *dwo_file;
936
937 /* The section the die comes from.
938 This is either .debug_info or .debug_types, or the .dwo variants. */
939 struct dwarf2_section_info *die_section;
940
941 /* die_section->buffer. */
942 const gdb_byte *buffer;
943
944 /* The end of the buffer. */
945 const gdb_byte *buffer_end;
946
947 /* The value of the DW_AT_comp_dir attribute. */
948 const char *comp_dir;
949
950 /* The abbreviation table to use when reading the DIEs. */
951 struct abbrev_table *abbrev_table;
952 };
953
954 /* Type of function passed to init_cutu_and_read_dies, et.al. */
955 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
956 const gdb_byte *info_ptr,
957 struct die_info *comp_unit_die,
958 int has_children,
959 void *data);
960
961 /* A 1-based directory index. This is a strong typedef to prevent
962 accidentally using a directory index as a 0-based index into an
963 array/vector. */
964 enum class dir_index : unsigned int {};
965
966 /* Likewise, a 1-based file name index. */
967 enum class file_name_index : unsigned int {};
968
969 struct file_entry
970 {
971 file_entry () = default;
972
973 file_entry (const char *name_, dir_index d_index_,
974 unsigned int mod_time_, unsigned int length_)
975 : name (name_),
976 d_index (d_index_),
977 mod_time (mod_time_),
978 length (length_)
979 {}
980
981 /* Return the include directory at D_INDEX stored in LH. Returns
982 NULL if D_INDEX is out of bounds. */
983 const char *include_dir (const line_header *lh) const;
984
985 /* The file name. Note this is an observing pointer. The memory is
986 owned by debug_line_buffer. */
987 const char *name {};
988
989 /* The directory index (1-based). */
990 dir_index d_index {};
991
992 unsigned int mod_time {};
993
994 unsigned int length {};
995
996 /* True if referenced by the Line Number Program. */
997 bool included_p {};
998
999 /* The associated symbol table, if any. */
1000 struct symtab *symtab {};
1001 };
1002
1003 /* The line number information for a compilation unit (found in the
1004 .debug_line section) begins with a "statement program header",
1005 which contains the following information. */
1006 struct line_header
1007 {
1008 line_header ()
1009 : offset_in_dwz {}
1010 {}
1011
1012 /* Add an entry to the include directory table. */
1013 void add_include_dir (const char *include_dir);
1014
1015 /* Add an entry to the file name table. */
1016 void add_file_name (const char *name, dir_index d_index,
1017 unsigned int mod_time, unsigned int length);
1018
1019 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1020 is out of bounds. */
1021 const char *include_dir_at (dir_index index) const
1022 {
1023 /* Convert directory index number (1-based) to vector index
1024 (0-based). */
1025 size_t vec_index = to_underlying (index) - 1;
1026
1027 if (vec_index >= include_dirs.size ())
1028 return NULL;
1029 return include_dirs[vec_index];
1030 }
1031
1032 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1033 is out of bounds. */
1034 file_entry *file_name_at (file_name_index index)
1035 {
1036 /* Convert file name index number (1-based) to vector index
1037 (0-based). */
1038 size_t vec_index = to_underlying (index) - 1;
1039
1040 if (vec_index >= file_names.size ())
1041 return NULL;
1042 return &file_names[vec_index];
1043 }
1044
1045 /* Const version of the above. */
1046 const file_entry *file_name_at (unsigned int index) const
1047 {
1048 if (index >= file_names.size ())
1049 return NULL;
1050 return &file_names[index];
1051 }
1052
1053 /* Offset of line number information in .debug_line section. */
1054 sect_offset sect_off {};
1055
1056 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1057 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1058
1059 unsigned int total_length {};
1060 unsigned short version {};
1061 unsigned int header_length {};
1062 unsigned char minimum_instruction_length {};
1063 unsigned char maximum_ops_per_instruction {};
1064 unsigned char default_is_stmt {};
1065 int line_base {};
1066 unsigned char line_range {};
1067 unsigned char opcode_base {};
1068
1069 /* standard_opcode_lengths[i] is the number of operands for the
1070 standard opcode whose value is i. This means that
1071 standard_opcode_lengths[0] is unused, and the last meaningful
1072 element is standard_opcode_lengths[opcode_base - 1]. */
1073 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1074
1075 /* The include_directories table. Note these are observing
1076 pointers. The memory is owned by debug_line_buffer. */
1077 std::vector<const char *> include_dirs;
1078
1079 /* The file_names table. */
1080 std::vector<file_entry> file_names;
1081
1082 /* The start and end of the statement program following this
1083 header. These point into dwarf2_per_objfile->line_buffer. */
1084 const gdb_byte *statement_program_start {}, *statement_program_end {};
1085 };
1086
1087 typedef std::unique_ptr<line_header> line_header_up;
1088
1089 const char *
1090 file_entry::include_dir (const line_header *lh) const
1091 {
1092 return lh->include_dir_at (d_index);
1093 }
1094
1095 /* When we construct a partial symbol table entry we only
1096 need this much information. */
1097 struct partial_die_info : public allocate_on_obstack
1098 {
1099 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1100
1101 /* Disable assign but still keep copy ctor, which is needed
1102 load_partial_dies. */
1103 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1104
1105 /* Adjust the partial die before generating a symbol for it. This
1106 function may set the is_external flag or change the DIE's
1107 name. */
1108 void fixup (struct dwarf2_cu *cu);
1109
1110 /* Read a minimal amount of information into the minimal die
1111 structure. */
1112 const gdb_byte *read (const struct die_reader_specs *reader,
1113 const struct abbrev_info &abbrev,
1114 const gdb_byte *info_ptr);
1115
1116 /* Offset of this DIE. */
1117 const sect_offset sect_off;
1118
1119 /* DWARF-2 tag for this DIE. */
1120 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1121
1122 /* Assorted flags describing the data found in this DIE. */
1123 const unsigned int has_children : 1;
1124
1125 unsigned int is_external : 1;
1126 unsigned int is_declaration : 1;
1127 unsigned int has_type : 1;
1128 unsigned int has_specification : 1;
1129 unsigned int has_pc_info : 1;
1130 unsigned int may_be_inlined : 1;
1131
1132 /* This DIE has been marked DW_AT_main_subprogram. */
1133 unsigned int main_subprogram : 1;
1134
1135 /* Flag set if the SCOPE field of this structure has been
1136 computed. */
1137 unsigned int scope_set : 1;
1138
1139 /* Flag set if the DIE has a byte_size attribute. */
1140 unsigned int has_byte_size : 1;
1141
1142 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1143 unsigned int has_const_value : 1;
1144
1145 /* Flag set if any of the DIE's children are template arguments. */
1146 unsigned int has_template_arguments : 1;
1147
1148 /* Flag set if fixup has been called on this die. */
1149 unsigned int fixup_called : 1;
1150
1151 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1152 unsigned int is_dwz : 1;
1153
1154 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1155 unsigned int spec_is_dwz : 1;
1156
1157 /* The name of this DIE. Normally the value of DW_AT_name, but
1158 sometimes a default name for unnamed DIEs. */
1159 const char *name = nullptr;
1160
1161 /* The linkage name, if present. */
1162 const char *linkage_name = nullptr;
1163
1164 /* The scope to prepend to our children. This is generally
1165 allocated on the comp_unit_obstack, so will disappear
1166 when this compilation unit leaves the cache. */
1167 const char *scope = nullptr;
1168
1169 /* Some data associated with the partial DIE. The tag determines
1170 which field is live. */
1171 union
1172 {
1173 /* The location description associated with this DIE, if any. */
1174 struct dwarf_block *locdesc;
1175 /* The offset of an import, for DW_TAG_imported_unit. */
1176 sect_offset sect_off;
1177 } d {};
1178
1179 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1180 CORE_ADDR lowpc = 0;
1181 CORE_ADDR highpc = 0;
1182
1183 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1184 DW_AT_sibling, if any. */
1185 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1186 could return DW_AT_sibling values to its caller load_partial_dies. */
1187 const gdb_byte *sibling = nullptr;
1188
1189 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1190 DW_AT_specification (or DW_AT_abstract_origin or
1191 DW_AT_extension). */
1192 sect_offset spec_offset {};
1193
1194 /* Pointers to this DIE's parent, first child, and next sibling,
1195 if any. */
1196 struct partial_die_info *die_parent = nullptr;
1197 struct partial_die_info *die_child = nullptr;
1198 struct partial_die_info *die_sibling = nullptr;
1199
1200 friend struct partial_die_info *
1201 dwarf2_cu::find_partial_die (sect_offset sect_off);
1202
1203 private:
1204 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1205 partial_die_info (sect_offset sect_off)
1206 : partial_die_info (sect_off, DW_TAG_padding, 0)
1207 {
1208 }
1209
1210 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1211 int has_children_)
1212 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1213 {
1214 is_external = 0;
1215 is_declaration = 0;
1216 has_type = 0;
1217 has_specification = 0;
1218 has_pc_info = 0;
1219 may_be_inlined = 0;
1220 main_subprogram = 0;
1221 scope_set = 0;
1222 has_byte_size = 0;
1223 has_const_value = 0;
1224 has_template_arguments = 0;
1225 fixup_called = 0;
1226 is_dwz = 0;
1227 spec_is_dwz = 0;
1228 }
1229 };
1230
1231 /* This data structure holds the information of an abbrev. */
1232 struct abbrev_info
1233 {
1234 unsigned int number; /* number identifying abbrev */
1235 enum dwarf_tag tag; /* dwarf tag */
1236 unsigned short has_children; /* boolean */
1237 unsigned short num_attrs; /* number of attributes */
1238 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1239 struct abbrev_info *next; /* next in chain */
1240 };
1241
1242 struct attr_abbrev
1243 {
1244 ENUM_BITFIELD(dwarf_attribute) name : 16;
1245 ENUM_BITFIELD(dwarf_form) form : 16;
1246
1247 /* It is valid only if FORM is DW_FORM_implicit_const. */
1248 LONGEST implicit_const;
1249 };
1250
1251 /* Size of abbrev_table.abbrev_hash_table. */
1252 #define ABBREV_HASH_SIZE 121
1253
1254 /* Top level data structure to contain an abbreviation table. */
1255
1256 struct abbrev_table
1257 {
1258 explicit abbrev_table (sect_offset off)
1259 : sect_off (off)
1260 {
1261 m_abbrevs =
1262 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1263 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1264 }
1265
1266 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1267
1268 /* Allocate space for a struct abbrev_info object in
1269 ABBREV_TABLE. */
1270 struct abbrev_info *alloc_abbrev ();
1271
1272 /* Add an abbreviation to the table. */
1273 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1274
1275 /* Look up an abbrev in the table.
1276 Returns NULL if the abbrev is not found. */
1277
1278 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1279
1280
1281 /* Where the abbrev table came from.
1282 This is used as a sanity check when the table is used. */
1283 const sect_offset sect_off;
1284
1285 /* Storage for the abbrev table. */
1286 auto_obstack abbrev_obstack;
1287
1288 private:
1289
1290 /* Hash table of abbrevs.
1291 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1292 It could be statically allocated, but the previous code didn't so we
1293 don't either. */
1294 struct abbrev_info **m_abbrevs;
1295 };
1296
1297 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1298
1299 /* Attributes have a name and a value. */
1300 struct attribute
1301 {
1302 ENUM_BITFIELD(dwarf_attribute) name : 16;
1303 ENUM_BITFIELD(dwarf_form) form : 15;
1304
1305 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1306 field should be in u.str (existing only for DW_STRING) but it is kept
1307 here for better struct attribute alignment. */
1308 unsigned int string_is_canonical : 1;
1309
1310 union
1311 {
1312 const char *str;
1313 struct dwarf_block *blk;
1314 ULONGEST unsnd;
1315 LONGEST snd;
1316 CORE_ADDR addr;
1317 ULONGEST signature;
1318 }
1319 u;
1320 };
1321
1322 /* This data structure holds a complete die structure. */
1323 struct die_info
1324 {
1325 /* DWARF-2 tag for this DIE. */
1326 ENUM_BITFIELD(dwarf_tag) tag : 16;
1327
1328 /* Number of attributes */
1329 unsigned char num_attrs;
1330
1331 /* True if we're presently building the full type name for the
1332 type derived from this DIE. */
1333 unsigned char building_fullname : 1;
1334
1335 /* True if this die is in process. PR 16581. */
1336 unsigned char in_process : 1;
1337
1338 /* Abbrev number */
1339 unsigned int abbrev;
1340
1341 /* Offset in .debug_info or .debug_types section. */
1342 sect_offset sect_off;
1343
1344 /* The dies in a compilation unit form an n-ary tree. PARENT
1345 points to this die's parent; CHILD points to the first child of
1346 this node; and all the children of a given node are chained
1347 together via their SIBLING fields. */
1348 struct die_info *child; /* Its first child, if any. */
1349 struct die_info *sibling; /* Its next sibling, if any. */
1350 struct die_info *parent; /* Its parent, if any. */
1351
1352 /* An array of attributes, with NUM_ATTRS elements. There may be
1353 zero, but it's not common and zero-sized arrays are not
1354 sufficiently portable C. */
1355 struct attribute attrs[1];
1356 };
1357
1358 /* Get at parts of an attribute structure. */
1359
1360 #define DW_STRING(attr) ((attr)->u.str)
1361 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1362 #define DW_UNSND(attr) ((attr)->u.unsnd)
1363 #define DW_BLOCK(attr) ((attr)->u.blk)
1364 #define DW_SND(attr) ((attr)->u.snd)
1365 #define DW_ADDR(attr) ((attr)->u.addr)
1366 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1367
1368 /* Blocks are a bunch of untyped bytes. */
1369 struct dwarf_block
1370 {
1371 size_t size;
1372
1373 /* Valid only if SIZE is not zero. */
1374 const gdb_byte *data;
1375 };
1376
1377 #ifndef ATTR_ALLOC_CHUNK
1378 #define ATTR_ALLOC_CHUNK 4
1379 #endif
1380
1381 /* Allocate fields for structs, unions and enums in this size. */
1382 #ifndef DW_FIELD_ALLOC_CHUNK
1383 #define DW_FIELD_ALLOC_CHUNK 4
1384 #endif
1385
1386 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1387 but this would require a corresponding change in unpack_field_as_long
1388 and friends. */
1389 static int bits_per_byte = 8;
1390
1391 /* When reading a variant or variant part, we track a bit more
1392 information about the field, and store it in an object of this
1393 type. */
1394
1395 struct variant_field
1396 {
1397 /* If we see a DW_TAG_variant, then this will be the discriminant
1398 value. */
1399 ULONGEST discriminant_value;
1400 /* If we see a DW_TAG_variant, then this will be set if this is the
1401 default branch. */
1402 bool default_branch;
1403 /* While reading a DW_TAG_variant_part, this will be set if this
1404 field is the discriminant. */
1405 bool is_discriminant;
1406 };
1407
1408 struct nextfield
1409 {
1410 int accessibility = 0;
1411 int virtuality = 0;
1412 /* Extra information to describe a variant or variant part. */
1413 struct variant_field variant {};
1414 struct field field {};
1415 };
1416
1417 struct fnfieldlist
1418 {
1419 const char *name = nullptr;
1420 std::vector<struct fn_field> fnfields;
1421 };
1422
1423 /* The routines that read and process dies for a C struct or C++ class
1424 pass lists of data member fields and lists of member function fields
1425 in an instance of a field_info structure, as defined below. */
1426 struct field_info
1427 {
1428 /* List of data member and baseclasses fields. */
1429 std::vector<struct nextfield> fields;
1430 std::vector<struct nextfield> baseclasses;
1431
1432 /* Number of fields (including baseclasses). */
1433 int nfields = 0;
1434
1435 /* Set if the accesibility of one of the fields is not public. */
1436 int non_public_fields = 0;
1437
1438 /* Member function fieldlist array, contains name of possibly overloaded
1439 member function, number of overloaded member functions and a pointer
1440 to the head of the member function field chain. */
1441 std::vector<struct fnfieldlist> fnfieldlists;
1442
1443 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1444 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1445 std::vector<struct decl_field> typedef_field_list;
1446
1447 /* Nested types defined by this class and the number of elements in this
1448 list. */
1449 std::vector<struct decl_field> nested_types_list;
1450 };
1451
1452 /* One item on the queue of compilation units to read in full symbols
1453 for. */
1454 struct dwarf2_queue_item
1455 {
1456 struct dwarf2_per_cu_data *per_cu;
1457 enum language pretend_language;
1458 struct dwarf2_queue_item *next;
1459 };
1460
1461 /* The current queue. */
1462 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1463
1464 /* Loaded secondary compilation units are kept in memory until they
1465 have not been referenced for the processing of this many
1466 compilation units. Set this to zero to disable caching. Cache
1467 sizes of up to at least twenty will improve startup time for
1468 typical inter-CU-reference binaries, at an obvious memory cost. */
1469 static int dwarf_max_cache_age = 5;
1470 static void
1471 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1472 struct cmd_list_element *c, const char *value)
1473 {
1474 fprintf_filtered (file, _("The upper bound on the age of cached "
1475 "DWARF compilation units is %s.\n"),
1476 value);
1477 }
1478 \f
1479 /* local function prototypes */
1480
1481 static const char *get_section_name (const struct dwarf2_section_info *);
1482
1483 static const char *get_section_file_name (const struct dwarf2_section_info *);
1484
1485 static void dwarf2_find_base_address (struct die_info *die,
1486 struct dwarf2_cu *cu);
1487
1488 static struct partial_symtab *create_partial_symtab
1489 (struct dwarf2_per_cu_data *per_cu, const char *name);
1490
1491 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1492 const gdb_byte *info_ptr,
1493 struct die_info *type_unit_die,
1494 int has_children, void *data);
1495
1496 static void dwarf2_build_psymtabs_hard
1497 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1498
1499 static void scan_partial_symbols (struct partial_die_info *,
1500 CORE_ADDR *, CORE_ADDR *,
1501 int, struct dwarf2_cu *);
1502
1503 static void add_partial_symbol (struct partial_die_info *,
1504 struct dwarf2_cu *);
1505
1506 static void add_partial_namespace (struct partial_die_info *pdi,
1507 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1508 int set_addrmap, struct dwarf2_cu *cu);
1509
1510 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1511 CORE_ADDR *highpc, int set_addrmap,
1512 struct dwarf2_cu *cu);
1513
1514 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1515 struct dwarf2_cu *cu);
1516
1517 static void add_partial_subprogram (struct partial_die_info *pdi,
1518 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1519 int need_pc, struct dwarf2_cu *cu);
1520
1521 static void dwarf2_read_symtab (struct partial_symtab *,
1522 struct objfile *);
1523
1524 static void psymtab_to_symtab_1 (struct partial_symtab *);
1525
1526 static abbrev_table_up abbrev_table_read_table
1527 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1528 sect_offset);
1529
1530 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1531
1532 static struct partial_die_info *load_partial_dies
1533 (const struct die_reader_specs *, const gdb_byte *, int);
1534
1535 static struct partial_die_info *find_partial_die (sect_offset, int,
1536 struct dwarf2_cu *);
1537
1538 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1539 struct attribute *, struct attr_abbrev *,
1540 const gdb_byte *);
1541
1542 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1543
1544 static int read_1_signed_byte (bfd *, const gdb_byte *);
1545
1546 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1547
1548 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1549
1550 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1551
1552 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1553 unsigned int *);
1554
1555 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1556
1557 static LONGEST read_checked_initial_length_and_offset
1558 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1559 unsigned int *, unsigned int *);
1560
1561 static LONGEST read_offset (bfd *, const gdb_byte *,
1562 const struct comp_unit_head *,
1563 unsigned int *);
1564
1565 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1566
1567 static sect_offset read_abbrev_offset
1568 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1569 struct dwarf2_section_info *, sect_offset);
1570
1571 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1572
1573 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1574
1575 static const char *read_indirect_string
1576 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1577 const struct comp_unit_head *, unsigned int *);
1578
1579 static const char *read_indirect_line_string
1580 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1581 const struct comp_unit_head *, unsigned int *);
1582
1583 static const char *read_indirect_string_at_offset
1584 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1585 LONGEST str_offset);
1586
1587 static const char *read_indirect_string_from_dwz
1588 (struct objfile *objfile, struct dwz_file *, LONGEST);
1589
1590 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1591
1592 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1593 const gdb_byte *,
1594 unsigned int *);
1595
1596 static const char *read_str_index (const struct die_reader_specs *reader,
1597 ULONGEST str_index);
1598
1599 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1600
1601 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1602 struct dwarf2_cu *);
1603
1604 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1605 unsigned int);
1606
1607 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1608 struct dwarf2_cu *cu);
1609
1610 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1611 struct dwarf2_cu *cu);
1612
1613 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1614
1615 static struct die_info *die_specification (struct die_info *die,
1616 struct dwarf2_cu **);
1617
1618 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1619 struct dwarf2_cu *cu);
1620
1621 static void dwarf_decode_lines (struct line_header *, const char *,
1622 struct dwarf2_cu *, struct partial_symtab *,
1623 CORE_ADDR, int decode_mapping);
1624
1625 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1626 const char *);
1627
1628 static struct symbol *new_symbol (struct die_info *, struct type *,
1629 struct dwarf2_cu *, struct symbol * = NULL);
1630
1631 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1632 struct dwarf2_cu *);
1633
1634 static void dwarf2_const_value_attr (const struct attribute *attr,
1635 struct type *type,
1636 const char *name,
1637 struct obstack *obstack,
1638 struct dwarf2_cu *cu, LONGEST *value,
1639 const gdb_byte **bytes,
1640 struct dwarf2_locexpr_baton **baton);
1641
1642 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1643
1644 static int need_gnat_info (struct dwarf2_cu *);
1645
1646 static struct type *die_descriptive_type (struct die_info *,
1647 struct dwarf2_cu *);
1648
1649 static void set_descriptive_type (struct type *, struct die_info *,
1650 struct dwarf2_cu *);
1651
1652 static struct type *die_containing_type (struct die_info *,
1653 struct dwarf2_cu *);
1654
1655 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1656 struct dwarf2_cu *);
1657
1658 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1659
1660 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1661
1662 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1663
1664 static char *typename_concat (struct obstack *obs, const char *prefix,
1665 const char *suffix, int physname,
1666 struct dwarf2_cu *cu);
1667
1668 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1669
1670 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1671
1672 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1673
1674 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1675
1676 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1677
1678 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1679
1680 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1681 struct dwarf2_cu *, struct partial_symtab *);
1682
1683 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1684 values. Keep the items ordered with increasing constraints compliance. */
1685 enum pc_bounds_kind
1686 {
1687 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1688 PC_BOUNDS_NOT_PRESENT,
1689
1690 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1691 were present but they do not form a valid range of PC addresses. */
1692 PC_BOUNDS_INVALID,
1693
1694 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1695 PC_BOUNDS_RANGES,
1696
1697 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1698 PC_BOUNDS_HIGH_LOW,
1699 };
1700
1701 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1702 CORE_ADDR *, CORE_ADDR *,
1703 struct dwarf2_cu *,
1704 struct partial_symtab *);
1705
1706 static void get_scope_pc_bounds (struct die_info *,
1707 CORE_ADDR *, CORE_ADDR *,
1708 struct dwarf2_cu *);
1709
1710 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1711 CORE_ADDR, struct dwarf2_cu *);
1712
1713 static void dwarf2_add_field (struct field_info *, struct die_info *,
1714 struct dwarf2_cu *);
1715
1716 static void dwarf2_attach_fields_to_type (struct field_info *,
1717 struct type *, struct dwarf2_cu *);
1718
1719 static void dwarf2_add_member_fn (struct field_info *,
1720 struct die_info *, struct type *,
1721 struct dwarf2_cu *);
1722
1723 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1724 struct type *,
1725 struct dwarf2_cu *);
1726
1727 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1728
1729 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1730
1731 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1732
1733 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1734
1735 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1736
1737 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1738
1739 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1740
1741 static struct type *read_module_type (struct die_info *die,
1742 struct dwarf2_cu *cu);
1743
1744 static const char *namespace_name (struct die_info *die,
1745 int *is_anonymous, struct dwarf2_cu *);
1746
1747 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1748
1749 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1750
1751 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1752 struct dwarf2_cu *);
1753
1754 static struct die_info *read_die_and_siblings_1
1755 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1756 struct die_info *);
1757
1758 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1759 const gdb_byte *info_ptr,
1760 const gdb_byte **new_info_ptr,
1761 struct die_info *parent);
1762
1763 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1764 struct die_info **, const gdb_byte *,
1765 int *, int);
1766
1767 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1768 struct die_info **, const gdb_byte *,
1769 int *);
1770
1771 static void process_die (struct die_info *, struct dwarf2_cu *);
1772
1773 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1774 struct obstack *);
1775
1776 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1777
1778 static const char *dwarf2_full_name (const char *name,
1779 struct die_info *die,
1780 struct dwarf2_cu *cu);
1781
1782 static const char *dwarf2_physname (const char *name, struct die_info *die,
1783 struct dwarf2_cu *cu);
1784
1785 static struct die_info *dwarf2_extension (struct die_info *die,
1786 struct dwarf2_cu **);
1787
1788 static const char *dwarf_tag_name (unsigned int);
1789
1790 static const char *dwarf_attr_name (unsigned int);
1791
1792 static const char *dwarf_form_name (unsigned int);
1793
1794 static const char *dwarf_bool_name (unsigned int);
1795
1796 static const char *dwarf_type_encoding_name (unsigned int);
1797
1798 static struct die_info *sibling_die (struct die_info *);
1799
1800 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1801
1802 static void dump_die_for_error (struct die_info *);
1803
1804 static void dump_die_1 (struct ui_file *, int level, int max_level,
1805 struct die_info *);
1806
1807 /*static*/ void dump_die (struct die_info *, int max_level);
1808
1809 static void store_in_ref_table (struct die_info *,
1810 struct dwarf2_cu *);
1811
1812 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1813
1814 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1815
1816 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1817 const struct attribute *,
1818 struct dwarf2_cu **);
1819
1820 static struct die_info *follow_die_ref (struct die_info *,
1821 const struct attribute *,
1822 struct dwarf2_cu **);
1823
1824 static struct die_info *follow_die_sig (struct die_info *,
1825 const struct attribute *,
1826 struct dwarf2_cu **);
1827
1828 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1829 struct dwarf2_cu *);
1830
1831 static struct type *get_DW_AT_signature_type (struct die_info *,
1832 const struct attribute *,
1833 struct dwarf2_cu *);
1834
1835 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1836
1837 static void read_signatured_type (struct signatured_type *);
1838
1839 static int attr_to_dynamic_prop (const struct attribute *attr,
1840 struct die_info *die, struct dwarf2_cu *cu,
1841 struct dynamic_prop *prop);
1842
1843 /* memory allocation interface */
1844
1845 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1846
1847 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1848
1849 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1850
1851 static int attr_form_is_block (const struct attribute *);
1852
1853 static int attr_form_is_section_offset (const struct attribute *);
1854
1855 static int attr_form_is_constant (const struct attribute *);
1856
1857 static int attr_form_is_ref (const struct attribute *);
1858
1859 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1860 struct dwarf2_loclist_baton *baton,
1861 const struct attribute *attr);
1862
1863 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1864 struct symbol *sym,
1865 struct dwarf2_cu *cu,
1866 int is_block);
1867
1868 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1869 const gdb_byte *info_ptr,
1870 struct abbrev_info *abbrev);
1871
1872 static hashval_t partial_die_hash (const void *item);
1873
1874 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1875
1876 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1877 (sect_offset sect_off, unsigned int offset_in_dwz,
1878 struct dwarf2_per_objfile *dwarf2_per_objfile);
1879
1880 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1881 struct die_info *comp_unit_die,
1882 enum language pretend_language);
1883
1884 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1885
1886 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1887
1888 static struct type *set_die_type (struct die_info *, struct type *,
1889 struct dwarf2_cu *);
1890
1891 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1892
1893 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1894
1895 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1896 enum language);
1897
1898 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1899 enum language);
1900
1901 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1902 enum language);
1903
1904 static void dwarf2_add_dependence (struct dwarf2_cu *,
1905 struct dwarf2_per_cu_data *);
1906
1907 static void dwarf2_mark (struct dwarf2_cu *);
1908
1909 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1910
1911 static struct type *get_die_type_at_offset (sect_offset,
1912 struct dwarf2_per_cu_data *);
1913
1914 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1915
1916 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1917 enum language pretend_language);
1918
1919 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1920
1921 /* Class, the destructor of which frees all allocated queue entries. This
1922 will only have work to do if an error was thrown while processing the
1923 dwarf. If no error was thrown then the queue entries should have all
1924 been processed, and freed, as we went along. */
1925
1926 class dwarf2_queue_guard
1927 {
1928 public:
1929 dwarf2_queue_guard () = default;
1930
1931 /* Free any entries remaining on the queue. There should only be
1932 entries left if we hit an error while processing the dwarf. */
1933 ~dwarf2_queue_guard ()
1934 {
1935 struct dwarf2_queue_item *item, *last;
1936
1937 item = dwarf2_queue;
1938 while (item)
1939 {
1940 /* Anything still marked queued is likely to be in an
1941 inconsistent state, so discard it. */
1942 if (item->per_cu->queued)
1943 {
1944 if (item->per_cu->cu != NULL)
1945 free_one_cached_comp_unit (item->per_cu);
1946 item->per_cu->queued = 0;
1947 }
1948
1949 last = item;
1950 item = item->next;
1951 xfree (last);
1952 }
1953
1954 dwarf2_queue = dwarf2_queue_tail = NULL;
1955 }
1956 };
1957
1958 /* The return type of find_file_and_directory. Note, the enclosed
1959 string pointers are only valid while this object is valid. */
1960
1961 struct file_and_directory
1962 {
1963 /* The filename. This is never NULL. */
1964 const char *name;
1965
1966 /* The compilation directory. NULL if not known. If we needed to
1967 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1968 points directly to the DW_AT_comp_dir string attribute owned by
1969 the obstack that owns the DIE. */
1970 const char *comp_dir;
1971
1972 /* If we needed to build a new string for comp_dir, this is what
1973 owns the storage. */
1974 std::string comp_dir_storage;
1975 };
1976
1977 static file_and_directory find_file_and_directory (struct die_info *die,
1978 struct dwarf2_cu *cu);
1979
1980 static char *file_full_name (int file, struct line_header *lh,
1981 const char *comp_dir);
1982
1983 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1984 enum class rcuh_kind { COMPILE, TYPE };
1985
1986 static const gdb_byte *read_and_check_comp_unit_head
1987 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1988 struct comp_unit_head *header,
1989 struct dwarf2_section_info *section,
1990 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1991 rcuh_kind section_kind);
1992
1993 static void init_cutu_and_read_dies
1994 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1995 int use_existing_cu, int keep, bool skip_partial,
1996 die_reader_func_ftype *die_reader_func, void *data);
1997
1998 static void init_cutu_and_read_dies_simple
1999 (struct dwarf2_per_cu_data *this_cu,
2000 die_reader_func_ftype *die_reader_func, void *data);
2001
2002 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2003
2004 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2005
2006 static struct dwo_unit *lookup_dwo_unit_in_dwp
2007 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2008 struct dwp_file *dwp_file, const char *comp_dir,
2009 ULONGEST signature, int is_debug_types);
2010
2011 static struct dwp_file *get_dwp_file
2012 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2013
2014 static struct dwo_unit *lookup_dwo_comp_unit
2015 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2016
2017 static struct dwo_unit *lookup_dwo_type_unit
2018 (struct signatured_type *, const char *, const char *);
2019
2020 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2021
2022 static void free_dwo_file (struct dwo_file *);
2023
2024 /* A unique_ptr helper to free a dwo_file. */
2025
2026 struct dwo_file_deleter
2027 {
2028 void operator() (struct dwo_file *df) const
2029 {
2030 free_dwo_file (df);
2031 }
2032 };
2033
2034 /* A unique pointer to a dwo_file. */
2035
2036 typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
2037
2038 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2039
2040 static void check_producer (struct dwarf2_cu *cu);
2041
2042 static void free_line_header_voidp (void *arg);
2043 \f
2044 /* Various complaints about symbol reading that don't abort the process. */
2045
2046 static void
2047 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2048 {
2049 complaint (_("statement list doesn't fit in .debug_line section"));
2050 }
2051
2052 static void
2053 dwarf2_debug_line_missing_file_complaint (void)
2054 {
2055 complaint (_(".debug_line section has line data without a file"));
2056 }
2057
2058 static void
2059 dwarf2_debug_line_missing_end_sequence_complaint (void)
2060 {
2061 complaint (_(".debug_line section has line "
2062 "program sequence without an end"));
2063 }
2064
2065 static void
2066 dwarf2_complex_location_expr_complaint (void)
2067 {
2068 complaint (_("location expression too complex"));
2069 }
2070
2071 static void
2072 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2073 int arg3)
2074 {
2075 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2076 arg1, arg2, arg3);
2077 }
2078
2079 static void
2080 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2081 {
2082 complaint (_("debug info runs off end of %s section"
2083 " [in module %s]"),
2084 get_section_name (section),
2085 get_section_file_name (section));
2086 }
2087
2088 static void
2089 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2090 {
2091 complaint (_("macro debug info contains a "
2092 "malformed macro definition:\n`%s'"),
2093 arg1);
2094 }
2095
2096 static void
2097 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2098 {
2099 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2100 arg1, arg2);
2101 }
2102
2103 /* Hash function for line_header_hash. */
2104
2105 static hashval_t
2106 line_header_hash (const struct line_header *ofs)
2107 {
2108 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2109 }
2110
2111 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2112
2113 static hashval_t
2114 line_header_hash_voidp (const void *item)
2115 {
2116 const struct line_header *ofs = (const struct line_header *) item;
2117
2118 return line_header_hash (ofs);
2119 }
2120
2121 /* Equality function for line_header_hash. */
2122
2123 static int
2124 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2125 {
2126 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2127 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2128
2129 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2130 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2131 }
2132
2133 \f
2134
2135 /* Read the given attribute value as an address, taking the attribute's
2136 form into account. */
2137
2138 static CORE_ADDR
2139 attr_value_as_address (struct attribute *attr)
2140 {
2141 CORE_ADDR addr;
2142
2143 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2144 {
2145 /* Aside from a few clearly defined exceptions, attributes that
2146 contain an address must always be in DW_FORM_addr form.
2147 Unfortunately, some compilers happen to be violating this
2148 requirement by encoding addresses using other forms, such
2149 as DW_FORM_data4 for example. For those broken compilers,
2150 we try to do our best, without any guarantee of success,
2151 to interpret the address correctly. It would also be nice
2152 to generate a complaint, but that would require us to maintain
2153 a list of legitimate cases where a non-address form is allowed,
2154 as well as update callers to pass in at least the CU's DWARF
2155 version. This is more overhead than what we're willing to
2156 expand for a pretty rare case. */
2157 addr = DW_UNSND (attr);
2158 }
2159 else
2160 addr = DW_ADDR (attr);
2161
2162 return addr;
2163 }
2164
2165 /* See declaration. */
2166
2167 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2168 const dwarf2_debug_sections *names)
2169 : objfile (objfile_)
2170 {
2171 if (names == NULL)
2172 names = &dwarf2_elf_names;
2173
2174 bfd *obfd = objfile->obfd;
2175
2176 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2177 locate_sections (obfd, sec, *names);
2178 }
2179
2180 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2181
2182 dwarf2_per_objfile::~dwarf2_per_objfile ()
2183 {
2184 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2185 free_cached_comp_units ();
2186
2187 if (quick_file_names_table)
2188 htab_delete (quick_file_names_table);
2189
2190 if (line_header_hash)
2191 htab_delete (line_header_hash);
2192
2193 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2194 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
2195
2196 for (signatured_type *sig_type : all_type_units)
2197 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
2198
2199 VEC_free (dwarf2_section_info_def, types);
2200
2201 if (dwo_files != NULL)
2202 free_dwo_files (dwo_files, objfile);
2203
2204 /* Everything else should be on the objfile obstack. */
2205 }
2206
2207 /* See declaration. */
2208
2209 void
2210 dwarf2_per_objfile::free_cached_comp_units ()
2211 {
2212 dwarf2_per_cu_data *per_cu = read_in_chain;
2213 dwarf2_per_cu_data **last_chain = &read_in_chain;
2214 while (per_cu != NULL)
2215 {
2216 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2217
2218 delete per_cu->cu;
2219 *last_chain = next_cu;
2220 per_cu = next_cu;
2221 }
2222 }
2223
2224 /* A helper class that calls free_cached_comp_units on
2225 destruction. */
2226
2227 class free_cached_comp_units
2228 {
2229 public:
2230
2231 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2232 : m_per_objfile (per_objfile)
2233 {
2234 }
2235
2236 ~free_cached_comp_units ()
2237 {
2238 m_per_objfile->free_cached_comp_units ();
2239 }
2240
2241 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2242
2243 private:
2244
2245 dwarf2_per_objfile *m_per_objfile;
2246 };
2247
2248 /* Try to locate the sections we need for DWARF 2 debugging
2249 information and return true if we have enough to do something.
2250 NAMES points to the dwarf2 section names, or is NULL if the standard
2251 ELF names are used. */
2252
2253 int
2254 dwarf2_has_info (struct objfile *objfile,
2255 const struct dwarf2_debug_sections *names)
2256 {
2257 if (objfile->flags & OBJF_READNEVER)
2258 return 0;
2259
2260 struct dwarf2_per_objfile *dwarf2_per_objfile
2261 = get_dwarf2_per_objfile (objfile);
2262
2263 if (dwarf2_per_objfile == NULL)
2264 {
2265 /* Initialize per-objfile state. */
2266 dwarf2_per_objfile
2267 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2268 names);
2269 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2270 }
2271 return (!dwarf2_per_objfile->info.is_virtual
2272 && dwarf2_per_objfile->info.s.section != NULL
2273 && !dwarf2_per_objfile->abbrev.is_virtual
2274 && dwarf2_per_objfile->abbrev.s.section != NULL);
2275 }
2276
2277 /* Return the containing section of virtual section SECTION. */
2278
2279 static struct dwarf2_section_info *
2280 get_containing_section (const struct dwarf2_section_info *section)
2281 {
2282 gdb_assert (section->is_virtual);
2283 return section->s.containing_section;
2284 }
2285
2286 /* Return the bfd owner of SECTION. */
2287
2288 static struct bfd *
2289 get_section_bfd_owner (const struct dwarf2_section_info *section)
2290 {
2291 if (section->is_virtual)
2292 {
2293 section = get_containing_section (section);
2294 gdb_assert (!section->is_virtual);
2295 }
2296 return section->s.section->owner;
2297 }
2298
2299 /* Return the bfd section of SECTION.
2300 Returns NULL if the section is not present. */
2301
2302 static asection *
2303 get_section_bfd_section (const struct dwarf2_section_info *section)
2304 {
2305 if (section->is_virtual)
2306 {
2307 section = get_containing_section (section);
2308 gdb_assert (!section->is_virtual);
2309 }
2310 return section->s.section;
2311 }
2312
2313 /* Return the name of SECTION. */
2314
2315 static const char *
2316 get_section_name (const struct dwarf2_section_info *section)
2317 {
2318 asection *sectp = get_section_bfd_section (section);
2319
2320 gdb_assert (sectp != NULL);
2321 return bfd_section_name (get_section_bfd_owner (section), sectp);
2322 }
2323
2324 /* Return the name of the file SECTION is in. */
2325
2326 static const char *
2327 get_section_file_name (const struct dwarf2_section_info *section)
2328 {
2329 bfd *abfd = get_section_bfd_owner (section);
2330
2331 return bfd_get_filename (abfd);
2332 }
2333
2334 /* Return the id of SECTION.
2335 Returns 0 if SECTION doesn't exist. */
2336
2337 static int
2338 get_section_id (const struct dwarf2_section_info *section)
2339 {
2340 asection *sectp = get_section_bfd_section (section);
2341
2342 if (sectp == NULL)
2343 return 0;
2344 return sectp->id;
2345 }
2346
2347 /* Return the flags of SECTION.
2348 SECTION (or containing section if this is a virtual section) must exist. */
2349
2350 static int
2351 get_section_flags (const struct dwarf2_section_info *section)
2352 {
2353 asection *sectp = get_section_bfd_section (section);
2354
2355 gdb_assert (sectp != NULL);
2356 return bfd_get_section_flags (sectp->owner, sectp);
2357 }
2358
2359 /* When loading sections, we look either for uncompressed section or for
2360 compressed section names. */
2361
2362 static int
2363 section_is_p (const char *section_name,
2364 const struct dwarf2_section_names *names)
2365 {
2366 if (names->normal != NULL
2367 && strcmp (section_name, names->normal) == 0)
2368 return 1;
2369 if (names->compressed != NULL
2370 && strcmp (section_name, names->compressed) == 0)
2371 return 1;
2372 return 0;
2373 }
2374
2375 /* See declaration. */
2376
2377 void
2378 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2379 const dwarf2_debug_sections &names)
2380 {
2381 flagword aflag = bfd_get_section_flags (abfd, sectp);
2382
2383 if ((aflag & SEC_HAS_CONTENTS) == 0)
2384 {
2385 }
2386 else if (section_is_p (sectp->name, &names.info))
2387 {
2388 this->info.s.section = sectp;
2389 this->info.size = bfd_get_section_size (sectp);
2390 }
2391 else if (section_is_p (sectp->name, &names.abbrev))
2392 {
2393 this->abbrev.s.section = sectp;
2394 this->abbrev.size = bfd_get_section_size (sectp);
2395 }
2396 else if (section_is_p (sectp->name, &names.line))
2397 {
2398 this->line.s.section = sectp;
2399 this->line.size = bfd_get_section_size (sectp);
2400 }
2401 else if (section_is_p (sectp->name, &names.loc))
2402 {
2403 this->loc.s.section = sectp;
2404 this->loc.size = bfd_get_section_size (sectp);
2405 }
2406 else if (section_is_p (sectp->name, &names.loclists))
2407 {
2408 this->loclists.s.section = sectp;
2409 this->loclists.size = bfd_get_section_size (sectp);
2410 }
2411 else if (section_is_p (sectp->name, &names.macinfo))
2412 {
2413 this->macinfo.s.section = sectp;
2414 this->macinfo.size = bfd_get_section_size (sectp);
2415 }
2416 else if (section_is_p (sectp->name, &names.macro))
2417 {
2418 this->macro.s.section = sectp;
2419 this->macro.size = bfd_get_section_size (sectp);
2420 }
2421 else if (section_is_p (sectp->name, &names.str))
2422 {
2423 this->str.s.section = sectp;
2424 this->str.size = bfd_get_section_size (sectp);
2425 }
2426 else if (section_is_p (sectp->name, &names.line_str))
2427 {
2428 this->line_str.s.section = sectp;
2429 this->line_str.size = bfd_get_section_size (sectp);
2430 }
2431 else if (section_is_p (sectp->name, &names.addr))
2432 {
2433 this->addr.s.section = sectp;
2434 this->addr.size = bfd_get_section_size (sectp);
2435 }
2436 else if (section_is_p (sectp->name, &names.frame))
2437 {
2438 this->frame.s.section = sectp;
2439 this->frame.size = bfd_get_section_size (sectp);
2440 }
2441 else if (section_is_p (sectp->name, &names.eh_frame))
2442 {
2443 this->eh_frame.s.section = sectp;
2444 this->eh_frame.size = bfd_get_section_size (sectp);
2445 }
2446 else if (section_is_p (sectp->name, &names.ranges))
2447 {
2448 this->ranges.s.section = sectp;
2449 this->ranges.size = bfd_get_section_size (sectp);
2450 }
2451 else if (section_is_p (sectp->name, &names.rnglists))
2452 {
2453 this->rnglists.s.section = sectp;
2454 this->rnglists.size = bfd_get_section_size (sectp);
2455 }
2456 else if (section_is_p (sectp->name, &names.types))
2457 {
2458 struct dwarf2_section_info type_section;
2459
2460 memset (&type_section, 0, sizeof (type_section));
2461 type_section.s.section = sectp;
2462 type_section.size = bfd_get_section_size (sectp);
2463
2464 VEC_safe_push (dwarf2_section_info_def, this->types,
2465 &type_section);
2466 }
2467 else if (section_is_p (sectp->name, &names.gdb_index))
2468 {
2469 this->gdb_index.s.section = sectp;
2470 this->gdb_index.size = bfd_get_section_size (sectp);
2471 }
2472 else if (section_is_p (sectp->name, &names.debug_names))
2473 {
2474 this->debug_names.s.section = sectp;
2475 this->debug_names.size = bfd_get_section_size (sectp);
2476 }
2477 else if (section_is_p (sectp->name, &names.debug_aranges))
2478 {
2479 this->debug_aranges.s.section = sectp;
2480 this->debug_aranges.size = bfd_get_section_size (sectp);
2481 }
2482
2483 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2484 && bfd_section_vma (abfd, sectp) == 0)
2485 this->has_section_at_zero = true;
2486 }
2487
2488 /* A helper function that decides whether a section is empty,
2489 or not present. */
2490
2491 static int
2492 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2493 {
2494 if (section->is_virtual)
2495 return section->size == 0;
2496 return section->s.section == NULL || section->size == 0;
2497 }
2498
2499 /* See dwarf2read.h. */
2500
2501 void
2502 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2503 {
2504 asection *sectp;
2505 bfd *abfd;
2506 gdb_byte *buf, *retbuf;
2507
2508 if (info->readin)
2509 return;
2510 info->buffer = NULL;
2511 info->readin = 1;
2512
2513 if (dwarf2_section_empty_p (info))
2514 return;
2515
2516 sectp = get_section_bfd_section (info);
2517
2518 /* If this is a virtual section we need to read in the real one first. */
2519 if (info->is_virtual)
2520 {
2521 struct dwarf2_section_info *containing_section =
2522 get_containing_section (info);
2523
2524 gdb_assert (sectp != NULL);
2525 if ((sectp->flags & SEC_RELOC) != 0)
2526 {
2527 error (_("Dwarf Error: DWP format V2 with relocations is not"
2528 " supported in section %s [in module %s]"),
2529 get_section_name (info), get_section_file_name (info));
2530 }
2531 dwarf2_read_section (objfile, containing_section);
2532 /* Other code should have already caught virtual sections that don't
2533 fit. */
2534 gdb_assert (info->virtual_offset + info->size
2535 <= containing_section->size);
2536 /* If the real section is empty or there was a problem reading the
2537 section we shouldn't get here. */
2538 gdb_assert (containing_section->buffer != NULL);
2539 info->buffer = containing_section->buffer + info->virtual_offset;
2540 return;
2541 }
2542
2543 /* If the section has relocations, we must read it ourselves.
2544 Otherwise we attach it to the BFD. */
2545 if ((sectp->flags & SEC_RELOC) == 0)
2546 {
2547 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2548 return;
2549 }
2550
2551 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2552 info->buffer = buf;
2553
2554 /* When debugging .o files, we may need to apply relocations; see
2555 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2556 We never compress sections in .o files, so we only need to
2557 try this when the section is not compressed. */
2558 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2559 if (retbuf != NULL)
2560 {
2561 info->buffer = retbuf;
2562 return;
2563 }
2564
2565 abfd = get_section_bfd_owner (info);
2566 gdb_assert (abfd != NULL);
2567
2568 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2569 || bfd_bread (buf, info->size, abfd) != info->size)
2570 {
2571 error (_("Dwarf Error: Can't read DWARF data"
2572 " in section %s [in module %s]"),
2573 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2574 }
2575 }
2576
2577 /* A helper function that returns the size of a section in a safe way.
2578 If you are positive that the section has been read before using the
2579 size, then it is safe to refer to the dwarf2_section_info object's
2580 "size" field directly. In other cases, you must call this
2581 function, because for compressed sections the size field is not set
2582 correctly until the section has been read. */
2583
2584 static bfd_size_type
2585 dwarf2_section_size (struct objfile *objfile,
2586 struct dwarf2_section_info *info)
2587 {
2588 if (!info->readin)
2589 dwarf2_read_section (objfile, info);
2590 return info->size;
2591 }
2592
2593 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2594 SECTION_NAME. */
2595
2596 void
2597 dwarf2_get_section_info (struct objfile *objfile,
2598 enum dwarf2_section_enum sect,
2599 asection **sectp, const gdb_byte **bufp,
2600 bfd_size_type *sizep)
2601 {
2602 struct dwarf2_per_objfile *data
2603 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2604 dwarf2_objfile_data_key);
2605 struct dwarf2_section_info *info;
2606
2607 /* We may see an objfile without any DWARF, in which case we just
2608 return nothing. */
2609 if (data == NULL)
2610 {
2611 *sectp = NULL;
2612 *bufp = NULL;
2613 *sizep = 0;
2614 return;
2615 }
2616 switch (sect)
2617 {
2618 case DWARF2_DEBUG_FRAME:
2619 info = &data->frame;
2620 break;
2621 case DWARF2_EH_FRAME:
2622 info = &data->eh_frame;
2623 break;
2624 default:
2625 gdb_assert_not_reached ("unexpected section");
2626 }
2627
2628 dwarf2_read_section (objfile, info);
2629
2630 *sectp = get_section_bfd_section (info);
2631 *bufp = info->buffer;
2632 *sizep = info->size;
2633 }
2634
2635 /* A helper function to find the sections for a .dwz file. */
2636
2637 static void
2638 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2639 {
2640 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2641
2642 /* Note that we only support the standard ELF names, because .dwz
2643 is ELF-only (at the time of writing). */
2644 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2645 {
2646 dwz_file->abbrev.s.section = sectp;
2647 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2648 }
2649 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2650 {
2651 dwz_file->info.s.section = sectp;
2652 dwz_file->info.size = bfd_get_section_size (sectp);
2653 }
2654 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2655 {
2656 dwz_file->str.s.section = sectp;
2657 dwz_file->str.size = bfd_get_section_size (sectp);
2658 }
2659 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2660 {
2661 dwz_file->line.s.section = sectp;
2662 dwz_file->line.size = bfd_get_section_size (sectp);
2663 }
2664 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2665 {
2666 dwz_file->macro.s.section = sectp;
2667 dwz_file->macro.size = bfd_get_section_size (sectp);
2668 }
2669 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2670 {
2671 dwz_file->gdb_index.s.section = sectp;
2672 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2673 }
2674 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2675 {
2676 dwz_file->debug_names.s.section = sectp;
2677 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2678 }
2679 }
2680
2681 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2682 there is no .gnu_debugaltlink section in the file. Error if there
2683 is such a section but the file cannot be found. */
2684
2685 static struct dwz_file *
2686 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2687 {
2688 const char *filename;
2689 bfd_size_type buildid_len_arg;
2690 size_t buildid_len;
2691 bfd_byte *buildid;
2692
2693 if (dwarf2_per_objfile->dwz_file != NULL)
2694 return dwarf2_per_objfile->dwz_file.get ();
2695
2696 bfd_set_error (bfd_error_no_error);
2697 gdb::unique_xmalloc_ptr<char> data
2698 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2699 &buildid_len_arg, &buildid));
2700 if (data == NULL)
2701 {
2702 if (bfd_get_error () == bfd_error_no_error)
2703 return NULL;
2704 error (_("could not read '.gnu_debugaltlink' section: %s"),
2705 bfd_errmsg (bfd_get_error ()));
2706 }
2707
2708 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2709
2710 buildid_len = (size_t) buildid_len_arg;
2711
2712 filename = data.get ();
2713
2714 std::string abs_storage;
2715 if (!IS_ABSOLUTE_PATH (filename))
2716 {
2717 gdb::unique_xmalloc_ptr<char> abs
2718 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2719
2720 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2721 filename = abs_storage.c_str ();
2722 }
2723
2724 /* First try the file name given in the section. If that doesn't
2725 work, try to use the build-id instead. */
2726 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2727 if (dwz_bfd != NULL)
2728 {
2729 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2730 dwz_bfd.reset (nullptr);
2731 }
2732
2733 if (dwz_bfd == NULL)
2734 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2735
2736 if (dwz_bfd == NULL)
2737 error (_("could not find '.gnu_debugaltlink' file for %s"),
2738 objfile_name (dwarf2_per_objfile->objfile));
2739
2740 std::unique_ptr<struct dwz_file> result
2741 (new struct dwz_file (std::move (dwz_bfd)));
2742
2743 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2744 result.get ());
2745
2746 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2747 result->dwz_bfd.get ());
2748 dwarf2_per_objfile->dwz_file = std::move (result);
2749 return dwarf2_per_objfile->dwz_file.get ();
2750 }
2751 \f
2752 /* DWARF quick_symbols_functions support. */
2753
2754 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2755 unique line tables, so we maintain a separate table of all .debug_line
2756 derived entries to support the sharing.
2757 All the quick functions need is the list of file names. We discard the
2758 line_header when we're done and don't need to record it here. */
2759 struct quick_file_names
2760 {
2761 /* The data used to construct the hash key. */
2762 struct stmt_list_hash hash;
2763
2764 /* The number of entries in file_names, real_names. */
2765 unsigned int num_file_names;
2766
2767 /* The file names from the line table, after being run through
2768 file_full_name. */
2769 const char **file_names;
2770
2771 /* The file names from the line table after being run through
2772 gdb_realpath. These are computed lazily. */
2773 const char **real_names;
2774 };
2775
2776 /* When using the index (and thus not using psymtabs), each CU has an
2777 object of this type. This is used to hold information needed by
2778 the various "quick" methods. */
2779 struct dwarf2_per_cu_quick_data
2780 {
2781 /* The file table. This can be NULL if there was no file table
2782 or it's currently not read in.
2783 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2784 struct quick_file_names *file_names;
2785
2786 /* The corresponding symbol table. This is NULL if symbols for this
2787 CU have not yet been read. */
2788 struct compunit_symtab *compunit_symtab;
2789
2790 /* A temporary mark bit used when iterating over all CUs in
2791 expand_symtabs_matching. */
2792 unsigned int mark : 1;
2793
2794 /* True if we've tried to read the file table and found there isn't one.
2795 There will be no point in trying to read it again next time. */
2796 unsigned int no_file_data : 1;
2797 };
2798
2799 /* Utility hash function for a stmt_list_hash. */
2800
2801 static hashval_t
2802 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2803 {
2804 hashval_t v = 0;
2805
2806 if (stmt_list_hash->dwo_unit != NULL)
2807 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2808 v += to_underlying (stmt_list_hash->line_sect_off);
2809 return v;
2810 }
2811
2812 /* Utility equality function for a stmt_list_hash. */
2813
2814 static int
2815 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2816 const struct stmt_list_hash *rhs)
2817 {
2818 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2819 return 0;
2820 if (lhs->dwo_unit != NULL
2821 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2822 return 0;
2823
2824 return lhs->line_sect_off == rhs->line_sect_off;
2825 }
2826
2827 /* Hash function for a quick_file_names. */
2828
2829 static hashval_t
2830 hash_file_name_entry (const void *e)
2831 {
2832 const struct quick_file_names *file_data
2833 = (const struct quick_file_names *) e;
2834
2835 return hash_stmt_list_entry (&file_data->hash);
2836 }
2837
2838 /* Equality function for a quick_file_names. */
2839
2840 static int
2841 eq_file_name_entry (const void *a, const void *b)
2842 {
2843 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2844 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2845
2846 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2847 }
2848
2849 /* Delete function for a quick_file_names. */
2850
2851 static void
2852 delete_file_name_entry (void *e)
2853 {
2854 struct quick_file_names *file_data = (struct quick_file_names *) e;
2855 int i;
2856
2857 for (i = 0; i < file_data->num_file_names; ++i)
2858 {
2859 xfree ((void*) file_data->file_names[i]);
2860 if (file_data->real_names)
2861 xfree ((void*) file_data->real_names[i]);
2862 }
2863
2864 /* The space for the struct itself lives on objfile_obstack,
2865 so we don't free it here. */
2866 }
2867
2868 /* Create a quick_file_names hash table. */
2869
2870 static htab_t
2871 create_quick_file_names_table (unsigned int nr_initial_entries)
2872 {
2873 return htab_create_alloc (nr_initial_entries,
2874 hash_file_name_entry, eq_file_name_entry,
2875 delete_file_name_entry, xcalloc, xfree);
2876 }
2877
2878 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2879 have to be created afterwards. You should call age_cached_comp_units after
2880 processing PER_CU->CU. dw2_setup must have been already called. */
2881
2882 static void
2883 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2884 {
2885 if (per_cu->is_debug_types)
2886 load_full_type_unit (per_cu);
2887 else
2888 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2889
2890 if (per_cu->cu == NULL)
2891 return; /* Dummy CU. */
2892
2893 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2894 }
2895
2896 /* Read in the symbols for PER_CU. */
2897
2898 static void
2899 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2900 {
2901 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2902
2903 /* Skip type_unit_groups, reading the type units they contain
2904 is handled elsewhere. */
2905 if (IS_TYPE_UNIT_GROUP (per_cu))
2906 return;
2907
2908 /* The destructor of dwarf2_queue_guard frees any entries left on
2909 the queue. After this point we're guaranteed to leave this function
2910 with the dwarf queue empty. */
2911 dwarf2_queue_guard q_guard;
2912
2913 if (dwarf2_per_objfile->using_index
2914 ? per_cu->v.quick->compunit_symtab == NULL
2915 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2916 {
2917 queue_comp_unit (per_cu, language_minimal);
2918 load_cu (per_cu, skip_partial);
2919
2920 /* If we just loaded a CU from a DWO, and we're working with an index
2921 that may badly handle TUs, load all the TUs in that DWO as well.
2922 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2923 if (!per_cu->is_debug_types
2924 && per_cu->cu != NULL
2925 && per_cu->cu->dwo_unit != NULL
2926 && dwarf2_per_objfile->index_table != NULL
2927 && dwarf2_per_objfile->index_table->version <= 7
2928 /* DWP files aren't supported yet. */
2929 && get_dwp_file (dwarf2_per_objfile) == NULL)
2930 queue_and_load_all_dwo_tus (per_cu);
2931 }
2932
2933 process_queue (dwarf2_per_objfile);
2934
2935 /* Age the cache, releasing compilation units that have not
2936 been used recently. */
2937 age_cached_comp_units (dwarf2_per_objfile);
2938 }
2939
2940 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2941 the objfile from which this CU came. Returns the resulting symbol
2942 table. */
2943
2944 static struct compunit_symtab *
2945 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2946 {
2947 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2948
2949 gdb_assert (dwarf2_per_objfile->using_index);
2950 if (!per_cu->v.quick->compunit_symtab)
2951 {
2952 free_cached_comp_units freer (dwarf2_per_objfile);
2953 scoped_restore decrementer = increment_reading_symtab ();
2954 dw2_do_instantiate_symtab (per_cu, skip_partial);
2955 process_cu_includes (dwarf2_per_objfile);
2956 }
2957
2958 return per_cu->v.quick->compunit_symtab;
2959 }
2960
2961 /* See declaration. */
2962
2963 dwarf2_per_cu_data *
2964 dwarf2_per_objfile::get_cutu (int index)
2965 {
2966 if (index >= this->all_comp_units.size ())
2967 {
2968 index -= this->all_comp_units.size ();
2969 gdb_assert (index < this->all_type_units.size ());
2970 return &this->all_type_units[index]->per_cu;
2971 }
2972
2973 return this->all_comp_units[index];
2974 }
2975
2976 /* See declaration. */
2977
2978 dwarf2_per_cu_data *
2979 dwarf2_per_objfile::get_cu (int index)
2980 {
2981 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2982
2983 return this->all_comp_units[index];
2984 }
2985
2986 /* See declaration. */
2987
2988 signatured_type *
2989 dwarf2_per_objfile::get_tu (int index)
2990 {
2991 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2992
2993 return this->all_type_units[index];
2994 }
2995
2996 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2997 objfile_obstack, and constructed with the specified field
2998 values. */
2999
3000 static dwarf2_per_cu_data *
3001 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3002 struct dwarf2_section_info *section,
3003 int is_dwz,
3004 sect_offset sect_off, ULONGEST length)
3005 {
3006 struct objfile *objfile = dwarf2_per_objfile->objfile;
3007 dwarf2_per_cu_data *the_cu
3008 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3009 struct dwarf2_per_cu_data);
3010 the_cu->sect_off = sect_off;
3011 the_cu->length = length;
3012 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3013 the_cu->section = section;
3014 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3015 struct dwarf2_per_cu_quick_data);
3016 the_cu->is_dwz = is_dwz;
3017 return the_cu;
3018 }
3019
3020 /* A helper for create_cus_from_index that handles a given list of
3021 CUs. */
3022
3023 static void
3024 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3025 const gdb_byte *cu_list, offset_type n_elements,
3026 struct dwarf2_section_info *section,
3027 int is_dwz)
3028 {
3029 for (offset_type i = 0; i < n_elements; i += 2)
3030 {
3031 gdb_static_assert (sizeof (ULONGEST) >= 8);
3032
3033 sect_offset sect_off
3034 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3035 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3036 cu_list += 2 * 8;
3037
3038 dwarf2_per_cu_data *per_cu
3039 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3040 sect_off, length);
3041 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3042 }
3043 }
3044
3045 /* Read the CU list from the mapped index, and use it to create all
3046 the CU objects for this objfile. */
3047
3048 static void
3049 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3050 const gdb_byte *cu_list, offset_type cu_list_elements,
3051 const gdb_byte *dwz_list, offset_type dwz_elements)
3052 {
3053 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3054 dwarf2_per_objfile->all_comp_units.reserve
3055 ((cu_list_elements + dwz_elements) / 2);
3056
3057 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3058 &dwarf2_per_objfile->info, 0);
3059
3060 if (dwz_elements == 0)
3061 return;
3062
3063 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3064 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3065 &dwz->info, 1);
3066 }
3067
3068 /* Create the signatured type hash table from the index. */
3069
3070 static void
3071 create_signatured_type_table_from_index
3072 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3073 struct dwarf2_section_info *section,
3074 const gdb_byte *bytes,
3075 offset_type elements)
3076 {
3077 struct objfile *objfile = dwarf2_per_objfile->objfile;
3078
3079 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3080 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3081
3082 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3083
3084 for (offset_type i = 0; i < elements; i += 3)
3085 {
3086 struct signatured_type *sig_type;
3087 ULONGEST signature;
3088 void **slot;
3089 cu_offset type_offset_in_tu;
3090
3091 gdb_static_assert (sizeof (ULONGEST) >= 8);
3092 sect_offset sect_off
3093 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3094 type_offset_in_tu
3095 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3096 BFD_ENDIAN_LITTLE);
3097 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3098 bytes += 3 * 8;
3099
3100 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3101 struct signatured_type);
3102 sig_type->signature = signature;
3103 sig_type->type_offset_in_tu = type_offset_in_tu;
3104 sig_type->per_cu.is_debug_types = 1;
3105 sig_type->per_cu.section = section;
3106 sig_type->per_cu.sect_off = sect_off;
3107 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3108 sig_type->per_cu.v.quick
3109 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3110 struct dwarf2_per_cu_quick_data);
3111
3112 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3113 *slot = sig_type;
3114
3115 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3116 }
3117
3118 dwarf2_per_objfile->signatured_types = sig_types_hash;
3119 }
3120
3121 /* Create the signatured type hash table from .debug_names. */
3122
3123 static void
3124 create_signatured_type_table_from_debug_names
3125 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3126 const mapped_debug_names &map,
3127 struct dwarf2_section_info *section,
3128 struct dwarf2_section_info *abbrev_section)
3129 {
3130 struct objfile *objfile = dwarf2_per_objfile->objfile;
3131
3132 dwarf2_read_section (objfile, section);
3133 dwarf2_read_section (objfile, abbrev_section);
3134
3135 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3136 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3137
3138 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3139
3140 for (uint32_t i = 0; i < map.tu_count; ++i)
3141 {
3142 struct signatured_type *sig_type;
3143 void **slot;
3144
3145 sect_offset sect_off
3146 = (sect_offset) (extract_unsigned_integer
3147 (map.tu_table_reordered + i * map.offset_size,
3148 map.offset_size,
3149 map.dwarf5_byte_order));
3150
3151 comp_unit_head cu_header;
3152 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3153 abbrev_section,
3154 section->buffer + to_underlying (sect_off),
3155 rcuh_kind::TYPE);
3156
3157 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3158 struct signatured_type);
3159 sig_type->signature = cu_header.signature;
3160 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3161 sig_type->per_cu.is_debug_types = 1;
3162 sig_type->per_cu.section = section;
3163 sig_type->per_cu.sect_off = sect_off;
3164 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3165 sig_type->per_cu.v.quick
3166 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3167 struct dwarf2_per_cu_quick_data);
3168
3169 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3170 *slot = sig_type;
3171
3172 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3173 }
3174
3175 dwarf2_per_objfile->signatured_types = sig_types_hash;
3176 }
3177
3178 /* Read the address map data from the mapped index, and use it to
3179 populate the objfile's psymtabs_addrmap. */
3180
3181 static void
3182 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3183 struct mapped_index *index)
3184 {
3185 struct objfile *objfile = dwarf2_per_objfile->objfile;
3186 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3187 const gdb_byte *iter, *end;
3188 struct addrmap *mutable_map;
3189 CORE_ADDR baseaddr;
3190
3191 auto_obstack temp_obstack;
3192
3193 mutable_map = addrmap_create_mutable (&temp_obstack);
3194
3195 iter = index->address_table.data ();
3196 end = iter + index->address_table.size ();
3197
3198 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3199
3200 while (iter < end)
3201 {
3202 ULONGEST hi, lo, cu_index;
3203 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3204 iter += 8;
3205 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3206 iter += 8;
3207 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3208 iter += 4;
3209
3210 if (lo > hi)
3211 {
3212 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3213 hex_string (lo), hex_string (hi));
3214 continue;
3215 }
3216
3217 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3218 {
3219 complaint (_(".gdb_index address table has invalid CU number %u"),
3220 (unsigned) cu_index);
3221 continue;
3222 }
3223
3224 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3225 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3226 addrmap_set_empty (mutable_map, lo, hi - 1,
3227 dwarf2_per_objfile->get_cu (cu_index));
3228 }
3229
3230 objfile->partial_symtabs->psymtabs_addrmap
3231 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3232 }
3233
3234 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3235 populate the objfile's psymtabs_addrmap. */
3236
3237 static void
3238 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3239 struct dwarf2_section_info *section)
3240 {
3241 struct objfile *objfile = dwarf2_per_objfile->objfile;
3242 bfd *abfd = objfile->obfd;
3243 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3244 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3245 SECT_OFF_TEXT (objfile));
3246
3247 auto_obstack temp_obstack;
3248 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3249
3250 std::unordered_map<sect_offset,
3251 dwarf2_per_cu_data *,
3252 gdb::hash_enum<sect_offset>>
3253 debug_info_offset_to_per_cu;
3254 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3255 {
3256 const auto insertpair
3257 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3258 if (!insertpair.second)
3259 {
3260 warning (_("Section .debug_aranges in %s has duplicate "
3261 "debug_info_offset %s, ignoring .debug_aranges."),
3262 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3263 return;
3264 }
3265 }
3266
3267 dwarf2_read_section (objfile, section);
3268
3269 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3270
3271 const gdb_byte *addr = section->buffer;
3272
3273 while (addr < section->buffer + section->size)
3274 {
3275 const gdb_byte *const entry_addr = addr;
3276 unsigned int bytes_read;
3277
3278 const LONGEST entry_length = read_initial_length (abfd, addr,
3279 &bytes_read);
3280 addr += bytes_read;
3281
3282 const gdb_byte *const entry_end = addr + entry_length;
3283 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3284 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3285 if (addr + entry_length > section->buffer + section->size)
3286 {
3287 warning (_("Section .debug_aranges in %s entry at offset %zu "
3288 "length %s exceeds section length %s, "
3289 "ignoring .debug_aranges."),
3290 objfile_name (objfile), entry_addr - section->buffer,
3291 plongest (bytes_read + entry_length),
3292 pulongest (section->size));
3293 return;
3294 }
3295
3296 /* The version number. */
3297 const uint16_t version = read_2_bytes (abfd, addr);
3298 addr += 2;
3299 if (version != 2)
3300 {
3301 warning (_("Section .debug_aranges in %s entry at offset %zu "
3302 "has unsupported version %d, ignoring .debug_aranges."),
3303 objfile_name (objfile), entry_addr - section->buffer,
3304 version);
3305 return;
3306 }
3307
3308 const uint64_t debug_info_offset
3309 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3310 addr += offset_size;
3311 const auto per_cu_it
3312 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3313 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3314 {
3315 warning (_("Section .debug_aranges in %s entry at offset %zu "
3316 "debug_info_offset %s does not exists, "
3317 "ignoring .debug_aranges."),
3318 objfile_name (objfile), entry_addr - section->buffer,
3319 pulongest (debug_info_offset));
3320 return;
3321 }
3322 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3323
3324 const uint8_t address_size = *addr++;
3325 if (address_size < 1 || address_size > 8)
3326 {
3327 warning (_("Section .debug_aranges in %s entry at offset %zu "
3328 "address_size %u is invalid, ignoring .debug_aranges."),
3329 objfile_name (objfile), entry_addr - section->buffer,
3330 address_size);
3331 return;
3332 }
3333
3334 const uint8_t segment_selector_size = *addr++;
3335 if (segment_selector_size != 0)
3336 {
3337 warning (_("Section .debug_aranges in %s entry at offset %zu "
3338 "segment_selector_size %u is not supported, "
3339 "ignoring .debug_aranges."),
3340 objfile_name (objfile), entry_addr - section->buffer,
3341 segment_selector_size);
3342 return;
3343 }
3344
3345 /* Must pad to an alignment boundary that is twice the address
3346 size. It is undocumented by the DWARF standard but GCC does
3347 use it. */
3348 for (size_t padding = ((-(addr - section->buffer))
3349 & (2 * address_size - 1));
3350 padding > 0; padding--)
3351 if (*addr++ != 0)
3352 {
3353 warning (_("Section .debug_aranges in %s entry at offset %zu "
3354 "padding is not zero, ignoring .debug_aranges."),
3355 objfile_name (objfile), entry_addr - section->buffer);
3356 return;
3357 }
3358
3359 for (;;)
3360 {
3361 if (addr + 2 * address_size > entry_end)
3362 {
3363 warning (_("Section .debug_aranges in %s entry at offset %zu "
3364 "address list is not properly terminated, "
3365 "ignoring .debug_aranges."),
3366 objfile_name (objfile), entry_addr - section->buffer);
3367 return;
3368 }
3369 ULONGEST start = extract_unsigned_integer (addr, address_size,
3370 dwarf5_byte_order);
3371 addr += address_size;
3372 ULONGEST length = extract_unsigned_integer (addr, address_size,
3373 dwarf5_byte_order);
3374 addr += address_size;
3375 if (start == 0 && length == 0)
3376 break;
3377 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3378 {
3379 /* Symbol was eliminated due to a COMDAT group. */
3380 continue;
3381 }
3382 ULONGEST end = start + length;
3383 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3384 - baseaddr);
3385 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3386 - baseaddr);
3387 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3388 }
3389 }
3390
3391 objfile->partial_symtabs->psymtabs_addrmap
3392 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3393 }
3394
3395 /* Find a slot in the mapped index INDEX for the object named NAME.
3396 If NAME is found, set *VEC_OUT to point to the CU vector in the
3397 constant pool and return true. If NAME cannot be found, return
3398 false. */
3399
3400 static bool
3401 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3402 offset_type **vec_out)
3403 {
3404 offset_type hash;
3405 offset_type slot, step;
3406 int (*cmp) (const char *, const char *);
3407
3408 gdb::unique_xmalloc_ptr<char> without_params;
3409 if (current_language->la_language == language_cplus
3410 || current_language->la_language == language_fortran
3411 || current_language->la_language == language_d)
3412 {
3413 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3414 not contain any. */
3415
3416 if (strchr (name, '(') != NULL)
3417 {
3418 without_params = cp_remove_params (name);
3419
3420 if (without_params != NULL)
3421 name = without_params.get ();
3422 }
3423 }
3424
3425 /* Index version 4 did not support case insensitive searches. But the
3426 indices for case insensitive languages are built in lowercase, therefore
3427 simulate our NAME being searched is also lowercased. */
3428 hash = mapped_index_string_hash ((index->version == 4
3429 && case_sensitivity == case_sensitive_off
3430 ? 5 : index->version),
3431 name);
3432
3433 slot = hash & (index->symbol_table.size () - 1);
3434 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3435 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3436
3437 for (;;)
3438 {
3439 const char *str;
3440
3441 const auto &bucket = index->symbol_table[slot];
3442 if (bucket.name == 0 && bucket.vec == 0)
3443 return false;
3444
3445 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3446 if (!cmp (name, str))
3447 {
3448 *vec_out = (offset_type *) (index->constant_pool
3449 + MAYBE_SWAP (bucket.vec));
3450 return true;
3451 }
3452
3453 slot = (slot + step) & (index->symbol_table.size () - 1);
3454 }
3455 }
3456
3457 /* A helper function that reads the .gdb_index from BUFFER and fills
3458 in MAP. FILENAME is the name of the file containing the data;
3459 it is used for error reporting. DEPRECATED_OK is true if it is
3460 ok to use deprecated sections.
3461
3462 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3463 out parameters that are filled in with information about the CU and
3464 TU lists in the section.
3465
3466 Returns true if all went well, false otherwise. */
3467
3468 static bool
3469 read_gdb_index_from_buffer (struct objfile *objfile,
3470 const char *filename,
3471 bool deprecated_ok,
3472 gdb::array_view<const gdb_byte> buffer,
3473 struct mapped_index *map,
3474 const gdb_byte **cu_list,
3475 offset_type *cu_list_elements,
3476 const gdb_byte **types_list,
3477 offset_type *types_list_elements)
3478 {
3479 const gdb_byte *addr = &buffer[0];
3480
3481 /* Version check. */
3482 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3483 /* Versions earlier than 3 emitted every copy of a psymbol. This
3484 causes the index to behave very poorly for certain requests. Version 3
3485 contained incomplete addrmap. So, it seems better to just ignore such
3486 indices. */
3487 if (version < 4)
3488 {
3489 static int warning_printed = 0;
3490 if (!warning_printed)
3491 {
3492 warning (_("Skipping obsolete .gdb_index section in %s."),
3493 filename);
3494 warning_printed = 1;
3495 }
3496 return 0;
3497 }
3498 /* Index version 4 uses a different hash function than index version
3499 5 and later.
3500
3501 Versions earlier than 6 did not emit psymbols for inlined
3502 functions. Using these files will cause GDB not to be able to
3503 set breakpoints on inlined functions by name, so we ignore these
3504 indices unless the user has done
3505 "set use-deprecated-index-sections on". */
3506 if (version < 6 && !deprecated_ok)
3507 {
3508 static int warning_printed = 0;
3509 if (!warning_printed)
3510 {
3511 warning (_("\
3512 Skipping deprecated .gdb_index section in %s.\n\
3513 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3514 to use the section anyway."),
3515 filename);
3516 warning_printed = 1;
3517 }
3518 return 0;
3519 }
3520 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3521 of the TU (for symbols coming from TUs),
3522 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3523 Plus gold-generated indices can have duplicate entries for global symbols,
3524 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3525 These are just performance bugs, and we can't distinguish gdb-generated
3526 indices from gold-generated ones, so issue no warning here. */
3527
3528 /* Indexes with higher version than the one supported by GDB may be no
3529 longer backward compatible. */
3530 if (version > 8)
3531 return 0;
3532
3533 map->version = version;
3534
3535 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3536
3537 int i = 0;
3538 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3539 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3540 / 8);
3541 ++i;
3542
3543 *types_list = addr + MAYBE_SWAP (metadata[i]);
3544 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3545 - MAYBE_SWAP (metadata[i]))
3546 / 8);
3547 ++i;
3548
3549 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3550 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3551 map->address_table
3552 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3553 ++i;
3554
3555 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3556 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3557 map->symbol_table
3558 = gdb::array_view<mapped_index::symbol_table_slot>
3559 ((mapped_index::symbol_table_slot *) symbol_table,
3560 (mapped_index::symbol_table_slot *) symbol_table_end);
3561
3562 ++i;
3563 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3564
3565 return 1;
3566 }
3567
3568 /* Callback types for dwarf2_read_gdb_index. */
3569
3570 typedef gdb::function_view
3571 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3572 get_gdb_index_contents_ftype;
3573 typedef gdb::function_view
3574 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3575 get_gdb_index_contents_dwz_ftype;
3576
3577 /* Read .gdb_index. If everything went ok, initialize the "quick"
3578 elements of all the CUs and return 1. Otherwise, return 0. */
3579
3580 static int
3581 dwarf2_read_gdb_index
3582 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3583 get_gdb_index_contents_ftype get_gdb_index_contents,
3584 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3585 {
3586 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3587 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3588 struct dwz_file *dwz;
3589 struct objfile *objfile = dwarf2_per_objfile->objfile;
3590
3591 gdb::array_view<const gdb_byte> main_index_contents
3592 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3593
3594 if (main_index_contents.empty ())
3595 return 0;
3596
3597 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3598 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3599 use_deprecated_index_sections,
3600 main_index_contents, map.get (), &cu_list,
3601 &cu_list_elements, &types_list,
3602 &types_list_elements))
3603 return 0;
3604
3605 /* Don't use the index if it's empty. */
3606 if (map->symbol_table.empty ())
3607 return 0;
3608
3609 /* If there is a .dwz file, read it so we can get its CU list as
3610 well. */
3611 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3612 if (dwz != NULL)
3613 {
3614 struct mapped_index dwz_map;
3615 const gdb_byte *dwz_types_ignore;
3616 offset_type dwz_types_elements_ignore;
3617
3618 gdb::array_view<const gdb_byte> dwz_index_content
3619 = get_gdb_index_contents_dwz (objfile, dwz);
3620
3621 if (dwz_index_content.empty ())
3622 return 0;
3623
3624 if (!read_gdb_index_from_buffer (objfile,
3625 bfd_get_filename (dwz->dwz_bfd), 1,
3626 dwz_index_content, &dwz_map,
3627 &dwz_list, &dwz_list_elements,
3628 &dwz_types_ignore,
3629 &dwz_types_elements_ignore))
3630 {
3631 warning (_("could not read '.gdb_index' section from %s; skipping"),
3632 bfd_get_filename (dwz->dwz_bfd));
3633 return 0;
3634 }
3635 }
3636
3637 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3638 dwz_list, dwz_list_elements);
3639
3640 if (types_list_elements)
3641 {
3642 struct dwarf2_section_info *section;
3643
3644 /* We can only handle a single .debug_types when we have an
3645 index. */
3646 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3647 return 0;
3648
3649 section = VEC_index (dwarf2_section_info_def,
3650 dwarf2_per_objfile->types, 0);
3651
3652 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3653 types_list, types_list_elements);
3654 }
3655
3656 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3657
3658 dwarf2_per_objfile->index_table = std::move (map);
3659 dwarf2_per_objfile->using_index = 1;
3660 dwarf2_per_objfile->quick_file_names_table =
3661 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3662
3663 return 1;
3664 }
3665
3666 /* die_reader_func for dw2_get_file_names. */
3667
3668 static void
3669 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3670 const gdb_byte *info_ptr,
3671 struct die_info *comp_unit_die,
3672 int has_children,
3673 void *data)
3674 {
3675 struct dwarf2_cu *cu = reader->cu;
3676 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3677 struct dwarf2_per_objfile *dwarf2_per_objfile
3678 = cu->per_cu->dwarf2_per_objfile;
3679 struct objfile *objfile = dwarf2_per_objfile->objfile;
3680 struct dwarf2_per_cu_data *lh_cu;
3681 struct attribute *attr;
3682 int i;
3683 void **slot;
3684 struct quick_file_names *qfn;
3685
3686 gdb_assert (! this_cu->is_debug_types);
3687
3688 /* Our callers never want to match partial units -- instead they
3689 will match the enclosing full CU. */
3690 if (comp_unit_die->tag == DW_TAG_partial_unit)
3691 {
3692 this_cu->v.quick->no_file_data = 1;
3693 return;
3694 }
3695
3696 lh_cu = this_cu;
3697 slot = NULL;
3698
3699 line_header_up lh;
3700 sect_offset line_offset {};
3701
3702 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3703 if (attr)
3704 {
3705 struct quick_file_names find_entry;
3706
3707 line_offset = (sect_offset) DW_UNSND (attr);
3708
3709 /* We may have already read in this line header (TU line header sharing).
3710 If we have we're done. */
3711 find_entry.hash.dwo_unit = cu->dwo_unit;
3712 find_entry.hash.line_sect_off = line_offset;
3713 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3714 &find_entry, INSERT);
3715 if (*slot != NULL)
3716 {
3717 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3718 return;
3719 }
3720
3721 lh = dwarf_decode_line_header (line_offset, cu);
3722 }
3723 if (lh == NULL)
3724 {
3725 lh_cu->v.quick->no_file_data = 1;
3726 return;
3727 }
3728
3729 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3730 qfn->hash.dwo_unit = cu->dwo_unit;
3731 qfn->hash.line_sect_off = line_offset;
3732 gdb_assert (slot != NULL);
3733 *slot = qfn;
3734
3735 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3736
3737 qfn->num_file_names = lh->file_names.size ();
3738 qfn->file_names =
3739 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3740 for (i = 0; i < lh->file_names.size (); ++i)
3741 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3742 qfn->real_names = NULL;
3743
3744 lh_cu->v.quick->file_names = qfn;
3745 }
3746
3747 /* A helper for the "quick" functions which attempts to read the line
3748 table for THIS_CU. */
3749
3750 static struct quick_file_names *
3751 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3752 {
3753 /* This should never be called for TUs. */
3754 gdb_assert (! this_cu->is_debug_types);
3755 /* Nor type unit groups. */
3756 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3757
3758 if (this_cu->v.quick->file_names != NULL)
3759 return this_cu->v.quick->file_names;
3760 /* If we know there is no line data, no point in looking again. */
3761 if (this_cu->v.quick->no_file_data)
3762 return NULL;
3763
3764 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3765
3766 if (this_cu->v.quick->no_file_data)
3767 return NULL;
3768 return this_cu->v.quick->file_names;
3769 }
3770
3771 /* A helper for the "quick" functions which computes and caches the
3772 real path for a given file name from the line table. */
3773
3774 static const char *
3775 dw2_get_real_path (struct objfile *objfile,
3776 struct quick_file_names *qfn, int index)
3777 {
3778 if (qfn->real_names == NULL)
3779 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3780 qfn->num_file_names, const char *);
3781
3782 if (qfn->real_names[index] == NULL)
3783 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3784
3785 return qfn->real_names[index];
3786 }
3787
3788 static struct symtab *
3789 dw2_find_last_source_symtab (struct objfile *objfile)
3790 {
3791 struct dwarf2_per_objfile *dwarf2_per_objfile
3792 = get_dwarf2_per_objfile (objfile);
3793 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3794 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3795
3796 if (cust == NULL)
3797 return NULL;
3798
3799 return compunit_primary_filetab (cust);
3800 }
3801
3802 /* Traversal function for dw2_forget_cached_source_info. */
3803
3804 static int
3805 dw2_free_cached_file_names (void **slot, void *info)
3806 {
3807 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3808
3809 if (file_data->real_names)
3810 {
3811 int i;
3812
3813 for (i = 0; i < file_data->num_file_names; ++i)
3814 {
3815 xfree ((void*) file_data->real_names[i]);
3816 file_data->real_names[i] = NULL;
3817 }
3818 }
3819
3820 return 1;
3821 }
3822
3823 static void
3824 dw2_forget_cached_source_info (struct objfile *objfile)
3825 {
3826 struct dwarf2_per_objfile *dwarf2_per_objfile
3827 = get_dwarf2_per_objfile (objfile);
3828
3829 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3830 dw2_free_cached_file_names, NULL);
3831 }
3832
3833 /* Helper function for dw2_map_symtabs_matching_filename that expands
3834 the symtabs and calls the iterator. */
3835
3836 static int
3837 dw2_map_expand_apply (struct objfile *objfile,
3838 struct dwarf2_per_cu_data *per_cu,
3839 const char *name, const char *real_path,
3840 gdb::function_view<bool (symtab *)> callback)
3841 {
3842 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3843
3844 /* Don't visit already-expanded CUs. */
3845 if (per_cu->v.quick->compunit_symtab)
3846 return 0;
3847
3848 /* This may expand more than one symtab, and we want to iterate over
3849 all of them. */
3850 dw2_instantiate_symtab (per_cu, false);
3851
3852 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3853 last_made, callback);
3854 }
3855
3856 /* Implementation of the map_symtabs_matching_filename method. */
3857
3858 static bool
3859 dw2_map_symtabs_matching_filename
3860 (struct objfile *objfile, const char *name, const char *real_path,
3861 gdb::function_view<bool (symtab *)> callback)
3862 {
3863 const char *name_basename = lbasename (name);
3864 struct dwarf2_per_objfile *dwarf2_per_objfile
3865 = get_dwarf2_per_objfile (objfile);
3866
3867 /* The rule is CUs specify all the files, including those used by
3868 any TU, so there's no need to scan TUs here. */
3869
3870 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3871 {
3872 /* We only need to look at symtabs not already expanded. */
3873 if (per_cu->v.quick->compunit_symtab)
3874 continue;
3875
3876 quick_file_names *file_data = dw2_get_file_names (per_cu);
3877 if (file_data == NULL)
3878 continue;
3879
3880 for (int j = 0; j < file_data->num_file_names; ++j)
3881 {
3882 const char *this_name = file_data->file_names[j];
3883 const char *this_real_name;
3884
3885 if (compare_filenames_for_search (this_name, name))
3886 {
3887 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3888 callback))
3889 return true;
3890 continue;
3891 }
3892
3893 /* Before we invoke realpath, which can get expensive when many
3894 files are involved, do a quick comparison of the basenames. */
3895 if (! basenames_may_differ
3896 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3897 continue;
3898
3899 this_real_name = dw2_get_real_path (objfile, file_data, j);
3900 if (compare_filenames_for_search (this_real_name, name))
3901 {
3902 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3903 callback))
3904 return true;
3905 continue;
3906 }
3907
3908 if (real_path != NULL)
3909 {
3910 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3911 gdb_assert (IS_ABSOLUTE_PATH (name));
3912 if (this_real_name != NULL
3913 && FILENAME_CMP (real_path, this_real_name) == 0)
3914 {
3915 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3916 callback))
3917 return true;
3918 continue;
3919 }
3920 }
3921 }
3922 }
3923
3924 return false;
3925 }
3926
3927 /* Struct used to manage iterating over all CUs looking for a symbol. */
3928
3929 struct dw2_symtab_iterator
3930 {
3931 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3932 struct dwarf2_per_objfile *dwarf2_per_objfile;
3933 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3934 int want_specific_block;
3935 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3936 Unused if !WANT_SPECIFIC_BLOCK. */
3937 int block_index;
3938 /* The kind of symbol we're looking for. */
3939 domain_enum domain;
3940 /* The list of CUs from the index entry of the symbol,
3941 or NULL if not found. */
3942 offset_type *vec;
3943 /* The next element in VEC to look at. */
3944 int next;
3945 /* The number of elements in VEC, or zero if there is no match. */
3946 int length;
3947 /* Have we seen a global version of the symbol?
3948 If so we can ignore all further global instances.
3949 This is to work around gold/15646, inefficient gold-generated
3950 indices. */
3951 int global_seen;
3952 };
3953
3954 /* Initialize the index symtab iterator ITER.
3955 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3956 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3957
3958 static void
3959 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3960 struct dwarf2_per_objfile *dwarf2_per_objfile,
3961 int want_specific_block,
3962 int block_index,
3963 domain_enum domain,
3964 const char *name)
3965 {
3966 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3967 iter->want_specific_block = want_specific_block;
3968 iter->block_index = block_index;
3969 iter->domain = domain;
3970 iter->next = 0;
3971 iter->global_seen = 0;
3972
3973 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3974
3975 /* index is NULL if OBJF_READNOW. */
3976 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3977 iter->length = MAYBE_SWAP (*iter->vec);
3978 else
3979 {
3980 iter->vec = NULL;
3981 iter->length = 0;
3982 }
3983 }
3984
3985 /* Return the next matching CU or NULL if there are no more. */
3986
3987 static struct dwarf2_per_cu_data *
3988 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3989 {
3990 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3991
3992 for ( ; iter->next < iter->length; ++iter->next)
3993 {
3994 offset_type cu_index_and_attrs =
3995 MAYBE_SWAP (iter->vec[iter->next + 1]);
3996 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3997 int want_static = iter->block_index != GLOBAL_BLOCK;
3998 /* This value is only valid for index versions >= 7. */
3999 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4000 gdb_index_symbol_kind symbol_kind =
4001 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4002 /* Only check the symbol attributes if they're present.
4003 Indices prior to version 7 don't record them,
4004 and indices >= 7 may elide them for certain symbols
4005 (gold does this). */
4006 int attrs_valid =
4007 (dwarf2_per_objfile->index_table->version >= 7
4008 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4009
4010 /* Don't crash on bad data. */
4011 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4012 + dwarf2_per_objfile->all_type_units.size ()))
4013 {
4014 complaint (_(".gdb_index entry has bad CU index"
4015 " [in module %s]"),
4016 objfile_name (dwarf2_per_objfile->objfile));
4017 continue;
4018 }
4019
4020 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4021
4022 /* Skip if already read in. */
4023 if (per_cu->v.quick->compunit_symtab)
4024 continue;
4025
4026 /* Check static vs global. */
4027 if (attrs_valid)
4028 {
4029 if (iter->want_specific_block
4030 && want_static != is_static)
4031 continue;
4032 /* Work around gold/15646. */
4033 if (!is_static && iter->global_seen)
4034 continue;
4035 if (!is_static)
4036 iter->global_seen = 1;
4037 }
4038
4039 /* Only check the symbol's kind if it has one. */
4040 if (attrs_valid)
4041 {
4042 switch (iter->domain)
4043 {
4044 case VAR_DOMAIN:
4045 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4046 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4047 /* Some types are also in VAR_DOMAIN. */
4048 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4049 continue;
4050 break;
4051 case STRUCT_DOMAIN:
4052 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4053 continue;
4054 break;
4055 case LABEL_DOMAIN:
4056 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4057 continue;
4058 break;
4059 default:
4060 break;
4061 }
4062 }
4063
4064 ++iter->next;
4065 return per_cu;
4066 }
4067
4068 return NULL;
4069 }
4070
4071 static struct compunit_symtab *
4072 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4073 const char *name, domain_enum domain)
4074 {
4075 struct compunit_symtab *stab_best = NULL;
4076 struct dwarf2_per_objfile *dwarf2_per_objfile
4077 = get_dwarf2_per_objfile (objfile);
4078
4079 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4080
4081 struct dw2_symtab_iterator iter;
4082 struct dwarf2_per_cu_data *per_cu;
4083
4084 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4085
4086 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4087 {
4088 struct symbol *sym, *with_opaque = NULL;
4089 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4090 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4091 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4092
4093 sym = block_find_symbol (block, name, domain,
4094 block_find_non_opaque_type_preferred,
4095 &with_opaque);
4096
4097 /* Some caution must be observed with overloaded functions
4098 and methods, since the index will not contain any overload
4099 information (but NAME might contain it). */
4100
4101 if (sym != NULL
4102 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4103 return stab;
4104 if (with_opaque != NULL
4105 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4106 stab_best = stab;
4107
4108 /* Keep looking through other CUs. */
4109 }
4110
4111 return stab_best;
4112 }
4113
4114 static void
4115 dw2_print_stats (struct objfile *objfile)
4116 {
4117 struct dwarf2_per_objfile *dwarf2_per_objfile
4118 = get_dwarf2_per_objfile (objfile);
4119 int total = (dwarf2_per_objfile->all_comp_units.size ()
4120 + dwarf2_per_objfile->all_type_units.size ());
4121 int count = 0;
4122
4123 for (int i = 0; i < total; ++i)
4124 {
4125 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4126
4127 if (!per_cu->v.quick->compunit_symtab)
4128 ++count;
4129 }
4130 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4131 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4132 }
4133
4134 /* This dumps minimal information about the index.
4135 It is called via "mt print objfiles".
4136 One use is to verify .gdb_index has been loaded by the
4137 gdb.dwarf2/gdb-index.exp testcase. */
4138
4139 static void
4140 dw2_dump (struct objfile *objfile)
4141 {
4142 struct dwarf2_per_objfile *dwarf2_per_objfile
4143 = get_dwarf2_per_objfile (objfile);
4144
4145 gdb_assert (dwarf2_per_objfile->using_index);
4146 printf_filtered (".gdb_index:");
4147 if (dwarf2_per_objfile->index_table != NULL)
4148 {
4149 printf_filtered (" version %d\n",
4150 dwarf2_per_objfile->index_table->version);
4151 }
4152 else
4153 printf_filtered (" faked for \"readnow\"\n");
4154 printf_filtered ("\n");
4155 }
4156
4157 static void
4158 dw2_expand_symtabs_for_function (struct objfile *objfile,
4159 const char *func_name)
4160 {
4161 struct dwarf2_per_objfile *dwarf2_per_objfile
4162 = get_dwarf2_per_objfile (objfile);
4163
4164 struct dw2_symtab_iterator iter;
4165 struct dwarf2_per_cu_data *per_cu;
4166
4167 /* Note: It doesn't matter what we pass for block_index here. */
4168 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4169 func_name);
4170
4171 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4172 dw2_instantiate_symtab (per_cu, false);
4173
4174 }
4175
4176 static void
4177 dw2_expand_all_symtabs (struct objfile *objfile)
4178 {
4179 struct dwarf2_per_objfile *dwarf2_per_objfile
4180 = get_dwarf2_per_objfile (objfile);
4181 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4182 + dwarf2_per_objfile->all_type_units.size ());
4183
4184 for (int i = 0; i < total_units; ++i)
4185 {
4186 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4187
4188 /* We don't want to directly expand a partial CU, because if we
4189 read it with the wrong language, then assertion failures can
4190 be triggered later on. See PR symtab/23010. So, tell
4191 dw2_instantiate_symtab to skip partial CUs -- any important
4192 partial CU will be read via DW_TAG_imported_unit anyway. */
4193 dw2_instantiate_symtab (per_cu, true);
4194 }
4195 }
4196
4197 static void
4198 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4199 const char *fullname)
4200 {
4201 struct dwarf2_per_objfile *dwarf2_per_objfile
4202 = get_dwarf2_per_objfile (objfile);
4203
4204 /* We don't need to consider type units here.
4205 This is only called for examining code, e.g. expand_line_sal.
4206 There can be an order of magnitude (or more) more type units
4207 than comp units, and we avoid them if we can. */
4208
4209 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4210 {
4211 /* We only need to look at symtabs not already expanded. */
4212 if (per_cu->v.quick->compunit_symtab)
4213 continue;
4214
4215 quick_file_names *file_data = dw2_get_file_names (per_cu);
4216 if (file_data == NULL)
4217 continue;
4218
4219 for (int j = 0; j < file_data->num_file_names; ++j)
4220 {
4221 const char *this_fullname = file_data->file_names[j];
4222
4223 if (filename_cmp (this_fullname, fullname) == 0)
4224 {
4225 dw2_instantiate_symtab (per_cu, false);
4226 break;
4227 }
4228 }
4229 }
4230 }
4231
4232 static void
4233 dw2_map_matching_symbols (struct objfile *objfile,
4234 const char * name, domain_enum domain,
4235 int global,
4236 int (*callback) (const struct block *,
4237 struct symbol *, void *),
4238 void *data, symbol_name_match_type match,
4239 symbol_compare_ftype *ordered_compare)
4240 {
4241 /* Currently unimplemented; used for Ada. The function can be called if the
4242 current language is Ada for a non-Ada objfile using GNU index. As Ada
4243 does not look for non-Ada symbols this function should just return. */
4244 }
4245
4246 /* Symbol name matcher for .gdb_index names.
4247
4248 Symbol names in .gdb_index have a few particularities:
4249
4250 - There's no indication of which is the language of each symbol.
4251
4252 Since each language has its own symbol name matching algorithm,
4253 and we don't know which language is the right one, we must match
4254 each symbol against all languages. This would be a potential
4255 performance problem if it were not mitigated by the
4256 mapped_index::name_components lookup table, which significantly
4257 reduces the number of times we need to call into this matcher,
4258 making it a non-issue.
4259
4260 - Symbol names in the index have no overload (parameter)
4261 information. I.e., in C++, "foo(int)" and "foo(long)" both
4262 appear as "foo" in the index, for example.
4263
4264 This means that the lookup names passed to the symbol name
4265 matcher functions must have no parameter information either
4266 because (e.g.) symbol search name "foo" does not match
4267 lookup-name "foo(int)" [while swapping search name for lookup
4268 name would match].
4269 */
4270 class gdb_index_symbol_name_matcher
4271 {
4272 public:
4273 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4274 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4275
4276 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4277 Returns true if any matcher matches. */
4278 bool matches (const char *symbol_name);
4279
4280 private:
4281 /* A reference to the lookup name we're matching against. */
4282 const lookup_name_info &m_lookup_name;
4283
4284 /* A vector holding all the different symbol name matchers, for all
4285 languages. */
4286 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4287 };
4288
4289 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4290 (const lookup_name_info &lookup_name)
4291 : m_lookup_name (lookup_name)
4292 {
4293 /* Prepare the vector of comparison functions upfront, to avoid
4294 doing the same work for each symbol. Care is taken to avoid
4295 matching with the same matcher more than once if/when multiple
4296 languages use the same matcher function. */
4297 auto &matchers = m_symbol_name_matcher_funcs;
4298 matchers.reserve (nr_languages);
4299
4300 matchers.push_back (default_symbol_name_matcher);
4301
4302 for (int i = 0; i < nr_languages; i++)
4303 {
4304 const language_defn *lang = language_def ((enum language) i);
4305 symbol_name_matcher_ftype *name_matcher
4306 = get_symbol_name_matcher (lang, m_lookup_name);
4307
4308 /* Don't insert the same comparison routine more than once.
4309 Note that we do this linear walk instead of a seemingly
4310 cheaper sorted insert, or use a std::set or something like
4311 that, because relative order of function addresses is not
4312 stable. This is not a problem in practice because the number
4313 of supported languages is low, and the cost here is tiny
4314 compared to the number of searches we'll do afterwards using
4315 this object. */
4316 if (name_matcher != default_symbol_name_matcher
4317 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4318 == matchers.end ()))
4319 matchers.push_back (name_matcher);
4320 }
4321 }
4322
4323 bool
4324 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4325 {
4326 for (auto matches_name : m_symbol_name_matcher_funcs)
4327 if (matches_name (symbol_name, m_lookup_name, NULL))
4328 return true;
4329
4330 return false;
4331 }
4332
4333 /* Starting from a search name, return the string that finds the upper
4334 bound of all strings that start with SEARCH_NAME in a sorted name
4335 list. Returns the empty string to indicate that the upper bound is
4336 the end of the list. */
4337
4338 static std::string
4339 make_sort_after_prefix_name (const char *search_name)
4340 {
4341 /* When looking to complete "func", we find the upper bound of all
4342 symbols that start with "func" by looking for where we'd insert
4343 the closest string that would follow "func" in lexicographical
4344 order. Usually, that's "func"-with-last-character-incremented,
4345 i.e. "fund". Mind non-ASCII characters, though. Usually those
4346 will be UTF-8 multi-byte sequences, but we can't be certain.
4347 Especially mind the 0xff character, which is a valid character in
4348 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4349 rule out compilers allowing it in identifiers. Note that
4350 conveniently, strcmp/strcasecmp are specified to compare
4351 characters interpreted as unsigned char. So what we do is treat
4352 the whole string as a base 256 number composed of a sequence of
4353 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4354 to 0, and carries 1 to the following more-significant position.
4355 If the very first character in SEARCH_NAME ends up incremented
4356 and carries/overflows, then the upper bound is the end of the
4357 list. The string after the empty string is also the empty
4358 string.
4359
4360 Some examples of this operation:
4361
4362 SEARCH_NAME => "+1" RESULT
4363
4364 "abc" => "abd"
4365 "ab\xff" => "ac"
4366 "\xff" "a" "\xff" => "\xff" "b"
4367 "\xff" => ""
4368 "\xff\xff" => ""
4369 "" => ""
4370
4371 Then, with these symbols for example:
4372
4373 func
4374 func1
4375 fund
4376
4377 completing "func" looks for symbols between "func" and
4378 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4379 which finds "func" and "func1", but not "fund".
4380
4381 And with:
4382
4383 funcÿ (Latin1 'ÿ' [0xff])
4384 funcÿ1
4385 fund
4386
4387 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4388 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4389
4390 And with:
4391
4392 ÿÿ (Latin1 'ÿ' [0xff])
4393 ÿÿ1
4394
4395 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4396 the end of the list.
4397 */
4398 std::string after = search_name;
4399 while (!after.empty () && (unsigned char) after.back () == 0xff)
4400 after.pop_back ();
4401 if (!after.empty ())
4402 after.back () = (unsigned char) after.back () + 1;
4403 return after;
4404 }
4405
4406 /* See declaration. */
4407
4408 std::pair<std::vector<name_component>::const_iterator,
4409 std::vector<name_component>::const_iterator>
4410 mapped_index_base::find_name_components_bounds
4411 (const lookup_name_info &lookup_name_without_params) const
4412 {
4413 auto *name_cmp
4414 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4415
4416 const char *cplus
4417 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4418
4419 /* Comparison function object for lower_bound that matches against a
4420 given symbol name. */
4421 auto lookup_compare_lower = [&] (const name_component &elem,
4422 const char *name)
4423 {
4424 const char *elem_qualified = this->symbol_name_at (elem.idx);
4425 const char *elem_name = elem_qualified + elem.name_offset;
4426 return name_cmp (elem_name, name) < 0;
4427 };
4428
4429 /* Comparison function object for upper_bound that matches against a
4430 given symbol name. */
4431 auto lookup_compare_upper = [&] (const char *name,
4432 const name_component &elem)
4433 {
4434 const char *elem_qualified = this->symbol_name_at (elem.idx);
4435 const char *elem_name = elem_qualified + elem.name_offset;
4436 return name_cmp (name, elem_name) < 0;
4437 };
4438
4439 auto begin = this->name_components.begin ();
4440 auto end = this->name_components.end ();
4441
4442 /* Find the lower bound. */
4443 auto lower = [&] ()
4444 {
4445 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4446 return begin;
4447 else
4448 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4449 } ();
4450
4451 /* Find the upper bound. */
4452 auto upper = [&] ()
4453 {
4454 if (lookup_name_without_params.completion_mode ())
4455 {
4456 /* In completion mode, we want UPPER to point past all
4457 symbols names that have the same prefix. I.e., with
4458 these symbols, and completing "func":
4459
4460 function << lower bound
4461 function1
4462 other_function << upper bound
4463
4464 We find the upper bound by looking for the insertion
4465 point of "func"-with-last-character-incremented,
4466 i.e. "fund". */
4467 std::string after = make_sort_after_prefix_name (cplus);
4468 if (after.empty ())
4469 return end;
4470 return std::lower_bound (lower, end, after.c_str (),
4471 lookup_compare_lower);
4472 }
4473 else
4474 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4475 } ();
4476
4477 return {lower, upper};
4478 }
4479
4480 /* See declaration. */
4481
4482 void
4483 mapped_index_base::build_name_components ()
4484 {
4485 if (!this->name_components.empty ())
4486 return;
4487
4488 this->name_components_casing = case_sensitivity;
4489 auto *name_cmp
4490 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4491
4492 /* The code below only knows how to break apart components of C++
4493 symbol names (and other languages that use '::' as
4494 namespace/module separator). If we add support for wild matching
4495 to some language that uses some other operator (E.g., Ada, Go and
4496 D use '.'), then we'll need to try splitting the symbol name
4497 according to that language too. Note that Ada does support wild
4498 matching, but doesn't currently support .gdb_index. */
4499 auto count = this->symbol_name_count ();
4500 for (offset_type idx = 0; idx < count; idx++)
4501 {
4502 if (this->symbol_name_slot_invalid (idx))
4503 continue;
4504
4505 const char *name = this->symbol_name_at (idx);
4506
4507 /* Add each name component to the name component table. */
4508 unsigned int previous_len = 0;
4509 for (unsigned int current_len = cp_find_first_component (name);
4510 name[current_len] != '\0';
4511 current_len += cp_find_first_component (name + current_len))
4512 {
4513 gdb_assert (name[current_len] == ':');
4514 this->name_components.push_back ({previous_len, idx});
4515 /* Skip the '::'. */
4516 current_len += 2;
4517 previous_len = current_len;
4518 }
4519 this->name_components.push_back ({previous_len, idx});
4520 }
4521
4522 /* Sort name_components elements by name. */
4523 auto name_comp_compare = [&] (const name_component &left,
4524 const name_component &right)
4525 {
4526 const char *left_qualified = this->symbol_name_at (left.idx);
4527 const char *right_qualified = this->symbol_name_at (right.idx);
4528
4529 const char *left_name = left_qualified + left.name_offset;
4530 const char *right_name = right_qualified + right.name_offset;
4531
4532 return name_cmp (left_name, right_name) < 0;
4533 };
4534
4535 std::sort (this->name_components.begin (),
4536 this->name_components.end (),
4537 name_comp_compare);
4538 }
4539
4540 /* Helper for dw2_expand_symtabs_matching that works with a
4541 mapped_index_base instead of the containing objfile. This is split
4542 to a separate function in order to be able to unit test the
4543 name_components matching using a mock mapped_index_base. For each
4544 symbol name that matches, calls MATCH_CALLBACK, passing it the
4545 symbol's index in the mapped_index_base symbol table. */
4546
4547 static void
4548 dw2_expand_symtabs_matching_symbol
4549 (mapped_index_base &index,
4550 const lookup_name_info &lookup_name_in,
4551 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4552 enum search_domain kind,
4553 gdb::function_view<void (offset_type)> match_callback)
4554 {
4555 lookup_name_info lookup_name_without_params
4556 = lookup_name_in.make_ignore_params ();
4557 gdb_index_symbol_name_matcher lookup_name_matcher
4558 (lookup_name_without_params);
4559
4560 /* Build the symbol name component sorted vector, if we haven't
4561 yet. */
4562 index.build_name_components ();
4563
4564 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4565
4566 /* Now for each symbol name in range, check to see if we have a name
4567 match, and if so, call the MATCH_CALLBACK callback. */
4568
4569 /* The same symbol may appear more than once in the range though.
4570 E.g., if we're looking for symbols that complete "w", and we have
4571 a symbol named "w1::w2", we'll find the two name components for
4572 that same symbol in the range. To be sure we only call the
4573 callback once per symbol, we first collect the symbol name
4574 indexes that matched in a temporary vector and ignore
4575 duplicates. */
4576 std::vector<offset_type> matches;
4577 matches.reserve (std::distance (bounds.first, bounds.second));
4578
4579 for (; bounds.first != bounds.second; ++bounds.first)
4580 {
4581 const char *qualified = index.symbol_name_at (bounds.first->idx);
4582
4583 if (!lookup_name_matcher.matches (qualified)
4584 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4585 continue;
4586
4587 matches.push_back (bounds.first->idx);
4588 }
4589
4590 std::sort (matches.begin (), matches.end ());
4591
4592 /* Finally call the callback, once per match. */
4593 ULONGEST prev = -1;
4594 for (offset_type idx : matches)
4595 {
4596 if (prev != idx)
4597 {
4598 match_callback (idx);
4599 prev = idx;
4600 }
4601 }
4602
4603 /* Above we use a type wider than idx's for 'prev', since 0 and
4604 (offset_type)-1 are both possible values. */
4605 static_assert (sizeof (prev) > sizeof (offset_type), "");
4606 }
4607
4608 #if GDB_SELF_TEST
4609
4610 namespace selftests { namespace dw2_expand_symtabs_matching {
4611
4612 /* A mock .gdb_index/.debug_names-like name index table, enough to
4613 exercise dw2_expand_symtabs_matching_symbol, which works with the
4614 mapped_index_base interface. Builds an index from the symbol list
4615 passed as parameter to the constructor. */
4616 class mock_mapped_index : public mapped_index_base
4617 {
4618 public:
4619 mock_mapped_index (gdb::array_view<const char *> symbols)
4620 : m_symbol_table (symbols)
4621 {}
4622
4623 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4624
4625 /* Return the number of names in the symbol table. */
4626 size_t symbol_name_count () const override
4627 {
4628 return m_symbol_table.size ();
4629 }
4630
4631 /* Get the name of the symbol at IDX in the symbol table. */
4632 const char *symbol_name_at (offset_type idx) const override
4633 {
4634 return m_symbol_table[idx];
4635 }
4636
4637 private:
4638 gdb::array_view<const char *> m_symbol_table;
4639 };
4640
4641 /* Convenience function that converts a NULL pointer to a "<null>"
4642 string, to pass to print routines. */
4643
4644 static const char *
4645 string_or_null (const char *str)
4646 {
4647 return str != NULL ? str : "<null>";
4648 }
4649
4650 /* Check if a lookup_name_info built from
4651 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4652 index. EXPECTED_LIST is the list of expected matches, in expected
4653 matching order. If no match expected, then an empty list is
4654 specified. Returns true on success. On failure prints a warning
4655 indicating the file:line that failed, and returns false. */
4656
4657 static bool
4658 check_match (const char *file, int line,
4659 mock_mapped_index &mock_index,
4660 const char *name, symbol_name_match_type match_type,
4661 bool completion_mode,
4662 std::initializer_list<const char *> expected_list)
4663 {
4664 lookup_name_info lookup_name (name, match_type, completion_mode);
4665
4666 bool matched = true;
4667
4668 auto mismatch = [&] (const char *expected_str,
4669 const char *got)
4670 {
4671 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4672 "expected=\"%s\", got=\"%s\"\n"),
4673 file, line,
4674 (match_type == symbol_name_match_type::FULL
4675 ? "FULL" : "WILD"),
4676 name, string_or_null (expected_str), string_or_null (got));
4677 matched = false;
4678 };
4679
4680 auto expected_it = expected_list.begin ();
4681 auto expected_end = expected_list.end ();
4682
4683 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4684 NULL, ALL_DOMAIN,
4685 [&] (offset_type idx)
4686 {
4687 const char *matched_name = mock_index.symbol_name_at (idx);
4688 const char *expected_str
4689 = expected_it == expected_end ? NULL : *expected_it++;
4690
4691 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4692 mismatch (expected_str, matched_name);
4693 });
4694
4695 const char *expected_str
4696 = expected_it == expected_end ? NULL : *expected_it++;
4697 if (expected_str != NULL)
4698 mismatch (expected_str, NULL);
4699
4700 return matched;
4701 }
4702
4703 /* The symbols added to the mock mapped_index for testing (in
4704 canonical form). */
4705 static const char *test_symbols[] = {
4706 "function",
4707 "std::bar",
4708 "std::zfunction",
4709 "std::zfunction2",
4710 "w1::w2",
4711 "ns::foo<char*>",
4712 "ns::foo<int>",
4713 "ns::foo<long>",
4714 "ns2::tmpl<int>::foo2",
4715 "(anonymous namespace)::A::B::C",
4716
4717 /* These are used to check that the increment-last-char in the
4718 matching algorithm for completion doesn't match "t1_fund" when
4719 completing "t1_func". */
4720 "t1_func",
4721 "t1_func1",
4722 "t1_fund",
4723 "t1_fund1",
4724
4725 /* A UTF-8 name with multi-byte sequences to make sure that
4726 cp-name-parser understands this as a single identifier ("função"
4727 is "function" in PT). */
4728 u8"u8função",
4729
4730 /* \377 (0xff) is Latin1 'ÿ'. */
4731 "yfunc\377",
4732
4733 /* \377 (0xff) is Latin1 'ÿ'. */
4734 "\377",
4735 "\377\377123",
4736
4737 /* A name with all sorts of complications. Starts with "z" to make
4738 it easier for the completion tests below. */
4739 #define Z_SYM_NAME \
4740 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4741 "::tuple<(anonymous namespace)::ui*, " \
4742 "std::default_delete<(anonymous namespace)::ui>, void>"
4743
4744 Z_SYM_NAME
4745 };
4746
4747 /* Returns true if the mapped_index_base::find_name_component_bounds
4748 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4749 in completion mode. */
4750
4751 static bool
4752 check_find_bounds_finds (mapped_index_base &index,
4753 const char *search_name,
4754 gdb::array_view<const char *> expected_syms)
4755 {
4756 lookup_name_info lookup_name (search_name,
4757 symbol_name_match_type::FULL, true);
4758
4759 auto bounds = index.find_name_components_bounds (lookup_name);
4760
4761 size_t distance = std::distance (bounds.first, bounds.second);
4762 if (distance != expected_syms.size ())
4763 return false;
4764
4765 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4766 {
4767 auto nc_elem = bounds.first + exp_elem;
4768 const char *qualified = index.symbol_name_at (nc_elem->idx);
4769 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4770 return false;
4771 }
4772
4773 return true;
4774 }
4775
4776 /* Test the lower-level mapped_index::find_name_component_bounds
4777 method. */
4778
4779 static void
4780 test_mapped_index_find_name_component_bounds ()
4781 {
4782 mock_mapped_index mock_index (test_symbols);
4783
4784 mock_index.build_name_components ();
4785
4786 /* Test the lower-level mapped_index::find_name_component_bounds
4787 method in completion mode. */
4788 {
4789 static const char *expected_syms[] = {
4790 "t1_func",
4791 "t1_func1",
4792 };
4793
4794 SELF_CHECK (check_find_bounds_finds (mock_index,
4795 "t1_func", expected_syms));
4796 }
4797
4798 /* Check that the increment-last-char in the name matching algorithm
4799 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4800 {
4801 static const char *expected_syms1[] = {
4802 "\377",
4803 "\377\377123",
4804 };
4805 SELF_CHECK (check_find_bounds_finds (mock_index,
4806 "\377", expected_syms1));
4807
4808 static const char *expected_syms2[] = {
4809 "\377\377123",
4810 };
4811 SELF_CHECK (check_find_bounds_finds (mock_index,
4812 "\377\377", expected_syms2));
4813 }
4814 }
4815
4816 /* Test dw2_expand_symtabs_matching_symbol. */
4817
4818 static void
4819 test_dw2_expand_symtabs_matching_symbol ()
4820 {
4821 mock_mapped_index mock_index (test_symbols);
4822
4823 /* We let all tests run until the end even if some fails, for debug
4824 convenience. */
4825 bool any_mismatch = false;
4826
4827 /* Create the expected symbols list (an initializer_list). Needed
4828 because lists have commas, and we need to pass them to CHECK,
4829 which is a macro. */
4830 #define EXPECT(...) { __VA_ARGS__ }
4831
4832 /* Wrapper for check_match that passes down the current
4833 __FILE__/__LINE__. */
4834 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4835 any_mismatch |= !check_match (__FILE__, __LINE__, \
4836 mock_index, \
4837 NAME, MATCH_TYPE, COMPLETION_MODE, \
4838 EXPECTED_LIST)
4839
4840 /* Identity checks. */
4841 for (const char *sym : test_symbols)
4842 {
4843 /* Should be able to match all existing symbols. */
4844 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4845 EXPECT (sym));
4846
4847 /* Should be able to match all existing symbols with
4848 parameters. */
4849 std::string with_params = std::string (sym) + "(int)";
4850 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4851 EXPECT (sym));
4852
4853 /* Should be able to match all existing symbols with
4854 parameters and qualifiers. */
4855 with_params = std::string (sym) + " ( int ) const";
4856 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4857 EXPECT (sym));
4858
4859 /* This should really find sym, but cp-name-parser.y doesn't
4860 know about lvalue/rvalue qualifiers yet. */
4861 with_params = std::string (sym) + " ( int ) &&";
4862 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4863 {});
4864 }
4865
4866 /* Check that the name matching algorithm for completion doesn't get
4867 confused with Latin1 'ÿ' / 0xff. */
4868 {
4869 static const char str[] = "\377";
4870 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4871 EXPECT ("\377", "\377\377123"));
4872 }
4873
4874 /* Check that the increment-last-char in the matching algorithm for
4875 completion doesn't match "t1_fund" when completing "t1_func". */
4876 {
4877 static const char str[] = "t1_func";
4878 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4879 EXPECT ("t1_func", "t1_func1"));
4880 }
4881
4882 /* Check that completion mode works at each prefix of the expected
4883 symbol name. */
4884 {
4885 static const char str[] = "function(int)";
4886 size_t len = strlen (str);
4887 std::string lookup;
4888
4889 for (size_t i = 1; i < len; i++)
4890 {
4891 lookup.assign (str, i);
4892 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4893 EXPECT ("function"));
4894 }
4895 }
4896
4897 /* While "w" is a prefix of both components, the match function
4898 should still only be called once. */
4899 {
4900 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4901 EXPECT ("w1::w2"));
4902 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4903 EXPECT ("w1::w2"));
4904 }
4905
4906 /* Same, with a "complicated" symbol. */
4907 {
4908 static const char str[] = Z_SYM_NAME;
4909 size_t len = strlen (str);
4910 std::string lookup;
4911
4912 for (size_t i = 1; i < len; i++)
4913 {
4914 lookup.assign (str, i);
4915 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4916 EXPECT (Z_SYM_NAME));
4917 }
4918 }
4919
4920 /* In FULL mode, an incomplete symbol doesn't match. */
4921 {
4922 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4923 {});
4924 }
4925
4926 /* A complete symbol with parameters matches any overload, since the
4927 index has no overload info. */
4928 {
4929 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4930 EXPECT ("std::zfunction", "std::zfunction2"));
4931 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4932 EXPECT ("std::zfunction", "std::zfunction2"));
4933 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4934 EXPECT ("std::zfunction", "std::zfunction2"));
4935 }
4936
4937 /* Check that whitespace is ignored appropriately. A symbol with a
4938 template argument list. */
4939 {
4940 static const char expected[] = "ns::foo<int>";
4941 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4942 EXPECT (expected));
4943 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4944 EXPECT (expected));
4945 }
4946
4947 /* Check that whitespace is ignored appropriately. A symbol with a
4948 template argument list that includes a pointer. */
4949 {
4950 static const char expected[] = "ns::foo<char*>";
4951 /* Try both completion and non-completion modes. */
4952 static const bool completion_mode[2] = {false, true};
4953 for (size_t i = 0; i < 2; i++)
4954 {
4955 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4956 completion_mode[i], EXPECT (expected));
4957 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4958 completion_mode[i], EXPECT (expected));
4959
4960 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4961 completion_mode[i], EXPECT (expected));
4962 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4963 completion_mode[i], EXPECT (expected));
4964 }
4965 }
4966
4967 {
4968 /* Check method qualifiers are ignored. */
4969 static const char expected[] = "ns::foo<char*>";
4970 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4971 symbol_name_match_type::FULL, true, EXPECT (expected));
4972 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4973 symbol_name_match_type::FULL, true, EXPECT (expected));
4974 CHECK_MATCH ("foo < char * > ( int ) const",
4975 symbol_name_match_type::WILD, true, EXPECT (expected));
4976 CHECK_MATCH ("foo < char * > ( int ) &&",
4977 symbol_name_match_type::WILD, true, EXPECT (expected));
4978 }
4979
4980 /* Test lookup names that don't match anything. */
4981 {
4982 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4983 {});
4984
4985 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4986 {});
4987 }
4988
4989 /* Some wild matching tests, exercising "(anonymous namespace)",
4990 which should not be confused with a parameter list. */
4991 {
4992 static const char *syms[] = {
4993 "A::B::C",
4994 "B::C",
4995 "C",
4996 "A :: B :: C ( int )",
4997 "B :: C ( int )",
4998 "C ( int )",
4999 };
5000
5001 for (const char *s : syms)
5002 {
5003 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5004 EXPECT ("(anonymous namespace)::A::B::C"));
5005 }
5006 }
5007
5008 {
5009 static const char expected[] = "ns2::tmpl<int>::foo2";
5010 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5011 EXPECT (expected));
5012 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5013 EXPECT (expected));
5014 }
5015
5016 SELF_CHECK (!any_mismatch);
5017
5018 #undef EXPECT
5019 #undef CHECK_MATCH
5020 }
5021
5022 static void
5023 run_test ()
5024 {
5025 test_mapped_index_find_name_component_bounds ();
5026 test_dw2_expand_symtabs_matching_symbol ();
5027 }
5028
5029 }} // namespace selftests::dw2_expand_symtabs_matching
5030
5031 #endif /* GDB_SELF_TEST */
5032
5033 /* If FILE_MATCHER is NULL or if PER_CU has
5034 dwarf2_per_cu_quick_data::MARK set (see
5035 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5036 EXPANSION_NOTIFY on it. */
5037
5038 static void
5039 dw2_expand_symtabs_matching_one
5040 (struct dwarf2_per_cu_data *per_cu,
5041 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5042 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5043 {
5044 if (file_matcher == NULL || per_cu->v.quick->mark)
5045 {
5046 bool symtab_was_null
5047 = (per_cu->v.quick->compunit_symtab == NULL);
5048
5049 dw2_instantiate_symtab (per_cu, false);
5050
5051 if (expansion_notify != NULL
5052 && symtab_was_null
5053 && per_cu->v.quick->compunit_symtab != NULL)
5054 expansion_notify (per_cu->v.quick->compunit_symtab);
5055 }
5056 }
5057
5058 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5059 matched, to expand corresponding CUs that were marked. IDX is the
5060 index of the symbol name that matched. */
5061
5062 static void
5063 dw2_expand_marked_cus
5064 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5065 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5066 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5067 search_domain kind)
5068 {
5069 offset_type *vec, vec_len, vec_idx;
5070 bool global_seen = false;
5071 mapped_index &index = *dwarf2_per_objfile->index_table;
5072
5073 vec = (offset_type *) (index.constant_pool
5074 + MAYBE_SWAP (index.symbol_table[idx].vec));
5075 vec_len = MAYBE_SWAP (vec[0]);
5076 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5077 {
5078 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5079 /* This value is only valid for index versions >= 7. */
5080 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5081 gdb_index_symbol_kind symbol_kind =
5082 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5083 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5084 /* Only check the symbol attributes if they're present.
5085 Indices prior to version 7 don't record them,
5086 and indices >= 7 may elide them for certain symbols
5087 (gold does this). */
5088 int attrs_valid =
5089 (index.version >= 7
5090 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5091
5092 /* Work around gold/15646. */
5093 if (attrs_valid)
5094 {
5095 if (!is_static && global_seen)
5096 continue;
5097 if (!is_static)
5098 global_seen = true;
5099 }
5100
5101 /* Only check the symbol's kind if it has one. */
5102 if (attrs_valid)
5103 {
5104 switch (kind)
5105 {
5106 case VARIABLES_DOMAIN:
5107 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5108 continue;
5109 break;
5110 case FUNCTIONS_DOMAIN:
5111 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5112 continue;
5113 break;
5114 case TYPES_DOMAIN:
5115 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5116 continue;
5117 break;
5118 default:
5119 break;
5120 }
5121 }
5122
5123 /* Don't crash on bad data. */
5124 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5125 + dwarf2_per_objfile->all_type_units.size ()))
5126 {
5127 complaint (_(".gdb_index entry has bad CU index"
5128 " [in module %s]"),
5129 objfile_name (dwarf2_per_objfile->objfile));
5130 continue;
5131 }
5132
5133 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5134 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5135 expansion_notify);
5136 }
5137 }
5138
5139 /* If FILE_MATCHER is non-NULL, set all the
5140 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5141 that match FILE_MATCHER. */
5142
5143 static void
5144 dw_expand_symtabs_matching_file_matcher
5145 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5146 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5147 {
5148 if (file_matcher == NULL)
5149 return;
5150
5151 objfile *const objfile = dwarf2_per_objfile->objfile;
5152
5153 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5154 htab_eq_pointer,
5155 NULL, xcalloc, xfree));
5156 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5157 htab_eq_pointer,
5158 NULL, xcalloc, xfree));
5159
5160 /* The rule is CUs specify all the files, including those used by
5161 any TU, so there's no need to scan TUs here. */
5162
5163 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5164 {
5165 QUIT;
5166
5167 per_cu->v.quick->mark = 0;
5168
5169 /* We only need to look at symtabs not already expanded. */
5170 if (per_cu->v.quick->compunit_symtab)
5171 continue;
5172
5173 quick_file_names *file_data = dw2_get_file_names (per_cu);
5174 if (file_data == NULL)
5175 continue;
5176
5177 if (htab_find (visited_not_found.get (), file_data) != NULL)
5178 continue;
5179 else if (htab_find (visited_found.get (), file_data) != NULL)
5180 {
5181 per_cu->v.quick->mark = 1;
5182 continue;
5183 }
5184
5185 for (int j = 0; j < file_data->num_file_names; ++j)
5186 {
5187 const char *this_real_name;
5188
5189 if (file_matcher (file_data->file_names[j], false))
5190 {
5191 per_cu->v.quick->mark = 1;
5192 break;
5193 }
5194
5195 /* Before we invoke realpath, which can get expensive when many
5196 files are involved, do a quick comparison of the basenames. */
5197 if (!basenames_may_differ
5198 && !file_matcher (lbasename (file_data->file_names[j]),
5199 true))
5200 continue;
5201
5202 this_real_name = dw2_get_real_path (objfile, file_data, j);
5203 if (file_matcher (this_real_name, false))
5204 {
5205 per_cu->v.quick->mark = 1;
5206 break;
5207 }
5208 }
5209
5210 void **slot = htab_find_slot (per_cu->v.quick->mark
5211 ? visited_found.get ()
5212 : visited_not_found.get (),
5213 file_data, INSERT);
5214 *slot = file_data;
5215 }
5216 }
5217
5218 static void
5219 dw2_expand_symtabs_matching
5220 (struct objfile *objfile,
5221 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5222 const lookup_name_info &lookup_name,
5223 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5224 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5225 enum search_domain kind)
5226 {
5227 struct dwarf2_per_objfile *dwarf2_per_objfile
5228 = get_dwarf2_per_objfile (objfile);
5229
5230 /* index_table is NULL if OBJF_READNOW. */
5231 if (!dwarf2_per_objfile->index_table)
5232 return;
5233
5234 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5235
5236 mapped_index &index = *dwarf2_per_objfile->index_table;
5237
5238 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5239 symbol_matcher,
5240 kind, [&] (offset_type idx)
5241 {
5242 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5243 expansion_notify, kind);
5244 });
5245 }
5246
5247 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5248 symtab. */
5249
5250 static struct compunit_symtab *
5251 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5252 CORE_ADDR pc)
5253 {
5254 int i;
5255
5256 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5257 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5258 return cust;
5259
5260 if (cust->includes == NULL)
5261 return NULL;
5262
5263 for (i = 0; cust->includes[i]; ++i)
5264 {
5265 struct compunit_symtab *s = cust->includes[i];
5266
5267 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5268 if (s != NULL)
5269 return s;
5270 }
5271
5272 return NULL;
5273 }
5274
5275 static struct compunit_symtab *
5276 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5277 struct bound_minimal_symbol msymbol,
5278 CORE_ADDR pc,
5279 struct obj_section *section,
5280 int warn_if_readin)
5281 {
5282 struct dwarf2_per_cu_data *data;
5283 struct compunit_symtab *result;
5284
5285 if (!objfile->partial_symtabs->psymtabs_addrmap)
5286 return NULL;
5287
5288 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5289 SECT_OFF_TEXT (objfile));
5290 data = (struct dwarf2_per_cu_data *) addrmap_find
5291 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5292 if (!data)
5293 return NULL;
5294
5295 if (warn_if_readin && data->v.quick->compunit_symtab)
5296 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5297 paddress (get_objfile_arch (objfile), pc));
5298
5299 result
5300 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5301 false),
5302 pc);
5303 gdb_assert (result != NULL);
5304 return result;
5305 }
5306
5307 static void
5308 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5309 void *data, int need_fullname)
5310 {
5311 struct dwarf2_per_objfile *dwarf2_per_objfile
5312 = get_dwarf2_per_objfile (objfile);
5313
5314 if (!dwarf2_per_objfile->filenames_cache)
5315 {
5316 dwarf2_per_objfile->filenames_cache.emplace ();
5317
5318 htab_up visited (htab_create_alloc (10,
5319 htab_hash_pointer, htab_eq_pointer,
5320 NULL, xcalloc, xfree));
5321
5322 /* The rule is CUs specify all the files, including those used
5323 by any TU, so there's no need to scan TUs here. We can
5324 ignore file names coming from already-expanded CUs. */
5325
5326 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5327 {
5328 if (per_cu->v.quick->compunit_symtab)
5329 {
5330 void **slot = htab_find_slot (visited.get (),
5331 per_cu->v.quick->file_names,
5332 INSERT);
5333
5334 *slot = per_cu->v.quick->file_names;
5335 }
5336 }
5337
5338 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5339 {
5340 /* We only need to look at symtabs not already expanded. */
5341 if (per_cu->v.quick->compunit_symtab)
5342 continue;
5343
5344 quick_file_names *file_data = dw2_get_file_names (per_cu);
5345 if (file_data == NULL)
5346 continue;
5347
5348 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5349 if (*slot)
5350 {
5351 /* Already visited. */
5352 continue;
5353 }
5354 *slot = file_data;
5355
5356 for (int j = 0; j < file_data->num_file_names; ++j)
5357 {
5358 const char *filename = file_data->file_names[j];
5359 dwarf2_per_objfile->filenames_cache->seen (filename);
5360 }
5361 }
5362 }
5363
5364 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5365 {
5366 gdb::unique_xmalloc_ptr<char> this_real_name;
5367
5368 if (need_fullname)
5369 this_real_name = gdb_realpath (filename);
5370 (*fun) (filename, this_real_name.get (), data);
5371 });
5372 }
5373
5374 static int
5375 dw2_has_symbols (struct objfile *objfile)
5376 {
5377 return 1;
5378 }
5379
5380 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5381 {
5382 dw2_has_symbols,
5383 dw2_find_last_source_symtab,
5384 dw2_forget_cached_source_info,
5385 dw2_map_symtabs_matching_filename,
5386 dw2_lookup_symbol,
5387 dw2_print_stats,
5388 dw2_dump,
5389 dw2_expand_symtabs_for_function,
5390 dw2_expand_all_symtabs,
5391 dw2_expand_symtabs_with_fullname,
5392 dw2_map_matching_symbols,
5393 dw2_expand_symtabs_matching,
5394 dw2_find_pc_sect_compunit_symtab,
5395 NULL,
5396 dw2_map_symbol_filenames
5397 };
5398
5399 /* DWARF-5 debug_names reader. */
5400
5401 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5402 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5403
5404 /* A helper function that reads the .debug_names section in SECTION
5405 and fills in MAP. FILENAME is the name of the file containing the
5406 section; it is used for error reporting.
5407
5408 Returns true if all went well, false otherwise. */
5409
5410 static bool
5411 read_debug_names_from_section (struct objfile *objfile,
5412 const char *filename,
5413 struct dwarf2_section_info *section,
5414 mapped_debug_names &map)
5415 {
5416 if (dwarf2_section_empty_p (section))
5417 return false;
5418
5419 /* Older elfutils strip versions could keep the section in the main
5420 executable while splitting it for the separate debug info file. */
5421 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5422 return false;
5423
5424 dwarf2_read_section (objfile, section);
5425
5426 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5427
5428 const gdb_byte *addr = section->buffer;
5429
5430 bfd *const abfd = get_section_bfd_owner (section);
5431
5432 unsigned int bytes_read;
5433 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5434 addr += bytes_read;
5435
5436 map.dwarf5_is_dwarf64 = bytes_read != 4;
5437 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5438 if (bytes_read + length != section->size)
5439 {
5440 /* There may be multiple per-CU indices. */
5441 warning (_("Section .debug_names in %s length %s does not match "
5442 "section length %s, ignoring .debug_names."),
5443 filename, plongest (bytes_read + length),
5444 pulongest (section->size));
5445 return false;
5446 }
5447
5448 /* The version number. */
5449 uint16_t version = read_2_bytes (abfd, addr);
5450 addr += 2;
5451 if (version != 5)
5452 {
5453 warning (_("Section .debug_names in %s has unsupported version %d, "
5454 "ignoring .debug_names."),
5455 filename, version);
5456 return false;
5457 }
5458
5459 /* Padding. */
5460 uint16_t padding = read_2_bytes (abfd, addr);
5461 addr += 2;
5462 if (padding != 0)
5463 {
5464 warning (_("Section .debug_names in %s has unsupported padding %d, "
5465 "ignoring .debug_names."),
5466 filename, padding);
5467 return false;
5468 }
5469
5470 /* comp_unit_count - The number of CUs in the CU list. */
5471 map.cu_count = read_4_bytes (abfd, addr);
5472 addr += 4;
5473
5474 /* local_type_unit_count - The number of TUs in the local TU
5475 list. */
5476 map.tu_count = read_4_bytes (abfd, addr);
5477 addr += 4;
5478
5479 /* foreign_type_unit_count - The number of TUs in the foreign TU
5480 list. */
5481 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5482 addr += 4;
5483 if (foreign_tu_count != 0)
5484 {
5485 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5486 "ignoring .debug_names."),
5487 filename, static_cast<unsigned long> (foreign_tu_count));
5488 return false;
5489 }
5490
5491 /* bucket_count - The number of hash buckets in the hash lookup
5492 table. */
5493 map.bucket_count = read_4_bytes (abfd, addr);
5494 addr += 4;
5495
5496 /* name_count - The number of unique names in the index. */
5497 map.name_count = read_4_bytes (abfd, addr);
5498 addr += 4;
5499
5500 /* abbrev_table_size - The size in bytes of the abbreviations
5501 table. */
5502 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5503 addr += 4;
5504
5505 /* augmentation_string_size - The size in bytes of the augmentation
5506 string. This value is rounded up to a multiple of 4. */
5507 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5508 addr += 4;
5509 map.augmentation_is_gdb = ((augmentation_string_size
5510 == sizeof (dwarf5_augmentation))
5511 && memcmp (addr, dwarf5_augmentation,
5512 sizeof (dwarf5_augmentation)) == 0);
5513 augmentation_string_size += (-augmentation_string_size) & 3;
5514 addr += augmentation_string_size;
5515
5516 /* List of CUs */
5517 map.cu_table_reordered = addr;
5518 addr += map.cu_count * map.offset_size;
5519
5520 /* List of Local TUs */
5521 map.tu_table_reordered = addr;
5522 addr += map.tu_count * map.offset_size;
5523
5524 /* Hash Lookup Table */
5525 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5526 addr += map.bucket_count * 4;
5527 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5528 addr += map.name_count * 4;
5529
5530 /* Name Table */
5531 map.name_table_string_offs_reordered = addr;
5532 addr += map.name_count * map.offset_size;
5533 map.name_table_entry_offs_reordered = addr;
5534 addr += map.name_count * map.offset_size;
5535
5536 const gdb_byte *abbrev_table_start = addr;
5537 for (;;)
5538 {
5539 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5540 addr += bytes_read;
5541 if (index_num == 0)
5542 break;
5543
5544 const auto insertpair
5545 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5546 if (!insertpair.second)
5547 {
5548 warning (_("Section .debug_names in %s has duplicate index %s, "
5549 "ignoring .debug_names."),
5550 filename, pulongest (index_num));
5551 return false;
5552 }
5553 mapped_debug_names::index_val &indexval = insertpair.first->second;
5554 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5555 addr += bytes_read;
5556
5557 for (;;)
5558 {
5559 mapped_debug_names::index_val::attr attr;
5560 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5561 addr += bytes_read;
5562 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5563 addr += bytes_read;
5564 if (attr.form == DW_FORM_implicit_const)
5565 {
5566 attr.implicit_const = read_signed_leb128 (abfd, addr,
5567 &bytes_read);
5568 addr += bytes_read;
5569 }
5570 if (attr.dw_idx == 0 && attr.form == 0)
5571 break;
5572 indexval.attr_vec.push_back (std::move (attr));
5573 }
5574 }
5575 if (addr != abbrev_table_start + abbrev_table_size)
5576 {
5577 warning (_("Section .debug_names in %s has abbreviation_table "
5578 "of size %zu vs. written as %u, ignoring .debug_names."),
5579 filename, addr - abbrev_table_start, abbrev_table_size);
5580 return false;
5581 }
5582 map.entry_pool = addr;
5583
5584 return true;
5585 }
5586
5587 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5588 list. */
5589
5590 static void
5591 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5592 const mapped_debug_names &map,
5593 dwarf2_section_info &section,
5594 bool is_dwz)
5595 {
5596 sect_offset sect_off_prev;
5597 for (uint32_t i = 0; i <= map.cu_count; ++i)
5598 {
5599 sect_offset sect_off_next;
5600 if (i < map.cu_count)
5601 {
5602 sect_off_next
5603 = (sect_offset) (extract_unsigned_integer
5604 (map.cu_table_reordered + i * map.offset_size,
5605 map.offset_size,
5606 map.dwarf5_byte_order));
5607 }
5608 else
5609 sect_off_next = (sect_offset) section.size;
5610 if (i >= 1)
5611 {
5612 const ULONGEST length = sect_off_next - sect_off_prev;
5613 dwarf2_per_cu_data *per_cu
5614 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5615 sect_off_prev, length);
5616 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5617 }
5618 sect_off_prev = sect_off_next;
5619 }
5620 }
5621
5622 /* Read the CU list from the mapped index, and use it to create all
5623 the CU objects for this dwarf2_per_objfile. */
5624
5625 static void
5626 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5627 const mapped_debug_names &map,
5628 const mapped_debug_names &dwz_map)
5629 {
5630 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5631 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5632
5633 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5634 dwarf2_per_objfile->info,
5635 false /* is_dwz */);
5636
5637 if (dwz_map.cu_count == 0)
5638 return;
5639
5640 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5641 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5642 true /* is_dwz */);
5643 }
5644
5645 /* Read .debug_names. If everything went ok, initialize the "quick"
5646 elements of all the CUs and return true. Otherwise, return false. */
5647
5648 static bool
5649 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5650 {
5651 std::unique_ptr<mapped_debug_names> map
5652 (new mapped_debug_names (dwarf2_per_objfile));
5653 mapped_debug_names dwz_map (dwarf2_per_objfile);
5654 struct objfile *objfile = dwarf2_per_objfile->objfile;
5655
5656 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5657 &dwarf2_per_objfile->debug_names,
5658 *map))
5659 return false;
5660
5661 /* Don't use the index if it's empty. */
5662 if (map->name_count == 0)
5663 return false;
5664
5665 /* If there is a .dwz file, read it so we can get its CU list as
5666 well. */
5667 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5668 if (dwz != NULL)
5669 {
5670 if (!read_debug_names_from_section (objfile,
5671 bfd_get_filename (dwz->dwz_bfd),
5672 &dwz->debug_names, dwz_map))
5673 {
5674 warning (_("could not read '.debug_names' section from %s; skipping"),
5675 bfd_get_filename (dwz->dwz_bfd));
5676 return false;
5677 }
5678 }
5679
5680 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5681
5682 if (map->tu_count != 0)
5683 {
5684 /* We can only handle a single .debug_types when we have an
5685 index. */
5686 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5687 return false;
5688
5689 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5690 dwarf2_per_objfile->types, 0);
5691
5692 create_signatured_type_table_from_debug_names
5693 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5694 }
5695
5696 create_addrmap_from_aranges (dwarf2_per_objfile,
5697 &dwarf2_per_objfile->debug_aranges);
5698
5699 dwarf2_per_objfile->debug_names_table = std::move (map);
5700 dwarf2_per_objfile->using_index = 1;
5701 dwarf2_per_objfile->quick_file_names_table =
5702 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5703
5704 return true;
5705 }
5706
5707 /* Type used to manage iterating over all CUs looking for a symbol for
5708 .debug_names. */
5709
5710 class dw2_debug_names_iterator
5711 {
5712 public:
5713 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5714 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5715 dw2_debug_names_iterator (const mapped_debug_names &map,
5716 bool want_specific_block,
5717 block_enum block_index, domain_enum domain,
5718 const char *name)
5719 : m_map (map), m_want_specific_block (want_specific_block),
5720 m_block_index (block_index), m_domain (domain),
5721 m_addr (find_vec_in_debug_names (map, name))
5722 {}
5723
5724 dw2_debug_names_iterator (const mapped_debug_names &map,
5725 search_domain search, uint32_t namei)
5726 : m_map (map),
5727 m_search (search),
5728 m_addr (find_vec_in_debug_names (map, namei))
5729 {}
5730
5731 /* Return the next matching CU or NULL if there are no more. */
5732 dwarf2_per_cu_data *next ();
5733
5734 private:
5735 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5736 const char *name);
5737 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5738 uint32_t namei);
5739
5740 /* The internalized form of .debug_names. */
5741 const mapped_debug_names &m_map;
5742
5743 /* If true, only look for symbols that match BLOCK_INDEX. */
5744 const bool m_want_specific_block = false;
5745
5746 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5747 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5748 value. */
5749 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5750
5751 /* The kind of symbol we're looking for. */
5752 const domain_enum m_domain = UNDEF_DOMAIN;
5753 const search_domain m_search = ALL_DOMAIN;
5754
5755 /* The list of CUs from the index entry of the symbol, or NULL if
5756 not found. */
5757 const gdb_byte *m_addr;
5758 };
5759
5760 const char *
5761 mapped_debug_names::namei_to_name (uint32_t namei) const
5762 {
5763 const ULONGEST namei_string_offs
5764 = extract_unsigned_integer ((name_table_string_offs_reordered
5765 + namei * offset_size),
5766 offset_size,
5767 dwarf5_byte_order);
5768 return read_indirect_string_at_offset
5769 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5770 }
5771
5772 /* Find a slot in .debug_names for the object named NAME. If NAME is
5773 found, return pointer to its pool data. If NAME cannot be found,
5774 return NULL. */
5775
5776 const gdb_byte *
5777 dw2_debug_names_iterator::find_vec_in_debug_names
5778 (const mapped_debug_names &map, const char *name)
5779 {
5780 int (*cmp) (const char *, const char *);
5781
5782 if (current_language->la_language == language_cplus
5783 || current_language->la_language == language_fortran
5784 || current_language->la_language == language_d)
5785 {
5786 /* NAME is already canonical. Drop any qualifiers as
5787 .debug_names does not contain any. */
5788
5789 if (strchr (name, '(') != NULL)
5790 {
5791 gdb::unique_xmalloc_ptr<char> without_params
5792 = cp_remove_params (name);
5793
5794 if (without_params != NULL)
5795 {
5796 name = without_params.get();
5797 }
5798 }
5799 }
5800
5801 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5802
5803 const uint32_t full_hash = dwarf5_djb_hash (name);
5804 uint32_t namei
5805 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5806 (map.bucket_table_reordered
5807 + (full_hash % map.bucket_count)), 4,
5808 map.dwarf5_byte_order);
5809 if (namei == 0)
5810 return NULL;
5811 --namei;
5812 if (namei >= map.name_count)
5813 {
5814 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5815 "[in module %s]"),
5816 namei, map.name_count,
5817 objfile_name (map.dwarf2_per_objfile->objfile));
5818 return NULL;
5819 }
5820
5821 for (;;)
5822 {
5823 const uint32_t namei_full_hash
5824 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5825 (map.hash_table_reordered + namei), 4,
5826 map.dwarf5_byte_order);
5827 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5828 return NULL;
5829
5830 if (full_hash == namei_full_hash)
5831 {
5832 const char *const namei_string = map.namei_to_name (namei);
5833
5834 #if 0 /* An expensive sanity check. */
5835 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5836 {
5837 complaint (_("Wrong .debug_names hash for string at index %u "
5838 "[in module %s]"),
5839 namei, objfile_name (dwarf2_per_objfile->objfile));
5840 return NULL;
5841 }
5842 #endif
5843
5844 if (cmp (namei_string, name) == 0)
5845 {
5846 const ULONGEST namei_entry_offs
5847 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5848 + namei * map.offset_size),
5849 map.offset_size, map.dwarf5_byte_order);
5850 return map.entry_pool + namei_entry_offs;
5851 }
5852 }
5853
5854 ++namei;
5855 if (namei >= map.name_count)
5856 return NULL;
5857 }
5858 }
5859
5860 const gdb_byte *
5861 dw2_debug_names_iterator::find_vec_in_debug_names
5862 (const mapped_debug_names &map, uint32_t namei)
5863 {
5864 if (namei >= map.name_count)
5865 {
5866 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5867 "[in module %s]"),
5868 namei, map.name_count,
5869 objfile_name (map.dwarf2_per_objfile->objfile));
5870 return NULL;
5871 }
5872
5873 const ULONGEST namei_entry_offs
5874 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5875 + namei * map.offset_size),
5876 map.offset_size, map.dwarf5_byte_order);
5877 return map.entry_pool + namei_entry_offs;
5878 }
5879
5880 /* See dw2_debug_names_iterator. */
5881
5882 dwarf2_per_cu_data *
5883 dw2_debug_names_iterator::next ()
5884 {
5885 if (m_addr == NULL)
5886 return NULL;
5887
5888 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5889 struct objfile *objfile = dwarf2_per_objfile->objfile;
5890 bfd *const abfd = objfile->obfd;
5891
5892 again:
5893
5894 unsigned int bytes_read;
5895 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5896 m_addr += bytes_read;
5897 if (abbrev == 0)
5898 return NULL;
5899
5900 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5901 if (indexval_it == m_map.abbrev_map.cend ())
5902 {
5903 complaint (_("Wrong .debug_names undefined abbrev code %s "
5904 "[in module %s]"),
5905 pulongest (abbrev), objfile_name (objfile));
5906 return NULL;
5907 }
5908 const mapped_debug_names::index_val &indexval = indexval_it->second;
5909 bool have_is_static = false;
5910 bool is_static;
5911 dwarf2_per_cu_data *per_cu = NULL;
5912 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5913 {
5914 ULONGEST ull;
5915 switch (attr.form)
5916 {
5917 case DW_FORM_implicit_const:
5918 ull = attr.implicit_const;
5919 break;
5920 case DW_FORM_flag_present:
5921 ull = 1;
5922 break;
5923 case DW_FORM_udata:
5924 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5925 m_addr += bytes_read;
5926 break;
5927 default:
5928 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5929 dwarf_form_name (attr.form),
5930 objfile_name (objfile));
5931 return NULL;
5932 }
5933 switch (attr.dw_idx)
5934 {
5935 case DW_IDX_compile_unit:
5936 /* Don't crash on bad data. */
5937 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5938 {
5939 complaint (_(".debug_names entry has bad CU index %s"
5940 " [in module %s]"),
5941 pulongest (ull),
5942 objfile_name (dwarf2_per_objfile->objfile));
5943 continue;
5944 }
5945 per_cu = dwarf2_per_objfile->get_cutu (ull);
5946 break;
5947 case DW_IDX_type_unit:
5948 /* Don't crash on bad data. */
5949 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5950 {
5951 complaint (_(".debug_names entry has bad TU index %s"
5952 " [in module %s]"),
5953 pulongest (ull),
5954 objfile_name (dwarf2_per_objfile->objfile));
5955 continue;
5956 }
5957 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5958 break;
5959 case DW_IDX_GNU_internal:
5960 if (!m_map.augmentation_is_gdb)
5961 break;
5962 have_is_static = true;
5963 is_static = true;
5964 break;
5965 case DW_IDX_GNU_external:
5966 if (!m_map.augmentation_is_gdb)
5967 break;
5968 have_is_static = true;
5969 is_static = false;
5970 break;
5971 }
5972 }
5973
5974 /* Skip if already read in. */
5975 if (per_cu->v.quick->compunit_symtab)
5976 goto again;
5977
5978 /* Check static vs global. */
5979 if (have_is_static)
5980 {
5981 const bool want_static = m_block_index != GLOBAL_BLOCK;
5982 if (m_want_specific_block && want_static != is_static)
5983 goto again;
5984 }
5985
5986 /* Match dw2_symtab_iter_next, symbol_kind
5987 and debug_names::psymbol_tag. */
5988 switch (m_domain)
5989 {
5990 case VAR_DOMAIN:
5991 switch (indexval.dwarf_tag)
5992 {
5993 case DW_TAG_variable:
5994 case DW_TAG_subprogram:
5995 /* Some types are also in VAR_DOMAIN. */
5996 case DW_TAG_typedef:
5997 case DW_TAG_structure_type:
5998 break;
5999 default:
6000 goto again;
6001 }
6002 break;
6003 case STRUCT_DOMAIN:
6004 switch (indexval.dwarf_tag)
6005 {
6006 case DW_TAG_typedef:
6007 case DW_TAG_structure_type:
6008 break;
6009 default:
6010 goto again;
6011 }
6012 break;
6013 case LABEL_DOMAIN:
6014 switch (indexval.dwarf_tag)
6015 {
6016 case 0:
6017 case DW_TAG_variable:
6018 break;
6019 default:
6020 goto again;
6021 }
6022 break;
6023 default:
6024 break;
6025 }
6026
6027 /* Match dw2_expand_symtabs_matching, symbol_kind and
6028 debug_names::psymbol_tag. */
6029 switch (m_search)
6030 {
6031 case VARIABLES_DOMAIN:
6032 switch (indexval.dwarf_tag)
6033 {
6034 case DW_TAG_variable:
6035 break;
6036 default:
6037 goto again;
6038 }
6039 break;
6040 case FUNCTIONS_DOMAIN:
6041 switch (indexval.dwarf_tag)
6042 {
6043 case DW_TAG_subprogram:
6044 break;
6045 default:
6046 goto again;
6047 }
6048 break;
6049 case TYPES_DOMAIN:
6050 switch (indexval.dwarf_tag)
6051 {
6052 case DW_TAG_typedef:
6053 case DW_TAG_structure_type:
6054 break;
6055 default:
6056 goto again;
6057 }
6058 break;
6059 default:
6060 break;
6061 }
6062
6063 return per_cu;
6064 }
6065
6066 static struct compunit_symtab *
6067 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6068 const char *name, domain_enum domain)
6069 {
6070 const block_enum block_index = static_cast<block_enum> (block_index_int);
6071 struct dwarf2_per_objfile *dwarf2_per_objfile
6072 = get_dwarf2_per_objfile (objfile);
6073
6074 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6075 if (!mapp)
6076 {
6077 /* index is NULL if OBJF_READNOW. */
6078 return NULL;
6079 }
6080 const auto &map = *mapp;
6081
6082 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6083 block_index, domain, name);
6084
6085 struct compunit_symtab *stab_best = NULL;
6086 struct dwarf2_per_cu_data *per_cu;
6087 while ((per_cu = iter.next ()) != NULL)
6088 {
6089 struct symbol *sym, *with_opaque = NULL;
6090 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6091 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6092 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6093
6094 sym = block_find_symbol (block, name, domain,
6095 block_find_non_opaque_type_preferred,
6096 &with_opaque);
6097
6098 /* Some caution must be observed with overloaded functions and
6099 methods, since the index will not contain any overload
6100 information (but NAME might contain it). */
6101
6102 if (sym != NULL
6103 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6104 return stab;
6105 if (with_opaque != NULL
6106 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6107 stab_best = stab;
6108
6109 /* Keep looking through other CUs. */
6110 }
6111
6112 return stab_best;
6113 }
6114
6115 /* This dumps minimal information about .debug_names. It is called
6116 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6117 uses this to verify that .debug_names has been loaded. */
6118
6119 static void
6120 dw2_debug_names_dump (struct objfile *objfile)
6121 {
6122 struct dwarf2_per_objfile *dwarf2_per_objfile
6123 = get_dwarf2_per_objfile (objfile);
6124
6125 gdb_assert (dwarf2_per_objfile->using_index);
6126 printf_filtered (".debug_names:");
6127 if (dwarf2_per_objfile->debug_names_table)
6128 printf_filtered (" exists\n");
6129 else
6130 printf_filtered (" faked for \"readnow\"\n");
6131 printf_filtered ("\n");
6132 }
6133
6134 static void
6135 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6136 const char *func_name)
6137 {
6138 struct dwarf2_per_objfile *dwarf2_per_objfile
6139 = get_dwarf2_per_objfile (objfile);
6140
6141 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6142 if (dwarf2_per_objfile->debug_names_table)
6143 {
6144 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6145
6146 /* Note: It doesn't matter what we pass for block_index here. */
6147 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6148 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6149
6150 struct dwarf2_per_cu_data *per_cu;
6151 while ((per_cu = iter.next ()) != NULL)
6152 dw2_instantiate_symtab (per_cu, false);
6153 }
6154 }
6155
6156 static void
6157 dw2_debug_names_expand_symtabs_matching
6158 (struct objfile *objfile,
6159 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6160 const lookup_name_info &lookup_name,
6161 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6162 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6163 enum search_domain kind)
6164 {
6165 struct dwarf2_per_objfile *dwarf2_per_objfile
6166 = get_dwarf2_per_objfile (objfile);
6167
6168 /* debug_names_table is NULL if OBJF_READNOW. */
6169 if (!dwarf2_per_objfile->debug_names_table)
6170 return;
6171
6172 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6173
6174 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6175
6176 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6177 symbol_matcher,
6178 kind, [&] (offset_type namei)
6179 {
6180 /* The name was matched, now expand corresponding CUs that were
6181 marked. */
6182 dw2_debug_names_iterator iter (map, kind, namei);
6183
6184 struct dwarf2_per_cu_data *per_cu;
6185 while ((per_cu = iter.next ()) != NULL)
6186 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6187 expansion_notify);
6188 });
6189 }
6190
6191 const struct quick_symbol_functions dwarf2_debug_names_functions =
6192 {
6193 dw2_has_symbols,
6194 dw2_find_last_source_symtab,
6195 dw2_forget_cached_source_info,
6196 dw2_map_symtabs_matching_filename,
6197 dw2_debug_names_lookup_symbol,
6198 dw2_print_stats,
6199 dw2_debug_names_dump,
6200 dw2_debug_names_expand_symtabs_for_function,
6201 dw2_expand_all_symtabs,
6202 dw2_expand_symtabs_with_fullname,
6203 dw2_map_matching_symbols,
6204 dw2_debug_names_expand_symtabs_matching,
6205 dw2_find_pc_sect_compunit_symtab,
6206 NULL,
6207 dw2_map_symbol_filenames
6208 };
6209
6210 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6211 to either a dwarf2_per_objfile or dwz_file object. */
6212
6213 template <typename T>
6214 static gdb::array_view<const gdb_byte>
6215 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6216 {
6217 dwarf2_section_info *section = &section_owner->gdb_index;
6218
6219 if (dwarf2_section_empty_p (section))
6220 return {};
6221
6222 /* Older elfutils strip versions could keep the section in the main
6223 executable while splitting it for the separate debug info file. */
6224 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6225 return {};
6226
6227 dwarf2_read_section (obj, section);
6228
6229 /* dwarf2_section_info::size is a bfd_size_type, while
6230 gdb::array_view works with size_t. On 32-bit hosts, with
6231 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6232 is 32-bit. So we need an explicit narrowing conversion here.
6233 This is fine, because it's impossible to allocate or mmap an
6234 array/buffer larger than what size_t can represent. */
6235 return gdb::make_array_view (section->buffer, section->size);
6236 }
6237
6238 /* Lookup the index cache for the contents of the index associated to
6239 DWARF2_OBJ. */
6240
6241 static gdb::array_view<const gdb_byte>
6242 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6243 {
6244 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6245 if (build_id == nullptr)
6246 return {};
6247
6248 return global_index_cache.lookup_gdb_index (build_id,
6249 &dwarf2_obj->index_cache_res);
6250 }
6251
6252 /* Same as the above, but for DWZ. */
6253
6254 static gdb::array_view<const gdb_byte>
6255 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6256 {
6257 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6258 if (build_id == nullptr)
6259 return {};
6260
6261 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6262 }
6263
6264 /* See symfile.h. */
6265
6266 bool
6267 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6268 {
6269 struct dwarf2_per_objfile *dwarf2_per_objfile
6270 = get_dwarf2_per_objfile (objfile);
6271
6272 /* If we're about to read full symbols, don't bother with the
6273 indices. In this case we also don't care if some other debug
6274 format is making psymtabs, because they are all about to be
6275 expanded anyway. */
6276 if ((objfile->flags & OBJF_READNOW))
6277 {
6278 dwarf2_per_objfile->using_index = 1;
6279 create_all_comp_units (dwarf2_per_objfile);
6280 create_all_type_units (dwarf2_per_objfile);
6281 dwarf2_per_objfile->quick_file_names_table
6282 = create_quick_file_names_table
6283 (dwarf2_per_objfile->all_comp_units.size ());
6284
6285 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6286 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6287 {
6288 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6289
6290 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6291 struct dwarf2_per_cu_quick_data);
6292 }
6293
6294 /* Return 1 so that gdb sees the "quick" functions. However,
6295 these functions will be no-ops because we will have expanded
6296 all symtabs. */
6297 *index_kind = dw_index_kind::GDB_INDEX;
6298 return true;
6299 }
6300
6301 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6302 {
6303 *index_kind = dw_index_kind::DEBUG_NAMES;
6304 return true;
6305 }
6306
6307 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6308 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6309 get_gdb_index_contents_from_section<dwz_file>))
6310 {
6311 *index_kind = dw_index_kind::GDB_INDEX;
6312 return true;
6313 }
6314
6315 /* ... otherwise, try to find the index in the index cache. */
6316 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6317 get_gdb_index_contents_from_cache,
6318 get_gdb_index_contents_from_cache_dwz))
6319 {
6320 global_index_cache.hit ();
6321 *index_kind = dw_index_kind::GDB_INDEX;
6322 return true;
6323 }
6324
6325 global_index_cache.miss ();
6326 return false;
6327 }
6328
6329 \f
6330
6331 /* Build a partial symbol table. */
6332
6333 void
6334 dwarf2_build_psymtabs (struct objfile *objfile)
6335 {
6336 struct dwarf2_per_objfile *dwarf2_per_objfile
6337 = get_dwarf2_per_objfile (objfile);
6338
6339 init_psymbol_list (objfile, 1024);
6340
6341 TRY
6342 {
6343 /* This isn't really ideal: all the data we allocate on the
6344 objfile's obstack is still uselessly kept around. However,
6345 freeing it seems unsafe. */
6346 psymtab_discarder psymtabs (objfile);
6347 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6348 psymtabs.keep ();
6349
6350 /* (maybe) store an index in the cache. */
6351 global_index_cache.store (dwarf2_per_objfile);
6352 }
6353 CATCH (except, RETURN_MASK_ERROR)
6354 {
6355 exception_print (gdb_stderr, except);
6356 }
6357 END_CATCH
6358 }
6359
6360 /* Return the total length of the CU described by HEADER. */
6361
6362 static unsigned int
6363 get_cu_length (const struct comp_unit_head *header)
6364 {
6365 return header->initial_length_size + header->length;
6366 }
6367
6368 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6369
6370 static inline bool
6371 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6372 {
6373 sect_offset bottom = cu_header->sect_off;
6374 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6375
6376 return sect_off >= bottom && sect_off < top;
6377 }
6378
6379 /* Find the base address of the compilation unit for range lists and
6380 location lists. It will normally be specified by DW_AT_low_pc.
6381 In DWARF-3 draft 4, the base address could be overridden by
6382 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6383 compilation units with discontinuous ranges. */
6384
6385 static void
6386 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6387 {
6388 struct attribute *attr;
6389
6390 cu->base_known = 0;
6391 cu->base_address = 0;
6392
6393 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6394 if (attr)
6395 {
6396 cu->base_address = attr_value_as_address (attr);
6397 cu->base_known = 1;
6398 }
6399 else
6400 {
6401 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6402 if (attr)
6403 {
6404 cu->base_address = attr_value_as_address (attr);
6405 cu->base_known = 1;
6406 }
6407 }
6408 }
6409
6410 /* Read in the comp unit header information from the debug_info at info_ptr.
6411 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6412 NOTE: This leaves members offset, first_die_offset to be filled in
6413 by the caller. */
6414
6415 static const gdb_byte *
6416 read_comp_unit_head (struct comp_unit_head *cu_header,
6417 const gdb_byte *info_ptr,
6418 struct dwarf2_section_info *section,
6419 rcuh_kind section_kind)
6420 {
6421 int signed_addr;
6422 unsigned int bytes_read;
6423 const char *filename = get_section_file_name (section);
6424 bfd *abfd = get_section_bfd_owner (section);
6425
6426 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6427 cu_header->initial_length_size = bytes_read;
6428 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6429 info_ptr += bytes_read;
6430 cu_header->version = read_2_bytes (abfd, info_ptr);
6431 if (cu_header->version < 2 || cu_header->version > 5)
6432 error (_("Dwarf Error: wrong version in compilation unit header "
6433 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6434 cu_header->version, filename);
6435 info_ptr += 2;
6436 if (cu_header->version < 5)
6437 switch (section_kind)
6438 {
6439 case rcuh_kind::COMPILE:
6440 cu_header->unit_type = DW_UT_compile;
6441 break;
6442 case rcuh_kind::TYPE:
6443 cu_header->unit_type = DW_UT_type;
6444 break;
6445 default:
6446 internal_error (__FILE__, __LINE__,
6447 _("read_comp_unit_head: invalid section_kind"));
6448 }
6449 else
6450 {
6451 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6452 (read_1_byte (abfd, info_ptr));
6453 info_ptr += 1;
6454 switch (cu_header->unit_type)
6455 {
6456 case DW_UT_compile:
6457 if (section_kind != rcuh_kind::COMPILE)
6458 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6459 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6460 filename);
6461 break;
6462 case DW_UT_type:
6463 section_kind = rcuh_kind::TYPE;
6464 break;
6465 default:
6466 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6467 "(is %d, should be %d or %d) [in module %s]"),
6468 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6469 }
6470
6471 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6472 info_ptr += 1;
6473 }
6474 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6475 cu_header,
6476 &bytes_read);
6477 info_ptr += bytes_read;
6478 if (cu_header->version < 5)
6479 {
6480 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6481 info_ptr += 1;
6482 }
6483 signed_addr = bfd_get_sign_extend_vma (abfd);
6484 if (signed_addr < 0)
6485 internal_error (__FILE__, __LINE__,
6486 _("read_comp_unit_head: dwarf from non elf file"));
6487 cu_header->signed_addr_p = signed_addr;
6488
6489 if (section_kind == rcuh_kind::TYPE)
6490 {
6491 LONGEST type_offset;
6492
6493 cu_header->signature = read_8_bytes (abfd, info_ptr);
6494 info_ptr += 8;
6495
6496 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6497 info_ptr += bytes_read;
6498 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6499 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6500 error (_("Dwarf Error: Too big type_offset in compilation unit "
6501 "header (is %s) [in module %s]"), plongest (type_offset),
6502 filename);
6503 }
6504
6505 return info_ptr;
6506 }
6507
6508 /* Helper function that returns the proper abbrev section for
6509 THIS_CU. */
6510
6511 static struct dwarf2_section_info *
6512 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6513 {
6514 struct dwarf2_section_info *abbrev;
6515 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6516
6517 if (this_cu->is_dwz)
6518 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6519 else
6520 abbrev = &dwarf2_per_objfile->abbrev;
6521
6522 return abbrev;
6523 }
6524
6525 /* Subroutine of read_and_check_comp_unit_head and
6526 read_and_check_type_unit_head to simplify them.
6527 Perform various error checking on the header. */
6528
6529 static void
6530 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6531 struct comp_unit_head *header,
6532 struct dwarf2_section_info *section,
6533 struct dwarf2_section_info *abbrev_section)
6534 {
6535 const char *filename = get_section_file_name (section);
6536
6537 if (to_underlying (header->abbrev_sect_off)
6538 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6539 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6540 "(offset %s + 6) [in module %s]"),
6541 sect_offset_str (header->abbrev_sect_off),
6542 sect_offset_str (header->sect_off),
6543 filename);
6544
6545 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6546 avoid potential 32-bit overflow. */
6547 if (((ULONGEST) header->sect_off + get_cu_length (header))
6548 > section->size)
6549 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6550 "(offset %s + 0) [in module %s]"),
6551 header->length, sect_offset_str (header->sect_off),
6552 filename);
6553 }
6554
6555 /* Read in a CU/TU header and perform some basic error checking.
6556 The contents of the header are stored in HEADER.
6557 The result is a pointer to the start of the first DIE. */
6558
6559 static const gdb_byte *
6560 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6561 struct comp_unit_head *header,
6562 struct dwarf2_section_info *section,
6563 struct dwarf2_section_info *abbrev_section,
6564 const gdb_byte *info_ptr,
6565 rcuh_kind section_kind)
6566 {
6567 const gdb_byte *beg_of_comp_unit = info_ptr;
6568
6569 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6570
6571 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6572
6573 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6574
6575 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6576 abbrev_section);
6577
6578 return info_ptr;
6579 }
6580
6581 /* Fetch the abbreviation table offset from a comp or type unit header. */
6582
6583 static sect_offset
6584 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6585 struct dwarf2_section_info *section,
6586 sect_offset sect_off)
6587 {
6588 bfd *abfd = get_section_bfd_owner (section);
6589 const gdb_byte *info_ptr;
6590 unsigned int initial_length_size, offset_size;
6591 uint16_t version;
6592
6593 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6594 info_ptr = section->buffer + to_underlying (sect_off);
6595 read_initial_length (abfd, info_ptr, &initial_length_size);
6596 offset_size = initial_length_size == 4 ? 4 : 8;
6597 info_ptr += initial_length_size;
6598
6599 version = read_2_bytes (abfd, info_ptr);
6600 info_ptr += 2;
6601 if (version >= 5)
6602 {
6603 /* Skip unit type and address size. */
6604 info_ptr += 2;
6605 }
6606
6607 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6608 }
6609
6610 /* Allocate a new partial symtab for file named NAME and mark this new
6611 partial symtab as being an include of PST. */
6612
6613 static void
6614 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6615 struct objfile *objfile)
6616 {
6617 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6618
6619 if (!IS_ABSOLUTE_PATH (subpst->filename))
6620 {
6621 /* It shares objfile->objfile_obstack. */
6622 subpst->dirname = pst->dirname;
6623 }
6624
6625 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6626 subpst->dependencies[0] = pst;
6627 subpst->number_of_dependencies = 1;
6628
6629 subpst->read_symtab = pst->read_symtab;
6630
6631 /* No private part is necessary for include psymtabs. This property
6632 can be used to differentiate between such include psymtabs and
6633 the regular ones. */
6634 subpst->read_symtab_private = NULL;
6635 }
6636
6637 /* Read the Line Number Program data and extract the list of files
6638 included by the source file represented by PST. Build an include
6639 partial symtab for each of these included files. */
6640
6641 static void
6642 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6643 struct die_info *die,
6644 struct partial_symtab *pst)
6645 {
6646 line_header_up lh;
6647 struct attribute *attr;
6648
6649 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6650 if (attr)
6651 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6652 if (lh == NULL)
6653 return; /* No linetable, so no includes. */
6654
6655 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6656 that we pass in the raw text_low here; that is ok because we're
6657 only decoding the line table to make include partial symtabs, and
6658 so the addresses aren't really used. */
6659 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6660 pst->raw_text_low (), 1);
6661 }
6662
6663 static hashval_t
6664 hash_signatured_type (const void *item)
6665 {
6666 const struct signatured_type *sig_type
6667 = (const struct signatured_type *) item;
6668
6669 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6670 return sig_type->signature;
6671 }
6672
6673 static int
6674 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6675 {
6676 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6677 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6678
6679 return lhs->signature == rhs->signature;
6680 }
6681
6682 /* Allocate a hash table for signatured types. */
6683
6684 static htab_t
6685 allocate_signatured_type_table (struct objfile *objfile)
6686 {
6687 return htab_create_alloc_ex (41,
6688 hash_signatured_type,
6689 eq_signatured_type,
6690 NULL,
6691 &objfile->objfile_obstack,
6692 hashtab_obstack_allocate,
6693 dummy_obstack_deallocate);
6694 }
6695
6696 /* A helper function to add a signatured type CU to a table. */
6697
6698 static int
6699 add_signatured_type_cu_to_table (void **slot, void *datum)
6700 {
6701 struct signatured_type *sigt = (struct signatured_type *) *slot;
6702 std::vector<signatured_type *> *all_type_units
6703 = (std::vector<signatured_type *> *) datum;
6704
6705 all_type_units->push_back (sigt);
6706
6707 return 1;
6708 }
6709
6710 /* A helper for create_debug_types_hash_table. Read types from SECTION
6711 and fill them into TYPES_HTAB. It will process only type units,
6712 therefore DW_UT_type. */
6713
6714 static void
6715 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6716 struct dwo_file *dwo_file,
6717 dwarf2_section_info *section, htab_t &types_htab,
6718 rcuh_kind section_kind)
6719 {
6720 struct objfile *objfile = dwarf2_per_objfile->objfile;
6721 struct dwarf2_section_info *abbrev_section;
6722 bfd *abfd;
6723 const gdb_byte *info_ptr, *end_ptr;
6724
6725 abbrev_section = (dwo_file != NULL
6726 ? &dwo_file->sections.abbrev
6727 : &dwarf2_per_objfile->abbrev);
6728
6729 if (dwarf_read_debug)
6730 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6731 get_section_name (section),
6732 get_section_file_name (abbrev_section));
6733
6734 dwarf2_read_section (objfile, section);
6735 info_ptr = section->buffer;
6736
6737 if (info_ptr == NULL)
6738 return;
6739
6740 /* We can't set abfd until now because the section may be empty or
6741 not present, in which case the bfd is unknown. */
6742 abfd = get_section_bfd_owner (section);
6743
6744 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6745 because we don't need to read any dies: the signature is in the
6746 header. */
6747
6748 end_ptr = info_ptr + section->size;
6749 while (info_ptr < end_ptr)
6750 {
6751 struct signatured_type *sig_type;
6752 struct dwo_unit *dwo_tu;
6753 void **slot;
6754 const gdb_byte *ptr = info_ptr;
6755 struct comp_unit_head header;
6756 unsigned int length;
6757
6758 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6759
6760 /* Initialize it due to a false compiler warning. */
6761 header.signature = -1;
6762 header.type_cu_offset_in_tu = (cu_offset) -1;
6763
6764 /* We need to read the type's signature in order to build the hash
6765 table, but we don't need anything else just yet. */
6766
6767 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6768 abbrev_section, ptr, section_kind);
6769
6770 length = get_cu_length (&header);
6771
6772 /* Skip dummy type units. */
6773 if (ptr >= info_ptr + length
6774 || peek_abbrev_code (abfd, ptr) == 0
6775 || header.unit_type != DW_UT_type)
6776 {
6777 info_ptr += length;
6778 continue;
6779 }
6780
6781 if (types_htab == NULL)
6782 {
6783 if (dwo_file)
6784 types_htab = allocate_dwo_unit_table (objfile);
6785 else
6786 types_htab = allocate_signatured_type_table (objfile);
6787 }
6788
6789 if (dwo_file)
6790 {
6791 sig_type = NULL;
6792 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6793 struct dwo_unit);
6794 dwo_tu->dwo_file = dwo_file;
6795 dwo_tu->signature = header.signature;
6796 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6797 dwo_tu->section = section;
6798 dwo_tu->sect_off = sect_off;
6799 dwo_tu->length = length;
6800 }
6801 else
6802 {
6803 /* N.B.: type_offset is not usable if this type uses a DWO file.
6804 The real type_offset is in the DWO file. */
6805 dwo_tu = NULL;
6806 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6807 struct signatured_type);
6808 sig_type->signature = header.signature;
6809 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6810 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6811 sig_type->per_cu.is_debug_types = 1;
6812 sig_type->per_cu.section = section;
6813 sig_type->per_cu.sect_off = sect_off;
6814 sig_type->per_cu.length = length;
6815 }
6816
6817 slot = htab_find_slot (types_htab,
6818 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6819 INSERT);
6820 gdb_assert (slot != NULL);
6821 if (*slot != NULL)
6822 {
6823 sect_offset dup_sect_off;
6824
6825 if (dwo_file)
6826 {
6827 const struct dwo_unit *dup_tu
6828 = (const struct dwo_unit *) *slot;
6829
6830 dup_sect_off = dup_tu->sect_off;
6831 }
6832 else
6833 {
6834 const struct signatured_type *dup_tu
6835 = (const struct signatured_type *) *slot;
6836
6837 dup_sect_off = dup_tu->per_cu.sect_off;
6838 }
6839
6840 complaint (_("debug type entry at offset %s is duplicate to"
6841 " the entry at offset %s, signature %s"),
6842 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6843 hex_string (header.signature));
6844 }
6845 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6846
6847 if (dwarf_read_debug > 1)
6848 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6849 sect_offset_str (sect_off),
6850 hex_string (header.signature));
6851
6852 info_ptr += length;
6853 }
6854 }
6855
6856 /* Create the hash table of all entries in the .debug_types
6857 (or .debug_types.dwo) section(s).
6858 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6859 otherwise it is NULL.
6860
6861 The result is a pointer to the hash table or NULL if there are no types.
6862
6863 Note: This function processes DWO files only, not DWP files. */
6864
6865 static void
6866 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6867 struct dwo_file *dwo_file,
6868 VEC (dwarf2_section_info_def) *types,
6869 htab_t &types_htab)
6870 {
6871 int ix;
6872 struct dwarf2_section_info *section;
6873
6874 if (VEC_empty (dwarf2_section_info_def, types))
6875 return;
6876
6877 for (ix = 0;
6878 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6879 ++ix)
6880 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6881 types_htab, rcuh_kind::TYPE);
6882 }
6883
6884 /* Create the hash table of all entries in the .debug_types section,
6885 and initialize all_type_units.
6886 The result is zero if there is an error (e.g. missing .debug_types section),
6887 otherwise non-zero. */
6888
6889 static int
6890 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6891 {
6892 htab_t types_htab = NULL;
6893
6894 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6895 &dwarf2_per_objfile->info, types_htab,
6896 rcuh_kind::COMPILE);
6897 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6898 dwarf2_per_objfile->types, types_htab);
6899 if (types_htab == NULL)
6900 {
6901 dwarf2_per_objfile->signatured_types = NULL;
6902 return 0;
6903 }
6904
6905 dwarf2_per_objfile->signatured_types = types_htab;
6906
6907 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6908 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6909
6910 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6911 &dwarf2_per_objfile->all_type_units);
6912
6913 return 1;
6914 }
6915
6916 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6917 If SLOT is non-NULL, it is the entry to use in the hash table.
6918 Otherwise we find one. */
6919
6920 static struct signatured_type *
6921 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6922 void **slot)
6923 {
6924 struct objfile *objfile = dwarf2_per_objfile->objfile;
6925
6926 if (dwarf2_per_objfile->all_type_units.size ()
6927 == dwarf2_per_objfile->all_type_units.capacity ())
6928 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6929
6930 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6931 struct signatured_type);
6932
6933 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6934 sig_type->signature = sig;
6935 sig_type->per_cu.is_debug_types = 1;
6936 if (dwarf2_per_objfile->using_index)
6937 {
6938 sig_type->per_cu.v.quick =
6939 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6940 struct dwarf2_per_cu_quick_data);
6941 }
6942
6943 if (slot == NULL)
6944 {
6945 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6946 sig_type, INSERT);
6947 }
6948 gdb_assert (*slot == NULL);
6949 *slot = sig_type;
6950 /* The rest of sig_type must be filled in by the caller. */
6951 return sig_type;
6952 }
6953
6954 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6955 Fill in SIG_ENTRY with DWO_ENTRY. */
6956
6957 static void
6958 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6959 struct signatured_type *sig_entry,
6960 struct dwo_unit *dwo_entry)
6961 {
6962 /* Make sure we're not clobbering something we don't expect to. */
6963 gdb_assert (! sig_entry->per_cu.queued);
6964 gdb_assert (sig_entry->per_cu.cu == NULL);
6965 if (dwarf2_per_objfile->using_index)
6966 {
6967 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6968 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6969 }
6970 else
6971 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6972 gdb_assert (sig_entry->signature == dwo_entry->signature);
6973 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6974 gdb_assert (sig_entry->type_unit_group == NULL);
6975 gdb_assert (sig_entry->dwo_unit == NULL);
6976
6977 sig_entry->per_cu.section = dwo_entry->section;
6978 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6979 sig_entry->per_cu.length = dwo_entry->length;
6980 sig_entry->per_cu.reading_dwo_directly = 1;
6981 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6982 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6983 sig_entry->dwo_unit = dwo_entry;
6984 }
6985
6986 /* Subroutine of lookup_signatured_type.
6987 If we haven't read the TU yet, create the signatured_type data structure
6988 for a TU to be read in directly from a DWO file, bypassing the stub.
6989 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6990 using .gdb_index, then when reading a CU we want to stay in the DWO file
6991 containing that CU. Otherwise we could end up reading several other DWO
6992 files (due to comdat folding) to process the transitive closure of all the
6993 mentioned TUs, and that can be slow. The current DWO file will have every
6994 type signature that it needs.
6995 We only do this for .gdb_index because in the psymtab case we already have
6996 to read all the DWOs to build the type unit groups. */
6997
6998 static struct signatured_type *
6999 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7000 {
7001 struct dwarf2_per_objfile *dwarf2_per_objfile
7002 = cu->per_cu->dwarf2_per_objfile;
7003 struct objfile *objfile = dwarf2_per_objfile->objfile;
7004 struct dwo_file *dwo_file;
7005 struct dwo_unit find_dwo_entry, *dwo_entry;
7006 struct signatured_type find_sig_entry, *sig_entry;
7007 void **slot;
7008
7009 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7010
7011 /* If TU skeletons have been removed then we may not have read in any
7012 TUs yet. */
7013 if (dwarf2_per_objfile->signatured_types == NULL)
7014 {
7015 dwarf2_per_objfile->signatured_types
7016 = allocate_signatured_type_table (objfile);
7017 }
7018
7019 /* We only ever need to read in one copy of a signatured type.
7020 Use the global signatured_types array to do our own comdat-folding
7021 of types. If this is the first time we're reading this TU, and
7022 the TU has an entry in .gdb_index, replace the recorded data from
7023 .gdb_index with this TU. */
7024
7025 find_sig_entry.signature = sig;
7026 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7027 &find_sig_entry, INSERT);
7028 sig_entry = (struct signatured_type *) *slot;
7029
7030 /* We can get here with the TU already read, *or* in the process of being
7031 read. Don't reassign the global entry to point to this DWO if that's
7032 the case. Also note that if the TU is already being read, it may not
7033 have come from a DWO, the program may be a mix of Fission-compiled
7034 code and non-Fission-compiled code. */
7035
7036 /* Have we already tried to read this TU?
7037 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7038 needn't exist in the global table yet). */
7039 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7040 return sig_entry;
7041
7042 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7043 dwo_unit of the TU itself. */
7044 dwo_file = cu->dwo_unit->dwo_file;
7045
7046 /* Ok, this is the first time we're reading this TU. */
7047 if (dwo_file->tus == NULL)
7048 return NULL;
7049 find_dwo_entry.signature = sig;
7050 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7051 if (dwo_entry == NULL)
7052 return NULL;
7053
7054 /* If the global table doesn't have an entry for this TU, add one. */
7055 if (sig_entry == NULL)
7056 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7057
7058 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7059 sig_entry->per_cu.tu_read = 1;
7060 return sig_entry;
7061 }
7062
7063 /* Subroutine of lookup_signatured_type.
7064 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7065 then try the DWP file. If the TU stub (skeleton) has been removed then
7066 it won't be in .gdb_index. */
7067
7068 static struct signatured_type *
7069 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7070 {
7071 struct dwarf2_per_objfile *dwarf2_per_objfile
7072 = cu->per_cu->dwarf2_per_objfile;
7073 struct objfile *objfile = dwarf2_per_objfile->objfile;
7074 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7075 struct dwo_unit *dwo_entry;
7076 struct signatured_type find_sig_entry, *sig_entry;
7077 void **slot;
7078
7079 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7080 gdb_assert (dwp_file != NULL);
7081
7082 /* If TU skeletons have been removed then we may not have read in any
7083 TUs yet. */
7084 if (dwarf2_per_objfile->signatured_types == NULL)
7085 {
7086 dwarf2_per_objfile->signatured_types
7087 = allocate_signatured_type_table (objfile);
7088 }
7089
7090 find_sig_entry.signature = sig;
7091 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7092 &find_sig_entry, INSERT);
7093 sig_entry = (struct signatured_type *) *slot;
7094
7095 /* Have we already tried to read this TU?
7096 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7097 needn't exist in the global table yet). */
7098 if (sig_entry != NULL)
7099 return sig_entry;
7100
7101 if (dwp_file->tus == NULL)
7102 return NULL;
7103 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7104 sig, 1 /* is_debug_types */);
7105 if (dwo_entry == NULL)
7106 return NULL;
7107
7108 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7109 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7110
7111 return sig_entry;
7112 }
7113
7114 /* Lookup a signature based type for DW_FORM_ref_sig8.
7115 Returns NULL if signature SIG is not present in the table.
7116 It is up to the caller to complain about this. */
7117
7118 static struct signatured_type *
7119 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7120 {
7121 struct dwarf2_per_objfile *dwarf2_per_objfile
7122 = cu->per_cu->dwarf2_per_objfile;
7123
7124 if (cu->dwo_unit
7125 && dwarf2_per_objfile->using_index)
7126 {
7127 /* We're in a DWO/DWP file, and we're using .gdb_index.
7128 These cases require special processing. */
7129 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7130 return lookup_dwo_signatured_type (cu, sig);
7131 else
7132 return lookup_dwp_signatured_type (cu, sig);
7133 }
7134 else
7135 {
7136 struct signatured_type find_entry, *entry;
7137
7138 if (dwarf2_per_objfile->signatured_types == NULL)
7139 return NULL;
7140 find_entry.signature = sig;
7141 entry = ((struct signatured_type *)
7142 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7143 return entry;
7144 }
7145 }
7146 \f
7147 /* Low level DIE reading support. */
7148
7149 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7150
7151 static void
7152 init_cu_die_reader (struct die_reader_specs *reader,
7153 struct dwarf2_cu *cu,
7154 struct dwarf2_section_info *section,
7155 struct dwo_file *dwo_file,
7156 struct abbrev_table *abbrev_table)
7157 {
7158 gdb_assert (section->readin && section->buffer != NULL);
7159 reader->abfd = get_section_bfd_owner (section);
7160 reader->cu = cu;
7161 reader->dwo_file = dwo_file;
7162 reader->die_section = section;
7163 reader->buffer = section->buffer;
7164 reader->buffer_end = section->buffer + section->size;
7165 reader->comp_dir = NULL;
7166 reader->abbrev_table = abbrev_table;
7167 }
7168
7169 /* Subroutine of init_cutu_and_read_dies to simplify it.
7170 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7171 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7172 already.
7173
7174 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7175 from it to the DIE in the DWO. If NULL we are skipping the stub.
7176 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7177 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7178 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7179 STUB_COMP_DIR may be non-NULL.
7180 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7181 are filled in with the info of the DIE from the DWO file.
7182 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7183 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7184 kept around for at least as long as *RESULT_READER.
7185
7186 The result is non-zero if a valid (non-dummy) DIE was found. */
7187
7188 static int
7189 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7190 struct dwo_unit *dwo_unit,
7191 struct die_info *stub_comp_unit_die,
7192 const char *stub_comp_dir,
7193 struct die_reader_specs *result_reader,
7194 const gdb_byte **result_info_ptr,
7195 struct die_info **result_comp_unit_die,
7196 int *result_has_children,
7197 abbrev_table_up *result_dwo_abbrev_table)
7198 {
7199 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7200 struct objfile *objfile = dwarf2_per_objfile->objfile;
7201 struct dwarf2_cu *cu = this_cu->cu;
7202 bfd *abfd;
7203 const gdb_byte *begin_info_ptr, *info_ptr;
7204 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7205 int i,num_extra_attrs;
7206 struct dwarf2_section_info *dwo_abbrev_section;
7207 struct attribute *attr;
7208 struct die_info *comp_unit_die;
7209
7210 /* At most one of these may be provided. */
7211 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7212
7213 /* These attributes aren't processed until later:
7214 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7215 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7216 referenced later. However, these attributes are found in the stub
7217 which we won't have later. In order to not impose this complication
7218 on the rest of the code, we read them here and copy them to the
7219 DWO CU/TU die. */
7220
7221 stmt_list = NULL;
7222 low_pc = NULL;
7223 high_pc = NULL;
7224 ranges = NULL;
7225 comp_dir = NULL;
7226
7227 if (stub_comp_unit_die != NULL)
7228 {
7229 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7230 DWO file. */
7231 if (! this_cu->is_debug_types)
7232 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7233 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7234 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7235 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7236 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7237
7238 /* There should be a DW_AT_addr_base attribute here (if needed).
7239 We need the value before we can process DW_FORM_GNU_addr_index. */
7240 cu->addr_base = 0;
7241 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7242 if (attr)
7243 cu->addr_base = DW_UNSND (attr);
7244
7245 /* There should be a DW_AT_ranges_base attribute here (if needed).
7246 We need the value before we can process DW_AT_ranges. */
7247 cu->ranges_base = 0;
7248 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7249 if (attr)
7250 cu->ranges_base = DW_UNSND (attr);
7251 }
7252 else if (stub_comp_dir != NULL)
7253 {
7254 /* Reconstruct the comp_dir attribute to simplify the code below. */
7255 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7256 comp_dir->name = DW_AT_comp_dir;
7257 comp_dir->form = DW_FORM_string;
7258 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7259 DW_STRING (comp_dir) = stub_comp_dir;
7260 }
7261
7262 /* Set up for reading the DWO CU/TU. */
7263 cu->dwo_unit = dwo_unit;
7264 dwarf2_section_info *section = dwo_unit->section;
7265 dwarf2_read_section (objfile, section);
7266 abfd = get_section_bfd_owner (section);
7267 begin_info_ptr = info_ptr = (section->buffer
7268 + to_underlying (dwo_unit->sect_off));
7269 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7270
7271 if (this_cu->is_debug_types)
7272 {
7273 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7274
7275 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7276 &cu->header, section,
7277 dwo_abbrev_section,
7278 info_ptr, rcuh_kind::TYPE);
7279 /* This is not an assert because it can be caused by bad debug info. */
7280 if (sig_type->signature != cu->header.signature)
7281 {
7282 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7283 " TU at offset %s [in module %s]"),
7284 hex_string (sig_type->signature),
7285 hex_string (cu->header.signature),
7286 sect_offset_str (dwo_unit->sect_off),
7287 bfd_get_filename (abfd));
7288 }
7289 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7290 /* For DWOs coming from DWP files, we don't know the CU length
7291 nor the type's offset in the TU until now. */
7292 dwo_unit->length = get_cu_length (&cu->header);
7293 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7294
7295 /* Establish the type offset that can be used to lookup the type.
7296 For DWO files, we don't know it until now. */
7297 sig_type->type_offset_in_section
7298 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7299 }
7300 else
7301 {
7302 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7303 &cu->header, section,
7304 dwo_abbrev_section,
7305 info_ptr, rcuh_kind::COMPILE);
7306 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7307 /* For DWOs coming from DWP files, we don't know the CU length
7308 until now. */
7309 dwo_unit->length = get_cu_length (&cu->header);
7310 }
7311
7312 *result_dwo_abbrev_table
7313 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7314 cu->header.abbrev_sect_off);
7315 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7316 result_dwo_abbrev_table->get ());
7317
7318 /* Read in the die, but leave space to copy over the attributes
7319 from the stub. This has the benefit of simplifying the rest of
7320 the code - all the work to maintain the illusion of a single
7321 DW_TAG_{compile,type}_unit DIE is done here. */
7322 num_extra_attrs = ((stmt_list != NULL)
7323 + (low_pc != NULL)
7324 + (high_pc != NULL)
7325 + (ranges != NULL)
7326 + (comp_dir != NULL));
7327 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7328 result_has_children, num_extra_attrs);
7329
7330 /* Copy over the attributes from the stub to the DIE we just read in. */
7331 comp_unit_die = *result_comp_unit_die;
7332 i = comp_unit_die->num_attrs;
7333 if (stmt_list != NULL)
7334 comp_unit_die->attrs[i++] = *stmt_list;
7335 if (low_pc != NULL)
7336 comp_unit_die->attrs[i++] = *low_pc;
7337 if (high_pc != NULL)
7338 comp_unit_die->attrs[i++] = *high_pc;
7339 if (ranges != NULL)
7340 comp_unit_die->attrs[i++] = *ranges;
7341 if (comp_dir != NULL)
7342 comp_unit_die->attrs[i++] = *comp_dir;
7343 comp_unit_die->num_attrs += num_extra_attrs;
7344
7345 if (dwarf_die_debug)
7346 {
7347 fprintf_unfiltered (gdb_stdlog,
7348 "Read die from %s@0x%x of %s:\n",
7349 get_section_name (section),
7350 (unsigned) (begin_info_ptr - section->buffer),
7351 bfd_get_filename (abfd));
7352 dump_die (comp_unit_die, dwarf_die_debug);
7353 }
7354
7355 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7356 TUs by skipping the stub and going directly to the entry in the DWO file.
7357 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7358 to get it via circuitous means. Blech. */
7359 if (comp_dir != NULL)
7360 result_reader->comp_dir = DW_STRING (comp_dir);
7361
7362 /* Skip dummy compilation units. */
7363 if (info_ptr >= begin_info_ptr + dwo_unit->length
7364 || peek_abbrev_code (abfd, info_ptr) == 0)
7365 return 0;
7366
7367 *result_info_ptr = info_ptr;
7368 return 1;
7369 }
7370
7371 /* Subroutine of init_cutu_and_read_dies to simplify it.
7372 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7373 Returns NULL if the specified DWO unit cannot be found. */
7374
7375 static struct dwo_unit *
7376 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7377 struct die_info *comp_unit_die)
7378 {
7379 struct dwarf2_cu *cu = this_cu->cu;
7380 ULONGEST signature;
7381 struct dwo_unit *dwo_unit;
7382 const char *comp_dir, *dwo_name;
7383
7384 gdb_assert (cu != NULL);
7385
7386 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7387 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7388 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7389
7390 if (this_cu->is_debug_types)
7391 {
7392 struct signatured_type *sig_type;
7393
7394 /* Since this_cu is the first member of struct signatured_type,
7395 we can go from a pointer to one to a pointer to the other. */
7396 sig_type = (struct signatured_type *) this_cu;
7397 signature = sig_type->signature;
7398 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7399 }
7400 else
7401 {
7402 struct attribute *attr;
7403
7404 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7405 if (! attr)
7406 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7407 " [in module %s]"),
7408 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7409 signature = DW_UNSND (attr);
7410 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7411 signature);
7412 }
7413
7414 return dwo_unit;
7415 }
7416
7417 /* Subroutine of init_cutu_and_read_dies to simplify it.
7418 See it for a description of the parameters.
7419 Read a TU directly from a DWO file, bypassing the stub. */
7420
7421 static void
7422 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7423 int use_existing_cu, int keep,
7424 die_reader_func_ftype *die_reader_func,
7425 void *data)
7426 {
7427 std::unique_ptr<dwarf2_cu> new_cu;
7428 struct signatured_type *sig_type;
7429 struct die_reader_specs reader;
7430 const gdb_byte *info_ptr;
7431 struct die_info *comp_unit_die;
7432 int has_children;
7433 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7434
7435 /* Verify we can do the following downcast, and that we have the
7436 data we need. */
7437 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7438 sig_type = (struct signatured_type *) this_cu;
7439 gdb_assert (sig_type->dwo_unit != NULL);
7440
7441 if (use_existing_cu && this_cu->cu != NULL)
7442 {
7443 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7444 /* There's no need to do the rereading_dwo_cu handling that
7445 init_cutu_and_read_dies does since we don't read the stub. */
7446 }
7447 else
7448 {
7449 /* If !use_existing_cu, this_cu->cu must be NULL. */
7450 gdb_assert (this_cu->cu == NULL);
7451 new_cu.reset (new dwarf2_cu (this_cu));
7452 }
7453
7454 /* A future optimization, if needed, would be to use an existing
7455 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7456 could share abbrev tables. */
7457
7458 /* The abbreviation table used by READER, this must live at least as long as
7459 READER. */
7460 abbrev_table_up dwo_abbrev_table;
7461
7462 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7463 NULL /* stub_comp_unit_die */,
7464 sig_type->dwo_unit->dwo_file->comp_dir,
7465 &reader, &info_ptr,
7466 &comp_unit_die, &has_children,
7467 &dwo_abbrev_table) == 0)
7468 {
7469 /* Dummy die. */
7470 return;
7471 }
7472
7473 /* All the "real" work is done here. */
7474 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7475
7476 /* This duplicates the code in init_cutu_and_read_dies,
7477 but the alternative is making the latter more complex.
7478 This function is only for the special case of using DWO files directly:
7479 no point in overly complicating the general case just to handle this. */
7480 if (new_cu != NULL && keep)
7481 {
7482 /* Link this CU into read_in_chain. */
7483 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7484 dwarf2_per_objfile->read_in_chain = this_cu;
7485 /* The chain owns it now. */
7486 new_cu.release ();
7487 }
7488 }
7489
7490 /* Initialize a CU (or TU) and read its DIEs.
7491 If the CU defers to a DWO file, read the DWO file as well.
7492
7493 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7494 Otherwise the table specified in the comp unit header is read in and used.
7495 This is an optimization for when we already have the abbrev table.
7496
7497 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7498 Otherwise, a new CU is allocated with xmalloc.
7499
7500 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7501 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7502
7503 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7504 linker) then DIE_READER_FUNC will not get called. */
7505
7506 static void
7507 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7508 struct abbrev_table *abbrev_table,
7509 int use_existing_cu, int keep,
7510 bool skip_partial,
7511 die_reader_func_ftype *die_reader_func,
7512 void *data)
7513 {
7514 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7515 struct objfile *objfile = dwarf2_per_objfile->objfile;
7516 struct dwarf2_section_info *section = this_cu->section;
7517 bfd *abfd = get_section_bfd_owner (section);
7518 struct dwarf2_cu *cu;
7519 const gdb_byte *begin_info_ptr, *info_ptr;
7520 struct die_reader_specs reader;
7521 struct die_info *comp_unit_die;
7522 int has_children;
7523 struct attribute *attr;
7524 struct signatured_type *sig_type = NULL;
7525 struct dwarf2_section_info *abbrev_section;
7526 /* Non-zero if CU currently points to a DWO file and we need to
7527 reread it. When this happens we need to reread the skeleton die
7528 before we can reread the DWO file (this only applies to CUs, not TUs). */
7529 int rereading_dwo_cu = 0;
7530
7531 if (dwarf_die_debug)
7532 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7533 this_cu->is_debug_types ? "type" : "comp",
7534 sect_offset_str (this_cu->sect_off));
7535
7536 if (use_existing_cu)
7537 gdb_assert (keep);
7538
7539 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7540 file (instead of going through the stub), short-circuit all of this. */
7541 if (this_cu->reading_dwo_directly)
7542 {
7543 /* Narrow down the scope of possibilities to have to understand. */
7544 gdb_assert (this_cu->is_debug_types);
7545 gdb_assert (abbrev_table == NULL);
7546 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7547 die_reader_func, data);
7548 return;
7549 }
7550
7551 /* This is cheap if the section is already read in. */
7552 dwarf2_read_section (objfile, section);
7553
7554 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7555
7556 abbrev_section = get_abbrev_section_for_cu (this_cu);
7557
7558 std::unique_ptr<dwarf2_cu> new_cu;
7559 if (use_existing_cu && this_cu->cu != NULL)
7560 {
7561 cu = this_cu->cu;
7562 /* If this CU is from a DWO file we need to start over, we need to
7563 refetch the attributes from the skeleton CU.
7564 This could be optimized by retrieving those attributes from when we
7565 were here the first time: the previous comp_unit_die was stored in
7566 comp_unit_obstack. But there's no data yet that we need this
7567 optimization. */
7568 if (cu->dwo_unit != NULL)
7569 rereading_dwo_cu = 1;
7570 }
7571 else
7572 {
7573 /* If !use_existing_cu, this_cu->cu must be NULL. */
7574 gdb_assert (this_cu->cu == NULL);
7575 new_cu.reset (new dwarf2_cu (this_cu));
7576 cu = new_cu.get ();
7577 }
7578
7579 /* Get the header. */
7580 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7581 {
7582 /* We already have the header, there's no need to read it in again. */
7583 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7584 }
7585 else
7586 {
7587 if (this_cu->is_debug_types)
7588 {
7589 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7590 &cu->header, section,
7591 abbrev_section, info_ptr,
7592 rcuh_kind::TYPE);
7593
7594 /* Since per_cu is the first member of struct signatured_type,
7595 we can go from a pointer to one to a pointer to the other. */
7596 sig_type = (struct signatured_type *) this_cu;
7597 gdb_assert (sig_type->signature == cu->header.signature);
7598 gdb_assert (sig_type->type_offset_in_tu
7599 == cu->header.type_cu_offset_in_tu);
7600 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7601
7602 /* LENGTH has not been set yet for type units if we're
7603 using .gdb_index. */
7604 this_cu->length = get_cu_length (&cu->header);
7605
7606 /* Establish the type offset that can be used to lookup the type. */
7607 sig_type->type_offset_in_section =
7608 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7609
7610 this_cu->dwarf_version = cu->header.version;
7611 }
7612 else
7613 {
7614 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7615 &cu->header, section,
7616 abbrev_section,
7617 info_ptr,
7618 rcuh_kind::COMPILE);
7619
7620 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7621 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7622 this_cu->dwarf_version = cu->header.version;
7623 }
7624 }
7625
7626 /* Skip dummy compilation units. */
7627 if (info_ptr >= begin_info_ptr + this_cu->length
7628 || peek_abbrev_code (abfd, info_ptr) == 0)
7629 return;
7630
7631 /* If we don't have them yet, read the abbrevs for this compilation unit.
7632 And if we need to read them now, make sure they're freed when we're
7633 done (own the table through ABBREV_TABLE_HOLDER). */
7634 abbrev_table_up abbrev_table_holder;
7635 if (abbrev_table != NULL)
7636 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7637 else
7638 {
7639 abbrev_table_holder
7640 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7641 cu->header.abbrev_sect_off);
7642 abbrev_table = abbrev_table_holder.get ();
7643 }
7644
7645 /* Read the top level CU/TU die. */
7646 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7647 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7648
7649 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7650 return;
7651
7652 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7653 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7654 table from the DWO file and pass the ownership over to us. It will be
7655 referenced from READER, so we must make sure to free it after we're done
7656 with READER.
7657
7658 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7659 DWO CU, that this test will fail (the attribute will not be present). */
7660 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7661 abbrev_table_up dwo_abbrev_table;
7662 if (attr)
7663 {
7664 struct dwo_unit *dwo_unit;
7665 struct die_info *dwo_comp_unit_die;
7666
7667 if (has_children)
7668 {
7669 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7670 " has children (offset %s) [in module %s]"),
7671 sect_offset_str (this_cu->sect_off),
7672 bfd_get_filename (abfd));
7673 }
7674 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7675 if (dwo_unit != NULL)
7676 {
7677 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7678 comp_unit_die, NULL,
7679 &reader, &info_ptr,
7680 &dwo_comp_unit_die, &has_children,
7681 &dwo_abbrev_table) == 0)
7682 {
7683 /* Dummy die. */
7684 return;
7685 }
7686 comp_unit_die = dwo_comp_unit_die;
7687 }
7688 else
7689 {
7690 /* Yikes, we couldn't find the rest of the DIE, we only have
7691 the stub. A complaint has already been logged. There's
7692 not much more we can do except pass on the stub DIE to
7693 die_reader_func. We don't want to throw an error on bad
7694 debug info. */
7695 }
7696 }
7697
7698 /* All of the above is setup for this call. Yikes. */
7699 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7700
7701 /* Done, clean up. */
7702 if (new_cu != NULL && keep)
7703 {
7704 /* Link this CU into read_in_chain. */
7705 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7706 dwarf2_per_objfile->read_in_chain = this_cu;
7707 /* The chain owns it now. */
7708 new_cu.release ();
7709 }
7710 }
7711
7712 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7713 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7714 to have already done the lookup to find the DWO file).
7715
7716 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7717 THIS_CU->is_debug_types, but nothing else.
7718
7719 We fill in THIS_CU->length.
7720
7721 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7722 linker) then DIE_READER_FUNC will not get called.
7723
7724 THIS_CU->cu is always freed when done.
7725 This is done in order to not leave THIS_CU->cu in a state where we have
7726 to care whether it refers to the "main" CU or the DWO CU. */
7727
7728 static void
7729 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7730 struct dwo_file *dwo_file,
7731 die_reader_func_ftype *die_reader_func,
7732 void *data)
7733 {
7734 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7735 struct objfile *objfile = dwarf2_per_objfile->objfile;
7736 struct dwarf2_section_info *section = this_cu->section;
7737 bfd *abfd = get_section_bfd_owner (section);
7738 struct dwarf2_section_info *abbrev_section;
7739 const gdb_byte *begin_info_ptr, *info_ptr;
7740 struct die_reader_specs reader;
7741 struct die_info *comp_unit_die;
7742 int has_children;
7743
7744 if (dwarf_die_debug)
7745 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7746 this_cu->is_debug_types ? "type" : "comp",
7747 sect_offset_str (this_cu->sect_off));
7748
7749 gdb_assert (this_cu->cu == NULL);
7750
7751 abbrev_section = (dwo_file != NULL
7752 ? &dwo_file->sections.abbrev
7753 : get_abbrev_section_for_cu (this_cu));
7754
7755 /* This is cheap if the section is already read in. */
7756 dwarf2_read_section (objfile, section);
7757
7758 struct dwarf2_cu cu (this_cu);
7759
7760 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7761 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7762 &cu.header, section,
7763 abbrev_section, info_ptr,
7764 (this_cu->is_debug_types
7765 ? rcuh_kind::TYPE
7766 : rcuh_kind::COMPILE));
7767
7768 this_cu->length = get_cu_length (&cu.header);
7769
7770 /* Skip dummy compilation units. */
7771 if (info_ptr >= begin_info_ptr + this_cu->length
7772 || peek_abbrev_code (abfd, info_ptr) == 0)
7773 return;
7774
7775 abbrev_table_up abbrev_table
7776 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7777 cu.header.abbrev_sect_off);
7778
7779 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7780 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7781
7782 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7783 }
7784
7785 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7786 does not lookup the specified DWO file.
7787 This cannot be used to read DWO files.
7788
7789 THIS_CU->cu is always freed when done.
7790 This is done in order to not leave THIS_CU->cu in a state where we have
7791 to care whether it refers to the "main" CU or the DWO CU.
7792 We can revisit this if the data shows there's a performance issue. */
7793
7794 static void
7795 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7796 die_reader_func_ftype *die_reader_func,
7797 void *data)
7798 {
7799 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7800 }
7801 \f
7802 /* Type Unit Groups.
7803
7804 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7805 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7806 so that all types coming from the same compilation (.o file) are grouped
7807 together. A future step could be to put the types in the same symtab as
7808 the CU the types ultimately came from. */
7809
7810 static hashval_t
7811 hash_type_unit_group (const void *item)
7812 {
7813 const struct type_unit_group *tu_group
7814 = (const struct type_unit_group *) item;
7815
7816 return hash_stmt_list_entry (&tu_group->hash);
7817 }
7818
7819 static int
7820 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7821 {
7822 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7823 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7824
7825 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7826 }
7827
7828 /* Allocate a hash table for type unit groups. */
7829
7830 static htab_t
7831 allocate_type_unit_groups_table (struct objfile *objfile)
7832 {
7833 return htab_create_alloc_ex (3,
7834 hash_type_unit_group,
7835 eq_type_unit_group,
7836 NULL,
7837 &objfile->objfile_obstack,
7838 hashtab_obstack_allocate,
7839 dummy_obstack_deallocate);
7840 }
7841
7842 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7843 partial symtabs. We combine several TUs per psymtab to not let the size
7844 of any one psymtab grow too big. */
7845 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7846 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7847
7848 /* Helper routine for get_type_unit_group.
7849 Create the type_unit_group object used to hold one or more TUs. */
7850
7851 static struct type_unit_group *
7852 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7853 {
7854 struct dwarf2_per_objfile *dwarf2_per_objfile
7855 = cu->per_cu->dwarf2_per_objfile;
7856 struct objfile *objfile = dwarf2_per_objfile->objfile;
7857 struct dwarf2_per_cu_data *per_cu;
7858 struct type_unit_group *tu_group;
7859
7860 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7861 struct type_unit_group);
7862 per_cu = &tu_group->per_cu;
7863 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7864
7865 if (dwarf2_per_objfile->using_index)
7866 {
7867 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7868 struct dwarf2_per_cu_quick_data);
7869 }
7870 else
7871 {
7872 unsigned int line_offset = to_underlying (line_offset_struct);
7873 struct partial_symtab *pst;
7874 std::string name;
7875
7876 /* Give the symtab a useful name for debug purposes. */
7877 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7878 name = string_printf ("<type_units_%d>",
7879 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7880 else
7881 name = string_printf ("<type_units_at_0x%x>", line_offset);
7882
7883 pst = create_partial_symtab (per_cu, name.c_str ());
7884 pst->anonymous = 1;
7885 }
7886
7887 tu_group->hash.dwo_unit = cu->dwo_unit;
7888 tu_group->hash.line_sect_off = line_offset_struct;
7889
7890 return tu_group;
7891 }
7892
7893 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7894 STMT_LIST is a DW_AT_stmt_list attribute. */
7895
7896 static struct type_unit_group *
7897 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7898 {
7899 struct dwarf2_per_objfile *dwarf2_per_objfile
7900 = cu->per_cu->dwarf2_per_objfile;
7901 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7902 struct type_unit_group *tu_group;
7903 void **slot;
7904 unsigned int line_offset;
7905 struct type_unit_group type_unit_group_for_lookup;
7906
7907 if (dwarf2_per_objfile->type_unit_groups == NULL)
7908 {
7909 dwarf2_per_objfile->type_unit_groups =
7910 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7911 }
7912
7913 /* Do we need to create a new group, or can we use an existing one? */
7914
7915 if (stmt_list)
7916 {
7917 line_offset = DW_UNSND (stmt_list);
7918 ++tu_stats->nr_symtab_sharers;
7919 }
7920 else
7921 {
7922 /* Ugh, no stmt_list. Rare, but we have to handle it.
7923 We can do various things here like create one group per TU or
7924 spread them over multiple groups to split up the expansion work.
7925 To avoid worst case scenarios (too many groups or too large groups)
7926 we, umm, group them in bunches. */
7927 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7928 | (tu_stats->nr_stmt_less_type_units
7929 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7930 ++tu_stats->nr_stmt_less_type_units;
7931 }
7932
7933 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7934 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7935 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7936 &type_unit_group_for_lookup, INSERT);
7937 if (*slot != NULL)
7938 {
7939 tu_group = (struct type_unit_group *) *slot;
7940 gdb_assert (tu_group != NULL);
7941 }
7942 else
7943 {
7944 sect_offset line_offset_struct = (sect_offset) line_offset;
7945 tu_group = create_type_unit_group (cu, line_offset_struct);
7946 *slot = tu_group;
7947 ++tu_stats->nr_symtabs;
7948 }
7949
7950 return tu_group;
7951 }
7952 \f
7953 /* Partial symbol tables. */
7954
7955 /* Create a psymtab named NAME and assign it to PER_CU.
7956
7957 The caller must fill in the following details:
7958 dirname, textlow, texthigh. */
7959
7960 static struct partial_symtab *
7961 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7962 {
7963 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7964 struct partial_symtab *pst;
7965
7966 pst = start_psymtab_common (objfile, name, 0);
7967
7968 pst->psymtabs_addrmap_supported = 1;
7969
7970 /* This is the glue that links PST into GDB's symbol API. */
7971 pst->read_symtab_private = per_cu;
7972 pst->read_symtab = dwarf2_read_symtab;
7973 per_cu->v.psymtab = pst;
7974
7975 return pst;
7976 }
7977
7978 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7979 type. */
7980
7981 struct process_psymtab_comp_unit_data
7982 {
7983 /* True if we are reading a DW_TAG_partial_unit. */
7984
7985 int want_partial_unit;
7986
7987 /* The "pretend" language that is used if the CU doesn't declare a
7988 language. */
7989
7990 enum language pretend_language;
7991 };
7992
7993 /* die_reader_func for process_psymtab_comp_unit. */
7994
7995 static void
7996 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7997 const gdb_byte *info_ptr,
7998 struct die_info *comp_unit_die,
7999 int has_children,
8000 void *data)
8001 {
8002 struct dwarf2_cu *cu = reader->cu;
8003 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8004 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8005 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8006 CORE_ADDR baseaddr;
8007 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8008 struct partial_symtab *pst;
8009 enum pc_bounds_kind cu_bounds_kind;
8010 const char *filename;
8011 struct process_psymtab_comp_unit_data *info
8012 = (struct process_psymtab_comp_unit_data *) data;
8013
8014 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8015 return;
8016
8017 gdb_assert (! per_cu->is_debug_types);
8018
8019 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8020
8021 /* Allocate a new partial symbol table structure. */
8022 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8023 if (filename == NULL)
8024 filename = "";
8025
8026 pst = create_partial_symtab (per_cu, filename);
8027
8028 /* This must be done before calling dwarf2_build_include_psymtabs. */
8029 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8030
8031 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8032
8033 dwarf2_find_base_address (comp_unit_die, cu);
8034
8035 /* Possibly set the default values of LOWPC and HIGHPC from
8036 `DW_AT_ranges'. */
8037 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8038 &best_highpc, cu, pst);
8039 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8040 {
8041 CORE_ADDR low
8042 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8043 - baseaddr);
8044 CORE_ADDR high
8045 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8046 - baseaddr - 1);
8047 /* Store the contiguous range if it is not empty; it can be
8048 empty for CUs with no code. */
8049 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8050 low, high, pst);
8051 }
8052
8053 /* Check if comp unit has_children.
8054 If so, read the rest of the partial symbols from this comp unit.
8055 If not, there's no more debug_info for this comp unit. */
8056 if (has_children)
8057 {
8058 struct partial_die_info *first_die;
8059 CORE_ADDR lowpc, highpc;
8060
8061 lowpc = ((CORE_ADDR) -1);
8062 highpc = ((CORE_ADDR) 0);
8063
8064 first_die = load_partial_dies (reader, info_ptr, 1);
8065
8066 scan_partial_symbols (first_die, &lowpc, &highpc,
8067 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8068
8069 /* If we didn't find a lowpc, set it to highpc to avoid
8070 complaints from `maint check'. */
8071 if (lowpc == ((CORE_ADDR) -1))
8072 lowpc = highpc;
8073
8074 /* If the compilation unit didn't have an explicit address range,
8075 then use the information extracted from its child dies. */
8076 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8077 {
8078 best_lowpc = lowpc;
8079 best_highpc = highpc;
8080 }
8081 }
8082 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8083 best_lowpc + baseaddr)
8084 - baseaddr);
8085 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8086 best_highpc + baseaddr)
8087 - baseaddr);
8088
8089 end_psymtab_common (objfile, pst);
8090
8091 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8092 {
8093 int i;
8094 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8095 struct dwarf2_per_cu_data *iter;
8096
8097 /* Fill in 'dependencies' here; we fill in 'users' in a
8098 post-pass. */
8099 pst->number_of_dependencies = len;
8100 pst->dependencies
8101 = objfile->partial_symtabs->allocate_dependencies (len);
8102 for (i = 0;
8103 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8104 i, iter);
8105 ++i)
8106 pst->dependencies[i] = iter->v.psymtab;
8107
8108 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8109 }
8110
8111 /* Get the list of files included in the current compilation unit,
8112 and build a psymtab for each of them. */
8113 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8114
8115 if (dwarf_read_debug)
8116 fprintf_unfiltered (gdb_stdlog,
8117 "Psymtab for %s unit @%s: %s - %s"
8118 ", %d global, %d static syms\n",
8119 per_cu->is_debug_types ? "type" : "comp",
8120 sect_offset_str (per_cu->sect_off),
8121 paddress (gdbarch, pst->text_low (objfile)),
8122 paddress (gdbarch, pst->text_high (objfile)),
8123 pst->n_global_syms, pst->n_static_syms);
8124 }
8125
8126 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8127 Process compilation unit THIS_CU for a psymtab. */
8128
8129 static void
8130 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8131 int want_partial_unit,
8132 enum language pretend_language)
8133 {
8134 /* If this compilation unit was already read in, free the
8135 cached copy in order to read it in again. This is
8136 necessary because we skipped some symbols when we first
8137 read in the compilation unit (see load_partial_dies).
8138 This problem could be avoided, but the benefit is unclear. */
8139 if (this_cu->cu != NULL)
8140 free_one_cached_comp_unit (this_cu);
8141
8142 if (this_cu->is_debug_types)
8143 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8144 build_type_psymtabs_reader, NULL);
8145 else
8146 {
8147 process_psymtab_comp_unit_data info;
8148 info.want_partial_unit = want_partial_unit;
8149 info.pretend_language = pretend_language;
8150 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8151 process_psymtab_comp_unit_reader, &info);
8152 }
8153
8154 /* Age out any secondary CUs. */
8155 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8156 }
8157
8158 /* Reader function for build_type_psymtabs. */
8159
8160 static void
8161 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8162 const gdb_byte *info_ptr,
8163 struct die_info *type_unit_die,
8164 int has_children,
8165 void *data)
8166 {
8167 struct dwarf2_per_objfile *dwarf2_per_objfile
8168 = reader->cu->per_cu->dwarf2_per_objfile;
8169 struct objfile *objfile = dwarf2_per_objfile->objfile;
8170 struct dwarf2_cu *cu = reader->cu;
8171 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8172 struct signatured_type *sig_type;
8173 struct type_unit_group *tu_group;
8174 struct attribute *attr;
8175 struct partial_die_info *first_die;
8176 CORE_ADDR lowpc, highpc;
8177 struct partial_symtab *pst;
8178
8179 gdb_assert (data == NULL);
8180 gdb_assert (per_cu->is_debug_types);
8181 sig_type = (struct signatured_type *) per_cu;
8182
8183 if (! has_children)
8184 return;
8185
8186 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8187 tu_group = get_type_unit_group (cu, attr);
8188
8189 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8190
8191 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8192 pst = create_partial_symtab (per_cu, "");
8193 pst->anonymous = 1;
8194
8195 first_die = load_partial_dies (reader, info_ptr, 1);
8196
8197 lowpc = (CORE_ADDR) -1;
8198 highpc = (CORE_ADDR) 0;
8199 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8200
8201 end_psymtab_common (objfile, pst);
8202 }
8203
8204 /* Struct used to sort TUs by their abbreviation table offset. */
8205
8206 struct tu_abbrev_offset
8207 {
8208 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8209 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8210 {}
8211
8212 signatured_type *sig_type;
8213 sect_offset abbrev_offset;
8214 };
8215
8216 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8217
8218 static bool
8219 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8220 const struct tu_abbrev_offset &b)
8221 {
8222 return a.abbrev_offset < b.abbrev_offset;
8223 }
8224
8225 /* Efficiently read all the type units.
8226 This does the bulk of the work for build_type_psymtabs.
8227
8228 The efficiency is because we sort TUs by the abbrev table they use and
8229 only read each abbrev table once. In one program there are 200K TUs
8230 sharing 8K abbrev tables.
8231
8232 The main purpose of this function is to support building the
8233 dwarf2_per_objfile->type_unit_groups table.
8234 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8235 can collapse the search space by grouping them by stmt_list.
8236 The savings can be significant, in the same program from above the 200K TUs
8237 share 8K stmt_list tables.
8238
8239 FUNC is expected to call get_type_unit_group, which will create the
8240 struct type_unit_group if necessary and add it to
8241 dwarf2_per_objfile->type_unit_groups. */
8242
8243 static void
8244 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8245 {
8246 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8247 abbrev_table_up abbrev_table;
8248 sect_offset abbrev_offset;
8249
8250 /* It's up to the caller to not call us multiple times. */
8251 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8252
8253 if (dwarf2_per_objfile->all_type_units.empty ())
8254 return;
8255
8256 /* TUs typically share abbrev tables, and there can be way more TUs than
8257 abbrev tables. Sort by abbrev table to reduce the number of times we
8258 read each abbrev table in.
8259 Alternatives are to punt or to maintain a cache of abbrev tables.
8260 This is simpler and efficient enough for now.
8261
8262 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8263 symtab to use). Typically TUs with the same abbrev offset have the same
8264 stmt_list value too so in practice this should work well.
8265
8266 The basic algorithm here is:
8267
8268 sort TUs by abbrev table
8269 for each TU with same abbrev table:
8270 read abbrev table if first user
8271 read TU top level DIE
8272 [IWBN if DWO skeletons had DW_AT_stmt_list]
8273 call FUNC */
8274
8275 if (dwarf_read_debug)
8276 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8277
8278 /* Sort in a separate table to maintain the order of all_type_units
8279 for .gdb_index: TU indices directly index all_type_units. */
8280 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8281 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8282
8283 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8284 sorted_by_abbrev.emplace_back
8285 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8286 sig_type->per_cu.section,
8287 sig_type->per_cu.sect_off));
8288
8289 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8290 sort_tu_by_abbrev_offset);
8291
8292 abbrev_offset = (sect_offset) ~(unsigned) 0;
8293
8294 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8295 {
8296 /* Switch to the next abbrev table if necessary. */
8297 if (abbrev_table == NULL
8298 || tu.abbrev_offset != abbrev_offset)
8299 {
8300 abbrev_offset = tu.abbrev_offset;
8301 abbrev_table =
8302 abbrev_table_read_table (dwarf2_per_objfile,
8303 &dwarf2_per_objfile->abbrev,
8304 abbrev_offset);
8305 ++tu_stats->nr_uniq_abbrev_tables;
8306 }
8307
8308 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8309 0, 0, false, build_type_psymtabs_reader, NULL);
8310 }
8311 }
8312
8313 /* Print collected type unit statistics. */
8314
8315 static void
8316 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8317 {
8318 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8319
8320 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8321 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8322 dwarf2_per_objfile->all_type_units.size ());
8323 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8324 tu_stats->nr_uniq_abbrev_tables);
8325 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8326 tu_stats->nr_symtabs);
8327 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8328 tu_stats->nr_symtab_sharers);
8329 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8330 tu_stats->nr_stmt_less_type_units);
8331 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8332 tu_stats->nr_all_type_units_reallocs);
8333 }
8334
8335 /* Traversal function for build_type_psymtabs. */
8336
8337 static int
8338 build_type_psymtab_dependencies (void **slot, void *info)
8339 {
8340 struct dwarf2_per_objfile *dwarf2_per_objfile
8341 = (struct dwarf2_per_objfile *) info;
8342 struct objfile *objfile = dwarf2_per_objfile->objfile;
8343 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8344 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8345 struct partial_symtab *pst = per_cu->v.psymtab;
8346 int len = VEC_length (sig_type_ptr, tu_group->tus);
8347 struct signatured_type *iter;
8348 int i;
8349
8350 gdb_assert (len > 0);
8351 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8352
8353 pst->number_of_dependencies = len;
8354 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8355 for (i = 0;
8356 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8357 ++i)
8358 {
8359 gdb_assert (iter->per_cu.is_debug_types);
8360 pst->dependencies[i] = iter->per_cu.v.psymtab;
8361 iter->type_unit_group = tu_group;
8362 }
8363
8364 VEC_free (sig_type_ptr, tu_group->tus);
8365
8366 return 1;
8367 }
8368
8369 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8370 Build partial symbol tables for the .debug_types comp-units. */
8371
8372 static void
8373 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8374 {
8375 if (! create_all_type_units (dwarf2_per_objfile))
8376 return;
8377
8378 build_type_psymtabs_1 (dwarf2_per_objfile);
8379 }
8380
8381 /* Traversal function for process_skeletonless_type_unit.
8382 Read a TU in a DWO file and build partial symbols for it. */
8383
8384 static int
8385 process_skeletonless_type_unit (void **slot, void *info)
8386 {
8387 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8388 struct dwarf2_per_objfile *dwarf2_per_objfile
8389 = (struct dwarf2_per_objfile *) info;
8390 struct signatured_type find_entry, *entry;
8391
8392 /* If this TU doesn't exist in the global table, add it and read it in. */
8393
8394 if (dwarf2_per_objfile->signatured_types == NULL)
8395 {
8396 dwarf2_per_objfile->signatured_types
8397 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8398 }
8399
8400 find_entry.signature = dwo_unit->signature;
8401 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8402 INSERT);
8403 /* If we've already seen this type there's nothing to do. What's happening
8404 is we're doing our own version of comdat-folding here. */
8405 if (*slot != NULL)
8406 return 1;
8407
8408 /* This does the job that create_all_type_units would have done for
8409 this TU. */
8410 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8411 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8412 *slot = entry;
8413
8414 /* This does the job that build_type_psymtabs_1 would have done. */
8415 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8416 build_type_psymtabs_reader, NULL);
8417
8418 return 1;
8419 }
8420
8421 /* Traversal function for process_skeletonless_type_units. */
8422
8423 static int
8424 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8425 {
8426 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8427
8428 if (dwo_file->tus != NULL)
8429 {
8430 htab_traverse_noresize (dwo_file->tus,
8431 process_skeletonless_type_unit, info);
8432 }
8433
8434 return 1;
8435 }
8436
8437 /* Scan all TUs of DWO files, verifying we've processed them.
8438 This is needed in case a TU was emitted without its skeleton.
8439 Note: This can't be done until we know what all the DWO files are. */
8440
8441 static void
8442 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8443 {
8444 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8445 if (get_dwp_file (dwarf2_per_objfile) == NULL
8446 && dwarf2_per_objfile->dwo_files != NULL)
8447 {
8448 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8449 process_dwo_file_for_skeletonless_type_units,
8450 dwarf2_per_objfile);
8451 }
8452 }
8453
8454 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8455
8456 static void
8457 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8458 {
8459 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8460 {
8461 struct partial_symtab *pst = per_cu->v.psymtab;
8462
8463 if (pst == NULL)
8464 continue;
8465
8466 for (int j = 0; j < pst->number_of_dependencies; ++j)
8467 {
8468 /* Set the 'user' field only if it is not already set. */
8469 if (pst->dependencies[j]->user == NULL)
8470 pst->dependencies[j]->user = pst;
8471 }
8472 }
8473 }
8474
8475 /* Build the partial symbol table by doing a quick pass through the
8476 .debug_info and .debug_abbrev sections. */
8477
8478 static void
8479 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8480 {
8481 struct objfile *objfile = dwarf2_per_objfile->objfile;
8482
8483 if (dwarf_read_debug)
8484 {
8485 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8486 objfile_name (objfile));
8487 }
8488
8489 dwarf2_per_objfile->reading_partial_symbols = 1;
8490
8491 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8492
8493 /* Any cached compilation units will be linked by the per-objfile
8494 read_in_chain. Make sure to free them when we're done. */
8495 free_cached_comp_units freer (dwarf2_per_objfile);
8496
8497 build_type_psymtabs (dwarf2_per_objfile);
8498
8499 create_all_comp_units (dwarf2_per_objfile);
8500
8501 /* Create a temporary address map on a temporary obstack. We later
8502 copy this to the final obstack. */
8503 auto_obstack temp_obstack;
8504
8505 scoped_restore save_psymtabs_addrmap
8506 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8507 addrmap_create_mutable (&temp_obstack));
8508
8509 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8510 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8511
8512 /* This has to wait until we read the CUs, we need the list of DWOs. */
8513 process_skeletonless_type_units (dwarf2_per_objfile);
8514
8515 /* Now that all TUs have been processed we can fill in the dependencies. */
8516 if (dwarf2_per_objfile->type_unit_groups != NULL)
8517 {
8518 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8519 build_type_psymtab_dependencies, dwarf2_per_objfile);
8520 }
8521
8522 if (dwarf_read_debug)
8523 print_tu_stats (dwarf2_per_objfile);
8524
8525 set_partial_user (dwarf2_per_objfile);
8526
8527 objfile->partial_symtabs->psymtabs_addrmap
8528 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8529 objfile->partial_symtabs->obstack ());
8530 /* At this point we want to keep the address map. */
8531 save_psymtabs_addrmap.release ();
8532
8533 if (dwarf_read_debug)
8534 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8535 objfile_name (objfile));
8536 }
8537
8538 /* die_reader_func for load_partial_comp_unit. */
8539
8540 static void
8541 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8542 const gdb_byte *info_ptr,
8543 struct die_info *comp_unit_die,
8544 int has_children,
8545 void *data)
8546 {
8547 struct dwarf2_cu *cu = reader->cu;
8548
8549 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8550
8551 /* Check if comp unit has_children.
8552 If so, read the rest of the partial symbols from this comp unit.
8553 If not, there's no more debug_info for this comp unit. */
8554 if (has_children)
8555 load_partial_dies (reader, info_ptr, 0);
8556 }
8557
8558 /* Load the partial DIEs for a secondary CU into memory.
8559 This is also used when rereading a primary CU with load_all_dies. */
8560
8561 static void
8562 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8563 {
8564 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8565 load_partial_comp_unit_reader, NULL);
8566 }
8567
8568 static void
8569 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8570 struct dwarf2_section_info *section,
8571 struct dwarf2_section_info *abbrev_section,
8572 unsigned int is_dwz)
8573 {
8574 const gdb_byte *info_ptr;
8575 struct objfile *objfile = dwarf2_per_objfile->objfile;
8576
8577 if (dwarf_read_debug)
8578 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8579 get_section_name (section),
8580 get_section_file_name (section));
8581
8582 dwarf2_read_section (objfile, section);
8583
8584 info_ptr = section->buffer;
8585
8586 while (info_ptr < section->buffer + section->size)
8587 {
8588 struct dwarf2_per_cu_data *this_cu;
8589
8590 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8591
8592 comp_unit_head cu_header;
8593 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8594 abbrev_section, info_ptr,
8595 rcuh_kind::COMPILE);
8596
8597 /* Save the compilation unit for later lookup. */
8598 if (cu_header.unit_type != DW_UT_type)
8599 {
8600 this_cu = XOBNEW (&objfile->objfile_obstack,
8601 struct dwarf2_per_cu_data);
8602 memset (this_cu, 0, sizeof (*this_cu));
8603 }
8604 else
8605 {
8606 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8607 struct signatured_type);
8608 memset (sig_type, 0, sizeof (*sig_type));
8609 sig_type->signature = cu_header.signature;
8610 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8611 this_cu = &sig_type->per_cu;
8612 }
8613 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8614 this_cu->sect_off = sect_off;
8615 this_cu->length = cu_header.length + cu_header.initial_length_size;
8616 this_cu->is_dwz = is_dwz;
8617 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8618 this_cu->section = section;
8619
8620 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8621
8622 info_ptr = info_ptr + this_cu->length;
8623 }
8624 }
8625
8626 /* Create a list of all compilation units in OBJFILE.
8627 This is only done for -readnow and building partial symtabs. */
8628
8629 static void
8630 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8631 {
8632 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8633 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8634 &dwarf2_per_objfile->abbrev, 0);
8635
8636 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8637 if (dwz != NULL)
8638 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8639 1);
8640 }
8641
8642 /* Process all loaded DIEs for compilation unit CU, starting at
8643 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8644 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8645 DW_AT_ranges). See the comments of add_partial_subprogram on how
8646 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8647
8648 static void
8649 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8650 CORE_ADDR *highpc, int set_addrmap,
8651 struct dwarf2_cu *cu)
8652 {
8653 struct partial_die_info *pdi;
8654
8655 /* Now, march along the PDI's, descending into ones which have
8656 interesting children but skipping the children of the other ones,
8657 until we reach the end of the compilation unit. */
8658
8659 pdi = first_die;
8660
8661 while (pdi != NULL)
8662 {
8663 pdi->fixup (cu);
8664
8665 /* Anonymous namespaces or modules have no name but have interesting
8666 children, so we need to look at them. Ditto for anonymous
8667 enums. */
8668
8669 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8670 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8671 || pdi->tag == DW_TAG_imported_unit
8672 || pdi->tag == DW_TAG_inlined_subroutine)
8673 {
8674 switch (pdi->tag)
8675 {
8676 case DW_TAG_subprogram:
8677 case DW_TAG_inlined_subroutine:
8678 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8679 break;
8680 case DW_TAG_constant:
8681 case DW_TAG_variable:
8682 case DW_TAG_typedef:
8683 case DW_TAG_union_type:
8684 if (!pdi->is_declaration)
8685 {
8686 add_partial_symbol (pdi, cu);
8687 }
8688 break;
8689 case DW_TAG_class_type:
8690 case DW_TAG_interface_type:
8691 case DW_TAG_structure_type:
8692 if (!pdi->is_declaration)
8693 {
8694 add_partial_symbol (pdi, cu);
8695 }
8696 if ((cu->language == language_rust
8697 || cu->language == language_cplus) && pdi->has_children)
8698 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8699 set_addrmap, cu);
8700 break;
8701 case DW_TAG_enumeration_type:
8702 if (!pdi->is_declaration)
8703 add_partial_enumeration (pdi, cu);
8704 break;
8705 case DW_TAG_base_type:
8706 case DW_TAG_subrange_type:
8707 /* File scope base type definitions are added to the partial
8708 symbol table. */
8709 add_partial_symbol (pdi, cu);
8710 break;
8711 case DW_TAG_namespace:
8712 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8713 break;
8714 case DW_TAG_module:
8715 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8716 break;
8717 case DW_TAG_imported_unit:
8718 {
8719 struct dwarf2_per_cu_data *per_cu;
8720
8721 /* For now we don't handle imported units in type units. */
8722 if (cu->per_cu->is_debug_types)
8723 {
8724 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8725 " supported in type units [in module %s]"),
8726 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8727 }
8728
8729 per_cu = dwarf2_find_containing_comp_unit
8730 (pdi->d.sect_off, pdi->is_dwz,
8731 cu->per_cu->dwarf2_per_objfile);
8732
8733 /* Go read the partial unit, if needed. */
8734 if (per_cu->v.psymtab == NULL)
8735 process_psymtab_comp_unit (per_cu, 1, cu->language);
8736
8737 VEC_safe_push (dwarf2_per_cu_ptr,
8738 cu->per_cu->imported_symtabs, per_cu);
8739 }
8740 break;
8741 case DW_TAG_imported_declaration:
8742 add_partial_symbol (pdi, cu);
8743 break;
8744 default:
8745 break;
8746 }
8747 }
8748
8749 /* If the die has a sibling, skip to the sibling. */
8750
8751 pdi = pdi->die_sibling;
8752 }
8753 }
8754
8755 /* Functions used to compute the fully scoped name of a partial DIE.
8756
8757 Normally, this is simple. For C++, the parent DIE's fully scoped
8758 name is concatenated with "::" and the partial DIE's name.
8759 Enumerators are an exception; they use the scope of their parent
8760 enumeration type, i.e. the name of the enumeration type is not
8761 prepended to the enumerator.
8762
8763 There are two complexities. One is DW_AT_specification; in this
8764 case "parent" means the parent of the target of the specification,
8765 instead of the direct parent of the DIE. The other is compilers
8766 which do not emit DW_TAG_namespace; in this case we try to guess
8767 the fully qualified name of structure types from their members'
8768 linkage names. This must be done using the DIE's children rather
8769 than the children of any DW_AT_specification target. We only need
8770 to do this for structures at the top level, i.e. if the target of
8771 any DW_AT_specification (if any; otherwise the DIE itself) does not
8772 have a parent. */
8773
8774 /* Compute the scope prefix associated with PDI's parent, in
8775 compilation unit CU. The result will be allocated on CU's
8776 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8777 field. NULL is returned if no prefix is necessary. */
8778 static const char *
8779 partial_die_parent_scope (struct partial_die_info *pdi,
8780 struct dwarf2_cu *cu)
8781 {
8782 const char *grandparent_scope;
8783 struct partial_die_info *parent, *real_pdi;
8784
8785 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8786 then this means the parent of the specification DIE. */
8787
8788 real_pdi = pdi;
8789 while (real_pdi->has_specification)
8790 real_pdi = find_partial_die (real_pdi->spec_offset,
8791 real_pdi->spec_is_dwz, cu);
8792
8793 parent = real_pdi->die_parent;
8794 if (parent == NULL)
8795 return NULL;
8796
8797 if (parent->scope_set)
8798 return parent->scope;
8799
8800 parent->fixup (cu);
8801
8802 grandparent_scope = partial_die_parent_scope (parent, cu);
8803
8804 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8805 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8806 Work around this problem here. */
8807 if (cu->language == language_cplus
8808 && parent->tag == DW_TAG_namespace
8809 && strcmp (parent->name, "::") == 0
8810 && grandparent_scope == NULL)
8811 {
8812 parent->scope = NULL;
8813 parent->scope_set = 1;
8814 return NULL;
8815 }
8816
8817 if (pdi->tag == DW_TAG_enumerator)
8818 /* Enumerators should not get the name of the enumeration as a prefix. */
8819 parent->scope = grandparent_scope;
8820 else if (parent->tag == DW_TAG_namespace
8821 || parent->tag == DW_TAG_module
8822 || parent->tag == DW_TAG_structure_type
8823 || parent->tag == DW_TAG_class_type
8824 || parent->tag == DW_TAG_interface_type
8825 || parent->tag == DW_TAG_union_type
8826 || parent->tag == DW_TAG_enumeration_type)
8827 {
8828 if (grandparent_scope == NULL)
8829 parent->scope = parent->name;
8830 else
8831 parent->scope = typename_concat (&cu->comp_unit_obstack,
8832 grandparent_scope,
8833 parent->name, 0, cu);
8834 }
8835 else
8836 {
8837 /* FIXME drow/2004-04-01: What should we be doing with
8838 function-local names? For partial symbols, we should probably be
8839 ignoring them. */
8840 complaint (_("unhandled containing DIE tag %d for DIE at %s"),
8841 parent->tag, sect_offset_str (pdi->sect_off));
8842 parent->scope = grandparent_scope;
8843 }
8844
8845 parent->scope_set = 1;
8846 return parent->scope;
8847 }
8848
8849 /* Return the fully scoped name associated with PDI, from compilation unit
8850 CU. The result will be allocated with malloc. */
8851
8852 static char *
8853 partial_die_full_name (struct partial_die_info *pdi,
8854 struct dwarf2_cu *cu)
8855 {
8856 const char *parent_scope;
8857
8858 /* If this is a template instantiation, we can not work out the
8859 template arguments from partial DIEs. So, unfortunately, we have
8860 to go through the full DIEs. At least any work we do building
8861 types here will be reused if full symbols are loaded later. */
8862 if (pdi->has_template_arguments)
8863 {
8864 pdi->fixup (cu);
8865
8866 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8867 {
8868 struct die_info *die;
8869 struct attribute attr;
8870 struct dwarf2_cu *ref_cu = cu;
8871
8872 /* DW_FORM_ref_addr is using section offset. */
8873 attr.name = (enum dwarf_attribute) 0;
8874 attr.form = DW_FORM_ref_addr;
8875 attr.u.unsnd = to_underlying (pdi->sect_off);
8876 die = follow_die_ref (NULL, &attr, &ref_cu);
8877
8878 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8879 }
8880 }
8881
8882 parent_scope = partial_die_parent_scope (pdi, cu);
8883 if (parent_scope == NULL)
8884 return NULL;
8885 else
8886 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8887 }
8888
8889 static void
8890 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8891 {
8892 struct dwarf2_per_objfile *dwarf2_per_objfile
8893 = cu->per_cu->dwarf2_per_objfile;
8894 struct objfile *objfile = dwarf2_per_objfile->objfile;
8895 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8896 CORE_ADDR addr = 0;
8897 const char *actual_name = NULL;
8898 CORE_ADDR baseaddr;
8899 char *built_actual_name;
8900
8901 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8902
8903 built_actual_name = partial_die_full_name (pdi, cu);
8904 if (built_actual_name != NULL)
8905 actual_name = built_actual_name;
8906
8907 if (actual_name == NULL)
8908 actual_name = pdi->name;
8909
8910 switch (pdi->tag)
8911 {
8912 case DW_TAG_inlined_subroutine:
8913 case DW_TAG_subprogram:
8914 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8915 - baseaddr);
8916 if (pdi->is_external || cu->language == language_ada)
8917 {
8918 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8919 of the global scope. But in Ada, we want to be able to access
8920 nested procedures globally. So all Ada subprograms are stored
8921 in the global scope. */
8922 add_psymbol_to_list (actual_name, strlen (actual_name),
8923 built_actual_name != NULL,
8924 VAR_DOMAIN, LOC_BLOCK,
8925 SECT_OFF_TEXT (objfile),
8926 psymbol_placement::GLOBAL,
8927 addr,
8928 cu->language, objfile);
8929 }
8930 else
8931 {
8932 add_psymbol_to_list (actual_name, strlen (actual_name),
8933 built_actual_name != NULL,
8934 VAR_DOMAIN, LOC_BLOCK,
8935 SECT_OFF_TEXT (objfile),
8936 psymbol_placement::STATIC,
8937 addr, cu->language, objfile);
8938 }
8939
8940 if (pdi->main_subprogram && actual_name != NULL)
8941 set_objfile_main_name (objfile, actual_name, cu->language);
8942 break;
8943 case DW_TAG_constant:
8944 add_psymbol_to_list (actual_name, strlen (actual_name),
8945 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8946 -1, (pdi->is_external
8947 ? psymbol_placement::GLOBAL
8948 : psymbol_placement::STATIC),
8949 0, cu->language, objfile);
8950 break;
8951 case DW_TAG_variable:
8952 if (pdi->d.locdesc)
8953 addr = decode_locdesc (pdi->d.locdesc, cu);
8954
8955 if (pdi->d.locdesc
8956 && addr == 0
8957 && !dwarf2_per_objfile->has_section_at_zero)
8958 {
8959 /* A global or static variable may also have been stripped
8960 out by the linker if unused, in which case its address
8961 will be nullified; do not add such variables into partial
8962 symbol table then. */
8963 }
8964 else if (pdi->is_external)
8965 {
8966 /* Global Variable.
8967 Don't enter into the minimal symbol tables as there is
8968 a minimal symbol table entry from the ELF symbols already.
8969 Enter into partial symbol table if it has a location
8970 descriptor or a type.
8971 If the location descriptor is missing, new_symbol will create
8972 a LOC_UNRESOLVED symbol, the address of the variable will then
8973 be determined from the minimal symbol table whenever the variable
8974 is referenced.
8975 The address for the partial symbol table entry is not
8976 used by GDB, but it comes in handy for debugging partial symbol
8977 table building. */
8978
8979 if (pdi->d.locdesc || pdi->has_type)
8980 add_psymbol_to_list (actual_name, strlen (actual_name),
8981 built_actual_name != NULL,
8982 VAR_DOMAIN, LOC_STATIC,
8983 SECT_OFF_TEXT (objfile),
8984 psymbol_placement::GLOBAL,
8985 addr, cu->language, objfile);
8986 }
8987 else
8988 {
8989 int has_loc = pdi->d.locdesc != NULL;
8990
8991 /* Static Variable. Skip symbols whose value we cannot know (those
8992 without location descriptors or constant values). */
8993 if (!has_loc && !pdi->has_const_value)
8994 {
8995 xfree (built_actual_name);
8996 return;
8997 }
8998
8999 add_psymbol_to_list (actual_name, strlen (actual_name),
9000 built_actual_name != NULL,
9001 VAR_DOMAIN, LOC_STATIC,
9002 SECT_OFF_TEXT (objfile),
9003 psymbol_placement::STATIC,
9004 has_loc ? addr : 0,
9005 cu->language, objfile);
9006 }
9007 break;
9008 case DW_TAG_typedef:
9009 case DW_TAG_base_type:
9010 case DW_TAG_subrange_type:
9011 add_psymbol_to_list (actual_name, strlen (actual_name),
9012 built_actual_name != NULL,
9013 VAR_DOMAIN, LOC_TYPEDEF, -1,
9014 psymbol_placement::STATIC,
9015 0, cu->language, objfile);
9016 break;
9017 case DW_TAG_imported_declaration:
9018 case DW_TAG_namespace:
9019 add_psymbol_to_list (actual_name, strlen (actual_name),
9020 built_actual_name != NULL,
9021 VAR_DOMAIN, LOC_TYPEDEF, -1,
9022 psymbol_placement::GLOBAL,
9023 0, cu->language, objfile);
9024 break;
9025 case DW_TAG_module:
9026 add_psymbol_to_list (actual_name, strlen (actual_name),
9027 built_actual_name != NULL,
9028 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9029 psymbol_placement::GLOBAL,
9030 0, cu->language, objfile);
9031 break;
9032 case DW_TAG_class_type:
9033 case DW_TAG_interface_type:
9034 case DW_TAG_structure_type:
9035 case DW_TAG_union_type:
9036 case DW_TAG_enumeration_type:
9037 /* Skip external references. The DWARF standard says in the section
9038 about "Structure, Union, and Class Type Entries": "An incomplete
9039 structure, union or class type is represented by a structure,
9040 union or class entry that does not have a byte size attribute
9041 and that has a DW_AT_declaration attribute." */
9042 if (!pdi->has_byte_size && pdi->is_declaration)
9043 {
9044 xfree (built_actual_name);
9045 return;
9046 }
9047
9048 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9049 static vs. global. */
9050 add_psymbol_to_list (actual_name, strlen (actual_name),
9051 built_actual_name != NULL,
9052 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9053 cu->language == language_cplus
9054 ? psymbol_placement::GLOBAL
9055 : psymbol_placement::STATIC,
9056 0, cu->language, objfile);
9057
9058 break;
9059 case DW_TAG_enumerator:
9060 add_psymbol_to_list (actual_name, strlen (actual_name),
9061 built_actual_name != NULL,
9062 VAR_DOMAIN, LOC_CONST, -1,
9063 cu->language == language_cplus
9064 ? psymbol_placement::GLOBAL
9065 : psymbol_placement::STATIC,
9066 0, cu->language, objfile);
9067 break;
9068 default:
9069 break;
9070 }
9071
9072 xfree (built_actual_name);
9073 }
9074
9075 /* Read a partial die corresponding to a namespace; also, add a symbol
9076 corresponding to that namespace to the symbol table. NAMESPACE is
9077 the name of the enclosing namespace. */
9078
9079 static void
9080 add_partial_namespace (struct partial_die_info *pdi,
9081 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9082 int set_addrmap, struct dwarf2_cu *cu)
9083 {
9084 /* Add a symbol for the namespace. */
9085
9086 add_partial_symbol (pdi, cu);
9087
9088 /* Now scan partial symbols in that namespace. */
9089
9090 if (pdi->has_children)
9091 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9092 }
9093
9094 /* Read a partial die corresponding to a Fortran module. */
9095
9096 static void
9097 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9098 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9099 {
9100 /* Add a symbol for the namespace. */
9101
9102 add_partial_symbol (pdi, cu);
9103
9104 /* Now scan partial symbols in that module. */
9105
9106 if (pdi->has_children)
9107 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9108 }
9109
9110 /* Read a partial die corresponding to a subprogram or an inlined
9111 subprogram and create a partial symbol for that subprogram.
9112 When the CU language allows it, this routine also defines a partial
9113 symbol for each nested subprogram that this subprogram contains.
9114 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9115 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9116
9117 PDI may also be a lexical block, in which case we simply search
9118 recursively for subprograms defined inside that lexical block.
9119 Again, this is only performed when the CU language allows this
9120 type of definitions. */
9121
9122 static void
9123 add_partial_subprogram (struct partial_die_info *pdi,
9124 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9125 int set_addrmap, struct dwarf2_cu *cu)
9126 {
9127 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9128 {
9129 if (pdi->has_pc_info)
9130 {
9131 if (pdi->lowpc < *lowpc)
9132 *lowpc = pdi->lowpc;
9133 if (pdi->highpc > *highpc)
9134 *highpc = pdi->highpc;
9135 if (set_addrmap)
9136 {
9137 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9138 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9139 CORE_ADDR baseaddr;
9140 CORE_ADDR this_highpc;
9141 CORE_ADDR this_lowpc;
9142
9143 baseaddr = ANOFFSET (objfile->section_offsets,
9144 SECT_OFF_TEXT (objfile));
9145 this_lowpc
9146 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9147 pdi->lowpc + baseaddr)
9148 - baseaddr);
9149 this_highpc
9150 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9151 pdi->highpc + baseaddr)
9152 - baseaddr);
9153 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9154 this_lowpc, this_highpc - 1,
9155 cu->per_cu->v.psymtab);
9156 }
9157 }
9158
9159 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9160 {
9161 if (!pdi->is_declaration)
9162 /* Ignore subprogram DIEs that do not have a name, they are
9163 illegal. Do not emit a complaint at this point, we will
9164 do so when we convert this psymtab into a symtab. */
9165 if (pdi->name)
9166 add_partial_symbol (pdi, cu);
9167 }
9168 }
9169
9170 if (! pdi->has_children)
9171 return;
9172
9173 if (cu->language == language_ada)
9174 {
9175 pdi = pdi->die_child;
9176 while (pdi != NULL)
9177 {
9178 pdi->fixup (cu);
9179 if (pdi->tag == DW_TAG_subprogram
9180 || pdi->tag == DW_TAG_inlined_subroutine
9181 || pdi->tag == DW_TAG_lexical_block)
9182 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9183 pdi = pdi->die_sibling;
9184 }
9185 }
9186 }
9187
9188 /* Read a partial die corresponding to an enumeration type. */
9189
9190 static void
9191 add_partial_enumeration (struct partial_die_info *enum_pdi,
9192 struct dwarf2_cu *cu)
9193 {
9194 struct partial_die_info *pdi;
9195
9196 if (enum_pdi->name != NULL)
9197 add_partial_symbol (enum_pdi, cu);
9198
9199 pdi = enum_pdi->die_child;
9200 while (pdi)
9201 {
9202 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9203 complaint (_("malformed enumerator DIE ignored"));
9204 else
9205 add_partial_symbol (pdi, cu);
9206 pdi = pdi->die_sibling;
9207 }
9208 }
9209
9210 /* Return the initial uleb128 in the die at INFO_PTR. */
9211
9212 static unsigned int
9213 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9214 {
9215 unsigned int bytes_read;
9216
9217 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9218 }
9219
9220 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9221 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9222
9223 Return the corresponding abbrev, or NULL if the number is zero (indicating
9224 an empty DIE). In either case *BYTES_READ will be set to the length of
9225 the initial number. */
9226
9227 static struct abbrev_info *
9228 peek_die_abbrev (const die_reader_specs &reader,
9229 const gdb_byte *info_ptr, unsigned int *bytes_read)
9230 {
9231 dwarf2_cu *cu = reader.cu;
9232 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9233 unsigned int abbrev_number
9234 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9235
9236 if (abbrev_number == 0)
9237 return NULL;
9238
9239 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9240 if (!abbrev)
9241 {
9242 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9243 " at offset %s [in module %s]"),
9244 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9245 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9246 }
9247
9248 return abbrev;
9249 }
9250
9251 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9252 Returns a pointer to the end of a series of DIEs, terminated by an empty
9253 DIE. Any children of the skipped DIEs will also be skipped. */
9254
9255 static const gdb_byte *
9256 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9257 {
9258 while (1)
9259 {
9260 unsigned int bytes_read;
9261 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9262
9263 if (abbrev == NULL)
9264 return info_ptr + bytes_read;
9265 else
9266 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9267 }
9268 }
9269
9270 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9271 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9272 abbrev corresponding to that skipped uleb128 should be passed in
9273 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9274 children. */
9275
9276 static const gdb_byte *
9277 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9278 struct abbrev_info *abbrev)
9279 {
9280 unsigned int bytes_read;
9281 struct attribute attr;
9282 bfd *abfd = reader->abfd;
9283 struct dwarf2_cu *cu = reader->cu;
9284 const gdb_byte *buffer = reader->buffer;
9285 const gdb_byte *buffer_end = reader->buffer_end;
9286 unsigned int form, i;
9287
9288 for (i = 0; i < abbrev->num_attrs; i++)
9289 {
9290 /* The only abbrev we care about is DW_AT_sibling. */
9291 if (abbrev->attrs[i].name == DW_AT_sibling)
9292 {
9293 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9294 if (attr.form == DW_FORM_ref_addr)
9295 complaint (_("ignoring absolute DW_AT_sibling"));
9296 else
9297 {
9298 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9299 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9300
9301 if (sibling_ptr < info_ptr)
9302 complaint (_("DW_AT_sibling points backwards"));
9303 else if (sibling_ptr > reader->buffer_end)
9304 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9305 else
9306 return sibling_ptr;
9307 }
9308 }
9309
9310 /* If it isn't DW_AT_sibling, skip this attribute. */
9311 form = abbrev->attrs[i].form;
9312 skip_attribute:
9313 switch (form)
9314 {
9315 case DW_FORM_ref_addr:
9316 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9317 and later it is offset sized. */
9318 if (cu->header.version == 2)
9319 info_ptr += cu->header.addr_size;
9320 else
9321 info_ptr += cu->header.offset_size;
9322 break;
9323 case DW_FORM_GNU_ref_alt:
9324 info_ptr += cu->header.offset_size;
9325 break;
9326 case DW_FORM_addr:
9327 info_ptr += cu->header.addr_size;
9328 break;
9329 case DW_FORM_data1:
9330 case DW_FORM_ref1:
9331 case DW_FORM_flag:
9332 info_ptr += 1;
9333 break;
9334 case DW_FORM_flag_present:
9335 case DW_FORM_implicit_const:
9336 break;
9337 case DW_FORM_data2:
9338 case DW_FORM_ref2:
9339 info_ptr += 2;
9340 break;
9341 case DW_FORM_data4:
9342 case DW_FORM_ref4:
9343 info_ptr += 4;
9344 break;
9345 case DW_FORM_data8:
9346 case DW_FORM_ref8:
9347 case DW_FORM_ref_sig8:
9348 info_ptr += 8;
9349 break;
9350 case DW_FORM_data16:
9351 info_ptr += 16;
9352 break;
9353 case DW_FORM_string:
9354 read_direct_string (abfd, info_ptr, &bytes_read);
9355 info_ptr += bytes_read;
9356 break;
9357 case DW_FORM_sec_offset:
9358 case DW_FORM_strp:
9359 case DW_FORM_GNU_strp_alt:
9360 info_ptr += cu->header.offset_size;
9361 break;
9362 case DW_FORM_exprloc:
9363 case DW_FORM_block:
9364 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9365 info_ptr += bytes_read;
9366 break;
9367 case DW_FORM_block1:
9368 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9369 break;
9370 case DW_FORM_block2:
9371 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9372 break;
9373 case DW_FORM_block4:
9374 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9375 break;
9376 case DW_FORM_sdata:
9377 case DW_FORM_udata:
9378 case DW_FORM_ref_udata:
9379 case DW_FORM_GNU_addr_index:
9380 case DW_FORM_GNU_str_index:
9381 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9382 break;
9383 case DW_FORM_indirect:
9384 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9385 info_ptr += bytes_read;
9386 /* We need to continue parsing from here, so just go back to
9387 the top. */
9388 goto skip_attribute;
9389
9390 default:
9391 error (_("Dwarf Error: Cannot handle %s "
9392 "in DWARF reader [in module %s]"),
9393 dwarf_form_name (form),
9394 bfd_get_filename (abfd));
9395 }
9396 }
9397
9398 if (abbrev->has_children)
9399 return skip_children (reader, info_ptr);
9400 else
9401 return info_ptr;
9402 }
9403
9404 /* Locate ORIG_PDI's sibling.
9405 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9406
9407 static const gdb_byte *
9408 locate_pdi_sibling (const struct die_reader_specs *reader,
9409 struct partial_die_info *orig_pdi,
9410 const gdb_byte *info_ptr)
9411 {
9412 /* Do we know the sibling already? */
9413
9414 if (orig_pdi->sibling)
9415 return orig_pdi->sibling;
9416
9417 /* Are there any children to deal with? */
9418
9419 if (!orig_pdi->has_children)
9420 return info_ptr;
9421
9422 /* Skip the children the long way. */
9423
9424 return skip_children (reader, info_ptr);
9425 }
9426
9427 /* Expand this partial symbol table into a full symbol table. SELF is
9428 not NULL. */
9429
9430 static void
9431 dwarf2_read_symtab (struct partial_symtab *self,
9432 struct objfile *objfile)
9433 {
9434 struct dwarf2_per_objfile *dwarf2_per_objfile
9435 = get_dwarf2_per_objfile (objfile);
9436
9437 if (self->readin)
9438 {
9439 warning (_("bug: psymtab for %s is already read in."),
9440 self->filename);
9441 }
9442 else
9443 {
9444 if (info_verbose)
9445 {
9446 printf_filtered (_("Reading in symbols for %s..."),
9447 self->filename);
9448 gdb_flush (gdb_stdout);
9449 }
9450
9451 /* If this psymtab is constructed from a debug-only objfile, the
9452 has_section_at_zero flag will not necessarily be correct. We
9453 can get the correct value for this flag by looking at the data
9454 associated with the (presumably stripped) associated objfile. */
9455 if (objfile->separate_debug_objfile_backlink)
9456 {
9457 struct dwarf2_per_objfile *dpo_backlink
9458 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9459
9460 dwarf2_per_objfile->has_section_at_zero
9461 = dpo_backlink->has_section_at_zero;
9462 }
9463
9464 dwarf2_per_objfile->reading_partial_symbols = 0;
9465
9466 psymtab_to_symtab_1 (self);
9467
9468 /* Finish up the debug error message. */
9469 if (info_verbose)
9470 printf_filtered (_("done.\n"));
9471 }
9472
9473 process_cu_includes (dwarf2_per_objfile);
9474 }
9475 \f
9476 /* Reading in full CUs. */
9477
9478 /* Add PER_CU to the queue. */
9479
9480 static void
9481 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9482 enum language pretend_language)
9483 {
9484 struct dwarf2_queue_item *item;
9485
9486 per_cu->queued = 1;
9487 item = XNEW (struct dwarf2_queue_item);
9488 item->per_cu = per_cu;
9489 item->pretend_language = pretend_language;
9490 item->next = NULL;
9491
9492 if (dwarf2_queue == NULL)
9493 dwarf2_queue = item;
9494 else
9495 dwarf2_queue_tail->next = item;
9496
9497 dwarf2_queue_tail = item;
9498 }
9499
9500 /* If PER_CU is not yet queued, add it to the queue.
9501 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9502 dependency.
9503 The result is non-zero if PER_CU was queued, otherwise the result is zero
9504 meaning either PER_CU is already queued or it is already loaded.
9505
9506 N.B. There is an invariant here that if a CU is queued then it is loaded.
9507 The caller is required to load PER_CU if we return non-zero. */
9508
9509 static int
9510 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9511 struct dwarf2_per_cu_data *per_cu,
9512 enum language pretend_language)
9513 {
9514 /* We may arrive here during partial symbol reading, if we need full
9515 DIEs to process an unusual case (e.g. template arguments). Do
9516 not queue PER_CU, just tell our caller to load its DIEs. */
9517 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9518 {
9519 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9520 return 1;
9521 return 0;
9522 }
9523
9524 /* Mark the dependence relation so that we don't flush PER_CU
9525 too early. */
9526 if (dependent_cu != NULL)
9527 dwarf2_add_dependence (dependent_cu, per_cu);
9528
9529 /* If it's already on the queue, we have nothing to do. */
9530 if (per_cu->queued)
9531 return 0;
9532
9533 /* If the compilation unit is already loaded, just mark it as
9534 used. */
9535 if (per_cu->cu != NULL)
9536 {
9537 per_cu->cu->last_used = 0;
9538 return 0;
9539 }
9540
9541 /* Add it to the queue. */
9542 queue_comp_unit (per_cu, pretend_language);
9543
9544 return 1;
9545 }
9546
9547 /* Process the queue. */
9548
9549 static void
9550 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9551 {
9552 struct dwarf2_queue_item *item, *next_item;
9553
9554 if (dwarf_read_debug)
9555 {
9556 fprintf_unfiltered (gdb_stdlog,
9557 "Expanding one or more symtabs of objfile %s ...\n",
9558 objfile_name (dwarf2_per_objfile->objfile));
9559 }
9560
9561 /* The queue starts out with one item, but following a DIE reference
9562 may load a new CU, adding it to the end of the queue. */
9563 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9564 {
9565 if ((dwarf2_per_objfile->using_index
9566 ? !item->per_cu->v.quick->compunit_symtab
9567 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9568 /* Skip dummy CUs. */
9569 && item->per_cu->cu != NULL)
9570 {
9571 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9572 unsigned int debug_print_threshold;
9573 char buf[100];
9574
9575 if (per_cu->is_debug_types)
9576 {
9577 struct signatured_type *sig_type =
9578 (struct signatured_type *) per_cu;
9579
9580 sprintf (buf, "TU %s at offset %s",
9581 hex_string (sig_type->signature),
9582 sect_offset_str (per_cu->sect_off));
9583 /* There can be 100s of TUs.
9584 Only print them in verbose mode. */
9585 debug_print_threshold = 2;
9586 }
9587 else
9588 {
9589 sprintf (buf, "CU at offset %s",
9590 sect_offset_str (per_cu->sect_off));
9591 debug_print_threshold = 1;
9592 }
9593
9594 if (dwarf_read_debug >= debug_print_threshold)
9595 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9596
9597 if (per_cu->is_debug_types)
9598 process_full_type_unit (per_cu, item->pretend_language);
9599 else
9600 process_full_comp_unit (per_cu, item->pretend_language);
9601
9602 if (dwarf_read_debug >= debug_print_threshold)
9603 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9604 }
9605
9606 item->per_cu->queued = 0;
9607 next_item = item->next;
9608 xfree (item);
9609 }
9610
9611 dwarf2_queue_tail = NULL;
9612
9613 if (dwarf_read_debug)
9614 {
9615 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9616 objfile_name (dwarf2_per_objfile->objfile));
9617 }
9618 }
9619
9620 /* Read in full symbols for PST, and anything it depends on. */
9621
9622 static void
9623 psymtab_to_symtab_1 (struct partial_symtab *pst)
9624 {
9625 struct dwarf2_per_cu_data *per_cu;
9626 int i;
9627
9628 if (pst->readin)
9629 return;
9630
9631 for (i = 0; i < pst->number_of_dependencies; i++)
9632 if (!pst->dependencies[i]->readin
9633 && pst->dependencies[i]->user == NULL)
9634 {
9635 /* Inform about additional files that need to be read in. */
9636 if (info_verbose)
9637 {
9638 /* FIXME: i18n: Need to make this a single string. */
9639 fputs_filtered (" ", gdb_stdout);
9640 wrap_here ("");
9641 fputs_filtered ("and ", gdb_stdout);
9642 wrap_here ("");
9643 printf_filtered ("%s...", pst->dependencies[i]->filename);
9644 wrap_here (""); /* Flush output. */
9645 gdb_flush (gdb_stdout);
9646 }
9647 psymtab_to_symtab_1 (pst->dependencies[i]);
9648 }
9649
9650 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9651
9652 if (per_cu == NULL)
9653 {
9654 /* It's an include file, no symbols to read for it.
9655 Everything is in the parent symtab. */
9656 pst->readin = 1;
9657 return;
9658 }
9659
9660 dw2_do_instantiate_symtab (per_cu, false);
9661 }
9662
9663 /* Trivial hash function for die_info: the hash value of a DIE
9664 is its offset in .debug_info for this objfile. */
9665
9666 static hashval_t
9667 die_hash (const void *item)
9668 {
9669 const struct die_info *die = (const struct die_info *) item;
9670
9671 return to_underlying (die->sect_off);
9672 }
9673
9674 /* Trivial comparison function for die_info structures: two DIEs
9675 are equal if they have the same offset. */
9676
9677 static int
9678 die_eq (const void *item_lhs, const void *item_rhs)
9679 {
9680 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9681 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9682
9683 return die_lhs->sect_off == die_rhs->sect_off;
9684 }
9685
9686 /* die_reader_func for load_full_comp_unit.
9687 This is identical to read_signatured_type_reader,
9688 but is kept separate for now. */
9689
9690 static void
9691 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9692 const gdb_byte *info_ptr,
9693 struct die_info *comp_unit_die,
9694 int has_children,
9695 void *data)
9696 {
9697 struct dwarf2_cu *cu = reader->cu;
9698 enum language *language_ptr = (enum language *) data;
9699
9700 gdb_assert (cu->die_hash == NULL);
9701 cu->die_hash =
9702 htab_create_alloc_ex (cu->header.length / 12,
9703 die_hash,
9704 die_eq,
9705 NULL,
9706 &cu->comp_unit_obstack,
9707 hashtab_obstack_allocate,
9708 dummy_obstack_deallocate);
9709
9710 if (has_children)
9711 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9712 &info_ptr, comp_unit_die);
9713 cu->dies = comp_unit_die;
9714 /* comp_unit_die is not stored in die_hash, no need. */
9715
9716 /* We try not to read any attributes in this function, because not
9717 all CUs needed for references have been loaded yet, and symbol
9718 table processing isn't initialized. But we have to set the CU language,
9719 or we won't be able to build types correctly.
9720 Similarly, if we do not read the producer, we can not apply
9721 producer-specific interpretation. */
9722 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9723 }
9724
9725 /* Load the DIEs associated with PER_CU into memory. */
9726
9727 static void
9728 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9729 bool skip_partial,
9730 enum language pretend_language)
9731 {
9732 gdb_assert (! this_cu->is_debug_types);
9733
9734 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9735 load_full_comp_unit_reader, &pretend_language);
9736 }
9737
9738 /* Add a DIE to the delayed physname list. */
9739
9740 static void
9741 add_to_method_list (struct type *type, int fnfield_index, int index,
9742 const char *name, struct die_info *die,
9743 struct dwarf2_cu *cu)
9744 {
9745 struct delayed_method_info mi;
9746 mi.type = type;
9747 mi.fnfield_index = fnfield_index;
9748 mi.index = index;
9749 mi.name = name;
9750 mi.die = die;
9751 cu->method_list.push_back (mi);
9752 }
9753
9754 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9755 "const" / "volatile". If so, decrements LEN by the length of the
9756 modifier and return true. Otherwise return false. */
9757
9758 template<size_t N>
9759 static bool
9760 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9761 {
9762 size_t mod_len = sizeof (mod) - 1;
9763 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9764 {
9765 len -= mod_len;
9766 return true;
9767 }
9768 return false;
9769 }
9770
9771 /* Compute the physnames of any methods on the CU's method list.
9772
9773 The computation of method physnames is delayed in order to avoid the
9774 (bad) condition that one of the method's formal parameters is of an as yet
9775 incomplete type. */
9776
9777 static void
9778 compute_delayed_physnames (struct dwarf2_cu *cu)
9779 {
9780 /* Only C++ delays computing physnames. */
9781 if (cu->method_list.empty ())
9782 return;
9783 gdb_assert (cu->language == language_cplus);
9784
9785 for (const delayed_method_info &mi : cu->method_list)
9786 {
9787 const char *physname;
9788 struct fn_fieldlist *fn_flp
9789 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9790 physname = dwarf2_physname (mi.name, mi.die, cu);
9791 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9792 = physname ? physname : "";
9793
9794 /* Since there's no tag to indicate whether a method is a
9795 const/volatile overload, extract that information out of the
9796 demangled name. */
9797 if (physname != NULL)
9798 {
9799 size_t len = strlen (physname);
9800
9801 while (1)
9802 {
9803 if (physname[len] == ')') /* shortcut */
9804 break;
9805 else if (check_modifier (physname, len, " const"))
9806 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9807 else if (check_modifier (physname, len, " volatile"))
9808 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9809 else
9810 break;
9811 }
9812 }
9813 }
9814
9815 /* The list is no longer needed. */
9816 cu->method_list.clear ();
9817 }
9818
9819 /* Go objects should be embedded in a DW_TAG_module DIE,
9820 and it's not clear if/how imported objects will appear.
9821 To keep Go support simple until that's worked out,
9822 go back through what we've read and create something usable.
9823 We could do this while processing each DIE, and feels kinda cleaner,
9824 but that way is more invasive.
9825 This is to, for example, allow the user to type "p var" or "b main"
9826 without having to specify the package name, and allow lookups
9827 of module.object to work in contexts that use the expression
9828 parser. */
9829
9830 static void
9831 fixup_go_packaging (struct dwarf2_cu *cu)
9832 {
9833 char *package_name = NULL;
9834 struct pending *list;
9835 int i;
9836
9837 for (list = *cu->get_builder ()->get_global_symbols ();
9838 list != NULL;
9839 list = list->next)
9840 {
9841 for (i = 0; i < list->nsyms; ++i)
9842 {
9843 struct symbol *sym = list->symbol[i];
9844
9845 if (SYMBOL_LANGUAGE (sym) == language_go
9846 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9847 {
9848 char *this_package_name = go_symbol_package_name (sym);
9849
9850 if (this_package_name == NULL)
9851 continue;
9852 if (package_name == NULL)
9853 package_name = this_package_name;
9854 else
9855 {
9856 struct objfile *objfile
9857 = cu->per_cu->dwarf2_per_objfile->objfile;
9858 if (strcmp (package_name, this_package_name) != 0)
9859 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9860 (symbol_symtab (sym) != NULL
9861 ? symtab_to_filename_for_display
9862 (symbol_symtab (sym))
9863 : objfile_name (objfile)),
9864 this_package_name, package_name);
9865 xfree (this_package_name);
9866 }
9867 }
9868 }
9869 }
9870
9871 if (package_name != NULL)
9872 {
9873 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9874 const char *saved_package_name
9875 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9876 package_name,
9877 strlen (package_name));
9878 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9879 saved_package_name);
9880 struct symbol *sym;
9881
9882 sym = allocate_symbol (objfile);
9883 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9884 SYMBOL_SET_NAMES (sym, saved_package_name,
9885 strlen (saved_package_name), 0, objfile);
9886 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9887 e.g., "main" finds the "main" module and not C's main(). */
9888 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9889 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9890 SYMBOL_TYPE (sym) = type;
9891
9892 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9893
9894 xfree (package_name);
9895 }
9896 }
9897
9898 /* Allocate a fully-qualified name consisting of the two parts on the
9899 obstack. */
9900
9901 static const char *
9902 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9903 {
9904 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9905 }
9906
9907 /* A helper that allocates a struct discriminant_info to attach to a
9908 union type. */
9909
9910 static struct discriminant_info *
9911 alloc_discriminant_info (struct type *type, int discriminant_index,
9912 int default_index)
9913 {
9914 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9915 gdb_assert (discriminant_index == -1
9916 || (discriminant_index >= 0
9917 && discriminant_index < TYPE_NFIELDS (type)));
9918 gdb_assert (default_index == -1
9919 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9920
9921 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9922
9923 struct discriminant_info *disc
9924 = ((struct discriminant_info *)
9925 TYPE_ZALLOC (type,
9926 offsetof (struct discriminant_info, discriminants)
9927 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9928 disc->default_index = default_index;
9929 disc->discriminant_index = discriminant_index;
9930
9931 struct dynamic_prop prop;
9932 prop.kind = PROP_UNDEFINED;
9933 prop.data.baton = disc;
9934
9935 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9936
9937 return disc;
9938 }
9939
9940 /* Some versions of rustc emitted enums in an unusual way.
9941
9942 Ordinary enums were emitted as unions. The first element of each
9943 structure in the union was named "RUST$ENUM$DISR". This element
9944 held the discriminant.
9945
9946 These versions of Rust also implemented the "non-zero"
9947 optimization. When the enum had two values, and one is empty and
9948 the other holds a pointer that cannot be zero, the pointer is used
9949 as the discriminant, with a zero value meaning the empty variant.
9950 Here, the union's first member is of the form
9951 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9952 where the fieldnos are the indices of the fields that should be
9953 traversed in order to find the field (which may be several fields deep)
9954 and the variantname is the name of the variant of the case when the
9955 field is zero.
9956
9957 This function recognizes whether TYPE is of one of these forms,
9958 and, if so, smashes it to be a variant type. */
9959
9960 static void
9961 quirk_rust_enum (struct type *type, struct objfile *objfile)
9962 {
9963 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9964
9965 /* We don't need to deal with empty enums. */
9966 if (TYPE_NFIELDS (type) == 0)
9967 return;
9968
9969 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9970 if (TYPE_NFIELDS (type) == 1
9971 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9972 {
9973 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9974
9975 /* Decode the field name to find the offset of the
9976 discriminant. */
9977 ULONGEST bit_offset = 0;
9978 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9979 while (name[0] >= '0' && name[0] <= '9')
9980 {
9981 char *tail;
9982 unsigned long index = strtoul (name, &tail, 10);
9983 name = tail;
9984 if (*name != '$'
9985 || index >= TYPE_NFIELDS (field_type)
9986 || (TYPE_FIELD_LOC_KIND (field_type, index)
9987 != FIELD_LOC_KIND_BITPOS))
9988 {
9989 complaint (_("Could not parse Rust enum encoding string \"%s\""
9990 "[in module %s]"),
9991 TYPE_FIELD_NAME (type, 0),
9992 objfile_name (objfile));
9993 return;
9994 }
9995 ++name;
9996
9997 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9998 field_type = TYPE_FIELD_TYPE (field_type, index);
9999 }
10000
10001 /* Make a union to hold the variants. */
10002 struct type *union_type = alloc_type (objfile);
10003 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10004 TYPE_NFIELDS (union_type) = 3;
10005 TYPE_FIELDS (union_type)
10006 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10007 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10008 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10009
10010 /* Put the discriminant must at index 0. */
10011 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10012 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10013 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10014 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10015
10016 /* The order of fields doesn't really matter, so put the real
10017 field at index 1 and the data-less field at index 2. */
10018 struct discriminant_info *disc
10019 = alloc_discriminant_info (union_type, 0, 1);
10020 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10021 TYPE_FIELD_NAME (union_type, 1)
10022 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10023 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10024 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10025 TYPE_FIELD_NAME (union_type, 1));
10026
10027 const char *dataless_name
10028 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10029 name);
10030 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10031 dataless_name);
10032 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10033 /* NAME points into the original discriminant name, which
10034 already has the correct lifetime. */
10035 TYPE_FIELD_NAME (union_type, 2) = name;
10036 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10037 disc->discriminants[2] = 0;
10038
10039 /* Smash this type to be a structure type. We have to do this
10040 because the type has already been recorded. */
10041 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10042 TYPE_NFIELDS (type) = 1;
10043 TYPE_FIELDS (type)
10044 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10045
10046 /* Install the variant part. */
10047 TYPE_FIELD_TYPE (type, 0) = union_type;
10048 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10049 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10050 }
10051 else if (TYPE_NFIELDS (type) == 1)
10052 {
10053 /* We assume that a union with a single field is a univariant
10054 enum. */
10055 /* Smash this type to be a structure type. We have to do this
10056 because the type has already been recorded. */
10057 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10058
10059 /* Make a union to hold the variants. */
10060 struct type *union_type = alloc_type (objfile);
10061 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10062 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10063 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10064 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10065 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10066
10067 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10068 const char *variant_name
10069 = rust_last_path_segment (TYPE_NAME (field_type));
10070 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10071 TYPE_NAME (field_type)
10072 = rust_fully_qualify (&objfile->objfile_obstack,
10073 TYPE_NAME (type), variant_name);
10074
10075 /* Install the union in the outer struct type. */
10076 TYPE_NFIELDS (type) = 1;
10077 TYPE_FIELDS (type)
10078 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10079 TYPE_FIELD_TYPE (type, 0) = union_type;
10080 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10081 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10082
10083 alloc_discriminant_info (union_type, -1, 0);
10084 }
10085 else
10086 {
10087 struct type *disr_type = nullptr;
10088 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10089 {
10090 disr_type = TYPE_FIELD_TYPE (type, i);
10091
10092 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10093 {
10094 /* All fields of a true enum will be structs. */
10095 return;
10096 }
10097 else if (TYPE_NFIELDS (disr_type) == 0)
10098 {
10099 /* Could be data-less variant, so keep going. */
10100 disr_type = nullptr;
10101 }
10102 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10103 "RUST$ENUM$DISR") != 0)
10104 {
10105 /* Not a Rust enum. */
10106 return;
10107 }
10108 else
10109 {
10110 /* Found one. */
10111 break;
10112 }
10113 }
10114
10115 /* If we got here without a discriminant, then it's probably
10116 just a union. */
10117 if (disr_type == nullptr)
10118 return;
10119
10120 /* Smash this type to be a structure type. We have to do this
10121 because the type has already been recorded. */
10122 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10123
10124 /* Make a union to hold the variants. */
10125 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10126 struct type *union_type = alloc_type (objfile);
10127 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10128 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10129 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10130 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10131 TYPE_FIELDS (union_type)
10132 = (struct field *) TYPE_ZALLOC (union_type,
10133 (TYPE_NFIELDS (union_type)
10134 * sizeof (struct field)));
10135
10136 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10137 TYPE_NFIELDS (type) * sizeof (struct field));
10138
10139 /* Install the discriminant at index 0 in the union. */
10140 TYPE_FIELD (union_type, 0) = *disr_field;
10141 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10142 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10143
10144 /* Install the union in the outer struct type. */
10145 TYPE_FIELD_TYPE (type, 0) = union_type;
10146 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10147 TYPE_NFIELDS (type) = 1;
10148
10149 /* Set the size and offset of the union type. */
10150 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10151
10152 /* We need a way to find the correct discriminant given a
10153 variant name. For convenience we build a map here. */
10154 struct type *enum_type = FIELD_TYPE (*disr_field);
10155 std::unordered_map<std::string, ULONGEST> discriminant_map;
10156 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10157 {
10158 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10159 {
10160 const char *name
10161 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10162 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10163 }
10164 }
10165
10166 int n_fields = TYPE_NFIELDS (union_type);
10167 struct discriminant_info *disc
10168 = alloc_discriminant_info (union_type, 0, -1);
10169 /* Skip the discriminant here. */
10170 for (int i = 1; i < n_fields; ++i)
10171 {
10172 /* Find the final word in the name of this variant's type.
10173 That name can be used to look up the correct
10174 discriminant. */
10175 const char *variant_name
10176 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10177 i)));
10178
10179 auto iter = discriminant_map.find (variant_name);
10180 if (iter != discriminant_map.end ())
10181 disc->discriminants[i] = iter->second;
10182
10183 /* Remove the discriminant field, if it exists. */
10184 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10185 if (TYPE_NFIELDS (sub_type) > 0)
10186 {
10187 --TYPE_NFIELDS (sub_type);
10188 ++TYPE_FIELDS (sub_type);
10189 }
10190 TYPE_FIELD_NAME (union_type, i) = variant_name;
10191 TYPE_NAME (sub_type)
10192 = rust_fully_qualify (&objfile->objfile_obstack,
10193 TYPE_NAME (type), variant_name);
10194 }
10195 }
10196 }
10197
10198 /* Rewrite some Rust unions to be structures with variants parts. */
10199
10200 static void
10201 rust_union_quirks (struct dwarf2_cu *cu)
10202 {
10203 gdb_assert (cu->language == language_rust);
10204 for (type *type_ : cu->rust_unions)
10205 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10206 /* We don't need this any more. */
10207 cu->rust_unions.clear ();
10208 }
10209
10210 /* Return the symtab for PER_CU. This works properly regardless of
10211 whether we're using the index or psymtabs. */
10212
10213 static struct compunit_symtab *
10214 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10215 {
10216 return (per_cu->dwarf2_per_objfile->using_index
10217 ? per_cu->v.quick->compunit_symtab
10218 : per_cu->v.psymtab->compunit_symtab);
10219 }
10220
10221 /* A helper function for computing the list of all symbol tables
10222 included by PER_CU. */
10223
10224 static void
10225 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10226 htab_t all_children, htab_t all_type_symtabs,
10227 struct dwarf2_per_cu_data *per_cu,
10228 struct compunit_symtab *immediate_parent)
10229 {
10230 void **slot;
10231 int ix;
10232 struct compunit_symtab *cust;
10233 struct dwarf2_per_cu_data *iter;
10234
10235 slot = htab_find_slot (all_children, per_cu, INSERT);
10236 if (*slot != NULL)
10237 {
10238 /* This inclusion and its children have been processed. */
10239 return;
10240 }
10241
10242 *slot = per_cu;
10243 /* Only add a CU if it has a symbol table. */
10244 cust = get_compunit_symtab (per_cu);
10245 if (cust != NULL)
10246 {
10247 /* If this is a type unit only add its symbol table if we haven't
10248 seen it yet (type unit per_cu's can share symtabs). */
10249 if (per_cu->is_debug_types)
10250 {
10251 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10252 if (*slot == NULL)
10253 {
10254 *slot = cust;
10255 result->push_back (cust);
10256 if (cust->user == NULL)
10257 cust->user = immediate_parent;
10258 }
10259 }
10260 else
10261 {
10262 result->push_back (cust);
10263 if (cust->user == NULL)
10264 cust->user = immediate_parent;
10265 }
10266 }
10267
10268 for (ix = 0;
10269 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10270 ++ix)
10271 {
10272 recursively_compute_inclusions (result, all_children,
10273 all_type_symtabs, iter, cust);
10274 }
10275 }
10276
10277 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10278 PER_CU. */
10279
10280 static void
10281 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10282 {
10283 gdb_assert (! per_cu->is_debug_types);
10284
10285 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10286 {
10287 int ix, len;
10288 struct dwarf2_per_cu_data *per_cu_iter;
10289 std::vector<compunit_symtab *> result_symtabs;
10290 htab_t all_children, all_type_symtabs;
10291 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10292
10293 /* If we don't have a symtab, we can just skip this case. */
10294 if (cust == NULL)
10295 return;
10296
10297 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10298 NULL, xcalloc, xfree);
10299 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10300 NULL, xcalloc, xfree);
10301
10302 for (ix = 0;
10303 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10304 ix, per_cu_iter);
10305 ++ix)
10306 {
10307 recursively_compute_inclusions (&result_symtabs, all_children,
10308 all_type_symtabs, per_cu_iter,
10309 cust);
10310 }
10311
10312 /* Now we have a transitive closure of all the included symtabs. */
10313 len = result_symtabs.size ();
10314 cust->includes
10315 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10316 struct compunit_symtab *, len + 1);
10317 memcpy (cust->includes, result_symtabs.data (),
10318 len * sizeof (compunit_symtab *));
10319 cust->includes[len] = NULL;
10320
10321 htab_delete (all_children);
10322 htab_delete (all_type_symtabs);
10323 }
10324 }
10325
10326 /* Compute the 'includes' field for the symtabs of all the CUs we just
10327 read. */
10328
10329 static void
10330 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10331 {
10332 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10333 {
10334 if (! iter->is_debug_types)
10335 compute_compunit_symtab_includes (iter);
10336 }
10337
10338 dwarf2_per_objfile->just_read_cus.clear ();
10339 }
10340
10341 /* Generate full symbol information for PER_CU, whose DIEs have
10342 already been loaded into memory. */
10343
10344 static void
10345 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10346 enum language pretend_language)
10347 {
10348 struct dwarf2_cu *cu = per_cu->cu;
10349 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10350 struct objfile *objfile = dwarf2_per_objfile->objfile;
10351 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10352 CORE_ADDR lowpc, highpc;
10353 struct compunit_symtab *cust;
10354 CORE_ADDR baseaddr;
10355 struct block *static_block;
10356 CORE_ADDR addr;
10357
10358 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10359
10360 /* Clear the list here in case something was left over. */
10361 cu->method_list.clear ();
10362
10363 cu->language = pretend_language;
10364 cu->language_defn = language_def (cu->language);
10365
10366 /* Do line number decoding in read_file_scope () */
10367 process_die (cu->dies, cu);
10368
10369 /* For now fudge the Go package. */
10370 if (cu->language == language_go)
10371 fixup_go_packaging (cu);
10372
10373 /* Now that we have processed all the DIEs in the CU, all the types
10374 should be complete, and it should now be safe to compute all of the
10375 physnames. */
10376 compute_delayed_physnames (cu);
10377
10378 if (cu->language == language_rust)
10379 rust_union_quirks (cu);
10380
10381 /* Some compilers don't define a DW_AT_high_pc attribute for the
10382 compilation unit. If the DW_AT_high_pc is missing, synthesize
10383 it, by scanning the DIE's below the compilation unit. */
10384 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10385
10386 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10387 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10388
10389 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10390 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10391 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10392 addrmap to help ensure it has an accurate map of pc values belonging to
10393 this comp unit. */
10394 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10395
10396 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10397 SECT_OFF_TEXT (objfile),
10398 0);
10399
10400 if (cust != NULL)
10401 {
10402 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10403
10404 /* Set symtab language to language from DW_AT_language. If the
10405 compilation is from a C file generated by language preprocessors, do
10406 not set the language if it was already deduced by start_subfile. */
10407 if (!(cu->language == language_c
10408 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10409 COMPUNIT_FILETABS (cust)->language = cu->language;
10410
10411 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10412 produce DW_AT_location with location lists but it can be possibly
10413 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10414 there were bugs in prologue debug info, fixed later in GCC-4.5
10415 by "unwind info for epilogues" patch (which is not directly related).
10416
10417 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10418 needed, it would be wrong due to missing DW_AT_producer there.
10419
10420 Still one can confuse GDB by using non-standard GCC compilation
10421 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10422 */
10423 if (cu->has_loclist && gcc_4_minor >= 5)
10424 cust->locations_valid = 1;
10425
10426 if (gcc_4_minor >= 5)
10427 cust->epilogue_unwind_valid = 1;
10428
10429 cust->call_site_htab = cu->call_site_htab;
10430 }
10431
10432 if (dwarf2_per_objfile->using_index)
10433 per_cu->v.quick->compunit_symtab = cust;
10434 else
10435 {
10436 struct partial_symtab *pst = per_cu->v.psymtab;
10437 pst->compunit_symtab = cust;
10438 pst->readin = 1;
10439 }
10440
10441 /* Push it for inclusion processing later. */
10442 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10443
10444 /* Not needed any more. */
10445 cu->reset_builder ();
10446 }
10447
10448 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10449 already been loaded into memory. */
10450
10451 static void
10452 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10453 enum language pretend_language)
10454 {
10455 struct dwarf2_cu *cu = per_cu->cu;
10456 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10457 struct objfile *objfile = dwarf2_per_objfile->objfile;
10458 struct compunit_symtab *cust;
10459 struct signatured_type *sig_type;
10460
10461 gdb_assert (per_cu->is_debug_types);
10462 sig_type = (struct signatured_type *) per_cu;
10463
10464 /* Clear the list here in case something was left over. */
10465 cu->method_list.clear ();
10466
10467 cu->language = pretend_language;
10468 cu->language_defn = language_def (cu->language);
10469
10470 /* The symbol tables are set up in read_type_unit_scope. */
10471 process_die (cu->dies, cu);
10472
10473 /* For now fudge the Go package. */
10474 if (cu->language == language_go)
10475 fixup_go_packaging (cu);
10476
10477 /* Now that we have processed all the DIEs in the CU, all the types
10478 should be complete, and it should now be safe to compute all of the
10479 physnames. */
10480 compute_delayed_physnames (cu);
10481
10482 if (cu->language == language_rust)
10483 rust_union_quirks (cu);
10484
10485 /* TUs share symbol tables.
10486 If this is the first TU to use this symtab, complete the construction
10487 of it with end_expandable_symtab. Otherwise, complete the addition of
10488 this TU's symbols to the existing symtab. */
10489 if (sig_type->type_unit_group->compunit_symtab == NULL)
10490 {
10491 buildsym_compunit *builder = cu->get_builder ();
10492 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10493 sig_type->type_unit_group->compunit_symtab = cust;
10494
10495 if (cust != NULL)
10496 {
10497 /* Set symtab language to language from DW_AT_language. If the
10498 compilation is from a C file generated by language preprocessors,
10499 do not set the language if it was already deduced by
10500 start_subfile. */
10501 if (!(cu->language == language_c
10502 && COMPUNIT_FILETABS (cust)->language != language_c))
10503 COMPUNIT_FILETABS (cust)->language = cu->language;
10504 }
10505 }
10506 else
10507 {
10508 cu->get_builder ()->augment_type_symtab ();
10509 cust = sig_type->type_unit_group->compunit_symtab;
10510 }
10511
10512 if (dwarf2_per_objfile->using_index)
10513 per_cu->v.quick->compunit_symtab = cust;
10514 else
10515 {
10516 struct partial_symtab *pst = per_cu->v.psymtab;
10517 pst->compunit_symtab = cust;
10518 pst->readin = 1;
10519 }
10520
10521 /* Not needed any more. */
10522 cu->reset_builder ();
10523 }
10524
10525 /* Process an imported unit DIE. */
10526
10527 static void
10528 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10529 {
10530 struct attribute *attr;
10531
10532 /* For now we don't handle imported units in type units. */
10533 if (cu->per_cu->is_debug_types)
10534 {
10535 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10536 " supported in type units [in module %s]"),
10537 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10538 }
10539
10540 attr = dwarf2_attr (die, DW_AT_import, cu);
10541 if (attr != NULL)
10542 {
10543 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10544 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10545 dwarf2_per_cu_data *per_cu
10546 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10547 cu->per_cu->dwarf2_per_objfile);
10548
10549 /* If necessary, add it to the queue and load its DIEs. */
10550 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10551 load_full_comp_unit (per_cu, false, cu->language);
10552
10553 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10554 per_cu);
10555 }
10556 }
10557
10558 /* RAII object that represents a process_die scope: i.e.,
10559 starts/finishes processing a DIE. */
10560 class process_die_scope
10561 {
10562 public:
10563 process_die_scope (die_info *die, dwarf2_cu *cu)
10564 : m_die (die), m_cu (cu)
10565 {
10566 /* We should only be processing DIEs not already in process. */
10567 gdb_assert (!m_die->in_process);
10568 m_die->in_process = true;
10569 }
10570
10571 ~process_die_scope ()
10572 {
10573 m_die->in_process = false;
10574
10575 /* If we're done processing the DIE for the CU that owns the line
10576 header, we don't need the line header anymore. */
10577 if (m_cu->line_header_die_owner == m_die)
10578 {
10579 delete m_cu->line_header;
10580 m_cu->line_header = NULL;
10581 m_cu->line_header_die_owner = NULL;
10582 }
10583 }
10584
10585 private:
10586 die_info *m_die;
10587 dwarf2_cu *m_cu;
10588 };
10589
10590 /* Process a die and its children. */
10591
10592 static void
10593 process_die (struct die_info *die, struct dwarf2_cu *cu)
10594 {
10595 process_die_scope scope (die, cu);
10596
10597 switch (die->tag)
10598 {
10599 case DW_TAG_padding:
10600 break;
10601 case DW_TAG_compile_unit:
10602 case DW_TAG_partial_unit:
10603 read_file_scope (die, cu);
10604 break;
10605 case DW_TAG_type_unit:
10606 read_type_unit_scope (die, cu);
10607 break;
10608 case DW_TAG_subprogram:
10609 case DW_TAG_inlined_subroutine:
10610 read_func_scope (die, cu);
10611 break;
10612 case DW_TAG_lexical_block:
10613 case DW_TAG_try_block:
10614 case DW_TAG_catch_block:
10615 read_lexical_block_scope (die, cu);
10616 break;
10617 case DW_TAG_call_site:
10618 case DW_TAG_GNU_call_site:
10619 read_call_site_scope (die, cu);
10620 break;
10621 case DW_TAG_class_type:
10622 case DW_TAG_interface_type:
10623 case DW_TAG_structure_type:
10624 case DW_TAG_union_type:
10625 process_structure_scope (die, cu);
10626 break;
10627 case DW_TAG_enumeration_type:
10628 process_enumeration_scope (die, cu);
10629 break;
10630
10631 /* These dies have a type, but processing them does not create
10632 a symbol or recurse to process the children. Therefore we can
10633 read them on-demand through read_type_die. */
10634 case DW_TAG_subroutine_type:
10635 case DW_TAG_set_type:
10636 case DW_TAG_array_type:
10637 case DW_TAG_pointer_type:
10638 case DW_TAG_ptr_to_member_type:
10639 case DW_TAG_reference_type:
10640 case DW_TAG_rvalue_reference_type:
10641 case DW_TAG_string_type:
10642 break;
10643
10644 case DW_TAG_base_type:
10645 case DW_TAG_subrange_type:
10646 case DW_TAG_typedef:
10647 /* Add a typedef symbol for the type definition, if it has a
10648 DW_AT_name. */
10649 new_symbol (die, read_type_die (die, cu), cu);
10650 break;
10651 case DW_TAG_common_block:
10652 read_common_block (die, cu);
10653 break;
10654 case DW_TAG_common_inclusion:
10655 break;
10656 case DW_TAG_namespace:
10657 cu->processing_has_namespace_info = true;
10658 read_namespace (die, cu);
10659 break;
10660 case DW_TAG_module:
10661 cu->processing_has_namespace_info = true;
10662 read_module (die, cu);
10663 break;
10664 case DW_TAG_imported_declaration:
10665 cu->processing_has_namespace_info = true;
10666 if (read_namespace_alias (die, cu))
10667 break;
10668 /* The declaration is not a global namespace alias. */
10669 /* Fall through. */
10670 case DW_TAG_imported_module:
10671 cu->processing_has_namespace_info = true;
10672 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10673 || cu->language != language_fortran))
10674 complaint (_("Tag '%s' has unexpected children"),
10675 dwarf_tag_name (die->tag));
10676 read_import_statement (die, cu);
10677 break;
10678
10679 case DW_TAG_imported_unit:
10680 process_imported_unit_die (die, cu);
10681 break;
10682
10683 case DW_TAG_variable:
10684 read_variable (die, cu);
10685 break;
10686
10687 default:
10688 new_symbol (die, NULL, cu);
10689 break;
10690 }
10691 }
10692 \f
10693 /* DWARF name computation. */
10694
10695 /* A helper function for dwarf2_compute_name which determines whether DIE
10696 needs to have the name of the scope prepended to the name listed in the
10697 die. */
10698
10699 static int
10700 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10701 {
10702 struct attribute *attr;
10703
10704 switch (die->tag)
10705 {
10706 case DW_TAG_namespace:
10707 case DW_TAG_typedef:
10708 case DW_TAG_class_type:
10709 case DW_TAG_interface_type:
10710 case DW_TAG_structure_type:
10711 case DW_TAG_union_type:
10712 case DW_TAG_enumeration_type:
10713 case DW_TAG_enumerator:
10714 case DW_TAG_subprogram:
10715 case DW_TAG_inlined_subroutine:
10716 case DW_TAG_member:
10717 case DW_TAG_imported_declaration:
10718 return 1;
10719
10720 case DW_TAG_variable:
10721 case DW_TAG_constant:
10722 /* We only need to prefix "globally" visible variables. These include
10723 any variable marked with DW_AT_external or any variable that
10724 lives in a namespace. [Variables in anonymous namespaces
10725 require prefixing, but they are not DW_AT_external.] */
10726
10727 if (dwarf2_attr (die, DW_AT_specification, cu))
10728 {
10729 struct dwarf2_cu *spec_cu = cu;
10730
10731 return die_needs_namespace (die_specification (die, &spec_cu),
10732 spec_cu);
10733 }
10734
10735 attr = dwarf2_attr (die, DW_AT_external, cu);
10736 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10737 && die->parent->tag != DW_TAG_module)
10738 return 0;
10739 /* A variable in a lexical block of some kind does not need a
10740 namespace, even though in C++ such variables may be external
10741 and have a mangled name. */
10742 if (die->parent->tag == DW_TAG_lexical_block
10743 || die->parent->tag == DW_TAG_try_block
10744 || die->parent->tag == DW_TAG_catch_block
10745 || die->parent->tag == DW_TAG_subprogram)
10746 return 0;
10747 return 1;
10748
10749 default:
10750 return 0;
10751 }
10752 }
10753
10754 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10755 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10756 defined for the given DIE. */
10757
10758 static struct attribute *
10759 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10760 {
10761 struct attribute *attr;
10762
10763 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10764 if (attr == NULL)
10765 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10766
10767 return attr;
10768 }
10769
10770 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10771 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10772 defined for the given DIE. */
10773
10774 static const char *
10775 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10776 {
10777 const char *linkage_name;
10778
10779 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10780 if (linkage_name == NULL)
10781 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10782
10783 return linkage_name;
10784 }
10785
10786 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10787 compute the physname for the object, which include a method's:
10788 - formal parameters (C++),
10789 - receiver type (Go),
10790
10791 The term "physname" is a bit confusing.
10792 For C++, for example, it is the demangled name.
10793 For Go, for example, it's the mangled name.
10794
10795 For Ada, return the DIE's linkage name rather than the fully qualified
10796 name. PHYSNAME is ignored..
10797
10798 The result is allocated on the objfile_obstack and canonicalized. */
10799
10800 static const char *
10801 dwarf2_compute_name (const char *name,
10802 struct die_info *die, struct dwarf2_cu *cu,
10803 int physname)
10804 {
10805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10806
10807 if (name == NULL)
10808 name = dwarf2_name (die, cu);
10809
10810 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10811 but otherwise compute it by typename_concat inside GDB.
10812 FIXME: Actually this is not really true, or at least not always true.
10813 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10814 Fortran names because there is no mangling standard. So new_symbol
10815 will set the demangled name to the result of dwarf2_full_name, and it is
10816 the demangled name that GDB uses if it exists. */
10817 if (cu->language == language_ada
10818 || (cu->language == language_fortran && physname))
10819 {
10820 /* For Ada unit, we prefer the linkage name over the name, as
10821 the former contains the exported name, which the user expects
10822 to be able to reference. Ideally, we want the user to be able
10823 to reference this entity using either natural or linkage name,
10824 but we haven't started looking at this enhancement yet. */
10825 const char *linkage_name = dw2_linkage_name (die, cu);
10826
10827 if (linkage_name != NULL)
10828 return linkage_name;
10829 }
10830
10831 /* These are the only languages we know how to qualify names in. */
10832 if (name != NULL
10833 && (cu->language == language_cplus
10834 || cu->language == language_fortran || cu->language == language_d
10835 || cu->language == language_rust))
10836 {
10837 if (die_needs_namespace (die, cu))
10838 {
10839 const char *prefix;
10840 const char *canonical_name = NULL;
10841
10842 string_file buf;
10843
10844 prefix = determine_prefix (die, cu);
10845 if (*prefix != '\0')
10846 {
10847 char *prefixed_name = typename_concat (NULL, prefix, name,
10848 physname, cu);
10849
10850 buf.puts (prefixed_name);
10851 xfree (prefixed_name);
10852 }
10853 else
10854 buf.puts (name);
10855
10856 /* Template parameters may be specified in the DIE's DW_AT_name, or
10857 as children with DW_TAG_template_type_param or
10858 DW_TAG_value_type_param. If the latter, add them to the name
10859 here. If the name already has template parameters, then
10860 skip this step; some versions of GCC emit both, and
10861 it is more efficient to use the pre-computed name.
10862
10863 Something to keep in mind about this process: it is very
10864 unlikely, or in some cases downright impossible, to produce
10865 something that will match the mangled name of a function.
10866 If the definition of the function has the same debug info,
10867 we should be able to match up with it anyway. But fallbacks
10868 using the minimal symbol, for instance to find a method
10869 implemented in a stripped copy of libstdc++, will not work.
10870 If we do not have debug info for the definition, we will have to
10871 match them up some other way.
10872
10873 When we do name matching there is a related problem with function
10874 templates; two instantiated function templates are allowed to
10875 differ only by their return types, which we do not add here. */
10876
10877 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10878 {
10879 struct attribute *attr;
10880 struct die_info *child;
10881 int first = 1;
10882
10883 die->building_fullname = 1;
10884
10885 for (child = die->child; child != NULL; child = child->sibling)
10886 {
10887 struct type *type;
10888 LONGEST value;
10889 const gdb_byte *bytes;
10890 struct dwarf2_locexpr_baton *baton;
10891 struct value *v;
10892
10893 if (child->tag != DW_TAG_template_type_param
10894 && child->tag != DW_TAG_template_value_param)
10895 continue;
10896
10897 if (first)
10898 {
10899 buf.puts ("<");
10900 first = 0;
10901 }
10902 else
10903 buf.puts (", ");
10904
10905 attr = dwarf2_attr (child, DW_AT_type, cu);
10906 if (attr == NULL)
10907 {
10908 complaint (_("template parameter missing DW_AT_type"));
10909 buf.puts ("UNKNOWN_TYPE");
10910 continue;
10911 }
10912 type = die_type (child, cu);
10913
10914 if (child->tag == DW_TAG_template_type_param)
10915 {
10916 c_print_type (type, "", &buf, -1, 0, cu->language,
10917 &type_print_raw_options);
10918 continue;
10919 }
10920
10921 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10922 if (attr == NULL)
10923 {
10924 complaint (_("template parameter missing "
10925 "DW_AT_const_value"));
10926 buf.puts ("UNKNOWN_VALUE");
10927 continue;
10928 }
10929
10930 dwarf2_const_value_attr (attr, type, name,
10931 &cu->comp_unit_obstack, cu,
10932 &value, &bytes, &baton);
10933
10934 if (TYPE_NOSIGN (type))
10935 /* GDB prints characters as NUMBER 'CHAR'. If that's
10936 changed, this can use value_print instead. */
10937 c_printchar (value, type, &buf);
10938 else
10939 {
10940 struct value_print_options opts;
10941
10942 if (baton != NULL)
10943 v = dwarf2_evaluate_loc_desc (type, NULL,
10944 baton->data,
10945 baton->size,
10946 baton->per_cu);
10947 else if (bytes != NULL)
10948 {
10949 v = allocate_value (type);
10950 memcpy (value_contents_writeable (v), bytes,
10951 TYPE_LENGTH (type));
10952 }
10953 else
10954 v = value_from_longest (type, value);
10955
10956 /* Specify decimal so that we do not depend on
10957 the radix. */
10958 get_formatted_print_options (&opts, 'd');
10959 opts.raw = 1;
10960 value_print (v, &buf, &opts);
10961 release_value (v);
10962 }
10963 }
10964
10965 die->building_fullname = 0;
10966
10967 if (!first)
10968 {
10969 /* Close the argument list, with a space if necessary
10970 (nested templates). */
10971 if (!buf.empty () && buf.string ().back () == '>')
10972 buf.puts (" >");
10973 else
10974 buf.puts (">");
10975 }
10976 }
10977
10978 /* For C++ methods, append formal parameter type
10979 information, if PHYSNAME. */
10980
10981 if (physname && die->tag == DW_TAG_subprogram
10982 && cu->language == language_cplus)
10983 {
10984 struct type *type = read_type_die (die, cu);
10985
10986 c_type_print_args (type, &buf, 1, cu->language,
10987 &type_print_raw_options);
10988
10989 if (cu->language == language_cplus)
10990 {
10991 /* Assume that an artificial first parameter is
10992 "this", but do not crash if it is not. RealView
10993 marks unnamed (and thus unused) parameters as
10994 artificial; there is no way to differentiate
10995 the two cases. */
10996 if (TYPE_NFIELDS (type) > 0
10997 && TYPE_FIELD_ARTIFICIAL (type, 0)
10998 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10999 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11000 0))))
11001 buf.puts (" const");
11002 }
11003 }
11004
11005 const std::string &intermediate_name = buf.string ();
11006
11007 if (cu->language == language_cplus)
11008 canonical_name
11009 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11010 &objfile->per_bfd->storage_obstack);
11011
11012 /* If we only computed INTERMEDIATE_NAME, or if
11013 INTERMEDIATE_NAME is already canonical, then we need to
11014 copy it to the appropriate obstack. */
11015 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11016 name = ((const char *)
11017 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11018 intermediate_name.c_str (),
11019 intermediate_name.length ()));
11020 else
11021 name = canonical_name;
11022 }
11023 }
11024
11025 return name;
11026 }
11027
11028 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11029 If scope qualifiers are appropriate they will be added. The result
11030 will be allocated on the storage_obstack, or NULL if the DIE does
11031 not have a name. NAME may either be from a previous call to
11032 dwarf2_name or NULL.
11033
11034 The output string will be canonicalized (if C++). */
11035
11036 static const char *
11037 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11038 {
11039 return dwarf2_compute_name (name, die, cu, 0);
11040 }
11041
11042 /* Construct a physname for the given DIE in CU. NAME may either be
11043 from a previous call to dwarf2_name or NULL. The result will be
11044 allocated on the objfile_objstack or NULL if the DIE does not have a
11045 name.
11046
11047 The output string will be canonicalized (if C++). */
11048
11049 static const char *
11050 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11051 {
11052 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11053 const char *retval, *mangled = NULL, *canon = NULL;
11054 int need_copy = 1;
11055
11056 /* In this case dwarf2_compute_name is just a shortcut not building anything
11057 on its own. */
11058 if (!die_needs_namespace (die, cu))
11059 return dwarf2_compute_name (name, die, cu, 1);
11060
11061 mangled = dw2_linkage_name (die, cu);
11062
11063 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11064 See https://github.com/rust-lang/rust/issues/32925. */
11065 if (cu->language == language_rust && mangled != NULL
11066 && strchr (mangled, '{') != NULL)
11067 mangled = NULL;
11068
11069 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11070 has computed. */
11071 gdb::unique_xmalloc_ptr<char> demangled;
11072 if (mangled != NULL)
11073 {
11074
11075 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11076 {
11077 /* Do nothing (do not demangle the symbol name). */
11078 }
11079 else if (cu->language == language_go)
11080 {
11081 /* This is a lie, but we already lie to the caller new_symbol.
11082 new_symbol assumes we return the mangled name.
11083 This just undoes that lie until things are cleaned up. */
11084 }
11085 else
11086 {
11087 /* Use DMGL_RET_DROP for C++ template functions to suppress
11088 their return type. It is easier for GDB users to search
11089 for such functions as `name(params)' than `long name(params)'.
11090 In such case the minimal symbol names do not match the full
11091 symbol names but for template functions there is never a need
11092 to look up their definition from their declaration so
11093 the only disadvantage remains the minimal symbol variant
11094 `long name(params)' does not have the proper inferior type. */
11095 demangled.reset (gdb_demangle (mangled,
11096 (DMGL_PARAMS | DMGL_ANSI
11097 | DMGL_RET_DROP)));
11098 }
11099 if (demangled)
11100 canon = demangled.get ();
11101 else
11102 {
11103 canon = mangled;
11104 need_copy = 0;
11105 }
11106 }
11107
11108 if (canon == NULL || check_physname)
11109 {
11110 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11111
11112 if (canon != NULL && strcmp (physname, canon) != 0)
11113 {
11114 /* It may not mean a bug in GDB. The compiler could also
11115 compute DW_AT_linkage_name incorrectly. But in such case
11116 GDB would need to be bug-to-bug compatible. */
11117
11118 complaint (_("Computed physname <%s> does not match demangled <%s> "
11119 "(from linkage <%s>) - DIE at %s [in module %s]"),
11120 physname, canon, mangled, sect_offset_str (die->sect_off),
11121 objfile_name (objfile));
11122
11123 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11124 is available here - over computed PHYSNAME. It is safer
11125 against both buggy GDB and buggy compilers. */
11126
11127 retval = canon;
11128 }
11129 else
11130 {
11131 retval = physname;
11132 need_copy = 0;
11133 }
11134 }
11135 else
11136 retval = canon;
11137
11138 if (need_copy)
11139 retval = ((const char *)
11140 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11141 retval, strlen (retval)));
11142
11143 return retval;
11144 }
11145
11146 /* Inspect DIE in CU for a namespace alias. If one exists, record
11147 a new symbol for it.
11148
11149 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11150
11151 static int
11152 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11153 {
11154 struct attribute *attr;
11155
11156 /* If the die does not have a name, this is not a namespace
11157 alias. */
11158 attr = dwarf2_attr (die, DW_AT_name, cu);
11159 if (attr != NULL)
11160 {
11161 int num;
11162 struct die_info *d = die;
11163 struct dwarf2_cu *imported_cu = cu;
11164
11165 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11166 keep inspecting DIEs until we hit the underlying import. */
11167 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11168 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11169 {
11170 attr = dwarf2_attr (d, DW_AT_import, cu);
11171 if (attr == NULL)
11172 break;
11173
11174 d = follow_die_ref (d, attr, &imported_cu);
11175 if (d->tag != DW_TAG_imported_declaration)
11176 break;
11177 }
11178
11179 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11180 {
11181 complaint (_("DIE at %s has too many recursively imported "
11182 "declarations"), sect_offset_str (d->sect_off));
11183 return 0;
11184 }
11185
11186 if (attr != NULL)
11187 {
11188 struct type *type;
11189 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11190
11191 type = get_die_type_at_offset (sect_off, cu->per_cu);
11192 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11193 {
11194 /* This declaration is a global namespace alias. Add
11195 a symbol for it whose type is the aliased namespace. */
11196 new_symbol (die, type, cu);
11197 return 1;
11198 }
11199 }
11200 }
11201
11202 return 0;
11203 }
11204
11205 /* Return the using directives repository (global or local?) to use in the
11206 current context for CU.
11207
11208 For Ada, imported declarations can materialize renamings, which *may* be
11209 global. However it is impossible (for now?) in DWARF to distinguish
11210 "external" imported declarations and "static" ones. As all imported
11211 declarations seem to be static in all other languages, make them all CU-wide
11212 global only in Ada. */
11213
11214 static struct using_direct **
11215 using_directives (struct dwarf2_cu *cu)
11216 {
11217 if (cu->language == language_ada
11218 && cu->get_builder ()->outermost_context_p ())
11219 return cu->get_builder ()->get_global_using_directives ();
11220 else
11221 return cu->get_builder ()->get_local_using_directives ();
11222 }
11223
11224 /* Read the import statement specified by the given die and record it. */
11225
11226 static void
11227 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11228 {
11229 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11230 struct attribute *import_attr;
11231 struct die_info *imported_die, *child_die;
11232 struct dwarf2_cu *imported_cu;
11233 const char *imported_name;
11234 const char *imported_name_prefix;
11235 const char *canonical_name;
11236 const char *import_alias;
11237 const char *imported_declaration = NULL;
11238 const char *import_prefix;
11239 std::vector<const char *> excludes;
11240
11241 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11242 if (import_attr == NULL)
11243 {
11244 complaint (_("Tag '%s' has no DW_AT_import"),
11245 dwarf_tag_name (die->tag));
11246 return;
11247 }
11248
11249 imported_cu = cu;
11250 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11251 imported_name = dwarf2_name (imported_die, imported_cu);
11252 if (imported_name == NULL)
11253 {
11254 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11255
11256 The import in the following code:
11257 namespace A
11258 {
11259 typedef int B;
11260 }
11261
11262 int main ()
11263 {
11264 using A::B;
11265 B b;
11266 return b;
11267 }
11268
11269 ...
11270 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11271 <52> DW_AT_decl_file : 1
11272 <53> DW_AT_decl_line : 6
11273 <54> DW_AT_import : <0x75>
11274 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11275 <59> DW_AT_name : B
11276 <5b> DW_AT_decl_file : 1
11277 <5c> DW_AT_decl_line : 2
11278 <5d> DW_AT_type : <0x6e>
11279 ...
11280 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11281 <76> DW_AT_byte_size : 4
11282 <77> DW_AT_encoding : 5 (signed)
11283
11284 imports the wrong die ( 0x75 instead of 0x58 ).
11285 This case will be ignored until the gcc bug is fixed. */
11286 return;
11287 }
11288
11289 /* Figure out the local name after import. */
11290 import_alias = dwarf2_name (die, cu);
11291
11292 /* Figure out where the statement is being imported to. */
11293 import_prefix = determine_prefix (die, cu);
11294
11295 /* Figure out what the scope of the imported die is and prepend it
11296 to the name of the imported die. */
11297 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11298
11299 if (imported_die->tag != DW_TAG_namespace
11300 && imported_die->tag != DW_TAG_module)
11301 {
11302 imported_declaration = imported_name;
11303 canonical_name = imported_name_prefix;
11304 }
11305 else if (strlen (imported_name_prefix) > 0)
11306 canonical_name = obconcat (&objfile->objfile_obstack,
11307 imported_name_prefix,
11308 (cu->language == language_d ? "." : "::"),
11309 imported_name, (char *) NULL);
11310 else
11311 canonical_name = imported_name;
11312
11313 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11314 for (child_die = die->child; child_die && child_die->tag;
11315 child_die = sibling_die (child_die))
11316 {
11317 /* DWARF-4: A Fortran use statement with a “rename list” may be
11318 represented by an imported module entry with an import attribute
11319 referring to the module and owned entries corresponding to those
11320 entities that are renamed as part of being imported. */
11321
11322 if (child_die->tag != DW_TAG_imported_declaration)
11323 {
11324 complaint (_("child DW_TAG_imported_declaration expected "
11325 "- DIE at %s [in module %s]"),
11326 sect_offset_str (child_die->sect_off),
11327 objfile_name (objfile));
11328 continue;
11329 }
11330
11331 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11332 if (import_attr == NULL)
11333 {
11334 complaint (_("Tag '%s' has no DW_AT_import"),
11335 dwarf_tag_name (child_die->tag));
11336 continue;
11337 }
11338
11339 imported_cu = cu;
11340 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11341 &imported_cu);
11342 imported_name = dwarf2_name (imported_die, imported_cu);
11343 if (imported_name == NULL)
11344 {
11345 complaint (_("child DW_TAG_imported_declaration has unknown "
11346 "imported name - DIE at %s [in module %s]"),
11347 sect_offset_str (child_die->sect_off),
11348 objfile_name (objfile));
11349 continue;
11350 }
11351
11352 excludes.push_back (imported_name);
11353
11354 process_die (child_die, cu);
11355 }
11356
11357 add_using_directive (using_directives (cu),
11358 import_prefix,
11359 canonical_name,
11360 import_alias,
11361 imported_declaration,
11362 excludes,
11363 0,
11364 &objfile->objfile_obstack);
11365 }
11366
11367 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11368 types, but gives them a size of zero. Starting with version 14,
11369 ICC is compatible with GCC. */
11370
11371 static bool
11372 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11373 {
11374 if (!cu->checked_producer)
11375 check_producer (cu);
11376
11377 return cu->producer_is_icc_lt_14;
11378 }
11379
11380 /* ICC generates a DW_AT_type for C void functions. This was observed on
11381 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11382 which says that void functions should not have a DW_AT_type. */
11383
11384 static bool
11385 producer_is_icc (struct dwarf2_cu *cu)
11386 {
11387 if (!cu->checked_producer)
11388 check_producer (cu);
11389
11390 return cu->producer_is_icc;
11391 }
11392
11393 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11394 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11395 this, it was first present in GCC release 4.3.0. */
11396
11397 static bool
11398 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11399 {
11400 if (!cu->checked_producer)
11401 check_producer (cu);
11402
11403 return cu->producer_is_gcc_lt_4_3;
11404 }
11405
11406 static file_and_directory
11407 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11408 {
11409 file_and_directory res;
11410
11411 /* Find the filename. Do not use dwarf2_name here, since the filename
11412 is not a source language identifier. */
11413 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11414 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11415
11416 if (res.comp_dir == NULL
11417 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11418 && IS_ABSOLUTE_PATH (res.name))
11419 {
11420 res.comp_dir_storage = ldirname (res.name);
11421 if (!res.comp_dir_storage.empty ())
11422 res.comp_dir = res.comp_dir_storage.c_str ();
11423 }
11424 if (res.comp_dir != NULL)
11425 {
11426 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11427 directory, get rid of it. */
11428 const char *cp = strchr (res.comp_dir, ':');
11429
11430 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11431 res.comp_dir = cp + 1;
11432 }
11433
11434 if (res.name == NULL)
11435 res.name = "<unknown>";
11436
11437 return res;
11438 }
11439
11440 /* Handle DW_AT_stmt_list for a compilation unit.
11441 DIE is the DW_TAG_compile_unit die for CU.
11442 COMP_DIR is the compilation directory. LOWPC is passed to
11443 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11444
11445 static void
11446 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11447 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11448 {
11449 struct dwarf2_per_objfile *dwarf2_per_objfile
11450 = cu->per_cu->dwarf2_per_objfile;
11451 struct objfile *objfile = dwarf2_per_objfile->objfile;
11452 struct attribute *attr;
11453 struct line_header line_header_local;
11454 hashval_t line_header_local_hash;
11455 void **slot;
11456 int decode_mapping;
11457
11458 gdb_assert (! cu->per_cu->is_debug_types);
11459
11460 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11461 if (attr == NULL)
11462 return;
11463
11464 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11465
11466 /* The line header hash table is only created if needed (it exists to
11467 prevent redundant reading of the line table for partial_units).
11468 If we're given a partial_unit, we'll need it. If we're given a
11469 compile_unit, then use the line header hash table if it's already
11470 created, but don't create one just yet. */
11471
11472 if (dwarf2_per_objfile->line_header_hash == NULL
11473 && die->tag == DW_TAG_partial_unit)
11474 {
11475 dwarf2_per_objfile->line_header_hash
11476 = htab_create_alloc_ex (127, line_header_hash_voidp,
11477 line_header_eq_voidp,
11478 free_line_header_voidp,
11479 &objfile->objfile_obstack,
11480 hashtab_obstack_allocate,
11481 dummy_obstack_deallocate);
11482 }
11483
11484 line_header_local.sect_off = line_offset;
11485 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11486 line_header_local_hash = line_header_hash (&line_header_local);
11487 if (dwarf2_per_objfile->line_header_hash != NULL)
11488 {
11489 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11490 &line_header_local,
11491 line_header_local_hash, NO_INSERT);
11492
11493 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11494 is not present in *SLOT (since if there is something in *SLOT then
11495 it will be for a partial_unit). */
11496 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11497 {
11498 gdb_assert (*slot != NULL);
11499 cu->line_header = (struct line_header *) *slot;
11500 return;
11501 }
11502 }
11503
11504 /* dwarf_decode_line_header does not yet provide sufficient information.
11505 We always have to call also dwarf_decode_lines for it. */
11506 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11507 if (lh == NULL)
11508 return;
11509
11510 cu->line_header = lh.release ();
11511 cu->line_header_die_owner = die;
11512
11513 if (dwarf2_per_objfile->line_header_hash == NULL)
11514 slot = NULL;
11515 else
11516 {
11517 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11518 &line_header_local,
11519 line_header_local_hash, INSERT);
11520 gdb_assert (slot != NULL);
11521 }
11522 if (slot != NULL && *slot == NULL)
11523 {
11524 /* This newly decoded line number information unit will be owned
11525 by line_header_hash hash table. */
11526 *slot = cu->line_header;
11527 cu->line_header_die_owner = NULL;
11528 }
11529 else
11530 {
11531 /* We cannot free any current entry in (*slot) as that struct line_header
11532 may be already used by multiple CUs. Create only temporary decoded
11533 line_header for this CU - it may happen at most once for each line
11534 number information unit. And if we're not using line_header_hash
11535 then this is what we want as well. */
11536 gdb_assert (die->tag != DW_TAG_partial_unit);
11537 }
11538 decode_mapping = (die->tag != DW_TAG_partial_unit);
11539 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11540 decode_mapping);
11541
11542 }
11543
11544 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11545
11546 static void
11547 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11548 {
11549 struct dwarf2_per_objfile *dwarf2_per_objfile
11550 = cu->per_cu->dwarf2_per_objfile;
11551 struct objfile *objfile = dwarf2_per_objfile->objfile;
11552 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11553 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11554 CORE_ADDR highpc = ((CORE_ADDR) 0);
11555 struct attribute *attr;
11556 struct die_info *child_die;
11557 CORE_ADDR baseaddr;
11558
11559 prepare_one_comp_unit (cu, die, cu->language);
11560 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11561
11562 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11563
11564 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11565 from finish_block. */
11566 if (lowpc == ((CORE_ADDR) -1))
11567 lowpc = highpc;
11568 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11569
11570 file_and_directory fnd = find_file_and_directory (die, cu);
11571
11572 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11573 standardised yet. As a workaround for the language detection we fall
11574 back to the DW_AT_producer string. */
11575 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11576 cu->language = language_opencl;
11577
11578 /* Similar hack for Go. */
11579 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11580 set_cu_language (DW_LANG_Go, cu);
11581
11582 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11583
11584 /* Decode line number information if present. We do this before
11585 processing child DIEs, so that the line header table is available
11586 for DW_AT_decl_file. */
11587 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11588
11589 /* Process all dies in compilation unit. */
11590 if (die->child != NULL)
11591 {
11592 child_die = die->child;
11593 while (child_die && child_die->tag)
11594 {
11595 process_die (child_die, cu);
11596 child_die = sibling_die (child_die);
11597 }
11598 }
11599
11600 /* Decode macro information, if present. Dwarf 2 macro information
11601 refers to information in the line number info statement program
11602 header, so we can only read it if we've read the header
11603 successfully. */
11604 attr = dwarf2_attr (die, DW_AT_macros, cu);
11605 if (attr == NULL)
11606 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11607 if (attr && cu->line_header)
11608 {
11609 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11610 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11611
11612 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11613 }
11614 else
11615 {
11616 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11617 if (attr && cu->line_header)
11618 {
11619 unsigned int macro_offset = DW_UNSND (attr);
11620
11621 dwarf_decode_macros (cu, macro_offset, 0);
11622 }
11623 }
11624 }
11625
11626 void
11627 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11628 {
11629 struct type_unit_group *tu_group;
11630 int first_time;
11631 struct attribute *attr;
11632 unsigned int i;
11633 struct signatured_type *sig_type;
11634
11635 gdb_assert (per_cu->is_debug_types);
11636 sig_type = (struct signatured_type *) per_cu;
11637
11638 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11639
11640 /* If we're using .gdb_index (includes -readnow) then
11641 per_cu->type_unit_group may not have been set up yet. */
11642 if (sig_type->type_unit_group == NULL)
11643 sig_type->type_unit_group = get_type_unit_group (this, attr);
11644 tu_group = sig_type->type_unit_group;
11645
11646 /* If we've already processed this stmt_list there's no real need to
11647 do it again, we could fake it and just recreate the part we need
11648 (file name,index -> symtab mapping). If data shows this optimization
11649 is useful we can do it then. */
11650 first_time = tu_group->compunit_symtab == NULL;
11651
11652 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11653 debug info. */
11654 line_header_up lh;
11655 if (attr != NULL)
11656 {
11657 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11658 lh = dwarf_decode_line_header (line_offset, this);
11659 }
11660 if (lh == NULL)
11661 {
11662 if (first_time)
11663 start_symtab ("", NULL, 0);
11664 else
11665 {
11666 gdb_assert (tu_group->symtabs == NULL);
11667 gdb_assert (m_builder == nullptr);
11668 struct compunit_symtab *cust = tu_group->compunit_symtab;
11669 m_builder.reset (new struct buildsym_compunit
11670 (COMPUNIT_OBJFILE (cust), "",
11671 COMPUNIT_DIRNAME (cust),
11672 compunit_language (cust),
11673 0, cust));
11674 }
11675 return;
11676 }
11677
11678 line_header = lh.release ();
11679 line_header_die_owner = die;
11680
11681 if (first_time)
11682 {
11683 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11684
11685 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11686 still initializing it, and our caller (a few levels up)
11687 process_full_type_unit still needs to know if this is the first
11688 time. */
11689
11690 tu_group->num_symtabs = line_header->file_names.size ();
11691 tu_group->symtabs = XNEWVEC (struct symtab *,
11692 line_header->file_names.size ());
11693
11694 for (i = 0; i < line_header->file_names.size (); ++i)
11695 {
11696 file_entry &fe = line_header->file_names[i];
11697
11698 dwarf2_start_subfile (this, fe.name,
11699 fe.include_dir (line_header));
11700 buildsym_compunit *b = get_builder ();
11701 if (b->get_current_subfile ()->symtab == NULL)
11702 {
11703 /* NOTE: start_subfile will recognize when it's been
11704 passed a file it has already seen. So we can't
11705 assume there's a simple mapping from
11706 cu->line_header->file_names to subfiles, plus
11707 cu->line_header->file_names may contain dups. */
11708 b->get_current_subfile ()->symtab
11709 = allocate_symtab (cust, b->get_current_subfile ()->name);
11710 }
11711
11712 fe.symtab = b->get_current_subfile ()->symtab;
11713 tu_group->symtabs[i] = fe.symtab;
11714 }
11715 }
11716 else
11717 {
11718 gdb_assert (m_builder == nullptr);
11719 struct compunit_symtab *cust = tu_group->compunit_symtab;
11720 m_builder.reset (new struct buildsym_compunit
11721 (COMPUNIT_OBJFILE (cust), "",
11722 COMPUNIT_DIRNAME (cust),
11723 compunit_language (cust),
11724 0, cust));
11725
11726 for (i = 0; i < line_header->file_names.size (); ++i)
11727 {
11728 file_entry &fe = line_header->file_names[i];
11729
11730 fe.symtab = tu_group->symtabs[i];
11731 }
11732 }
11733
11734 /* The main symtab is allocated last. Type units don't have DW_AT_name
11735 so they don't have a "real" (so to speak) symtab anyway.
11736 There is later code that will assign the main symtab to all symbols
11737 that don't have one. We need to handle the case of a symbol with a
11738 missing symtab (DW_AT_decl_file) anyway. */
11739 }
11740
11741 /* Process DW_TAG_type_unit.
11742 For TUs we want to skip the first top level sibling if it's not the
11743 actual type being defined by this TU. In this case the first top
11744 level sibling is there to provide context only. */
11745
11746 static void
11747 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11748 {
11749 struct die_info *child_die;
11750
11751 prepare_one_comp_unit (cu, die, language_minimal);
11752
11753 /* Initialize (or reinitialize) the machinery for building symtabs.
11754 We do this before processing child DIEs, so that the line header table
11755 is available for DW_AT_decl_file. */
11756 cu->setup_type_unit_groups (die);
11757
11758 if (die->child != NULL)
11759 {
11760 child_die = die->child;
11761 while (child_die && child_die->tag)
11762 {
11763 process_die (child_die, cu);
11764 child_die = sibling_die (child_die);
11765 }
11766 }
11767 }
11768 \f
11769 /* DWO/DWP files.
11770
11771 http://gcc.gnu.org/wiki/DebugFission
11772 http://gcc.gnu.org/wiki/DebugFissionDWP
11773
11774 To simplify handling of both DWO files ("object" files with the DWARF info)
11775 and DWP files (a file with the DWOs packaged up into one file), we treat
11776 DWP files as having a collection of virtual DWO files. */
11777
11778 static hashval_t
11779 hash_dwo_file (const void *item)
11780 {
11781 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11782 hashval_t hash;
11783
11784 hash = htab_hash_string (dwo_file->dwo_name);
11785 if (dwo_file->comp_dir != NULL)
11786 hash += htab_hash_string (dwo_file->comp_dir);
11787 return hash;
11788 }
11789
11790 static int
11791 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11792 {
11793 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11794 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11795
11796 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11797 return 0;
11798 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11799 return lhs->comp_dir == rhs->comp_dir;
11800 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11801 }
11802
11803 /* Allocate a hash table for DWO files. */
11804
11805 static htab_t
11806 allocate_dwo_file_hash_table (struct objfile *objfile)
11807 {
11808 return htab_create_alloc_ex (41,
11809 hash_dwo_file,
11810 eq_dwo_file,
11811 NULL,
11812 &objfile->objfile_obstack,
11813 hashtab_obstack_allocate,
11814 dummy_obstack_deallocate);
11815 }
11816
11817 /* Lookup DWO file DWO_NAME. */
11818
11819 static void **
11820 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11821 const char *dwo_name,
11822 const char *comp_dir)
11823 {
11824 struct dwo_file find_entry;
11825 void **slot;
11826
11827 if (dwarf2_per_objfile->dwo_files == NULL)
11828 dwarf2_per_objfile->dwo_files
11829 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11830
11831 memset (&find_entry, 0, sizeof (find_entry));
11832 find_entry.dwo_name = dwo_name;
11833 find_entry.comp_dir = comp_dir;
11834 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11835
11836 return slot;
11837 }
11838
11839 static hashval_t
11840 hash_dwo_unit (const void *item)
11841 {
11842 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11843
11844 /* This drops the top 32 bits of the id, but is ok for a hash. */
11845 return dwo_unit->signature;
11846 }
11847
11848 static int
11849 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11850 {
11851 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11852 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11853
11854 /* The signature is assumed to be unique within the DWO file.
11855 So while object file CU dwo_id's always have the value zero,
11856 that's OK, assuming each object file DWO file has only one CU,
11857 and that's the rule for now. */
11858 return lhs->signature == rhs->signature;
11859 }
11860
11861 /* Allocate a hash table for DWO CUs,TUs.
11862 There is one of these tables for each of CUs,TUs for each DWO file. */
11863
11864 static htab_t
11865 allocate_dwo_unit_table (struct objfile *objfile)
11866 {
11867 /* Start out with a pretty small number.
11868 Generally DWO files contain only one CU and maybe some TUs. */
11869 return htab_create_alloc_ex (3,
11870 hash_dwo_unit,
11871 eq_dwo_unit,
11872 NULL,
11873 &objfile->objfile_obstack,
11874 hashtab_obstack_allocate,
11875 dummy_obstack_deallocate);
11876 }
11877
11878 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11879
11880 struct create_dwo_cu_data
11881 {
11882 struct dwo_file *dwo_file;
11883 struct dwo_unit dwo_unit;
11884 };
11885
11886 /* die_reader_func for create_dwo_cu. */
11887
11888 static void
11889 create_dwo_cu_reader (const struct die_reader_specs *reader,
11890 const gdb_byte *info_ptr,
11891 struct die_info *comp_unit_die,
11892 int has_children,
11893 void *datap)
11894 {
11895 struct dwarf2_cu *cu = reader->cu;
11896 sect_offset sect_off = cu->per_cu->sect_off;
11897 struct dwarf2_section_info *section = cu->per_cu->section;
11898 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11899 struct dwo_file *dwo_file = data->dwo_file;
11900 struct dwo_unit *dwo_unit = &data->dwo_unit;
11901 struct attribute *attr;
11902
11903 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11904 if (attr == NULL)
11905 {
11906 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11907 " its dwo_id [in module %s]"),
11908 sect_offset_str (sect_off), dwo_file->dwo_name);
11909 return;
11910 }
11911
11912 dwo_unit->dwo_file = dwo_file;
11913 dwo_unit->signature = DW_UNSND (attr);
11914 dwo_unit->section = section;
11915 dwo_unit->sect_off = sect_off;
11916 dwo_unit->length = cu->per_cu->length;
11917
11918 if (dwarf_read_debug)
11919 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11920 sect_offset_str (sect_off),
11921 hex_string (dwo_unit->signature));
11922 }
11923
11924 /* Create the dwo_units for the CUs in a DWO_FILE.
11925 Note: This function processes DWO files only, not DWP files. */
11926
11927 static void
11928 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11929 struct dwo_file &dwo_file, dwarf2_section_info &section,
11930 htab_t &cus_htab)
11931 {
11932 struct objfile *objfile = dwarf2_per_objfile->objfile;
11933 const gdb_byte *info_ptr, *end_ptr;
11934
11935 dwarf2_read_section (objfile, &section);
11936 info_ptr = section.buffer;
11937
11938 if (info_ptr == NULL)
11939 return;
11940
11941 if (dwarf_read_debug)
11942 {
11943 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11944 get_section_name (&section),
11945 get_section_file_name (&section));
11946 }
11947
11948 end_ptr = info_ptr + section.size;
11949 while (info_ptr < end_ptr)
11950 {
11951 struct dwarf2_per_cu_data per_cu;
11952 struct create_dwo_cu_data create_dwo_cu_data;
11953 struct dwo_unit *dwo_unit;
11954 void **slot;
11955 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11956
11957 memset (&create_dwo_cu_data.dwo_unit, 0,
11958 sizeof (create_dwo_cu_data.dwo_unit));
11959 memset (&per_cu, 0, sizeof (per_cu));
11960 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11961 per_cu.is_debug_types = 0;
11962 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11963 per_cu.section = &section;
11964 create_dwo_cu_data.dwo_file = &dwo_file;
11965
11966 init_cutu_and_read_dies_no_follow (
11967 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11968 info_ptr += per_cu.length;
11969
11970 // If the unit could not be parsed, skip it.
11971 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11972 continue;
11973
11974 if (cus_htab == NULL)
11975 cus_htab = allocate_dwo_unit_table (objfile);
11976
11977 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11978 *dwo_unit = create_dwo_cu_data.dwo_unit;
11979 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11980 gdb_assert (slot != NULL);
11981 if (*slot != NULL)
11982 {
11983 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11984 sect_offset dup_sect_off = dup_cu->sect_off;
11985
11986 complaint (_("debug cu entry at offset %s is duplicate to"
11987 " the entry at offset %s, signature %s"),
11988 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11989 hex_string (dwo_unit->signature));
11990 }
11991 *slot = (void *)dwo_unit;
11992 }
11993 }
11994
11995 /* DWP file .debug_{cu,tu}_index section format:
11996 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11997
11998 DWP Version 1:
11999
12000 Both index sections have the same format, and serve to map a 64-bit
12001 signature to a set of section numbers. Each section begins with a header,
12002 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12003 indexes, and a pool of 32-bit section numbers. The index sections will be
12004 aligned at 8-byte boundaries in the file.
12005
12006 The index section header consists of:
12007
12008 V, 32 bit version number
12009 -, 32 bits unused
12010 N, 32 bit number of compilation units or type units in the index
12011 M, 32 bit number of slots in the hash table
12012
12013 Numbers are recorded using the byte order of the application binary.
12014
12015 The hash table begins at offset 16 in the section, and consists of an array
12016 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12017 order of the application binary). Unused slots in the hash table are 0.
12018 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12019
12020 The parallel table begins immediately after the hash table
12021 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12022 array of 32-bit indexes (using the byte order of the application binary),
12023 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12024 table contains a 32-bit index into the pool of section numbers. For unused
12025 hash table slots, the corresponding entry in the parallel table will be 0.
12026
12027 The pool of section numbers begins immediately following the hash table
12028 (at offset 16 + 12 * M from the beginning of the section). The pool of
12029 section numbers consists of an array of 32-bit words (using the byte order
12030 of the application binary). Each item in the array is indexed starting
12031 from 0. The hash table entry provides the index of the first section
12032 number in the set. Additional section numbers in the set follow, and the
12033 set is terminated by a 0 entry (section number 0 is not used in ELF).
12034
12035 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12036 section must be the first entry in the set, and the .debug_abbrev.dwo must
12037 be the second entry. Other members of the set may follow in any order.
12038
12039 ---
12040
12041 DWP Version 2:
12042
12043 DWP Version 2 combines all the .debug_info, etc. sections into one,
12044 and the entries in the index tables are now offsets into these sections.
12045 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12046 section.
12047
12048 Index Section Contents:
12049 Header
12050 Hash Table of Signatures dwp_hash_table.hash_table
12051 Parallel Table of Indices dwp_hash_table.unit_table
12052 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12053 Table of Section Sizes dwp_hash_table.v2.sizes
12054
12055 The index section header consists of:
12056
12057 V, 32 bit version number
12058 L, 32 bit number of columns in the table of section offsets
12059 N, 32 bit number of compilation units or type units in the index
12060 M, 32 bit number of slots in the hash table
12061
12062 Numbers are recorded using the byte order of the application binary.
12063
12064 The hash table has the same format as version 1.
12065 The parallel table of indices has the same format as version 1,
12066 except that the entries are origin-1 indices into the table of sections
12067 offsets and the table of section sizes.
12068
12069 The table of offsets begins immediately following the parallel table
12070 (at offset 16 + 12 * M from the beginning of the section). The table is
12071 a two-dimensional array of 32-bit words (using the byte order of the
12072 application binary), with L columns and N+1 rows, in row-major order.
12073 Each row in the array is indexed starting from 0. The first row provides
12074 a key to the remaining rows: each column in this row provides an identifier
12075 for a debug section, and the offsets in the same column of subsequent rows
12076 refer to that section. The section identifiers are:
12077
12078 DW_SECT_INFO 1 .debug_info.dwo
12079 DW_SECT_TYPES 2 .debug_types.dwo
12080 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12081 DW_SECT_LINE 4 .debug_line.dwo
12082 DW_SECT_LOC 5 .debug_loc.dwo
12083 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12084 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12085 DW_SECT_MACRO 8 .debug_macro.dwo
12086
12087 The offsets provided by the CU and TU index sections are the base offsets
12088 for the contributions made by each CU or TU to the corresponding section
12089 in the package file. Each CU and TU header contains an abbrev_offset
12090 field, used to find the abbreviations table for that CU or TU within the
12091 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12092 be interpreted as relative to the base offset given in the index section.
12093 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12094 should be interpreted as relative to the base offset for .debug_line.dwo,
12095 and offsets into other debug sections obtained from DWARF attributes should
12096 also be interpreted as relative to the corresponding base offset.
12097
12098 The table of sizes begins immediately following the table of offsets.
12099 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12100 with L columns and N rows, in row-major order. Each row in the array is
12101 indexed starting from 1 (row 0 is shared by the two tables).
12102
12103 ---
12104
12105 Hash table lookup is handled the same in version 1 and 2:
12106
12107 We assume that N and M will not exceed 2^32 - 1.
12108 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12109
12110 Given a 64-bit compilation unit signature or a type signature S, an entry
12111 in the hash table is located as follows:
12112
12113 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12114 the low-order k bits all set to 1.
12115
12116 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12117
12118 3) If the hash table entry at index H matches the signature, use that
12119 entry. If the hash table entry at index H is unused (all zeroes),
12120 terminate the search: the signature is not present in the table.
12121
12122 4) Let H = (H + H') modulo M. Repeat at Step 3.
12123
12124 Because M > N and H' and M are relatively prime, the search is guaranteed
12125 to stop at an unused slot or find the match. */
12126
12127 /* Create a hash table to map DWO IDs to their CU/TU entry in
12128 .debug_{info,types}.dwo in DWP_FILE.
12129 Returns NULL if there isn't one.
12130 Note: This function processes DWP files only, not DWO files. */
12131
12132 static struct dwp_hash_table *
12133 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12134 struct dwp_file *dwp_file, int is_debug_types)
12135 {
12136 struct objfile *objfile = dwarf2_per_objfile->objfile;
12137 bfd *dbfd = dwp_file->dbfd.get ();
12138 const gdb_byte *index_ptr, *index_end;
12139 struct dwarf2_section_info *index;
12140 uint32_t version, nr_columns, nr_units, nr_slots;
12141 struct dwp_hash_table *htab;
12142
12143 if (is_debug_types)
12144 index = &dwp_file->sections.tu_index;
12145 else
12146 index = &dwp_file->sections.cu_index;
12147
12148 if (dwarf2_section_empty_p (index))
12149 return NULL;
12150 dwarf2_read_section (objfile, index);
12151
12152 index_ptr = index->buffer;
12153 index_end = index_ptr + index->size;
12154
12155 version = read_4_bytes (dbfd, index_ptr);
12156 index_ptr += 4;
12157 if (version == 2)
12158 nr_columns = read_4_bytes (dbfd, index_ptr);
12159 else
12160 nr_columns = 0;
12161 index_ptr += 4;
12162 nr_units = read_4_bytes (dbfd, index_ptr);
12163 index_ptr += 4;
12164 nr_slots = read_4_bytes (dbfd, index_ptr);
12165 index_ptr += 4;
12166
12167 if (version != 1 && version != 2)
12168 {
12169 error (_("Dwarf Error: unsupported DWP file version (%s)"
12170 " [in module %s]"),
12171 pulongest (version), dwp_file->name);
12172 }
12173 if (nr_slots != (nr_slots & -nr_slots))
12174 {
12175 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12176 " is not power of 2 [in module %s]"),
12177 pulongest (nr_slots), dwp_file->name);
12178 }
12179
12180 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12181 htab->version = version;
12182 htab->nr_columns = nr_columns;
12183 htab->nr_units = nr_units;
12184 htab->nr_slots = nr_slots;
12185 htab->hash_table = index_ptr;
12186 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12187
12188 /* Exit early if the table is empty. */
12189 if (nr_slots == 0 || nr_units == 0
12190 || (version == 2 && nr_columns == 0))
12191 {
12192 /* All must be zero. */
12193 if (nr_slots != 0 || nr_units != 0
12194 || (version == 2 && nr_columns != 0))
12195 {
12196 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12197 " all zero [in modules %s]"),
12198 dwp_file->name);
12199 }
12200 return htab;
12201 }
12202
12203 if (version == 1)
12204 {
12205 htab->section_pool.v1.indices =
12206 htab->unit_table + sizeof (uint32_t) * nr_slots;
12207 /* It's harder to decide whether the section is too small in v1.
12208 V1 is deprecated anyway so we punt. */
12209 }
12210 else
12211 {
12212 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12213 int *ids = htab->section_pool.v2.section_ids;
12214 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12215 /* Reverse map for error checking. */
12216 int ids_seen[DW_SECT_MAX + 1];
12217 int i;
12218
12219 if (nr_columns < 2)
12220 {
12221 error (_("Dwarf Error: bad DWP hash table, too few columns"
12222 " in section table [in module %s]"),
12223 dwp_file->name);
12224 }
12225 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12226 {
12227 error (_("Dwarf Error: bad DWP hash table, too many columns"
12228 " in section table [in module %s]"),
12229 dwp_file->name);
12230 }
12231 memset (ids, 255, sizeof_ids);
12232 memset (ids_seen, 255, sizeof (ids_seen));
12233 for (i = 0; i < nr_columns; ++i)
12234 {
12235 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12236
12237 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12238 {
12239 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12240 " in section table [in module %s]"),
12241 id, dwp_file->name);
12242 }
12243 if (ids_seen[id] != -1)
12244 {
12245 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12246 " id %d in section table [in module %s]"),
12247 id, dwp_file->name);
12248 }
12249 ids_seen[id] = i;
12250 ids[i] = id;
12251 }
12252 /* Must have exactly one info or types section. */
12253 if (((ids_seen[DW_SECT_INFO] != -1)
12254 + (ids_seen[DW_SECT_TYPES] != -1))
12255 != 1)
12256 {
12257 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12258 " DWO info/types section [in module %s]"),
12259 dwp_file->name);
12260 }
12261 /* Must have an abbrev section. */
12262 if (ids_seen[DW_SECT_ABBREV] == -1)
12263 {
12264 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12265 " section [in module %s]"),
12266 dwp_file->name);
12267 }
12268 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12269 htab->section_pool.v2.sizes =
12270 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12271 * nr_units * nr_columns);
12272 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12273 * nr_units * nr_columns))
12274 > index_end)
12275 {
12276 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12277 " [in module %s]"),
12278 dwp_file->name);
12279 }
12280 }
12281
12282 return htab;
12283 }
12284
12285 /* Update SECTIONS with the data from SECTP.
12286
12287 This function is like the other "locate" section routines that are
12288 passed to bfd_map_over_sections, but in this context the sections to
12289 read comes from the DWP V1 hash table, not the full ELF section table.
12290
12291 The result is non-zero for success, or zero if an error was found. */
12292
12293 static int
12294 locate_v1_virtual_dwo_sections (asection *sectp,
12295 struct virtual_v1_dwo_sections *sections)
12296 {
12297 const struct dwop_section_names *names = &dwop_section_names;
12298
12299 if (section_is_p (sectp->name, &names->abbrev_dwo))
12300 {
12301 /* There can be only one. */
12302 if (sections->abbrev.s.section != NULL)
12303 return 0;
12304 sections->abbrev.s.section = sectp;
12305 sections->abbrev.size = bfd_get_section_size (sectp);
12306 }
12307 else if (section_is_p (sectp->name, &names->info_dwo)
12308 || section_is_p (sectp->name, &names->types_dwo))
12309 {
12310 /* There can be only one. */
12311 if (sections->info_or_types.s.section != NULL)
12312 return 0;
12313 sections->info_or_types.s.section = sectp;
12314 sections->info_or_types.size = bfd_get_section_size (sectp);
12315 }
12316 else if (section_is_p (sectp->name, &names->line_dwo))
12317 {
12318 /* There can be only one. */
12319 if (sections->line.s.section != NULL)
12320 return 0;
12321 sections->line.s.section = sectp;
12322 sections->line.size = bfd_get_section_size (sectp);
12323 }
12324 else if (section_is_p (sectp->name, &names->loc_dwo))
12325 {
12326 /* There can be only one. */
12327 if (sections->loc.s.section != NULL)
12328 return 0;
12329 sections->loc.s.section = sectp;
12330 sections->loc.size = bfd_get_section_size (sectp);
12331 }
12332 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12333 {
12334 /* There can be only one. */
12335 if (sections->macinfo.s.section != NULL)
12336 return 0;
12337 sections->macinfo.s.section = sectp;
12338 sections->macinfo.size = bfd_get_section_size (sectp);
12339 }
12340 else if (section_is_p (sectp->name, &names->macro_dwo))
12341 {
12342 /* There can be only one. */
12343 if (sections->macro.s.section != NULL)
12344 return 0;
12345 sections->macro.s.section = sectp;
12346 sections->macro.size = bfd_get_section_size (sectp);
12347 }
12348 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12349 {
12350 /* There can be only one. */
12351 if (sections->str_offsets.s.section != NULL)
12352 return 0;
12353 sections->str_offsets.s.section = sectp;
12354 sections->str_offsets.size = bfd_get_section_size (sectp);
12355 }
12356 else
12357 {
12358 /* No other kind of section is valid. */
12359 return 0;
12360 }
12361
12362 return 1;
12363 }
12364
12365 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12366 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12367 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12368 This is for DWP version 1 files. */
12369
12370 static struct dwo_unit *
12371 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12372 struct dwp_file *dwp_file,
12373 uint32_t unit_index,
12374 const char *comp_dir,
12375 ULONGEST signature, int is_debug_types)
12376 {
12377 struct objfile *objfile = dwarf2_per_objfile->objfile;
12378 const struct dwp_hash_table *dwp_htab =
12379 is_debug_types ? dwp_file->tus : dwp_file->cus;
12380 bfd *dbfd = dwp_file->dbfd.get ();
12381 const char *kind = is_debug_types ? "TU" : "CU";
12382 struct dwo_file *dwo_file;
12383 struct dwo_unit *dwo_unit;
12384 struct virtual_v1_dwo_sections sections;
12385 void **dwo_file_slot;
12386 int i;
12387
12388 gdb_assert (dwp_file->version == 1);
12389
12390 if (dwarf_read_debug)
12391 {
12392 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12393 kind,
12394 pulongest (unit_index), hex_string (signature),
12395 dwp_file->name);
12396 }
12397
12398 /* Fetch the sections of this DWO unit.
12399 Put a limit on the number of sections we look for so that bad data
12400 doesn't cause us to loop forever. */
12401
12402 #define MAX_NR_V1_DWO_SECTIONS \
12403 (1 /* .debug_info or .debug_types */ \
12404 + 1 /* .debug_abbrev */ \
12405 + 1 /* .debug_line */ \
12406 + 1 /* .debug_loc */ \
12407 + 1 /* .debug_str_offsets */ \
12408 + 1 /* .debug_macro or .debug_macinfo */ \
12409 + 1 /* trailing zero */)
12410
12411 memset (&sections, 0, sizeof (sections));
12412
12413 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12414 {
12415 asection *sectp;
12416 uint32_t section_nr =
12417 read_4_bytes (dbfd,
12418 dwp_htab->section_pool.v1.indices
12419 + (unit_index + i) * sizeof (uint32_t));
12420
12421 if (section_nr == 0)
12422 break;
12423 if (section_nr >= dwp_file->num_sections)
12424 {
12425 error (_("Dwarf Error: bad DWP hash table, section number too large"
12426 " [in module %s]"),
12427 dwp_file->name);
12428 }
12429
12430 sectp = dwp_file->elf_sections[section_nr];
12431 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12432 {
12433 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12434 " [in module %s]"),
12435 dwp_file->name);
12436 }
12437 }
12438
12439 if (i < 2
12440 || dwarf2_section_empty_p (&sections.info_or_types)
12441 || dwarf2_section_empty_p (&sections.abbrev))
12442 {
12443 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12444 " [in module %s]"),
12445 dwp_file->name);
12446 }
12447 if (i == MAX_NR_V1_DWO_SECTIONS)
12448 {
12449 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12450 " [in module %s]"),
12451 dwp_file->name);
12452 }
12453
12454 /* It's easier for the rest of the code if we fake a struct dwo_file and
12455 have dwo_unit "live" in that. At least for now.
12456
12457 The DWP file can be made up of a random collection of CUs and TUs.
12458 However, for each CU + set of TUs that came from the same original DWO
12459 file, we can combine them back into a virtual DWO file to save space
12460 (fewer struct dwo_file objects to allocate). Remember that for really
12461 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12462
12463 std::string virtual_dwo_name =
12464 string_printf ("virtual-dwo/%d-%d-%d-%d",
12465 get_section_id (&sections.abbrev),
12466 get_section_id (&sections.line),
12467 get_section_id (&sections.loc),
12468 get_section_id (&sections.str_offsets));
12469 /* Can we use an existing virtual DWO file? */
12470 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12471 virtual_dwo_name.c_str (),
12472 comp_dir);
12473 /* Create one if necessary. */
12474 if (*dwo_file_slot == NULL)
12475 {
12476 if (dwarf_read_debug)
12477 {
12478 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12479 virtual_dwo_name.c_str ());
12480 }
12481 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12482 dwo_file->dwo_name
12483 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12484 virtual_dwo_name.c_str (),
12485 virtual_dwo_name.size ());
12486 dwo_file->comp_dir = comp_dir;
12487 dwo_file->sections.abbrev = sections.abbrev;
12488 dwo_file->sections.line = sections.line;
12489 dwo_file->sections.loc = sections.loc;
12490 dwo_file->sections.macinfo = sections.macinfo;
12491 dwo_file->sections.macro = sections.macro;
12492 dwo_file->sections.str_offsets = sections.str_offsets;
12493 /* The "str" section is global to the entire DWP file. */
12494 dwo_file->sections.str = dwp_file->sections.str;
12495 /* The info or types section is assigned below to dwo_unit,
12496 there's no need to record it in dwo_file.
12497 Also, we can't simply record type sections in dwo_file because
12498 we record a pointer into the vector in dwo_unit. As we collect more
12499 types we'll grow the vector and eventually have to reallocate space
12500 for it, invalidating all copies of pointers into the previous
12501 contents. */
12502 *dwo_file_slot = dwo_file;
12503 }
12504 else
12505 {
12506 if (dwarf_read_debug)
12507 {
12508 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12509 virtual_dwo_name.c_str ());
12510 }
12511 dwo_file = (struct dwo_file *) *dwo_file_slot;
12512 }
12513
12514 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12515 dwo_unit->dwo_file = dwo_file;
12516 dwo_unit->signature = signature;
12517 dwo_unit->section =
12518 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12519 *dwo_unit->section = sections.info_or_types;
12520 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12521
12522 return dwo_unit;
12523 }
12524
12525 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12526 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12527 piece within that section used by a TU/CU, return a virtual section
12528 of just that piece. */
12529
12530 static struct dwarf2_section_info
12531 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12532 struct dwarf2_section_info *section,
12533 bfd_size_type offset, bfd_size_type size)
12534 {
12535 struct dwarf2_section_info result;
12536 asection *sectp;
12537
12538 gdb_assert (section != NULL);
12539 gdb_assert (!section->is_virtual);
12540
12541 memset (&result, 0, sizeof (result));
12542 result.s.containing_section = section;
12543 result.is_virtual = 1;
12544
12545 if (size == 0)
12546 return result;
12547
12548 sectp = get_section_bfd_section (section);
12549
12550 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12551 bounds of the real section. This is a pretty-rare event, so just
12552 flag an error (easier) instead of a warning and trying to cope. */
12553 if (sectp == NULL
12554 || offset + size > bfd_get_section_size (sectp))
12555 {
12556 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12557 " in section %s [in module %s]"),
12558 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12559 objfile_name (dwarf2_per_objfile->objfile));
12560 }
12561
12562 result.virtual_offset = offset;
12563 result.size = size;
12564 return result;
12565 }
12566
12567 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12568 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12569 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12570 This is for DWP version 2 files. */
12571
12572 static struct dwo_unit *
12573 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12574 struct dwp_file *dwp_file,
12575 uint32_t unit_index,
12576 const char *comp_dir,
12577 ULONGEST signature, int is_debug_types)
12578 {
12579 struct objfile *objfile = dwarf2_per_objfile->objfile;
12580 const struct dwp_hash_table *dwp_htab =
12581 is_debug_types ? dwp_file->tus : dwp_file->cus;
12582 bfd *dbfd = dwp_file->dbfd.get ();
12583 const char *kind = is_debug_types ? "TU" : "CU";
12584 struct dwo_file *dwo_file;
12585 struct dwo_unit *dwo_unit;
12586 struct virtual_v2_dwo_sections sections;
12587 void **dwo_file_slot;
12588 int i;
12589
12590 gdb_assert (dwp_file->version == 2);
12591
12592 if (dwarf_read_debug)
12593 {
12594 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12595 kind,
12596 pulongest (unit_index), hex_string (signature),
12597 dwp_file->name);
12598 }
12599
12600 /* Fetch the section offsets of this DWO unit. */
12601
12602 memset (&sections, 0, sizeof (sections));
12603
12604 for (i = 0; i < dwp_htab->nr_columns; ++i)
12605 {
12606 uint32_t offset = read_4_bytes (dbfd,
12607 dwp_htab->section_pool.v2.offsets
12608 + (((unit_index - 1) * dwp_htab->nr_columns
12609 + i)
12610 * sizeof (uint32_t)));
12611 uint32_t size = read_4_bytes (dbfd,
12612 dwp_htab->section_pool.v2.sizes
12613 + (((unit_index - 1) * dwp_htab->nr_columns
12614 + i)
12615 * sizeof (uint32_t)));
12616
12617 switch (dwp_htab->section_pool.v2.section_ids[i])
12618 {
12619 case DW_SECT_INFO:
12620 case DW_SECT_TYPES:
12621 sections.info_or_types_offset = offset;
12622 sections.info_or_types_size = size;
12623 break;
12624 case DW_SECT_ABBREV:
12625 sections.abbrev_offset = offset;
12626 sections.abbrev_size = size;
12627 break;
12628 case DW_SECT_LINE:
12629 sections.line_offset = offset;
12630 sections.line_size = size;
12631 break;
12632 case DW_SECT_LOC:
12633 sections.loc_offset = offset;
12634 sections.loc_size = size;
12635 break;
12636 case DW_SECT_STR_OFFSETS:
12637 sections.str_offsets_offset = offset;
12638 sections.str_offsets_size = size;
12639 break;
12640 case DW_SECT_MACINFO:
12641 sections.macinfo_offset = offset;
12642 sections.macinfo_size = size;
12643 break;
12644 case DW_SECT_MACRO:
12645 sections.macro_offset = offset;
12646 sections.macro_size = size;
12647 break;
12648 }
12649 }
12650
12651 /* It's easier for the rest of the code if we fake a struct dwo_file and
12652 have dwo_unit "live" in that. At least for now.
12653
12654 The DWP file can be made up of a random collection of CUs and TUs.
12655 However, for each CU + set of TUs that came from the same original DWO
12656 file, we can combine them back into a virtual DWO file to save space
12657 (fewer struct dwo_file objects to allocate). Remember that for really
12658 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12659
12660 std::string virtual_dwo_name =
12661 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12662 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12663 (long) (sections.line_size ? sections.line_offset : 0),
12664 (long) (sections.loc_size ? sections.loc_offset : 0),
12665 (long) (sections.str_offsets_size
12666 ? sections.str_offsets_offset : 0));
12667 /* Can we use an existing virtual DWO file? */
12668 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12669 virtual_dwo_name.c_str (),
12670 comp_dir);
12671 /* Create one if necessary. */
12672 if (*dwo_file_slot == NULL)
12673 {
12674 if (dwarf_read_debug)
12675 {
12676 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12677 virtual_dwo_name.c_str ());
12678 }
12679 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12680 dwo_file->dwo_name
12681 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12682 virtual_dwo_name.c_str (),
12683 virtual_dwo_name.size ());
12684 dwo_file->comp_dir = comp_dir;
12685 dwo_file->sections.abbrev =
12686 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12687 sections.abbrev_offset, sections.abbrev_size);
12688 dwo_file->sections.line =
12689 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12690 sections.line_offset, sections.line_size);
12691 dwo_file->sections.loc =
12692 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12693 sections.loc_offset, sections.loc_size);
12694 dwo_file->sections.macinfo =
12695 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12696 sections.macinfo_offset, sections.macinfo_size);
12697 dwo_file->sections.macro =
12698 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12699 sections.macro_offset, sections.macro_size);
12700 dwo_file->sections.str_offsets =
12701 create_dwp_v2_section (dwarf2_per_objfile,
12702 &dwp_file->sections.str_offsets,
12703 sections.str_offsets_offset,
12704 sections.str_offsets_size);
12705 /* The "str" section is global to the entire DWP file. */
12706 dwo_file->sections.str = dwp_file->sections.str;
12707 /* The info or types section is assigned below to dwo_unit,
12708 there's no need to record it in dwo_file.
12709 Also, we can't simply record type sections in dwo_file because
12710 we record a pointer into the vector in dwo_unit. As we collect more
12711 types we'll grow the vector and eventually have to reallocate space
12712 for it, invalidating all copies of pointers into the previous
12713 contents. */
12714 *dwo_file_slot = dwo_file;
12715 }
12716 else
12717 {
12718 if (dwarf_read_debug)
12719 {
12720 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12721 virtual_dwo_name.c_str ());
12722 }
12723 dwo_file = (struct dwo_file *) *dwo_file_slot;
12724 }
12725
12726 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12727 dwo_unit->dwo_file = dwo_file;
12728 dwo_unit->signature = signature;
12729 dwo_unit->section =
12730 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12731 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12732 is_debug_types
12733 ? &dwp_file->sections.types
12734 : &dwp_file->sections.info,
12735 sections.info_or_types_offset,
12736 sections.info_or_types_size);
12737 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12738
12739 return dwo_unit;
12740 }
12741
12742 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12743 Returns NULL if the signature isn't found. */
12744
12745 static struct dwo_unit *
12746 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12747 struct dwp_file *dwp_file, const char *comp_dir,
12748 ULONGEST signature, int is_debug_types)
12749 {
12750 const struct dwp_hash_table *dwp_htab =
12751 is_debug_types ? dwp_file->tus : dwp_file->cus;
12752 bfd *dbfd = dwp_file->dbfd.get ();
12753 uint32_t mask = dwp_htab->nr_slots - 1;
12754 uint32_t hash = signature & mask;
12755 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12756 unsigned int i;
12757 void **slot;
12758 struct dwo_unit find_dwo_cu;
12759
12760 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12761 find_dwo_cu.signature = signature;
12762 slot = htab_find_slot (is_debug_types
12763 ? dwp_file->loaded_tus
12764 : dwp_file->loaded_cus,
12765 &find_dwo_cu, INSERT);
12766
12767 if (*slot != NULL)
12768 return (struct dwo_unit *) *slot;
12769
12770 /* Use a for loop so that we don't loop forever on bad debug info. */
12771 for (i = 0; i < dwp_htab->nr_slots; ++i)
12772 {
12773 ULONGEST signature_in_table;
12774
12775 signature_in_table =
12776 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12777 if (signature_in_table == signature)
12778 {
12779 uint32_t unit_index =
12780 read_4_bytes (dbfd,
12781 dwp_htab->unit_table + hash * sizeof (uint32_t));
12782
12783 if (dwp_file->version == 1)
12784 {
12785 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12786 dwp_file, unit_index,
12787 comp_dir, signature,
12788 is_debug_types);
12789 }
12790 else
12791 {
12792 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12793 dwp_file, unit_index,
12794 comp_dir, signature,
12795 is_debug_types);
12796 }
12797 return (struct dwo_unit *) *slot;
12798 }
12799 if (signature_in_table == 0)
12800 return NULL;
12801 hash = (hash + hash2) & mask;
12802 }
12803
12804 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12805 " [in module %s]"),
12806 dwp_file->name);
12807 }
12808
12809 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12810 Open the file specified by FILE_NAME and hand it off to BFD for
12811 preliminary analysis. Return a newly initialized bfd *, which
12812 includes a canonicalized copy of FILE_NAME.
12813 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12814 SEARCH_CWD is true if the current directory is to be searched.
12815 It will be searched before debug-file-directory.
12816 If successful, the file is added to the bfd include table of the
12817 objfile's bfd (see gdb_bfd_record_inclusion).
12818 If unable to find/open the file, return NULL.
12819 NOTE: This function is derived from symfile_bfd_open. */
12820
12821 static gdb_bfd_ref_ptr
12822 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12823 const char *file_name, int is_dwp, int search_cwd)
12824 {
12825 int desc;
12826 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12827 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12828 to debug_file_directory. */
12829 const char *search_path;
12830 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12831
12832 gdb::unique_xmalloc_ptr<char> search_path_holder;
12833 if (search_cwd)
12834 {
12835 if (*debug_file_directory != '\0')
12836 {
12837 search_path_holder.reset (concat (".", dirname_separator_string,
12838 debug_file_directory,
12839 (char *) NULL));
12840 search_path = search_path_holder.get ();
12841 }
12842 else
12843 search_path = ".";
12844 }
12845 else
12846 search_path = debug_file_directory;
12847
12848 openp_flags flags = OPF_RETURN_REALPATH;
12849 if (is_dwp)
12850 flags |= OPF_SEARCH_IN_PATH;
12851
12852 gdb::unique_xmalloc_ptr<char> absolute_name;
12853 desc = openp (search_path, flags, file_name,
12854 O_RDONLY | O_BINARY, &absolute_name);
12855 if (desc < 0)
12856 return NULL;
12857
12858 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12859 gnutarget, desc));
12860 if (sym_bfd == NULL)
12861 return NULL;
12862 bfd_set_cacheable (sym_bfd.get (), 1);
12863
12864 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12865 return NULL;
12866
12867 /* Success. Record the bfd as having been included by the objfile's bfd.
12868 This is important because things like demangled_names_hash lives in the
12869 objfile's per_bfd space and may have references to things like symbol
12870 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12871 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12872
12873 return sym_bfd;
12874 }
12875
12876 /* Try to open DWO file FILE_NAME.
12877 COMP_DIR is the DW_AT_comp_dir attribute.
12878 The result is the bfd handle of the file.
12879 If there is a problem finding or opening the file, return NULL.
12880 Upon success, the canonicalized path of the file is stored in the bfd,
12881 same as symfile_bfd_open. */
12882
12883 static gdb_bfd_ref_ptr
12884 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12885 const char *file_name, const char *comp_dir)
12886 {
12887 if (IS_ABSOLUTE_PATH (file_name))
12888 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12889 0 /*is_dwp*/, 0 /*search_cwd*/);
12890
12891 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12892
12893 if (comp_dir != NULL)
12894 {
12895 char *path_to_try = concat (comp_dir, SLASH_STRING,
12896 file_name, (char *) NULL);
12897
12898 /* NOTE: If comp_dir is a relative path, this will also try the
12899 search path, which seems useful. */
12900 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12901 path_to_try,
12902 0 /*is_dwp*/,
12903 1 /*search_cwd*/));
12904 xfree (path_to_try);
12905 if (abfd != NULL)
12906 return abfd;
12907 }
12908
12909 /* That didn't work, try debug-file-directory, which, despite its name,
12910 is a list of paths. */
12911
12912 if (*debug_file_directory == '\0')
12913 return NULL;
12914
12915 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12916 0 /*is_dwp*/, 1 /*search_cwd*/);
12917 }
12918
12919 /* This function is mapped across the sections and remembers the offset and
12920 size of each of the DWO debugging sections we are interested in. */
12921
12922 static void
12923 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12924 {
12925 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12926 const struct dwop_section_names *names = &dwop_section_names;
12927
12928 if (section_is_p (sectp->name, &names->abbrev_dwo))
12929 {
12930 dwo_sections->abbrev.s.section = sectp;
12931 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12932 }
12933 else if (section_is_p (sectp->name, &names->info_dwo))
12934 {
12935 dwo_sections->info.s.section = sectp;
12936 dwo_sections->info.size = bfd_get_section_size (sectp);
12937 }
12938 else if (section_is_p (sectp->name, &names->line_dwo))
12939 {
12940 dwo_sections->line.s.section = sectp;
12941 dwo_sections->line.size = bfd_get_section_size (sectp);
12942 }
12943 else if (section_is_p (sectp->name, &names->loc_dwo))
12944 {
12945 dwo_sections->loc.s.section = sectp;
12946 dwo_sections->loc.size = bfd_get_section_size (sectp);
12947 }
12948 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12949 {
12950 dwo_sections->macinfo.s.section = sectp;
12951 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12952 }
12953 else if (section_is_p (sectp->name, &names->macro_dwo))
12954 {
12955 dwo_sections->macro.s.section = sectp;
12956 dwo_sections->macro.size = bfd_get_section_size (sectp);
12957 }
12958 else if (section_is_p (sectp->name, &names->str_dwo))
12959 {
12960 dwo_sections->str.s.section = sectp;
12961 dwo_sections->str.size = bfd_get_section_size (sectp);
12962 }
12963 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12964 {
12965 dwo_sections->str_offsets.s.section = sectp;
12966 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12967 }
12968 else if (section_is_p (sectp->name, &names->types_dwo))
12969 {
12970 struct dwarf2_section_info type_section;
12971
12972 memset (&type_section, 0, sizeof (type_section));
12973 type_section.s.section = sectp;
12974 type_section.size = bfd_get_section_size (sectp);
12975 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12976 &type_section);
12977 }
12978 }
12979
12980 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12981 by PER_CU. This is for the non-DWP case.
12982 The result is NULL if DWO_NAME can't be found. */
12983
12984 static struct dwo_file *
12985 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12986 const char *dwo_name, const char *comp_dir)
12987 {
12988 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12989 struct objfile *objfile = dwarf2_per_objfile->objfile;
12990
12991 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
12992 if (dbfd == NULL)
12993 {
12994 if (dwarf_read_debug)
12995 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12996 return NULL;
12997 }
12998
12999 /* We use a unique pointer here, despite the obstack allocation,
13000 because a dwo_file needs some cleanup if it is abandoned. */
13001 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
13002 struct dwo_file));
13003 dwo_file->dwo_name = dwo_name;
13004 dwo_file->comp_dir = comp_dir;
13005 dwo_file->dbfd = dbfd.release ();
13006
13007 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13008 &dwo_file->sections);
13009
13010 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13011 dwo_file->cus);
13012
13013 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13014 dwo_file->sections.types, dwo_file->tus);
13015
13016 if (dwarf_read_debug)
13017 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13018
13019 return dwo_file.release ();
13020 }
13021
13022 /* This function is mapped across the sections and remembers the offset and
13023 size of each of the DWP debugging sections common to version 1 and 2 that
13024 we are interested in. */
13025
13026 static void
13027 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13028 void *dwp_file_ptr)
13029 {
13030 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13031 const struct dwop_section_names *names = &dwop_section_names;
13032 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13033
13034 /* Record the ELF section number for later lookup: this is what the
13035 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13036 gdb_assert (elf_section_nr < dwp_file->num_sections);
13037 dwp_file->elf_sections[elf_section_nr] = sectp;
13038
13039 /* Look for specific sections that we need. */
13040 if (section_is_p (sectp->name, &names->str_dwo))
13041 {
13042 dwp_file->sections.str.s.section = sectp;
13043 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13044 }
13045 else if (section_is_p (sectp->name, &names->cu_index))
13046 {
13047 dwp_file->sections.cu_index.s.section = sectp;
13048 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13049 }
13050 else if (section_is_p (sectp->name, &names->tu_index))
13051 {
13052 dwp_file->sections.tu_index.s.section = sectp;
13053 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13054 }
13055 }
13056
13057 /* This function is mapped across the sections and remembers the offset and
13058 size of each of the DWP version 2 debugging sections that we are interested
13059 in. This is split into a separate function because we don't know if we
13060 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13061
13062 static void
13063 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13064 {
13065 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13066 const struct dwop_section_names *names = &dwop_section_names;
13067 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13068
13069 /* Record the ELF section number for later lookup: this is what the
13070 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13071 gdb_assert (elf_section_nr < dwp_file->num_sections);
13072 dwp_file->elf_sections[elf_section_nr] = sectp;
13073
13074 /* Look for specific sections that we need. */
13075 if (section_is_p (sectp->name, &names->abbrev_dwo))
13076 {
13077 dwp_file->sections.abbrev.s.section = sectp;
13078 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13079 }
13080 else if (section_is_p (sectp->name, &names->info_dwo))
13081 {
13082 dwp_file->sections.info.s.section = sectp;
13083 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13084 }
13085 else if (section_is_p (sectp->name, &names->line_dwo))
13086 {
13087 dwp_file->sections.line.s.section = sectp;
13088 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13089 }
13090 else if (section_is_p (sectp->name, &names->loc_dwo))
13091 {
13092 dwp_file->sections.loc.s.section = sectp;
13093 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13094 }
13095 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13096 {
13097 dwp_file->sections.macinfo.s.section = sectp;
13098 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13099 }
13100 else if (section_is_p (sectp->name, &names->macro_dwo))
13101 {
13102 dwp_file->sections.macro.s.section = sectp;
13103 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13104 }
13105 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13106 {
13107 dwp_file->sections.str_offsets.s.section = sectp;
13108 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13109 }
13110 else if (section_is_p (sectp->name, &names->types_dwo))
13111 {
13112 dwp_file->sections.types.s.section = sectp;
13113 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13114 }
13115 }
13116
13117 /* Hash function for dwp_file loaded CUs/TUs. */
13118
13119 static hashval_t
13120 hash_dwp_loaded_cutus (const void *item)
13121 {
13122 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13123
13124 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13125 return dwo_unit->signature;
13126 }
13127
13128 /* Equality function for dwp_file loaded CUs/TUs. */
13129
13130 static int
13131 eq_dwp_loaded_cutus (const void *a, const void *b)
13132 {
13133 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13134 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13135
13136 return dua->signature == dub->signature;
13137 }
13138
13139 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13140
13141 static htab_t
13142 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13143 {
13144 return htab_create_alloc_ex (3,
13145 hash_dwp_loaded_cutus,
13146 eq_dwp_loaded_cutus,
13147 NULL,
13148 &objfile->objfile_obstack,
13149 hashtab_obstack_allocate,
13150 dummy_obstack_deallocate);
13151 }
13152
13153 /* Try to open DWP file FILE_NAME.
13154 The result is the bfd handle of the file.
13155 If there is a problem finding or opening the file, return NULL.
13156 Upon success, the canonicalized path of the file is stored in the bfd,
13157 same as symfile_bfd_open. */
13158
13159 static gdb_bfd_ref_ptr
13160 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13161 const char *file_name)
13162 {
13163 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13164 1 /*is_dwp*/,
13165 1 /*search_cwd*/));
13166 if (abfd != NULL)
13167 return abfd;
13168
13169 /* Work around upstream bug 15652.
13170 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13171 [Whether that's a "bug" is debatable, but it is getting in our way.]
13172 We have no real idea where the dwp file is, because gdb's realpath-ing
13173 of the executable's path may have discarded the needed info.
13174 [IWBN if the dwp file name was recorded in the executable, akin to
13175 .gnu_debuglink, but that doesn't exist yet.]
13176 Strip the directory from FILE_NAME and search again. */
13177 if (*debug_file_directory != '\0')
13178 {
13179 /* Don't implicitly search the current directory here.
13180 If the user wants to search "." to handle this case,
13181 it must be added to debug-file-directory. */
13182 return try_open_dwop_file (dwarf2_per_objfile,
13183 lbasename (file_name), 1 /*is_dwp*/,
13184 0 /*search_cwd*/);
13185 }
13186
13187 return NULL;
13188 }
13189
13190 /* Initialize the use of the DWP file for the current objfile.
13191 By convention the name of the DWP file is ${objfile}.dwp.
13192 The result is NULL if it can't be found. */
13193
13194 static std::unique_ptr<struct dwp_file>
13195 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13196 {
13197 struct objfile *objfile = dwarf2_per_objfile->objfile;
13198
13199 /* Try to find first .dwp for the binary file before any symbolic links
13200 resolving. */
13201
13202 /* If the objfile is a debug file, find the name of the real binary
13203 file and get the name of dwp file from there. */
13204 std::string dwp_name;
13205 if (objfile->separate_debug_objfile_backlink != NULL)
13206 {
13207 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13208 const char *backlink_basename = lbasename (backlink->original_name);
13209
13210 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13211 }
13212 else
13213 dwp_name = objfile->original_name;
13214
13215 dwp_name += ".dwp";
13216
13217 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13218 if (dbfd == NULL
13219 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13220 {
13221 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13222 dwp_name = objfile_name (objfile);
13223 dwp_name += ".dwp";
13224 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13225 }
13226
13227 if (dbfd == NULL)
13228 {
13229 if (dwarf_read_debug)
13230 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13231 return std::unique_ptr<dwp_file> ();
13232 }
13233
13234 const char *name = bfd_get_filename (dbfd.get ());
13235 std::unique_ptr<struct dwp_file> dwp_file
13236 (new struct dwp_file (name, std::move (dbfd)));
13237
13238 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13239 dwp_file->elf_sections =
13240 OBSTACK_CALLOC (&objfile->objfile_obstack,
13241 dwp_file->num_sections, asection *);
13242
13243 bfd_map_over_sections (dwp_file->dbfd.get (),
13244 dwarf2_locate_common_dwp_sections,
13245 dwp_file.get ());
13246
13247 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13248 0);
13249
13250 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13251 1);
13252
13253 /* The DWP file version is stored in the hash table. Oh well. */
13254 if (dwp_file->cus && dwp_file->tus
13255 && dwp_file->cus->version != dwp_file->tus->version)
13256 {
13257 /* Technically speaking, we should try to limp along, but this is
13258 pretty bizarre. We use pulongest here because that's the established
13259 portability solution (e.g, we cannot use %u for uint32_t). */
13260 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13261 " TU version %s [in DWP file %s]"),
13262 pulongest (dwp_file->cus->version),
13263 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13264 }
13265
13266 if (dwp_file->cus)
13267 dwp_file->version = dwp_file->cus->version;
13268 else if (dwp_file->tus)
13269 dwp_file->version = dwp_file->tus->version;
13270 else
13271 dwp_file->version = 2;
13272
13273 if (dwp_file->version == 2)
13274 bfd_map_over_sections (dwp_file->dbfd.get (),
13275 dwarf2_locate_v2_dwp_sections,
13276 dwp_file.get ());
13277
13278 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13279 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13280
13281 if (dwarf_read_debug)
13282 {
13283 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13284 fprintf_unfiltered (gdb_stdlog,
13285 " %s CUs, %s TUs\n",
13286 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13287 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13288 }
13289
13290 return dwp_file;
13291 }
13292
13293 /* Wrapper around open_and_init_dwp_file, only open it once. */
13294
13295 static struct dwp_file *
13296 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13297 {
13298 if (! dwarf2_per_objfile->dwp_checked)
13299 {
13300 dwarf2_per_objfile->dwp_file
13301 = open_and_init_dwp_file (dwarf2_per_objfile);
13302 dwarf2_per_objfile->dwp_checked = 1;
13303 }
13304 return dwarf2_per_objfile->dwp_file.get ();
13305 }
13306
13307 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13308 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13309 or in the DWP file for the objfile, referenced by THIS_UNIT.
13310 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13311 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13312
13313 This is called, for example, when wanting to read a variable with a
13314 complex location. Therefore we don't want to do file i/o for every call.
13315 Therefore we don't want to look for a DWO file on every call.
13316 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13317 then we check if we've already seen DWO_NAME, and only THEN do we check
13318 for a DWO file.
13319
13320 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13321 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13322
13323 static struct dwo_unit *
13324 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13325 const char *dwo_name, const char *comp_dir,
13326 ULONGEST signature, int is_debug_types)
13327 {
13328 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13329 struct objfile *objfile = dwarf2_per_objfile->objfile;
13330 const char *kind = is_debug_types ? "TU" : "CU";
13331 void **dwo_file_slot;
13332 struct dwo_file *dwo_file;
13333 struct dwp_file *dwp_file;
13334
13335 /* First see if there's a DWP file.
13336 If we have a DWP file but didn't find the DWO inside it, don't
13337 look for the original DWO file. It makes gdb behave differently
13338 depending on whether one is debugging in the build tree. */
13339
13340 dwp_file = get_dwp_file (dwarf2_per_objfile);
13341 if (dwp_file != NULL)
13342 {
13343 const struct dwp_hash_table *dwp_htab =
13344 is_debug_types ? dwp_file->tus : dwp_file->cus;
13345
13346 if (dwp_htab != NULL)
13347 {
13348 struct dwo_unit *dwo_cutu =
13349 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13350 signature, is_debug_types);
13351
13352 if (dwo_cutu != NULL)
13353 {
13354 if (dwarf_read_debug)
13355 {
13356 fprintf_unfiltered (gdb_stdlog,
13357 "Virtual DWO %s %s found: @%s\n",
13358 kind, hex_string (signature),
13359 host_address_to_string (dwo_cutu));
13360 }
13361 return dwo_cutu;
13362 }
13363 }
13364 }
13365 else
13366 {
13367 /* No DWP file, look for the DWO file. */
13368
13369 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13370 dwo_name, comp_dir);
13371 if (*dwo_file_slot == NULL)
13372 {
13373 /* Read in the file and build a table of the CUs/TUs it contains. */
13374 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13375 }
13376 /* NOTE: This will be NULL if unable to open the file. */
13377 dwo_file = (struct dwo_file *) *dwo_file_slot;
13378
13379 if (dwo_file != NULL)
13380 {
13381 struct dwo_unit *dwo_cutu = NULL;
13382
13383 if (is_debug_types && dwo_file->tus)
13384 {
13385 struct dwo_unit find_dwo_cutu;
13386
13387 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13388 find_dwo_cutu.signature = signature;
13389 dwo_cutu
13390 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13391 }
13392 else if (!is_debug_types && dwo_file->cus)
13393 {
13394 struct dwo_unit find_dwo_cutu;
13395
13396 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13397 find_dwo_cutu.signature = signature;
13398 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13399 &find_dwo_cutu);
13400 }
13401
13402 if (dwo_cutu != NULL)
13403 {
13404 if (dwarf_read_debug)
13405 {
13406 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13407 kind, dwo_name, hex_string (signature),
13408 host_address_to_string (dwo_cutu));
13409 }
13410 return dwo_cutu;
13411 }
13412 }
13413 }
13414
13415 /* We didn't find it. This could mean a dwo_id mismatch, or
13416 someone deleted the DWO/DWP file, or the search path isn't set up
13417 correctly to find the file. */
13418
13419 if (dwarf_read_debug)
13420 {
13421 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13422 kind, dwo_name, hex_string (signature));
13423 }
13424
13425 /* This is a warning and not a complaint because it can be caused by
13426 pilot error (e.g., user accidentally deleting the DWO). */
13427 {
13428 /* Print the name of the DWP file if we looked there, helps the user
13429 better diagnose the problem. */
13430 std::string dwp_text;
13431
13432 if (dwp_file != NULL)
13433 dwp_text = string_printf (" [in DWP file %s]",
13434 lbasename (dwp_file->name));
13435
13436 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13437 " [in module %s]"),
13438 kind, dwo_name, hex_string (signature),
13439 dwp_text.c_str (),
13440 this_unit->is_debug_types ? "TU" : "CU",
13441 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13442 }
13443 return NULL;
13444 }
13445
13446 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13447 See lookup_dwo_cutu_unit for details. */
13448
13449 static struct dwo_unit *
13450 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13451 const char *dwo_name, const char *comp_dir,
13452 ULONGEST signature)
13453 {
13454 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13455 }
13456
13457 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13458 See lookup_dwo_cutu_unit for details. */
13459
13460 static struct dwo_unit *
13461 lookup_dwo_type_unit (struct signatured_type *this_tu,
13462 const char *dwo_name, const char *comp_dir)
13463 {
13464 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13465 }
13466
13467 /* Traversal function for queue_and_load_all_dwo_tus. */
13468
13469 static int
13470 queue_and_load_dwo_tu (void **slot, void *info)
13471 {
13472 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13473 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13474 ULONGEST signature = dwo_unit->signature;
13475 struct signatured_type *sig_type =
13476 lookup_dwo_signatured_type (per_cu->cu, signature);
13477
13478 if (sig_type != NULL)
13479 {
13480 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13481
13482 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13483 a real dependency of PER_CU on SIG_TYPE. That is detected later
13484 while processing PER_CU. */
13485 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13486 load_full_type_unit (sig_cu);
13487 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13488 }
13489
13490 return 1;
13491 }
13492
13493 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13494 The DWO may have the only definition of the type, though it may not be
13495 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13496 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13497
13498 static void
13499 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13500 {
13501 struct dwo_unit *dwo_unit;
13502 struct dwo_file *dwo_file;
13503
13504 gdb_assert (!per_cu->is_debug_types);
13505 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13506 gdb_assert (per_cu->cu != NULL);
13507
13508 dwo_unit = per_cu->cu->dwo_unit;
13509 gdb_assert (dwo_unit != NULL);
13510
13511 dwo_file = dwo_unit->dwo_file;
13512 if (dwo_file->tus != NULL)
13513 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13514 }
13515
13516 /* Free all resources associated with DWO_FILE.
13517 Close the DWO file and munmap the sections. */
13518
13519 static void
13520 free_dwo_file (struct dwo_file *dwo_file)
13521 {
13522 /* Note: dbfd is NULL for virtual DWO files. */
13523 gdb_bfd_unref (dwo_file->dbfd);
13524
13525 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13526 }
13527
13528 /* Traversal function for free_dwo_files. */
13529
13530 static int
13531 free_dwo_file_from_slot (void **slot, void *info)
13532 {
13533 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13534
13535 free_dwo_file (dwo_file);
13536
13537 return 1;
13538 }
13539
13540 /* Free all resources associated with DWO_FILES. */
13541
13542 static void
13543 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13544 {
13545 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13546 }
13547 \f
13548 /* Read in various DIEs. */
13549
13550 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13551 Inherit only the children of the DW_AT_abstract_origin DIE not being
13552 already referenced by DW_AT_abstract_origin from the children of the
13553 current DIE. */
13554
13555 static void
13556 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13557 {
13558 struct die_info *child_die;
13559 sect_offset *offsetp;
13560 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13561 struct die_info *origin_die;
13562 /* Iterator of the ORIGIN_DIE children. */
13563 struct die_info *origin_child_die;
13564 struct attribute *attr;
13565 struct dwarf2_cu *origin_cu;
13566 struct pending **origin_previous_list_in_scope;
13567
13568 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13569 if (!attr)
13570 return;
13571
13572 /* Note that following die references may follow to a die in a
13573 different cu. */
13574
13575 origin_cu = cu;
13576 origin_die = follow_die_ref (die, attr, &origin_cu);
13577
13578 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13579 symbols in. */
13580 origin_previous_list_in_scope = origin_cu->list_in_scope;
13581 origin_cu->list_in_scope = cu->list_in_scope;
13582
13583 if (die->tag != origin_die->tag
13584 && !(die->tag == DW_TAG_inlined_subroutine
13585 && origin_die->tag == DW_TAG_subprogram))
13586 complaint (_("DIE %s and its abstract origin %s have different tags"),
13587 sect_offset_str (die->sect_off),
13588 sect_offset_str (origin_die->sect_off));
13589
13590 std::vector<sect_offset> offsets;
13591
13592 for (child_die = die->child;
13593 child_die && child_die->tag;
13594 child_die = sibling_die (child_die))
13595 {
13596 struct die_info *child_origin_die;
13597 struct dwarf2_cu *child_origin_cu;
13598
13599 /* We are trying to process concrete instance entries:
13600 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13601 it's not relevant to our analysis here. i.e. detecting DIEs that are
13602 present in the abstract instance but not referenced in the concrete
13603 one. */
13604 if (child_die->tag == DW_TAG_call_site
13605 || child_die->tag == DW_TAG_GNU_call_site)
13606 continue;
13607
13608 /* For each CHILD_DIE, find the corresponding child of
13609 ORIGIN_DIE. If there is more than one layer of
13610 DW_AT_abstract_origin, follow them all; there shouldn't be,
13611 but GCC versions at least through 4.4 generate this (GCC PR
13612 40573). */
13613 child_origin_die = child_die;
13614 child_origin_cu = cu;
13615 while (1)
13616 {
13617 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13618 child_origin_cu);
13619 if (attr == NULL)
13620 break;
13621 child_origin_die = follow_die_ref (child_origin_die, attr,
13622 &child_origin_cu);
13623 }
13624
13625 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13626 counterpart may exist. */
13627 if (child_origin_die != child_die)
13628 {
13629 if (child_die->tag != child_origin_die->tag
13630 && !(child_die->tag == DW_TAG_inlined_subroutine
13631 && child_origin_die->tag == DW_TAG_subprogram))
13632 complaint (_("Child DIE %s and its abstract origin %s have "
13633 "different tags"),
13634 sect_offset_str (child_die->sect_off),
13635 sect_offset_str (child_origin_die->sect_off));
13636 if (child_origin_die->parent != origin_die)
13637 complaint (_("Child DIE %s and its abstract origin %s have "
13638 "different parents"),
13639 sect_offset_str (child_die->sect_off),
13640 sect_offset_str (child_origin_die->sect_off));
13641 else
13642 offsets.push_back (child_origin_die->sect_off);
13643 }
13644 }
13645 std::sort (offsets.begin (), offsets.end ());
13646 sect_offset *offsets_end = offsets.data () + offsets.size ();
13647 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13648 if (offsetp[-1] == *offsetp)
13649 complaint (_("Multiple children of DIE %s refer "
13650 "to DIE %s as their abstract origin"),
13651 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13652
13653 offsetp = offsets.data ();
13654 origin_child_die = origin_die->child;
13655 while (origin_child_die && origin_child_die->tag)
13656 {
13657 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13658 while (offsetp < offsets_end
13659 && *offsetp < origin_child_die->sect_off)
13660 offsetp++;
13661 if (offsetp >= offsets_end
13662 || *offsetp > origin_child_die->sect_off)
13663 {
13664 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13665 Check whether we're already processing ORIGIN_CHILD_DIE.
13666 This can happen with mutually referenced abstract_origins.
13667 PR 16581. */
13668 if (!origin_child_die->in_process)
13669 process_die (origin_child_die, origin_cu);
13670 }
13671 origin_child_die = sibling_die (origin_child_die);
13672 }
13673 origin_cu->list_in_scope = origin_previous_list_in_scope;
13674 }
13675
13676 static void
13677 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13678 {
13679 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13680 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13681 struct context_stack *newobj;
13682 CORE_ADDR lowpc;
13683 CORE_ADDR highpc;
13684 struct die_info *child_die;
13685 struct attribute *attr, *call_line, *call_file;
13686 const char *name;
13687 CORE_ADDR baseaddr;
13688 struct block *block;
13689 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13690 std::vector<struct symbol *> template_args;
13691 struct template_symbol *templ_func = NULL;
13692
13693 if (inlined_func)
13694 {
13695 /* If we do not have call site information, we can't show the
13696 caller of this inlined function. That's too confusing, so
13697 only use the scope for local variables. */
13698 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13699 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13700 if (call_line == NULL || call_file == NULL)
13701 {
13702 read_lexical_block_scope (die, cu);
13703 return;
13704 }
13705 }
13706
13707 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13708
13709 name = dwarf2_name (die, cu);
13710
13711 /* Ignore functions with missing or empty names. These are actually
13712 illegal according to the DWARF standard. */
13713 if (name == NULL)
13714 {
13715 complaint (_("missing name for subprogram DIE at %s"),
13716 sect_offset_str (die->sect_off));
13717 return;
13718 }
13719
13720 /* Ignore functions with missing or invalid low and high pc attributes. */
13721 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13722 <= PC_BOUNDS_INVALID)
13723 {
13724 attr = dwarf2_attr (die, DW_AT_external, cu);
13725 if (!attr || !DW_UNSND (attr))
13726 complaint (_("cannot get low and high bounds "
13727 "for subprogram DIE at %s"),
13728 sect_offset_str (die->sect_off));
13729 return;
13730 }
13731
13732 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13733 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13734
13735 /* If we have any template arguments, then we must allocate a
13736 different sort of symbol. */
13737 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13738 {
13739 if (child_die->tag == DW_TAG_template_type_param
13740 || child_die->tag == DW_TAG_template_value_param)
13741 {
13742 templ_func = allocate_template_symbol (objfile);
13743 templ_func->subclass = SYMBOL_TEMPLATE;
13744 break;
13745 }
13746 }
13747
13748 newobj = cu->get_builder ()->push_context (0, lowpc);
13749 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13750 (struct symbol *) templ_func);
13751
13752 /* If there is a location expression for DW_AT_frame_base, record
13753 it. */
13754 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13755 if (attr)
13756 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13757
13758 /* If there is a location for the static link, record it. */
13759 newobj->static_link = NULL;
13760 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13761 if (attr)
13762 {
13763 newobj->static_link
13764 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13765 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13766 }
13767
13768 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13769
13770 if (die->child != NULL)
13771 {
13772 child_die = die->child;
13773 while (child_die && child_die->tag)
13774 {
13775 if (child_die->tag == DW_TAG_template_type_param
13776 || child_die->tag == DW_TAG_template_value_param)
13777 {
13778 struct symbol *arg = new_symbol (child_die, NULL, cu);
13779
13780 if (arg != NULL)
13781 template_args.push_back (arg);
13782 }
13783 else
13784 process_die (child_die, cu);
13785 child_die = sibling_die (child_die);
13786 }
13787 }
13788
13789 inherit_abstract_dies (die, cu);
13790
13791 /* If we have a DW_AT_specification, we might need to import using
13792 directives from the context of the specification DIE. See the
13793 comment in determine_prefix. */
13794 if (cu->language == language_cplus
13795 && dwarf2_attr (die, DW_AT_specification, cu))
13796 {
13797 struct dwarf2_cu *spec_cu = cu;
13798 struct die_info *spec_die = die_specification (die, &spec_cu);
13799
13800 while (spec_die)
13801 {
13802 child_die = spec_die->child;
13803 while (child_die && child_die->tag)
13804 {
13805 if (child_die->tag == DW_TAG_imported_module)
13806 process_die (child_die, spec_cu);
13807 child_die = sibling_die (child_die);
13808 }
13809
13810 /* In some cases, GCC generates specification DIEs that
13811 themselves contain DW_AT_specification attributes. */
13812 spec_die = die_specification (spec_die, &spec_cu);
13813 }
13814 }
13815
13816 struct context_stack cstk = cu->get_builder ()->pop_context ();
13817 /* Make a block for the local symbols within. */
13818 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13819 cstk.static_link, lowpc, highpc);
13820
13821 /* For C++, set the block's scope. */
13822 if ((cu->language == language_cplus
13823 || cu->language == language_fortran
13824 || cu->language == language_d
13825 || cu->language == language_rust)
13826 && cu->processing_has_namespace_info)
13827 block_set_scope (block, determine_prefix (die, cu),
13828 &objfile->objfile_obstack);
13829
13830 /* If we have address ranges, record them. */
13831 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13832
13833 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13834
13835 /* Attach template arguments to function. */
13836 if (!template_args.empty ())
13837 {
13838 gdb_assert (templ_func != NULL);
13839
13840 templ_func->n_template_arguments = template_args.size ();
13841 templ_func->template_arguments
13842 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13843 templ_func->n_template_arguments);
13844 memcpy (templ_func->template_arguments,
13845 template_args.data (),
13846 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13847
13848 /* Make sure that the symtab is set on the new symbols. Even
13849 though they don't appear in this symtab directly, other parts
13850 of gdb assume that symbols do, and this is reasonably
13851 true. */
13852 for (symbol *sym : template_args)
13853 symbol_set_symtab (sym, symbol_symtab (templ_func));
13854 }
13855
13856 /* In C++, we can have functions nested inside functions (e.g., when
13857 a function declares a class that has methods). This means that
13858 when we finish processing a function scope, we may need to go
13859 back to building a containing block's symbol lists. */
13860 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13861 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13862
13863 /* If we've finished processing a top-level function, subsequent
13864 symbols go in the file symbol list. */
13865 if (cu->get_builder ()->outermost_context_p ())
13866 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13867 }
13868
13869 /* Process all the DIES contained within a lexical block scope. Start
13870 a new scope, process the dies, and then close the scope. */
13871
13872 static void
13873 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13874 {
13875 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13876 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13877 CORE_ADDR lowpc, highpc;
13878 struct die_info *child_die;
13879 CORE_ADDR baseaddr;
13880
13881 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13882
13883 /* Ignore blocks with missing or invalid low and high pc attributes. */
13884 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13885 as multiple lexical blocks? Handling children in a sane way would
13886 be nasty. Might be easier to properly extend generic blocks to
13887 describe ranges. */
13888 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13889 {
13890 case PC_BOUNDS_NOT_PRESENT:
13891 /* DW_TAG_lexical_block has no attributes, process its children as if
13892 there was no wrapping by that DW_TAG_lexical_block.
13893 GCC does no longer produces such DWARF since GCC r224161. */
13894 for (child_die = die->child;
13895 child_die != NULL && child_die->tag;
13896 child_die = sibling_die (child_die))
13897 process_die (child_die, cu);
13898 return;
13899 case PC_BOUNDS_INVALID:
13900 return;
13901 }
13902 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13903 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13904
13905 cu->get_builder ()->push_context (0, lowpc);
13906 if (die->child != NULL)
13907 {
13908 child_die = die->child;
13909 while (child_die && child_die->tag)
13910 {
13911 process_die (child_die, cu);
13912 child_die = sibling_die (child_die);
13913 }
13914 }
13915 inherit_abstract_dies (die, cu);
13916 struct context_stack cstk = cu->get_builder ()->pop_context ();
13917
13918 if (*cu->get_builder ()->get_local_symbols () != NULL
13919 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13920 {
13921 struct block *block
13922 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13923 cstk.start_addr, highpc);
13924
13925 /* Note that recording ranges after traversing children, as we
13926 do here, means that recording a parent's ranges entails
13927 walking across all its children's ranges as they appear in
13928 the address map, which is quadratic behavior.
13929
13930 It would be nicer to record the parent's ranges before
13931 traversing its children, simply overriding whatever you find
13932 there. But since we don't even decide whether to create a
13933 block until after we've traversed its children, that's hard
13934 to do. */
13935 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13936 }
13937 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13938 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13939 }
13940
13941 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13942
13943 static void
13944 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13945 {
13946 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13947 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13948 CORE_ADDR pc, baseaddr;
13949 struct attribute *attr;
13950 struct call_site *call_site, call_site_local;
13951 void **slot;
13952 int nparams;
13953 struct die_info *child_die;
13954
13955 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13956
13957 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13958 if (attr == NULL)
13959 {
13960 /* This was a pre-DWARF-5 GNU extension alias
13961 for DW_AT_call_return_pc. */
13962 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13963 }
13964 if (!attr)
13965 {
13966 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13967 "DIE %s [in module %s]"),
13968 sect_offset_str (die->sect_off), objfile_name (objfile));
13969 return;
13970 }
13971 pc = attr_value_as_address (attr) + baseaddr;
13972 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13973
13974 if (cu->call_site_htab == NULL)
13975 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13976 NULL, &objfile->objfile_obstack,
13977 hashtab_obstack_allocate, NULL);
13978 call_site_local.pc = pc;
13979 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13980 if (*slot != NULL)
13981 {
13982 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13983 "DIE %s [in module %s]"),
13984 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13985 objfile_name (objfile));
13986 return;
13987 }
13988
13989 /* Count parameters at the caller. */
13990
13991 nparams = 0;
13992 for (child_die = die->child; child_die && child_die->tag;
13993 child_die = sibling_die (child_die))
13994 {
13995 if (child_die->tag != DW_TAG_call_site_parameter
13996 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13997 {
13998 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13999 "DW_TAG_call_site child DIE %s [in module %s]"),
14000 child_die->tag, sect_offset_str (child_die->sect_off),
14001 objfile_name (objfile));
14002 continue;
14003 }
14004
14005 nparams++;
14006 }
14007
14008 call_site
14009 = ((struct call_site *)
14010 obstack_alloc (&objfile->objfile_obstack,
14011 sizeof (*call_site)
14012 + (sizeof (*call_site->parameter) * (nparams - 1))));
14013 *slot = call_site;
14014 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14015 call_site->pc = pc;
14016
14017 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14018 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14019 {
14020 struct die_info *func_die;
14021
14022 /* Skip also over DW_TAG_inlined_subroutine. */
14023 for (func_die = die->parent;
14024 func_die && func_die->tag != DW_TAG_subprogram
14025 && func_die->tag != DW_TAG_subroutine_type;
14026 func_die = func_die->parent);
14027
14028 /* DW_AT_call_all_calls is a superset
14029 of DW_AT_call_all_tail_calls. */
14030 if (func_die
14031 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14032 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14033 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14034 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14035 {
14036 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14037 not complete. But keep CALL_SITE for look ups via call_site_htab,
14038 both the initial caller containing the real return address PC and
14039 the final callee containing the current PC of a chain of tail
14040 calls do not need to have the tail call list complete. But any
14041 function candidate for a virtual tail call frame searched via
14042 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14043 determined unambiguously. */
14044 }
14045 else
14046 {
14047 struct type *func_type = NULL;
14048
14049 if (func_die)
14050 func_type = get_die_type (func_die, cu);
14051 if (func_type != NULL)
14052 {
14053 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14054
14055 /* Enlist this call site to the function. */
14056 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14057 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14058 }
14059 else
14060 complaint (_("Cannot find function owning DW_TAG_call_site "
14061 "DIE %s [in module %s]"),
14062 sect_offset_str (die->sect_off), objfile_name (objfile));
14063 }
14064 }
14065
14066 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14067 if (attr == NULL)
14068 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14069 if (attr == NULL)
14070 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14071 if (attr == NULL)
14072 {
14073 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14074 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14075 }
14076 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14077 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14078 /* Keep NULL DWARF_BLOCK. */;
14079 else if (attr_form_is_block (attr))
14080 {
14081 struct dwarf2_locexpr_baton *dlbaton;
14082
14083 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14084 dlbaton->data = DW_BLOCK (attr)->data;
14085 dlbaton->size = DW_BLOCK (attr)->size;
14086 dlbaton->per_cu = cu->per_cu;
14087
14088 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14089 }
14090 else if (attr_form_is_ref (attr))
14091 {
14092 struct dwarf2_cu *target_cu = cu;
14093 struct die_info *target_die;
14094
14095 target_die = follow_die_ref (die, attr, &target_cu);
14096 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14097 if (die_is_declaration (target_die, target_cu))
14098 {
14099 const char *target_physname;
14100
14101 /* Prefer the mangled name; otherwise compute the demangled one. */
14102 target_physname = dw2_linkage_name (target_die, target_cu);
14103 if (target_physname == NULL)
14104 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14105 if (target_physname == NULL)
14106 complaint (_("DW_AT_call_target target DIE has invalid "
14107 "physname, for referencing DIE %s [in module %s]"),
14108 sect_offset_str (die->sect_off), objfile_name (objfile));
14109 else
14110 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14111 }
14112 else
14113 {
14114 CORE_ADDR lowpc;
14115
14116 /* DW_AT_entry_pc should be preferred. */
14117 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14118 <= PC_BOUNDS_INVALID)
14119 complaint (_("DW_AT_call_target target DIE has invalid "
14120 "low pc, for referencing DIE %s [in module %s]"),
14121 sect_offset_str (die->sect_off), objfile_name (objfile));
14122 else
14123 {
14124 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14125 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14126 }
14127 }
14128 }
14129 else
14130 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14131 "block nor reference, for DIE %s [in module %s]"),
14132 sect_offset_str (die->sect_off), objfile_name (objfile));
14133
14134 call_site->per_cu = cu->per_cu;
14135
14136 for (child_die = die->child;
14137 child_die && child_die->tag;
14138 child_die = sibling_die (child_die))
14139 {
14140 struct call_site_parameter *parameter;
14141 struct attribute *loc, *origin;
14142
14143 if (child_die->tag != DW_TAG_call_site_parameter
14144 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14145 {
14146 /* Already printed the complaint above. */
14147 continue;
14148 }
14149
14150 gdb_assert (call_site->parameter_count < nparams);
14151 parameter = &call_site->parameter[call_site->parameter_count];
14152
14153 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14154 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14155 register is contained in DW_AT_call_value. */
14156
14157 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14158 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14159 if (origin == NULL)
14160 {
14161 /* This was a pre-DWARF-5 GNU extension alias
14162 for DW_AT_call_parameter. */
14163 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14164 }
14165 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14166 {
14167 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14168
14169 sect_offset sect_off
14170 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14171 if (!offset_in_cu_p (&cu->header, sect_off))
14172 {
14173 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14174 binding can be done only inside one CU. Such referenced DIE
14175 therefore cannot be even moved to DW_TAG_partial_unit. */
14176 complaint (_("DW_AT_call_parameter offset is not in CU for "
14177 "DW_TAG_call_site child DIE %s [in module %s]"),
14178 sect_offset_str (child_die->sect_off),
14179 objfile_name (objfile));
14180 continue;
14181 }
14182 parameter->u.param_cu_off
14183 = (cu_offset) (sect_off - cu->header.sect_off);
14184 }
14185 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14186 {
14187 complaint (_("No DW_FORM_block* DW_AT_location for "
14188 "DW_TAG_call_site child DIE %s [in module %s]"),
14189 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14190 continue;
14191 }
14192 else
14193 {
14194 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14195 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14196 if (parameter->u.dwarf_reg != -1)
14197 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14198 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14199 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14200 &parameter->u.fb_offset))
14201 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14202 else
14203 {
14204 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14205 "for DW_FORM_block* DW_AT_location is supported for "
14206 "DW_TAG_call_site child DIE %s "
14207 "[in module %s]"),
14208 sect_offset_str (child_die->sect_off),
14209 objfile_name (objfile));
14210 continue;
14211 }
14212 }
14213
14214 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14215 if (attr == NULL)
14216 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14217 if (!attr_form_is_block (attr))
14218 {
14219 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14220 "DW_TAG_call_site child DIE %s [in module %s]"),
14221 sect_offset_str (child_die->sect_off),
14222 objfile_name (objfile));
14223 continue;
14224 }
14225 parameter->value = DW_BLOCK (attr)->data;
14226 parameter->value_size = DW_BLOCK (attr)->size;
14227
14228 /* Parameters are not pre-cleared by memset above. */
14229 parameter->data_value = NULL;
14230 parameter->data_value_size = 0;
14231 call_site->parameter_count++;
14232
14233 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14234 if (attr == NULL)
14235 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14236 if (attr)
14237 {
14238 if (!attr_form_is_block (attr))
14239 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14240 "DW_TAG_call_site child DIE %s [in module %s]"),
14241 sect_offset_str (child_die->sect_off),
14242 objfile_name (objfile));
14243 else
14244 {
14245 parameter->data_value = DW_BLOCK (attr)->data;
14246 parameter->data_value_size = DW_BLOCK (attr)->size;
14247 }
14248 }
14249 }
14250 }
14251
14252 /* Helper function for read_variable. If DIE represents a virtual
14253 table, then return the type of the concrete object that is
14254 associated with the virtual table. Otherwise, return NULL. */
14255
14256 static struct type *
14257 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14258 {
14259 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14260 if (attr == NULL)
14261 return NULL;
14262
14263 /* Find the type DIE. */
14264 struct die_info *type_die = NULL;
14265 struct dwarf2_cu *type_cu = cu;
14266
14267 if (attr_form_is_ref (attr))
14268 type_die = follow_die_ref (die, attr, &type_cu);
14269 if (type_die == NULL)
14270 return NULL;
14271
14272 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14273 return NULL;
14274 return die_containing_type (type_die, type_cu);
14275 }
14276
14277 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14278
14279 static void
14280 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14281 {
14282 struct rust_vtable_symbol *storage = NULL;
14283
14284 if (cu->language == language_rust)
14285 {
14286 struct type *containing_type = rust_containing_type (die, cu);
14287
14288 if (containing_type != NULL)
14289 {
14290 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14291
14292 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14293 struct rust_vtable_symbol);
14294 initialize_objfile_symbol (storage);
14295 storage->concrete_type = containing_type;
14296 storage->subclass = SYMBOL_RUST_VTABLE;
14297 }
14298 }
14299
14300 struct symbol *res = new_symbol (die, NULL, cu, storage);
14301 struct attribute *abstract_origin
14302 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14303 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14304 if (res == NULL && loc && abstract_origin)
14305 {
14306 /* We have a variable without a name, but with a location and an abstract
14307 origin. This may be a concrete instance of an abstract variable
14308 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14309 later. */
14310 struct dwarf2_cu *origin_cu = cu;
14311 struct die_info *origin_die
14312 = follow_die_ref (die, abstract_origin, &origin_cu);
14313 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14314 dpo->abstract_to_concrete[origin_die].push_back (die);
14315 }
14316 }
14317
14318 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14319 reading .debug_rnglists.
14320 Callback's type should be:
14321 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14322 Return true if the attributes are present and valid, otherwise,
14323 return false. */
14324
14325 template <typename Callback>
14326 static bool
14327 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14328 Callback &&callback)
14329 {
14330 struct dwarf2_per_objfile *dwarf2_per_objfile
14331 = cu->per_cu->dwarf2_per_objfile;
14332 struct objfile *objfile = dwarf2_per_objfile->objfile;
14333 bfd *obfd = objfile->obfd;
14334 /* Base address selection entry. */
14335 CORE_ADDR base;
14336 int found_base;
14337 const gdb_byte *buffer;
14338 CORE_ADDR baseaddr;
14339 bool overflow = false;
14340
14341 found_base = cu->base_known;
14342 base = cu->base_address;
14343
14344 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14345 if (offset >= dwarf2_per_objfile->rnglists.size)
14346 {
14347 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14348 offset);
14349 return false;
14350 }
14351 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14352
14353 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14354
14355 while (1)
14356 {
14357 /* Initialize it due to a false compiler warning. */
14358 CORE_ADDR range_beginning = 0, range_end = 0;
14359 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14360 + dwarf2_per_objfile->rnglists.size);
14361 unsigned int bytes_read;
14362
14363 if (buffer == buf_end)
14364 {
14365 overflow = true;
14366 break;
14367 }
14368 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14369 switch (rlet)
14370 {
14371 case DW_RLE_end_of_list:
14372 break;
14373 case DW_RLE_base_address:
14374 if (buffer + cu->header.addr_size > buf_end)
14375 {
14376 overflow = true;
14377 break;
14378 }
14379 base = read_address (obfd, buffer, cu, &bytes_read);
14380 found_base = 1;
14381 buffer += bytes_read;
14382 break;
14383 case DW_RLE_start_length:
14384 if (buffer + cu->header.addr_size > buf_end)
14385 {
14386 overflow = true;
14387 break;
14388 }
14389 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14390 buffer += bytes_read;
14391 range_end = (range_beginning
14392 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14393 buffer += bytes_read;
14394 if (buffer > buf_end)
14395 {
14396 overflow = true;
14397 break;
14398 }
14399 break;
14400 case DW_RLE_offset_pair:
14401 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14402 buffer += bytes_read;
14403 if (buffer > buf_end)
14404 {
14405 overflow = true;
14406 break;
14407 }
14408 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14409 buffer += bytes_read;
14410 if (buffer > buf_end)
14411 {
14412 overflow = true;
14413 break;
14414 }
14415 break;
14416 case DW_RLE_start_end:
14417 if (buffer + 2 * cu->header.addr_size > buf_end)
14418 {
14419 overflow = true;
14420 break;
14421 }
14422 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14423 buffer += bytes_read;
14424 range_end = read_address (obfd, buffer, cu, &bytes_read);
14425 buffer += bytes_read;
14426 break;
14427 default:
14428 complaint (_("Invalid .debug_rnglists data (no base address)"));
14429 return false;
14430 }
14431 if (rlet == DW_RLE_end_of_list || overflow)
14432 break;
14433 if (rlet == DW_RLE_base_address)
14434 continue;
14435
14436 if (!found_base)
14437 {
14438 /* We have no valid base address for the ranges
14439 data. */
14440 complaint (_("Invalid .debug_rnglists data (no base address)"));
14441 return false;
14442 }
14443
14444 if (range_beginning > range_end)
14445 {
14446 /* Inverted range entries are invalid. */
14447 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14448 return false;
14449 }
14450
14451 /* Empty range entries have no effect. */
14452 if (range_beginning == range_end)
14453 continue;
14454
14455 range_beginning += base;
14456 range_end += base;
14457
14458 /* A not-uncommon case of bad debug info.
14459 Don't pollute the addrmap with bad data. */
14460 if (range_beginning + baseaddr == 0
14461 && !dwarf2_per_objfile->has_section_at_zero)
14462 {
14463 complaint (_(".debug_rnglists entry has start address of zero"
14464 " [in module %s]"), objfile_name (objfile));
14465 continue;
14466 }
14467
14468 callback (range_beginning, range_end);
14469 }
14470
14471 if (overflow)
14472 {
14473 complaint (_("Offset %d is not terminated "
14474 "for DW_AT_ranges attribute"),
14475 offset);
14476 return false;
14477 }
14478
14479 return true;
14480 }
14481
14482 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14483 Callback's type should be:
14484 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14485 Return 1 if the attributes are present and valid, otherwise, return 0. */
14486
14487 template <typename Callback>
14488 static int
14489 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14490 Callback &&callback)
14491 {
14492 struct dwarf2_per_objfile *dwarf2_per_objfile
14493 = cu->per_cu->dwarf2_per_objfile;
14494 struct objfile *objfile = dwarf2_per_objfile->objfile;
14495 struct comp_unit_head *cu_header = &cu->header;
14496 bfd *obfd = objfile->obfd;
14497 unsigned int addr_size = cu_header->addr_size;
14498 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14499 /* Base address selection entry. */
14500 CORE_ADDR base;
14501 int found_base;
14502 unsigned int dummy;
14503 const gdb_byte *buffer;
14504 CORE_ADDR baseaddr;
14505
14506 if (cu_header->version >= 5)
14507 return dwarf2_rnglists_process (offset, cu, callback);
14508
14509 found_base = cu->base_known;
14510 base = cu->base_address;
14511
14512 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14513 if (offset >= dwarf2_per_objfile->ranges.size)
14514 {
14515 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14516 offset);
14517 return 0;
14518 }
14519 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14520
14521 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14522
14523 while (1)
14524 {
14525 CORE_ADDR range_beginning, range_end;
14526
14527 range_beginning = read_address (obfd, buffer, cu, &dummy);
14528 buffer += addr_size;
14529 range_end = read_address (obfd, buffer, cu, &dummy);
14530 buffer += addr_size;
14531 offset += 2 * addr_size;
14532
14533 /* An end of list marker is a pair of zero addresses. */
14534 if (range_beginning == 0 && range_end == 0)
14535 /* Found the end of list entry. */
14536 break;
14537
14538 /* Each base address selection entry is a pair of 2 values.
14539 The first is the largest possible address, the second is
14540 the base address. Check for a base address here. */
14541 if ((range_beginning & mask) == mask)
14542 {
14543 /* If we found the largest possible address, then we already
14544 have the base address in range_end. */
14545 base = range_end;
14546 found_base = 1;
14547 continue;
14548 }
14549
14550 if (!found_base)
14551 {
14552 /* We have no valid base address for the ranges
14553 data. */
14554 complaint (_("Invalid .debug_ranges data (no base address)"));
14555 return 0;
14556 }
14557
14558 if (range_beginning > range_end)
14559 {
14560 /* Inverted range entries are invalid. */
14561 complaint (_("Invalid .debug_ranges data (inverted range)"));
14562 return 0;
14563 }
14564
14565 /* Empty range entries have no effect. */
14566 if (range_beginning == range_end)
14567 continue;
14568
14569 range_beginning += base;
14570 range_end += base;
14571
14572 /* A not-uncommon case of bad debug info.
14573 Don't pollute the addrmap with bad data. */
14574 if (range_beginning + baseaddr == 0
14575 && !dwarf2_per_objfile->has_section_at_zero)
14576 {
14577 complaint (_(".debug_ranges entry has start address of zero"
14578 " [in module %s]"), objfile_name (objfile));
14579 continue;
14580 }
14581
14582 callback (range_beginning, range_end);
14583 }
14584
14585 return 1;
14586 }
14587
14588 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14589 Return 1 if the attributes are present and valid, otherwise, return 0.
14590 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14591
14592 static int
14593 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14594 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14595 struct partial_symtab *ranges_pst)
14596 {
14597 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14598 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14599 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14600 SECT_OFF_TEXT (objfile));
14601 int low_set = 0;
14602 CORE_ADDR low = 0;
14603 CORE_ADDR high = 0;
14604 int retval;
14605
14606 retval = dwarf2_ranges_process (offset, cu,
14607 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14608 {
14609 if (ranges_pst != NULL)
14610 {
14611 CORE_ADDR lowpc;
14612 CORE_ADDR highpc;
14613
14614 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14615 range_beginning + baseaddr)
14616 - baseaddr);
14617 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14618 range_end + baseaddr)
14619 - baseaddr);
14620 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14621 lowpc, highpc - 1, ranges_pst);
14622 }
14623
14624 /* FIXME: This is recording everything as a low-high
14625 segment of consecutive addresses. We should have a
14626 data structure for discontiguous block ranges
14627 instead. */
14628 if (! low_set)
14629 {
14630 low = range_beginning;
14631 high = range_end;
14632 low_set = 1;
14633 }
14634 else
14635 {
14636 if (range_beginning < low)
14637 low = range_beginning;
14638 if (range_end > high)
14639 high = range_end;
14640 }
14641 });
14642 if (!retval)
14643 return 0;
14644
14645 if (! low_set)
14646 /* If the first entry is an end-of-list marker, the range
14647 describes an empty scope, i.e. no instructions. */
14648 return 0;
14649
14650 if (low_return)
14651 *low_return = low;
14652 if (high_return)
14653 *high_return = high;
14654 return 1;
14655 }
14656
14657 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14658 definition for the return value. *LOWPC and *HIGHPC are set iff
14659 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14660
14661 static enum pc_bounds_kind
14662 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14663 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14664 struct partial_symtab *pst)
14665 {
14666 struct dwarf2_per_objfile *dwarf2_per_objfile
14667 = cu->per_cu->dwarf2_per_objfile;
14668 struct attribute *attr;
14669 struct attribute *attr_high;
14670 CORE_ADDR low = 0;
14671 CORE_ADDR high = 0;
14672 enum pc_bounds_kind ret;
14673
14674 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14675 if (attr_high)
14676 {
14677 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14678 if (attr)
14679 {
14680 low = attr_value_as_address (attr);
14681 high = attr_value_as_address (attr_high);
14682 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14683 high += low;
14684 }
14685 else
14686 /* Found high w/o low attribute. */
14687 return PC_BOUNDS_INVALID;
14688
14689 /* Found consecutive range of addresses. */
14690 ret = PC_BOUNDS_HIGH_LOW;
14691 }
14692 else
14693 {
14694 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14695 if (attr != NULL)
14696 {
14697 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14698 We take advantage of the fact that DW_AT_ranges does not appear
14699 in DW_TAG_compile_unit of DWO files. */
14700 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14701 unsigned int ranges_offset = (DW_UNSND (attr)
14702 + (need_ranges_base
14703 ? cu->ranges_base
14704 : 0));
14705
14706 /* Value of the DW_AT_ranges attribute is the offset in the
14707 .debug_ranges section. */
14708 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14709 return PC_BOUNDS_INVALID;
14710 /* Found discontinuous range of addresses. */
14711 ret = PC_BOUNDS_RANGES;
14712 }
14713 else
14714 return PC_BOUNDS_NOT_PRESENT;
14715 }
14716
14717 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14718 if (high <= low)
14719 return PC_BOUNDS_INVALID;
14720
14721 /* When using the GNU linker, .gnu.linkonce. sections are used to
14722 eliminate duplicate copies of functions and vtables and such.
14723 The linker will arbitrarily choose one and discard the others.
14724 The AT_*_pc values for such functions refer to local labels in
14725 these sections. If the section from that file was discarded, the
14726 labels are not in the output, so the relocs get a value of 0.
14727 If this is a discarded function, mark the pc bounds as invalid,
14728 so that GDB will ignore it. */
14729 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14730 return PC_BOUNDS_INVALID;
14731
14732 *lowpc = low;
14733 if (highpc)
14734 *highpc = high;
14735 return ret;
14736 }
14737
14738 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14739 its low and high PC addresses. Do nothing if these addresses could not
14740 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14741 and HIGHPC to the high address if greater than HIGHPC. */
14742
14743 static void
14744 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14745 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14746 struct dwarf2_cu *cu)
14747 {
14748 CORE_ADDR low, high;
14749 struct die_info *child = die->child;
14750
14751 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14752 {
14753 *lowpc = std::min (*lowpc, low);
14754 *highpc = std::max (*highpc, high);
14755 }
14756
14757 /* If the language does not allow nested subprograms (either inside
14758 subprograms or lexical blocks), we're done. */
14759 if (cu->language != language_ada)
14760 return;
14761
14762 /* Check all the children of the given DIE. If it contains nested
14763 subprograms, then check their pc bounds. Likewise, we need to
14764 check lexical blocks as well, as they may also contain subprogram
14765 definitions. */
14766 while (child && child->tag)
14767 {
14768 if (child->tag == DW_TAG_subprogram
14769 || child->tag == DW_TAG_lexical_block)
14770 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14771 child = sibling_die (child);
14772 }
14773 }
14774
14775 /* Get the low and high pc's represented by the scope DIE, and store
14776 them in *LOWPC and *HIGHPC. If the correct values can't be
14777 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14778
14779 static void
14780 get_scope_pc_bounds (struct die_info *die,
14781 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14782 struct dwarf2_cu *cu)
14783 {
14784 CORE_ADDR best_low = (CORE_ADDR) -1;
14785 CORE_ADDR best_high = (CORE_ADDR) 0;
14786 CORE_ADDR current_low, current_high;
14787
14788 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14789 >= PC_BOUNDS_RANGES)
14790 {
14791 best_low = current_low;
14792 best_high = current_high;
14793 }
14794 else
14795 {
14796 struct die_info *child = die->child;
14797
14798 while (child && child->tag)
14799 {
14800 switch (child->tag) {
14801 case DW_TAG_subprogram:
14802 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14803 break;
14804 case DW_TAG_namespace:
14805 case DW_TAG_module:
14806 /* FIXME: carlton/2004-01-16: Should we do this for
14807 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14808 that current GCC's always emit the DIEs corresponding
14809 to definitions of methods of classes as children of a
14810 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14811 the DIEs giving the declarations, which could be
14812 anywhere). But I don't see any reason why the
14813 standards says that they have to be there. */
14814 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14815
14816 if (current_low != ((CORE_ADDR) -1))
14817 {
14818 best_low = std::min (best_low, current_low);
14819 best_high = std::max (best_high, current_high);
14820 }
14821 break;
14822 default:
14823 /* Ignore. */
14824 break;
14825 }
14826
14827 child = sibling_die (child);
14828 }
14829 }
14830
14831 *lowpc = best_low;
14832 *highpc = best_high;
14833 }
14834
14835 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14836 in DIE. */
14837
14838 static void
14839 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14840 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14841 {
14842 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14843 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14844 struct attribute *attr;
14845 struct attribute *attr_high;
14846
14847 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14848 if (attr_high)
14849 {
14850 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14851 if (attr)
14852 {
14853 CORE_ADDR low = attr_value_as_address (attr);
14854 CORE_ADDR high = attr_value_as_address (attr_high);
14855
14856 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14857 high += low;
14858
14859 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14860 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14861 cu->get_builder ()->record_block_range (block, low, high - 1);
14862 }
14863 }
14864
14865 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14866 if (attr)
14867 {
14868 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14869 We take advantage of the fact that DW_AT_ranges does not appear
14870 in DW_TAG_compile_unit of DWO files. */
14871 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14872
14873 /* The value of the DW_AT_ranges attribute is the offset of the
14874 address range list in the .debug_ranges section. */
14875 unsigned long offset = (DW_UNSND (attr)
14876 + (need_ranges_base ? cu->ranges_base : 0));
14877
14878 std::vector<blockrange> blockvec;
14879 dwarf2_ranges_process (offset, cu,
14880 [&] (CORE_ADDR start, CORE_ADDR end)
14881 {
14882 start += baseaddr;
14883 end += baseaddr;
14884 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14885 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14886 cu->get_builder ()->record_block_range (block, start, end - 1);
14887 blockvec.emplace_back (start, end);
14888 });
14889
14890 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14891 }
14892 }
14893
14894 /* Check whether the producer field indicates either of GCC < 4.6, or the
14895 Intel C/C++ compiler, and cache the result in CU. */
14896
14897 static void
14898 check_producer (struct dwarf2_cu *cu)
14899 {
14900 int major, minor;
14901
14902 if (cu->producer == NULL)
14903 {
14904 /* For unknown compilers expect their behavior is DWARF version
14905 compliant.
14906
14907 GCC started to support .debug_types sections by -gdwarf-4 since
14908 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14909 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14910 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14911 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14912 }
14913 else if (producer_is_gcc (cu->producer, &major, &minor))
14914 {
14915 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14916 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14917 }
14918 else if (producer_is_icc (cu->producer, &major, &minor))
14919 {
14920 cu->producer_is_icc = true;
14921 cu->producer_is_icc_lt_14 = major < 14;
14922 }
14923 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14924 cu->producer_is_codewarrior = true;
14925 else
14926 {
14927 /* For other non-GCC compilers, expect their behavior is DWARF version
14928 compliant. */
14929 }
14930
14931 cu->checked_producer = true;
14932 }
14933
14934 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14935 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14936 during 4.6.0 experimental. */
14937
14938 static bool
14939 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14940 {
14941 if (!cu->checked_producer)
14942 check_producer (cu);
14943
14944 return cu->producer_is_gxx_lt_4_6;
14945 }
14946
14947
14948 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14949 with incorrect is_stmt attributes. */
14950
14951 static bool
14952 producer_is_codewarrior (struct dwarf2_cu *cu)
14953 {
14954 if (!cu->checked_producer)
14955 check_producer (cu);
14956
14957 return cu->producer_is_codewarrior;
14958 }
14959
14960 /* Return the default accessibility type if it is not overriden by
14961 DW_AT_accessibility. */
14962
14963 static enum dwarf_access_attribute
14964 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14965 {
14966 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14967 {
14968 /* The default DWARF 2 accessibility for members is public, the default
14969 accessibility for inheritance is private. */
14970
14971 if (die->tag != DW_TAG_inheritance)
14972 return DW_ACCESS_public;
14973 else
14974 return DW_ACCESS_private;
14975 }
14976 else
14977 {
14978 /* DWARF 3+ defines the default accessibility a different way. The same
14979 rules apply now for DW_TAG_inheritance as for the members and it only
14980 depends on the container kind. */
14981
14982 if (die->parent->tag == DW_TAG_class_type)
14983 return DW_ACCESS_private;
14984 else
14985 return DW_ACCESS_public;
14986 }
14987 }
14988
14989 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14990 offset. If the attribute was not found return 0, otherwise return
14991 1. If it was found but could not properly be handled, set *OFFSET
14992 to 0. */
14993
14994 static int
14995 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14996 LONGEST *offset)
14997 {
14998 struct attribute *attr;
14999
15000 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15001 if (attr != NULL)
15002 {
15003 *offset = 0;
15004
15005 /* Note that we do not check for a section offset first here.
15006 This is because DW_AT_data_member_location is new in DWARF 4,
15007 so if we see it, we can assume that a constant form is really
15008 a constant and not a section offset. */
15009 if (attr_form_is_constant (attr))
15010 *offset = dwarf2_get_attr_constant_value (attr, 0);
15011 else if (attr_form_is_section_offset (attr))
15012 dwarf2_complex_location_expr_complaint ();
15013 else if (attr_form_is_block (attr))
15014 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15015 else
15016 dwarf2_complex_location_expr_complaint ();
15017
15018 return 1;
15019 }
15020
15021 return 0;
15022 }
15023
15024 /* Add an aggregate field to the field list. */
15025
15026 static void
15027 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15028 struct dwarf2_cu *cu)
15029 {
15030 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15031 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15032 struct nextfield *new_field;
15033 struct attribute *attr;
15034 struct field *fp;
15035 const char *fieldname = "";
15036
15037 if (die->tag == DW_TAG_inheritance)
15038 {
15039 fip->baseclasses.emplace_back ();
15040 new_field = &fip->baseclasses.back ();
15041 }
15042 else
15043 {
15044 fip->fields.emplace_back ();
15045 new_field = &fip->fields.back ();
15046 }
15047
15048 fip->nfields++;
15049
15050 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15051 if (attr)
15052 new_field->accessibility = DW_UNSND (attr);
15053 else
15054 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15055 if (new_field->accessibility != DW_ACCESS_public)
15056 fip->non_public_fields = 1;
15057
15058 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15059 if (attr)
15060 new_field->virtuality = DW_UNSND (attr);
15061 else
15062 new_field->virtuality = DW_VIRTUALITY_none;
15063
15064 fp = &new_field->field;
15065
15066 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15067 {
15068 LONGEST offset;
15069
15070 /* Data member other than a C++ static data member. */
15071
15072 /* Get type of field. */
15073 fp->type = die_type (die, cu);
15074
15075 SET_FIELD_BITPOS (*fp, 0);
15076
15077 /* Get bit size of field (zero if none). */
15078 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15079 if (attr)
15080 {
15081 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15082 }
15083 else
15084 {
15085 FIELD_BITSIZE (*fp) = 0;
15086 }
15087
15088 /* Get bit offset of field. */
15089 if (handle_data_member_location (die, cu, &offset))
15090 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15091 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15092 if (attr)
15093 {
15094 if (gdbarch_bits_big_endian (gdbarch))
15095 {
15096 /* For big endian bits, the DW_AT_bit_offset gives the
15097 additional bit offset from the MSB of the containing
15098 anonymous object to the MSB of the field. We don't
15099 have to do anything special since we don't need to
15100 know the size of the anonymous object. */
15101 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15102 }
15103 else
15104 {
15105 /* For little endian bits, compute the bit offset to the
15106 MSB of the anonymous object, subtract off the number of
15107 bits from the MSB of the field to the MSB of the
15108 object, and then subtract off the number of bits of
15109 the field itself. The result is the bit offset of
15110 the LSB of the field. */
15111 int anonymous_size;
15112 int bit_offset = DW_UNSND (attr);
15113
15114 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15115 if (attr)
15116 {
15117 /* The size of the anonymous object containing
15118 the bit field is explicit, so use the
15119 indicated size (in bytes). */
15120 anonymous_size = DW_UNSND (attr);
15121 }
15122 else
15123 {
15124 /* The size of the anonymous object containing
15125 the bit field must be inferred from the type
15126 attribute of the data member containing the
15127 bit field. */
15128 anonymous_size = TYPE_LENGTH (fp->type);
15129 }
15130 SET_FIELD_BITPOS (*fp,
15131 (FIELD_BITPOS (*fp)
15132 + anonymous_size * bits_per_byte
15133 - bit_offset - FIELD_BITSIZE (*fp)));
15134 }
15135 }
15136 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15137 if (attr != NULL)
15138 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15139 + dwarf2_get_attr_constant_value (attr, 0)));
15140
15141 /* Get name of field. */
15142 fieldname = dwarf2_name (die, cu);
15143 if (fieldname == NULL)
15144 fieldname = "";
15145
15146 /* The name is already allocated along with this objfile, so we don't
15147 need to duplicate it for the type. */
15148 fp->name = fieldname;
15149
15150 /* Change accessibility for artificial fields (e.g. virtual table
15151 pointer or virtual base class pointer) to private. */
15152 if (dwarf2_attr (die, DW_AT_artificial, cu))
15153 {
15154 FIELD_ARTIFICIAL (*fp) = 1;
15155 new_field->accessibility = DW_ACCESS_private;
15156 fip->non_public_fields = 1;
15157 }
15158 }
15159 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15160 {
15161 /* C++ static member. */
15162
15163 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15164 is a declaration, but all versions of G++ as of this writing
15165 (so through at least 3.2.1) incorrectly generate
15166 DW_TAG_variable tags. */
15167
15168 const char *physname;
15169
15170 /* Get name of field. */
15171 fieldname = dwarf2_name (die, cu);
15172 if (fieldname == NULL)
15173 return;
15174
15175 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15176 if (attr
15177 /* Only create a symbol if this is an external value.
15178 new_symbol checks this and puts the value in the global symbol
15179 table, which we want. If it is not external, new_symbol
15180 will try to put the value in cu->list_in_scope which is wrong. */
15181 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15182 {
15183 /* A static const member, not much different than an enum as far as
15184 we're concerned, except that we can support more types. */
15185 new_symbol (die, NULL, cu);
15186 }
15187
15188 /* Get physical name. */
15189 physname = dwarf2_physname (fieldname, die, cu);
15190
15191 /* The name is already allocated along with this objfile, so we don't
15192 need to duplicate it for the type. */
15193 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15194 FIELD_TYPE (*fp) = die_type (die, cu);
15195 FIELD_NAME (*fp) = fieldname;
15196 }
15197 else if (die->tag == DW_TAG_inheritance)
15198 {
15199 LONGEST offset;
15200
15201 /* C++ base class field. */
15202 if (handle_data_member_location (die, cu, &offset))
15203 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15204 FIELD_BITSIZE (*fp) = 0;
15205 FIELD_TYPE (*fp) = die_type (die, cu);
15206 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15207 }
15208 else if (die->tag == DW_TAG_variant_part)
15209 {
15210 /* process_structure_scope will treat this DIE as a union. */
15211 process_structure_scope (die, cu);
15212
15213 /* The variant part is relative to the start of the enclosing
15214 structure. */
15215 SET_FIELD_BITPOS (*fp, 0);
15216 fp->type = get_die_type (die, cu);
15217 fp->artificial = 1;
15218 fp->name = "<<variant>>";
15219
15220 /* Normally a DW_TAG_variant_part won't have a size, but our
15221 representation requires one, so set it to the maximum of the
15222 child sizes. */
15223 if (TYPE_LENGTH (fp->type) == 0)
15224 {
15225 unsigned max = 0;
15226 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15227 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15228 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15229 TYPE_LENGTH (fp->type) = max;
15230 }
15231 }
15232 else
15233 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15234 }
15235
15236 /* Can the type given by DIE define another type? */
15237
15238 static bool
15239 type_can_define_types (const struct die_info *die)
15240 {
15241 switch (die->tag)
15242 {
15243 case DW_TAG_typedef:
15244 case DW_TAG_class_type:
15245 case DW_TAG_structure_type:
15246 case DW_TAG_union_type:
15247 case DW_TAG_enumeration_type:
15248 return true;
15249
15250 default:
15251 return false;
15252 }
15253 }
15254
15255 /* Add a type definition defined in the scope of the FIP's class. */
15256
15257 static void
15258 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15259 struct dwarf2_cu *cu)
15260 {
15261 struct decl_field fp;
15262 memset (&fp, 0, sizeof (fp));
15263
15264 gdb_assert (type_can_define_types (die));
15265
15266 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15267 fp.name = dwarf2_name (die, cu);
15268 fp.type = read_type_die (die, cu);
15269
15270 /* Save accessibility. */
15271 enum dwarf_access_attribute accessibility;
15272 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15273 if (attr != NULL)
15274 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15275 else
15276 accessibility = dwarf2_default_access_attribute (die, cu);
15277 switch (accessibility)
15278 {
15279 case DW_ACCESS_public:
15280 /* The assumed value if neither private nor protected. */
15281 break;
15282 case DW_ACCESS_private:
15283 fp.is_private = 1;
15284 break;
15285 case DW_ACCESS_protected:
15286 fp.is_protected = 1;
15287 break;
15288 default:
15289 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15290 }
15291
15292 if (die->tag == DW_TAG_typedef)
15293 fip->typedef_field_list.push_back (fp);
15294 else
15295 fip->nested_types_list.push_back (fp);
15296 }
15297
15298 /* Create the vector of fields, and attach it to the type. */
15299
15300 static void
15301 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15302 struct dwarf2_cu *cu)
15303 {
15304 int nfields = fip->nfields;
15305
15306 /* Record the field count, allocate space for the array of fields,
15307 and create blank accessibility bitfields if necessary. */
15308 TYPE_NFIELDS (type) = nfields;
15309 TYPE_FIELDS (type) = (struct field *)
15310 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15311
15312 if (fip->non_public_fields && cu->language != language_ada)
15313 {
15314 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15315
15316 TYPE_FIELD_PRIVATE_BITS (type) =
15317 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15318 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15319
15320 TYPE_FIELD_PROTECTED_BITS (type) =
15321 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15322 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15323
15324 TYPE_FIELD_IGNORE_BITS (type) =
15325 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15326 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15327 }
15328
15329 /* If the type has baseclasses, allocate and clear a bit vector for
15330 TYPE_FIELD_VIRTUAL_BITS. */
15331 if (!fip->baseclasses.empty () && cu->language != language_ada)
15332 {
15333 int num_bytes = B_BYTES (fip->baseclasses.size ());
15334 unsigned char *pointer;
15335
15336 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15337 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15338 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15339 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15340 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15341 }
15342
15343 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15344 {
15345 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15346
15347 for (int index = 0; index < nfields; ++index)
15348 {
15349 struct nextfield &field = fip->fields[index];
15350
15351 if (field.variant.is_discriminant)
15352 di->discriminant_index = index;
15353 else if (field.variant.default_branch)
15354 di->default_index = index;
15355 else
15356 di->discriminants[index] = field.variant.discriminant_value;
15357 }
15358 }
15359
15360 /* Copy the saved-up fields into the field vector. */
15361 for (int i = 0; i < nfields; ++i)
15362 {
15363 struct nextfield &field
15364 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15365 : fip->fields[i - fip->baseclasses.size ()]);
15366
15367 TYPE_FIELD (type, i) = field.field;
15368 switch (field.accessibility)
15369 {
15370 case DW_ACCESS_private:
15371 if (cu->language != language_ada)
15372 SET_TYPE_FIELD_PRIVATE (type, i);
15373 break;
15374
15375 case DW_ACCESS_protected:
15376 if (cu->language != language_ada)
15377 SET_TYPE_FIELD_PROTECTED (type, i);
15378 break;
15379
15380 case DW_ACCESS_public:
15381 break;
15382
15383 default:
15384 /* Unknown accessibility. Complain and treat it as public. */
15385 {
15386 complaint (_("unsupported accessibility %d"),
15387 field.accessibility);
15388 }
15389 break;
15390 }
15391 if (i < fip->baseclasses.size ())
15392 {
15393 switch (field.virtuality)
15394 {
15395 case DW_VIRTUALITY_virtual:
15396 case DW_VIRTUALITY_pure_virtual:
15397 if (cu->language == language_ada)
15398 error (_("unexpected virtuality in component of Ada type"));
15399 SET_TYPE_FIELD_VIRTUAL (type, i);
15400 break;
15401 }
15402 }
15403 }
15404 }
15405
15406 /* Return true if this member function is a constructor, false
15407 otherwise. */
15408
15409 static int
15410 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15411 {
15412 const char *fieldname;
15413 const char *type_name;
15414 int len;
15415
15416 if (die->parent == NULL)
15417 return 0;
15418
15419 if (die->parent->tag != DW_TAG_structure_type
15420 && die->parent->tag != DW_TAG_union_type
15421 && die->parent->tag != DW_TAG_class_type)
15422 return 0;
15423
15424 fieldname = dwarf2_name (die, cu);
15425 type_name = dwarf2_name (die->parent, cu);
15426 if (fieldname == NULL || type_name == NULL)
15427 return 0;
15428
15429 len = strlen (fieldname);
15430 return (strncmp (fieldname, type_name, len) == 0
15431 && (type_name[len] == '\0' || type_name[len] == '<'));
15432 }
15433
15434 /* Add a member function to the proper fieldlist. */
15435
15436 static void
15437 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15438 struct type *type, struct dwarf2_cu *cu)
15439 {
15440 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15441 struct attribute *attr;
15442 int i;
15443 struct fnfieldlist *flp = nullptr;
15444 struct fn_field *fnp;
15445 const char *fieldname;
15446 struct type *this_type;
15447 enum dwarf_access_attribute accessibility;
15448
15449 if (cu->language == language_ada)
15450 error (_("unexpected member function in Ada type"));
15451
15452 /* Get name of member function. */
15453 fieldname = dwarf2_name (die, cu);
15454 if (fieldname == NULL)
15455 return;
15456
15457 /* Look up member function name in fieldlist. */
15458 for (i = 0; i < fip->fnfieldlists.size (); i++)
15459 {
15460 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15461 {
15462 flp = &fip->fnfieldlists[i];
15463 break;
15464 }
15465 }
15466
15467 /* Create a new fnfieldlist if necessary. */
15468 if (flp == nullptr)
15469 {
15470 fip->fnfieldlists.emplace_back ();
15471 flp = &fip->fnfieldlists.back ();
15472 flp->name = fieldname;
15473 i = fip->fnfieldlists.size () - 1;
15474 }
15475
15476 /* Create a new member function field and add it to the vector of
15477 fnfieldlists. */
15478 flp->fnfields.emplace_back ();
15479 fnp = &flp->fnfields.back ();
15480
15481 /* Delay processing of the physname until later. */
15482 if (cu->language == language_cplus)
15483 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15484 die, cu);
15485 else
15486 {
15487 const char *physname = dwarf2_physname (fieldname, die, cu);
15488 fnp->physname = physname ? physname : "";
15489 }
15490
15491 fnp->type = alloc_type (objfile);
15492 this_type = read_type_die (die, cu);
15493 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15494 {
15495 int nparams = TYPE_NFIELDS (this_type);
15496
15497 /* TYPE is the domain of this method, and THIS_TYPE is the type
15498 of the method itself (TYPE_CODE_METHOD). */
15499 smash_to_method_type (fnp->type, type,
15500 TYPE_TARGET_TYPE (this_type),
15501 TYPE_FIELDS (this_type),
15502 TYPE_NFIELDS (this_type),
15503 TYPE_VARARGS (this_type));
15504
15505 /* Handle static member functions.
15506 Dwarf2 has no clean way to discern C++ static and non-static
15507 member functions. G++ helps GDB by marking the first
15508 parameter for non-static member functions (which is the this
15509 pointer) as artificial. We obtain this information from
15510 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15511 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15512 fnp->voffset = VOFFSET_STATIC;
15513 }
15514 else
15515 complaint (_("member function type missing for '%s'"),
15516 dwarf2_full_name (fieldname, die, cu));
15517
15518 /* Get fcontext from DW_AT_containing_type if present. */
15519 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15520 fnp->fcontext = die_containing_type (die, cu);
15521
15522 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15523 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15524
15525 /* Get accessibility. */
15526 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15527 if (attr)
15528 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15529 else
15530 accessibility = dwarf2_default_access_attribute (die, cu);
15531 switch (accessibility)
15532 {
15533 case DW_ACCESS_private:
15534 fnp->is_private = 1;
15535 break;
15536 case DW_ACCESS_protected:
15537 fnp->is_protected = 1;
15538 break;
15539 }
15540
15541 /* Check for artificial methods. */
15542 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15543 if (attr && DW_UNSND (attr) != 0)
15544 fnp->is_artificial = 1;
15545
15546 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15547
15548 /* Get index in virtual function table if it is a virtual member
15549 function. For older versions of GCC, this is an offset in the
15550 appropriate virtual table, as specified by DW_AT_containing_type.
15551 For everyone else, it is an expression to be evaluated relative
15552 to the object address. */
15553
15554 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15555 if (attr)
15556 {
15557 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15558 {
15559 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15560 {
15561 /* Old-style GCC. */
15562 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15563 }
15564 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15565 || (DW_BLOCK (attr)->size > 1
15566 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15567 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15568 {
15569 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15570 if ((fnp->voffset % cu->header.addr_size) != 0)
15571 dwarf2_complex_location_expr_complaint ();
15572 else
15573 fnp->voffset /= cu->header.addr_size;
15574 fnp->voffset += 2;
15575 }
15576 else
15577 dwarf2_complex_location_expr_complaint ();
15578
15579 if (!fnp->fcontext)
15580 {
15581 /* If there is no `this' field and no DW_AT_containing_type,
15582 we cannot actually find a base class context for the
15583 vtable! */
15584 if (TYPE_NFIELDS (this_type) == 0
15585 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15586 {
15587 complaint (_("cannot determine context for virtual member "
15588 "function \"%s\" (offset %s)"),
15589 fieldname, sect_offset_str (die->sect_off));
15590 }
15591 else
15592 {
15593 fnp->fcontext
15594 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15595 }
15596 }
15597 }
15598 else if (attr_form_is_section_offset (attr))
15599 {
15600 dwarf2_complex_location_expr_complaint ();
15601 }
15602 else
15603 {
15604 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15605 fieldname);
15606 }
15607 }
15608 else
15609 {
15610 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15611 if (attr && DW_UNSND (attr))
15612 {
15613 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15614 complaint (_("Member function \"%s\" (offset %s) is virtual "
15615 "but the vtable offset is not specified"),
15616 fieldname, sect_offset_str (die->sect_off));
15617 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15618 TYPE_CPLUS_DYNAMIC (type) = 1;
15619 }
15620 }
15621 }
15622
15623 /* Create the vector of member function fields, and attach it to the type. */
15624
15625 static void
15626 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15627 struct dwarf2_cu *cu)
15628 {
15629 if (cu->language == language_ada)
15630 error (_("unexpected member functions in Ada type"));
15631
15632 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15633 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15634 TYPE_ALLOC (type,
15635 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15636
15637 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15638 {
15639 struct fnfieldlist &nf = fip->fnfieldlists[i];
15640 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15641
15642 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15643 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15644 fn_flp->fn_fields = (struct fn_field *)
15645 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15646
15647 for (int k = 0; k < nf.fnfields.size (); ++k)
15648 fn_flp->fn_fields[k] = nf.fnfields[k];
15649 }
15650
15651 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15652 }
15653
15654 /* Returns non-zero if NAME is the name of a vtable member in CU's
15655 language, zero otherwise. */
15656 static int
15657 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15658 {
15659 static const char vptr[] = "_vptr";
15660
15661 /* Look for the C++ form of the vtable. */
15662 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15663 return 1;
15664
15665 return 0;
15666 }
15667
15668 /* GCC outputs unnamed structures that are really pointers to member
15669 functions, with the ABI-specified layout. If TYPE describes
15670 such a structure, smash it into a member function type.
15671
15672 GCC shouldn't do this; it should just output pointer to member DIEs.
15673 This is GCC PR debug/28767. */
15674
15675 static void
15676 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15677 {
15678 struct type *pfn_type, *self_type, *new_type;
15679
15680 /* Check for a structure with no name and two children. */
15681 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15682 return;
15683
15684 /* Check for __pfn and __delta members. */
15685 if (TYPE_FIELD_NAME (type, 0) == NULL
15686 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15687 || TYPE_FIELD_NAME (type, 1) == NULL
15688 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15689 return;
15690
15691 /* Find the type of the method. */
15692 pfn_type = TYPE_FIELD_TYPE (type, 0);
15693 if (pfn_type == NULL
15694 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15695 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15696 return;
15697
15698 /* Look for the "this" argument. */
15699 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15700 if (TYPE_NFIELDS (pfn_type) == 0
15701 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15702 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15703 return;
15704
15705 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15706 new_type = alloc_type (objfile);
15707 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15708 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15709 TYPE_VARARGS (pfn_type));
15710 smash_to_methodptr_type (type, new_type);
15711 }
15712
15713 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15714 appropriate error checking and issuing complaints if there is a
15715 problem. */
15716
15717 static ULONGEST
15718 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15719 {
15720 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15721
15722 if (attr == nullptr)
15723 return 0;
15724
15725 if (!attr_form_is_constant (attr))
15726 {
15727 complaint (_("DW_AT_alignment must have constant form"
15728 " - DIE at %s [in module %s]"),
15729 sect_offset_str (die->sect_off),
15730 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15731 return 0;
15732 }
15733
15734 ULONGEST align;
15735 if (attr->form == DW_FORM_sdata)
15736 {
15737 LONGEST val = DW_SND (attr);
15738 if (val < 0)
15739 {
15740 complaint (_("DW_AT_alignment value must not be negative"
15741 " - DIE at %s [in module %s]"),
15742 sect_offset_str (die->sect_off),
15743 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15744 return 0;
15745 }
15746 align = val;
15747 }
15748 else
15749 align = DW_UNSND (attr);
15750
15751 if (align == 0)
15752 {
15753 complaint (_("DW_AT_alignment value must not be zero"
15754 " - DIE at %s [in module %s]"),
15755 sect_offset_str (die->sect_off),
15756 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15757 return 0;
15758 }
15759 if ((align & (align - 1)) != 0)
15760 {
15761 complaint (_("DW_AT_alignment value must be a power of 2"
15762 " - DIE at %s [in module %s]"),
15763 sect_offset_str (die->sect_off),
15764 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15765 return 0;
15766 }
15767
15768 return align;
15769 }
15770
15771 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15772 the alignment for TYPE. */
15773
15774 static void
15775 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15776 struct type *type)
15777 {
15778 if (!set_type_align (type, get_alignment (cu, die)))
15779 complaint (_("DW_AT_alignment value too large"
15780 " - DIE at %s [in module %s]"),
15781 sect_offset_str (die->sect_off),
15782 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15783 }
15784
15785 /* Called when we find the DIE that starts a structure or union scope
15786 (definition) to create a type for the structure or union. Fill in
15787 the type's name and general properties; the members will not be
15788 processed until process_structure_scope. A symbol table entry for
15789 the type will also not be done until process_structure_scope (assuming
15790 the type has a name).
15791
15792 NOTE: we need to call these functions regardless of whether or not the
15793 DIE has a DW_AT_name attribute, since it might be an anonymous
15794 structure or union. This gets the type entered into our set of
15795 user defined types. */
15796
15797 static struct type *
15798 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15799 {
15800 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15801 struct type *type;
15802 struct attribute *attr;
15803 const char *name;
15804
15805 /* If the definition of this type lives in .debug_types, read that type.
15806 Don't follow DW_AT_specification though, that will take us back up
15807 the chain and we want to go down. */
15808 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15809 if (attr)
15810 {
15811 type = get_DW_AT_signature_type (die, attr, cu);
15812
15813 /* The type's CU may not be the same as CU.
15814 Ensure TYPE is recorded with CU in die_type_hash. */
15815 return set_die_type (die, type, cu);
15816 }
15817
15818 type = alloc_type (objfile);
15819 INIT_CPLUS_SPECIFIC (type);
15820
15821 name = dwarf2_name (die, cu);
15822 if (name != NULL)
15823 {
15824 if (cu->language == language_cplus
15825 || cu->language == language_d
15826 || cu->language == language_rust)
15827 {
15828 const char *full_name = dwarf2_full_name (name, die, cu);
15829
15830 /* dwarf2_full_name might have already finished building the DIE's
15831 type. If so, there is no need to continue. */
15832 if (get_die_type (die, cu) != NULL)
15833 return get_die_type (die, cu);
15834
15835 TYPE_NAME (type) = full_name;
15836 }
15837 else
15838 {
15839 /* The name is already allocated along with this objfile, so
15840 we don't need to duplicate it for the type. */
15841 TYPE_NAME (type) = name;
15842 }
15843 }
15844
15845 if (die->tag == DW_TAG_structure_type)
15846 {
15847 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15848 }
15849 else if (die->tag == DW_TAG_union_type)
15850 {
15851 TYPE_CODE (type) = TYPE_CODE_UNION;
15852 }
15853 else if (die->tag == DW_TAG_variant_part)
15854 {
15855 TYPE_CODE (type) = TYPE_CODE_UNION;
15856 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15857 }
15858 else
15859 {
15860 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15861 }
15862
15863 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15864 TYPE_DECLARED_CLASS (type) = 1;
15865
15866 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15867 if (attr)
15868 {
15869 if (attr_form_is_constant (attr))
15870 TYPE_LENGTH (type) = DW_UNSND (attr);
15871 else
15872 {
15873 /* For the moment, dynamic type sizes are not supported
15874 by GDB's struct type. The actual size is determined
15875 on-demand when resolving the type of a given object,
15876 so set the type's length to zero for now. Otherwise,
15877 we record an expression as the length, and that expression
15878 could lead to a very large value, which could eventually
15879 lead to us trying to allocate that much memory when creating
15880 a value of that type. */
15881 TYPE_LENGTH (type) = 0;
15882 }
15883 }
15884 else
15885 {
15886 TYPE_LENGTH (type) = 0;
15887 }
15888
15889 maybe_set_alignment (cu, die, type);
15890
15891 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15892 {
15893 /* ICC<14 does not output the required DW_AT_declaration on
15894 incomplete types, but gives them a size of zero. */
15895 TYPE_STUB (type) = 1;
15896 }
15897 else
15898 TYPE_STUB_SUPPORTED (type) = 1;
15899
15900 if (die_is_declaration (die, cu))
15901 TYPE_STUB (type) = 1;
15902 else if (attr == NULL && die->child == NULL
15903 && producer_is_realview (cu->producer))
15904 /* RealView does not output the required DW_AT_declaration
15905 on incomplete types. */
15906 TYPE_STUB (type) = 1;
15907
15908 /* We need to add the type field to the die immediately so we don't
15909 infinitely recurse when dealing with pointers to the structure
15910 type within the structure itself. */
15911 set_die_type (die, type, cu);
15912
15913 /* set_die_type should be already done. */
15914 set_descriptive_type (type, die, cu);
15915
15916 return type;
15917 }
15918
15919 /* A helper for process_structure_scope that handles a single member
15920 DIE. */
15921
15922 static void
15923 handle_struct_member_die (struct die_info *child_die, struct type *type,
15924 struct field_info *fi,
15925 std::vector<struct symbol *> *template_args,
15926 struct dwarf2_cu *cu)
15927 {
15928 if (child_die->tag == DW_TAG_member
15929 || child_die->tag == DW_TAG_variable
15930 || child_die->tag == DW_TAG_variant_part)
15931 {
15932 /* NOTE: carlton/2002-11-05: A C++ static data member
15933 should be a DW_TAG_member that is a declaration, but
15934 all versions of G++ as of this writing (so through at
15935 least 3.2.1) incorrectly generate DW_TAG_variable
15936 tags for them instead. */
15937 dwarf2_add_field (fi, child_die, cu);
15938 }
15939 else if (child_die->tag == DW_TAG_subprogram)
15940 {
15941 /* Rust doesn't have member functions in the C++ sense.
15942 However, it does emit ordinary functions as children
15943 of a struct DIE. */
15944 if (cu->language == language_rust)
15945 read_func_scope (child_die, cu);
15946 else
15947 {
15948 /* C++ member function. */
15949 dwarf2_add_member_fn (fi, child_die, type, cu);
15950 }
15951 }
15952 else if (child_die->tag == DW_TAG_inheritance)
15953 {
15954 /* C++ base class field. */
15955 dwarf2_add_field (fi, child_die, cu);
15956 }
15957 else if (type_can_define_types (child_die))
15958 dwarf2_add_type_defn (fi, child_die, cu);
15959 else if (child_die->tag == DW_TAG_template_type_param
15960 || child_die->tag == DW_TAG_template_value_param)
15961 {
15962 struct symbol *arg = new_symbol (child_die, NULL, cu);
15963
15964 if (arg != NULL)
15965 template_args->push_back (arg);
15966 }
15967 else if (child_die->tag == DW_TAG_variant)
15968 {
15969 /* In a variant we want to get the discriminant and also add a
15970 field for our sole member child. */
15971 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15972
15973 for (struct die_info *variant_child = child_die->child;
15974 variant_child != NULL;
15975 variant_child = sibling_die (variant_child))
15976 {
15977 if (variant_child->tag == DW_TAG_member)
15978 {
15979 handle_struct_member_die (variant_child, type, fi,
15980 template_args, cu);
15981 /* Only handle the one. */
15982 break;
15983 }
15984 }
15985
15986 /* We don't handle this but we might as well report it if we see
15987 it. */
15988 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15989 complaint (_("DW_AT_discr_list is not supported yet"
15990 " - DIE at %s [in module %s]"),
15991 sect_offset_str (child_die->sect_off),
15992 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15993
15994 /* The first field was just added, so we can stash the
15995 discriminant there. */
15996 gdb_assert (!fi->fields.empty ());
15997 if (discr == NULL)
15998 fi->fields.back ().variant.default_branch = true;
15999 else
16000 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16001 }
16002 }
16003
16004 /* Finish creating a structure or union type, including filling in
16005 its members and creating a symbol for it. */
16006
16007 static void
16008 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16009 {
16010 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16011 struct die_info *child_die;
16012 struct type *type;
16013
16014 type = get_die_type (die, cu);
16015 if (type == NULL)
16016 type = read_structure_type (die, cu);
16017
16018 /* When reading a DW_TAG_variant_part, we need to notice when we
16019 read the discriminant member, so we can record it later in the
16020 discriminant_info. */
16021 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16022 sect_offset discr_offset;
16023 bool has_template_parameters = false;
16024
16025 if (is_variant_part)
16026 {
16027 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16028 if (discr == NULL)
16029 {
16030 /* Maybe it's a univariant form, an extension we support.
16031 In this case arrange not to check the offset. */
16032 is_variant_part = false;
16033 }
16034 else if (attr_form_is_ref (discr))
16035 {
16036 struct dwarf2_cu *target_cu = cu;
16037 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16038
16039 discr_offset = target_die->sect_off;
16040 }
16041 else
16042 {
16043 complaint (_("DW_AT_discr does not have DIE reference form"
16044 " - DIE at %s [in module %s]"),
16045 sect_offset_str (die->sect_off),
16046 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16047 is_variant_part = false;
16048 }
16049 }
16050
16051 if (die->child != NULL && ! die_is_declaration (die, cu))
16052 {
16053 struct field_info fi;
16054 std::vector<struct symbol *> template_args;
16055
16056 child_die = die->child;
16057
16058 while (child_die && child_die->tag)
16059 {
16060 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16061
16062 if (is_variant_part && discr_offset == child_die->sect_off)
16063 fi.fields.back ().variant.is_discriminant = true;
16064
16065 child_die = sibling_die (child_die);
16066 }
16067
16068 /* Attach template arguments to type. */
16069 if (!template_args.empty ())
16070 {
16071 has_template_parameters = true;
16072 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16073 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16074 TYPE_TEMPLATE_ARGUMENTS (type)
16075 = XOBNEWVEC (&objfile->objfile_obstack,
16076 struct symbol *,
16077 TYPE_N_TEMPLATE_ARGUMENTS (type));
16078 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16079 template_args.data (),
16080 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16081 * sizeof (struct symbol *)));
16082 }
16083
16084 /* Attach fields and member functions to the type. */
16085 if (fi.nfields)
16086 dwarf2_attach_fields_to_type (&fi, type, cu);
16087 if (!fi.fnfieldlists.empty ())
16088 {
16089 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16090
16091 /* Get the type which refers to the base class (possibly this
16092 class itself) which contains the vtable pointer for the current
16093 class from the DW_AT_containing_type attribute. This use of
16094 DW_AT_containing_type is a GNU extension. */
16095
16096 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16097 {
16098 struct type *t = die_containing_type (die, cu);
16099
16100 set_type_vptr_basetype (type, t);
16101 if (type == t)
16102 {
16103 int i;
16104
16105 /* Our own class provides vtbl ptr. */
16106 for (i = TYPE_NFIELDS (t) - 1;
16107 i >= TYPE_N_BASECLASSES (t);
16108 --i)
16109 {
16110 const char *fieldname = TYPE_FIELD_NAME (t, i);
16111
16112 if (is_vtable_name (fieldname, cu))
16113 {
16114 set_type_vptr_fieldno (type, i);
16115 break;
16116 }
16117 }
16118
16119 /* Complain if virtual function table field not found. */
16120 if (i < TYPE_N_BASECLASSES (t))
16121 complaint (_("virtual function table pointer "
16122 "not found when defining class '%s'"),
16123 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16124 }
16125 else
16126 {
16127 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16128 }
16129 }
16130 else if (cu->producer
16131 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16132 {
16133 /* The IBM XLC compiler does not provide direct indication
16134 of the containing type, but the vtable pointer is
16135 always named __vfp. */
16136
16137 int i;
16138
16139 for (i = TYPE_NFIELDS (type) - 1;
16140 i >= TYPE_N_BASECLASSES (type);
16141 --i)
16142 {
16143 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16144 {
16145 set_type_vptr_fieldno (type, i);
16146 set_type_vptr_basetype (type, type);
16147 break;
16148 }
16149 }
16150 }
16151 }
16152
16153 /* Copy fi.typedef_field_list linked list elements content into the
16154 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16155 if (!fi.typedef_field_list.empty ())
16156 {
16157 int count = fi.typedef_field_list.size ();
16158
16159 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16160 TYPE_TYPEDEF_FIELD_ARRAY (type)
16161 = ((struct decl_field *)
16162 TYPE_ALLOC (type,
16163 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16164 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16165
16166 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16167 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16168 }
16169
16170 /* Copy fi.nested_types_list linked list elements content into the
16171 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16172 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16173 {
16174 int count = fi.nested_types_list.size ();
16175
16176 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16177 TYPE_NESTED_TYPES_ARRAY (type)
16178 = ((struct decl_field *)
16179 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16180 TYPE_NESTED_TYPES_COUNT (type) = count;
16181
16182 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16183 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16184 }
16185 }
16186
16187 quirk_gcc_member_function_pointer (type, objfile);
16188 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16189 cu->rust_unions.push_back (type);
16190
16191 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16192 snapshots) has been known to create a die giving a declaration
16193 for a class that has, as a child, a die giving a definition for a
16194 nested class. So we have to process our children even if the
16195 current die is a declaration. Normally, of course, a declaration
16196 won't have any children at all. */
16197
16198 child_die = die->child;
16199
16200 while (child_die != NULL && child_die->tag)
16201 {
16202 if (child_die->tag == DW_TAG_member
16203 || child_die->tag == DW_TAG_variable
16204 || child_die->tag == DW_TAG_inheritance
16205 || child_die->tag == DW_TAG_template_value_param
16206 || child_die->tag == DW_TAG_template_type_param)
16207 {
16208 /* Do nothing. */
16209 }
16210 else
16211 process_die (child_die, cu);
16212
16213 child_die = sibling_die (child_die);
16214 }
16215
16216 /* Do not consider external references. According to the DWARF standard,
16217 these DIEs are identified by the fact that they have no byte_size
16218 attribute, and a declaration attribute. */
16219 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16220 || !die_is_declaration (die, cu))
16221 {
16222 struct symbol *sym = new_symbol (die, type, cu);
16223
16224 if (has_template_parameters)
16225 {
16226 /* Make sure that the symtab is set on the new symbols.
16227 Even though they don't appear in this symtab directly,
16228 other parts of gdb assume that symbols do, and this is
16229 reasonably true. */
16230 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16231 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i),
16232 symbol_symtab (sym));
16233 }
16234 }
16235 }
16236
16237 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16238 update TYPE using some information only available in DIE's children. */
16239
16240 static void
16241 update_enumeration_type_from_children (struct die_info *die,
16242 struct type *type,
16243 struct dwarf2_cu *cu)
16244 {
16245 struct die_info *child_die;
16246 int unsigned_enum = 1;
16247 int flag_enum = 1;
16248 ULONGEST mask = 0;
16249
16250 auto_obstack obstack;
16251
16252 for (child_die = die->child;
16253 child_die != NULL && child_die->tag;
16254 child_die = sibling_die (child_die))
16255 {
16256 struct attribute *attr;
16257 LONGEST value;
16258 const gdb_byte *bytes;
16259 struct dwarf2_locexpr_baton *baton;
16260 const char *name;
16261
16262 if (child_die->tag != DW_TAG_enumerator)
16263 continue;
16264
16265 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16266 if (attr == NULL)
16267 continue;
16268
16269 name = dwarf2_name (child_die, cu);
16270 if (name == NULL)
16271 name = "<anonymous enumerator>";
16272
16273 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16274 &value, &bytes, &baton);
16275 if (value < 0)
16276 {
16277 unsigned_enum = 0;
16278 flag_enum = 0;
16279 }
16280 else if ((mask & value) != 0)
16281 flag_enum = 0;
16282 else
16283 mask |= value;
16284
16285 /* If we already know that the enum type is neither unsigned, nor
16286 a flag type, no need to look at the rest of the enumerates. */
16287 if (!unsigned_enum && !flag_enum)
16288 break;
16289 }
16290
16291 if (unsigned_enum)
16292 TYPE_UNSIGNED (type) = 1;
16293 if (flag_enum)
16294 TYPE_FLAG_ENUM (type) = 1;
16295 }
16296
16297 /* Given a DW_AT_enumeration_type die, set its type. We do not
16298 complete the type's fields yet, or create any symbols. */
16299
16300 static struct type *
16301 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16302 {
16303 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16304 struct type *type;
16305 struct attribute *attr;
16306 const char *name;
16307
16308 /* If the definition of this type lives in .debug_types, read that type.
16309 Don't follow DW_AT_specification though, that will take us back up
16310 the chain and we want to go down. */
16311 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16312 if (attr)
16313 {
16314 type = get_DW_AT_signature_type (die, attr, cu);
16315
16316 /* The type's CU may not be the same as CU.
16317 Ensure TYPE is recorded with CU in die_type_hash. */
16318 return set_die_type (die, type, cu);
16319 }
16320
16321 type = alloc_type (objfile);
16322
16323 TYPE_CODE (type) = TYPE_CODE_ENUM;
16324 name = dwarf2_full_name (NULL, die, cu);
16325 if (name != NULL)
16326 TYPE_NAME (type) = name;
16327
16328 attr = dwarf2_attr (die, DW_AT_type, cu);
16329 if (attr != NULL)
16330 {
16331 struct type *underlying_type = die_type (die, cu);
16332
16333 TYPE_TARGET_TYPE (type) = underlying_type;
16334 }
16335
16336 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16337 if (attr)
16338 {
16339 TYPE_LENGTH (type) = DW_UNSND (attr);
16340 }
16341 else
16342 {
16343 TYPE_LENGTH (type) = 0;
16344 }
16345
16346 maybe_set_alignment (cu, die, type);
16347
16348 /* The enumeration DIE can be incomplete. In Ada, any type can be
16349 declared as private in the package spec, and then defined only
16350 inside the package body. Such types are known as Taft Amendment
16351 Types. When another package uses such a type, an incomplete DIE
16352 may be generated by the compiler. */
16353 if (die_is_declaration (die, cu))
16354 TYPE_STUB (type) = 1;
16355
16356 /* Finish the creation of this type by using the enum's children.
16357 We must call this even when the underlying type has been provided
16358 so that we can determine if we're looking at a "flag" enum. */
16359 update_enumeration_type_from_children (die, type, cu);
16360
16361 /* If this type has an underlying type that is not a stub, then we
16362 may use its attributes. We always use the "unsigned" attribute
16363 in this situation, because ordinarily we guess whether the type
16364 is unsigned -- but the guess can be wrong and the underlying type
16365 can tell us the reality. However, we defer to a local size
16366 attribute if one exists, because this lets the compiler override
16367 the underlying type if needed. */
16368 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16369 {
16370 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16371 if (TYPE_LENGTH (type) == 0)
16372 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16373 if (TYPE_RAW_ALIGN (type) == 0
16374 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16375 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16376 }
16377
16378 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16379
16380 return set_die_type (die, type, cu);
16381 }
16382
16383 /* Given a pointer to a die which begins an enumeration, process all
16384 the dies that define the members of the enumeration, and create the
16385 symbol for the enumeration type.
16386
16387 NOTE: We reverse the order of the element list. */
16388
16389 static void
16390 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16391 {
16392 struct type *this_type;
16393
16394 this_type = get_die_type (die, cu);
16395 if (this_type == NULL)
16396 this_type = read_enumeration_type (die, cu);
16397
16398 if (die->child != NULL)
16399 {
16400 struct die_info *child_die;
16401 struct symbol *sym;
16402 struct field *fields = NULL;
16403 int num_fields = 0;
16404 const char *name;
16405
16406 child_die = die->child;
16407 while (child_die && child_die->tag)
16408 {
16409 if (child_die->tag != DW_TAG_enumerator)
16410 {
16411 process_die (child_die, cu);
16412 }
16413 else
16414 {
16415 name = dwarf2_name (child_die, cu);
16416 if (name)
16417 {
16418 sym = new_symbol (child_die, this_type, cu);
16419
16420 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16421 {
16422 fields = (struct field *)
16423 xrealloc (fields,
16424 (num_fields + DW_FIELD_ALLOC_CHUNK)
16425 * sizeof (struct field));
16426 }
16427
16428 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16429 FIELD_TYPE (fields[num_fields]) = NULL;
16430 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16431 FIELD_BITSIZE (fields[num_fields]) = 0;
16432
16433 num_fields++;
16434 }
16435 }
16436
16437 child_die = sibling_die (child_die);
16438 }
16439
16440 if (num_fields)
16441 {
16442 TYPE_NFIELDS (this_type) = num_fields;
16443 TYPE_FIELDS (this_type) = (struct field *)
16444 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16445 memcpy (TYPE_FIELDS (this_type), fields,
16446 sizeof (struct field) * num_fields);
16447 xfree (fields);
16448 }
16449 }
16450
16451 /* If we are reading an enum from a .debug_types unit, and the enum
16452 is a declaration, and the enum is not the signatured type in the
16453 unit, then we do not want to add a symbol for it. Adding a
16454 symbol would in some cases obscure the true definition of the
16455 enum, giving users an incomplete type when the definition is
16456 actually available. Note that we do not want to do this for all
16457 enums which are just declarations, because C++0x allows forward
16458 enum declarations. */
16459 if (cu->per_cu->is_debug_types
16460 && die_is_declaration (die, cu))
16461 {
16462 struct signatured_type *sig_type;
16463
16464 sig_type = (struct signatured_type *) cu->per_cu;
16465 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16466 if (sig_type->type_offset_in_section != die->sect_off)
16467 return;
16468 }
16469
16470 new_symbol (die, this_type, cu);
16471 }
16472
16473 /* Extract all information from a DW_TAG_array_type DIE and put it in
16474 the DIE's type field. For now, this only handles one dimensional
16475 arrays. */
16476
16477 static struct type *
16478 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16479 {
16480 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16481 struct die_info *child_die;
16482 struct type *type;
16483 struct type *element_type, *range_type, *index_type;
16484 struct attribute *attr;
16485 const char *name;
16486 struct dynamic_prop *byte_stride_prop = NULL;
16487 unsigned int bit_stride = 0;
16488
16489 element_type = die_type (die, cu);
16490
16491 /* The die_type call above may have already set the type for this DIE. */
16492 type = get_die_type (die, cu);
16493 if (type)
16494 return type;
16495
16496 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16497 if (attr != NULL)
16498 {
16499 int stride_ok;
16500
16501 byte_stride_prop
16502 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16503 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16504 if (!stride_ok)
16505 {
16506 complaint (_("unable to read array DW_AT_byte_stride "
16507 " - DIE at %s [in module %s]"),
16508 sect_offset_str (die->sect_off),
16509 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16510 /* Ignore this attribute. We will likely not be able to print
16511 arrays of this type correctly, but there is little we can do
16512 to help if we cannot read the attribute's value. */
16513 byte_stride_prop = NULL;
16514 }
16515 }
16516
16517 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16518 if (attr != NULL)
16519 bit_stride = DW_UNSND (attr);
16520
16521 /* Irix 6.2 native cc creates array types without children for
16522 arrays with unspecified length. */
16523 if (die->child == NULL)
16524 {
16525 index_type = objfile_type (objfile)->builtin_int;
16526 range_type = create_static_range_type (NULL, index_type, 0, -1);
16527 type = create_array_type_with_stride (NULL, element_type, range_type,
16528 byte_stride_prop, bit_stride);
16529 return set_die_type (die, type, cu);
16530 }
16531
16532 std::vector<struct type *> range_types;
16533 child_die = die->child;
16534 while (child_die && child_die->tag)
16535 {
16536 if (child_die->tag == DW_TAG_subrange_type)
16537 {
16538 struct type *child_type = read_type_die (child_die, cu);
16539
16540 if (child_type != NULL)
16541 {
16542 /* The range type was succesfully read. Save it for the
16543 array type creation. */
16544 range_types.push_back (child_type);
16545 }
16546 }
16547 child_die = sibling_die (child_die);
16548 }
16549
16550 /* Dwarf2 dimensions are output from left to right, create the
16551 necessary array types in backwards order. */
16552
16553 type = element_type;
16554
16555 if (read_array_order (die, cu) == DW_ORD_col_major)
16556 {
16557 int i = 0;
16558
16559 while (i < range_types.size ())
16560 type = create_array_type_with_stride (NULL, type, range_types[i++],
16561 byte_stride_prop, bit_stride);
16562 }
16563 else
16564 {
16565 size_t ndim = range_types.size ();
16566 while (ndim-- > 0)
16567 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16568 byte_stride_prop, bit_stride);
16569 }
16570
16571 /* Understand Dwarf2 support for vector types (like they occur on
16572 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16573 array type. This is not part of the Dwarf2/3 standard yet, but a
16574 custom vendor extension. The main difference between a regular
16575 array and the vector variant is that vectors are passed by value
16576 to functions. */
16577 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16578 if (attr)
16579 make_vector_type (type);
16580
16581 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16582 implementation may choose to implement triple vectors using this
16583 attribute. */
16584 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16585 if (attr)
16586 {
16587 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16588 TYPE_LENGTH (type) = DW_UNSND (attr);
16589 else
16590 complaint (_("DW_AT_byte_size for array type smaller "
16591 "than the total size of elements"));
16592 }
16593
16594 name = dwarf2_name (die, cu);
16595 if (name)
16596 TYPE_NAME (type) = name;
16597
16598 maybe_set_alignment (cu, die, type);
16599
16600 /* Install the type in the die. */
16601 set_die_type (die, type, cu);
16602
16603 /* set_die_type should be already done. */
16604 set_descriptive_type (type, die, cu);
16605
16606 return type;
16607 }
16608
16609 static enum dwarf_array_dim_ordering
16610 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16611 {
16612 struct attribute *attr;
16613
16614 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16615
16616 if (attr)
16617 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16618
16619 /* GNU F77 is a special case, as at 08/2004 array type info is the
16620 opposite order to the dwarf2 specification, but data is still
16621 laid out as per normal fortran.
16622
16623 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16624 version checking. */
16625
16626 if (cu->language == language_fortran
16627 && cu->producer && strstr (cu->producer, "GNU F77"))
16628 {
16629 return DW_ORD_row_major;
16630 }
16631
16632 switch (cu->language_defn->la_array_ordering)
16633 {
16634 case array_column_major:
16635 return DW_ORD_col_major;
16636 case array_row_major:
16637 default:
16638 return DW_ORD_row_major;
16639 };
16640 }
16641
16642 /* Extract all information from a DW_TAG_set_type DIE and put it in
16643 the DIE's type field. */
16644
16645 static struct type *
16646 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16647 {
16648 struct type *domain_type, *set_type;
16649 struct attribute *attr;
16650
16651 domain_type = die_type (die, cu);
16652
16653 /* The die_type call above may have already set the type for this DIE. */
16654 set_type = get_die_type (die, cu);
16655 if (set_type)
16656 return set_type;
16657
16658 set_type = create_set_type (NULL, domain_type);
16659
16660 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16661 if (attr)
16662 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16663
16664 maybe_set_alignment (cu, die, set_type);
16665
16666 return set_die_type (die, set_type, cu);
16667 }
16668
16669 /* A helper for read_common_block that creates a locexpr baton.
16670 SYM is the symbol which we are marking as computed.
16671 COMMON_DIE is the DIE for the common block.
16672 COMMON_LOC is the location expression attribute for the common
16673 block itself.
16674 MEMBER_LOC is the location expression attribute for the particular
16675 member of the common block that we are processing.
16676 CU is the CU from which the above come. */
16677
16678 static void
16679 mark_common_block_symbol_computed (struct symbol *sym,
16680 struct die_info *common_die,
16681 struct attribute *common_loc,
16682 struct attribute *member_loc,
16683 struct dwarf2_cu *cu)
16684 {
16685 struct dwarf2_per_objfile *dwarf2_per_objfile
16686 = cu->per_cu->dwarf2_per_objfile;
16687 struct objfile *objfile = dwarf2_per_objfile->objfile;
16688 struct dwarf2_locexpr_baton *baton;
16689 gdb_byte *ptr;
16690 unsigned int cu_off;
16691 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16692 LONGEST offset = 0;
16693
16694 gdb_assert (common_loc && member_loc);
16695 gdb_assert (attr_form_is_block (common_loc));
16696 gdb_assert (attr_form_is_block (member_loc)
16697 || attr_form_is_constant (member_loc));
16698
16699 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16700 baton->per_cu = cu->per_cu;
16701 gdb_assert (baton->per_cu);
16702
16703 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16704
16705 if (attr_form_is_constant (member_loc))
16706 {
16707 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16708 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16709 }
16710 else
16711 baton->size += DW_BLOCK (member_loc)->size;
16712
16713 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16714 baton->data = ptr;
16715
16716 *ptr++ = DW_OP_call4;
16717 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16718 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16719 ptr += 4;
16720
16721 if (attr_form_is_constant (member_loc))
16722 {
16723 *ptr++ = DW_OP_addr;
16724 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16725 ptr += cu->header.addr_size;
16726 }
16727 else
16728 {
16729 /* We have to copy the data here, because DW_OP_call4 will only
16730 use a DW_AT_location attribute. */
16731 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16732 ptr += DW_BLOCK (member_loc)->size;
16733 }
16734
16735 *ptr++ = DW_OP_plus;
16736 gdb_assert (ptr - baton->data == baton->size);
16737
16738 SYMBOL_LOCATION_BATON (sym) = baton;
16739 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16740 }
16741
16742 /* Create appropriate locally-scoped variables for all the
16743 DW_TAG_common_block entries. Also create a struct common_block
16744 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16745 is used to sepate the common blocks name namespace from regular
16746 variable names. */
16747
16748 static void
16749 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16750 {
16751 struct attribute *attr;
16752
16753 attr = dwarf2_attr (die, DW_AT_location, cu);
16754 if (attr)
16755 {
16756 /* Support the .debug_loc offsets. */
16757 if (attr_form_is_block (attr))
16758 {
16759 /* Ok. */
16760 }
16761 else if (attr_form_is_section_offset (attr))
16762 {
16763 dwarf2_complex_location_expr_complaint ();
16764 attr = NULL;
16765 }
16766 else
16767 {
16768 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16769 "common block member");
16770 attr = NULL;
16771 }
16772 }
16773
16774 if (die->child != NULL)
16775 {
16776 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16777 struct die_info *child_die;
16778 size_t n_entries = 0, size;
16779 struct common_block *common_block;
16780 struct symbol *sym;
16781
16782 for (child_die = die->child;
16783 child_die && child_die->tag;
16784 child_die = sibling_die (child_die))
16785 ++n_entries;
16786
16787 size = (sizeof (struct common_block)
16788 + (n_entries - 1) * sizeof (struct symbol *));
16789 common_block
16790 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16791 size);
16792 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16793 common_block->n_entries = 0;
16794
16795 for (child_die = die->child;
16796 child_die && child_die->tag;
16797 child_die = sibling_die (child_die))
16798 {
16799 /* Create the symbol in the DW_TAG_common_block block in the current
16800 symbol scope. */
16801 sym = new_symbol (child_die, NULL, cu);
16802 if (sym != NULL)
16803 {
16804 struct attribute *member_loc;
16805
16806 common_block->contents[common_block->n_entries++] = sym;
16807
16808 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16809 cu);
16810 if (member_loc)
16811 {
16812 /* GDB has handled this for a long time, but it is
16813 not specified by DWARF. It seems to have been
16814 emitted by gfortran at least as recently as:
16815 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16816 complaint (_("Variable in common block has "
16817 "DW_AT_data_member_location "
16818 "- DIE at %s [in module %s]"),
16819 sect_offset_str (child_die->sect_off),
16820 objfile_name (objfile));
16821
16822 if (attr_form_is_section_offset (member_loc))
16823 dwarf2_complex_location_expr_complaint ();
16824 else if (attr_form_is_constant (member_loc)
16825 || attr_form_is_block (member_loc))
16826 {
16827 if (attr)
16828 mark_common_block_symbol_computed (sym, die, attr,
16829 member_loc, cu);
16830 }
16831 else
16832 dwarf2_complex_location_expr_complaint ();
16833 }
16834 }
16835 }
16836
16837 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16838 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16839 }
16840 }
16841
16842 /* Create a type for a C++ namespace. */
16843
16844 static struct type *
16845 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16846 {
16847 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16848 const char *previous_prefix, *name;
16849 int is_anonymous;
16850 struct type *type;
16851
16852 /* For extensions, reuse the type of the original namespace. */
16853 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16854 {
16855 struct die_info *ext_die;
16856 struct dwarf2_cu *ext_cu = cu;
16857
16858 ext_die = dwarf2_extension (die, &ext_cu);
16859 type = read_type_die (ext_die, ext_cu);
16860
16861 /* EXT_CU may not be the same as CU.
16862 Ensure TYPE is recorded with CU in die_type_hash. */
16863 return set_die_type (die, type, cu);
16864 }
16865
16866 name = namespace_name (die, &is_anonymous, cu);
16867
16868 /* Now build the name of the current namespace. */
16869
16870 previous_prefix = determine_prefix (die, cu);
16871 if (previous_prefix[0] != '\0')
16872 name = typename_concat (&objfile->objfile_obstack,
16873 previous_prefix, name, 0, cu);
16874
16875 /* Create the type. */
16876 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16877
16878 return set_die_type (die, type, cu);
16879 }
16880
16881 /* Read a namespace scope. */
16882
16883 static void
16884 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16885 {
16886 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16887 int is_anonymous;
16888
16889 /* Add a symbol associated to this if we haven't seen the namespace
16890 before. Also, add a using directive if it's an anonymous
16891 namespace. */
16892
16893 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16894 {
16895 struct type *type;
16896
16897 type = read_type_die (die, cu);
16898 new_symbol (die, type, cu);
16899
16900 namespace_name (die, &is_anonymous, cu);
16901 if (is_anonymous)
16902 {
16903 const char *previous_prefix = determine_prefix (die, cu);
16904
16905 std::vector<const char *> excludes;
16906 add_using_directive (using_directives (cu),
16907 previous_prefix, TYPE_NAME (type), NULL,
16908 NULL, excludes, 0, &objfile->objfile_obstack);
16909 }
16910 }
16911
16912 if (die->child != NULL)
16913 {
16914 struct die_info *child_die = die->child;
16915
16916 while (child_die && child_die->tag)
16917 {
16918 process_die (child_die, cu);
16919 child_die = sibling_die (child_die);
16920 }
16921 }
16922 }
16923
16924 /* Read a Fortran module as type. This DIE can be only a declaration used for
16925 imported module. Still we need that type as local Fortran "use ... only"
16926 declaration imports depend on the created type in determine_prefix. */
16927
16928 static struct type *
16929 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16930 {
16931 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16932 const char *module_name;
16933 struct type *type;
16934
16935 module_name = dwarf2_name (die, cu);
16936 if (!module_name)
16937 complaint (_("DW_TAG_module has no name, offset %s"),
16938 sect_offset_str (die->sect_off));
16939 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16940
16941 return set_die_type (die, type, cu);
16942 }
16943
16944 /* Read a Fortran module. */
16945
16946 static void
16947 read_module (struct die_info *die, struct dwarf2_cu *cu)
16948 {
16949 struct die_info *child_die = die->child;
16950 struct type *type;
16951
16952 type = read_type_die (die, cu);
16953 new_symbol (die, type, cu);
16954
16955 while (child_die && child_die->tag)
16956 {
16957 process_die (child_die, cu);
16958 child_die = sibling_die (child_die);
16959 }
16960 }
16961
16962 /* Return the name of the namespace represented by DIE. Set
16963 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16964 namespace. */
16965
16966 static const char *
16967 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16968 {
16969 struct die_info *current_die;
16970 const char *name = NULL;
16971
16972 /* Loop through the extensions until we find a name. */
16973
16974 for (current_die = die;
16975 current_die != NULL;
16976 current_die = dwarf2_extension (die, &cu))
16977 {
16978 /* We don't use dwarf2_name here so that we can detect the absence
16979 of a name -> anonymous namespace. */
16980 name = dwarf2_string_attr (die, DW_AT_name, cu);
16981
16982 if (name != NULL)
16983 break;
16984 }
16985
16986 /* Is it an anonymous namespace? */
16987
16988 *is_anonymous = (name == NULL);
16989 if (*is_anonymous)
16990 name = CP_ANONYMOUS_NAMESPACE_STR;
16991
16992 return name;
16993 }
16994
16995 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16996 the user defined type vector. */
16997
16998 static struct type *
16999 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17000 {
17001 struct gdbarch *gdbarch
17002 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17003 struct comp_unit_head *cu_header = &cu->header;
17004 struct type *type;
17005 struct attribute *attr_byte_size;
17006 struct attribute *attr_address_class;
17007 int byte_size, addr_class;
17008 struct type *target_type;
17009
17010 target_type = die_type (die, cu);
17011
17012 /* The die_type call above may have already set the type for this DIE. */
17013 type = get_die_type (die, cu);
17014 if (type)
17015 return type;
17016
17017 type = lookup_pointer_type (target_type);
17018
17019 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17020 if (attr_byte_size)
17021 byte_size = DW_UNSND (attr_byte_size);
17022 else
17023 byte_size = cu_header->addr_size;
17024
17025 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17026 if (attr_address_class)
17027 addr_class = DW_UNSND (attr_address_class);
17028 else
17029 addr_class = DW_ADDR_none;
17030
17031 ULONGEST alignment = get_alignment (cu, die);
17032
17033 /* If the pointer size, alignment, or address class is different
17034 than the default, create a type variant marked as such and set
17035 the length accordingly. */
17036 if (TYPE_LENGTH (type) != byte_size
17037 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17038 && alignment != TYPE_RAW_ALIGN (type))
17039 || addr_class != DW_ADDR_none)
17040 {
17041 if (gdbarch_address_class_type_flags_p (gdbarch))
17042 {
17043 int type_flags;
17044
17045 type_flags = gdbarch_address_class_type_flags
17046 (gdbarch, byte_size, addr_class);
17047 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17048 == 0);
17049 type = make_type_with_address_space (type, type_flags);
17050 }
17051 else if (TYPE_LENGTH (type) != byte_size)
17052 {
17053 complaint (_("invalid pointer size %d"), byte_size);
17054 }
17055 else if (TYPE_RAW_ALIGN (type) != alignment)
17056 {
17057 complaint (_("Invalid DW_AT_alignment"
17058 " - DIE at %s [in module %s]"),
17059 sect_offset_str (die->sect_off),
17060 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17061 }
17062 else
17063 {
17064 /* Should we also complain about unhandled address classes? */
17065 }
17066 }
17067
17068 TYPE_LENGTH (type) = byte_size;
17069 set_type_align (type, alignment);
17070 return set_die_type (die, type, cu);
17071 }
17072
17073 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17074 the user defined type vector. */
17075
17076 static struct type *
17077 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17078 {
17079 struct type *type;
17080 struct type *to_type;
17081 struct type *domain;
17082
17083 to_type = die_type (die, cu);
17084 domain = die_containing_type (die, cu);
17085
17086 /* The calls above may have already set the type for this DIE. */
17087 type = get_die_type (die, cu);
17088 if (type)
17089 return type;
17090
17091 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17092 type = lookup_methodptr_type (to_type);
17093 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17094 {
17095 struct type *new_type
17096 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17097
17098 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17099 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17100 TYPE_VARARGS (to_type));
17101 type = lookup_methodptr_type (new_type);
17102 }
17103 else
17104 type = lookup_memberptr_type (to_type, domain);
17105
17106 return set_die_type (die, type, cu);
17107 }
17108
17109 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17110 the user defined type vector. */
17111
17112 static struct type *
17113 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17114 enum type_code refcode)
17115 {
17116 struct comp_unit_head *cu_header = &cu->header;
17117 struct type *type, *target_type;
17118 struct attribute *attr;
17119
17120 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17121
17122 target_type = die_type (die, cu);
17123
17124 /* The die_type call above may have already set the type for this DIE. */
17125 type = get_die_type (die, cu);
17126 if (type)
17127 return type;
17128
17129 type = lookup_reference_type (target_type, refcode);
17130 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17131 if (attr)
17132 {
17133 TYPE_LENGTH (type) = DW_UNSND (attr);
17134 }
17135 else
17136 {
17137 TYPE_LENGTH (type) = cu_header->addr_size;
17138 }
17139 maybe_set_alignment (cu, die, type);
17140 return set_die_type (die, type, cu);
17141 }
17142
17143 /* Add the given cv-qualifiers to the element type of the array. GCC
17144 outputs DWARF type qualifiers that apply to an array, not the
17145 element type. But GDB relies on the array element type to carry
17146 the cv-qualifiers. This mimics section 6.7.3 of the C99
17147 specification. */
17148
17149 static struct type *
17150 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17151 struct type *base_type, int cnst, int voltl)
17152 {
17153 struct type *el_type, *inner_array;
17154
17155 base_type = copy_type (base_type);
17156 inner_array = base_type;
17157
17158 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17159 {
17160 TYPE_TARGET_TYPE (inner_array) =
17161 copy_type (TYPE_TARGET_TYPE (inner_array));
17162 inner_array = TYPE_TARGET_TYPE (inner_array);
17163 }
17164
17165 el_type = TYPE_TARGET_TYPE (inner_array);
17166 cnst |= TYPE_CONST (el_type);
17167 voltl |= TYPE_VOLATILE (el_type);
17168 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17169
17170 return set_die_type (die, base_type, cu);
17171 }
17172
17173 static struct type *
17174 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17175 {
17176 struct type *base_type, *cv_type;
17177
17178 base_type = die_type (die, cu);
17179
17180 /* The die_type call above may have already set the type for this DIE. */
17181 cv_type = get_die_type (die, cu);
17182 if (cv_type)
17183 return cv_type;
17184
17185 /* In case the const qualifier is applied to an array type, the element type
17186 is so qualified, not the array type (section 6.7.3 of C99). */
17187 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17188 return add_array_cv_type (die, cu, base_type, 1, 0);
17189
17190 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17191 return set_die_type (die, cv_type, cu);
17192 }
17193
17194 static struct type *
17195 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17196 {
17197 struct type *base_type, *cv_type;
17198
17199 base_type = die_type (die, cu);
17200
17201 /* The die_type call above may have already set the type for this DIE. */
17202 cv_type = get_die_type (die, cu);
17203 if (cv_type)
17204 return cv_type;
17205
17206 /* In case the volatile qualifier is applied to an array type, the
17207 element type is so qualified, not the array type (section 6.7.3
17208 of C99). */
17209 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17210 return add_array_cv_type (die, cu, base_type, 0, 1);
17211
17212 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17213 return set_die_type (die, cv_type, cu);
17214 }
17215
17216 /* Handle DW_TAG_restrict_type. */
17217
17218 static struct type *
17219 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17220 {
17221 struct type *base_type, *cv_type;
17222
17223 base_type = die_type (die, cu);
17224
17225 /* The die_type call above may have already set the type for this DIE. */
17226 cv_type = get_die_type (die, cu);
17227 if (cv_type)
17228 return cv_type;
17229
17230 cv_type = make_restrict_type (base_type);
17231 return set_die_type (die, cv_type, cu);
17232 }
17233
17234 /* Handle DW_TAG_atomic_type. */
17235
17236 static struct type *
17237 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17238 {
17239 struct type *base_type, *cv_type;
17240
17241 base_type = die_type (die, cu);
17242
17243 /* The die_type call above may have already set the type for this DIE. */
17244 cv_type = get_die_type (die, cu);
17245 if (cv_type)
17246 return cv_type;
17247
17248 cv_type = make_atomic_type (base_type);
17249 return set_die_type (die, cv_type, cu);
17250 }
17251
17252 /* Extract all information from a DW_TAG_string_type DIE and add to
17253 the user defined type vector. It isn't really a user defined type,
17254 but it behaves like one, with other DIE's using an AT_user_def_type
17255 attribute to reference it. */
17256
17257 static struct type *
17258 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17259 {
17260 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17261 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17262 struct type *type, *range_type, *index_type, *char_type;
17263 struct attribute *attr;
17264 unsigned int length;
17265
17266 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17267 if (attr)
17268 {
17269 length = DW_UNSND (attr);
17270 }
17271 else
17272 {
17273 /* Check for the DW_AT_byte_size attribute. */
17274 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17275 if (attr)
17276 {
17277 length = DW_UNSND (attr);
17278 }
17279 else
17280 {
17281 length = 1;
17282 }
17283 }
17284
17285 index_type = objfile_type (objfile)->builtin_int;
17286 range_type = create_static_range_type (NULL, index_type, 1, length);
17287 char_type = language_string_char_type (cu->language_defn, gdbarch);
17288 type = create_string_type (NULL, char_type, range_type);
17289
17290 return set_die_type (die, type, cu);
17291 }
17292
17293 /* Assuming that DIE corresponds to a function, returns nonzero
17294 if the function is prototyped. */
17295
17296 static int
17297 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17298 {
17299 struct attribute *attr;
17300
17301 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17302 if (attr && (DW_UNSND (attr) != 0))
17303 return 1;
17304
17305 /* The DWARF standard implies that the DW_AT_prototyped attribute
17306 is only meaninful for C, but the concept also extends to other
17307 languages that allow unprototyped functions (Eg: Objective C).
17308 For all other languages, assume that functions are always
17309 prototyped. */
17310 if (cu->language != language_c
17311 && cu->language != language_objc
17312 && cu->language != language_opencl)
17313 return 1;
17314
17315 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17316 prototyped and unprototyped functions; default to prototyped,
17317 since that is more common in modern code (and RealView warns
17318 about unprototyped functions). */
17319 if (producer_is_realview (cu->producer))
17320 return 1;
17321
17322 return 0;
17323 }
17324
17325 /* Handle DIES due to C code like:
17326
17327 struct foo
17328 {
17329 int (*funcp)(int a, long l);
17330 int b;
17331 };
17332
17333 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17334
17335 static struct type *
17336 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17337 {
17338 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17339 struct type *type; /* Type that this function returns. */
17340 struct type *ftype; /* Function that returns above type. */
17341 struct attribute *attr;
17342
17343 type = die_type (die, cu);
17344
17345 /* The die_type call above may have already set the type for this DIE. */
17346 ftype = get_die_type (die, cu);
17347 if (ftype)
17348 return ftype;
17349
17350 ftype = lookup_function_type (type);
17351
17352 if (prototyped_function_p (die, cu))
17353 TYPE_PROTOTYPED (ftype) = 1;
17354
17355 /* Store the calling convention in the type if it's available in
17356 the subroutine die. Otherwise set the calling convention to
17357 the default value DW_CC_normal. */
17358 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17359 if (attr)
17360 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17361 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17362 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17363 else
17364 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17365
17366 /* Record whether the function returns normally to its caller or not
17367 if the DWARF producer set that information. */
17368 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17369 if (attr && (DW_UNSND (attr) != 0))
17370 TYPE_NO_RETURN (ftype) = 1;
17371
17372 /* We need to add the subroutine type to the die immediately so
17373 we don't infinitely recurse when dealing with parameters
17374 declared as the same subroutine type. */
17375 set_die_type (die, ftype, cu);
17376
17377 if (die->child != NULL)
17378 {
17379 struct type *void_type = objfile_type (objfile)->builtin_void;
17380 struct die_info *child_die;
17381 int nparams, iparams;
17382
17383 /* Count the number of parameters.
17384 FIXME: GDB currently ignores vararg functions, but knows about
17385 vararg member functions. */
17386 nparams = 0;
17387 child_die = die->child;
17388 while (child_die && child_die->tag)
17389 {
17390 if (child_die->tag == DW_TAG_formal_parameter)
17391 nparams++;
17392 else if (child_die->tag == DW_TAG_unspecified_parameters)
17393 TYPE_VARARGS (ftype) = 1;
17394 child_die = sibling_die (child_die);
17395 }
17396
17397 /* Allocate storage for parameters and fill them in. */
17398 TYPE_NFIELDS (ftype) = nparams;
17399 TYPE_FIELDS (ftype) = (struct field *)
17400 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17401
17402 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17403 even if we error out during the parameters reading below. */
17404 for (iparams = 0; iparams < nparams; iparams++)
17405 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17406
17407 iparams = 0;
17408 child_die = die->child;
17409 while (child_die && child_die->tag)
17410 {
17411 if (child_die->tag == DW_TAG_formal_parameter)
17412 {
17413 struct type *arg_type;
17414
17415 /* DWARF version 2 has no clean way to discern C++
17416 static and non-static member functions. G++ helps
17417 GDB by marking the first parameter for non-static
17418 member functions (which is the this pointer) as
17419 artificial. We pass this information to
17420 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17421
17422 DWARF version 3 added DW_AT_object_pointer, which GCC
17423 4.5 does not yet generate. */
17424 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17425 if (attr)
17426 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17427 else
17428 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17429 arg_type = die_type (child_die, cu);
17430
17431 /* RealView does not mark THIS as const, which the testsuite
17432 expects. GCC marks THIS as const in method definitions,
17433 but not in the class specifications (GCC PR 43053). */
17434 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17435 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17436 {
17437 int is_this = 0;
17438 struct dwarf2_cu *arg_cu = cu;
17439 const char *name = dwarf2_name (child_die, cu);
17440
17441 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17442 if (attr)
17443 {
17444 /* If the compiler emits this, use it. */
17445 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17446 is_this = 1;
17447 }
17448 else if (name && strcmp (name, "this") == 0)
17449 /* Function definitions will have the argument names. */
17450 is_this = 1;
17451 else if (name == NULL && iparams == 0)
17452 /* Declarations may not have the names, so like
17453 elsewhere in GDB, assume an artificial first
17454 argument is "this". */
17455 is_this = 1;
17456
17457 if (is_this)
17458 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17459 arg_type, 0);
17460 }
17461
17462 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17463 iparams++;
17464 }
17465 child_die = sibling_die (child_die);
17466 }
17467 }
17468
17469 return ftype;
17470 }
17471
17472 static struct type *
17473 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17474 {
17475 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17476 const char *name = NULL;
17477 struct type *this_type, *target_type;
17478
17479 name = dwarf2_full_name (NULL, die, cu);
17480 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17481 TYPE_TARGET_STUB (this_type) = 1;
17482 set_die_type (die, this_type, cu);
17483 target_type = die_type (die, cu);
17484 if (target_type != this_type)
17485 TYPE_TARGET_TYPE (this_type) = target_type;
17486 else
17487 {
17488 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17489 spec and cause infinite loops in GDB. */
17490 complaint (_("Self-referential DW_TAG_typedef "
17491 "- DIE at %s [in module %s]"),
17492 sect_offset_str (die->sect_off), objfile_name (objfile));
17493 TYPE_TARGET_TYPE (this_type) = NULL;
17494 }
17495 return this_type;
17496 }
17497
17498 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17499 (which may be different from NAME) to the architecture back-end to allow
17500 it to guess the correct format if necessary. */
17501
17502 static struct type *
17503 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17504 const char *name_hint)
17505 {
17506 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17507 const struct floatformat **format;
17508 struct type *type;
17509
17510 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17511 if (format)
17512 type = init_float_type (objfile, bits, name, format);
17513 else
17514 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17515
17516 return type;
17517 }
17518
17519 /* Allocate an integer type of size BITS and name NAME. */
17520
17521 static struct type *
17522 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17523 int bits, int unsigned_p, const char *name)
17524 {
17525 struct type *type;
17526
17527 /* Versions of Intel's C Compiler generate an integer type called "void"
17528 instead of using DW_TAG_unspecified_type. This has been seen on
17529 at least versions 14, 17, and 18. */
17530 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17531 && strcmp (name, "void") == 0)
17532 type = objfile_type (objfile)->builtin_void;
17533 else
17534 type = init_integer_type (objfile, bits, unsigned_p, name);
17535
17536 return type;
17537 }
17538
17539 /* Initialise and return a floating point type of size BITS suitable for
17540 use as a component of a complex number. The NAME_HINT is passed through
17541 when initialising the floating point type and is the name of the complex
17542 type.
17543
17544 As DWARF doesn't currently provide an explicit name for the components
17545 of a complex number, but it can be helpful to have these components
17546 named, we try to select a suitable name based on the size of the
17547 component. */
17548 static struct type *
17549 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17550 struct objfile *objfile,
17551 int bits, const char *name_hint)
17552 {
17553 gdbarch *gdbarch = get_objfile_arch (objfile);
17554 struct type *tt = nullptr;
17555
17556 switch (bits)
17557 {
17558 case 32:
17559 tt = builtin_type (gdbarch)->builtin_float;
17560 break;
17561 case 64:
17562 tt = builtin_type (gdbarch)->builtin_double;
17563 break;
17564 case 128:
17565 tt = builtin_type (gdbarch)->builtin_long_double;
17566 break;
17567 }
17568
17569 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17570 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17571 }
17572
17573 /* Find a representation of a given base type and install
17574 it in the TYPE field of the die. */
17575
17576 static struct type *
17577 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17578 {
17579 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17580 struct type *type;
17581 struct attribute *attr;
17582 int encoding = 0, bits = 0;
17583 const char *name;
17584
17585 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17586 if (attr)
17587 {
17588 encoding = DW_UNSND (attr);
17589 }
17590 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17591 if (attr)
17592 {
17593 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17594 }
17595 name = dwarf2_name (die, cu);
17596 if (!name)
17597 {
17598 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17599 }
17600
17601 switch (encoding)
17602 {
17603 case DW_ATE_address:
17604 /* Turn DW_ATE_address into a void * pointer. */
17605 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17606 type = init_pointer_type (objfile, bits, name, type);
17607 break;
17608 case DW_ATE_boolean:
17609 type = init_boolean_type (objfile, bits, 1, name);
17610 break;
17611 case DW_ATE_complex_float:
17612 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
17613 type = init_complex_type (objfile, name, type);
17614 break;
17615 case DW_ATE_decimal_float:
17616 type = init_decfloat_type (objfile, bits, name);
17617 break;
17618 case DW_ATE_float:
17619 type = dwarf2_init_float_type (objfile, bits, name, name);
17620 break;
17621 case DW_ATE_signed:
17622 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17623 break;
17624 case DW_ATE_unsigned:
17625 if (cu->language == language_fortran
17626 && name
17627 && startswith (name, "character("))
17628 type = init_character_type (objfile, bits, 1, name);
17629 else
17630 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17631 break;
17632 case DW_ATE_signed_char:
17633 if (cu->language == language_ada || cu->language == language_m2
17634 || cu->language == language_pascal
17635 || cu->language == language_fortran)
17636 type = init_character_type (objfile, bits, 0, name);
17637 else
17638 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17639 break;
17640 case DW_ATE_unsigned_char:
17641 if (cu->language == language_ada || cu->language == language_m2
17642 || cu->language == language_pascal
17643 || cu->language == language_fortran
17644 || cu->language == language_rust)
17645 type = init_character_type (objfile, bits, 1, name);
17646 else
17647 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17648 break;
17649 case DW_ATE_UTF:
17650 {
17651 gdbarch *arch = get_objfile_arch (objfile);
17652
17653 if (bits == 16)
17654 type = builtin_type (arch)->builtin_char16;
17655 else if (bits == 32)
17656 type = builtin_type (arch)->builtin_char32;
17657 else
17658 {
17659 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17660 bits);
17661 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17662 }
17663 return set_die_type (die, type, cu);
17664 }
17665 break;
17666
17667 default:
17668 complaint (_("unsupported DW_AT_encoding: '%s'"),
17669 dwarf_type_encoding_name (encoding));
17670 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17671 break;
17672 }
17673
17674 if (name && strcmp (name, "char") == 0)
17675 TYPE_NOSIGN (type) = 1;
17676
17677 maybe_set_alignment (cu, die, type);
17678
17679 return set_die_type (die, type, cu);
17680 }
17681
17682 /* Parse dwarf attribute if it's a block, reference or constant and put the
17683 resulting value of the attribute into struct bound_prop.
17684 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17685
17686 static int
17687 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17688 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17689 {
17690 struct dwarf2_property_baton *baton;
17691 struct obstack *obstack
17692 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17693
17694 if (attr == NULL || prop == NULL)
17695 return 0;
17696
17697 if (attr_form_is_block (attr))
17698 {
17699 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17700 baton->referenced_type = NULL;
17701 baton->locexpr.per_cu = cu->per_cu;
17702 baton->locexpr.size = DW_BLOCK (attr)->size;
17703 baton->locexpr.data = DW_BLOCK (attr)->data;
17704 prop->data.baton = baton;
17705 prop->kind = PROP_LOCEXPR;
17706 gdb_assert (prop->data.baton != NULL);
17707 }
17708 else if (attr_form_is_ref (attr))
17709 {
17710 struct dwarf2_cu *target_cu = cu;
17711 struct die_info *target_die;
17712 struct attribute *target_attr;
17713
17714 target_die = follow_die_ref (die, attr, &target_cu);
17715 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17716 if (target_attr == NULL)
17717 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17718 target_cu);
17719 if (target_attr == NULL)
17720 return 0;
17721
17722 switch (target_attr->name)
17723 {
17724 case DW_AT_location:
17725 if (attr_form_is_section_offset (target_attr))
17726 {
17727 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17728 baton->referenced_type = die_type (target_die, target_cu);
17729 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17730 prop->data.baton = baton;
17731 prop->kind = PROP_LOCLIST;
17732 gdb_assert (prop->data.baton != NULL);
17733 }
17734 else if (attr_form_is_block (target_attr))
17735 {
17736 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17737 baton->referenced_type = die_type (target_die, target_cu);
17738 baton->locexpr.per_cu = cu->per_cu;
17739 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17740 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17741 prop->data.baton = baton;
17742 prop->kind = PROP_LOCEXPR;
17743 gdb_assert (prop->data.baton != NULL);
17744 }
17745 else
17746 {
17747 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17748 "dynamic property");
17749 return 0;
17750 }
17751 break;
17752 case DW_AT_data_member_location:
17753 {
17754 LONGEST offset;
17755
17756 if (!handle_data_member_location (target_die, target_cu,
17757 &offset))
17758 return 0;
17759
17760 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17761 baton->referenced_type = read_type_die (target_die->parent,
17762 target_cu);
17763 baton->offset_info.offset = offset;
17764 baton->offset_info.type = die_type (target_die, target_cu);
17765 prop->data.baton = baton;
17766 prop->kind = PROP_ADDR_OFFSET;
17767 break;
17768 }
17769 }
17770 }
17771 else if (attr_form_is_constant (attr))
17772 {
17773 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17774 prop->kind = PROP_CONST;
17775 }
17776 else
17777 {
17778 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17779 dwarf2_name (die, cu));
17780 return 0;
17781 }
17782
17783 return 1;
17784 }
17785
17786 /* Read the given DW_AT_subrange DIE. */
17787
17788 static struct type *
17789 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17790 {
17791 struct type *base_type, *orig_base_type;
17792 struct type *range_type;
17793 struct attribute *attr;
17794 struct dynamic_prop low, high;
17795 int low_default_is_valid;
17796 int high_bound_is_count = 0;
17797 const char *name;
17798 ULONGEST negative_mask;
17799
17800 orig_base_type = die_type (die, cu);
17801 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17802 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17803 creating the range type, but we use the result of check_typedef
17804 when examining properties of the type. */
17805 base_type = check_typedef (orig_base_type);
17806
17807 /* The die_type call above may have already set the type for this DIE. */
17808 range_type = get_die_type (die, cu);
17809 if (range_type)
17810 return range_type;
17811
17812 low.kind = PROP_CONST;
17813 high.kind = PROP_CONST;
17814 high.data.const_val = 0;
17815
17816 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17817 omitting DW_AT_lower_bound. */
17818 switch (cu->language)
17819 {
17820 case language_c:
17821 case language_cplus:
17822 low.data.const_val = 0;
17823 low_default_is_valid = 1;
17824 break;
17825 case language_fortran:
17826 low.data.const_val = 1;
17827 low_default_is_valid = 1;
17828 break;
17829 case language_d:
17830 case language_objc:
17831 case language_rust:
17832 low.data.const_val = 0;
17833 low_default_is_valid = (cu->header.version >= 4);
17834 break;
17835 case language_ada:
17836 case language_m2:
17837 case language_pascal:
17838 low.data.const_val = 1;
17839 low_default_is_valid = (cu->header.version >= 4);
17840 break;
17841 default:
17842 low.data.const_val = 0;
17843 low_default_is_valid = 0;
17844 break;
17845 }
17846
17847 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17848 if (attr)
17849 attr_to_dynamic_prop (attr, die, cu, &low);
17850 else if (!low_default_is_valid)
17851 complaint (_("Missing DW_AT_lower_bound "
17852 "- DIE at %s [in module %s]"),
17853 sect_offset_str (die->sect_off),
17854 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17855
17856 struct attribute *attr_ub, *attr_count;
17857 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17858 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17859 {
17860 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17861 if (attr_to_dynamic_prop (attr, die, cu, &high))
17862 {
17863 /* If bounds are constant do the final calculation here. */
17864 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17865 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17866 else
17867 high_bound_is_count = 1;
17868 }
17869 else
17870 {
17871 if (attr_ub != NULL)
17872 complaint (_("Unresolved DW_AT_upper_bound "
17873 "- DIE at %s [in module %s]"),
17874 sect_offset_str (die->sect_off),
17875 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17876 if (attr_count != NULL)
17877 complaint (_("Unresolved DW_AT_count "
17878 "- DIE at %s [in module %s]"),
17879 sect_offset_str (die->sect_off),
17880 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17881 }
17882
17883 }
17884
17885 /* Dwarf-2 specifications explicitly allows to create subrange types
17886 without specifying a base type.
17887 In that case, the base type must be set to the type of
17888 the lower bound, upper bound or count, in that order, if any of these
17889 three attributes references an object that has a type.
17890 If no base type is found, the Dwarf-2 specifications say that
17891 a signed integer type of size equal to the size of an address should
17892 be used.
17893 For the following C code: `extern char gdb_int [];'
17894 GCC produces an empty range DIE.
17895 FIXME: muller/2010-05-28: Possible references to object for low bound,
17896 high bound or count are not yet handled by this code. */
17897 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17898 {
17899 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17900 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17901 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17902 struct type *int_type = objfile_type (objfile)->builtin_int;
17903
17904 /* Test "int", "long int", and "long long int" objfile types,
17905 and select the first one having a size above or equal to the
17906 architecture address size. */
17907 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17908 base_type = int_type;
17909 else
17910 {
17911 int_type = objfile_type (objfile)->builtin_long;
17912 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17913 base_type = int_type;
17914 else
17915 {
17916 int_type = objfile_type (objfile)->builtin_long_long;
17917 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17918 base_type = int_type;
17919 }
17920 }
17921 }
17922
17923 /* Normally, the DWARF producers are expected to use a signed
17924 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17925 But this is unfortunately not always the case, as witnessed
17926 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17927 is used instead. To work around that ambiguity, we treat
17928 the bounds as signed, and thus sign-extend their values, when
17929 the base type is signed. */
17930 negative_mask =
17931 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17932 if (low.kind == PROP_CONST
17933 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17934 low.data.const_val |= negative_mask;
17935 if (high.kind == PROP_CONST
17936 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17937 high.data.const_val |= negative_mask;
17938
17939 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17940
17941 if (high_bound_is_count)
17942 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17943
17944 /* Ada expects an empty array on no boundary attributes. */
17945 if (attr == NULL && cu->language != language_ada)
17946 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17947
17948 name = dwarf2_name (die, cu);
17949 if (name)
17950 TYPE_NAME (range_type) = name;
17951
17952 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17953 if (attr)
17954 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17955
17956 maybe_set_alignment (cu, die, range_type);
17957
17958 set_die_type (die, range_type, cu);
17959
17960 /* set_die_type should be already done. */
17961 set_descriptive_type (range_type, die, cu);
17962
17963 return range_type;
17964 }
17965
17966 static struct type *
17967 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17968 {
17969 struct type *type;
17970
17971 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17972 NULL);
17973 TYPE_NAME (type) = dwarf2_name (die, cu);
17974
17975 /* In Ada, an unspecified type is typically used when the description
17976 of the type is defered to a different unit. When encountering
17977 such a type, we treat it as a stub, and try to resolve it later on,
17978 when needed. */
17979 if (cu->language == language_ada)
17980 TYPE_STUB (type) = 1;
17981
17982 return set_die_type (die, type, cu);
17983 }
17984
17985 /* Read a single die and all its descendents. Set the die's sibling
17986 field to NULL; set other fields in the die correctly, and set all
17987 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17988 location of the info_ptr after reading all of those dies. PARENT
17989 is the parent of the die in question. */
17990
17991 static struct die_info *
17992 read_die_and_children (const struct die_reader_specs *reader,
17993 const gdb_byte *info_ptr,
17994 const gdb_byte **new_info_ptr,
17995 struct die_info *parent)
17996 {
17997 struct die_info *die;
17998 const gdb_byte *cur_ptr;
17999 int has_children;
18000
18001 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18002 if (die == NULL)
18003 {
18004 *new_info_ptr = cur_ptr;
18005 return NULL;
18006 }
18007 store_in_ref_table (die, reader->cu);
18008
18009 if (has_children)
18010 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18011 else
18012 {
18013 die->child = NULL;
18014 *new_info_ptr = cur_ptr;
18015 }
18016
18017 die->sibling = NULL;
18018 die->parent = parent;
18019 return die;
18020 }
18021
18022 /* Read a die, all of its descendents, and all of its siblings; set
18023 all of the fields of all of the dies correctly. Arguments are as
18024 in read_die_and_children. */
18025
18026 static struct die_info *
18027 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18028 const gdb_byte *info_ptr,
18029 const gdb_byte **new_info_ptr,
18030 struct die_info *parent)
18031 {
18032 struct die_info *first_die, *last_sibling;
18033 const gdb_byte *cur_ptr;
18034
18035 cur_ptr = info_ptr;
18036 first_die = last_sibling = NULL;
18037
18038 while (1)
18039 {
18040 struct die_info *die
18041 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18042
18043 if (die == NULL)
18044 {
18045 *new_info_ptr = cur_ptr;
18046 return first_die;
18047 }
18048
18049 if (!first_die)
18050 first_die = die;
18051 else
18052 last_sibling->sibling = die;
18053
18054 last_sibling = die;
18055 }
18056 }
18057
18058 /* Read a die, all of its descendents, and all of its siblings; set
18059 all of the fields of all of the dies correctly. Arguments are as
18060 in read_die_and_children.
18061 This the main entry point for reading a DIE and all its children. */
18062
18063 static struct die_info *
18064 read_die_and_siblings (const struct die_reader_specs *reader,
18065 const gdb_byte *info_ptr,
18066 const gdb_byte **new_info_ptr,
18067 struct die_info *parent)
18068 {
18069 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18070 new_info_ptr, parent);
18071
18072 if (dwarf_die_debug)
18073 {
18074 fprintf_unfiltered (gdb_stdlog,
18075 "Read die from %s@0x%x of %s:\n",
18076 get_section_name (reader->die_section),
18077 (unsigned) (info_ptr - reader->die_section->buffer),
18078 bfd_get_filename (reader->abfd));
18079 dump_die (die, dwarf_die_debug);
18080 }
18081
18082 return die;
18083 }
18084
18085 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18086 attributes.
18087 The caller is responsible for filling in the extra attributes
18088 and updating (*DIEP)->num_attrs.
18089 Set DIEP to point to a newly allocated die with its information,
18090 except for its child, sibling, and parent fields.
18091 Set HAS_CHILDREN to tell whether the die has children or not. */
18092
18093 static const gdb_byte *
18094 read_full_die_1 (const struct die_reader_specs *reader,
18095 struct die_info **diep, const gdb_byte *info_ptr,
18096 int *has_children, int num_extra_attrs)
18097 {
18098 unsigned int abbrev_number, bytes_read, i;
18099 struct abbrev_info *abbrev;
18100 struct die_info *die;
18101 struct dwarf2_cu *cu = reader->cu;
18102 bfd *abfd = reader->abfd;
18103
18104 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18105 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18106 info_ptr += bytes_read;
18107 if (!abbrev_number)
18108 {
18109 *diep = NULL;
18110 *has_children = 0;
18111 return info_ptr;
18112 }
18113
18114 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18115 if (!abbrev)
18116 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18117 abbrev_number,
18118 bfd_get_filename (abfd));
18119
18120 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18121 die->sect_off = sect_off;
18122 die->tag = abbrev->tag;
18123 die->abbrev = abbrev_number;
18124
18125 /* Make the result usable.
18126 The caller needs to update num_attrs after adding the extra
18127 attributes. */
18128 die->num_attrs = abbrev->num_attrs;
18129
18130 for (i = 0; i < abbrev->num_attrs; ++i)
18131 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18132 info_ptr);
18133
18134 *diep = die;
18135 *has_children = abbrev->has_children;
18136 return info_ptr;
18137 }
18138
18139 /* Read a die and all its attributes.
18140 Set DIEP to point to a newly allocated die with its information,
18141 except for its child, sibling, and parent fields.
18142 Set HAS_CHILDREN to tell whether the die has children or not. */
18143
18144 static const gdb_byte *
18145 read_full_die (const struct die_reader_specs *reader,
18146 struct die_info **diep, const gdb_byte *info_ptr,
18147 int *has_children)
18148 {
18149 const gdb_byte *result;
18150
18151 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18152
18153 if (dwarf_die_debug)
18154 {
18155 fprintf_unfiltered (gdb_stdlog,
18156 "Read die from %s@0x%x of %s:\n",
18157 get_section_name (reader->die_section),
18158 (unsigned) (info_ptr - reader->die_section->buffer),
18159 bfd_get_filename (reader->abfd));
18160 dump_die (*diep, dwarf_die_debug);
18161 }
18162
18163 return result;
18164 }
18165 \f
18166 /* Abbreviation tables.
18167
18168 In DWARF version 2, the description of the debugging information is
18169 stored in a separate .debug_abbrev section. Before we read any
18170 dies from a section we read in all abbreviations and install them
18171 in a hash table. */
18172
18173 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18174
18175 struct abbrev_info *
18176 abbrev_table::alloc_abbrev ()
18177 {
18178 struct abbrev_info *abbrev;
18179
18180 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18181 memset (abbrev, 0, sizeof (struct abbrev_info));
18182
18183 return abbrev;
18184 }
18185
18186 /* Add an abbreviation to the table. */
18187
18188 void
18189 abbrev_table::add_abbrev (unsigned int abbrev_number,
18190 struct abbrev_info *abbrev)
18191 {
18192 unsigned int hash_number;
18193
18194 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18195 abbrev->next = m_abbrevs[hash_number];
18196 m_abbrevs[hash_number] = abbrev;
18197 }
18198
18199 /* Look up an abbrev in the table.
18200 Returns NULL if the abbrev is not found. */
18201
18202 struct abbrev_info *
18203 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18204 {
18205 unsigned int hash_number;
18206 struct abbrev_info *abbrev;
18207
18208 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18209 abbrev = m_abbrevs[hash_number];
18210
18211 while (abbrev)
18212 {
18213 if (abbrev->number == abbrev_number)
18214 return abbrev;
18215 abbrev = abbrev->next;
18216 }
18217 return NULL;
18218 }
18219
18220 /* Read in an abbrev table. */
18221
18222 static abbrev_table_up
18223 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18224 struct dwarf2_section_info *section,
18225 sect_offset sect_off)
18226 {
18227 struct objfile *objfile = dwarf2_per_objfile->objfile;
18228 bfd *abfd = get_section_bfd_owner (section);
18229 const gdb_byte *abbrev_ptr;
18230 struct abbrev_info *cur_abbrev;
18231 unsigned int abbrev_number, bytes_read, abbrev_name;
18232 unsigned int abbrev_form;
18233 struct attr_abbrev *cur_attrs;
18234 unsigned int allocated_attrs;
18235
18236 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18237
18238 dwarf2_read_section (objfile, section);
18239 abbrev_ptr = section->buffer + to_underlying (sect_off);
18240 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18241 abbrev_ptr += bytes_read;
18242
18243 allocated_attrs = ATTR_ALLOC_CHUNK;
18244 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18245
18246 /* Loop until we reach an abbrev number of 0. */
18247 while (abbrev_number)
18248 {
18249 cur_abbrev = abbrev_table->alloc_abbrev ();
18250
18251 /* read in abbrev header */
18252 cur_abbrev->number = abbrev_number;
18253 cur_abbrev->tag
18254 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18255 abbrev_ptr += bytes_read;
18256 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18257 abbrev_ptr += 1;
18258
18259 /* now read in declarations */
18260 for (;;)
18261 {
18262 LONGEST implicit_const;
18263
18264 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18265 abbrev_ptr += bytes_read;
18266 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18267 abbrev_ptr += bytes_read;
18268 if (abbrev_form == DW_FORM_implicit_const)
18269 {
18270 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18271 &bytes_read);
18272 abbrev_ptr += bytes_read;
18273 }
18274 else
18275 {
18276 /* Initialize it due to a false compiler warning. */
18277 implicit_const = -1;
18278 }
18279
18280 if (abbrev_name == 0)
18281 break;
18282
18283 if (cur_abbrev->num_attrs == allocated_attrs)
18284 {
18285 allocated_attrs += ATTR_ALLOC_CHUNK;
18286 cur_attrs
18287 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18288 }
18289
18290 cur_attrs[cur_abbrev->num_attrs].name
18291 = (enum dwarf_attribute) abbrev_name;
18292 cur_attrs[cur_abbrev->num_attrs].form
18293 = (enum dwarf_form) abbrev_form;
18294 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18295 ++cur_abbrev->num_attrs;
18296 }
18297
18298 cur_abbrev->attrs =
18299 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18300 cur_abbrev->num_attrs);
18301 memcpy (cur_abbrev->attrs, cur_attrs,
18302 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18303
18304 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18305
18306 /* Get next abbreviation.
18307 Under Irix6 the abbreviations for a compilation unit are not
18308 always properly terminated with an abbrev number of 0.
18309 Exit loop if we encounter an abbreviation which we have
18310 already read (which means we are about to read the abbreviations
18311 for the next compile unit) or if the end of the abbreviation
18312 table is reached. */
18313 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18314 break;
18315 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18316 abbrev_ptr += bytes_read;
18317 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18318 break;
18319 }
18320
18321 xfree (cur_attrs);
18322 return abbrev_table;
18323 }
18324
18325 /* Returns nonzero if TAG represents a type that we might generate a partial
18326 symbol for. */
18327
18328 static int
18329 is_type_tag_for_partial (int tag)
18330 {
18331 switch (tag)
18332 {
18333 #if 0
18334 /* Some types that would be reasonable to generate partial symbols for,
18335 that we don't at present. */
18336 case DW_TAG_array_type:
18337 case DW_TAG_file_type:
18338 case DW_TAG_ptr_to_member_type:
18339 case DW_TAG_set_type:
18340 case DW_TAG_string_type:
18341 case DW_TAG_subroutine_type:
18342 #endif
18343 case DW_TAG_base_type:
18344 case DW_TAG_class_type:
18345 case DW_TAG_interface_type:
18346 case DW_TAG_enumeration_type:
18347 case DW_TAG_structure_type:
18348 case DW_TAG_subrange_type:
18349 case DW_TAG_typedef:
18350 case DW_TAG_union_type:
18351 return 1;
18352 default:
18353 return 0;
18354 }
18355 }
18356
18357 /* Load all DIEs that are interesting for partial symbols into memory. */
18358
18359 static struct partial_die_info *
18360 load_partial_dies (const struct die_reader_specs *reader,
18361 const gdb_byte *info_ptr, int building_psymtab)
18362 {
18363 struct dwarf2_cu *cu = reader->cu;
18364 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18365 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18366 unsigned int bytes_read;
18367 unsigned int load_all = 0;
18368 int nesting_level = 1;
18369
18370 parent_die = NULL;
18371 last_die = NULL;
18372
18373 gdb_assert (cu->per_cu != NULL);
18374 if (cu->per_cu->load_all_dies)
18375 load_all = 1;
18376
18377 cu->partial_dies
18378 = htab_create_alloc_ex (cu->header.length / 12,
18379 partial_die_hash,
18380 partial_die_eq,
18381 NULL,
18382 &cu->comp_unit_obstack,
18383 hashtab_obstack_allocate,
18384 dummy_obstack_deallocate);
18385
18386 while (1)
18387 {
18388 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18389
18390 /* A NULL abbrev means the end of a series of children. */
18391 if (abbrev == NULL)
18392 {
18393 if (--nesting_level == 0)
18394 return first_die;
18395
18396 info_ptr += bytes_read;
18397 last_die = parent_die;
18398 parent_die = parent_die->die_parent;
18399 continue;
18400 }
18401
18402 /* Check for template arguments. We never save these; if
18403 they're seen, we just mark the parent, and go on our way. */
18404 if (parent_die != NULL
18405 && cu->language == language_cplus
18406 && (abbrev->tag == DW_TAG_template_type_param
18407 || abbrev->tag == DW_TAG_template_value_param))
18408 {
18409 parent_die->has_template_arguments = 1;
18410
18411 if (!load_all)
18412 {
18413 /* We don't need a partial DIE for the template argument. */
18414 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18415 continue;
18416 }
18417 }
18418
18419 /* We only recurse into c++ subprograms looking for template arguments.
18420 Skip their other children. */
18421 if (!load_all
18422 && cu->language == language_cplus
18423 && parent_die != NULL
18424 && parent_die->tag == DW_TAG_subprogram)
18425 {
18426 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18427 continue;
18428 }
18429
18430 /* Check whether this DIE is interesting enough to save. Normally
18431 we would not be interested in members here, but there may be
18432 later variables referencing them via DW_AT_specification (for
18433 static members). */
18434 if (!load_all
18435 && !is_type_tag_for_partial (abbrev->tag)
18436 && abbrev->tag != DW_TAG_constant
18437 && abbrev->tag != DW_TAG_enumerator
18438 && abbrev->tag != DW_TAG_subprogram
18439 && abbrev->tag != DW_TAG_inlined_subroutine
18440 && abbrev->tag != DW_TAG_lexical_block
18441 && abbrev->tag != DW_TAG_variable
18442 && abbrev->tag != DW_TAG_namespace
18443 && abbrev->tag != DW_TAG_module
18444 && abbrev->tag != DW_TAG_member
18445 && abbrev->tag != DW_TAG_imported_unit
18446 && abbrev->tag != DW_TAG_imported_declaration)
18447 {
18448 /* Otherwise we skip to the next sibling, if any. */
18449 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18450 continue;
18451 }
18452
18453 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18454 abbrev);
18455
18456 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18457
18458 /* This two-pass algorithm for processing partial symbols has a
18459 high cost in cache pressure. Thus, handle some simple cases
18460 here which cover the majority of C partial symbols. DIEs
18461 which neither have specification tags in them, nor could have
18462 specification tags elsewhere pointing at them, can simply be
18463 processed and discarded.
18464
18465 This segment is also optional; scan_partial_symbols and
18466 add_partial_symbol will handle these DIEs if we chain
18467 them in normally. When compilers which do not emit large
18468 quantities of duplicate debug information are more common,
18469 this code can probably be removed. */
18470
18471 /* Any complete simple types at the top level (pretty much all
18472 of them, for a language without namespaces), can be processed
18473 directly. */
18474 if (parent_die == NULL
18475 && pdi.has_specification == 0
18476 && pdi.is_declaration == 0
18477 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18478 || pdi.tag == DW_TAG_base_type
18479 || pdi.tag == DW_TAG_subrange_type))
18480 {
18481 if (building_psymtab && pdi.name != NULL)
18482 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18483 VAR_DOMAIN, LOC_TYPEDEF, -1,
18484 psymbol_placement::STATIC,
18485 0, cu->language, objfile);
18486 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18487 continue;
18488 }
18489
18490 /* The exception for DW_TAG_typedef with has_children above is
18491 a workaround of GCC PR debug/47510. In the case of this complaint
18492 type_name_or_error will error on such types later.
18493
18494 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18495 it could not find the child DIEs referenced later, this is checked
18496 above. In correct DWARF DW_TAG_typedef should have no children. */
18497
18498 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18499 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18500 "- DIE at %s [in module %s]"),
18501 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18502
18503 /* If we're at the second level, and we're an enumerator, and
18504 our parent has no specification (meaning possibly lives in a
18505 namespace elsewhere), then we can add the partial symbol now
18506 instead of queueing it. */
18507 if (pdi.tag == DW_TAG_enumerator
18508 && parent_die != NULL
18509 && parent_die->die_parent == NULL
18510 && parent_die->tag == DW_TAG_enumeration_type
18511 && parent_die->has_specification == 0)
18512 {
18513 if (pdi.name == NULL)
18514 complaint (_("malformed enumerator DIE ignored"));
18515 else if (building_psymtab)
18516 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18517 VAR_DOMAIN, LOC_CONST, -1,
18518 cu->language == language_cplus
18519 ? psymbol_placement::GLOBAL
18520 : psymbol_placement::STATIC,
18521 0, cu->language, objfile);
18522
18523 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18524 continue;
18525 }
18526
18527 struct partial_die_info *part_die
18528 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18529
18530 /* We'll save this DIE so link it in. */
18531 part_die->die_parent = parent_die;
18532 part_die->die_sibling = NULL;
18533 part_die->die_child = NULL;
18534
18535 if (last_die && last_die == parent_die)
18536 last_die->die_child = part_die;
18537 else if (last_die)
18538 last_die->die_sibling = part_die;
18539
18540 last_die = part_die;
18541
18542 if (first_die == NULL)
18543 first_die = part_die;
18544
18545 /* Maybe add the DIE to the hash table. Not all DIEs that we
18546 find interesting need to be in the hash table, because we
18547 also have the parent/sibling/child chains; only those that we
18548 might refer to by offset later during partial symbol reading.
18549
18550 For now this means things that might have be the target of a
18551 DW_AT_specification, DW_AT_abstract_origin, or
18552 DW_AT_extension. DW_AT_extension will refer only to
18553 namespaces; DW_AT_abstract_origin refers to functions (and
18554 many things under the function DIE, but we do not recurse
18555 into function DIEs during partial symbol reading) and
18556 possibly variables as well; DW_AT_specification refers to
18557 declarations. Declarations ought to have the DW_AT_declaration
18558 flag. It happens that GCC forgets to put it in sometimes, but
18559 only for functions, not for types.
18560
18561 Adding more things than necessary to the hash table is harmless
18562 except for the performance cost. Adding too few will result in
18563 wasted time in find_partial_die, when we reread the compilation
18564 unit with load_all_dies set. */
18565
18566 if (load_all
18567 || abbrev->tag == DW_TAG_constant
18568 || abbrev->tag == DW_TAG_subprogram
18569 || abbrev->tag == DW_TAG_variable
18570 || abbrev->tag == DW_TAG_namespace
18571 || part_die->is_declaration)
18572 {
18573 void **slot;
18574
18575 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18576 to_underlying (part_die->sect_off),
18577 INSERT);
18578 *slot = part_die;
18579 }
18580
18581 /* For some DIEs we want to follow their children (if any). For C
18582 we have no reason to follow the children of structures; for other
18583 languages we have to, so that we can get at method physnames
18584 to infer fully qualified class names, for DW_AT_specification,
18585 and for C++ template arguments. For C++, we also look one level
18586 inside functions to find template arguments (if the name of the
18587 function does not already contain the template arguments).
18588
18589 For Ada, we need to scan the children of subprograms and lexical
18590 blocks as well because Ada allows the definition of nested
18591 entities that could be interesting for the debugger, such as
18592 nested subprograms for instance. */
18593 if (last_die->has_children
18594 && (load_all
18595 || last_die->tag == DW_TAG_namespace
18596 || last_die->tag == DW_TAG_module
18597 || last_die->tag == DW_TAG_enumeration_type
18598 || (cu->language == language_cplus
18599 && last_die->tag == DW_TAG_subprogram
18600 && (last_die->name == NULL
18601 || strchr (last_die->name, '<') == NULL))
18602 || (cu->language != language_c
18603 && (last_die->tag == DW_TAG_class_type
18604 || last_die->tag == DW_TAG_interface_type
18605 || last_die->tag == DW_TAG_structure_type
18606 || last_die->tag == DW_TAG_union_type))
18607 || (cu->language == language_ada
18608 && (last_die->tag == DW_TAG_subprogram
18609 || last_die->tag == DW_TAG_lexical_block))))
18610 {
18611 nesting_level++;
18612 parent_die = last_die;
18613 continue;
18614 }
18615
18616 /* Otherwise we skip to the next sibling, if any. */
18617 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18618
18619 /* Back to the top, do it again. */
18620 }
18621 }
18622
18623 partial_die_info::partial_die_info (sect_offset sect_off_,
18624 struct abbrev_info *abbrev)
18625 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18626 {
18627 }
18628
18629 /* Read a minimal amount of information into the minimal die structure.
18630 INFO_PTR should point just after the initial uleb128 of a DIE. */
18631
18632 const gdb_byte *
18633 partial_die_info::read (const struct die_reader_specs *reader,
18634 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18635 {
18636 struct dwarf2_cu *cu = reader->cu;
18637 struct dwarf2_per_objfile *dwarf2_per_objfile
18638 = cu->per_cu->dwarf2_per_objfile;
18639 unsigned int i;
18640 int has_low_pc_attr = 0;
18641 int has_high_pc_attr = 0;
18642 int high_pc_relative = 0;
18643
18644 for (i = 0; i < abbrev.num_attrs; ++i)
18645 {
18646 struct attribute attr;
18647
18648 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18649
18650 /* Store the data if it is of an attribute we want to keep in a
18651 partial symbol table. */
18652 switch (attr.name)
18653 {
18654 case DW_AT_name:
18655 switch (tag)
18656 {
18657 case DW_TAG_compile_unit:
18658 case DW_TAG_partial_unit:
18659 case DW_TAG_type_unit:
18660 /* Compilation units have a DW_AT_name that is a filename, not
18661 a source language identifier. */
18662 case DW_TAG_enumeration_type:
18663 case DW_TAG_enumerator:
18664 /* These tags always have simple identifiers already; no need
18665 to canonicalize them. */
18666 name = DW_STRING (&attr);
18667 break;
18668 default:
18669 {
18670 struct objfile *objfile = dwarf2_per_objfile->objfile;
18671
18672 name
18673 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18674 &objfile->per_bfd->storage_obstack);
18675 }
18676 break;
18677 }
18678 break;
18679 case DW_AT_linkage_name:
18680 case DW_AT_MIPS_linkage_name:
18681 /* Note that both forms of linkage name might appear. We
18682 assume they will be the same, and we only store the last
18683 one we see. */
18684 if (cu->language == language_ada)
18685 name = DW_STRING (&attr);
18686 linkage_name = DW_STRING (&attr);
18687 break;
18688 case DW_AT_low_pc:
18689 has_low_pc_attr = 1;
18690 lowpc = attr_value_as_address (&attr);
18691 break;
18692 case DW_AT_high_pc:
18693 has_high_pc_attr = 1;
18694 highpc = attr_value_as_address (&attr);
18695 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18696 high_pc_relative = 1;
18697 break;
18698 case DW_AT_location:
18699 /* Support the .debug_loc offsets. */
18700 if (attr_form_is_block (&attr))
18701 {
18702 d.locdesc = DW_BLOCK (&attr);
18703 }
18704 else if (attr_form_is_section_offset (&attr))
18705 {
18706 dwarf2_complex_location_expr_complaint ();
18707 }
18708 else
18709 {
18710 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18711 "partial symbol information");
18712 }
18713 break;
18714 case DW_AT_external:
18715 is_external = DW_UNSND (&attr);
18716 break;
18717 case DW_AT_declaration:
18718 is_declaration = DW_UNSND (&attr);
18719 break;
18720 case DW_AT_type:
18721 has_type = 1;
18722 break;
18723 case DW_AT_abstract_origin:
18724 case DW_AT_specification:
18725 case DW_AT_extension:
18726 has_specification = 1;
18727 spec_offset = dwarf2_get_ref_die_offset (&attr);
18728 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18729 || cu->per_cu->is_dwz);
18730 break;
18731 case DW_AT_sibling:
18732 /* Ignore absolute siblings, they might point outside of
18733 the current compile unit. */
18734 if (attr.form == DW_FORM_ref_addr)
18735 complaint (_("ignoring absolute DW_AT_sibling"));
18736 else
18737 {
18738 const gdb_byte *buffer = reader->buffer;
18739 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18740 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18741
18742 if (sibling_ptr < info_ptr)
18743 complaint (_("DW_AT_sibling points backwards"));
18744 else if (sibling_ptr > reader->buffer_end)
18745 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18746 else
18747 sibling = sibling_ptr;
18748 }
18749 break;
18750 case DW_AT_byte_size:
18751 has_byte_size = 1;
18752 break;
18753 case DW_AT_const_value:
18754 has_const_value = 1;
18755 break;
18756 case DW_AT_calling_convention:
18757 /* DWARF doesn't provide a way to identify a program's source-level
18758 entry point. DW_AT_calling_convention attributes are only meant
18759 to describe functions' calling conventions.
18760
18761 However, because it's a necessary piece of information in
18762 Fortran, and before DWARF 4 DW_CC_program was the only
18763 piece of debugging information whose definition refers to
18764 a 'main program' at all, several compilers marked Fortran
18765 main programs with DW_CC_program --- even when those
18766 functions use the standard calling conventions.
18767
18768 Although DWARF now specifies a way to provide this
18769 information, we support this practice for backward
18770 compatibility. */
18771 if (DW_UNSND (&attr) == DW_CC_program
18772 && cu->language == language_fortran)
18773 main_subprogram = 1;
18774 break;
18775 case DW_AT_inline:
18776 if (DW_UNSND (&attr) == DW_INL_inlined
18777 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18778 may_be_inlined = 1;
18779 break;
18780
18781 case DW_AT_import:
18782 if (tag == DW_TAG_imported_unit)
18783 {
18784 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18785 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18786 || cu->per_cu->is_dwz);
18787 }
18788 break;
18789
18790 case DW_AT_main_subprogram:
18791 main_subprogram = DW_UNSND (&attr);
18792 break;
18793
18794 case DW_AT_ranges:
18795 {
18796 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18797 but that requires a full DIE, so instead we just
18798 reimplement it. */
18799 int need_ranges_base = tag != DW_TAG_compile_unit;
18800 unsigned int ranges_offset = (DW_UNSND (&attr)
18801 + (need_ranges_base
18802 ? cu->ranges_base
18803 : 0));
18804
18805 /* Value of the DW_AT_ranges attribute is the offset in the
18806 .debug_ranges section. */
18807 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18808 nullptr))
18809 has_pc_info = 1;
18810 }
18811 break;
18812
18813 default:
18814 break;
18815 }
18816 }
18817
18818 if (high_pc_relative)
18819 highpc += lowpc;
18820
18821 if (has_low_pc_attr && has_high_pc_attr)
18822 {
18823 /* When using the GNU linker, .gnu.linkonce. sections are used to
18824 eliminate duplicate copies of functions and vtables and such.
18825 The linker will arbitrarily choose one and discard the others.
18826 The AT_*_pc values for such functions refer to local labels in
18827 these sections. If the section from that file was discarded, the
18828 labels are not in the output, so the relocs get a value of 0.
18829 If this is a discarded function, mark the pc bounds as invalid,
18830 so that GDB will ignore it. */
18831 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18832 {
18833 struct objfile *objfile = dwarf2_per_objfile->objfile;
18834 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18835
18836 complaint (_("DW_AT_low_pc %s is zero "
18837 "for DIE at %s [in module %s]"),
18838 paddress (gdbarch, lowpc),
18839 sect_offset_str (sect_off),
18840 objfile_name (objfile));
18841 }
18842 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18843 else if (lowpc >= highpc)
18844 {
18845 struct objfile *objfile = dwarf2_per_objfile->objfile;
18846 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18847
18848 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18849 "for DIE at %s [in module %s]"),
18850 paddress (gdbarch, lowpc),
18851 paddress (gdbarch, highpc),
18852 sect_offset_str (sect_off),
18853 objfile_name (objfile));
18854 }
18855 else
18856 has_pc_info = 1;
18857 }
18858
18859 return info_ptr;
18860 }
18861
18862 /* Find a cached partial DIE at OFFSET in CU. */
18863
18864 struct partial_die_info *
18865 dwarf2_cu::find_partial_die (sect_offset sect_off)
18866 {
18867 struct partial_die_info *lookup_die = NULL;
18868 struct partial_die_info part_die (sect_off);
18869
18870 lookup_die = ((struct partial_die_info *)
18871 htab_find_with_hash (partial_dies, &part_die,
18872 to_underlying (sect_off)));
18873
18874 return lookup_die;
18875 }
18876
18877 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18878 except in the case of .debug_types DIEs which do not reference
18879 outside their CU (they do however referencing other types via
18880 DW_FORM_ref_sig8). */
18881
18882 static struct partial_die_info *
18883 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18884 {
18885 struct dwarf2_per_objfile *dwarf2_per_objfile
18886 = cu->per_cu->dwarf2_per_objfile;
18887 struct objfile *objfile = dwarf2_per_objfile->objfile;
18888 struct dwarf2_per_cu_data *per_cu = NULL;
18889 struct partial_die_info *pd = NULL;
18890
18891 if (offset_in_dwz == cu->per_cu->is_dwz
18892 && offset_in_cu_p (&cu->header, sect_off))
18893 {
18894 pd = cu->find_partial_die (sect_off);
18895 if (pd != NULL)
18896 return pd;
18897 /* We missed recording what we needed.
18898 Load all dies and try again. */
18899 per_cu = cu->per_cu;
18900 }
18901 else
18902 {
18903 /* TUs don't reference other CUs/TUs (except via type signatures). */
18904 if (cu->per_cu->is_debug_types)
18905 {
18906 error (_("Dwarf Error: Type Unit at offset %s contains"
18907 " external reference to offset %s [in module %s].\n"),
18908 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18909 bfd_get_filename (objfile->obfd));
18910 }
18911 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18912 dwarf2_per_objfile);
18913
18914 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18915 load_partial_comp_unit (per_cu);
18916
18917 per_cu->cu->last_used = 0;
18918 pd = per_cu->cu->find_partial_die (sect_off);
18919 }
18920
18921 /* If we didn't find it, and not all dies have been loaded,
18922 load them all and try again. */
18923
18924 if (pd == NULL && per_cu->load_all_dies == 0)
18925 {
18926 per_cu->load_all_dies = 1;
18927
18928 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18929 THIS_CU->cu may already be in use. So we can't just free it and
18930 replace its DIEs with the ones we read in. Instead, we leave those
18931 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18932 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18933 set. */
18934 load_partial_comp_unit (per_cu);
18935
18936 pd = per_cu->cu->find_partial_die (sect_off);
18937 }
18938
18939 if (pd == NULL)
18940 internal_error (__FILE__, __LINE__,
18941 _("could not find partial DIE %s "
18942 "in cache [from module %s]\n"),
18943 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18944 return pd;
18945 }
18946
18947 /* See if we can figure out if the class lives in a namespace. We do
18948 this by looking for a member function; its demangled name will
18949 contain namespace info, if there is any. */
18950
18951 static void
18952 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18953 struct dwarf2_cu *cu)
18954 {
18955 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18956 what template types look like, because the demangler
18957 frequently doesn't give the same name as the debug info. We
18958 could fix this by only using the demangled name to get the
18959 prefix (but see comment in read_structure_type). */
18960
18961 struct partial_die_info *real_pdi;
18962 struct partial_die_info *child_pdi;
18963
18964 /* If this DIE (this DIE's specification, if any) has a parent, then
18965 we should not do this. We'll prepend the parent's fully qualified
18966 name when we create the partial symbol. */
18967
18968 real_pdi = struct_pdi;
18969 while (real_pdi->has_specification)
18970 real_pdi = find_partial_die (real_pdi->spec_offset,
18971 real_pdi->spec_is_dwz, cu);
18972
18973 if (real_pdi->die_parent != NULL)
18974 return;
18975
18976 for (child_pdi = struct_pdi->die_child;
18977 child_pdi != NULL;
18978 child_pdi = child_pdi->die_sibling)
18979 {
18980 if (child_pdi->tag == DW_TAG_subprogram
18981 && child_pdi->linkage_name != NULL)
18982 {
18983 char *actual_class_name
18984 = language_class_name_from_physname (cu->language_defn,
18985 child_pdi->linkage_name);
18986 if (actual_class_name != NULL)
18987 {
18988 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18989 struct_pdi->name
18990 = ((const char *)
18991 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18992 actual_class_name,
18993 strlen (actual_class_name)));
18994 xfree (actual_class_name);
18995 }
18996 break;
18997 }
18998 }
18999 }
19000
19001 void
19002 partial_die_info::fixup (struct dwarf2_cu *cu)
19003 {
19004 /* Once we've fixed up a die, there's no point in doing so again.
19005 This also avoids a memory leak if we were to call
19006 guess_partial_die_structure_name multiple times. */
19007 if (fixup_called)
19008 return;
19009
19010 /* If we found a reference attribute and the DIE has no name, try
19011 to find a name in the referred to DIE. */
19012
19013 if (name == NULL && has_specification)
19014 {
19015 struct partial_die_info *spec_die;
19016
19017 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
19018
19019 spec_die->fixup (cu);
19020
19021 if (spec_die->name)
19022 {
19023 name = spec_die->name;
19024
19025 /* Copy DW_AT_external attribute if it is set. */
19026 if (spec_die->is_external)
19027 is_external = spec_die->is_external;
19028 }
19029 }
19030
19031 /* Set default names for some unnamed DIEs. */
19032
19033 if (name == NULL && tag == DW_TAG_namespace)
19034 name = CP_ANONYMOUS_NAMESPACE_STR;
19035
19036 /* If there is no parent die to provide a namespace, and there are
19037 children, see if we can determine the namespace from their linkage
19038 name. */
19039 if (cu->language == language_cplus
19040 && !VEC_empty (dwarf2_section_info_def,
19041 cu->per_cu->dwarf2_per_objfile->types)
19042 && die_parent == NULL
19043 && has_children
19044 && (tag == DW_TAG_class_type
19045 || tag == DW_TAG_structure_type
19046 || tag == DW_TAG_union_type))
19047 guess_partial_die_structure_name (this, cu);
19048
19049 /* GCC might emit a nameless struct or union that has a linkage
19050 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19051 if (name == NULL
19052 && (tag == DW_TAG_class_type
19053 || tag == DW_TAG_interface_type
19054 || tag == DW_TAG_structure_type
19055 || tag == DW_TAG_union_type)
19056 && linkage_name != NULL)
19057 {
19058 char *demangled;
19059
19060 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19061 if (demangled)
19062 {
19063 const char *base;
19064
19065 /* Strip any leading namespaces/classes, keep only the base name.
19066 DW_AT_name for named DIEs does not contain the prefixes. */
19067 base = strrchr (demangled, ':');
19068 if (base && base > demangled && base[-1] == ':')
19069 base++;
19070 else
19071 base = demangled;
19072
19073 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19074 name
19075 = ((const char *)
19076 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19077 base, strlen (base)));
19078 xfree (demangled);
19079 }
19080 }
19081
19082 fixup_called = 1;
19083 }
19084
19085 /* Read an attribute value described by an attribute form. */
19086
19087 static const gdb_byte *
19088 read_attribute_value (const struct die_reader_specs *reader,
19089 struct attribute *attr, unsigned form,
19090 LONGEST implicit_const, const gdb_byte *info_ptr)
19091 {
19092 struct dwarf2_cu *cu = reader->cu;
19093 struct dwarf2_per_objfile *dwarf2_per_objfile
19094 = cu->per_cu->dwarf2_per_objfile;
19095 struct objfile *objfile = dwarf2_per_objfile->objfile;
19096 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19097 bfd *abfd = reader->abfd;
19098 struct comp_unit_head *cu_header = &cu->header;
19099 unsigned int bytes_read;
19100 struct dwarf_block *blk;
19101
19102 attr->form = (enum dwarf_form) form;
19103 switch (form)
19104 {
19105 case DW_FORM_ref_addr:
19106 if (cu->header.version == 2)
19107 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19108 else
19109 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19110 &cu->header, &bytes_read);
19111 info_ptr += bytes_read;
19112 break;
19113 case DW_FORM_GNU_ref_alt:
19114 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19115 info_ptr += bytes_read;
19116 break;
19117 case DW_FORM_addr:
19118 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19119 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19120 info_ptr += bytes_read;
19121 break;
19122 case DW_FORM_block2:
19123 blk = dwarf_alloc_block (cu);
19124 blk->size = read_2_bytes (abfd, info_ptr);
19125 info_ptr += 2;
19126 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19127 info_ptr += blk->size;
19128 DW_BLOCK (attr) = blk;
19129 break;
19130 case DW_FORM_block4:
19131 blk = dwarf_alloc_block (cu);
19132 blk->size = read_4_bytes (abfd, info_ptr);
19133 info_ptr += 4;
19134 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19135 info_ptr += blk->size;
19136 DW_BLOCK (attr) = blk;
19137 break;
19138 case DW_FORM_data2:
19139 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19140 info_ptr += 2;
19141 break;
19142 case DW_FORM_data4:
19143 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19144 info_ptr += 4;
19145 break;
19146 case DW_FORM_data8:
19147 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19148 info_ptr += 8;
19149 break;
19150 case DW_FORM_data16:
19151 blk = dwarf_alloc_block (cu);
19152 blk->size = 16;
19153 blk->data = read_n_bytes (abfd, info_ptr, 16);
19154 info_ptr += 16;
19155 DW_BLOCK (attr) = blk;
19156 break;
19157 case DW_FORM_sec_offset:
19158 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19159 info_ptr += bytes_read;
19160 break;
19161 case DW_FORM_string:
19162 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19163 DW_STRING_IS_CANONICAL (attr) = 0;
19164 info_ptr += bytes_read;
19165 break;
19166 case DW_FORM_strp:
19167 if (!cu->per_cu->is_dwz)
19168 {
19169 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19170 abfd, info_ptr, cu_header,
19171 &bytes_read);
19172 DW_STRING_IS_CANONICAL (attr) = 0;
19173 info_ptr += bytes_read;
19174 break;
19175 }
19176 /* FALLTHROUGH */
19177 case DW_FORM_line_strp:
19178 if (!cu->per_cu->is_dwz)
19179 {
19180 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19181 abfd, info_ptr,
19182 cu_header, &bytes_read);
19183 DW_STRING_IS_CANONICAL (attr) = 0;
19184 info_ptr += bytes_read;
19185 break;
19186 }
19187 /* FALLTHROUGH */
19188 case DW_FORM_GNU_strp_alt:
19189 {
19190 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19191 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19192 &bytes_read);
19193
19194 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19195 dwz, str_offset);
19196 DW_STRING_IS_CANONICAL (attr) = 0;
19197 info_ptr += bytes_read;
19198 }
19199 break;
19200 case DW_FORM_exprloc:
19201 case DW_FORM_block:
19202 blk = dwarf_alloc_block (cu);
19203 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19204 info_ptr += bytes_read;
19205 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19206 info_ptr += blk->size;
19207 DW_BLOCK (attr) = blk;
19208 break;
19209 case DW_FORM_block1:
19210 blk = dwarf_alloc_block (cu);
19211 blk->size = read_1_byte (abfd, info_ptr);
19212 info_ptr += 1;
19213 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19214 info_ptr += blk->size;
19215 DW_BLOCK (attr) = blk;
19216 break;
19217 case DW_FORM_data1:
19218 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19219 info_ptr += 1;
19220 break;
19221 case DW_FORM_flag:
19222 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19223 info_ptr += 1;
19224 break;
19225 case DW_FORM_flag_present:
19226 DW_UNSND (attr) = 1;
19227 break;
19228 case DW_FORM_sdata:
19229 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19230 info_ptr += bytes_read;
19231 break;
19232 case DW_FORM_udata:
19233 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19234 info_ptr += bytes_read;
19235 break;
19236 case DW_FORM_ref1:
19237 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19238 + read_1_byte (abfd, info_ptr));
19239 info_ptr += 1;
19240 break;
19241 case DW_FORM_ref2:
19242 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19243 + read_2_bytes (abfd, info_ptr));
19244 info_ptr += 2;
19245 break;
19246 case DW_FORM_ref4:
19247 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19248 + read_4_bytes (abfd, info_ptr));
19249 info_ptr += 4;
19250 break;
19251 case DW_FORM_ref8:
19252 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19253 + read_8_bytes (abfd, info_ptr));
19254 info_ptr += 8;
19255 break;
19256 case DW_FORM_ref_sig8:
19257 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19258 info_ptr += 8;
19259 break;
19260 case DW_FORM_ref_udata:
19261 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19262 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19263 info_ptr += bytes_read;
19264 break;
19265 case DW_FORM_indirect:
19266 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19267 info_ptr += bytes_read;
19268 if (form == DW_FORM_implicit_const)
19269 {
19270 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19271 info_ptr += bytes_read;
19272 }
19273 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19274 info_ptr);
19275 break;
19276 case DW_FORM_implicit_const:
19277 DW_SND (attr) = implicit_const;
19278 break;
19279 case DW_FORM_GNU_addr_index:
19280 if (reader->dwo_file == NULL)
19281 {
19282 /* For now flag a hard error.
19283 Later we can turn this into a complaint. */
19284 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19285 dwarf_form_name (form),
19286 bfd_get_filename (abfd));
19287 }
19288 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19289 info_ptr += bytes_read;
19290 break;
19291 case DW_FORM_GNU_str_index:
19292 if (reader->dwo_file == NULL)
19293 {
19294 /* For now flag a hard error.
19295 Later we can turn this into a complaint if warranted. */
19296 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19297 dwarf_form_name (form),
19298 bfd_get_filename (abfd));
19299 }
19300 {
19301 ULONGEST str_index =
19302 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19303
19304 DW_STRING (attr) = read_str_index (reader, str_index);
19305 DW_STRING_IS_CANONICAL (attr) = 0;
19306 info_ptr += bytes_read;
19307 }
19308 break;
19309 default:
19310 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19311 dwarf_form_name (form),
19312 bfd_get_filename (abfd));
19313 }
19314
19315 /* Super hack. */
19316 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19317 attr->form = DW_FORM_GNU_ref_alt;
19318
19319 /* We have seen instances where the compiler tried to emit a byte
19320 size attribute of -1 which ended up being encoded as an unsigned
19321 0xffffffff. Although 0xffffffff is technically a valid size value,
19322 an object of this size seems pretty unlikely so we can relatively
19323 safely treat these cases as if the size attribute was invalid and
19324 treat them as zero by default. */
19325 if (attr->name == DW_AT_byte_size
19326 && form == DW_FORM_data4
19327 && DW_UNSND (attr) >= 0xffffffff)
19328 {
19329 complaint
19330 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19331 hex_string (DW_UNSND (attr)));
19332 DW_UNSND (attr) = 0;
19333 }
19334
19335 return info_ptr;
19336 }
19337
19338 /* Read an attribute described by an abbreviated attribute. */
19339
19340 static const gdb_byte *
19341 read_attribute (const struct die_reader_specs *reader,
19342 struct attribute *attr, struct attr_abbrev *abbrev,
19343 const gdb_byte *info_ptr)
19344 {
19345 attr->name = abbrev->name;
19346 return read_attribute_value (reader, attr, abbrev->form,
19347 abbrev->implicit_const, info_ptr);
19348 }
19349
19350 /* Read dwarf information from a buffer. */
19351
19352 static unsigned int
19353 read_1_byte (bfd *abfd, const gdb_byte *buf)
19354 {
19355 return bfd_get_8 (abfd, buf);
19356 }
19357
19358 static int
19359 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19360 {
19361 return bfd_get_signed_8 (abfd, buf);
19362 }
19363
19364 static unsigned int
19365 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19366 {
19367 return bfd_get_16 (abfd, buf);
19368 }
19369
19370 static int
19371 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19372 {
19373 return bfd_get_signed_16 (abfd, buf);
19374 }
19375
19376 static unsigned int
19377 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19378 {
19379 return bfd_get_32 (abfd, buf);
19380 }
19381
19382 static int
19383 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19384 {
19385 return bfd_get_signed_32 (abfd, buf);
19386 }
19387
19388 static ULONGEST
19389 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19390 {
19391 return bfd_get_64 (abfd, buf);
19392 }
19393
19394 static CORE_ADDR
19395 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19396 unsigned int *bytes_read)
19397 {
19398 struct comp_unit_head *cu_header = &cu->header;
19399 CORE_ADDR retval = 0;
19400
19401 if (cu_header->signed_addr_p)
19402 {
19403 switch (cu_header->addr_size)
19404 {
19405 case 2:
19406 retval = bfd_get_signed_16 (abfd, buf);
19407 break;
19408 case 4:
19409 retval = bfd_get_signed_32 (abfd, buf);
19410 break;
19411 case 8:
19412 retval = bfd_get_signed_64 (abfd, buf);
19413 break;
19414 default:
19415 internal_error (__FILE__, __LINE__,
19416 _("read_address: bad switch, signed [in module %s]"),
19417 bfd_get_filename (abfd));
19418 }
19419 }
19420 else
19421 {
19422 switch (cu_header->addr_size)
19423 {
19424 case 2:
19425 retval = bfd_get_16 (abfd, buf);
19426 break;
19427 case 4:
19428 retval = bfd_get_32 (abfd, buf);
19429 break;
19430 case 8:
19431 retval = bfd_get_64 (abfd, buf);
19432 break;
19433 default:
19434 internal_error (__FILE__, __LINE__,
19435 _("read_address: bad switch, "
19436 "unsigned [in module %s]"),
19437 bfd_get_filename (abfd));
19438 }
19439 }
19440
19441 *bytes_read = cu_header->addr_size;
19442 return retval;
19443 }
19444
19445 /* Read the initial length from a section. The (draft) DWARF 3
19446 specification allows the initial length to take up either 4 bytes
19447 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19448 bytes describe the length and all offsets will be 8 bytes in length
19449 instead of 4.
19450
19451 An older, non-standard 64-bit format is also handled by this
19452 function. The older format in question stores the initial length
19453 as an 8-byte quantity without an escape value. Lengths greater
19454 than 2^32 aren't very common which means that the initial 4 bytes
19455 is almost always zero. Since a length value of zero doesn't make
19456 sense for the 32-bit format, this initial zero can be considered to
19457 be an escape value which indicates the presence of the older 64-bit
19458 format. As written, the code can't detect (old format) lengths
19459 greater than 4GB. If it becomes necessary to handle lengths
19460 somewhat larger than 4GB, we could allow other small values (such
19461 as the non-sensical values of 1, 2, and 3) to also be used as
19462 escape values indicating the presence of the old format.
19463
19464 The value returned via bytes_read should be used to increment the
19465 relevant pointer after calling read_initial_length().
19466
19467 [ Note: read_initial_length() and read_offset() are based on the
19468 document entitled "DWARF Debugging Information Format", revision
19469 3, draft 8, dated November 19, 2001. This document was obtained
19470 from:
19471
19472 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19473
19474 This document is only a draft and is subject to change. (So beware.)
19475
19476 Details regarding the older, non-standard 64-bit format were
19477 determined empirically by examining 64-bit ELF files produced by
19478 the SGI toolchain on an IRIX 6.5 machine.
19479
19480 - Kevin, July 16, 2002
19481 ] */
19482
19483 static LONGEST
19484 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19485 {
19486 LONGEST length = bfd_get_32 (abfd, buf);
19487
19488 if (length == 0xffffffff)
19489 {
19490 length = bfd_get_64 (abfd, buf + 4);
19491 *bytes_read = 12;
19492 }
19493 else if (length == 0)
19494 {
19495 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19496 length = bfd_get_64 (abfd, buf);
19497 *bytes_read = 8;
19498 }
19499 else
19500 {
19501 *bytes_read = 4;
19502 }
19503
19504 return length;
19505 }
19506
19507 /* Cover function for read_initial_length.
19508 Returns the length of the object at BUF, and stores the size of the
19509 initial length in *BYTES_READ and stores the size that offsets will be in
19510 *OFFSET_SIZE.
19511 If the initial length size is not equivalent to that specified in
19512 CU_HEADER then issue a complaint.
19513 This is useful when reading non-comp-unit headers. */
19514
19515 static LONGEST
19516 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19517 const struct comp_unit_head *cu_header,
19518 unsigned int *bytes_read,
19519 unsigned int *offset_size)
19520 {
19521 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19522
19523 gdb_assert (cu_header->initial_length_size == 4
19524 || cu_header->initial_length_size == 8
19525 || cu_header->initial_length_size == 12);
19526
19527 if (cu_header->initial_length_size != *bytes_read)
19528 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19529
19530 *offset_size = (*bytes_read == 4) ? 4 : 8;
19531 return length;
19532 }
19533
19534 /* Read an offset from the data stream. The size of the offset is
19535 given by cu_header->offset_size. */
19536
19537 static LONGEST
19538 read_offset (bfd *abfd, const gdb_byte *buf,
19539 const struct comp_unit_head *cu_header,
19540 unsigned int *bytes_read)
19541 {
19542 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19543
19544 *bytes_read = cu_header->offset_size;
19545 return offset;
19546 }
19547
19548 /* Read an offset from the data stream. */
19549
19550 static LONGEST
19551 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19552 {
19553 LONGEST retval = 0;
19554
19555 switch (offset_size)
19556 {
19557 case 4:
19558 retval = bfd_get_32 (abfd, buf);
19559 break;
19560 case 8:
19561 retval = bfd_get_64 (abfd, buf);
19562 break;
19563 default:
19564 internal_error (__FILE__, __LINE__,
19565 _("read_offset_1: bad switch [in module %s]"),
19566 bfd_get_filename (abfd));
19567 }
19568
19569 return retval;
19570 }
19571
19572 static const gdb_byte *
19573 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19574 {
19575 /* If the size of a host char is 8 bits, we can return a pointer
19576 to the buffer, otherwise we have to copy the data to a buffer
19577 allocated on the temporary obstack. */
19578 gdb_assert (HOST_CHAR_BIT == 8);
19579 return buf;
19580 }
19581
19582 static const char *
19583 read_direct_string (bfd *abfd, const gdb_byte *buf,
19584 unsigned int *bytes_read_ptr)
19585 {
19586 /* If the size of a host char is 8 bits, we can return a pointer
19587 to the string, otherwise we have to copy the string to a buffer
19588 allocated on the temporary obstack. */
19589 gdb_assert (HOST_CHAR_BIT == 8);
19590 if (*buf == '\0')
19591 {
19592 *bytes_read_ptr = 1;
19593 return NULL;
19594 }
19595 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19596 return (const char *) buf;
19597 }
19598
19599 /* Return pointer to string at section SECT offset STR_OFFSET with error
19600 reporting strings FORM_NAME and SECT_NAME. */
19601
19602 static const char *
19603 read_indirect_string_at_offset_from (struct objfile *objfile,
19604 bfd *abfd, LONGEST str_offset,
19605 struct dwarf2_section_info *sect,
19606 const char *form_name,
19607 const char *sect_name)
19608 {
19609 dwarf2_read_section (objfile, sect);
19610 if (sect->buffer == NULL)
19611 error (_("%s used without %s section [in module %s]"),
19612 form_name, sect_name, bfd_get_filename (abfd));
19613 if (str_offset >= sect->size)
19614 error (_("%s pointing outside of %s section [in module %s]"),
19615 form_name, sect_name, bfd_get_filename (abfd));
19616 gdb_assert (HOST_CHAR_BIT == 8);
19617 if (sect->buffer[str_offset] == '\0')
19618 return NULL;
19619 return (const char *) (sect->buffer + str_offset);
19620 }
19621
19622 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19623
19624 static const char *
19625 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19626 bfd *abfd, LONGEST str_offset)
19627 {
19628 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19629 abfd, str_offset,
19630 &dwarf2_per_objfile->str,
19631 "DW_FORM_strp", ".debug_str");
19632 }
19633
19634 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19635
19636 static const char *
19637 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19638 bfd *abfd, LONGEST str_offset)
19639 {
19640 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19641 abfd, str_offset,
19642 &dwarf2_per_objfile->line_str,
19643 "DW_FORM_line_strp",
19644 ".debug_line_str");
19645 }
19646
19647 /* Read a string at offset STR_OFFSET in the .debug_str section from
19648 the .dwz file DWZ. Throw an error if the offset is too large. If
19649 the string consists of a single NUL byte, return NULL; otherwise
19650 return a pointer to the string. */
19651
19652 static const char *
19653 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19654 LONGEST str_offset)
19655 {
19656 dwarf2_read_section (objfile, &dwz->str);
19657
19658 if (dwz->str.buffer == NULL)
19659 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19660 "section [in module %s]"),
19661 bfd_get_filename (dwz->dwz_bfd));
19662 if (str_offset >= dwz->str.size)
19663 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19664 ".debug_str section [in module %s]"),
19665 bfd_get_filename (dwz->dwz_bfd));
19666 gdb_assert (HOST_CHAR_BIT == 8);
19667 if (dwz->str.buffer[str_offset] == '\0')
19668 return NULL;
19669 return (const char *) (dwz->str.buffer + str_offset);
19670 }
19671
19672 /* Return pointer to string at .debug_str offset as read from BUF.
19673 BUF is assumed to be in a compilation unit described by CU_HEADER.
19674 Return *BYTES_READ_PTR count of bytes read from BUF. */
19675
19676 static const char *
19677 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19678 const gdb_byte *buf,
19679 const struct comp_unit_head *cu_header,
19680 unsigned int *bytes_read_ptr)
19681 {
19682 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19683
19684 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19685 }
19686
19687 /* Return pointer to string at .debug_line_str offset as read from BUF.
19688 BUF is assumed to be in a compilation unit described by CU_HEADER.
19689 Return *BYTES_READ_PTR count of bytes read from BUF. */
19690
19691 static const char *
19692 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19693 bfd *abfd, const gdb_byte *buf,
19694 const struct comp_unit_head *cu_header,
19695 unsigned int *bytes_read_ptr)
19696 {
19697 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19698
19699 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19700 str_offset);
19701 }
19702
19703 ULONGEST
19704 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19705 unsigned int *bytes_read_ptr)
19706 {
19707 ULONGEST result;
19708 unsigned int num_read;
19709 int shift;
19710 unsigned char byte;
19711
19712 result = 0;
19713 shift = 0;
19714 num_read = 0;
19715 while (1)
19716 {
19717 byte = bfd_get_8 (abfd, buf);
19718 buf++;
19719 num_read++;
19720 result |= ((ULONGEST) (byte & 127) << shift);
19721 if ((byte & 128) == 0)
19722 {
19723 break;
19724 }
19725 shift += 7;
19726 }
19727 *bytes_read_ptr = num_read;
19728 return result;
19729 }
19730
19731 static LONGEST
19732 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19733 unsigned int *bytes_read_ptr)
19734 {
19735 ULONGEST result;
19736 int shift, num_read;
19737 unsigned char byte;
19738
19739 result = 0;
19740 shift = 0;
19741 num_read = 0;
19742 while (1)
19743 {
19744 byte = bfd_get_8 (abfd, buf);
19745 buf++;
19746 num_read++;
19747 result |= ((ULONGEST) (byte & 127) << shift);
19748 shift += 7;
19749 if ((byte & 128) == 0)
19750 {
19751 break;
19752 }
19753 }
19754 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19755 result |= -(((ULONGEST) 1) << shift);
19756 *bytes_read_ptr = num_read;
19757 return result;
19758 }
19759
19760 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19761 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19762 ADDR_SIZE is the size of addresses from the CU header. */
19763
19764 static CORE_ADDR
19765 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19766 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19767 {
19768 struct objfile *objfile = dwarf2_per_objfile->objfile;
19769 bfd *abfd = objfile->obfd;
19770 const gdb_byte *info_ptr;
19771
19772 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19773 if (dwarf2_per_objfile->addr.buffer == NULL)
19774 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19775 objfile_name (objfile));
19776 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19777 error (_("DW_FORM_addr_index pointing outside of "
19778 ".debug_addr section [in module %s]"),
19779 objfile_name (objfile));
19780 info_ptr = (dwarf2_per_objfile->addr.buffer
19781 + addr_base + addr_index * addr_size);
19782 if (addr_size == 4)
19783 return bfd_get_32 (abfd, info_ptr);
19784 else
19785 return bfd_get_64 (abfd, info_ptr);
19786 }
19787
19788 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19789
19790 static CORE_ADDR
19791 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19792 {
19793 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19794 cu->addr_base, cu->header.addr_size);
19795 }
19796
19797 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19798
19799 static CORE_ADDR
19800 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19801 unsigned int *bytes_read)
19802 {
19803 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19804 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19805
19806 return read_addr_index (cu, addr_index);
19807 }
19808
19809 /* Data structure to pass results from dwarf2_read_addr_index_reader
19810 back to dwarf2_read_addr_index. */
19811
19812 struct dwarf2_read_addr_index_data
19813 {
19814 ULONGEST addr_base;
19815 int addr_size;
19816 };
19817
19818 /* die_reader_func for dwarf2_read_addr_index. */
19819
19820 static void
19821 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19822 const gdb_byte *info_ptr,
19823 struct die_info *comp_unit_die,
19824 int has_children,
19825 void *data)
19826 {
19827 struct dwarf2_cu *cu = reader->cu;
19828 struct dwarf2_read_addr_index_data *aidata =
19829 (struct dwarf2_read_addr_index_data *) data;
19830
19831 aidata->addr_base = cu->addr_base;
19832 aidata->addr_size = cu->header.addr_size;
19833 }
19834
19835 /* Given an index in .debug_addr, fetch the value.
19836 NOTE: This can be called during dwarf expression evaluation,
19837 long after the debug information has been read, and thus per_cu->cu
19838 may no longer exist. */
19839
19840 CORE_ADDR
19841 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19842 unsigned int addr_index)
19843 {
19844 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19845 struct dwarf2_cu *cu = per_cu->cu;
19846 ULONGEST addr_base;
19847 int addr_size;
19848
19849 /* We need addr_base and addr_size.
19850 If we don't have PER_CU->cu, we have to get it.
19851 Nasty, but the alternative is storing the needed info in PER_CU,
19852 which at this point doesn't seem justified: it's not clear how frequently
19853 it would get used and it would increase the size of every PER_CU.
19854 Entry points like dwarf2_per_cu_addr_size do a similar thing
19855 so we're not in uncharted territory here.
19856 Alas we need to be a bit more complicated as addr_base is contained
19857 in the DIE.
19858
19859 We don't need to read the entire CU(/TU).
19860 We just need the header and top level die.
19861
19862 IWBN to use the aging mechanism to let us lazily later discard the CU.
19863 For now we skip this optimization. */
19864
19865 if (cu != NULL)
19866 {
19867 addr_base = cu->addr_base;
19868 addr_size = cu->header.addr_size;
19869 }
19870 else
19871 {
19872 struct dwarf2_read_addr_index_data aidata;
19873
19874 /* Note: We can't use init_cutu_and_read_dies_simple here,
19875 we need addr_base. */
19876 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
19877 dwarf2_read_addr_index_reader, &aidata);
19878 addr_base = aidata.addr_base;
19879 addr_size = aidata.addr_size;
19880 }
19881
19882 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19883 addr_size);
19884 }
19885
19886 /* Given a DW_FORM_GNU_str_index, fetch the string.
19887 This is only used by the Fission support. */
19888
19889 static const char *
19890 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19891 {
19892 struct dwarf2_cu *cu = reader->cu;
19893 struct dwarf2_per_objfile *dwarf2_per_objfile
19894 = cu->per_cu->dwarf2_per_objfile;
19895 struct objfile *objfile = dwarf2_per_objfile->objfile;
19896 const char *objf_name = objfile_name (objfile);
19897 bfd *abfd = objfile->obfd;
19898 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19899 struct dwarf2_section_info *str_offsets_section =
19900 &reader->dwo_file->sections.str_offsets;
19901 const gdb_byte *info_ptr;
19902 ULONGEST str_offset;
19903 static const char form_name[] = "DW_FORM_GNU_str_index";
19904
19905 dwarf2_read_section (objfile, str_section);
19906 dwarf2_read_section (objfile, str_offsets_section);
19907 if (str_section->buffer == NULL)
19908 error (_("%s used without .debug_str.dwo section"
19909 " in CU at offset %s [in module %s]"),
19910 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19911 if (str_offsets_section->buffer == NULL)
19912 error (_("%s used without .debug_str_offsets.dwo section"
19913 " in CU at offset %s [in module %s]"),
19914 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19915 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19916 error (_("%s pointing outside of .debug_str_offsets.dwo"
19917 " section in CU at offset %s [in module %s]"),
19918 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19919 info_ptr = (str_offsets_section->buffer
19920 + str_index * cu->header.offset_size);
19921 if (cu->header.offset_size == 4)
19922 str_offset = bfd_get_32 (abfd, info_ptr);
19923 else
19924 str_offset = bfd_get_64 (abfd, info_ptr);
19925 if (str_offset >= str_section->size)
19926 error (_("Offset from %s pointing outside of"
19927 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19928 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19929 return (const char *) (str_section->buffer + str_offset);
19930 }
19931
19932 /* Return the length of an LEB128 number in BUF. */
19933
19934 static int
19935 leb128_size (const gdb_byte *buf)
19936 {
19937 const gdb_byte *begin = buf;
19938 gdb_byte byte;
19939
19940 while (1)
19941 {
19942 byte = *buf++;
19943 if ((byte & 128) == 0)
19944 return buf - begin;
19945 }
19946 }
19947
19948 static void
19949 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19950 {
19951 switch (lang)
19952 {
19953 case DW_LANG_C89:
19954 case DW_LANG_C99:
19955 case DW_LANG_C11:
19956 case DW_LANG_C:
19957 case DW_LANG_UPC:
19958 cu->language = language_c;
19959 break;
19960 case DW_LANG_Java:
19961 case DW_LANG_C_plus_plus:
19962 case DW_LANG_C_plus_plus_11:
19963 case DW_LANG_C_plus_plus_14:
19964 cu->language = language_cplus;
19965 break;
19966 case DW_LANG_D:
19967 cu->language = language_d;
19968 break;
19969 case DW_LANG_Fortran77:
19970 case DW_LANG_Fortran90:
19971 case DW_LANG_Fortran95:
19972 case DW_LANG_Fortran03:
19973 case DW_LANG_Fortran08:
19974 cu->language = language_fortran;
19975 break;
19976 case DW_LANG_Go:
19977 cu->language = language_go;
19978 break;
19979 case DW_LANG_Mips_Assembler:
19980 cu->language = language_asm;
19981 break;
19982 case DW_LANG_Ada83:
19983 case DW_LANG_Ada95:
19984 cu->language = language_ada;
19985 break;
19986 case DW_LANG_Modula2:
19987 cu->language = language_m2;
19988 break;
19989 case DW_LANG_Pascal83:
19990 cu->language = language_pascal;
19991 break;
19992 case DW_LANG_ObjC:
19993 cu->language = language_objc;
19994 break;
19995 case DW_LANG_Rust:
19996 case DW_LANG_Rust_old:
19997 cu->language = language_rust;
19998 break;
19999 case DW_LANG_Cobol74:
20000 case DW_LANG_Cobol85:
20001 default:
20002 cu->language = language_minimal;
20003 break;
20004 }
20005 cu->language_defn = language_def (cu->language);
20006 }
20007
20008 /* Return the named attribute or NULL if not there. */
20009
20010 static struct attribute *
20011 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20012 {
20013 for (;;)
20014 {
20015 unsigned int i;
20016 struct attribute *spec = NULL;
20017
20018 for (i = 0; i < die->num_attrs; ++i)
20019 {
20020 if (die->attrs[i].name == name)
20021 return &die->attrs[i];
20022 if (die->attrs[i].name == DW_AT_specification
20023 || die->attrs[i].name == DW_AT_abstract_origin)
20024 spec = &die->attrs[i];
20025 }
20026
20027 if (!spec)
20028 break;
20029
20030 die = follow_die_ref (die, spec, &cu);
20031 }
20032
20033 return NULL;
20034 }
20035
20036 /* Return the named attribute or NULL if not there,
20037 but do not follow DW_AT_specification, etc.
20038 This is for use in contexts where we're reading .debug_types dies.
20039 Following DW_AT_specification, DW_AT_abstract_origin will take us
20040 back up the chain, and we want to go down. */
20041
20042 static struct attribute *
20043 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20044 {
20045 unsigned int i;
20046
20047 for (i = 0; i < die->num_attrs; ++i)
20048 if (die->attrs[i].name == name)
20049 return &die->attrs[i];
20050
20051 return NULL;
20052 }
20053
20054 /* Return the string associated with a string-typed attribute, or NULL if it
20055 is either not found or is of an incorrect type. */
20056
20057 static const char *
20058 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20059 {
20060 struct attribute *attr;
20061 const char *str = NULL;
20062
20063 attr = dwarf2_attr (die, name, cu);
20064
20065 if (attr != NULL)
20066 {
20067 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20068 || attr->form == DW_FORM_string
20069 || attr->form == DW_FORM_GNU_str_index
20070 || attr->form == DW_FORM_GNU_strp_alt)
20071 str = DW_STRING (attr);
20072 else
20073 complaint (_("string type expected for attribute %s for "
20074 "DIE at %s in module %s"),
20075 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20076 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20077 }
20078
20079 return str;
20080 }
20081
20082 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20083 and holds a non-zero value. This function should only be used for
20084 DW_FORM_flag or DW_FORM_flag_present attributes. */
20085
20086 static int
20087 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20088 {
20089 struct attribute *attr = dwarf2_attr (die, name, cu);
20090
20091 return (attr && DW_UNSND (attr));
20092 }
20093
20094 static int
20095 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20096 {
20097 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20098 which value is non-zero. However, we have to be careful with
20099 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20100 (via dwarf2_flag_true_p) follows this attribute. So we may
20101 end up accidently finding a declaration attribute that belongs
20102 to a different DIE referenced by the specification attribute,
20103 even though the given DIE does not have a declaration attribute. */
20104 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20105 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20106 }
20107
20108 /* Return the die giving the specification for DIE, if there is
20109 one. *SPEC_CU is the CU containing DIE on input, and the CU
20110 containing the return value on output. If there is no
20111 specification, but there is an abstract origin, that is
20112 returned. */
20113
20114 static struct die_info *
20115 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20116 {
20117 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20118 *spec_cu);
20119
20120 if (spec_attr == NULL)
20121 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20122
20123 if (spec_attr == NULL)
20124 return NULL;
20125 else
20126 return follow_die_ref (die, spec_attr, spec_cu);
20127 }
20128
20129 /* Stub for free_line_header to match void * callback types. */
20130
20131 static void
20132 free_line_header_voidp (void *arg)
20133 {
20134 struct line_header *lh = (struct line_header *) arg;
20135
20136 delete lh;
20137 }
20138
20139 void
20140 line_header::add_include_dir (const char *include_dir)
20141 {
20142 if (dwarf_line_debug >= 2)
20143 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20144 include_dirs.size () + 1, include_dir);
20145
20146 include_dirs.push_back (include_dir);
20147 }
20148
20149 void
20150 line_header::add_file_name (const char *name,
20151 dir_index d_index,
20152 unsigned int mod_time,
20153 unsigned int length)
20154 {
20155 if (dwarf_line_debug >= 2)
20156 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20157 (unsigned) file_names.size () + 1, name);
20158
20159 file_names.emplace_back (name, d_index, mod_time, length);
20160 }
20161
20162 /* A convenience function to find the proper .debug_line section for a CU. */
20163
20164 static struct dwarf2_section_info *
20165 get_debug_line_section (struct dwarf2_cu *cu)
20166 {
20167 struct dwarf2_section_info *section;
20168 struct dwarf2_per_objfile *dwarf2_per_objfile
20169 = cu->per_cu->dwarf2_per_objfile;
20170
20171 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20172 DWO file. */
20173 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20174 section = &cu->dwo_unit->dwo_file->sections.line;
20175 else if (cu->per_cu->is_dwz)
20176 {
20177 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20178
20179 section = &dwz->line;
20180 }
20181 else
20182 section = &dwarf2_per_objfile->line;
20183
20184 return section;
20185 }
20186
20187 /* Read directory or file name entry format, starting with byte of
20188 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20189 entries count and the entries themselves in the described entry
20190 format. */
20191
20192 static void
20193 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20194 bfd *abfd, const gdb_byte **bufp,
20195 struct line_header *lh,
20196 const struct comp_unit_head *cu_header,
20197 void (*callback) (struct line_header *lh,
20198 const char *name,
20199 dir_index d_index,
20200 unsigned int mod_time,
20201 unsigned int length))
20202 {
20203 gdb_byte format_count, formati;
20204 ULONGEST data_count, datai;
20205 const gdb_byte *buf = *bufp;
20206 const gdb_byte *format_header_data;
20207 unsigned int bytes_read;
20208
20209 format_count = read_1_byte (abfd, buf);
20210 buf += 1;
20211 format_header_data = buf;
20212 for (formati = 0; formati < format_count; formati++)
20213 {
20214 read_unsigned_leb128 (abfd, buf, &bytes_read);
20215 buf += bytes_read;
20216 read_unsigned_leb128 (abfd, buf, &bytes_read);
20217 buf += bytes_read;
20218 }
20219
20220 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20221 buf += bytes_read;
20222 for (datai = 0; datai < data_count; datai++)
20223 {
20224 const gdb_byte *format = format_header_data;
20225 struct file_entry fe;
20226
20227 for (formati = 0; formati < format_count; formati++)
20228 {
20229 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20230 format += bytes_read;
20231
20232 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20233 format += bytes_read;
20234
20235 gdb::optional<const char *> string;
20236 gdb::optional<unsigned int> uint;
20237
20238 switch (form)
20239 {
20240 case DW_FORM_string:
20241 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20242 buf += bytes_read;
20243 break;
20244
20245 case DW_FORM_line_strp:
20246 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20247 abfd, buf,
20248 cu_header,
20249 &bytes_read));
20250 buf += bytes_read;
20251 break;
20252
20253 case DW_FORM_data1:
20254 uint.emplace (read_1_byte (abfd, buf));
20255 buf += 1;
20256 break;
20257
20258 case DW_FORM_data2:
20259 uint.emplace (read_2_bytes (abfd, buf));
20260 buf += 2;
20261 break;
20262
20263 case DW_FORM_data4:
20264 uint.emplace (read_4_bytes (abfd, buf));
20265 buf += 4;
20266 break;
20267
20268 case DW_FORM_data8:
20269 uint.emplace (read_8_bytes (abfd, buf));
20270 buf += 8;
20271 break;
20272
20273 case DW_FORM_udata:
20274 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20275 buf += bytes_read;
20276 break;
20277
20278 case DW_FORM_block:
20279 /* It is valid only for DW_LNCT_timestamp which is ignored by
20280 current GDB. */
20281 break;
20282 }
20283
20284 switch (content_type)
20285 {
20286 case DW_LNCT_path:
20287 if (string.has_value ())
20288 fe.name = *string;
20289 break;
20290 case DW_LNCT_directory_index:
20291 if (uint.has_value ())
20292 fe.d_index = (dir_index) *uint;
20293 break;
20294 case DW_LNCT_timestamp:
20295 if (uint.has_value ())
20296 fe.mod_time = *uint;
20297 break;
20298 case DW_LNCT_size:
20299 if (uint.has_value ())
20300 fe.length = *uint;
20301 break;
20302 case DW_LNCT_MD5:
20303 break;
20304 default:
20305 complaint (_("Unknown format content type %s"),
20306 pulongest (content_type));
20307 }
20308 }
20309
20310 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20311 }
20312
20313 *bufp = buf;
20314 }
20315
20316 /* Read the statement program header starting at OFFSET in
20317 .debug_line, or .debug_line.dwo. Return a pointer
20318 to a struct line_header, allocated using xmalloc.
20319 Returns NULL if there is a problem reading the header, e.g., if it
20320 has a version we don't understand.
20321
20322 NOTE: the strings in the include directory and file name tables of
20323 the returned object point into the dwarf line section buffer,
20324 and must not be freed. */
20325
20326 static line_header_up
20327 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20328 {
20329 const gdb_byte *line_ptr;
20330 unsigned int bytes_read, offset_size;
20331 int i;
20332 const char *cur_dir, *cur_file;
20333 struct dwarf2_section_info *section;
20334 bfd *abfd;
20335 struct dwarf2_per_objfile *dwarf2_per_objfile
20336 = cu->per_cu->dwarf2_per_objfile;
20337
20338 section = get_debug_line_section (cu);
20339 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20340 if (section->buffer == NULL)
20341 {
20342 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20343 complaint (_("missing .debug_line.dwo section"));
20344 else
20345 complaint (_("missing .debug_line section"));
20346 return 0;
20347 }
20348
20349 /* We can't do this until we know the section is non-empty.
20350 Only then do we know we have such a section. */
20351 abfd = get_section_bfd_owner (section);
20352
20353 /* Make sure that at least there's room for the total_length field.
20354 That could be 12 bytes long, but we're just going to fudge that. */
20355 if (to_underlying (sect_off) + 4 >= section->size)
20356 {
20357 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20358 return 0;
20359 }
20360
20361 line_header_up lh (new line_header ());
20362
20363 lh->sect_off = sect_off;
20364 lh->offset_in_dwz = cu->per_cu->is_dwz;
20365
20366 line_ptr = section->buffer + to_underlying (sect_off);
20367
20368 /* Read in the header. */
20369 lh->total_length =
20370 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20371 &bytes_read, &offset_size);
20372 line_ptr += bytes_read;
20373 if (line_ptr + lh->total_length > (section->buffer + section->size))
20374 {
20375 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20376 return 0;
20377 }
20378 lh->statement_program_end = line_ptr + lh->total_length;
20379 lh->version = read_2_bytes (abfd, line_ptr);
20380 line_ptr += 2;
20381 if (lh->version > 5)
20382 {
20383 /* This is a version we don't understand. The format could have
20384 changed in ways we don't handle properly so just punt. */
20385 complaint (_("unsupported version in .debug_line section"));
20386 return NULL;
20387 }
20388 if (lh->version >= 5)
20389 {
20390 gdb_byte segment_selector_size;
20391
20392 /* Skip address size. */
20393 read_1_byte (abfd, line_ptr);
20394 line_ptr += 1;
20395
20396 segment_selector_size = read_1_byte (abfd, line_ptr);
20397 line_ptr += 1;
20398 if (segment_selector_size != 0)
20399 {
20400 complaint (_("unsupported segment selector size %u "
20401 "in .debug_line section"),
20402 segment_selector_size);
20403 return NULL;
20404 }
20405 }
20406 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20407 line_ptr += offset_size;
20408 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20409 line_ptr += 1;
20410 if (lh->version >= 4)
20411 {
20412 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20413 line_ptr += 1;
20414 }
20415 else
20416 lh->maximum_ops_per_instruction = 1;
20417
20418 if (lh->maximum_ops_per_instruction == 0)
20419 {
20420 lh->maximum_ops_per_instruction = 1;
20421 complaint (_("invalid maximum_ops_per_instruction "
20422 "in `.debug_line' section"));
20423 }
20424
20425 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20426 line_ptr += 1;
20427 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20428 line_ptr += 1;
20429 lh->line_range = read_1_byte (abfd, line_ptr);
20430 line_ptr += 1;
20431 lh->opcode_base = read_1_byte (abfd, line_ptr);
20432 line_ptr += 1;
20433 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20434
20435 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20436 for (i = 1; i < lh->opcode_base; ++i)
20437 {
20438 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20439 line_ptr += 1;
20440 }
20441
20442 if (lh->version >= 5)
20443 {
20444 /* Read directory table. */
20445 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20446 &cu->header,
20447 [] (struct line_header *header, const char *name,
20448 dir_index d_index, unsigned int mod_time,
20449 unsigned int length)
20450 {
20451 header->add_include_dir (name);
20452 });
20453
20454 /* Read file name table. */
20455 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20456 &cu->header,
20457 [] (struct line_header *header, const char *name,
20458 dir_index d_index, unsigned int mod_time,
20459 unsigned int length)
20460 {
20461 header->add_file_name (name, d_index, mod_time, length);
20462 });
20463 }
20464 else
20465 {
20466 /* Read directory table. */
20467 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20468 {
20469 line_ptr += bytes_read;
20470 lh->add_include_dir (cur_dir);
20471 }
20472 line_ptr += bytes_read;
20473
20474 /* Read file name table. */
20475 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20476 {
20477 unsigned int mod_time, length;
20478 dir_index d_index;
20479
20480 line_ptr += bytes_read;
20481 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20482 line_ptr += bytes_read;
20483 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20484 line_ptr += bytes_read;
20485 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20486 line_ptr += bytes_read;
20487
20488 lh->add_file_name (cur_file, d_index, mod_time, length);
20489 }
20490 line_ptr += bytes_read;
20491 }
20492 lh->statement_program_start = line_ptr;
20493
20494 if (line_ptr > (section->buffer + section->size))
20495 complaint (_("line number info header doesn't "
20496 "fit in `.debug_line' section"));
20497
20498 return lh;
20499 }
20500
20501 /* Subroutine of dwarf_decode_lines to simplify it.
20502 Return the file name of the psymtab for included file FILE_INDEX
20503 in line header LH of PST.
20504 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20505 If space for the result is malloc'd, *NAME_HOLDER will be set.
20506 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20507
20508 static const char *
20509 psymtab_include_file_name (const struct line_header *lh, int file_index,
20510 const struct partial_symtab *pst,
20511 const char *comp_dir,
20512 gdb::unique_xmalloc_ptr<char> *name_holder)
20513 {
20514 const file_entry &fe = lh->file_names[file_index];
20515 const char *include_name = fe.name;
20516 const char *include_name_to_compare = include_name;
20517 const char *pst_filename;
20518 int file_is_pst;
20519
20520 const char *dir_name = fe.include_dir (lh);
20521
20522 gdb::unique_xmalloc_ptr<char> hold_compare;
20523 if (!IS_ABSOLUTE_PATH (include_name)
20524 && (dir_name != NULL || comp_dir != NULL))
20525 {
20526 /* Avoid creating a duplicate psymtab for PST.
20527 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20528 Before we do the comparison, however, we need to account
20529 for DIR_NAME and COMP_DIR.
20530 First prepend dir_name (if non-NULL). If we still don't
20531 have an absolute path prepend comp_dir (if non-NULL).
20532 However, the directory we record in the include-file's
20533 psymtab does not contain COMP_DIR (to match the
20534 corresponding symtab(s)).
20535
20536 Example:
20537
20538 bash$ cd /tmp
20539 bash$ gcc -g ./hello.c
20540 include_name = "hello.c"
20541 dir_name = "."
20542 DW_AT_comp_dir = comp_dir = "/tmp"
20543 DW_AT_name = "./hello.c"
20544
20545 */
20546
20547 if (dir_name != NULL)
20548 {
20549 name_holder->reset (concat (dir_name, SLASH_STRING,
20550 include_name, (char *) NULL));
20551 include_name = name_holder->get ();
20552 include_name_to_compare = include_name;
20553 }
20554 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20555 {
20556 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20557 include_name, (char *) NULL));
20558 include_name_to_compare = hold_compare.get ();
20559 }
20560 }
20561
20562 pst_filename = pst->filename;
20563 gdb::unique_xmalloc_ptr<char> copied_name;
20564 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20565 {
20566 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20567 pst_filename, (char *) NULL));
20568 pst_filename = copied_name.get ();
20569 }
20570
20571 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20572
20573 if (file_is_pst)
20574 return NULL;
20575 return include_name;
20576 }
20577
20578 /* State machine to track the state of the line number program. */
20579
20580 class lnp_state_machine
20581 {
20582 public:
20583 /* Initialize a machine state for the start of a line number
20584 program. */
20585 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20586 bool record_lines_p);
20587
20588 file_entry *current_file ()
20589 {
20590 /* lh->file_names is 0-based, but the file name numbers in the
20591 statement program are 1-based. */
20592 return m_line_header->file_name_at (m_file);
20593 }
20594
20595 /* Record the line in the state machine. END_SEQUENCE is true if
20596 we're processing the end of a sequence. */
20597 void record_line (bool end_sequence);
20598
20599 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20600 nop-out rest of the lines in this sequence. */
20601 void check_line_address (struct dwarf2_cu *cu,
20602 const gdb_byte *line_ptr,
20603 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20604
20605 void handle_set_discriminator (unsigned int discriminator)
20606 {
20607 m_discriminator = discriminator;
20608 m_line_has_non_zero_discriminator |= discriminator != 0;
20609 }
20610
20611 /* Handle DW_LNE_set_address. */
20612 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20613 {
20614 m_op_index = 0;
20615 address += baseaddr;
20616 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20617 }
20618
20619 /* Handle DW_LNS_advance_pc. */
20620 void handle_advance_pc (CORE_ADDR adjust);
20621
20622 /* Handle a special opcode. */
20623 void handle_special_opcode (unsigned char op_code);
20624
20625 /* Handle DW_LNS_advance_line. */
20626 void handle_advance_line (int line_delta)
20627 {
20628 advance_line (line_delta);
20629 }
20630
20631 /* Handle DW_LNS_set_file. */
20632 void handle_set_file (file_name_index file);
20633
20634 /* Handle DW_LNS_negate_stmt. */
20635 void handle_negate_stmt ()
20636 {
20637 m_is_stmt = !m_is_stmt;
20638 }
20639
20640 /* Handle DW_LNS_const_add_pc. */
20641 void handle_const_add_pc ();
20642
20643 /* Handle DW_LNS_fixed_advance_pc. */
20644 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20645 {
20646 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20647 m_op_index = 0;
20648 }
20649
20650 /* Handle DW_LNS_copy. */
20651 void handle_copy ()
20652 {
20653 record_line (false);
20654 m_discriminator = 0;
20655 }
20656
20657 /* Handle DW_LNE_end_sequence. */
20658 void handle_end_sequence ()
20659 {
20660 m_currently_recording_lines = true;
20661 }
20662
20663 private:
20664 /* Advance the line by LINE_DELTA. */
20665 void advance_line (int line_delta)
20666 {
20667 m_line += line_delta;
20668
20669 if (line_delta != 0)
20670 m_line_has_non_zero_discriminator = m_discriminator != 0;
20671 }
20672
20673 struct dwarf2_cu *m_cu;
20674
20675 gdbarch *m_gdbarch;
20676
20677 /* True if we're recording lines.
20678 Otherwise we're building partial symtabs and are just interested in
20679 finding include files mentioned by the line number program. */
20680 bool m_record_lines_p;
20681
20682 /* The line number header. */
20683 line_header *m_line_header;
20684
20685 /* These are part of the standard DWARF line number state machine,
20686 and initialized according to the DWARF spec. */
20687
20688 unsigned char m_op_index = 0;
20689 /* The line table index (1-based) of the current file. */
20690 file_name_index m_file = (file_name_index) 1;
20691 unsigned int m_line = 1;
20692
20693 /* These are initialized in the constructor. */
20694
20695 CORE_ADDR m_address;
20696 bool m_is_stmt;
20697 unsigned int m_discriminator;
20698
20699 /* Additional bits of state we need to track. */
20700
20701 /* The last file that we called dwarf2_start_subfile for.
20702 This is only used for TLLs. */
20703 unsigned int m_last_file = 0;
20704 /* The last file a line number was recorded for. */
20705 struct subfile *m_last_subfile = NULL;
20706
20707 /* When true, record the lines we decode. */
20708 bool m_currently_recording_lines = false;
20709
20710 /* The last line number that was recorded, used to coalesce
20711 consecutive entries for the same line. This can happen, for
20712 example, when discriminators are present. PR 17276. */
20713 unsigned int m_last_line = 0;
20714 bool m_line_has_non_zero_discriminator = false;
20715 };
20716
20717 void
20718 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20719 {
20720 CORE_ADDR addr_adj = (((m_op_index + adjust)
20721 / m_line_header->maximum_ops_per_instruction)
20722 * m_line_header->minimum_instruction_length);
20723 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20724 m_op_index = ((m_op_index + adjust)
20725 % m_line_header->maximum_ops_per_instruction);
20726 }
20727
20728 void
20729 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20730 {
20731 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20732 CORE_ADDR addr_adj = (((m_op_index
20733 + (adj_opcode / m_line_header->line_range))
20734 / m_line_header->maximum_ops_per_instruction)
20735 * m_line_header->minimum_instruction_length);
20736 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20737 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20738 % m_line_header->maximum_ops_per_instruction);
20739
20740 int line_delta = (m_line_header->line_base
20741 + (adj_opcode % m_line_header->line_range));
20742 advance_line (line_delta);
20743 record_line (false);
20744 m_discriminator = 0;
20745 }
20746
20747 void
20748 lnp_state_machine::handle_set_file (file_name_index file)
20749 {
20750 m_file = file;
20751
20752 const file_entry *fe = current_file ();
20753 if (fe == NULL)
20754 dwarf2_debug_line_missing_file_complaint ();
20755 else if (m_record_lines_p)
20756 {
20757 const char *dir = fe->include_dir (m_line_header);
20758
20759 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20760 m_line_has_non_zero_discriminator = m_discriminator != 0;
20761 dwarf2_start_subfile (m_cu, fe->name, dir);
20762 }
20763 }
20764
20765 void
20766 lnp_state_machine::handle_const_add_pc ()
20767 {
20768 CORE_ADDR adjust
20769 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20770
20771 CORE_ADDR addr_adj
20772 = (((m_op_index + adjust)
20773 / m_line_header->maximum_ops_per_instruction)
20774 * m_line_header->minimum_instruction_length);
20775
20776 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20777 m_op_index = ((m_op_index + adjust)
20778 % m_line_header->maximum_ops_per_instruction);
20779 }
20780
20781 /* Return non-zero if we should add LINE to the line number table.
20782 LINE is the line to add, LAST_LINE is the last line that was added,
20783 LAST_SUBFILE is the subfile for LAST_LINE.
20784 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20785 had a non-zero discriminator.
20786
20787 We have to be careful in the presence of discriminators.
20788 E.g., for this line:
20789
20790 for (i = 0; i < 100000; i++);
20791
20792 clang can emit four line number entries for that one line,
20793 each with a different discriminator.
20794 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20795
20796 However, we want gdb to coalesce all four entries into one.
20797 Otherwise the user could stepi into the middle of the line and
20798 gdb would get confused about whether the pc really was in the
20799 middle of the line.
20800
20801 Things are further complicated by the fact that two consecutive
20802 line number entries for the same line is a heuristic used by gcc
20803 to denote the end of the prologue. So we can't just discard duplicate
20804 entries, we have to be selective about it. The heuristic we use is
20805 that we only collapse consecutive entries for the same line if at least
20806 one of those entries has a non-zero discriminator. PR 17276.
20807
20808 Note: Addresses in the line number state machine can never go backwards
20809 within one sequence, thus this coalescing is ok. */
20810
20811 static int
20812 dwarf_record_line_p (struct dwarf2_cu *cu,
20813 unsigned int line, unsigned int last_line,
20814 int line_has_non_zero_discriminator,
20815 struct subfile *last_subfile)
20816 {
20817 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20818 return 1;
20819 if (line != last_line)
20820 return 1;
20821 /* Same line for the same file that we've seen already.
20822 As a last check, for pr 17276, only record the line if the line
20823 has never had a non-zero discriminator. */
20824 if (!line_has_non_zero_discriminator)
20825 return 1;
20826 return 0;
20827 }
20828
20829 /* Use the CU's builder to record line number LINE beginning at
20830 address ADDRESS in the line table of subfile SUBFILE. */
20831
20832 static void
20833 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20834 unsigned int line, CORE_ADDR address,
20835 struct dwarf2_cu *cu)
20836 {
20837 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20838
20839 if (dwarf_line_debug)
20840 {
20841 fprintf_unfiltered (gdb_stdlog,
20842 "Recording line %u, file %s, address %s\n",
20843 line, lbasename (subfile->name),
20844 paddress (gdbarch, address));
20845 }
20846
20847 if (cu != nullptr)
20848 cu->get_builder ()->record_line (subfile, line, addr);
20849 }
20850
20851 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20852 Mark the end of a set of line number records.
20853 The arguments are the same as for dwarf_record_line_1.
20854 If SUBFILE is NULL the request is ignored. */
20855
20856 static void
20857 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20858 CORE_ADDR address, struct dwarf2_cu *cu)
20859 {
20860 if (subfile == NULL)
20861 return;
20862
20863 if (dwarf_line_debug)
20864 {
20865 fprintf_unfiltered (gdb_stdlog,
20866 "Finishing current line, file %s, address %s\n",
20867 lbasename (subfile->name),
20868 paddress (gdbarch, address));
20869 }
20870
20871 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
20872 }
20873
20874 void
20875 lnp_state_machine::record_line (bool end_sequence)
20876 {
20877 if (dwarf_line_debug)
20878 {
20879 fprintf_unfiltered (gdb_stdlog,
20880 "Processing actual line %u: file %u,"
20881 " address %s, is_stmt %u, discrim %u\n",
20882 m_line, to_underlying (m_file),
20883 paddress (m_gdbarch, m_address),
20884 m_is_stmt, m_discriminator);
20885 }
20886
20887 file_entry *fe = current_file ();
20888
20889 if (fe == NULL)
20890 dwarf2_debug_line_missing_file_complaint ();
20891 /* For now we ignore lines not starting on an instruction boundary.
20892 But not when processing end_sequence for compatibility with the
20893 previous version of the code. */
20894 else if (m_op_index == 0 || end_sequence)
20895 {
20896 fe->included_p = 1;
20897 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
20898 {
20899 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20900 || end_sequence)
20901 {
20902 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20903 m_currently_recording_lines ? m_cu : nullptr);
20904 }
20905
20906 if (!end_sequence)
20907 {
20908 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20909 m_line_has_non_zero_discriminator,
20910 m_last_subfile))
20911 {
20912 buildsym_compunit *builder = m_cu->get_builder ();
20913 dwarf_record_line_1 (m_gdbarch,
20914 builder->get_current_subfile (),
20915 m_line, m_address,
20916 m_currently_recording_lines ? m_cu : nullptr);
20917 }
20918 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20919 m_last_line = m_line;
20920 }
20921 }
20922 }
20923 }
20924
20925 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20926 line_header *lh, bool record_lines_p)
20927 {
20928 m_cu = cu;
20929 m_gdbarch = arch;
20930 m_record_lines_p = record_lines_p;
20931 m_line_header = lh;
20932
20933 m_currently_recording_lines = true;
20934
20935 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20936 was a line entry for it so that the backend has a chance to adjust it
20937 and also record it in case it needs it. This is currently used by MIPS
20938 code, cf. `mips_adjust_dwarf2_line'. */
20939 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20940 m_is_stmt = lh->default_is_stmt;
20941 m_discriminator = 0;
20942 }
20943
20944 void
20945 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20946 const gdb_byte *line_ptr,
20947 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20948 {
20949 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20950 the pc range of the CU. However, we restrict the test to only ADDRESS
20951 values of zero to preserve GDB's previous behaviour which is to handle
20952 the specific case of a function being GC'd by the linker. */
20953
20954 if (address == 0 && address < unrelocated_lowpc)
20955 {
20956 /* This line table is for a function which has been
20957 GCd by the linker. Ignore it. PR gdb/12528 */
20958
20959 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20960 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20961
20962 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20963 line_offset, objfile_name (objfile));
20964 m_currently_recording_lines = false;
20965 /* Note: m_currently_recording_lines is left as false until we see
20966 DW_LNE_end_sequence. */
20967 }
20968 }
20969
20970 /* Subroutine of dwarf_decode_lines to simplify it.
20971 Process the line number information in LH.
20972 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20973 program in order to set included_p for every referenced header. */
20974
20975 static void
20976 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20977 const int decode_for_pst_p, CORE_ADDR lowpc)
20978 {
20979 const gdb_byte *line_ptr, *extended_end;
20980 const gdb_byte *line_end;
20981 unsigned int bytes_read, extended_len;
20982 unsigned char op_code, extended_op;
20983 CORE_ADDR baseaddr;
20984 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20985 bfd *abfd = objfile->obfd;
20986 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20987 /* True if we're recording line info (as opposed to building partial
20988 symtabs and just interested in finding include files mentioned by
20989 the line number program). */
20990 bool record_lines_p = !decode_for_pst_p;
20991
20992 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20993
20994 line_ptr = lh->statement_program_start;
20995 line_end = lh->statement_program_end;
20996
20997 /* Read the statement sequences until there's nothing left. */
20998 while (line_ptr < line_end)
20999 {
21000 /* The DWARF line number program state machine. Reset the state
21001 machine at the start of each sequence. */
21002 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21003 bool end_sequence = false;
21004
21005 if (record_lines_p)
21006 {
21007 /* Start a subfile for the current file of the state
21008 machine. */
21009 const file_entry *fe = state_machine.current_file ();
21010
21011 if (fe != NULL)
21012 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21013 }
21014
21015 /* Decode the table. */
21016 while (line_ptr < line_end && !end_sequence)
21017 {
21018 op_code = read_1_byte (abfd, line_ptr);
21019 line_ptr += 1;
21020
21021 if (op_code >= lh->opcode_base)
21022 {
21023 /* Special opcode. */
21024 state_machine.handle_special_opcode (op_code);
21025 }
21026 else switch (op_code)
21027 {
21028 case DW_LNS_extended_op:
21029 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21030 &bytes_read);
21031 line_ptr += bytes_read;
21032 extended_end = line_ptr + extended_len;
21033 extended_op = read_1_byte (abfd, line_ptr);
21034 line_ptr += 1;
21035 switch (extended_op)
21036 {
21037 case DW_LNE_end_sequence:
21038 state_machine.handle_end_sequence ();
21039 end_sequence = true;
21040 break;
21041 case DW_LNE_set_address:
21042 {
21043 CORE_ADDR address
21044 = read_address (abfd, line_ptr, cu, &bytes_read);
21045 line_ptr += bytes_read;
21046
21047 state_machine.check_line_address (cu, line_ptr,
21048 lowpc - baseaddr, address);
21049 state_machine.handle_set_address (baseaddr, address);
21050 }
21051 break;
21052 case DW_LNE_define_file:
21053 {
21054 const char *cur_file;
21055 unsigned int mod_time, length;
21056 dir_index dindex;
21057
21058 cur_file = read_direct_string (abfd, line_ptr,
21059 &bytes_read);
21060 line_ptr += bytes_read;
21061 dindex = (dir_index)
21062 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21063 line_ptr += bytes_read;
21064 mod_time =
21065 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21066 line_ptr += bytes_read;
21067 length =
21068 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21069 line_ptr += bytes_read;
21070 lh->add_file_name (cur_file, dindex, mod_time, length);
21071 }
21072 break;
21073 case DW_LNE_set_discriminator:
21074 {
21075 /* The discriminator is not interesting to the
21076 debugger; just ignore it. We still need to
21077 check its value though:
21078 if there are consecutive entries for the same
21079 (non-prologue) line we want to coalesce them.
21080 PR 17276. */
21081 unsigned int discr
21082 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21083 line_ptr += bytes_read;
21084
21085 state_machine.handle_set_discriminator (discr);
21086 }
21087 break;
21088 default:
21089 complaint (_("mangled .debug_line section"));
21090 return;
21091 }
21092 /* Make sure that we parsed the extended op correctly. If e.g.
21093 we expected a different address size than the producer used,
21094 we may have read the wrong number of bytes. */
21095 if (line_ptr != extended_end)
21096 {
21097 complaint (_("mangled .debug_line section"));
21098 return;
21099 }
21100 break;
21101 case DW_LNS_copy:
21102 state_machine.handle_copy ();
21103 break;
21104 case DW_LNS_advance_pc:
21105 {
21106 CORE_ADDR adjust
21107 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21108 line_ptr += bytes_read;
21109
21110 state_machine.handle_advance_pc (adjust);
21111 }
21112 break;
21113 case DW_LNS_advance_line:
21114 {
21115 int line_delta
21116 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21117 line_ptr += bytes_read;
21118
21119 state_machine.handle_advance_line (line_delta);
21120 }
21121 break;
21122 case DW_LNS_set_file:
21123 {
21124 file_name_index file
21125 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21126 &bytes_read);
21127 line_ptr += bytes_read;
21128
21129 state_machine.handle_set_file (file);
21130 }
21131 break;
21132 case DW_LNS_set_column:
21133 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21134 line_ptr += bytes_read;
21135 break;
21136 case DW_LNS_negate_stmt:
21137 state_machine.handle_negate_stmt ();
21138 break;
21139 case DW_LNS_set_basic_block:
21140 break;
21141 /* Add to the address register of the state machine the
21142 address increment value corresponding to special opcode
21143 255. I.e., this value is scaled by the minimum
21144 instruction length since special opcode 255 would have
21145 scaled the increment. */
21146 case DW_LNS_const_add_pc:
21147 state_machine.handle_const_add_pc ();
21148 break;
21149 case DW_LNS_fixed_advance_pc:
21150 {
21151 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21152 line_ptr += 2;
21153
21154 state_machine.handle_fixed_advance_pc (addr_adj);
21155 }
21156 break;
21157 default:
21158 {
21159 /* Unknown standard opcode, ignore it. */
21160 int i;
21161
21162 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21163 {
21164 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21165 line_ptr += bytes_read;
21166 }
21167 }
21168 }
21169 }
21170
21171 if (!end_sequence)
21172 dwarf2_debug_line_missing_end_sequence_complaint ();
21173
21174 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21175 in which case we still finish recording the last line). */
21176 state_machine.record_line (true);
21177 }
21178 }
21179
21180 /* Decode the Line Number Program (LNP) for the given line_header
21181 structure and CU. The actual information extracted and the type
21182 of structures created from the LNP depends on the value of PST.
21183
21184 1. If PST is NULL, then this procedure uses the data from the program
21185 to create all necessary symbol tables, and their linetables.
21186
21187 2. If PST is not NULL, this procedure reads the program to determine
21188 the list of files included by the unit represented by PST, and
21189 builds all the associated partial symbol tables.
21190
21191 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21192 It is used for relative paths in the line table.
21193 NOTE: When processing partial symtabs (pst != NULL),
21194 comp_dir == pst->dirname.
21195
21196 NOTE: It is important that psymtabs have the same file name (via strcmp)
21197 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21198 symtab we don't use it in the name of the psymtabs we create.
21199 E.g. expand_line_sal requires this when finding psymtabs to expand.
21200 A good testcase for this is mb-inline.exp.
21201
21202 LOWPC is the lowest address in CU (or 0 if not known).
21203
21204 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21205 for its PC<->lines mapping information. Otherwise only the filename
21206 table is read in. */
21207
21208 static void
21209 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21210 struct dwarf2_cu *cu, struct partial_symtab *pst,
21211 CORE_ADDR lowpc, int decode_mapping)
21212 {
21213 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21214 const int decode_for_pst_p = (pst != NULL);
21215
21216 if (decode_mapping)
21217 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21218
21219 if (decode_for_pst_p)
21220 {
21221 int file_index;
21222
21223 /* Now that we're done scanning the Line Header Program, we can
21224 create the psymtab of each included file. */
21225 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21226 if (lh->file_names[file_index].included_p == 1)
21227 {
21228 gdb::unique_xmalloc_ptr<char> name_holder;
21229 const char *include_name =
21230 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21231 &name_holder);
21232 if (include_name != NULL)
21233 dwarf2_create_include_psymtab (include_name, pst, objfile);
21234 }
21235 }
21236 else
21237 {
21238 /* Make sure a symtab is created for every file, even files
21239 which contain only variables (i.e. no code with associated
21240 line numbers). */
21241 buildsym_compunit *builder = cu->get_builder ();
21242 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21243 int i;
21244
21245 for (i = 0; i < lh->file_names.size (); i++)
21246 {
21247 file_entry &fe = lh->file_names[i];
21248
21249 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21250
21251 if (builder->get_current_subfile ()->symtab == NULL)
21252 {
21253 builder->get_current_subfile ()->symtab
21254 = allocate_symtab (cust,
21255 builder->get_current_subfile ()->name);
21256 }
21257 fe.symtab = builder->get_current_subfile ()->symtab;
21258 }
21259 }
21260 }
21261
21262 /* Start a subfile for DWARF. FILENAME is the name of the file and
21263 DIRNAME the name of the source directory which contains FILENAME
21264 or NULL if not known.
21265 This routine tries to keep line numbers from identical absolute and
21266 relative file names in a common subfile.
21267
21268 Using the `list' example from the GDB testsuite, which resides in
21269 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21270 of /srcdir/list0.c yields the following debugging information for list0.c:
21271
21272 DW_AT_name: /srcdir/list0.c
21273 DW_AT_comp_dir: /compdir
21274 files.files[0].name: list0.h
21275 files.files[0].dir: /srcdir
21276 files.files[1].name: list0.c
21277 files.files[1].dir: /srcdir
21278
21279 The line number information for list0.c has to end up in a single
21280 subfile, so that `break /srcdir/list0.c:1' works as expected.
21281 start_subfile will ensure that this happens provided that we pass the
21282 concatenation of files.files[1].dir and files.files[1].name as the
21283 subfile's name. */
21284
21285 static void
21286 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21287 const char *dirname)
21288 {
21289 char *copy = NULL;
21290
21291 /* In order not to lose the line information directory,
21292 we concatenate it to the filename when it makes sense.
21293 Note that the Dwarf3 standard says (speaking of filenames in line
21294 information): ``The directory index is ignored for file names
21295 that represent full path names''. Thus ignoring dirname in the
21296 `else' branch below isn't an issue. */
21297
21298 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21299 {
21300 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21301 filename = copy;
21302 }
21303
21304 cu->get_builder ()->start_subfile (filename);
21305
21306 if (copy != NULL)
21307 xfree (copy);
21308 }
21309
21310 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21311 buildsym_compunit constructor. */
21312
21313 struct compunit_symtab *
21314 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21315 CORE_ADDR low_pc)
21316 {
21317 gdb_assert (m_builder == nullptr);
21318
21319 m_builder.reset (new struct buildsym_compunit
21320 (per_cu->dwarf2_per_objfile->objfile,
21321 name, comp_dir, language, low_pc));
21322
21323 list_in_scope = get_builder ()->get_file_symbols ();
21324
21325 get_builder ()->record_debugformat ("DWARF 2");
21326 get_builder ()->record_producer (producer);
21327
21328 processing_has_namespace_info = false;
21329
21330 return get_builder ()->get_compunit_symtab ();
21331 }
21332
21333 static void
21334 var_decode_location (struct attribute *attr, struct symbol *sym,
21335 struct dwarf2_cu *cu)
21336 {
21337 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21338 struct comp_unit_head *cu_header = &cu->header;
21339
21340 /* NOTE drow/2003-01-30: There used to be a comment and some special
21341 code here to turn a symbol with DW_AT_external and a
21342 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21343 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21344 with some versions of binutils) where shared libraries could have
21345 relocations against symbols in their debug information - the
21346 minimal symbol would have the right address, but the debug info
21347 would not. It's no longer necessary, because we will explicitly
21348 apply relocations when we read in the debug information now. */
21349
21350 /* A DW_AT_location attribute with no contents indicates that a
21351 variable has been optimized away. */
21352 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21353 {
21354 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21355 return;
21356 }
21357
21358 /* Handle one degenerate form of location expression specially, to
21359 preserve GDB's previous behavior when section offsets are
21360 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21361 then mark this symbol as LOC_STATIC. */
21362
21363 if (attr_form_is_block (attr)
21364 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21365 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21366 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21367 && (DW_BLOCK (attr)->size
21368 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21369 {
21370 unsigned int dummy;
21371
21372 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21373 SYMBOL_VALUE_ADDRESS (sym) =
21374 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21375 else
21376 SYMBOL_VALUE_ADDRESS (sym) =
21377 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21378 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21379 fixup_symbol_section (sym, objfile);
21380 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21381 SYMBOL_SECTION (sym));
21382 return;
21383 }
21384
21385 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21386 expression evaluator, and use LOC_COMPUTED only when necessary
21387 (i.e. when the value of a register or memory location is
21388 referenced, or a thread-local block, etc.). Then again, it might
21389 not be worthwhile. I'm assuming that it isn't unless performance
21390 or memory numbers show me otherwise. */
21391
21392 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21393
21394 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21395 cu->has_loclist = true;
21396 }
21397
21398 /* Given a pointer to a DWARF information entry, figure out if we need
21399 to make a symbol table entry for it, and if so, create a new entry
21400 and return a pointer to it.
21401 If TYPE is NULL, determine symbol type from the die, otherwise
21402 used the passed type.
21403 If SPACE is not NULL, use it to hold the new symbol. If it is
21404 NULL, allocate a new symbol on the objfile's obstack. */
21405
21406 static struct symbol *
21407 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21408 struct symbol *space)
21409 {
21410 struct dwarf2_per_objfile *dwarf2_per_objfile
21411 = cu->per_cu->dwarf2_per_objfile;
21412 struct objfile *objfile = dwarf2_per_objfile->objfile;
21413 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21414 struct symbol *sym = NULL;
21415 const char *name;
21416 struct attribute *attr = NULL;
21417 struct attribute *attr2 = NULL;
21418 CORE_ADDR baseaddr;
21419 struct pending **list_to_add = NULL;
21420
21421 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21422
21423 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21424
21425 name = dwarf2_name (die, cu);
21426 if (name)
21427 {
21428 const char *linkagename;
21429 int suppress_add = 0;
21430
21431 if (space)
21432 sym = space;
21433 else
21434 sym = allocate_symbol (objfile);
21435 OBJSTAT (objfile, n_syms++);
21436
21437 /* Cache this symbol's name and the name's demangled form (if any). */
21438 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21439 linkagename = dwarf2_physname (name, die, cu);
21440 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21441
21442 /* Fortran does not have mangling standard and the mangling does differ
21443 between gfortran, iFort etc. */
21444 if (cu->language == language_fortran
21445 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21446 symbol_set_demangled_name (&(sym->ginfo),
21447 dwarf2_full_name (name, die, cu),
21448 NULL);
21449
21450 /* Default assumptions.
21451 Use the passed type or decode it from the die. */
21452 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21453 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21454 if (type != NULL)
21455 SYMBOL_TYPE (sym) = type;
21456 else
21457 SYMBOL_TYPE (sym) = die_type (die, cu);
21458 attr = dwarf2_attr (die,
21459 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21460 cu);
21461 if (attr)
21462 {
21463 SYMBOL_LINE (sym) = DW_UNSND (attr);
21464 }
21465
21466 attr = dwarf2_attr (die,
21467 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21468 cu);
21469 if (attr)
21470 {
21471 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21472 struct file_entry *fe;
21473
21474 if (cu->line_header != NULL)
21475 fe = cu->line_header->file_name_at (file_index);
21476 else
21477 fe = NULL;
21478
21479 if (fe == NULL)
21480 complaint (_("file index out of range"));
21481 else
21482 symbol_set_symtab (sym, fe->symtab);
21483 }
21484
21485 switch (die->tag)
21486 {
21487 case DW_TAG_label:
21488 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21489 if (attr)
21490 {
21491 CORE_ADDR addr;
21492
21493 addr = attr_value_as_address (attr);
21494 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21495 SYMBOL_VALUE_ADDRESS (sym) = addr;
21496 }
21497 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21498 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21499 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21500 add_symbol_to_list (sym, cu->list_in_scope);
21501 break;
21502 case DW_TAG_subprogram:
21503 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21504 finish_block. */
21505 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21506 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21507 if ((attr2 && (DW_UNSND (attr2) != 0))
21508 || cu->language == language_ada)
21509 {
21510 /* Subprograms marked external are stored as a global symbol.
21511 Ada subprograms, whether marked external or not, are always
21512 stored as a global symbol, because we want to be able to
21513 access them globally. For instance, we want to be able
21514 to break on a nested subprogram without having to
21515 specify the context. */
21516 list_to_add = cu->get_builder ()->get_global_symbols ();
21517 }
21518 else
21519 {
21520 list_to_add = cu->list_in_scope;
21521 }
21522 break;
21523 case DW_TAG_inlined_subroutine:
21524 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21525 finish_block. */
21526 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21527 SYMBOL_INLINED (sym) = 1;
21528 list_to_add = cu->list_in_scope;
21529 break;
21530 case DW_TAG_template_value_param:
21531 suppress_add = 1;
21532 /* Fall through. */
21533 case DW_TAG_constant:
21534 case DW_TAG_variable:
21535 case DW_TAG_member:
21536 /* Compilation with minimal debug info may result in
21537 variables with missing type entries. Change the
21538 misleading `void' type to something sensible. */
21539 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21540 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21541
21542 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21543 /* In the case of DW_TAG_member, we should only be called for
21544 static const members. */
21545 if (die->tag == DW_TAG_member)
21546 {
21547 /* dwarf2_add_field uses die_is_declaration,
21548 so we do the same. */
21549 gdb_assert (die_is_declaration (die, cu));
21550 gdb_assert (attr);
21551 }
21552 if (attr)
21553 {
21554 dwarf2_const_value (attr, sym, cu);
21555 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21556 if (!suppress_add)
21557 {
21558 if (attr2 && (DW_UNSND (attr2) != 0))
21559 list_to_add = cu->get_builder ()->get_global_symbols ();
21560 else
21561 list_to_add = cu->list_in_scope;
21562 }
21563 break;
21564 }
21565 attr = dwarf2_attr (die, DW_AT_location, cu);
21566 if (attr)
21567 {
21568 var_decode_location (attr, sym, cu);
21569 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21570
21571 /* Fortran explicitly imports any global symbols to the local
21572 scope by DW_TAG_common_block. */
21573 if (cu->language == language_fortran && die->parent
21574 && die->parent->tag == DW_TAG_common_block)
21575 attr2 = NULL;
21576
21577 if (SYMBOL_CLASS (sym) == LOC_STATIC
21578 && SYMBOL_VALUE_ADDRESS (sym) == 0
21579 && !dwarf2_per_objfile->has_section_at_zero)
21580 {
21581 /* When a static variable is eliminated by the linker,
21582 the corresponding debug information is not stripped
21583 out, but the variable address is set to null;
21584 do not add such variables into symbol table. */
21585 }
21586 else if (attr2 && (DW_UNSND (attr2) != 0))
21587 {
21588 /* Workaround gfortran PR debug/40040 - it uses
21589 DW_AT_location for variables in -fPIC libraries which may
21590 get overriden by other libraries/executable and get
21591 a different address. Resolve it by the minimal symbol
21592 which may come from inferior's executable using copy
21593 relocation. Make this workaround only for gfortran as for
21594 other compilers GDB cannot guess the minimal symbol
21595 Fortran mangling kind. */
21596 if (cu->language == language_fortran && die->parent
21597 && die->parent->tag == DW_TAG_module
21598 && cu->producer
21599 && startswith (cu->producer, "GNU Fortran"))
21600 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21601
21602 /* A variable with DW_AT_external is never static,
21603 but it may be block-scoped. */
21604 list_to_add
21605 = ((cu->list_in_scope
21606 == cu->get_builder ()->get_file_symbols ())
21607 ? cu->get_builder ()->get_global_symbols ()
21608 : cu->list_in_scope);
21609 }
21610 else
21611 list_to_add = cu->list_in_scope;
21612 }
21613 else
21614 {
21615 /* We do not know the address of this symbol.
21616 If it is an external symbol and we have type information
21617 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21618 The address of the variable will then be determined from
21619 the minimal symbol table whenever the variable is
21620 referenced. */
21621 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21622
21623 /* Fortran explicitly imports any global symbols to the local
21624 scope by DW_TAG_common_block. */
21625 if (cu->language == language_fortran && die->parent
21626 && die->parent->tag == DW_TAG_common_block)
21627 {
21628 /* SYMBOL_CLASS doesn't matter here because
21629 read_common_block is going to reset it. */
21630 if (!suppress_add)
21631 list_to_add = cu->list_in_scope;
21632 }
21633 else if (attr2 && (DW_UNSND (attr2) != 0)
21634 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21635 {
21636 /* A variable with DW_AT_external is never static, but it
21637 may be block-scoped. */
21638 list_to_add
21639 = ((cu->list_in_scope
21640 == cu->get_builder ()->get_file_symbols ())
21641 ? cu->get_builder ()->get_global_symbols ()
21642 : cu->list_in_scope);
21643
21644 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21645 }
21646 else if (!die_is_declaration (die, cu))
21647 {
21648 /* Use the default LOC_OPTIMIZED_OUT class. */
21649 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21650 if (!suppress_add)
21651 list_to_add = cu->list_in_scope;
21652 }
21653 }
21654 break;
21655 case DW_TAG_formal_parameter:
21656 {
21657 /* If we are inside a function, mark this as an argument. If
21658 not, we might be looking at an argument to an inlined function
21659 when we do not have enough information to show inlined frames;
21660 pretend it's a local variable in that case so that the user can
21661 still see it. */
21662 struct context_stack *curr
21663 = cu->get_builder ()->get_current_context_stack ();
21664 if (curr != nullptr && curr->name != nullptr)
21665 SYMBOL_IS_ARGUMENT (sym) = 1;
21666 attr = dwarf2_attr (die, DW_AT_location, cu);
21667 if (attr)
21668 {
21669 var_decode_location (attr, sym, cu);
21670 }
21671 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21672 if (attr)
21673 {
21674 dwarf2_const_value (attr, sym, cu);
21675 }
21676
21677 list_to_add = cu->list_in_scope;
21678 }
21679 break;
21680 case DW_TAG_unspecified_parameters:
21681 /* From varargs functions; gdb doesn't seem to have any
21682 interest in this information, so just ignore it for now.
21683 (FIXME?) */
21684 break;
21685 case DW_TAG_template_type_param:
21686 suppress_add = 1;
21687 /* Fall through. */
21688 case DW_TAG_class_type:
21689 case DW_TAG_interface_type:
21690 case DW_TAG_structure_type:
21691 case DW_TAG_union_type:
21692 case DW_TAG_set_type:
21693 case DW_TAG_enumeration_type:
21694 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21695 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21696
21697 {
21698 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21699 really ever be static objects: otherwise, if you try
21700 to, say, break of a class's method and you're in a file
21701 which doesn't mention that class, it won't work unless
21702 the check for all static symbols in lookup_symbol_aux
21703 saves you. See the OtherFileClass tests in
21704 gdb.c++/namespace.exp. */
21705
21706 if (!suppress_add)
21707 {
21708 buildsym_compunit *builder = cu->get_builder ();
21709 list_to_add
21710 = (cu->list_in_scope == builder->get_file_symbols ()
21711 && cu->language == language_cplus
21712 ? builder->get_global_symbols ()
21713 : cu->list_in_scope);
21714
21715 /* The semantics of C++ state that "struct foo {
21716 ... }" also defines a typedef for "foo". */
21717 if (cu->language == language_cplus
21718 || cu->language == language_ada
21719 || cu->language == language_d
21720 || cu->language == language_rust)
21721 {
21722 /* The symbol's name is already allocated along
21723 with this objfile, so we don't need to
21724 duplicate it for the type. */
21725 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21726 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21727 }
21728 }
21729 }
21730 break;
21731 case DW_TAG_typedef:
21732 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21733 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21734 list_to_add = cu->list_in_scope;
21735 break;
21736 case DW_TAG_base_type:
21737 case DW_TAG_subrange_type:
21738 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21739 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21740 list_to_add = cu->list_in_scope;
21741 break;
21742 case DW_TAG_enumerator:
21743 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21744 if (attr)
21745 {
21746 dwarf2_const_value (attr, sym, cu);
21747 }
21748 {
21749 /* NOTE: carlton/2003-11-10: See comment above in the
21750 DW_TAG_class_type, etc. block. */
21751
21752 list_to_add
21753 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21754 && cu->language == language_cplus
21755 ? cu->get_builder ()->get_global_symbols ()
21756 : cu->list_in_scope);
21757 }
21758 break;
21759 case DW_TAG_imported_declaration:
21760 case DW_TAG_namespace:
21761 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21762 list_to_add = cu->get_builder ()->get_global_symbols ();
21763 break;
21764 case DW_TAG_module:
21765 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21766 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21767 list_to_add = cu->get_builder ()->get_global_symbols ();
21768 break;
21769 case DW_TAG_common_block:
21770 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21771 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21772 add_symbol_to_list (sym, cu->list_in_scope);
21773 break;
21774 default:
21775 /* Not a tag we recognize. Hopefully we aren't processing
21776 trash data, but since we must specifically ignore things
21777 we don't recognize, there is nothing else we should do at
21778 this point. */
21779 complaint (_("unsupported tag: '%s'"),
21780 dwarf_tag_name (die->tag));
21781 break;
21782 }
21783
21784 if (suppress_add)
21785 {
21786 sym->hash_next = objfile->template_symbols;
21787 objfile->template_symbols = sym;
21788 list_to_add = NULL;
21789 }
21790
21791 if (list_to_add != NULL)
21792 add_symbol_to_list (sym, list_to_add);
21793
21794 /* For the benefit of old versions of GCC, check for anonymous
21795 namespaces based on the demangled name. */
21796 if (!cu->processing_has_namespace_info
21797 && cu->language == language_cplus)
21798 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21799 }
21800 return (sym);
21801 }
21802
21803 /* Given an attr with a DW_FORM_dataN value in host byte order,
21804 zero-extend it as appropriate for the symbol's type. The DWARF
21805 standard (v4) is not entirely clear about the meaning of using
21806 DW_FORM_dataN for a constant with a signed type, where the type is
21807 wider than the data. The conclusion of a discussion on the DWARF
21808 list was that this is unspecified. We choose to always zero-extend
21809 because that is the interpretation long in use by GCC. */
21810
21811 static gdb_byte *
21812 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21813 struct dwarf2_cu *cu, LONGEST *value, int bits)
21814 {
21815 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21816 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21817 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21818 LONGEST l = DW_UNSND (attr);
21819
21820 if (bits < sizeof (*value) * 8)
21821 {
21822 l &= ((LONGEST) 1 << bits) - 1;
21823 *value = l;
21824 }
21825 else if (bits == sizeof (*value) * 8)
21826 *value = l;
21827 else
21828 {
21829 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21830 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21831 return bytes;
21832 }
21833
21834 return NULL;
21835 }
21836
21837 /* Read a constant value from an attribute. Either set *VALUE, or if
21838 the value does not fit in *VALUE, set *BYTES - either already
21839 allocated on the objfile obstack, or newly allocated on OBSTACK,
21840 or, set *BATON, if we translated the constant to a location
21841 expression. */
21842
21843 static void
21844 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21845 const char *name, struct obstack *obstack,
21846 struct dwarf2_cu *cu,
21847 LONGEST *value, const gdb_byte **bytes,
21848 struct dwarf2_locexpr_baton **baton)
21849 {
21850 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21851 struct comp_unit_head *cu_header = &cu->header;
21852 struct dwarf_block *blk;
21853 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21854 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21855
21856 *value = 0;
21857 *bytes = NULL;
21858 *baton = NULL;
21859
21860 switch (attr->form)
21861 {
21862 case DW_FORM_addr:
21863 case DW_FORM_GNU_addr_index:
21864 {
21865 gdb_byte *data;
21866
21867 if (TYPE_LENGTH (type) != cu_header->addr_size)
21868 dwarf2_const_value_length_mismatch_complaint (name,
21869 cu_header->addr_size,
21870 TYPE_LENGTH (type));
21871 /* Symbols of this form are reasonably rare, so we just
21872 piggyback on the existing location code rather than writing
21873 a new implementation of symbol_computed_ops. */
21874 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21875 (*baton)->per_cu = cu->per_cu;
21876 gdb_assert ((*baton)->per_cu);
21877
21878 (*baton)->size = 2 + cu_header->addr_size;
21879 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21880 (*baton)->data = data;
21881
21882 data[0] = DW_OP_addr;
21883 store_unsigned_integer (&data[1], cu_header->addr_size,
21884 byte_order, DW_ADDR (attr));
21885 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21886 }
21887 break;
21888 case DW_FORM_string:
21889 case DW_FORM_strp:
21890 case DW_FORM_GNU_str_index:
21891 case DW_FORM_GNU_strp_alt:
21892 /* DW_STRING is already allocated on the objfile obstack, point
21893 directly to it. */
21894 *bytes = (const gdb_byte *) DW_STRING (attr);
21895 break;
21896 case DW_FORM_block1:
21897 case DW_FORM_block2:
21898 case DW_FORM_block4:
21899 case DW_FORM_block:
21900 case DW_FORM_exprloc:
21901 case DW_FORM_data16:
21902 blk = DW_BLOCK (attr);
21903 if (TYPE_LENGTH (type) != blk->size)
21904 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21905 TYPE_LENGTH (type));
21906 *bytes = blk->data;
21907 break;
21908
21909 /* The DW_AT_const_value attributes are supposed to carry the
21910 symbol's value "represented as it would be on the target
21911 architecture." By the time we get here, it's already been
21912 converted to host endianness, so we just need to sign- or
21913 zero-extend it as appropriate. */
21914 case DW_FORM_data1:
21915 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21916 break;
21917 case DW_FORM_data2:
21918 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21919 break;
21920 case DW_FORM_data4:
21921 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21922 break;
21923 case DW_FORM_data8:
21924 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21925 break;
21926
21927 case DW_FORM_sdata:
21928 case DW_FORM_implicit_const:
21929 *value = DW_SND (attr);
21930 break;
21931
21932 case DW_FORM_udata:
21933 *value = DW_UNSND (attr);
21934 break;
21935
21936 default:
21937 complaint (_("unsupported const value attribute form: '%s'"),
21938 dwarf_form_name (attr->form));
21939 *value = 0;
21940 break;
21941 }
21942 }
21943
21944
21945 /* Copy constant value from an attribute to a symbol. */
21946
21947 static void
21948 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21949 struct dwarf2_cu *cu)
21950 {
21951 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21952 LONGEST value;
21953 const gdb_byte *bytes;
21954 struct dwarf2_locexpr_baton *baton;
21955
21956 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21957 SYMBOL_PRINT_NAME (sym),
21958 &objfile->objfile_obstack, cu,
21959 &value, &bytes, &baton);
21960
21961 if (baton != NULL)
21962 {
21963 SYMBOL_LOCATION_BATON (sym) = baton;
21964 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21965 }
21966 else if (bytes != NULL)
21967 {
21968 SYMBOL_VALUE_BYTES (sym) = bytes;
21969 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21970 }
21971 else
21972 {
21973 SYMBOL_VALUE (sym) = value;
21974 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21975 }
21976 }
21977
21978 /* Return the type of the die in question using its DW_AT_type attribute. */
21979
21980 static struct type *
21981 die_type (struct die_info *die, struct dwarf2_cu *cu)
21982 {
21983 struct attribute *type_attr;
21984
21985 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21986 if (!type_attr)
21987 {
21988 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21989 /* A missing DW_AT_type represents a void type. */
21990 return objfile_type (objfile)->builtin_void;
21991 }
21992
21993 return lookup_die_type (die, type_attr, cu);
21994 }
21995
21996 /* True iff CU's producer generates GNAT Ada auxiliary information
21997 that allows to find parallel types through that information instead
21998 of having to do expensive parallel lookups by type name. */
21999
22000 static int
22001 need_gnat_info (struct dwarf2_cu *cu)
22002 {
22003 /* Assume that the Ada compiler was GNAT, which always produces
22004 the auxiliary information. */
22005 return (cu->language == language_ada);
22006 }
22007
22008 /* Return the auxiliary type of the die in question using its
22009 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22010 attribute is not present. */
22011
22012 static struct type *
22013 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22014 {
22015 struct attribute *type_attr;
22016
22017 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22018 if (!type_attr)
22019 return NULL;
22020
22021 return lookup_die_type (die, type_attr, cu);
22022 }
22023
22024 /* If DIE has a descriptive_type attribute, then set the TYPE's
22025 descriptive type accordingly. */
22026
22027 static void
22028 set_descriptive_type (struct type *type, struct die_info *die,
22029 struct dwarf2_cu *cu)
22030 {
22031 struct type *descriptive_type = die_descriptive_type (die, cu);
22032
22033 if (descriptive_type)
22034 {
22035 ALLOCATE_GNAT_AUX_TYPE (type);
22036 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22037 }
22038 }
22039
22040 /* Return the containing type of the die in question using its
22041 DW_AT_containing_type attribute. */
22042
22043 static struct type *
22044 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22045 {
22046 struct attribute *type_attr;
22047 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22048
22049 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22050 if (!type_attr)
22051 error (_("Dwarf Error: Problem turning containing type into gdb type "
22052 "[in module %s]"), objfile_name (objfile));
22053
22054 return lookup_die_type (die, type_attr, cu);
22055 }
22056
22057 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22058
22059 static struct type *
22060 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22061 {
22062 struct dwarf2_per_objfile *dwarf2_per_objfile
22063 = cu->per_cu->dwarf2_per_objfile;
22064 struct objfile *objfile = dwarf2_per_objfile->objfile;
22065 char *saved;
22066
22067 std::string message
22068 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22069 objfile_name (objfile),
22070 sect_offset_str (cu->header.sect_off),
22071 sect_offset_str (die->sect_off));
22072 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
22073 message.c_str (), message.length ());
22074
22075 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22076 }
22077
22078 /* Look up the type of DIE in CU using its type attribute ATTR.
22079 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22080 DW_AT_containing_type.
22081 If there is no type substitute an error marker. */
22082
22083 static struct type *
22084 lookup_die_type (struct die_info *die, const struct attribute *attr,
22085 struct dwarf2_cu *cu)
22086 {
22087 struct dwarf2_per_objfile *dwarf2_per_objfile
22088 = cu->per_cu->dwarf2_per_objfile;
22089 struct objfile *objfile = dwarf2_per_objfile->objfile;
22090 struct type *this_type;
22091
22092 gdb_assert (attr->name == DW_AT_type
22093 || attr->name == DW_AT_GNAT_descriptive_type
22094 || attr->name == DW_AT_containing_type);
22095
22096 /* First see if we have it cached. */
22097
22098 if (attr->form == DW_FORM_GNU_ref_alt)
22099 {
22100 struct dwarf2_per_cu_data *per_cu;
22101 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22102
22103 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22104 dwarf2_per_objfile);
22105 this_type = get_die_type_at_offset (sect_off, per_cu);
22106 }
22107 else if (attr_form_is_ref (attr))
22108 {
22109 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22110
22111 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22112 }
22113 else if (attr->form == DW_FORM_ref_sig8)
22114 {
22115 ULONGEST signature = DW_SIGNATURE (attr);
22116
22117 return get_signatured_type (die, signature, cu);
22118 }
22119 else
22120 {
22121 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22122 " at %s [in module %s]"),
22123 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22124 objfile_name (objfile));
22125 return build_error_marker_type (cu, die);
22126 }
22127
22128 /* If not cached we need to read it in. */
22129
22130 if (this_type == NULL)
22131 {
22132 struct die_info *type_die = NULL;
22133 struct dwarf2_cu *type_cu = cu;
22134
22135 if (attr_form_is_ref (attr))
22136 type_die = follow_die_ref (die, attr, &type_cu);
22137 if (type_die == NULL)
22138 return build_error_marker_type (cu, die);
22139 /* If we find the type now, it's probably because the type came
22140 from an inter-CU reference and the type's CU got expanded before
22141 ours. */
22142 this_type = read_type_die (type_die, type_cu);
22143 }
22144
22145 /* If we still don't have a type use an error marker. */
22146
22147 if (this_type == NULL)
22148 return build_error_marker_type (cu, die);
22149
22150 return this_type;
22151 }
22152
22153 /* Return the type in DIE, CU.
22154 Returns NULL for invalid types.
22155
22156 This first does a lookup in die_type_hash,
22157 and only reads the die in if necessary.
22158
22159 NOTE: This can be called when reading in partial or full symbols. */
22160
22161 static struct type *
22162 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22163 {
22164 struct type *this_type;
22165
22166 this_type = get_die_type (die, cu);
22167 if (this_type)
22168 return this_type;
22169
22170 return read_type_die_1 (die, cu);
22171 }
22172
22173 /* Read the type in DIE, CU.
22174 Returns NULL for invalid types. */
22175
22176 static struct type *
22177 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22178 {
22179 struct type *this_type = NULL;
22180
22181 switch (die->tag)
22182 {
22183 case DW_TAG_class_type:
22184 case DW_TAG_interface_type:
22185 case DW_TAG_structure_type:
22186 case DW_TAG_union_type:
22187 this_type = read_structure_type (die, cu);
22188 break;
22189 case DW_TAG_enumeration_type:
22190 this_type = read_enumeration_type (die, cu);
22191 break;
22192 case DW_TAG_subprogram:
22193 case DW_TAG_subroutine_type:
22194 case DW_TAG_inlined_subroutine:
22195 this_type = read_subroutine_type (die, cu);
22196 break;
22197 case DW_TAG_array_type:
22198 this_type = read_array_type (die, cu);
22199 break;
22200 case DW_TAG_set_type:
22201 this_type = read_set_type (die, cu);
22202 break;
22203 case DW_TAG_pointer_type:
22204 this_type = read_tag_pointer_type (die, cu);
22205 break;
22206 case DW_TAG_ptr_to_member_type:
22207 this_type = read_tag_ptr_to_member_type (die, cu);
22208 break;
22209 case DW_TAG_reference_type:
22210 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22211 break;
22212 case DW_TAG_rvalue_reference_type:
22213 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22214 break;
22215 case DW_TAG_const_type:
22216 this_type = read_tag_const_type (die, cu);
22217 break;
22218 case DW_TAG_volatile_type:
22219 this_type = read_tag_volatile_type (die, cu);
22220 break;
22221 case DW_TAG_restrict_type:
22222 this_type = read_tag_restrict_type (die, cu);
22223 break;
22224 case DW_TAG_string_type:
22225 this_type = read_tag_string_type (die, cu);
22226 break;
22227 case DW_TAG_typedef:
22228 this_type = read_typedef (die, cu);
22229 break;
22230 case DW_TAG_subrange_type:
22231 this_type = read_subrange_type (die, cu);
22232 break;
22233 case DW_TAG_base_type:
22234 this_type = read_base_type (die, cu);
22235 break;
22236 case DW_TAG_unspecified_type:
22237 this_type = read_unspecified_type (die, cu);
22238 break;
22239 case DW_TAG_namespace:
22240 this_type = read_namespace_type (die, cu);
22241 break;
22242 case DW_TAG_module:
22243 this_type = read_module_type (die, cu);
22244 break;
22245 case DW_TAG_atomic_type:
22246 this_type = read_tag_atomic_type (die, cu);
22247 break;
22248 default:
22249 complaint (_("unexpected tag in read_type_die: '%s'"),
22250 dwarf_tag_name (die->tag));
22251 break;
22252 }
22253
22254 return this_type;
22255 }
22256
22257 /* See if we can figure out if the class lives in a namespace. We do
22258 this by looking for a member function; its demangled name will
22259 contain namespace info, if there is any.
22260 Return the computed name or NULL.
22261 Space for the result is allocated on the objfile's obstack.
22262 This is the full-die version of guess_partial_die_structure_name.
22263 In this case we know DIE has no useful parent. */
22264
22265 static char *
22266 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22267 {
22268 struct die_info *spec_die;
22269 struct dwarf2_cu *spec_cu;
22270 struct die_info *child;
22271 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22272
22273 spec_cu = cu;
22274 spec_die = die_specification (die, &spec_cu);
22275 if (spec_die != NULL)
22276 {
22277 die = spec_die;
22278 cu = spec_cu;
22279 }
22280
22281 for (child = die->child;
22282 child != NULL;
22283 child = child->sibling)
22284 {
22285 if (child->tag == DW_TAG_subprogram)
22286 {
22287 const char *linkage_name = dw2_linkage_name (child, cu);
22288
22289 if (linkage_name != NULL)
22290 {
22291 char *actual_name
22292 = language_class_name_from_physname (cu->language_defn,
22293 linkage_name);
22294 char *name = NULL;
22295
22296 if (actual_name != NULL)
22297 {
22298 const char *die_name = dwarf2_name (die, cu);
22299
22300 if (die_name != NULL
22301 && strcmp (die_name, actual_name) != 0)
22302 {
22303 /* Strip off the class name from the full name.
22304 We want the prefix. */
22305 int die_name_len = strlen (die_name);
22306 int actual_name_len = strlen (actual_name);
22307
22308 /* Test for '::' as a sanity check. */
22309 if (actual_name_len > die_name_len + 2
22310 && actual_name[actual_name_len
22311 - die_name_len - 1] == ':')
22312 name = (char *) obstack_copy0 (
22313 &objfile->per_bfd->storage_obstack,
22314 actual_name, actual_name_len - die_name_len - 2);
22315 }
22316 }
22317 xfree (actual_name);
22318 return name;
22319 }
22320 }
22321 }
22322
22323 return NULL;
22324 }
22325
22326 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22327 prefix part in such case. See
22328 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22329
22330 static const char *
22331 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22332 {
22333 struct attribute *attr;
22334 const char *base;
22335
22336 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22337 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22338 return NULL;
22339
22340 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22341 return NULL;
22342
22343 attr = dw2_linkage_name_attr (die, cu);
22344 if (attr == NULL || DW_STRING (attr) == NULL)
22345 return NULL;
22346
22347 /* dwarf2_name had to be already called. */
22348 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22349
22350 /* Strip the base name, keep any leading namespaces/classes. */
22351 base = strrchr (DW_STRING (attr), ':');
22352 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22353 return "";
22354
22355 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22356 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22357 DW_STRING (attr),
22358 &base[-1] - DW_STRING (attr));
22359 }
22360
22361 /* Return the name of the namespace/class that DIE is defined within,
22362 or "" if we can't tell. The caller should not xfree the result.
22363
22364 For example, if we're within the method foo() in the following
22365 code:
22366
22367 namespace N {
22368 class C {
22369 void foo () {
22370 }
22371 };
22372 }
22373
22374 then determine_prefix on foo's die will return "N::C". */
22375
22376 static const char *
22377 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22378 {
22379 struct dwarf2_per_objfile *dwarf2_per_objfile
22380 = cu->per_cu->dwarf2_per_objfile;
22381 struct die_info *parent, *spec_die;
22382 struct dwarf2_cu *spec_cu;
22383 struct type *parent_type;
22384 const char *retval;
22385
22386 if (cu->language != language_cplus
22387 && cu->language != language_fortran && cu->language != language_d
22388 && cu->language != language_rust)
22389 return "";
22390
22391 retval = anonymous_struct_prefix (die, cu);
22392 if (retval)
22393 return retval;
22394
22395 /* We have to be careful in the presence of DW_AT_specification.
22396 For example, with GCC 3.4, given the code
22397
22398 namespace N {
22399 void foo() {
22400 // Definition of N::foo.
22401 }
22402 }
22403
22404 then we'll have a tree of DIEs like this:
22405
22406 1: DW_TAG_compile_unit
22407 2: DW_TAG_namespace // N
22408 3: DW_TAG_subprogram // declaration of N::foo
22409 4: DW_TAG_subprogram // definition of N::foo
22410 DW_AT_specification // refers to die #3
22411
22412 Thus, when processing die #4, we have to pretend that we're in
22413 the context of its DW_AT_specification, namely the contex of die
22414 #3. */
22415 spec_cu = cu;
22416 spec_die = die_specification (die, &spec_cu);
22417 if (spec_die == NULL)
22418 parent = die->parent;
22419 else
22420 {
22421 parent = spec_die->parent;
22422 cu = spec_cu;
22423 }
22424
22425 if (parent == NULL)
22426 return "";
22427 else if (parent->building_fullname)
22428 {
22429 const char *name;
22430 const char *parent_name;
22431
22432 /* It has been seen on RealView 2.2 built binaries,
22433 DW_TAG_template_type_param types actually _defined_ as
22434 children of the parent class:
22435
22436 enum E {};
22437 template class <class Enum> Class{};
22438 Class<enum E> class_e;
22439
22440 1: DW_TAG_class_type (Class)
22441 2: DW_TAG_enumeration_type (E)
22442 3: DW_TAG_enumerator (enum1:0)
22443 3: DW_TAG_enumerator (enum2:1)
22444 ...
22445 2: DW_TAG_template_type_param
22446 DW_AT_type DW_FORM_ref_udata (E)
22447
22448 Besides being broken debug info, it can put GDB into an
22449 infinite loop. Consider:
22450
22451 When we're building the full name for Class<E>, we'll start
22452 at Class, and go look over its template type parameters,
22453 finding E. We'll then try to build the full name of E, and
22454 reach here. We're now trying to build the full name of E,
22455 and look over the parent DIE for containing scope. In the
22456 broken case, if we followed the parent DIE of E, we'd again
22457 find Class, and once again go look at its template type
22458 arguments, etc., etc. Simply don't consider such parent die
22459 as source-level parent of this die (it can't be, the language
22460 doesn't allow it), and break the loop here. */
22461 name = dwarf2_name (die, cu);
22462 parent_name = dwarf2_name (parent, cu);
22463 complaint (_("template param type '%s' defined within parent '%s'"),
22464 name ? name : "<unknown>",
22465 parent_name ? parent_name : "<unknown>");
22466 return "";
22467 }
22468 else
22469 switch (parent->tag)
22470 {
22471 case DW_TAG_namespace:
22472 parent_type = read_type_die (parent, cu);
22473 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22474 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22475 Work around this problem here. */
22476 if (cu->language == language_cplus
22477 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22478 return "";
22479 /* We give a name to even anonymous namespaces. */
22480 return TYPE_NAME (parent_type);
22481 case DW_TAG_class_type:
22482 case DW_TAG_interface_type:
22483 case DW_TAG_structure_type:
22484 case DW_TAG_union_type:
22485 case DW_TAG_module:
22486 parent_type = read_type_die (parent, cu);
22487 if (TYPE_NAME (parent_type) != NULL)
22488 return TYPE_NAME (parent_type);
22489 else
22490 /* An anonymous structure is only allowed non-static data
22491 members; no typedefs, no member functions, et cetera.
22492 So it does not need a prefix. */
22493 return "";
22494 case DW_TAG_compile_unit:
22495 case DW_TAG_partial_unit:
22496 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22497 if (cu->language == language_cplus
22498 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22499 && die->child != NULL
22500 && (die->tag == DW_TAG_class_type
22501 || die->tag == DW_TAG_structure_type
22502 || die->tag == DW_TAG_union_type))
22503 {
22504 char *name = guess_full_die_structure_name (die, cu);
22505 if (name != NULL)
22506 return name;
22507 }
22508 return "";
22509 case DW_TAG_enumeration_type:
22510 parent_type = read_type_die (parent, cu);
22511 if (TYPE_DECLARED_CLASS (parent_type))
22512 {
22513 if (TYPE_NAME (parent_type) != NULL)
22514 return TYPE_NAME (parent_type);
22515 return "";
22516 }
22517 /* Fall through. */
22518 default:
22519 return determine_prefix (parent, cu);
22520 }
22521 }
22522
22523 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22524 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22525 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22526 an obconcat, otherwise allocate storage for the result. The CU argument is
22527 used to determine the language and hence, the appropriate separator. */
22528
22529 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22530
22531 static char *
22532 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22533 int physname, struct dwarf2_cu *cu)
22534 {
22535 const char *lead = "";
22536 const char *sep;
22537
22538 if (suffix == NULL || suffix[0] == '\0'
22539 || prefix == NULL || prefix[0] == '\0')
22540 sep = "";
22541 else if (cu->language == language_d)
22542 {
22543 /* For D, the 'main' function could be defined in any module, but it
22544 should never be prefixed. */
22545 if (strcmp (suffix, "D main") == 0)
22546 {
22547 prefix = "";
22548 sep = "";
22549 }
22550 else
22551 sep = ".";
22552 }
22553 else if (cu->language == language_fortran && physname)
22554 {
22555 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22556 DW_AT_MIPS_linkage_name is preferred and used instead. */
22557
22558 lead = "__";
22559 sep = "_MOD_";
22560 }
22561 else
22562 sep = "::";
22563
22564 if (prefix == NULL)
22565 prefix = "";
22566 if (suffix == NULL)
22567 suffix = "";
22568
22569 if (obs == NULL)
22570 {
22571 char *retval
22572 = ((char *)
22573 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22574
22575 strcpy (retval, lead);
22576 strcat (retval, prefix);
22577 strcat (retval, sep);
22578 strcat (retval, suffix);
22579 return retval;
22580 }
22581 else
22582 {
22583 /* We have an obstack. */
22584 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22585 }
22586 }
22587
22588 /* Return sibling of die, NULL if no sibling. */
22589
22590 static struct die_info *
22591 sibling_die (struct die_info *die)
22592 {
22593 return die->sibling;
22594 }
22595
22596 /* Get name of a die, return NULL if not found. */
22597
22598 static const char *
22599 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22600 struct obstack *obstack)
22601 {
22602 if (name && cu->language == language_cplus)
22603 {
22604 std::string canon_name = cp_canonicalize_string (name);
22605
22606 if (!canon_name.empty ())
22607 {
22608 if (canon_name != name)
22609 name = (const char *) obstack_copy0 (obstack,
22610 canon_name.c_str (),
22611 canon_name.length ());
22612 }
22613 }
22614
22615 return name;
22616 }
22617
22618 /* Get name of a die, return NULL if not found.
22619 Anonymous namespaces are converted to their magic string. */
22620
22621 static const char *
22622 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22623 {
22624 struct attribute *attr;
22625 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22626
22627 attr = dwarf2_attr (die, DW_AT_name, cu);
22628 if ((!attr || !DW_STRING (attr))
22629 && die->tag != DW_TAG_namespace
22630 && die->tag != DW_TAG_class_type
22631 && die->tag != DW_TAG_interface_type
22632 && die->tag != DW_TAG_structure_type
22633 && die->tag != DW_TAG_union_type)
22634 return NULL;
22635
22636 switch (die->tag)
22637 {
22638 case DW_TAG_compile_unit:
22639 case DW_TAG_partial_unit:
22640 /* Compilation units have a DW_AT_name that is a filename, not
22641 a source language identifier. */
22642 case DW_TAG_enumeration_type:
22643 case DW_TAG_enumerator:
22644 /* These tags always have simple identifiers already; no need
22645 to canonicalize them. */
22646 return DW_STRING (attr);
22647
22648 case DW_TAG_namespace:
22649 if (attr != NULL && DW_STRING (attr) != NULL)
22650 return DW_STRING (attr);
22651 return CP_ANONYMOUS_NAMESPACE_STR;
22652
22653 case DW_TAG_class_type:
22654 case DW_TAG_interface_type:
22655 case DW_TAG_structure_type:
22656 case DW_TAG_union_type:
22657 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22658 structures or unions. These were of the form "._%d" in GCC 4.1,
22659 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22660 and GCC 4.4. We work around this problem by ignoring these. */
22661 if (attr && DW_STRING (attr)
22662 && (startswith (DW_STRING (attr), "._")
22663 || startswith (DW_STRING (attr), "<anonymous")))
22664 return NULL;
22665
22666 /* GCC might emit a nameless typedef that has a linkage name. See
22667 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22668 if (!attr || DW_STRING (attr) == NULL)
22669 {
22670 char *demangled = NULL;
22671
22672 attr = dw2_linkage_name_attr (die, cu);
22673 if (attr == NULL || DW_STRING (attr) == NULL)
22674 return NULL;
22675
22676 /* Avoid demangling DW_STRING (attr) the second time on a second
22677 call for the same DIE. */
22678 if (!DW_STRING_IS_CANONICAL (attr))
22679 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22680
22681 if (demangled)
22682 {
22683 const char *base;
22684
22685 /* FIXME: we already did this for the partial symbol... */
22686 DW_STRING (attr)
22687 = ((const char *)
22688 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22689 demangled, strlen (demangled)));
22690 DW_STRING_IS_CANONICAL (attr) = 1;
22691 xfree (demangled);
22692
22693 /* Strip any leading namespaces/classes, keep only the base name.
22694 DW_AT_name for named DIEs does not contain the prefixes. */
22695 base = strrchr (DW_STRING (attr), ':');
22696 if (base && base > DW_STRING (attr) && base[-1] == ':')
22697 return &base[1];
22698 else
22699 return DW_STRING (attr);
22700 }
22701 }
22702 break;
22703
22704 default:
22705 break;
22706 }
22707
22708 if (!DW_STRING_IS_CANONICAL (attr))
22709 {
22710 DW_STRING (attr)
22711 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22712 &objfile->per_bfd->storage_obstack);
22713 DW_STRING_IS_CANONICAL (attr) = 1;
22714 }
22715 return DW_STRING (attr);
22716 }
22717
22718 /* Return the die that this die in an extension of, or NULL if there
22719 is none. *EXT_CU is the CU containing DIE on input, and the CU
22720 containing the return value on output. */
22721
22722 static struct die_info *
22723 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22724 {
22725 struct attribute *attr;
22726
22727 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22728 if (attr == NULL)
22729 return NULL;
22730
22731 return follow_die_ref (die, attr, ext_cu);
22732 }
22733
22734 /* Convert a DIE tag into its string name. */
22735
22736 static const char *
22737 dwarf_tag_name (unsigned tag)
22738 {
22739 const char *name = get_DW_TAG_name (tag);
22740
22741 if (name == NULL)
22742 return "DW_TAG_<unknown>";
22743
22744 return name;
22745 }
22746
22747 /* Convert a DWARF attribute code into its string name. */
22748
22749 static const char *
22750 dwarf_attr_name (unsigned attr)
22751 {
22752 const char *name;
22753
22754 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22755 if (attr == DW_AT_MIPS_fde)
22756 return "DW_AT_MIPS_fde";
22757 #else
22758 if (attr == DW_AT_HP_block_index)
22759 return "DW_AT_HP_block_index";
22760 #endif
22761
22762 name = get_DW_AT_name (attr);
22763
22764 if (name == NULL)
22765 return "DW_AT_<unknown>";
22766
22767 return name;
22768 }
22769
22770 /* Convert a DWARF value form code into its string name. */
22771
22772 static const char *
22773 dwarf_form_name (unsigned form)
22774 {
22775 const char *name = get_DW_FORM_name (form);
22776
22777 if (name == NULL)
22778 return "DW_FORM_<unknown>";
22779
22780 return name;
22781 }
22782
22783 static const char *
22784 dwarf_bool_name (unsigned mybool)
22785 {
22786 if (mybool)
22787 return "TRUE";
22788 else
22789 return "FALSE";
22790 }
22791
22792 /* Convert a DWARF type code into its string name. */
22793
22794 static const char *
22795 dwarf_type_encoding_name (unsigned enc)
22796 {
22797 const char *name = get_DW_ATE_name (enc);
22798
22799 if (name == NULL)
22800 return "DW_ATE_<unknown>";
22801
22802 return name;
22803 }
22804
22805 static void
22806 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22807 {
22808 unsigned int i;
22809
22810 print_spaces (indent, f);
22811 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22812 dwarf_tag_name (die->tag), die->abbrev,
22813 sect_offset_str (die->sect_off));
22814
22815 if (die->parent != NULL)
22816 {
22817 print_spaces (indent, f);
22818 fprintf_unfiltered (f, " parent at offset: %s\n",
22819 sect_offset_str (die->parent->sect_off));
22820 }
22821
22822 print_spaces (indent, f);
22823 fprintf_unfiltered (f, " has children: %s\n",
22824 dwarf_bool_name (die->child != NULL));
22825
22826 print_spaces (indent, f);
22827 fprintf_unfiltered (f, " attributes:\n");
22828
22829 for (i = 0; i < die->num_attrs; ++i)
22830 {
22831 print_spaces (indent, f);
22832 fprintf_unfiltered (f, " %s (%s) ",
22833 dwarf_attr_name (die->attrs[i].name),
22834 dwarf_form_name (die->attrs[i].form));
22835
22836 switch (die->attrs[i].form)
22837 {
22838 case DW_FORM_addr:
22839 case DW_FORM_GNU_addr_index:
22840 fprintf_unfiltered (f, "address: ");
22841 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22842 break;
22843 case DW_FORM_block2:
22844 case DW_FORM_block4:
22845 case DW_FORM_block:
22846 case DW_FORM_block1:
22847 fprintf_unfiltered (f, "block: size %s",
22848 pulongest (DW_BLOCK (&die->attrs[i])->size));
22849 break;
22850 case DW_FORM_exprloc:
22851 fprintf_unfiltered (f, "expression: size %s",
22852 pulongest (DW_BLOCK (&die->attrs[i])->size));
22853 break;
22854 case DW_FORM_data16:
22855 fprintf_unfiltered (f, "constant of 16 bytes");
22856 break;
22857 case DW_FORM_ref_addr:
22858 fprintf_unfiltered (f, "ref address: ");
22859 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22860 break;
22861 case DW_FORM_GNU_ref_alt:
22862 fprintf_unfiltered (f, "alt ref address: ");
22863 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22864 break;
22865 case DW_FORM_ref1:
22866 case DW_FORM_ref2:
22867 case DW_FORM_ref4:
22868 case DW_FORM_ref8:
22869 case DW_FORM_ref_udata:
22870 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22871 (long) (DW_UNSND (&die->attrs[i])));
22872 break;
22873 case DW_FORM_data1:
22874 case DW_FORM_data2:
22875 case DW_FORM_data4:
22876 case DW_FORM_data8:
22877 case DW_FORM_udata:
22878 case DW_FORM_sdata:
22879 fprintf_unfiltered (f, "constant: %s",
22880 pulongest (DW_UNSND (&die->attrs[i])));
22881 break;
22882 case DW_FORM_sec_offset:
22883 fprintf_unfiltered (f, "section offset: %s",
22884 pulongest (DW_UNSND (&die->attrs[i])));
22885 break;
22886 case DW_FORM_ref_sig8:
22887 fprintf_unfiltered (f, "signature: %s",
22888 hex_string (DW_SIGNATURE (&die->attrs[i])));
22889 break;
22890 case DW_FORM_string:
22891 case DW_FORM_strp:
22892 case DW_FORM_line_strp:
22893 case DW_FORM_GNU_str_index:
22894 case DW_FORM_GNU_strp_alt:
22895 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22896 DW_STRING (&die->attrs[i])
22897 ? DW_STRING (&die->attrs[i]) : "",
22898 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22899 break;
22900 case DW_FORM_flag:
22901 if (DW_UNSND (&die->attrs[i]))
22902 fprintf_unfiltered (f, "flag: TRUE");
22903 else
22904 fprintf_unfiltered (f, "flag: FALSE");
22905 break;
22906 case DW_FORM_flag_present:
22907 fprintf_unfiltered (f, "flag: TRUE");
22908 break;
22909 case DW_FORM_indirect:
22910 /* The reader will have reduced the indirect form to
22911 the "base form" so this form should not occur. */
22912 fprintf_unfiltered (f,
22913 "unexpected attribute form: DW_FORM_indirect");
22914 break;
22915 case DW_FORM_implicit_const:
22916 fprintf_unfiltered (f, "constant: %s",
22917 plongest (DW_SND (&die->attrs[i])));
22918 break;
22919 default:
22920 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22921 die->attrs[i].form);
22922 break;
22923 }
22924 fprintf_unfiltered (f, "\n");
22925 }
22926 }
22927
22928 static void
22929 dump_die_for_error (struct die_info *die)
22930 {
22931 dump_die_shallow (gdb_stderr, 0, die);
22932 }
22933
22934 static void
22935 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22936 {
22937 int indent = level * 4;
22938
22939 gdb_assert (die != NULL);
22940
22941 if (level >= max_level)
22942 return;
22943
22944 dump_die_shallow (f, indent, die);
22945
22946 if (die->child != NULL)
22947 {
22948 print_spaces (indent, f);
22949 fprintf_unfiltered (f, " Children:");
22950 if (level + 1 < max_level)
22951 {
22952 fprintf_unfiltered (f, "\n");
22953 dump_die_1 (f, level + 1, max_level, die->child);
22954 }
22955 else
22956 {
22957 fprintf_unfiltered (f,
22958 " [not printed, max nesting level reached]\n");
22959 }
22960 }
22961
22962 if (die->sibling != NULL && level > 0)
22963 {
22964 dump_die_1 (f, level, max_level, die->sibling);
22965 }
22966 }
22967
22968 /* This is called from the pdie macro in gdbinit.in.
22969 It's not static so gcc will keep a copy callable from gdb. */
22970
22971 void
22972 dump_die (struct die_info *die, int max_level)
22973 {
22974 dump_die_1 (gdb_stdlog, 0, max_level, die);
22975 }
22976
22977 static void
22978 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22979 {
22980 void **slot;
22981
22982 slot = htab_find_slot_with_hash (cu->die_hash, die,
22983 to_underlying (die->sect_off),
22984 INSERT);
22985
22986 *slot = die;
22987 }
22988
22989 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22990 required kind. */
22991
22992 static sect_offset
22993 dwarf2_get_ref_die_offset (const struct attribute *attr)
22994 {
22995 if (attr_form_is_ref (attr))
22996 return (sect_offset) DW_UNSND (attr);
22997
22998 complaint (_("unsupported die ref attribute form: '%s'"),
22999 dwarf_form_name (attr->form));
23000 return {};
23001 }
23002
23003 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23004 * the value held by the attribute is not constant. */
23005
23006 static LONGEST
23007 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23008 {
23009 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23010 return DW_SND (attr);
23011 else if (attr->form == DW_FORM_udata
23012 || attr->form == DW_FORM_data1
23013 || attr->form == DW_FORM_data2
23014 || attr->form == DW_FORM_data4
23015 || attr->form == DW_FORM_data8)
23016 return DW_UNSND (attr);
23017 else
23018 {
23019 /* For DW_FORM_data16 see attr_form_is_constant. */
23020 complaint (_("Attribute value is not a constant (%s)"),
23021 dwarf_form_name (attr->form));
23022 return default_value;
23023 }
23024 }
23025
23026 /* Follow reference or signature attribute ATTR of SRC_DIE.
23027 On entry *REF_CU is the CU of SRC_DIE.
23028 On exit *REF_CU is the CU of the result. */
23029
23030 static struct die_info *
23031 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23032 struct dwarf2_cu **ref_cu)
23033 {
23034 struct die_info *die;
23035
23036 if (attr_form_is_ref (attr))
23037 die = follow_die_ref (src_die, attr, ref_cu);
23038 else if (attr->form == DW_FORM_ref_sig8)
23039 die = follow_die_sig (src_die, attr, ref_cu);
23040 else
23041 {
23042 dump_die_for_error (src_die);
23043 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23044 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23045 }
23046
23047 return die;
23048 }
23049
23050 /* Follow reference OFFSET.
23051 On entry *REF_CU is the CU of the source die referencing OFFSET.
23052 On exit *REF_CU is the CU of the result.
23053 Returns NULL if OFFSET is invalid. */
23054
23055 static struct die_info *
23056 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23057 struct dwarf2_cu **ref_cu)
23058 {
23059 struct die_info temp_die;
23060 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23061 struct dwarf2_per_objfile *dwarf2_per_objfile
23062 = cu->per_cu->dwarf2_per_objfile;
23063
23064 gdb_assert (cu->per_cu != NULL);
23065
23066 target_cu = cu;
23067
23068 if (cu->per_cu->is_debug_types)
23069 {
23070 /* .debug_types CUs cannot reference anything outside their CU.
23071 If they need to, they have to reference a signatured type via
23072 DW_FORM_ref_sig8. */
23073 if (!offset_in_cu_p (&cu->header, sect_off))
23074 return NULL;
23075 }
23076 else if (offset_in_dwz != cu->per_cu->is_dwz
23077 || !offset_in_cu_p (&cu->header, sect_off))
23078 {
23079 struct dwarf2_per_cu_data *per_cu;
23080
23081 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23082 dwarf2_per_objfile);
23083
23084 /* If necessary, add it to the queue and load its DIEs. */
23085 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23086 load_full_comp_unit (per_cu, false, cu->language);
23087
23088 target_cu = per_cu->cu;
23089 }
23090 else if (cu->dies == NULL)
23091 {
23092 /* We're loading full DIEs during partial symbol reading. */
23093 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23094 load_full_comp_unit (cu->per_cu, false, language_minimal);
23095 }
23096
23097 *ref_cu = target_cu;
23098 temp_die.sect_off = sect_off;
23099
23100 if (target_cu != cu)
23101 target_cu->ancestor = cu;
23102
23103 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23104 &temp_die,
23105 to_underlying (sect_off));
23106 }
23107
23108 /* Follow reference attribute ATTR of SRC_DIE.
23109 On entry *REF_CU is the CU of SRC_DIE.
23110 On exit *REF_CU is the CU of the result. */
23111
23112 static struct die_info *
23113 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23114 struct dwarf2_cu **ref_cu)
23115 {
23116 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23117 struct dwarf2_cu *cu = *ref_cu;
23118 struct die_info *die;
23119
23120 die = follow_die_offset (sect_off,
23121 (attr->form == DW_FORM_GNU_ref_alt
23122 || cu->per_cu->is_dwz),
23123 ref_cu);
23124 if (!die)
23125 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23126 "at %s [in module %s]"),
23127 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23128 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23129
23130 return die;
23131 }
23132
23133 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23134 Returned value is intended for DW_OP_call*. Returned
23135 dwarf2_locexpr_baton->data has lifetime of
23136 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23137
23138 struct dwarf2_locexpr_baton
23139 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23140 struct dwarf2_per_cu_data *per_cu,
23141 CORE_ADDR (*get_frame_pc) (void *baton),
23142 void *baton, bool resolve_abstract_p)
23143 {
23144 struct dwarf2_cu *cu;
23145 struct die_info *die;
23146 struct attribute *attr;
23147 struct dwarf2_locexpr_baton retval;
23148 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23149 struct objfile *objfile = dwarf2_per_objfile->objfile;
23150
23151 if (per_cu->cu == NULL)
23152 load_cu (per_cu, false);
23153 cu = per_cu->cu;
23154 if (cu == NULL)
23155 {
23156 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23157 Instead just throw an error, not much else we can do. */
23158 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23159 sect_offset_str (sect_off), objfile_name (objfile));
23160 }
23161
23162 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23163 if (!die)
23164 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23165 sect_offset_str (sect_off), objfile_name (objfile));
23166
23167 attr = dwarf2_attr (die, DW_AT_location, cu);
23168 if (!attr && resolve_abstract_p
23169 && (dwarf2_per_objfile->abstract_to_concrete.find (die)
23170 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23171 {
23172 CORE_ADDR pc = (*get_frame_pc) (baton);
23173
23174 for (const auto &cand : dwarf2_per_objfile->abstract_to_concrete[die])
23175 {
23176 if (!cand->parent
23177 || cand->parent->tag != DW_TAG_subprogram)
23178 continue;
23179
23180 CORE_ADDR pc_low, pc_high;
23181 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23182 if (pc_low == ((CORE_ADDR) -1)
23183 || !(pc_low <= pc && pc < pc_high))
23184 continue;
23185
23186 die = cand;
23187 attr = dwarf2_attr (die, DW_AT_location, cu);
23188 break;
23189 }
23190 }
23191
23192 if (!attr)
23193 {
23194 /* DWARF: "If there is no such attribute, then there is no effect.".
23195 DATA is ignored if SIZE is 0. */
23196
23197 retval.data = NULL;
23198 retval.size = 0;
23199 }
23200 else if (attr_form_is_section_offset (attr))
23201 {
23202 struct dwarf2_loclist_baton loclist_baton;
23203 CORE_ADDR pc = (*get_frame_pc) (baton);
23204 size_t size;
23205
23206 fill_in_loclist_baton (cu, &loclist_baton, attr);
23207
23208 retval.data = dwarf2_find_location_expression (&loclist_baton,
23209 &size, pc);
23210 retval.size = size;
23211 }
23212 else
23213 {
23214 if (!attr_form_is_block (attr))
23215 error (_("Dwarf Error: DIE at %s referenced in module %s "
23216 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23217 sect_offset_str (sect_off), objfile_name (objfile));
23218
23219 retval.data = DW_BLOCK (attr)->data;
23220 retval.size = DW_BLOCK (attr)->size;
23221 }
23222 retval.per_cu = cu->per_cu;
23223
23224 age_cached_comp_units (dwarf2_per_objfile);
23225
23226 return retval;
23227 }
23228
23229 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23230 offset. */
23231
23232 struct dwarf2_locexpr_baton
23233 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23234 struct dwarf2_per_cu_data *per_cu,
23235 CORE_ADDR (*get_frame_pc) (void *baton),
23236 void *baton)
23237 {
23238 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23239
23240 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23241 }
23242
23243 /* Write a constant of a given type as target-ordered bytes into
23244 OBSTACK. */
23245
23246 static const gdb_byte *
23247 write_constant_as_bytes (struct obstack *obstack,
23248 enum bfd_endian byte_order,
23249 struct type *type,
23250 ULONGEST value,
23251 LONGEST *len)
23252 {
23253 gdb_byte *result;
23254
23255 *len = TYPE_LENGTH (type);
23256 result = (gdb_byte *) obstack_alloc (obstack, *len);
23257 store_unsigned_integer (result, *len, byte_order, value);
23258
23259 return result;
23260 }
23261
23262 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23263 pointer to the constant bytes and set LEN to the length of the
23264 data. If memory is needed, allocate it on OBSTACK. If the DIE
23265 does not have a DW_AT_const_value, return NULL. */
23266
23267 const gdb_byte *
23268 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23269 struct dwarf2_per_cu_data *per_cu,
23270 struct obstack *obstack,
23271 LONGEST *len)
23272 {
23273 struct dwarf2_cu *cu;
23274 struct die_info *die;
23275 struct attribute *attr;
23276 const gdb_byte *result = NULL;
23277 struct type *type;
23278 LONGEST value;
23279 enum bfd_endian byte_order;
23280 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23281
23282 if (per_cu->cu == NULL)
23283 load_cu (per_cu, false);
23284 cu = per_cu->cu;
23285 if (cu == NULL)
23286 {
23287 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23288 Instead just throw an error, not much else we can do. */
23289 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23290 sect_offset_str (sect_off), objfile_name (objfile));
23291 }
23292
23293 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23294 if (!die)
23295 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23296 sect_offset_str (sect_off), objfile_name (objfile));
23297
23298 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23299 if (attr == NULL)
23300 return NULL;
23301
23302 byte_order = (bfd_big_endian (objfile->obfd)
23303 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23304
23305 switch (attr->form)
23306 {
23307 case DW_FORM_addr:
23308 case DW_FORM_GNU_addr_index:
23309 {
23310 gdb_byte *tem;
23311
23312 *len = cu->header.addr_size;
23313 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23314 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23315 result = tem;
23316 }
23317 break;
23318 case DW_FORM_string:
23319 case DW_FORM_strp:
23320 case DW_FORM_GNU_str_index:
23321 case DW_FORM_GNU_strp_alt:
23322 /* DW_STRING is already allocated on the objfile obstack, point
23323 directly to it. */
23324 result = (const gdb_byte *) DW_STRING (attr);
23325 *len = strlen (DW_STRING (attr));
23326 break;
23327 case DW_FORM_block1:
23328 case DW_FORM_block2:
23329 case DW_FORM_block4:
23330 case DW_FORM_block:
23331 case DW_FORM_exprloc:
23332 case DW_FORM_data16:
23333 result = DW_BLOCK (attr)->data;
23334 *len = DW_BLOCK (attr)->size;
23335 break;
23336
23337 /* The DW_AT_const_value attributes are supposed to carry the
23338 symbol's value "represented as it would be on the target
23339 architecture." By the time we get here, it's already been
23340 converted to host endianness, so we just need to sign- or
23341 zero-extend it as appropriate. */
23342 case DW_FORM_data1:
23343 type = die_type (die, cu);
23344 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23345 if (result == NULL)
23346 result = write_constant_as_bytes (obstack, byte_order,
23347 type, value, len);
23348 break;
23349 case DW_FORM_data2:
23350 type = die_type (die, cu);
23351 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23352 if (result == NULL)
23353 result = write_constant_as_bytes (obstack, byte_order,
23354 type, value, len);
23355 break;
23356 case DW_FORM_data4:
23357 type = die_type (die, cu);
23358 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23359 if (result == NULL)
23360 result = write_constant_as_bytes (obstack, byte_order,
23361 type, value, len);
23362 break;
23363 case DW_FORM_data8:
23364 type = die_type (die, cu);
23365 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23366 if (result == NULL)
23367 result = write_constant_as_bytes (obstack, byte_order,
23368 type, value, len);
23369 break;
23370
23371 case DW_FORM_sdata:
23372 case DW_FORM_implicit_const:
23373 type = die_type (die, cu);
23374 result = write_constant_as_bytes (obstack, byte_order,
23375 type, DW_SND (attr), len);
23376 break;
23377
23378 case DW_FORM_udata:
23379 type = die_type (die, cu);
23380 result = write_constant_as_bytes (obstack, byte_order,
23381 type, DW_UNSND (attr), len);
23382 break;
23383
23384 default:
23385 complaint (_("unsupported const value attribute form: '%s'"),
23386 dwarf_form_name (attr->form));
23387 break;
23388 }
23389
23390 return result;
23391 }
23392
23393 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23394 valid type for this die is found. */
23395
23396 struct type *
23397 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23398 struct dwarf2_per_cu_data *per_cu)
23399 {
23400 struct dwarf2_cu *cu;
23401 struct die_info *die;
23402
23403 if (per_cu->cu == NULL)
23404 load_cu (per_cu, false);
23405 cu = per_cu->cu;
23406 if (!cu)
23407 return NULL;
23408
23409 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23410 if (!die)
23411 return NULL;
23412
23413 return die_type (die, cu);
23414 }
23415
23416 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23417 PER_CU. */
23418
23419 struct type *
23420 dwarf2_get_die_type (cu_offset die_offset,
23421 struct dwarf2_per_cu_data *per_cu)
23422 {
23423 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23424 return get_die_type_at_offset (die_offset_sect, per_cu);
23425 }
23426
23427 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23428 On entry *REF_CU is the CU of SRC_DIE.
23429 On exit *REF_CU is the CU of the result.
23430 Returns NULL if the referenced DIE isn't found. */
23431
23432 static struct die_info *
23433 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23434 struct dwarf2_cu **ref_cu)
23435 {
23436 struct die_info temp_die;
23437 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23438 struct die_info *die;
23439
23440 /* While it might be nice to assert sig_type->type == NULL here,
23441 we can get here for DW_AT_imported_declaration where we need
23442 the DIE not the type. */
23443
23444 /* If necessary, add it to the queue and load its DIEs. */
23445
23446 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23447 read_signatured_type (sig_type);
23448
23449 sig_cu = sig_type->per_cu.cu;
23450 gdb_assert (sig_cu != NULL);
23451 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23452 temp_die.sect_off = sig_type->type_offset_in_section;
23453 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23454 to_underlying (temp_die.sect_off));
23455 if (die)
23456 {
23457 struct dwarf2_per_objfile *dwarf2_per_objfile
23458 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23459
23460 /* For .gdb_index version 7 keep track of included TUs.
23461 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23462 if (dwarf2_per_objfile->index_table != NULL
23463 && dwarf2_per_objfile->index_table->version <= 7)
23464 {
23465 VEC_safe_push (dwarf2_per_cu_ptr,
23466 (*ref_cu)->per_cu->imported_symtabs,
23467 sig_cu->per_cu);
23468 }
23469
23470 *ref_cu = sig_cu;
23471 if (sig_cu != cu)
23472 sig_cu->ancestor = cu;
23473
23474 return die;
23475 }
23476
23477 return NULL;
23478 }
23479
23480 /* Follow signatured type referenced by ATTR in SRC_DIE.
23481 On entry *REF_CU is the CU of SRC_DIE.
23482 On exit *REF_CU is the CU of the result.
23483 The result is the DIE of the type.
23484 If the referenced type cannot be found an error is thrown. */
23485
23486 static struct die_info *
23487 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23488 struct dwarf2_cu **ref_cu)
23489 {
23490 ULONGEST signature = DW_SIGNATURE (attr);
23491 struct signatured_type *sig_type;
23492 struct die_info *die;
23493
23494 gdb_assert (attr->form == DW_FORM_ref_sig8);
23495
23496 sig_type = lookup_signatured_type (*ref_cu, signature);
23497 /* sig_type will be NULL if the signatured type is missing from
23498 the debug info. */
23499 if (sig_type == NULL)
23500 {
23501 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23502 " from DIE at %s [in module %s]"),
23503 hex_string (signature), sect_offset_str (src_die->sect_off),
23504 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23505 }
23506
23507 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23508 if (die == NULL)
23509 {
23510 dump_die_for_error (src_die);
23511 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23512 " from DIE at %s [in module %s]"),
23513 hex_string (signature), sect_offset_str (src_die->sect_off),
23514 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23515 }
23516
23517 return die;
23518 }
23519
23520 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23521 reading in and processing the type unit if necessary. */
23522
23523 static struct type *
23524 get_signatured_type (struct die_info *die, ULONGEST signature,
23525 struct dwarf2_cu *cu)
23526 {
23527 struct dwarf2_per_objfile *dwarf2_per_objfile
23528 = cu->per_cu->dwarf2_per_objfile;
23529 struct signatured_type *sig_type;
23530 struct dwarf2_cu *type_cu;
23531 struct die_info *type_die;
23532 struct type *type;
23533
23534 sig_type = lookup_signatured_type (cu, signature);
23535 /* sig_type will be NULL if the signatured type is missing from
23536 the debug info. */
23537 if (sig_type == NULL)
23538 {
23539 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23540 " from DIE at %s [in module %s]"),
23541 hex_string (signature), sect_offset_str (die->sect_off),
23542 objfile_name (dwarf2_per_objfile->objfile));
23543 return build_error_marker_type (cu, die);
23544 }
23545
23546 /* If we already know the type we're done. */
23547 if (sig_type->type != NULL)
23548 return sig_type->type;
23549
23550 type_cu = cu;
23551 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23552 if (type_die != NULL)
23553 {
23554 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23555 is created. This is important, for example, because for c++ classes
23556 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23557 type = read_type_die (type_die, type_cu);
23558 if (type == NULL)
23559 {
23560 complaint (_("Dwarf Error: Cannot build signatured type %s"
23561 " referenced from DIE at %s [in module %s]"),
23562 hex_string (signature), sect_offset_str (die->sect_off),
23563 objfile_name (dwarf2_per_objfile->objfile));
23564 type = build_error_marker_type (cu, die);
23565 }
23566 }
23567 else
23568 {
23569 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23570 " from DIE at %s [in module %s]"),
23571 hex_string (signature), sect_offset_str (die->sect_off),
23572 objfile_name (dwarf2_per_objfile->objfile));
23573 type = build_error_marker_type (cu, die);
23574 }
23575 sig_type->type = type;
23576
23577 return type;
23578 }
23579
23580 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23581 reading in and processing the type unit if necessary. */
23582
23583 static struct type *
23584 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23585 struct dwarf2_cu *cu) /* ARI: editCase function */
23586 {
23587 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23588 if (attr_form_is_ref (attr))
23589 {
23590 struct dwarf2_cu *type_cu = cu;
23591 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23592
23593 return read_type_die (type_die, type_cu);
23594 }
23595 else if (attr->form == DW_FORM_ref_sig8)
23596 {
23597 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23598 }
23599 else
23600 {
23601 struct dwarf2_per_objfile *dwarf2_per_objfile
23602 = cu->per_cu->dwarf2_per_objfile;
23603
23604 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23605 " at %s [in module %s]"),
23606 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23607 objfile_name (dwarf2_per_objfile->objfile));
23608 return build_error_marker_type (cu, die);
23609 }
23610 }
23611
23612 /* Load the DIEs associated with type unit PER_CU into memory. */
23613
23614 static void
23615 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23616 {
23617 struct signatured_type *sig_type;
23618
23619 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23620 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23621
23622 /* We have the per_cu, but we need the signatured_type.
23623 Fortunately this is an easy translation. */
23624 gdb_assert (per_cu->is_debug_types);
23625 sig_type = (struct signatured_type *) per_cu;
23626
23627 gdb_assert (per_cu->cu == NULL);
23628
23629 read_signatured_type (sig_type);
23630
23631 gdb_assert (per_cu->cu != NULL);
23632 }
23633
23634 /* die_reader_func for read_signatured_type.
23635 This is identical to load_full_comp_unit_reader,
23636 but is kept separate for now. */
23637
23638 static void
23639 read_signatured_type_reader (const struct die_reader_specs *reader,
23640 const gdb_byte *info_ptr,
23641 struct die_info *comp_unit_die,
23642 int has_children,
23643 void *data)
23644 {
23645 struct dwarf2_cu *cu = reader->cu;
23646
23647 gdb_assert (cu->die_hash == NULL);
23648 cu->die_hash =
23649 htab_create_alloc_ex (cu->header.length / 12,
23650 die_hash,
23651 die_eq,
23652 NULL,
23653 &cu->comp_unit_obstack,
23654 hashtab_obstack_allocate,
23655 dummy_obstack_deallocate);
23656
23657 if (has_children)
23658 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23659 &info_ptr, comp_unit_die);
23660 cu->dies = comp_unit_die;
23661 /* comp_unit_die is not stored in die_hash, no need. */
23662
23663 /* We try not to read any attributes in this function, because not
23664 all CUs needed for references have been loaded yet, and symbol
23665 table processing isn't initialized. But we have to set the CU language,
23666 or we won't be able to build types correctly.
23667 Similarly, if we do not read the producer, we can not apply
23668 producer-specific interpretation. */
23669 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23670 }
23671
23672 /* Read in a signatured type and build its CU and DIEs.
23673 If the type is a stub for the real type in a DWO file,
23674 read in the real type from the DWO file as well. */
23675
23676 static void
23677 read_signatured_type (struct signatured_type *sig_type)
23678 {
23679 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23680
23681 gdb_assert (per_cu->is_debug_types);
23682 gdb_assert (per_cu->cu == NULL);
23683
23684 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
23685 read_signatured_type_reader, NULL);
23686 sig_type->per_cu.tu_read = 1;
23687 }
23688
23689 /* Decode simple location descriptions.
23690 Given a pointer to a dwarf block that defines a location, compute
23691 the location and return the value.
23692
23693 NOTE drow/2003-11-18: This function is called in two situations
23694 now: for the address of static or global variables (partial symbols
23695 only) and for offsets into structures which are expected to be
23696 (more or less) constant. The partial symbol case should go away,
23697 and only the constant case should remain. That will let this
23698 function complain more accurately. A few special modes are allowed
23699 without complaint for global variables (for instance, global
23700 register values and thread-local values).
23701
23702 A location description containing no operations indicates that the
23703 object is optimized out. The return value is 0 for that case.
23704 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23705 callers will only want a very basic result and this can become a
23706 complaint.
23707
23708 Note that stack[0] is unused except as a default error return. */
23709
23710 static CORE_ADDR
23711 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23712 {
23713 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23714 size_t i;
23715 size_t size = blk->size;
23716 const gdb_byte *data = blk->data;
23717 CORE_ADDR stack[64];
23718 int stacki;
23719 unsigned int bytes_read, unsnd;
23720 gdb_byte op;
23721
23722 i = 0;
23723 stacki = 0;
23724 stack[stacki] = 0;
23725 stack[++stacki] = 0;
23726
23727 while (i < size)
23728 {
23729 op = data[i++];
23730 switch (op)
23731 {
23732 case DW_OP_lit0:
23733 case DW_OP_lit1:
23734 case DW_OP_lit2:
23735 case DW_OP_lit3:
23736 case DW_OP_lit4:
23737 case DW_OP_lit5:
23738 case DW_OP_lit6:
23739 case DW_OP_lit7:
23740 case DW_OP_lit8:
23741 case DW_OP_lit9:
23742 case DW_OP_lit10:
23743 case DW_OP_lit11:
23744 case DW_OP_lit12:
23745 case DW_OP_lit13:
23746 case DW_OP_lit14:
23747 case DW_OP_lit15:
23748 case DW_OP_lit16:
23749 case DW_OP_lit17:
23750 case DW_OP_lit18:
23751 case DW_OP_lit19:
23752 case DW_OP_lit20:
23753 case DW_OP_lit21:
23754 case DW_OP_lit22:
23755 case DW_OP_lit23:
23756 case DW_OP_lit24:
23757 case DW_OP_lit25:
23758 case DW_OP_lit26:
23759 case DW_OP_lit27:
23760 case DW_OP_lit28:
23761 case DW_OP_lit29:
23762 case DW_OP_lit30:
23763 case DW_OP_lit31:
23764 stack[++stacki] = op - DW_OP_lit0;
23765 break;
23766
23767 case DW_OP_reg0:
23768 case DW_OP_reg1:
23769 case DW_OP_reg2:
23770 case DW_OP_reg3:
23771 case DW_OP_reg4:
23772 case DW_OP_reg5:
23773 case DW_OP_reg6:
23774 case DW_OP_reg7:
23775 case DW_OP_reg8:
23776 case DW_OP_reg9:
23777 case DW_OP_reg10:
23778 case DW_OP_reg11:
23779 case DW_OP_reg12:
23780 case DW_OP_reg13:
23781 case DW_OP_reg14:
23782 case DW_OP_reg15:
23783 case DW_OP_reg16:
23784 case DW_OP_reg17:
23785 case DW_OP_reg18:
23786 case DW_OP_reg19:
23787 case DW_OP_reg20:
23788 case DW_OP_reg21:
23789 case DW_OP_reg22:
23790 case DW_OP_reg23:
23791 case DW_OP_reg24:
23792 case DW_OP_reg25:
23793 case DW_OP_reg26:
23794 case DW_OP_reg27:
23795 case DW_OP_reg28:
23796 case DW_OP_reg29:
23797 case DW_OP_reg30:
23798 case DW_OP_reg31:
23799 stack[++stacki] = op - DW_OP_reg0;
23800 if (i < size)
23801 dwarf2_complex_location_expr_complaint ();
23802 break;
23803
23804 case DW_OP_regx:
23805 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23806 i += bytes_read;
23807 stack[++stacki] = unsnd;
23808 if (i < size)
23809 dwarf2_complex_location_expr_complaint ();
23810 break;
23811
23812 case DW_OP_addr:
23813 stack[++stacki] = read_address (objfile->obfd, &data[i],
23814 cu, &bytes_read);
23815 i += bytes_read;
23816 break;
23817
23818 case DW_OP_const1u:
23819 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23820 i += 1;
23821 break;
23822
23823 case DW_OP_const1s:
23824 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23825 i += 1;
23826 break;
23827
23828 case DW_OP_const2u:
23829 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23830 i += 2;
23831 break;
23832
23833 case DW_OP_const2s:
23834 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23835 i += 2;
23836 break;
23837
23838 case DW_OP_const4u:
23839 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23840 i += 4;
23841 break;
23842
23843 case DW_OP_const4s:
23844 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23845 i += 4;
23846 break;
23847
23848 case DW_OP_const8u:
23849 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23850 i += 8;
23851 break;
23852
23853 case DW_OP_constu:
23854 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23855 &bytes_read);
23856 i += bytes_read;
23857 break;
23858
23859 case DW_OP_consts:
23860 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23861 i += bytes_read;
23862 break;
23863
23864 case DW_OP_dup:
23865 stack[stacki + 1] = stack[stacki];
23866 stacki++;
23867 break;
23868
23869 case DW_OP_plus:
23870 stack[stacki - 1] += stack[stacki];
23871 stacki--;
23872 break;
23873
23874 case DW_OP_plus_uconst:
23875 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23876 &bytes_read);
23877 i += bytes_read;
23878 break;
23879
23880 case DW_OP_minus:
23881 stack[stacki - 1] -= stack[stacki];
23882 stacki--;
23883 break;
23884
23885 case DW_OP_deref:
23886 /* If we're not the last op, then we definitely can't encode
23887 this using GDB's address_class enum. This is valid for partial
23888 global symbols, although the variable's address will be bogus
23889 in the psymtab. */
23890 if (i < size)
23891 dwarf2_complex_location_expr_complaint ();
23892 break;
23893
23894 case DW_OP_GNU_push_tls_address:
23895 case DW_OP_form_tls_address:
23896 /* The top of the stack has the offset from the beginning
23897 of the thread control block at which the variable is located. */
23898 /* Nothing should follow this operator, so the top of stack would
23899 be returned. */
23900 /* This is valid for partial global symbols, but the variable's
23901 address will be bogus in the psymtab. Make it always at least
23902 non-zero to not look as a variable garbage collected by linker
23903 which have DW_OP_addr 0. */
23904 if (i < size)
23905 dwarf2_complex_location_expr_complaint ();
23906 stack[stacki]++;
23907 break;
23908
23909 case DW_OP_GNU_uninit:
23910 break;
23911
23912 case DW_OP_GNU_addr_index:
23913 case DW_OP_GNU_const_index:
23914 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23915 &bytes_read);
23916 i += bytes_read;
23917 break;
23918
23919 default:
23920 {
23921 const char *name = get_DW_OP_name (op);
23922
23923 if (name)
23924 complaint (_("unsupported stack op: '%s'"),
23925 name);
23926 else
23927 complaint (_("unsupported stack op: '%02x'"),
23928 op);
23929 }
23930
23931 return (stack[stacki]);
23932 }
23933
23934 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23935 outside of the allocated space. Also enforce minimum>0. */
23936 if (stacki >= ARRAY_SIZE (stack) - 1)
23937 {
23938 complaint (_("location description stack overflow"));
23939 return 0;
23940 }
23941
23942 if (stacki <= 0)
23943 {
23944 complaint (_("location description stack underflow"));
23945 return 0;
23946 }
23947 }
23948 return (stack[stacki]);
23949 }
23950
23951 /* memory allocation interface */
23952
23953 static struct dwarf_block *
23954 dwarf_alloc_block (struct dwarf2_cu *cu)
23955 {
23956 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23957 }
23958
23959 static struct die_info *
23960 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23961 {
23962 struct die_info *die;
23963 size_t size = sizeof (struct die_info);
23964
23965 if (num_attrs > 1)
23966 size += (num_attrs - 1) * sizeof (struct attribute);
23967
23968 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23969 memset (die, 0, sizeof (struct die_info));
23970 return (die);
23971 }
23972
23973 \f
23974 /* Macro support. */
23975
23976 /* Return file name relative to the compilation directory of file number I in
23977 *LH's file name table. The result is allocated using xmalloc; the caller is
23978 responsible for freeing it. */
23979
23980 static char *
23981 file_file_name (int file, struct line_header *lh)
23982 {
23983 /* Is the file number a valid index into the line header's file name
23984 table? Remember that file numbers start with one, not zero. */
23985 if (1 <= file && file <= lh->file_names.size ())
23986 {
23987 const file_entry &fe = lh->file_names[file - 1];
23988
23989 if (!IS_ABSOLUTE_PATH (fe.name))
23990 {
23991 const char *dir = fe.include_dir (lh);
23992 if (dir != NULL)
23993 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23994 }
23995 return xstrdup (fe.name);
23996 }
23997 else
23998 {
23999 /* The compiler produced a bogus file number. We can at least
24000 record the macro definitions made in the file, even if we
24001 won't be able to find the file by name. */
24002 char fake_name[80];
24003
24004 xsnprintf (fake_name, sizeof (fake_name),
24005 "<bad macro file number %d>", file);
24006
24007 complaint (_("bad file number in macro information (%d)"),
24008 file);
24009
24010 return xstrdup (fake_name);
24011 }
24012 }
24013
24014 /* Return the full name of file number I in *LH's file name table.
24015 Use COMP_DIR as the name of the current directory of the
24016 compilation. The result is allocated using xmalloc; the caller is
24017 responsible for freeing it. */
24018 static char *
24019 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24020 {
24021 /* Is the file number a valid index into the line header's file name
24022 table? Remember that file numbers start with one, not zero. */
24023 if (1 <= file && file <= lh->file_names.size ())
24024 {
24025 char *relative = file_file_name (file, lh);
24026
24027 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24028 return relative;
24029 return reconcat (relative, comp_dir, SLASH_STRING,
24030 relative, (char *) NULL);
24031 }
24032 else
24033 return file_file_name (file, lh);
24034 }
24035
24036
24037 static struct macro_source_file *
24038 macro_start_file (struct dwarf2_cu *cu,
24039 int file, int line,
24040 struct macro_source_file *current_file,
24041 struct line_header *lh)
24042 {
24043 /* File name relative to the compilation directory of this source file. */
24044 char *file_name = file_file_name (file, lh);
24045
24046 if (! current_file)
24047 {
24048 /* Note: We don't create a macro table for this compilation unit
24049 at all until we actually get a filename. */
24050 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24051
24052 /* If we have no current file, then this must be the start_file
24053 directive for the compilation unit's main source file. */
24054 current_file = macro_set_main (macro_table, file_name);
24055 macro_define_special (macro_table);
24056 }
24057 else
24058 current_file = macro_include (current_file, line, file_name);
24059
24060 xfree (file_name);
24061
24062 return current_file;
24063 }
24064
24065 static const char *
24066 consume_improper_spaces (const char *p, const char *body)
24067 {
24068 if (*p == ' ')
24069 {
24070 complaint (_("macro definition contains spaces "
24071 "in formal argument list:\n`%s'"),
24072 body);
24073
24074 while (*p == ' ')
24075 p++;
24076 }
24077
24078 return p;
24079 }
24080
24081
24082 static void
24083 parse_macro_definition (struct macro_source_file *file, int line,
24084 const char *body)
24085 {
24086 const char *p;
24087
24088 /* The body string takes one of two forms. For object-like macro
24089 definitions, it should be:
24090
24091 <macro name> " " <definition>
24092
24093 For function-like macro definitions, it should be:
24094
24095 <macro name> "() " <definition>
24096 or
24097 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24098
24099 Spaces may appear only where explicitly indicated, and in the
24100 <definition>.
24101
24102 The Dwarf 2 spec says that an object-like macro's name is always
24103 followed by a space, but versions of GCC around March 2002 omit
24104 the space when the macro's definition is the empty string.
24105
24106 The Dwarf 2 spec says that there should be no spaces between the
24107 formal arguments in a function-like macro's formal argument list,
24108 but versions of GCC around March 2002 include spaces after the
24109 commas. */
24110
24111
24112 /* Find the extent of the macro name. The macro name is terminated
24113 by either a space or null character (for an object-like macro) or
24114 an opening paren (for a function-like macro). */
24115 for (p = body; *p; p++)
24116 if (*p == ' ' || *p == '(')
24117 break;
24118
24119 if (*p == ' ' || *p == '\0')
24120 {
24121 /* It's an object-like macro. */
24122 int name_len = p - body;
24123 char *name = savestring (body, name_len);
24124 const char *replacement;
24125
24126 if (*p == ' ')
24127 replacement = body + name_len + 1;
24128 else
24129 {
24130 dwarf2_macro_malformed_definition_complaint (body);
24131 replacement = body + name_len;
24132 }
24133
24134 macro_define_object (file, line, name, replacement);
24135
24136 xfree (name);
24137 }
24138 else if (*p == '(')
24139 {
24140 /* It's a function-like macro. */
24141 char *name = savestring (body, p - body);
24142 int argc = 0;
24143 int argv_size = 1;
24144 char **argv = XNEWVEC (char *, argv_size);
24145
24146 p++;
24147
24148 p = consume_improper_spaces (p, body);
24149
24150 /* Parse the formal argument list. */
24151 while (*p && *p != ')')
24152 {
24153 /* Find the extent of the current argument name. */
24154 const char *arg_start = p;
24155
24156 while (*p && *p != ',' && *p != ')' && *p != ' ')
24157 p++;
24158
24159 if (! *p || p == arg_start)
24160 dwarf2_macro_malformed_definition_complaint (body);
24161 else
24162 {
24163 /* Make sure argv has room for the new argument. */
24164 if (argc >= argv_size)
24165 {
24166 argv_size *= 2;
24167 argv = XRESIZEVEC (char *, argv, argv_size);
24168 }
24169
24170 argv[argc++] = savestring (arg_start, p - arg_start);
24171 }
24172
24173 p = consume_improper_spaces (p, body);
24174
24175 /* Consume the comma, if present. */
24176 if (*p == ',')
24177 {
24178 p++;
24179
24180 p = consume_improper_spaces (p, body);
24181 }
24182 }
24183
24184 if (*p == ')')
24185 {
24186 p++;
24187
24188 if (*p == ' ')
24189 /* Perfectly formed definition, no complaints. */
24190 macro_define_function (file, line, name,
24191 argc, (const char **) argv,
24192 p + 1);
24193 else if (*p == '\0')
24194 {
24195 /* Complain, but do define it. */
24196 dwarf2_macro_malformed_definition_complaint (body);
24197 macro_define_function (file, line, name,
24198 argc, (const char **) argv,
24199 p);
24200 }
24201 else
24202 /* Just complain. */
24203 dwarf2_macro_malformed_definition_complaint (body);
24204 }
24205 else
24206 /* Just complain. */
24207 dwarf2_macro_malformed_definition_complaint (body);
24208
24209 xfree (name);
24210 {
24211 int i;
24212
24213 for (i = 0; i < argc; i++)
24214 xfree (argv[i]);
24215 }
24216 xfree (argv);
24217 }
24218 else
24219 dwarf2_macro_malformed_definition_complaint (body);
24220 }
24221
24222 /* Skip some bytes from BYTES according to the form given in FORM.
24223 Returns the new pointer. */
24224
24225 static const gdb_byte *
24226 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24227 enum dwarf_form form,
24228 unsigned int offset_size,
24229 struct dwarf2_section_info *section)
24230 {
24231 unsigned int bytes_read;
24232
24233 switch (form)
24234 {
24235 case DW_FORM_data1:
24236 case DW_FORM_flag:
24237 ++bytes;
24238 break;
24239
24240 case DW_FORM_data2:
24241 bytes += 2;
24242 break;
24243
24244 case DW_FORM_data4:
24245 bytes += 4;
24246 break;
24247
24248 case DW_FORM_data8:
24249 bytes += 8;
24250 break;
24251
24252 case DW_FORM_data16:
24253 bytes += 16;
24254 break;
24255
24256 case DW_FORM_string:
24257 read_direct_string (abfd, bytes, &bytes_read);
24258 bytes += bytes_read;
24259 break;
24260
24261 case DW_FORM_sec_offset:
24262 case DW_FORM_strp:
24263 case DW_FORM_GNU_strp_alt:
24264 bytes += offset_size;
24265 break;
24266
24267 case DW_FORM_block:
24268 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24269 bytes += bytes_read;
24270 break;
24271
24272 case DW_FORM_block1:
24273 bytes += 1 + read_1_byte (abfd, bytes);
24274 break;
24275 case DW_FORM_block2:
24276 bytes += 2 + read_2_bytes (abfd, bytes);
24277 break;
24278 case DW_FORM_block4:
24279 bytes += 4 + read_4_bytes (abfd, bytes);
24280 break;
24281
24282 case DW_FORM_sdata:
24283 case DW_FORM_udata:
24284 case DW_FORM_GNU_addr_index:
24285 case DW_FORM_GNU_str_index:
24286 bytes = gdb_skip_leb128 (bytes, buffer_end);
24287 if (bytes == NULL)
24288 {
24289 dwarf2_section_buffer_overflow_complaint (section);
24290 return NULL;
24291 }
24292 break;
24293
24294 case DW_FORM_implicit_const:
24295 break;
24296
24297 default:
24298 {
24299 complaint (_("invalid form 0x%x in `%s'"),
24300 form, get_section_name (section));
24301 return NULL;
24302 }
24303 }
24304
24305 return bytes;
24306 }
24307
24308 /* A helper for dwarf_decode_macros that handles skipping an unknown
24309 opcode. Returns an updated pointer to the macro data buffer; or,
24310 on error, issues a complaint and returns NULL. */
24311
24312 static const gdb_byte *
24313 skip_unknown_opcode (unsigned int opcode,
24314 const gdb_byte **opcode_definitions,
24315 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24316 bfd *abfd,
24317 unsigned int offset_size,
24318 struct dwarf2_section_info *section)
24319 {
24320 unsigned int bytes_read, i;
24321 unsigned long arg;
24322 const gdb_byte *defn;
24323
24324 if (opcode_definitions[opcode] == NULL)
24325 {
24326 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24327 opcode);
24328 return NULL;
24329 }
24330
24331 defn = opcode_definitions[opcode];
24332 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24333 defn += bytes_read;
24334
24335 for (i = 0; i < arg; ++i)
24336 {
24337 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24338 (enum dwarf_form) defn[i], offset_size,
24339 section);
24340 if (mac_ptr == NULL)
24341 {
24342 /* skip_form_bytes already issued the complaint. */
24343 return NULL;
24344 }
24345 }
24346
24347 return mac_ptr;
24348 }
24349
24350 /* A helper function which parses the header of a macro section.
24351 If the macro section is the extended (for now called "GNU") type,
24352 then this updates *OFFSET_SIZE. Returns a pointer to just after
24353 the header, or issues a complaint and returns NULL on error. */
24354
24355 static const gdb_byte *
24356 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24357 bfd *abfd,
24358 const gdb_byte *mac_ptr,
24359 unsigned int *offset_size,
24360 int section_is_gnu)
24361 {
24362 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24363
24364 if (section_is_gnu)
24365 {
24366 unsigned int version, flags;
24367
24368 version = read_2_bytes (abfd, mac_ptr);
24369 if (version != 4 && version != 5)
24370 {
24371 complaint (_("unrecognized version `%d' in .debug_macro section"),
24372 version);
24373 return NULL;
24374 }
24375 mac_ptr += 2;
24376
24377 flags = read_1_byte (abfd, mac_ptr);
24378 ++mac_ptr;
24379 *offset_size = (flags & 1) ? 8 : 4;
24380
24381 if ((flags & 2) != 0)
24382 /* We don't need the line table offset. */
24383 mac_ptr += *offset_size;
24384
24385 /* Vendor opcode descriptions. */
24386 if ((flags & 4) != 0)
24387 {
24388 unsigned int i, count;
24389
24390 count = read_1_byte (abfd, mac_ptr);
24391 ++mac_ptr;
24392 for (i = 0; i < count; ++i)
24393 {
24394 unsigned int opcode, bytes_read;
24395 unsigned long arg;
24396
24397 opcode = read_1_byte (abfd, mac_ptr);
24398 ++mac_ptr;
24399 opcode_definitions[opcode] = mac_ptr;
24400 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24401 mac_ptr += bytes_read;
24402 mac_ptr += arg;
24403 }
24404 }
24405 }
24406
24407 return mac_ptr;
24408 }
24409
24410 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24411 including DW_MACRO_import. */
24412
24413 static void
24414 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24415 bfd *abfd,
24416 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24417 struct macro_source_file *current_file,
24418 struct line_header *lh,
24419 struct dwarf2_section_info *section,
24420 int section_is_gnu, int section_is_dwz,
24421 unsigned int offset_size,
24422 htab_t include_hash)
24423 {
24424 struct dwarf2_per_objfile *dwarf2_per_objfile
24425 = cu->per_cu->dwarf2_per_objfile;
24426 struct objfile *objfile = dwarf2_per_objfile->objfile;
24427 enum dwarf_macro_record_type macinfo_type;
24428 int at_commandline;
24429 const gdb_byte *opcode_definitions[256];
24430
24431 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24432 &offset_size, section_is_gnu);
24433 if (mac_ptr == NULL)
24434 {
24435 /* We already issued a complaint. */
24436 return;
24437 }
24438
24439 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24440 GDB is still reading the definitions from command line. First
24441 DW_MACINFO_start_file will need to be ignored as it was already executed
24442 to create CURRENT_FILE for the main source holding also the command line
24443 definitions. On first met DW_MACINFO_start_file this flag is reset to
24444 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24445
24446 at_commandline = 1;
24447
24448 do
24449 {
24450 /* Do we at least have room for a macinfo type byte? */
24451 if (mac_ptr >= mac_end)
24452 {
24453 dwarf2_section_buffer_overflow_complaint (section);
24454 break;
24455 }
24456
24457 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24458 mac_ptr++;
24459
24460 /* Note that we rely on the fact that the corresponding GNU and
24461 DWARF constants are the same. */
24462 DIAGNOSTIC_PUSH
24463 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24464 switch (macinfo_type)
24465 {
24466 /* A zero macinfo type indicates the end of the macro
24467 information. */
24468 case 0:
24469 break;
24470
24471 case DW_MACRO_define:
24472 case DW_MACRO_undef:
24473 case DW_MACRO_define_strp:
24474 case DW_MACRO_undef_strp:
24475 case DW_MACRO_define_sup:
24476 case DW_MACRO_undef_sup:
24477 {
24478 unsigned int bytes_read;
24479 int line;
24480 const char *body;
24481 int is_define;
24482
24483 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24484 mac_ptr += bytes_read;
24485
24486 if (macinfo_type == DW_MACRO_define
24487 || macinfo_type == DW_MACRO_undef)
24488 {
24489 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24490 mac_ptr += bytes_read;
24491 }
24492 else
24493 {
24494 LONGEST str_offset;
24495
24496 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24497 mac_ptr += offset_size;
24498
24499 if (macinfo_type == DW_MACRO_define_sup
24500 || macinfo_type == DW_MACRO_undef_sup
24501 || section_is_dwz)
24502 {
24503 struct dwz_file *dwz
24504 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24505
24506 body = read_indirect_string_from_dwz (objfile,
24507 dwz, str_offset);
24508 }
24509 else
24510 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24511 abfd, str_offset);
24512 }
24513
24514 is_define = (macinfo_type == DW_MACRO_define
24515 || macinfo_type == DW_MACRO_define_strp
24516 || macinfo_type == DW_MACRO_define_sup);
24517 if (! current_file)
24518 {
24519 /* DWARF violation as no main source is present. */
24520 complaint (_("debug info with no main source gives macro %s "
24521 "on line %d: %s"),
24522 is_define ? _("definition") : _("undefinition"),
24523 line, body);
24524 break;
24525 }
24526 if ((line == 0 && !at_commandline)
24527 || (line != 0 && at_commandline))
24528 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24529 at_commandline ? _("command-line") : _("in-file"),
24530 is_define ? _("definition") : _("undefinition"),
24531 line == 0 ? _("zero") : _("non-zero"), line, body);
24532
24533 if (is_define)
24534 parse_macro_definition (current_file, line, body);
24535 else
24536 {
24537 gdb_assert (macinfo_type == DW_MACRO_undef
24538 || macinfo_type == DW_MACRO_undef_strp
24539 || macinfo_type == DW_MACRO_undef_sup);
24540 macro_undef (current_file, line, body);
24541 }
24542 }
24543 break;
24544
24545 case DW_MACRO_start_file:
24546 {
24547 unsigned int bytes_read;
24548 int line, file;
24549
24550 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24551 mac_ptr += bytes_read;
24552 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24553 mac_ptr += bytes_read;
24554
24555 if ((line == 0 && !at_commandline)
24556 || (line != 0 && at_commandline))
24557 complaint (_("debug info gives source %d included "
24558 "from %s at %s line %d"),
24559 file, at_commandline ? _("command-line") : _("file"),
24560 line == 0 ? _("zero") : _("non-zero"), line);
24561
24562 if (at_commandline)
24563 {
24564 /* This DW_MACRO_start_file was executed in the
24565 pass one. */
24566 at_commandline = 0;
24567 }
24568 else
24569 current_file = macro_start_file (cu, file, line, current_file,
24570 lh);
24571 }
24572 break;
24573
24574 case DW_MACRO_end_file:
24575 if (! current_file)
24576 complaint (_("macro debug info has an unmatched "
24577 "`close_file' directive"));
24578 else
24579 {
24580 current_file = current_file->included_by;
24581 if (! current_file)
24582 {
24583 enum dwarf_macro_record_type next_type;
24584
24585 /* GCC circa March 2002 doesn't produce the zero
24586 type byte marking the end of the compilation
24587 unit. Complain if it's not there, but exit no
24588 matter what. */
24589
24590 /* Do we at least have room for a macinfo type byte? */
24591 if (mac_ptr >= mac_end)
24592 {
24593 dwarf2_section_buffer_overflow_complaint (section);
24594 return;
24595 }
24596
24597 /* We don't increment mac_ptr here, so this is just
24598 a look-ahead. */
24599 next_type
24600 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24601 mac_ptr);
24602 if (next_type != 0)
24603 complaint (_("no terminating 0-type entry for "
24604 "macros in `.debug_macinfo' section"));
24605
24606 return;
24607 }
24608 }
24609 break;
24610
24611 case DW_MACRO_import:
24612 case DW_MACRO_import_sup:
24613 {
24614 LONGEST offset;
24615 void **slot;
24616 bfd *include_bfd = abfd;
24617 struct dwarf2_section_info *include_section = section;
24618 const gdb_byte *include_mac_end = mac_end;
24619 int is_dwz = section_is_dwz;
24620 const gdb_byte *new_mac_ptr;
24621
24622 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24623 mac_ptr += offset_size;
24624
24625 if (macinfo_type == DW_MACRO_import_sup)
24626 {
24627 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24628
24629 dwarf2_read_section (objfile, &dwz->macro);
24630
24631 include_section = &dwz->macro;
24632 include_bfd = get_section_bfd_owner (include_section);
24633 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24634 is_dwz = 1;
24635 }
24636
24637 new_mac_ptr = include_section->buffer + offset;
24638 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24639
24640 if (*slot != NULL)
24641 {
24642 /* This has actually happened; see
24643 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24644 complaint (_("recursive DW_MACRO_import in "
24645 ".debug_macro section"));
24646 }
24647 else
24648 {
24649 *slot = (void *) new_mac_ptr;
24650
24651 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24652 include_mac_end, current_file, lh,
24653 section, section_is_gnu, is_dwz,
24654 offset_size, include_hash);
24655
24656 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24657 }
24658 }
24659 break;
24660
24661 case DW_MACINFO_vendor_ext:
24662 if (!section_is_gnu)
24663 {
24664 unsigned int bytes_read;
24665
24666 /* This reads the constant, but since we don't recognize
24667 any vendor extensions, we ignore it. */
24668 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24669 mac_ptr += bytes_read;
24670 read_direct_string (abfd, mac_ptr, &bytes_read);
24671 mac_ptr += bytes_read;
24672
24673 /* We don't recognize any vendor extensions. */
24674 break;
24675 }
24676 /* FALLTHROUGH */
24677
24678 default:
24679 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24680 mac_ptr, mac_end, abfd, offset_size,
24681 section);
24682 if (mac_ptr == NULL)
24683 return;
24684 break;
24685 }
24686 DIAGNOSTIC_POP
24687 } while (macinfo_type != 0);
24688 }
24689
24690 static void
24691 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24692 int section_is_gnu)
24693 {
24694 struct dwarf2_per_objfile *dwarf2_per_objfile
24695 = cu->per_cu->dwarf2_per_objfile;
24696 struct objfile *objfile = dwarf2_per_objfile->objfile;
24697 struct line_header *lh = cu->line_header;
24698 bfd *abfd;
24699 const gdb_byte *mac_ptr, *mac_end;
24700 struct macro_source_file *current_file = 0;
24701 enum dwarf_macro_record_type macinfo_type;
24702 unsigned int offset_size = cu->header.offset_size;
24703 const gdb_byte *opcode_definitions[256];
24704 void **slot;
24705 struct dwarf2_section_info *section;
24706 const char *section_name;
24707
24708 if (cu->dwo_unit != NULL)
24709 {
24710 if (section_is_gnu)
24711 {
24712 section = &cu->dwo_unit->dwo_file->sections.macro;
24713 section_name = ".debug_macro.dwo";
24714 }
24715 else
24716 {
24717 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24718 section_name = ".debug_macinfo.dwo";
24719 }
24720 }
24721 else
24722 {
24723 if (section_is_gnu)
24724 {
24725 section = &dwarf2_per_objfile->macro;
24726 section_name = ".debug_macro";
24727 }
24728 else
24729 {
24730 section = &dwarf2_per_objfile->macinfo;
24731 section_name = ".debug_macinfo";
24732 }
24733 }
24734
24735 dwarf2_read_section (objfile, section);
24736 if (section->buffer == NULL)
24737 {
24738 complaint (_("missing %s section"), section_name);
24739 return;
24740 }
24741 abfd = get_section_bfd_owner (section);
24742
24743 /* First pass: Find the name of the base filename.
24744 This filename is needed in order to process all macros whose definition
24745 (or undefinition) comes from the command line. These macros are defined
24746 before the first DW_MACINFO_start_file entry, and yet still need to be
24747 associated to the base file.
24748
24749 To determine the base file name, we scan the macro definitions until we
24750 reach the first DW_MACINFO_start_file entry. We then initialize
24751 CURRENT_FILE accordingly so that any macro definition found before the
24752 first DW_MACINFO_start_file can still be associated to the base file. */
24753
24754 mac_ptr = section->buffer + offset;
24755 mac_end = section->buffer + section->size;
24756
24757 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24758 &offset_size, section_is_gnu);
24759 if (mac_ptr == NULL)
24760 {
24761 /* We already issued a complaint. */
24762 return;
24763 }
24764
24765 do
24766 {
24767 /* Do we at least have room for a macinfo type byte? */
24768 if (mac_ptr >= mac_end)
24769 {
24770 /* Complaint is printed during the second pass as GDB will probably
24771 stop the first pass earlier upon finding
24772 DW_MACINFO_start_file. */
24773 break;
24774 }
24775
24776 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24777 mac_ptr++;
24778
24779 /* Note that we rely on the fact that the corresponding GNU and
24780 DWARF constants are the same. */
24781 DIAGNOSTIC_PUSH
24782 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24783 switch (macinfo_type)
24784 {
24785 /* A zero macinfo type indicates the end of the macro
24786 information. */
24787 case 0:
24788 break;
24789
24790 case DW_MACRO_define:
24791 case DW_MACRO_undef:
24792 /* Only skip the data by MAC_PTR. */
24793 {
24794 unsigned int bytes_read;
24795
24796 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24797 mac_ptr += bytes_read;
24798 read_direct_string (abfd, mac_ptr, &bytes_read);
24799 mac_ptr += bytes_read;
24800 }
24801 break;
24802
24803 case DW_MACRO_start_file:
24804 {
24805 unsigned int bytes_read;
24806 int line, file;
24807
24808 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24809 mac_ptr += bytes_read;
24810 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24811 mac_ptr += bytes_read;
24812
24813 current_file = macro_start_file (cu, file, line, current_file, lh);
24814 }
24815 break;
24816
24817 case DW_MACRO_end_file:
24818 /* No data to skip by MAC_PTR. */
24819 break;
24820
24821 case DW_MACRO_define_strp:
24822 case DW_MACRO_undef_strp:
24823 case DW_MACRO_define_sup:
24824 case DW_MACRO_undef_sup:
24825 {
24826 unsigned int bytes_read;
24827
24828 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24829 mac_ptr += bytes_read;
24830 mac_ptr += offset_size;
24831 }
24832 break;
24833
24834 case DW_MACRO_import:
24835 case DW_MACRO_import_sup:
24836 /* Note that, according to the spec, a transparent include
24837 chain cannot call DW_MACRO_start_file. So, we can just
24838 skip this opcode. */
24839 mac_ptr += offset_size;
24840 break;
24841
24842 case DW_MACINFO_vendor_ext:
24843 /* Only skip the data by MAC_PTR. */
24844 if (!section_is_gnu)
24845 {
24846 unsigned int bytes_read;
24847
24848 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24849 mac_ptr += bytes_read;
24850 read_direct_string (abfd, mac_ptr, &bytes_read);
24851 mac_ptr += bytes_read;
24852 }
24853 /* FALLTHROUGH */
24854
24855 default:
24856 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24857 mac_ptr, mac_end, abfd, offset_size,
24858 section);
24859 if (mac_ptr == NULL)
24860 return;
24861 break;
24862 }
24863 DIAGNOSTIC_POP
24864 } while (macinfo_type != 0 && current_file == NULL);
24865
24866 /* Second pass: Process all entries.
24867
24868 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24869 command-line macro definitions/undefinitions. This flag is unset when we
24870 reach the first DW_MACINFO_start_file entry. */
24871
24872 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24873 htab_eq_pointer,
24874 NULL, xcalloc, xfree));
24875 mac_ptr = section->buffer + offset;
24876 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24877 *slot = (void *) mac_ptr;
24878 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
24879 current_file, lh, section,
24880 section_is_gnu, 0, offset_size,
24881 include_hash.get ());
24882 }
24883
24884 /* Check if the attribute's form is a DW_FORM_block*
24885 if so return true else false. */
24886
24887 static int
24888 attr_form_is_block (const struct attribute *attr)
24889 {
24890 return (attr == NULL ? 0 :
24891 attr->form == DW_FORM_block1
24892 || attr->form == DW_FORM_block2
24893 || attr->form == DW_FORM_block4
24894 || attr->form == DW_FORM_block
24895 || attr->form == DW_FORM_exprloc);
24896 }
24897
24898 /* Return non-zero if ATTR's value is a section offset --- classes
24899 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24900 You may use DW_UNSND (attr) to retrieve such offsets.
24901
24902 Section 7.5.4, "Attribute Encodings", explains that no attribute
24903 may have a value that belongs to more than one of these classes; it
24904 would be ambiguous if we did, because we use the same forms for all
24905 of them. */
24906
24907 static int
24908 attr_form_is_section_offset (const struct attribute *attr)
24909 {
24910 return (attr->form == DW_FORM_data4
24911 || attr->form == DW_FORM_data8
24912 || attr->form == DW_FORM_sec_offset);
24913 }
24914
24915 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24916 zero otherwise. When this function returns true, you can apply
24917 dwarf2_get_attr_constant_value to it.
24918
24919 However, note that for some attributes you must check
24920 attr_form_is_section_offset before using this test. DW_FORM_data4
24921 and DW_FORM_data8 are members of both the constant class, and of
24922 the classes that contain offsets into other debug sections
24923 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24924 that, if an attribute's can be either a constant or one of the
24925 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24926 taken as section offsets, not constants.
24927
24928 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24929 cannot handle that. */
24930
24931 static int
24932 attr_form_is_constant (const struct attribute *attr)
24933 {
24934 switch (attr->form)
24935 {
24936 case DW_FORM_sdata:
24937 case DW_FORM_udata:
24938 case DW_FORM_data1:
24939 case DW_FORM_data2:
24940 case DW_FORM_data4:
24941 case DW_FORM_data8:
24942 case DW_FORM_implicit_const:
24943 return 1;
24944 default:
24945 return 0;
24946 }
24947 }
24948
24949
24950 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24951 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24952
24953 static int
24954 attr_form_is_ref (const struct attribute *attr)
24955 {
24956 switch (attr->form)
24957 {
24958 case DW_FORM_ref_addr:
24959 case DW_FORM_ref1:
24960 case DW_FORM_ref2:
24961 case DW_FORM_ref4:
24962 case DW_FORM_ref8:
24963 case DW_FORM_ref_udata:
24964 case DW_FORM_GNU_ref_alt:
24965 return 1;
24966 default:
24967 return 0;
24968 }
24969 }
24970
24971 /* Return the .debug_loc section to use for CU.
24972 For DWO files use .debug_loc.dwo. */
24973
24974 static struct dwarf2_section_info *
24975 cu_debug_loc_section (struct dwarf2_cu *cu)
24976 {
24977 struct dwarf2_per_objfile *dwarf2_per_objfile
24978 = cu->per_cu->dwarf2_per_objfile;
24979
24980 if (cu->dwo_unit)
24981 {
24982 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24983
24984 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24985 }
24986 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24987 : &dwarf2_per_objfile->loc);
24988 }
24989
24990 /* A helper function that fills in a dwarf2_loclist_baton. */
24991
24992 static void
24993 fill_in_loclist_baton (struct dwarf2_cu *cu,
24994 struct dwarf2_loclist_baton *baton,
24995 const struct attribute *attr)
24996 {
24997 struct dwarf2_per_objfile *dwarf2_per_objfile
24998 = cu->per_cu->dwarf2_per_objfile;
24999 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25000
25001 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25002
25003 baton->per_cu = cu->per_cu;
25004 gdb_assert (baton->per_cu);
25005 /* We don't know how long the location list is, but make sure we
25006 don't run off the edge of the section. */
25007 baton->size = section->size - DW_UNSND (attr);
25008 baton->data = section->buffer + DW_UNSND (attr);
25009 baton->base_address = cu->base_address;
25010 baton->from_dwo = cu->dwo_unit != NULL;
25011 }
25012
25013 static void
25014 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25015 struct dwarf2_cu *cu, int is_block)
25016 {
25017 struct dwarf2_per_objfile *dwarf2_per_objfile
25018 = cu->per_cu->dwarf2_per_objfile;
25019 struct objfile *objfile = dwarf2_per_objfile->objfile;
25020 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25021
25022 if (attr_form_is_section_offset (attr)
25023 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25024 the section. If so, fall through to the complaint in the
25025 other branch. */
25026 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25027 {
25028 struct dwarf2_loclist_baton *baton;
25029
25030 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25031
25032 fill_in_loclist_baton (cu, baton, attr);
25033
25034 if (cu->base_known == 0)
25035 complaint (_("Location list used without "
25036 "specifying the CU base address."));
25037
25038 SYMBOL_ACLASS_INDEX (sym) = (is_block
25039 ? dwarf2_loclist_block_index
25040 : dwarf2_loclist_index);
25041 SYMBOL_LOCATION_BATON (sym) = baton;
25042 }
25043 else
25044 {
25045 struct dwarf2_locexpr_baton *baton;
25046
25047 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25048 baton->per_cu = cu->per_cu;
25049 gdb_assert (baton->per_cu);
25050
25051 if (attr_form_is_block (attr))
25052 {
25053 /* Note that we're just copying the block's data pointer
25054 here, not the actual data. We're still pointing into the
25055 info_buffer for SYM's objfile; right now we never release
25056 that buffer, but when we do clean up properly this may
25057 need to change. */
25058 baton->size = DW_BLOCK (attr)->size;
25059 baton->data = DW_BLOCK (attr)->data;
25060 }
25061 else
25062 {
25063 dwarf2_invalid_attrib_class_complaint ("location description",
25064 SYMBOL_NATURAL_NAME (sym));
25065 baton->size = 0;
25066 }
25067
25068 SYMBOL_ACLASS_INDEX (sym) = (is_block
25069 ? dwarf2_locexpr_block_index
25070 : dwarf2_locexpr_index);
25071 SYMBOL_LOCATION_BATON (sym) = baton;
25072 }
25073 }
25074
25075 /* Return the OBJFILE associated with the compilation unit CU. If CU
25076 came from a separate debuginfo file, then the master objfile is
25077 returned. */
25078
25079 struct objfile *
25080 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25081 {
25082 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25083
25084 /* Return the master objfile, so that we can report and look up the
25085 correct file containing this variable. */
25086 if (objfile->separate_debug_objfile_backlink)
25087 objfile = objfile->separate_debug_objfile_backlink;
25088
25089 return objfile;
25090 }
25091
25092 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25093 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25094 CU_HEADERP first. */
25095
25096 static const struct comp_unit_head *
25097 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25098 struct dwarf2_per_cu_data *per_cu)
25099 {
25100 const gdb_byte *info_ptr;
25101
25102 if (per_cu->cu)
25103 return &per_cu->cu->header;
25104
25105 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25106
25107 memset (cu_headerp, 0, sizeof (*cu_headerp));
25108 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25109 rcuh_kind::COMPILE);
25110
25111 return cu_headerp;
25112 }
25113
25114 /* Return the address size given in the compilation unit header for CU. */
25115
25116 int
25117 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25118 {
25119 struct comp_unit_head cu_header_local;
25120 const struct comp_unit_head *cu_headerp;
25121
25122 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25123
25124 return cu_headerp->addr_size;
25125 }
25126
25127 /* Return the offset size given in the compilation unit header for CU. */
25128
25129 int
25130 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25131 {
25132 struct comp_unit_head cu_header_local;
25133 const struct comp_unit_head *cu_headerp;
25134
25135 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25136
25137 return cu_headerp->offset_size;
25138 }
25139
25140 /* See its dwarf2loc.h declaration. */
25141
25142 int
25143 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25144 {
25145 struct comp_unit_head cu_header_local;
25146 const struct comp_unit_head *cu_headerp;
25147
25148 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25149
25150 if (cu_headerp->version == 2)
25151 return cu_headerp->addr_size;
25152 else
25153 return cu_headerp->offset_size;
25154 }
25155
25156 /* Return the text offset of the CU. The returned offset comes from
25157 this CU's objfile. If this objfile came from a separate debuginfo
25158 file, then the offset may be different from the corresponding
25159 offset in the parent objfile. */
25160
25161 CORE_ADDR
25162 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25163 {
25164 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25165
25166 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25167 }
25168
25169 /* Return DWARF version number of PER_CU. */
25170
25171 short
25172 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25173 {
25174 return per_cu->dwarf_version;
25175 }
25176
25177 /* Locate the .debug_info compilation unit from CU's objfile which contains
25178 the DIE at OFFSET. Raises an error on failure. */
25179
25180 static struct dwarf2_per_cu_data *
25181 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25182 unsigned int offset_in_dwz,
25183 struct dwarf2_per_objfile *dwarf2_per_objfile)
25184 {
25185 struct dwarf2_per_cu_data *this_cu;
25186 int low, high;
25187
25188 low = 0;
25189 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25190 while (high > low)
25191 {
25192 struct dwarf2_per_cu_data *mid_cu;
25193 int mid = low + (high - low) / 2;
25194
25195 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25196 if (mid_cu->is_dwz > offset_in_dwz
25197 || (mid_cu->is_dwz == offset_in_dwz
25198 && mid_cu->sect_off + mid_cu->length >= sect_off))
25199 high = mid;
25200 else
25201 low = mid + 1;
25202 }
25203 gdb_assert (low == high);
25204 this_cu = dwarf2_per_objfile->all_comp_units[low];
25205 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25206 {
25207 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25208 error (_("Dwarf Error: could not find partial DIE containing "
25209 "offset %s [in module %s]"),
25210 sect_offset_str (sect_off),
25211 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25212
25213 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25214 <= sect_off);
25215 return dwarf2_per_objfile->all_comp_units[low-1];
25216 }
25217 else
25218 {
25219 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25220 && sect_off >= this_cu->sect_off + this_cu->length)
25221 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25222 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25223 return this_cu;
25224 }
25225 }
25226
25227 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25228
25229 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25230 : per_cu (per_cu_),
25231 mark (false),
25232 has_loclist (false),
25233 checked_producer (false),
25234 producer_is_gxx_lt_4_6 (false),
25235 producer_is_gcc_lt_4_3 (false),
25236 producer_is_icc (false),
25237 producer_is_icc_lt_14 (false),
25238 producer_is_codewarrior (false),
25239 processing_has_namespace_info (false)
25240 {
25241 per_cu->cu = this;
25242 }
25243
25244 /* Destroy a dwarf2_cu. */
25245
25246 dwarf2_cu::~dwarf2_cu ()
25247 {
25248 per_cu->cu = NULL;
25249 }
25250
25251 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25252
25253 static void
25254 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25255 enum language pretend_language)
25256 {
25257 struct attribute *attr;
25258
25259 /* Set the language we're debugging. */
25260 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25261 if (attr)
25262 set_cu_language (DW_UNSND (attr), cu);
25263 else
25264 {
25265 cu->language = pretend_language;
25266 cu->language_defn = language_def (cu->language);
25267 }
25268
25269 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25270 }
25271
25272 /* Increase the age counter on each cached compilation unit, and free
25273 any that are too old. */
25274
25275 static void
25276 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25277 {
25278 struct dwarf2_per_cu_data *per_cu, **last_chain;
25279
25280 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25281 per_cu = dwarf2_per_objfile->read_in_chain;
25282 while (per_cu != NULL)
25283 {
25284 per_cu->cu->last_used ++;
25285 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25286 dwarf2_mark (per_cu->cu);
25287 per_cu = per_cu->cu->read_in_chain;
25288 }
25289
25290 per_cu = dwarf2_per_objfile->read_in_chain;
25291 last_chain = &dwarf2_per_objfile->read_in_chain;
25292 while (per_cu != NULL)
25293 {
25294 struct dwarf2_per_cu_data *next_cu;
25295
25296 next_cu = per_cu->cu->read_in_chain;
25297
25298 if (!per_cu->cu->mark)
25299 {
25300 delete per_cu->cu;
25301 *last_chain = next_cu;
25302 }
25303 else
25304 last_chain = &per_cu->cu->read_in_chain;
25305
25306 per_cu = next_cu;
25307 }
25308 }
25309
25310 /* Remove a single compilation unit from the cache. */
25311
25312 static void
25313 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25314 {
25315 struct dwarf2_per_cu_data *per_cu, **last_chain;
25316 struct dwarf2_per_objfile *dwarf2_per_objfile
25317 = target_per_cu->dwarf2_per_objfile;
25318
25319 per_cu = dwarf2_per_objfile->read_in_chain;
25320 last_chain = &dwarf2_per_objfile->read_in_chain;
25321 while (per_cu != NULL)
25322 {
25323 struct dwarf2_per_cu_data *next_cu;
25324
25325 next_cu = per_cu->cu->read_in_chain;
25326
25327 if (per_cu == target_per_cu)
25328 {
25329 delete per_cu->cu;
25330 per_cu->cu = NULL;
25331 *last_chain = next_cu;
25332 break;
25333 }
25334 else
25335 last_chain = &per_cu->cu->read_in_chain;
25336
25337 per_cu = next_cu;
25338 }
25339 }
25340
25341 /* Cleanup function for the dwarf2_per_objfile data. */
25342
25343 static void
25344 dwarf2_free_objfile (struct objfile *objfile, void *datum)
25345 {
25346 struct dwarf2_per_objfile *dwarf2_per_objfile
25347 = static_cast<struct dwarf2_per_objfile *> (datum);
25348
25349 delete dwarf2_per_objfile;
25350 }
25351
25352 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25353 We store these in a hash table separate from the DIEs, and preserve them
25354 when the DIEs are flushed out of cache.
25355
25356 The CU "per_cu" pointer is needed because offset alone is not enough to
25357 uniquely identify the type. A file may have multiple .debug_types sections,
25358 or the type may come from a DWO file. Furthermore, while it's more logical
25359 to use per_cu->section+offset, with Fission the section with the data is in
25360 the DWO file but we don't know that section at the point we need it.
25361 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25362 because we can enter the lookup routine, get_die_type_at_offset, from
25363 outside this file, and thus won't necessarily have PER_CU->cu.
25364 Fortunately, PER_CU is stable for the life of the objfile. */
25365
25366 struct dwarf2_per_cu_offset_and_type
25367 {
25368 const struct dwarf2_per_cu_data *per_cu;
25369 sect_offset sect_off;
25370 struct type *type;
25371 };
25372
25373 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25374
25375 static hashval_t
25376 per_cu_offset_and_type_hash (const void *item)
25377 {
25378 const struct dwarf2_per_cu_offset_and_type *ofs
25379 = (const struct dwarf2_per_cu_offset_and_type *) item;
25380
25381 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25382 }
25383
25384 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25385
25386 static int
25387 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25388 {
25389 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25390 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25391 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25392 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25393
25394 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25395 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25396 }
25397
25398 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25399 table if necessary. For convenience, return TYPE.
25400
25401 The DIEs reading must have careful ordering to:
25402 * Not cause infite loops trying to read in DIEs as a prerequisite for
25403 reading current DIE.
25404 * Not trying to dereference contents of still incompletely read in types
25405 while reading in other DIEs.
25406 * Enable referencing still incompletely read in types just by a pointer to
25407 the type without accessing its fields.
25408
25409 Therefore caller should follow these rules:
25410 * Try to fetch any prerequisite types we may need to build this DIE type
25411 before building the type and calling set_die_type.
25412 * After building type call set_die_type for current DIE as soon as
25413 possible before fetching more types to complete the current type.
25414 * Make the type as complete as possible before fetching more types. */
25415
25416 static struct type *
25417 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25418 {
25419 struct dwarf2_per_objfile *dwarf2_per_objfile
25420 = cu->per_cu->dwarf2_per_objfile;
25421 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25422 struct objfile *objfile = dwarf2_per_objfile->objfile;
25423 struct attribute *attr;
25424 struct dynamic_prop prop;
25425
25426 /* For Ada types, make sure that the gnat-specific data is always
25427 initialized (if not already set). There are a few types where
25428 we should not be doing so, because the type-specific area is
25429 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25430 where the type-specific area is used to store the floatformat).
25431 But this is not a problem, because the gnat-specific information
25432 is actually not needed for these types. */
25433 if (need_gnat_info (cu)
25434 && TYPE_CODE (type) != TYPE_CODE_FUNC
25435 && TYPE_CODE (type) != TYPE_CODE_FLT
25436 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25437 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25438 && TYPE_CODE (type) != TYPE_CODE_METHOD
25439 && !HAVE_GNAT_AUX_INFO (type))
25440 INIT_GNAT_SPECIFIC (type);
25441
25442 /* Read DW_AT_allocated and set in type. */
25443 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25444 if (attr_form_is_block (attr))
25445 {
25446 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25447 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25448 }
25449 else if (attr != NULL)
25450 {
25451 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25452 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25453 sect_offset_str (die->sect_off));
25454 }
25455
25456 /* Read DW_AT_associated and set in type. */
25457 attr = dwarf2_attr (die, DW_AT_associated, cu);
25458 if (attr_form_is_block (attr))
25459 {
25460 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25461 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25462 }
25463 else if (attr != NULL)
25464 {
25465 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25466 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25467 sect_offset_str (die->sect_off));
25468 }
25469
25470 /* Read DW_AT_data_location and set in type. */
25471 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25472 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25473 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25474
25475 if (dwarf2_per_objfile->die_type_hash == NULL)
25476 {
25477 dwarf2_per_objfile->die_type_hash =
25478 htab_create_alloc_ex (127,
25479 per_cu_offset_and_type_hash,
25480 per_cu_offset_and_type_eq,
25481 NULL,
25482 &objfile->objfile_obstack,
25483 hashtab_obstack_allocate,
25484 dummy_obstack_deallocate);
25485 }
25486
25487 ofs.per_cu = cu->per_cu;
25488 ofs.sect_off = die->sect_off;
25489 ofs.type = type;
25490 slot = (struct dwarf2_per_cu_offset_and_type **)
25491 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25492 if (*slot)
25493 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25494 sect_offset_str (die->sect_off));
25495 *slot = XOBNEW (&objfile->objfile_obstack,
25496 struct dwarf2_per_cu_offset_and_type);
25497 **slot = ofs;
25498 return type;
25499 }
25500
25501 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25502 or return NULL if the die does not have a saved type. */
25503
25504 static struct type *
25505 get_die_type_at_offset (sect_offset sect_off,
25506 struct dwarf2_per_cu_data *per_cu)
25507 {
25508 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25509 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25510
25511 if (dwarf2_per_objfile->die_type_hash == NULL)
25512 return NULL;
25513
25514 ofs.per_cu = per_cu;
25515 ofs.sect_off = sect_off;
25516 slot = ((struct dwarf2_per_cu_offset_and_type *)
25517 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25518 if (slot)
25519 return slot->type;
25520 else
25521 return NULL;
25522 }
25523
25524 /* Look up the type for DIE in CU in die_type_hash,
25525 or return NULL if DIE does not have a saved type. */
25526
25527 static struct type *
25528 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25529 {
25530 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25531 }
25532
25533 /* Add a dependence relationship from CU to REF_PER_CU. */
25534
25535 static void
25536 dwarf2_add_dependence (struct dwarf2_cu *cu,
25537 struct dwarf2_per_cu_data *ref_per_cu)
25538 {
25539 void **slot;
25540
25541 if (cu->dependencies == NULL)
25542 cu->dependencies
25543 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25544 NULL, &cu->comp_unit_obstack,
25545 hashtab_obstack_allocate,
25546 dummy_obstack_deallocate);
25547
25548 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25549 if (*slot == NULL)
25550 *slot = ref_per_cu;
25551 }
25552
25553 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25554 Set the mark field in every compilation unit in the
25555 cache that we must keep because we are keeping CU. */
25556
25557 static int
25558 dwarf2_mark_helper (void **slot, void *data)
25559 {
25560 struct dwarf2_per_cu_data *per_cu;
25561
25562 per_cu = (struct dwarf2_per_cu_data *) *slot;
25563
25564 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25565 reading of the chain. As such dependencies remain valid it is not much
25566 useful to track and undo them during QUIT cleanups. */
25567 if (per_cu->cu == NULL)
25568 return 1;
25569
25570 if (per_cu->cu->mark)
25571 return 1;
25572 per_cu->cu->mark = true;
25573
25574 if (per_cu->cu->dependencies != NULL)
25575 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25576
25577 return 1;
25578 }
25579
25580 /* Set the mark field in CU and in every other compilation unit in the
25581 cache that we must keep because we are keeping CU. */
25582
25583 static void
25584 dwarf2_mark (struct dwarf2_cu *cu)
25585 {
25586 if (cu->mark)
25587 return;
25588 cu->mark = true;
25589 if (cu->dependencies != NULL)
25590 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25591 }
25592
25593 static void
25594 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25595 {
25596 while (per_cu)
25597 {
25598 per_cu->cu->mark = false;
25599 per_cu = per_cu->cu->read_in_chain;
25600 }
25601 }
25602
25603 /* Trivial hash function for partial_die_info: the hash value of a DIE
25604 is its offset in .debug_info for this objfile. */
25605
25606 static hashval_t
25607 partial_die_hash (const void *item)
25608 {
25609 const struct partial_die_info *part_die
25610 = (const struct partial_die_info *) item;
25611
25612 return to_underlying (part_die->sect_off);
25613 }
25614
25615 /* Trivial comparison function for partial_die_info structures: two DIEs
25616 are equal if they have the same offset. */
25617
25618 static int
25619 partial_die_eq (const void *item_lhs, const void *item_rhs)
25620 {
25621 const struct partial_die_info *part_die_lhs
25622 = (const struct partial_die_info *) item_lhs;
25623 const struct partial_die_info *part_die_rhs
25624 = (const struct partial_die_info *) item_rhs;
25625
25626 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25627 }
25628
25629 struct cmd_list_element *set_dwarf_cmdlist;
25630 struct cmd_list_element *show_dwarf_cmdlist;
25631
25632 static void
25633 set_dwarf_cmd (const char *args, int from_tty)
25634 {
25635 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25636 gdb_stdout);
25637 }
25638
25639 static void
25640 show_dwarf_cmd (const char *args, int from_tty)
25641 {
25642 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25643 }
25644
25645 int dwarf_always_disassemble;
25646
25647 static void
25648 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25649 struct cmd_list_element *c, const char *value)
25650 {
25651 fprintf_filtered (file,
25652 _("Whether to always disassemble "
25653 "DWARF expressions is %s.\n"),
25654 value);
25655 }
25656
25657 static void
25658 show_check_physname (struct ui_file *file, int from_tty,
25659 struct cmd_list_element *c, const char *value)
25660 {
25661 fprintf_filtered (file,
25662 _("Whether to check \"physname\" is %s.\n"),
25663 value);
25664 }
25665
25666 void
25667 _initialize_dwarf2_read (void)
25668 {
25669 dwarf2_objfile_data_key
25670 = register_objfile_data_with_cleanup (nullptr, dwarf2_free_objfile);
25671
25672 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25673 Set DWARF specific variables.\n\
25674 Configure DWARF variables such as the cache size"),
25675 &set_dwarf_cmdlist, "maintenance set dwarf ",
25676 0/*allow-unknown*/, &maintenance_set_cmdlist);
25677
25678 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25679 Show DWARF specific variables\n\
25680 Show DWARF variables such as the cache size"),
25681 &show_dwarf_cmdlist, "maintenance show dwarf ",
25682 0/*allow-unknown*/, &maintenance_show_cmdlist);
25683
25684 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25685 &dwarf_max_cache_age, _("\
25686 Set the upper bound on the age of cached DWARF compilation units."), _("\
25687 Show the upper bound on the age of cached DWARF compilation units."), _("\
25688 A higher limit means that cached compilation units will be stored\n\
25689 in memory longer, and more total memory will be used. Zero disables\n\
25690 caching, which can slow down startup."),
25691 NULL,
25692 show_dwarf_max_cache_age,
25693 &set_dwarf_cmdlist,
25694 &show_dwarf_cmdlist);
25695
25696 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25697 &dwarf_always_disassemble, _("\
25698 Set whether `info address' always disassembles DWARF expressions."), _("\
25699 Show whether `info address' always disassembles DWARF expressions."), _("\
25700 When enabled, DWARF expressions are always printed in an assembly-like\n\
25701 syntax. When disabled, expressions will be printed in a more\n\
25702 conversational style, when possible."),
25703 NULL,
25704 show_dwarf_always_disassemble,
25705 &set_dwarf_cmdlist,
25706 &show_dwarf_cmdlist);
25707
25708 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25709 Set debugging of the DWARF reader."), _("\
25710 Show debugging of the DWARF reader."), _("\
25711 When enabled (non-zero), debugging messages are printed during DWARF\n\
25712 reading and symtab expansion. A value of 1 (one) provides basic\n\
25713 information. A value greater than 1 provides more verbose information."),
25714 NULL,
25715 NULL,
25716 &setdebuglist, &showdebuglist);
25717
25718 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25719 Set debugging of the DWARF DIE reader."), _("\
25720 Show debugging of the DWARF DIE reader."), _("\
25721 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25722 The value is the maximum depth to print."),
25723 NULL,
25724 NULL,
25725 &setdebuglist, &showdebuglist);
25726
25727 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25728 Set debugging of the dwarf line reader."), _("\
25729 Show debugging of the dwarf line reader."), _("\
25730 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25731 A value of 1 (one) provides basic information.\n\
25732 A value greater than 1 provides more verbose information."),
25733 NULL,
25734 NULL,
25735 &setdebuglist, &showdebuglist);
25736
25737 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25738 Set cross-checking of \"physname\" code against demangler."), _("\
25739 Show cross-checking of \"physname\" code against demangler."), _("\
25740 When enabled, GDB's internal \"physname\" code is checked against\n\
25741 the demangler."),
25742 NULL, show_check_physname,
25743 &setdebuglist, &showdebuglist);
25744
25745 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25746 no_class, &use_deprecated_index_sections, _("\
25747 Set whether to use deprecated gdb_index sections."), _("\
25748 Show whether to use deprecated gdb_index sections."), _("\
25749 When enabled, deprecated .gdb_index sections are used anyway.\n\
25750 Normally they are ignored either because of a missing feature or\n\
25751 performance issue.\n\
25752 Warning: This option must be enabled before gdb reads the file."),
25753 NULL,
25754 NULL,
25755 &setlist, &showlist);
25756
25757 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25758 &dwarf2_locexpr_funcs);
25759 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25760 &dwarf2_loclist_funcs);
25761
25762 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25763 &dwarf2_block_frame_base_locexpr_funcs);
25764 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25765 &dwarf2_block_frame_base_loclist_funcs);
25766
25767 #if GDB_SELF_TEST
25768 selftests::register_test ("dw2_expand_symtabs_matching",
25769 selftests::dw2_expand_symtabs_matching::run_test);
25770 #endif
25771 }
This page took 0.573392 seconds and 5 git commands to generate.