Make psymbols and psymtabs independent of the program space
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2018 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2read.h"
33 #include "dwarf-index-common.h"
34 #include "bfd.h"
35 #include "elf-bfd.h"
36 #include "symtab.h"
37 #include "gdbtypes.h"
38 #include "objfiles.h"
39 #include "dwarf2.h"
40 #include "buildsym.h"
41 #include "demangle.h"
42 #include "gdb-demangle.h"
43 #include "expression.h"
44 #include "filenames.h" /* for DOSish file names */
45 #include "macrotab.h"
46 #include "language.h"
47 #include "complaints.h"
48 #include "bcache.h"
49 #include "dwarf2expr.h"
50 #include "dwarf2loc.h"
51 #include "cp-support.h"
52 #include "hashtab.h"
53 #include "command.h"
54 #include "gdbcmd.h"
55 #include "block.h"
56 #include "addrmap.h"
57 #include "typeprint.h"
58 #include "psympriv.h"
59 #include <sys/stat.h>
60 #include "completer.h"
61 #include "vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71 #include "filestuff.h"
72 #include "build-id.h"
73 #include "namespace.h"
74 #include "common/gdb_unlinker.h"
75 #include "common/function-view.h"
76 #include "common/gdb_optional.h"
77 #include "common/underlying.h"
78 #include "common/byte-vector.h"
79 #include "common/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <sys/types.h>
84 #include <algorithm>
85 #include <unordered_set>
86 #include <unordered_map>
87 #include "selftest.h"
88 #include <cmath>
89 #include <set>
90 #include <forward_list>
91 #include "rust-lang.h"
92 #include "common/pathstuff.h"
93
94 /* When == 1, print basic high level tracing messages.
95 When > 1, be more verbose.
96 This is in contrast to the low level DIE reading of dwarf_die_debug. */
97 static unsigned int dwarf_read_debug = 0;
98
99 /* When non-zero, dump DIEs after they are read in. */
100 static unsigned int dwarf_die_debug = 0;
101
102 /* When non-zero, dump line number entries as they are read in. */
103 static unsigned int dwarf_line_debug = 0;
104
105 /* When non-zero, cross-check physname against demangler. */
106 static int check_physname = 0;
107
108 /* When non-zero, do not reject deprecated .gdb_index sections. */
109 static int use_deprecated_index_sections = 0;
110
111 static const struct objfile_data *dwarf2_objfile_data_key;
112
113 /* The "aclass" indices for various kinds of computed DWARF symbols. */
114
115 static int dwarf2_locexpr_index;
116 static int dwarf2_loclist_index;
117 static int dwarf2_locexpr_block_index;
118 static int dwarf2_loclist_block_index;
119
120 /* An index into a (C++) symbol name component in a symbol name as
121 recorded in the mapped_index's symbol table. For each C++ symbol
122 in the symbol table, we record one entry for the start of each
123 component in the symbol in a table of name components, and then
124 sort the table, in order to be able to binary search symbol names,
125 ignoring leading namespaces, both completion and regular look up.
126 For example, for symbol "A::B::C", we'll have an entry that points
127 to "A::B::C", another that points to "B::C", and another for "C".
128 Note that function symbols in GDB index have no parameter
129 information, just the function/method names. You can convert a
130 name_component to a "const char *" using the
131 'mapped_index::symbol_name_at(offset_type)' method. */
132
133 struct name_component
134 {
135 /* Offset in the symbol name where the component starts. Stored as
136 a (32-bit) offset instead of a pointer to save memory and improve
137 locality on 64-bit architectures. */
138 offset_type name_offset;
139
140 /* The symbol's index in the symbol and constant pool tables of a
141 mapped_index. */
142 offset_type idx;
143 };
144
145 /* Base class containing bits shared by both .gdb_index and
146 .debug_name indexes. */
147
148 struct mapped_index_base
149 {
150 mapped_index_base () = default;
151 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
152
153 /* The name_component table (a sorted vector). See name_component's
154 description above. */
155 std::vector<name_component> name_components;
156
157 /* How NAME_COMPONENTS is sorted. */
158 enum case_sensitivity name_components_casing;
159
160 /* Return the number of names in the symbol table. */
161 virtual size_t symbol_name_count () const = 0;
162
163 /* Get the name of the symbol at IDX in the symbol table. */
164 virtual const char *symbol_name_at (offset_type idx) const = 0;
165
166 /* Return whether the name at IDX in the symbol table should be
167 ignored. */
168 virtual bool symbol_name_slot_invalid (offset_type idx) const
169 {
170 return false;
171 }
172
173 /* Build the symbol name component sorted vector, if we haven't
174 yet. */
175 void build_name_components ();
176
177 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
178 possible matches for LN_NO_PARAMS in the name component
179 vector. */
180 std::pair<std::vector<name_component>::const_iterator,
181 std::vector<name_component>::const_iterator>
182 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
183
184 /* Prevent deleting/destroying via a base class pointer. */
185 protected:
186 ~mapped_index_base() = default;
187 };
188
189 /* A description of the mapped index. The file format is described in
190 a comment by the code that writes the index. */
191 struct mapped_index final : public mapped_index_base
192 {
193 /* A slot/bucket in the symbol table hash. */
194 struct symbol_table_slot
195 {
196 const offset_type name;
197 const offset_type vec;
198 };
199
200 /* Index data format version. */
201 int version = 0;
202
203 /* The address table data. */
204 gdb::array_view<const gdb_byte> address_table;
205
206 /* The symbol table, implemented as a hash table. */
207 gdb::array_view<symbol_table_slot> symbol_table;
208
209 /* A pointer to the constant pool. */
210 const char *constant_pool = nullptr;
211
212 bool symbol_name_slot_invalid (offset_type idx) const override
213 {
214 const auto &bucket = this->symbol_table[idx];
215 return bucket.name == 0 && bucket.vec;
216 }
217
218 /* Convenience method to get at the name of the symbol at IDX in the
219 symbol table. */
220 const char *symbol_name_at (offset_type idx) const override
221 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
222
223 size_t symbol_name_count () const override
224 { return this->symbol_table.size (); }
225 };
226
227 /* A description of the mapped .debug_names.
228 Uninitialized map has CU_COUNT 0. */
229 struct mapped_debug_names final : public mapped_index_base
230 {
231 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
232 : dwarf2_per_objfile (dwarf2_per_objfile_)
233 {}
234
235 struct dwarf2_per_objfile *dwarf2_per_objfile;
236 bfd_endian dwarf5_byte_order;
237 bool dwarf5_is_dwarf64;
238 bool augmentation_is_gdb;
239 uint8_t offset_size;
240 uint32_t cu_count = 0;
241 uint32_t tu_count, bucket_count, name_count;
242 const gdb_byte *cu_table_reordered, *tu_table_reordered;
243 const uint32_t *bucket_table_reordered, *hash_table_reordered;
244 const gdb_byte *name_table_string_offs_reordered;
245 const gdb_byte *name_table_entry_offs_reordered;
246 const gdb_byte *entry_pool;
247
248 struct index_val
249 {
250 ULONGEST dwarf_tag;
251 struct attr
252 {
253 /* Attribute name DW_IDX_*. */
254 ULONGEST dw_idx;
255
256 /* Attribute form DW_FORM_*. */
257 ULONGEST form;
258
259 /* Value if FORM is DW_FORM_implicit_const. */
260 LONGEST implicit_const;
261 };
262 std::vector<attr> attr_vec;
263 };
264
265 std::unordered_map<ULONGEST, index_val> abbrev_map;
266
267 const char *namei_to_name (uint32_t namei) const;
268
269 /* Implementation of the mapped_index_base virtual interface, for
270 the name_components cache. */
271
272 const char *symbol_name_at (offset_type idx) const override
273 { return namei_to_name (idx); }
274
275 size_t symbol_name_count () const override
276 { return this->name_count; }
277 };
278
279 /* See dwarf2read.h. */
280
281 dwarf2_per_objfile *
282 get_dwarf2_per_objfile (struct objfile *objfile)
283 {
284 return ((struct dwarf2_per_objfile *)
285 objfile_data (objfile, dwarf2_objfile_data_key));
286 }
287
288 /* Set the dwarf2_per_objfile associated to OBJFILE. */
289
290 void
291 set_dwarf2_per_objfile (struct objfile *objfile,
292 struct dwarf2_per_objfile *dwarf2_per_objfile)
293 {
294 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
295 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
296 }
297
298 /* Default names of the debugging sections. */
299
300 /* Note that if the debugging section has been compressed, it might
301 have a name like .zdebug_info. */
302
303 static const struct dwarf2_debug_sections dwarf2_elf_names =
304 {
305 { ".debug_info", ".zdebug_info" },
306 { ".debug_abbrev", ".zdebug_abbrev" },
307 { ".debug_line", ".zdebug_line" },
308 { ".debug_loc", ".zdebug_loc" },
309 { ".debug_loclists", ".zdebug_loclists" },
310 { ".debug_macinfo", ".zdebug_macinfo" },
311 { ".debug_macro", ".zdebug_macro" },
312 { ".debug_str", ".zdebug_str" },
313 { ".debug_line_str", ".zdebug_line_str" },
314 { ".debug_ranges", ".zdebug_ranges" },
315 { ".debug_rnglists", ".zdebug_rnglists" },
316 { ".debug_types", ".zdebug_types" },
317 { ".debug_addr", ".zdebug_addr" },
318 { ".debug_frame", ".zdebug_frame" },
319 { ".eh_frame", NULL },
320 { ".gdb_index", ".zgdb_index" },
321 { ".debug_names", ".zdebug_names" },
322 { ".debug_aranges", ".zdebug_aranges" },
323 23
324 };
325
326 /* List of DWO/DWP sections. */
327
328 static const struct dwop_section_names
329 {
330 struct dwarf2_section_names abbrev_dwo;
331 struct dwarf2_section_names info_dwo;
332 struct dwarf2_section_names line_dwo;
333 struct dwarf2_section_names loc_dwo;
334 struct dwarf2_section_names loclists_dwo;
335 struct dwarf2_section_names macinfo_dwo;
336 struct dwarf2_section_names macro_dwo;
337 struct dwarf2_section_names str_dwo;
338 struct dwarf2_section_names str_offsets_dwo;
339 struct dwarf2_section_names types_dwo;
340 struct dwarf2_section_names cu_index;
341 struct dwarf2_section_names tu_index;
342 }
343 dwop_section_names =
344 {
345 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
346 { ".debug_info.dwo", ".zdebug_info.dwo" },
347 { ".debug_line.dwo", ".zdebug_line.dwo" },
348 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
349 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
350 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
351 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
352 { ".debug_str.dwo", ".zdebug_str.dwo" },
353 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
354 { ".debug_types.dwo", ".zdebug_types.dwo" },
355 { ".debug_cu_index", ".zdebug_cu_index" },
356 { ".debug_tu_index", ".zdebug_tu_index" },
357 };
358
359 /* local data types */
360
361 /* The data in a compilation unit header, after target2host
362 translation, looks like this. */
363 struct comp_unit_head
364 {
365 unsigned int length;
366 short version;
367 unsigned char addr_size;
368 unsigned char signed_addr_p;
369 sect_offset abbrev_sect_off;
370
371 /* Size of file offsets; either 4 or 8. */
372 unsigned int offset_size;
373
374 /* Size of the length field; either 4 or 12. */
375 unsigned int initial_length_size;
376
377 enum dwarf_unit_type unit_type;
378
379 /* Offset to the first byte of this compilation unit header in the
380 .debug_info section, for resolving relative reference dies. */
381 sect_offset sect_off;
382
383 /* Offset to first die in this cu from the start of the cu.
384 This will be the first byte following the compilation unit header. */
385 cu_offset first_die_cu_offset;
386
387 /* 64-bit signature of this type unit - it is valid only for
388 UNIT_TYPE DW_UT_type. */
389 ULONGEST signature;
390
391 /* For types, offset in the type's DIE of the type defined by this TU. */
392 cu_offset type_cu_offset_in_tu;
393 };
394
395 /* Type used for delaying computation of method physnames.
396 See comments for compute_delayed_physnames. */
397 struct delayed_method_info
398 {
399 /* The type to which the method is attached, i.e., its parent class. */
400 struct type *type;
401
402 /* The index of the method in the type's function fieldlists. */
403 int fnfield_index;
404
405 /* The index of the method in the fieldlist. */
406 int index;
407
408 /* The name of the DIE. */
409 const char *name;
410
411 /* The DIE associated with this method. */
412 struct die_info *die;
413 };
414
415 /* Internal state when decoding a particular compilation unit. */
416 struct dwarf2_cu
417 {
418 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
419 ~dwarf2_cu ();
420
421 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
422
423 /* The header of the compilation unit. */
424 struct comp_unit_head header {};
425
426 /* Base address of this compilation unit. */
427 CORE_ADDR base_address = 0;
428
429 /* Non-zero if base_address has been set. */
430 int base_known = 0;
431
432 /* The language we are debugging. */
433 enum language language = language_unknown;
434 const struct language_defn *language_defn = nullptr;
435
436 const char *producer = nullptr;
437
438 /* The symtab builder for this CU. This is only non-NULL when full
439 symbols are being read. */
440 std::unique_ptr<buildsym_compunit> builder;
441
442 /* The generic symbol table building routines have separate lists for
443 file scope symbols and all all other scopes (local scopes). So
444 we need to select the right one to pass to add_symbol_to_list().
445 We do it by keeping a pointer to the correct list in list_in_scope.
446
447 FIXME: The original dwarf code just treated the file scope as the
448 first local scope, and all other local scopes as nested local
449 scopes, and worked fine. Check to see if we really need to
450 distinguish these in buildsym.c. */
451 struct pending **list_in_scope = nullptr;
452
453 /* Hash table holding all the loaded partial DIEs
454 with partial_die->offset.SECT_OFF as hash. */
455 htab_t partial_dies = nullptr;
456
457 /* Storage for things with the same lifetime as this read-in compilation
458 unit, including partial DIEs. */
459 auto_obstack comp_unit_obstack;
460
461 /* When multiple dwarf2_cu structures are living in memory, this field
462 chains them all together, so that they can be released efficiently.
463 We will probably also want a generation counter so that most-recently-used
464 compilation units are cached... */
465 struct dwarf2_per_cu_data *read_in_chain = nullptr;
466
467 /* Backlink to our per_cu entry. */
468 struct dwarf2_per_cu_data *per_cu;
469
470 /* How many compilation units ago was this CU last referenced? */
471 int last_used = 0;
472
473 /* A hash table of DIE cu_offset for following references with
474 die_info->offset.sect_off as hash. */
475 htab_t die_hash = nullptr;
476
477 /* Full DIEs if read in. */
478 struct die_info *dies = nullptr;
479
480 /* A set of pointers to dwarf2_per_cu_data objects for compilation
481 units referenced by this one. Only set during full symbol processing;
482 partial symbol tables do not have dependencies. */
483 htab_t dependencies = nullptr;
484
485 /* Header data from the line table, during full symbol processing. */
486 struct line_header *line_header = nullptr;
487 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
488 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
489 this is the DW_TAG_compile_unit die for this CU. We'll hold on
490 to the line header as long as this DIE is being processed. See
491 process_die_scope. */
492 die_info *line_header_die_owner = nullptr;
493
494 /* A list of methods which need to have physnames computed
495 after all type information has been read. */
496 std::vector<delayed_method_info> method_list;
497
498 /* To be copied to symtab->call_site_htab. */
499 htab_t call_site_htab = nullptr;
500
501 /* Non-NULL if this CU came from a DWO file.
502 There is an invariant here that is important to remember:
503 Except for attributes copied from the top level DIE in the "main"
504 (or "stub") file in preparation for reading the DWO file
505 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
506 Either there isn't a DWO file (in which case this is NULL and the point
507 is moot), or there is and either we're not going to read it (in which
508 case this is NULL) or there is and we are reading it (in which case this
509 is non-NULL). */
510 struct dwo_unit *dwo_unit = nullptr;
511
512 /* The DW_AT_addr_base attribute if present, zero otherwise
513 (zero is a valid value though).
514 Note this value comes from the Fission stub CU/TU's DIE. */
515 ULONGEST addr_base = 0;
516
517 /* The DW_AT_ranges_base attribute if present, zero otherwise
518 (zero is a valid value though).
519 Note this value comes from the Fission stub CU/TU's DIE.
520 Also note that the value is zero in the non-DWO case so this value can
521 be used without needing to know whether DWO files are in use or not.
522 N.B. This does not apply to DW_AT_ranges appearing in
523 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
524 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
525 DW_AT_ranges_base *would* have to be applied, and we'd have to care
526 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
527 ULONGEST ranges_base = 0;
528
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
537 /* Mark used when releasing cached dies. */
538 unsigned int mark : 1;
539
540 /* This CU references .debug_loc. See the symtab->locations_valid field.
541 This test is imperfect as there may exist optimized debug code not using
542 any location list and still facing inlining issues if handled as
543 unoptimized code. For a future better test see GCC PR other/32998. */
544 unsigned int has_loclist : 1;
545
546 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
547 if all the producer_is_* fields are valid. This information is cached
548 because profiling CU expansion showed excessive time spent in
549 producer_is_gxx_lt_4_6. */
550 unsigned int checked_producer : 1;
551 unsigned int producer_is_gxx_lt_4_6 : 1;
552 unsigned int producer_is_gcc_lt_4_3 : 1;
553 unsigned int producer_is_icc_lt_14 : 1;
554
555 /* When set, the file that we're processing is known to have
556 debugging info for C++ namespaces. GCC 3.3.x did not produce
557 this information, but later versions do. */
558
559 unsigned int processing_has_namespace_info : 1;
560
561 struct partial_die_info *find_partial_die (sect_offset sect_off);
562 };
563
564 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
565 This includes type_unit_group and quick_file_names. */
566
567 struct stmt_list_hash
568 {
569 /* The DWO unit this table is from or NULL if there is none. */
570 struct dwo_unit *dwo_unit;
571
572 /* Offset in .debug_line or .debug_line.dwo. */
573 sect_offset line_sect_off;
574 };
575
576 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
577 an object of this type. */
578
579 struct type_unit_group
580 {
581 /* dwarf2read.c's main "handle" on a TU symtab.
582 To simplify things we create an artificial CU that "includes" all the
583 type units using this stmt_list so that the rest of the code still has
584 a "per_cu" handle on the symtab.
585 This PER_CU is recognized by having no section. */
586 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
587 struct dwarf2_per_cu_data per_cu;
588
589 /* The TUs that share this DW_AT_stmt_list entry.
590 This is added to while parsing type units to build partial symtabs,
591 and is deleted afterwards and not used again. */
592 VEC (sig_type_ptr) *tus;
593
594 /* The compunit symtab.
595 Type units in a group needn't all be defined in the same source file,
596 so we create an essentially anonymous symtab as the compunit symtab. */
597 struct compunit_symtab *compunit_symtab;
598
599 /* The data used to construct the hash key. */
600 struct stmt_list_hash hash;
601
602 /* The number of symtabs from the line header.
603 The value here must match line_header.num_file_names. */
604 unsigned int num_symtabs;
605
606 /* The symbol tables for this TU (obtained from the files listed in
607 DW_AT_stmt_list).
608 WARNING: The order of entries here must match the order of entries
609 in the line header. After the first TU using this type_unit_group, the
610 line header for the subsequent TUs is recreated from this. This is done
611 because we need to use the same symtabs for each TU using the same
612 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
613 there's no guarantee the line header doesn't have duplicate entries. */
614 struct symtab **symtabs;
615 };
616
617 /* These sections are what may appear in a (real or virtual) DWO file. */
618
619 struct dwo_sections
620 {
621 struct dwarf2_section_info abbrev;
622 struct dwarf2_section_info line;
623 struct dwarf2_section_info loc;
624 struct dwarf2_section_info loclists;
625 struct dwarf2_section_info macinfo;
626 struct dwarf2_section_info macro;
627 struct dwarf2_section_info str;
628 struct dwarf2_section_info str_offsets;
629 /* In the case of a virtual DWO file, these two are unused. */
630 struct dwarf2_section_info info;
631 VEC (dwarf2_section_info_def) *types;
632 };
633
634 /* CUs/TUs in DWP/DWO files. */
635
636 struct dwo_unit
637 {
638 /* Backlink to the containing struct dwo_file. */
639 struct dwo_file *dwo_file;
640
641 /* The "id" that distinguishes this CU/TU.
642 .debug_info calls this "dwo_id", .debug_types calls this "signature".
643 Since signatures came first, we stick with it for consistency. */
644 ULONGEST signature;
645
646 /* The section this CU/TU lives in, in the DWO file. */
647 struct dwarf2_section_info *section;
648
649 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
650 sect_offset sect_off;
651 unsigned int length;
652
653 /* For types, offset in the type's DIE of the type defined by this TU. */
654 cu_offset type_offset_in_tu;
655 };
656
657 /* include/dwarf2.h defines the DWP section codes.
658 It defines a max value but it doesn't define a min value, which we
659 use for error checking, so provide one. */
660
661 enum dwp_v2_section_ids
662 {
663 DW_SECT_MIN = 1
664 };
665
666 /* Data for one DWO file.
667
668 This includes virtual DWO files (a virtual DWO file is a DWO file as it
669 appears in a DWP file). DWP files don't really have DWO files per se -
670 comdat folding of types "loses" the DWO file they came from, and from
671 a high level view DWP files appear to contain a mass of random types.
672 However, to maintain consistency with the non-DWP case we pretend DWP
673 files contain virtual DWO files, and we assign each TU with one virtual
674 DWO file (generally based on the line and abbrev section offsets -
675 a heuristic that seems to work in practice). */
676
677 struct dwo_file
678 {
679 /* The DW_AT_GNU_dwo_name attribute.
680 For virtual DWO files the name is constructed from the section offsets
681 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
682 from related CU+TUs. */
683 const char *dwo_name;
684
685 /* The DW_AT_comp_dir attribute. */
686 const char *comp_dir;
687
688 /* The bfd, when the file is open. Otherwise this is NULL.
689 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
690 bfd *dbfd;
691
692 /* The sections that make up this DWO file.
693 Remember that for virtual DWO files in DWP V2, these are virtual
694 sections (for lack of a better name). */
695 struct dwo_sections sections;
696
697 /* The CUs in the file.
698 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
699 an extension to handle LLVM's Link Time Optimization output (where
700 multiple source files may be compiled into a single object/dwo pair). */
701 htab_t cus;
702
703 /* Table of TUs in the file.
704 Each element is a struct dwo_unit. */
705 htab_t tus;
706 };
707
708 /* These sections are what may appear in a DWP file. */
709
710 struct dwp_sections
711 {
712 /* These are used by both DWP version 1 and 2. */
713 struct dwarf2_section_info str;
714 struct dwarf2_section_info cu_index;
715 struct dwarf2_section_info tu_index;
716
717 /* These are only used by DWP version 2 files.
718 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
719 sections are referenced by section number, and are not recorded here.
720 In DWP version 2 there is at most one copy of all these sections, each
721 section being (effectively) comprised of the concatenation of all of the
722 individual sections that exist in the version 1 format.
723 To keep the code simple we treat each of these concatenated pieces as a
724 section itself (a virtual section?). */
725 struct dwarf2_section_info abbrev;
726 struct dwarf2_section_info info;
727 struct dwarf2_section_info line;
728 struct dwarf2_section_info loc;
729 struct dwarf2_section_info macinfo;
730 struct dwarf2_section_info macro;
731 struct dwarf2_section_info str_offsets;
732 struct dwarf2_section_info types;
733 };
734
735 /* These sections are what may appear in a virtual DWO file in DWP version 1.
736 A virtual DWO file is a DWO file as it appears in a DWP file. */
737
738 struct virtual_v1_dwo_sections
739 {
740 struct dwarf2_section_info abbrev;
741 struct dwarf2_section_info line;
742 struct dwarf2_section_info loc;
743 struct dwarf2_section_info macinfo;
744 struct dwarf2_section_info macro;
745 struct dwarf2_section_info str_offsets;
746 /* Each DWP hash table entry records one CU or one TU.
747 That is recorded here, and copied to dwo_unit.section. */
748 struct dwarf2_section_info info_or_types;
749 };
750
751 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
752 In version 2, the sections of the DWO files are concatenated together
753 and stored in one section of that name. Thus each ELF section contains
754 several "virtual" sections. */
755
756 struct virtual_v2_dwo_sections
757 {
758 bfd_size_type abbrev_offset;
759 bfd_size_type abbrev_size;
760
761 bfd_size_type line_offset;
762 bfd_size_type line_size;
763
764 bfd_size_type loc_offset;
765 bfd_size_type loc_size;
766
767 bfd_size_type macinfo_offset;
768 bfd_size_type macinfo_size;
769
770 bfd_size_type macro_offset;
771 bfd_size_type macro_size;
772
773 bfd_size_type str_offsets_offset;
774 bfd_size_type str_offsets_size;
775
776 /* Each DWP hash table entry records one CU or one TU.
777 That is recorded here, and copied to dwo_unit.section. */
778 bfd_size_type info_or_types_offset;
779 bfd_size_type info_or_types_size;
780 };
781
782 /* Contents of DWP hash tables. */
783
784 struct dwp_hash_table
785 {
786 uint32_t version, nr_columns;
787 uint32_t nr_units, nr_slots;
788 const gdb_byte *hash_table, *unit_table;
789 union
790 {
791 struct
792 {
793 const gdb_byte *indices;
794 } v1;
795 struct
796 {
797 /* This is indexed by column number and gives the id of the section
798 in that column. */
799 #define MAX_NR_V2_DWO_SECTIONS \
800 (1 /* .debug_info or .debug_types */ \
801 + 1 /* .debug_abbrev */ \
802 + 1 /* .debug_line */ \
803 + 1 /* .debug_loc */ \
804 + 1 /* .debug_str_offsets */ \
805 + 1 /* .debug_macro or .debug_macinfo */)
806 int section_ids[MAX_NR_V2_DWO_SECTIONS];
807 const gdb_byte *offsets;
808 const gdb_byte *sizes;
809 } v2;
810 } section_pool;
811 };
812
813 /* Data for one DWP file. */
814
815 struct dwp_file
816 {
817 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
818 : name (name_),
819 dbfd (std::move (abfd))
820 {
821 }
822
823 /* Name of the file. */
824 const char *name;
825
826 /* File format version. */
827 int version = 0;
828
829 /* The bfd. */
830 gdb_bfd_ref_ptr dbfd;
831
832 /* Section info for this file. */
833 struct dwp_sections sections {};
834
835 /* Table of CUs in the file. */
836 const struct dwp_hash_table *cus = nullptr;
837
838 /* Table of TUs in the file. */
839 const struct dwp_hash_table *tus = nullptr;
840
841 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
842 htab_t loaded_cus {};
843 htab_t loaded_tus {};
844
845 /* Table to map ELF section numbers to their sections.
846 This is only needed for the DWP V1 file format. */
847 unsigned int num_sections = 0;
848 asection **elf_sections = nullptr;
849 };
850
851 /* This represents a '.dwz' file. */
852
853 struct dwz_file
854 {
855 dwz_file (gdb_bfd_ref_ptr &&bfd)
856 : dwz_bfd (std::move (bfd))
857 {
858 }
859
860 /* A dwz file can only contain a few sections. */
861 struct dwarf2_section_info abbrev {};
862 struct dwarf2_section_info info {};
863 struct dwarf2_section_info str {};
864 struct dwarf2_section_info line {};
865 struct dwarf2_section_info macro {};
866 struct dwarf2_section_info gdb_index {};
867 struct dwarf2_section_info debug_names {};
868
869 /* The dwz's BFD. */
870 gdb_bfd_ref_ptr dwz_bfd;
871 };
872
873 /* Struct used to pass misc. parameters to read_die_and_children, et
874 al. which are used for both .debug_info and .debug_types dies.
875 All parameters here are unchanging for the life of the call. This
876 struct exists to abstract away the constant parameters of die reading. */
877
878 struct die_reader_specs
879 {
880 /* The bfd of die_section. */
881 bfd* abfd;
882
883 /* The CU of the DIE we are parsing. */
884 struct dwarf2_cu *cu;
885
886 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
887 struct dwo_file *dwo_file;
888
889 /* The section the die comes from.
890 This is either .debug_info or .debug_types, or the .dwo variants. */
891 struct dwarf2_section_info *die_section;
892
893 /* die_section->buffer. */
894 const gdb_byte *buffer;
895
896 /* The end of the buffer. */
897 const gdb_byte *buffer_end;
898
899 /* The value of the DW_AT_comp_dir attribute. */
900 const char *comp_dir;
901
902 /* The abbreviation table to use when reading the DIEs. */
903 struct abbrev_table *abbrev_table;
904 };
905
906 /* Type of function passed to init_cutu_and_read_dies, et.al. */
907 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
908 const gdb_byte *info_ptr,
909 struct die_info *comp_unit_die,
910 int has_children,
911 void *data);
912
913 /* A 1-based directory index. This is a strong typedef to prevent
914 accidentally using a directory index as a 0-based index into an
915 array/vector. */
916 enum class dir_index : unsigned int {};
917
918 /* Likewise, a 1-based file name index. */
919 enum class file_name_index : unsigned int {};
920
921 struct file_entry
922 {
923 file_entry () = default;
924
925 file_entry (const char *name_, dir_index d_index_,
926 unsigned int mod_time_, unsigned int length_)
927 : name (name_),
928 d_index (d_index_),
929 mod_time (mod_time_),
930 length (length_)
931 {}
932
933 /* Return the include directory at D_INDEX stored in LH. Returns
934 NULL if D_INDEX is out of bounds. */
935 const char *include_dir (const line_header *lh) const;
936
937 /* The file name. Note this is an observing pointer. The memory is
938 owned by debug_line_buffer. */
939 const char *name {};
940
941 /* The directory index (1-based). */
942 dir_index d_index {};
943
944 unsigned int mod_time {};
945
946 unsigned int length {};
947
948 /* True if referenced by the Line Number Program. */
949 bool included_p {};
950
951 /* The associated symbol table, if any. */
952 struct symtab *symtab {};
953 };
954
955 /* The line number information for a compilation unit (found in the
956 .debug_line section) begins with a "statement program header",
957 which contains the following information. */
958 struct line_header
959 {
960 line_header ()
961 : offset_in_dwz {}
962 {}
963
964 /* Add an entry to the include directory table. */
965 void add_include_dir (const char *include_dir);
966
967 /* Add an entry to the file name table. */
968 void add_file_name (const char *name, dir_index d_index,
969 unsigned int mod_time, unsigned int length);
970
971 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
972 is out of bounds. */
973 const char *include_dir_at (dir_index index) const
974 {
975 /* Convert directory index number (1-based) to vector index
976 (0-based). */
977 size_t vec_index = to_underlying (index) - 1;
978
979 if (vec_index >= include_dirs.size ())
980 return NULL;
981 return include_dirs[vec_index];
982 }
983
984 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
985 is out of bounds. */
986 file_entry *file_name_at (file_name_index index)
987 {
988 /* Convert file name index number (1-based) to vector index
989 (0-based). */
990 size_t vec_index = to_underlying (index) - 1;
991
992 if (vec_index >= file_names.size ())
993 return NULL;
994 return &file_names[vec_index];
995 }
996
997 /* Const version of the above. */
998 const file_entry *file_name_at (unsigned int index) const
999 {
1000 if (index >= file_names.size ())
1001 return NULL;
1002 return &file_names[index];
1003 }
1004
1005 /* Offset of line number information in .debug_line section. */
1006 sect_offset sect_off {};
1007
1008 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1009 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1010
1011 unsigned int total_length {};
1012 unsigned short version {};
1013 unsigned int header_length {};
1014 unsigned char minimum_instruction_length {};
1015 unsigned char maximum_ops_per_instruction {};
1016 unsigned char default_is_stmt {};
1017 int line_base {};
1018 unsigned char line_range {};
1019 unsigned char opcode_base {};
1020
1021 /* standard_opcode_lengths[i] is the number of operands for the
1022 standard opcode whose value is i. This means that
1023 standard_opcode_lengths[0] is unused, and the last meaningful
1024 element is standard_opcode_lengths[opcode_base - 1]. */
1025 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1026
1027 /* The include_directories table. Note these are observing
1028 pointers. The memory is owned by debug_line_buffer. */
1029 std::vector<const char *> include_dirs;
1030
1031 /* The file_names table. */
1032 std::vector<file_entry> file_names;
1033
1034 /* The start and end of the statement program following this
1035 header. These point into dwarf2_per_objfile->line_buffer. */
1036 const gdb_byte *statement_program_start {}, *statement_program_end {};
1037 };
1038
1039 typedef std::unique_ptr<line_header> line_header_up;
1040
1041 const char *
1042 file_entry::include_dir (const line_header *lh) const
1043 {
1044 return lh->include_dir_at (d_index);
1045 }
1046
1047 /* When we construct a partial symbol table entry we only
1048 need this much information. */
1049 struct partial_die_info : public allocate_on_obstack
1050 {
1051 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1052
1053 /* Disable assign but still keep copy ctor, which is needed
1054 load_partial_dies. */
1055 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1056
1057 /* Adjust the partial die before generating a symbol for it. This
1058 function may set the is_external flag or change the DIE's
1059 name. */
1060 void fixup (struct dwarf2_cu *cu);
1061
1062 /* Read a minimal amount of information into the minimal die
1063 structure. */
1064 const gdb_byte *read (const struct die_reader_specs *reader,
1065 const struct abbrev_info &abbrev,
1066 const gdb_byte *info_ptr);
1067
1068 /* Offset of this DIE. */
1069 const sect_offset sect_off;
1070
1071 /* DWARF-2 tag for this DIE. */
1072 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1073
1074 /* Assorted flags describing the data found in this DIE. */
1075 const unsigned int has_children : 1;
1076
1077 unsigned int is_external : 1;
1078 unsigned int is_declaration : 1;
1079 unsigned int has_type : 1;
1080 unsigned int has_specification : 1;
1081 unsigned int has_pc_info : 1;
1082 unsigned int may_be_inlined : 1;
1083
1084 /* This DIE has been marked DW_AT_main_subprogram. */
1085 unsigned int main_subprogram : 1;
1086
1087 /* Flag set if the SCOPE field of this structure has been
1088 computed. */
1089 unsigned int scope_set : 1;
1090
1091 /* Flag set if the DIE has a byte_size attribute. */
1092 unsigned int has_byte_size : 1;
1093
1094 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1095 unsigned int has_const_value : 1;
1096
1097 /* Flag set if any of the DIE's children are template arguments. */
1098 unsigned int has_template_arguments : 1;
1099
1100 /* Flag set if fixup has been called on this die. */
1101 unsigned int fixup_called : 1;
1102
1103 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1104 unsigned int is_dwz : 1;
1105
1106 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1107 unsigned int spec_is_dwz : 1;
1108
1109 /* The name of this DIE. Normally the value of DW_AT_name, but
1110 sometimes a default name for unnamed DIEs. */
1111 const char *name = nullptr;
1112
1113 /* The linkage name, if present. */
1114 const char *linkage_name = nullptr;
1115
1116 /* The scope to prepend to our children. This is generally
1117 allocated on the comp_unit_obstack, so will disappear
1118 when this compilation unit leaves the cache. */
1119 const char *scope = nullptr;
1120
1121 /* Some data associated with the partial DIE. The tag determines
1122 which field is live. */
1123 union
1124 {
1125 /* The location description associated with this DIE, if any. */
1126 struct dwarf_block *locdesc;
1127 /* The offset of an import, for DW_TAG_imported_unit. */
1128 sect_offset sect_off;
1129 } d {};
1130
1131 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1132 CORE_ADDR lowpc = 0;
1133 CORE_ADDR highpc = 0;
1134
1135 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1136 DW_AT_sibling, if any. */
1137 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1138 could return DW_AT_sibling values to its caller load_partial_dies. */
1139 const gdb_byte *sibling = nullptr;
1140
1141 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1142 DW_AT_specification (or DW_AT_abstract_origin or
1143 DW_AT_extension). */
1144 sect_offset spec_offset {};
1145
1146 /* Pointers to this DIE's parent, first child, and next sibling,
1147 if any. */
1148 struct partial_die_info *die_parent = nullptr;
1149 struct partial_die_info *die_child = nullptr;
1150 struct partial_die_info *die_sibling = nullptr;
1151
1152 friend struct partial_die_info *
1153 dwarf2_cu::find_partial_die (sect_offset sect_off);
1154
1155 private:
1156 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1157 partial_die_info (sect_offset sect_off)
1158 : partial_die_info (sect_off, DW_TAG_padding, 0)
1159 {
1160 }
1161
1162 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1163 int has_children_)
1164 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1165 {
1166 is_external = 0;
1167 is_declaration = 0;
1168 has_type = 0;
1169 has_specification = 0;
1170 has_pc_info = 0;
1171 may_be_inlined = 0;
1172 main_subprogram = 0;
1173 scope_set = 0;
1174 has_byte_size = 0;
1175 has_const_value = 0;
1176 has_template_arguments = 0;
1177 fixup_called = 0;
1178 is_dwz = 0;
1179 spec_is_dwz = 0;
1180 }
1181 };
1182
1183 /* This data structure holds the information of an abbrev. */
1184 struct abbrev_info
1185 {
1186 unsigned int number; /* number identifying abbrev */
1187 enum dwarf_tag tag; /* dwarf tag */
1188 unsigned short has_children; /* boolean */
1189 unsigned short num_attrs; /* number of attributes */
1190 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1191 struct abbrev_info *next; /* next in chain */
1192 };
1193
1194 struct attr_abbrev
1195 {
1196 ENUM_BITFIELD(dwarf_attribute) name : 16;
1197 ENUM_BITFIELD(dwarf_form) form : 16;
1198
1199 /* It is valid only if FORM is DW_FORM_implicit_const. */
1200 LONGEST implicit_const;
1201 };
1202
1203 /* Size of abbrev_table.abbrev_hash_table. */
1204 #define ABBREV_HASH_SIZE 121
1205
1206 /* Top level data structure to contain an abbreviation table. */
1207
1208 struct abbrev_table
1209 {
1210 explicit abbrev_table (sect_offset off)
1211 : sect_off (off)
1212 {
1213 m_abbrevs =
1214 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1215 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1216 }
1217
1218 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1219
1220 /* Allocate space for a struct abbrev_info object in
1221 ABBREV_TABLE. */
1222 struct abbrev_info *alloc_abbrev ();
1223
1224 /* Add an abbreviation to the table. */
1225 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1226
1227 /* Look up an abbrev in the table.
1228 Returns NULL if the abbrev is not found. */
1229
1230 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1231
1232
1233 /* Where the abbrev table came from.
1234 This is used as a sanity check when the table is used. */
1235 const sect_offset sect_off;
1236
1237 /* Storage for the abbrev table. */
1238 auto_obstack abbrev_obstack;
1239
1240 private:
1241
1242 /* Hash table of abbrevs.
1243 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1244 It could be statically allocated, but the previous code didn't so we
1245 don't either. */
1246 struct abbrev_info **m_abbrevs;
1247 };
1248
1249 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1250
1251 /* Attributes have a name and a value. */
1252 struct attribute
1253 {
1254 ENUM_BITFIELD(dwarf_attribute) name : 16;
1255 ENUM_BITFIELD(dwarf_form) form : 15;
1256
1257 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1258 field should be in u.str (existing only for DW_STRING) but it is kept
1259 here for better struct attribute alignment. */
1260 unsigned int string_is_canonical : 1;
1261
1262 union
1263 {
1264 const char *str;
1265 struct dwarf_block *blk;
1266 ULONGEST unsnd;
1267 LONGEST snd;
1268 CORE_ADDR addr;
1269 ULONGEST signature;
1270 }
1271 u;
1272 };
1273
1274 /* This data structure holds a complete die structure. */
1275 struct die_info
1276 {
1277 /* DWARF-2 tag for this DIE. */
1278 ENUM_BITFIELD(dwarf_tag) tag : 16;
1279
1280 /* Number of attributes */
1281 unsigned char num_attrs;
1282
1283 /* True if we're presently building the full type name for the
1284 type derived from this DIE. */
1285 unsigned char building_fullname : 1;
1286
1287 /* True if this die is in process. PR 16581. */
1288 unsigned char in_process : 1;
1289
1290 /* Abbrev number */
1291 unsigned int abbrev;
1292
1293 /* Offset in .debug_info or .debug_types section. */
1294 sect_offset sect_off;
1295
1296 /* The dies in a compilation unit form an n-ary tree. PARENT
1297 points to this die's parent; CHILD points to the first child of
1298 this node; and all the children of a given node are chained
1299 together via their SIBLING fields. */
1300 struct die_info *child; /* Its first child, if any. */
1301 struct die_info *sibling; /* Its next sibling, if any. */
1302 struct die_info *parent; /* Its parent, if any. */
1303
1304 /* An array of attributes, with NUM_ATTRS elements. There may be
1305 zero, but it's not common and zero-sized arrays are not
1306 sufficiently portable C. */
1307 struct attribute attrs[1];
1308 };
1309
1310 /* Get at parts of an attribute structure. */
1311
1312 #define DW_STRING(attr) ((attr)->u.str)
1313 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1314 #define DW_UNSND(attr) ((attr)->u.unsnd)
1315 #define DW_BLOCK(attr) ((attr)->u.blk)
1316 #define DW_SND(attr) ((attr)->u.snd)
1317 #define DW_ADDR(attr) ((attr)->u.addr)
1318 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1319
1320 /* Blocks are a bunch of untyped bytes. */
1321 struct dwarf_block
1322 {
1323 size_t size;
1324
1325 /* Valid only if SIZE is not zero. */
1326 const gdb_byte *data;
1327 };
1328
1329 #ifndef ATTR_ALLOC_CHUNK
1330 #define ATTR_ALLOC_CHUNK 4
1331 #endif
1332
1333 /* Allocate fields for structs, unions and enums in this size. */
1334 #ifndef DW_FIELD_ALLOC_CHUNK
1335 #define DW_FIELD_ALLOC_CHUNK 4
1336 #endif
1337
1338 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1339 but this would require a corresponding change in unpack_field_as_long
1340 and friends. */
1341 static int bits_per_byte = 8;
1342
1343 /* When reading a variant or variant part, we track a bit more
1344 information about the field, and store it in an object of this
1345 type. */
1346
1347 struct variant_field
1348 {
1349 /* If we see a DW_TAG_variant, then this will be the discriminant
1350 value. */
1351 ULONGEST discriminant_value;
1352 /* If we see a DW_TAG_variant, then this will be set if this is the
1353 default branch. */
1354 bool default_branch;
1355 /* While reading a DW_TAG_variant_part, this will be set if this
1356 field is the discriminant. */
1357 bool is_discriminant;
1358 };
1359
1360 struct nextfield
1361 {
1362 int accessibility = 0;
1363 int virtuality = 0;
1364 /* Extra information to describe a variant or variant part. */
1365 struct variant_field variant {};
1366 struct field field {};
1367 };
1368
1369 struct fnfieldlist
1370 {
1371 const char *name = nullptr;
1372 std::vector<struct fn_field> fnfields;
1373 };
1374
1375 /* The routines that read and process dies for a C struct or C++ class
1376 pass lists of data member fields and lists of member function fields
1377 in an instance of a field_info structure, as defined below. */
1378 struct field_info
1379 {
1380 /* List of data member and baseclasses fields. */
1381 std::vector<struct nextfield> fields;
1382 std::vector<struct nextfield> baseclasses;
1383
1384 /* Number of fields (including baseclasses). */
1385 int nfields = 0;
1386
1387 /* Set if the accesibility of one of the fields is not public. */
1388 int non_public_fields = 0;
1389
1390 /* Member function fieldlist array, contains name of possibly overloaded
1391 member function, number of overloaded member functions and a pointer
1392 to the head of the member function field chain. */
1393 std::vector<struct fnfieldlist> fnfieldlists;
1394
1395 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1396 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1397 std::vector<struct decl_field> typedef_field_list;
1398
1399 /* Nested types defined by this class and the number of elements in this
1400 list. */
1401 std::vector<struct decl_field> nested_types_list;
1402 };
1403
1404 /* One item on the queue of compilation units to read in full symbols
1405 for. */
1406 struct dwarf2_queue_item
1407 {
1408 struct dwarf2_per_cu_data *per_cu;
1409 enum language pretend_language;
1410 struct dwarf2_queue_item *next;
1411 };
1412
1413 /* The current queue. */
1414 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1415
1416 /* Loaded secondary compilation units are kept in memory until they
1417 have not been referenced for the processing of this many
1418 compilation units. Set this to zero to disable caching. Cache
1419 sizes of up to at least twenty will improve startup time for
1420 typical inter-CU-reference binaries, at an obvious memory cost. */
1421 static int dwarf_max_cache_age = 5;
1422 static void
1423 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1424 struct cmd_list_element *c, const char *value)
1425 {
1426 fprintf_filtered (file, _("The upper bound on the age of cached "
1427 "DWARF compilation units is %s.\n"),
1428 value);
1429 }
1430 \f
1431 /* local function prototypes */
1432
1433 static const char *get_section_name (const struct dwarf2_section_info *);
1434
1435 static const char *get_section_file_name (const struct dwarf2_section_info *);
1436
1437 static void dwarf2_find_base_address (struct die_info *die,
1438 struct dwarf2_cu *cu);
1439
1440 static struct partial_symtab *create_partial_symtab
1441 (struct dwarf2_per_cu_data *per_cu, const char *name);
1442
1443 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1444 const gdb_byte *info_ptr,
1445 struct die_info *type_unit_die,
1446 int has_children, void *data);
1447
1448 static void dwarf2_build_psymtabs_hard
1449 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1450
1451 static void scan_partial_symbols (struct partial_die_info *,
1452 CORE_ADDR *, CORE_ADDR *,
1453 int, struct dwarf2_cu *);
1454
1455 static void add_partial_symbol (struct partial_die_info *,
1456 struct dwarf2_cu *);
1457
1458 static void add_partial_namespace (struct partial_die_info *pdi,
1459 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1460 int set_addrmap, struct dwarf2_cu *cu);
1461
1462 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1463 CORE_ADDR *highpc, int set_addrmap,
1464 struct dwarf2_cu *cu);
1465
1466 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1467 struct dwarf2_cu *cu);
1468
1469 static void add_partial_subprogram (struct partial_die_info *pdi,
1470 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1471 int need_pc, struct dwarf2_cu *cu);
1472
1473 static void dwarf2_read_symtab (struct partial_symtab *,
1474 struct objfile *);
1475
1476 static void psymtab_to_symtab_1 (struct partial_symtab *);
1477
1478 static abbrev_table_up abbrev_table_read_table
1479 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1480 sect_offset);
1481
1482 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1483
1484 static struct partial_die_info *load_partial_dies
1485 (const struct die_reader_specs *, const gdb_byte *, int);
1486
1487 static struct partial_die_info *find_partial_die (sect_offset, int,
1488 struct dwarf2_cu *);
1489
1490 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1491 struct attribute *, struct attr_abbrev *,
1492 const gdb_byte *);
1493
1494 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1495
1496 static int read_1_signed_byte (bfd *, const gdb_byte *);
1497
1498 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1499
1500 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1501
1502 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1503
1504 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1505 unsigned int *);
1506
1507 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1508
1509 static LONGEST read_checked_initial_length_and_offset
1510 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1511 unsigned int *, unsigned int *);
1512
1513 static LONGEST read_offset (bfd *, const gdb_byte *,
1514 const struct comp_unit_head *,
1515 unsigned int *);
1516
1517 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1518
1519 static sect_offset read_abbrev_offset
1520 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1521 struct dwarf2_section_info *, sect_offset);
1522
1523 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1524
1525 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1526
1527 static const char *read_indirect_string
1528 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1529 const struct comp_unit_head *, unsigned int *);
1530
1531 static const char *read_indirect_line_string
1532 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1533 const struct comp_unit_head *, unsigned int *);
1534
1535 static const char *read_indirect_string_at_offset
1536 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1537 LONGEST str_offset);
1538
1539 static const char *read_indirect_string_from_dwz
1540 (struct objfile *objfile, struct dwz_file *, LONGEST);
1541
1542 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1543
1544 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1545 const gdb_byte *,
1546 unsigned int *);
1547
1548 static const char *read_str_index (const struct die_reader_specs *reader,
1549 ULONGEST str_index);
1550
1551 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1552
1553 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1554 struct dwarf2_cu *);
1555
1556 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1557 unsigned int);
1558
1559 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1560 struct dwarf2_cu *cu);
1561
1562 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1563 struct dwarf2_cu *cu);
1564
1565 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1566
1567 static struct die_info *die_specification (struct die_info *die,
1568 struct dwarf2_cu **);
1569
1570 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1571 struct dwarf2_cu *cu);
1572
1573 static void dwarf_decode_lines (struct line_header *, const char *,
1574 struct dwarf2_cu *, struct partial_symtab *,
1575 CORE_ADDR, int decode_mapping);
1576
1577 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1578 const char *);
1579
1580 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1581 const char *, const char *,
1582 CORE_ADDR);
1583
1584 static struct symbol *new_symbol (struct die_info *, struct type *,
1585 struct dwarf2_cu *, struct symbol * = NULL);
1586
1587 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1588 struct dwarf2_cu *);
1589
1590 static void dwarf2_const_value_attr (const struct attribute *attr,
1591 struct type *type,
1592 const char *name,
1593 struct obstack *obstack,
1594 struct dwarf2_cu *cu, LONGEST *value,
1595 const gdb_byte **bytes,
1596 struct dwarf2_locexpr_baton **baton);
1597
1598 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1599
1600 static int need_gnat_info (struct dwarf2_cu *);
1601
1602 static struct type *die_descriptive_type (struct die_info *,
1603 struct dwarf2_cu *);
1604
1605 static void set_descriptive_type (struct type *, struct die_info *,
1606 struct dwarf2_cu *);
1607
1608 static struct type *die_containing_type (struct die_info *,
1609 struct dwarf2_cu *);
1610
1611 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1612 struct dwarf2_cu *);
1613
1614 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1615
1616 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1617
1618 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1619
1620 static char *typename_concat (struct obstack *obs, const char *prefix,
1621 const char *suffix, int physname,
1622 struct dwarf2_cu *cu);
1623
1624 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1625
1626 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1627
1628 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1629
1630 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1631
1632 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1633
1634 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1635
1636 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1637 struct dwarf2_cu *, struct partial_symtab *);
1638
1639 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1640 values. Keep the items ordered with increasing constraints compliance. */
1641 enum pc_bounds_kind
1642 {
1643 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1644 PC_BOUNDS_NOT_PRESENT,
1645
1646 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1647 were present but they do not form a valid range of PC addresses. */
1648 PC_BOUNDS_INVALID,
1649
1650 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1651 PC_BOUNDS_RANGES,
1652
1653 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1654 PC_BOUNDS_HIGH_LOW,
1655 };
1656
1657 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1658 CORE_ADDR *, CORE_ADDR *,
1659 struct dwarf2_cu *,
1660 struct partial_symtab *);
1661
1662 static void get_scope_pc_bounds (struct die_info *,
1663 CORE_ADDR *, CORE_ADDR *,
1664 struct dwarf2_cu *);
1665
1666 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1667 CORE_ADDR, struct dwarf2_cu *);
1668
1669 static void dwarf2_add_field (struct field_info *, struct die_info *,
1670 struct dwarf2_cu *);
1671
1672 static void dwarf2_attach_fields_to_type (struct field_info *,
1673 struct type *, struct dwarf2_cu *);
1674
1675 static void dwarf2_add_member_fn (struct field_info *,
1676 struct die_info *, struct type *,
1677 struct dwarf2_cu *);
1678
1679 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1680 struct type *,
1681 struct dwarf2_cu *);
1682
1683 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1684
1685 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1686
1687 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1688
1689 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1690
1691 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1692
1693 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1694
1695 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1696
1697 static struct type *read_module_type (struct die_info *die,
1698 struct dwarf2_cu *cu);
1699
1700 static const char *namespace_name (struct die_info *die,
1701 int *is_anonymous, struct dwarf2_cu *);
1702
1703 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1704
1705 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1706
1707 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1708 struct dwarf2_cu *);
1709
1710 static struct die_info *read_die_and_siblings_1
1711 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1712 struct die_info *);
1713
1714 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1715 const gdb_byte *info_ptr,
1716 const gdb_byte **new_info_ptr,
1717 struct die_info *parent);
1718
1719 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1720 struct die_info **, const gdb_byte *,
1721 int *, int);
1722
1723 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1724 struct die_info **, const gdb_byte *,
1725 int *);
1726
1727 static void process_die (struct die_info *, struct dwarf2_cu *);
1728
1729 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1730 struct obstack *);
1731
1732 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1733
1734 static const char *dwarf2_full_name (const char *name,
1735 struct die_info *die,
1736 struct dwarf2_cu *cu);
1737
1738 static const char *dwarf2_physname (const char *name, struct die_info *die,
1739 struct dwarf2_cu *cu);
1740
1741 static struct die_info *dwarf2_extension (struct die_info *die,
1742 struct dwarf2_cu **);
1743
1744 static const char *dwarf_tag_name (unsigned int);
1745
1746 static const char *dwarf_attr_name (unsigned int);
1747
1748 static const char *dwarf_form_name (unsigned int);
1749
1750 static const char *dwarf_bool_name (unsigned int);
1751
1752 static const char *dwarf_type_encoding_name (unsigned int);
1753
1754 static struct die_info *sibling_die (struct die_info *);
1755
1756 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1757
1758 static void dump_die_for_error (struct die_info *);
1759
1760 static void dump_die_1 (struct ui_file *, int level, int max_level,
1761 struct die_info *);
1762
1763 /*static*/ void dump_die (struct die_info *, int max_level);
1764
1765 static void store_in_ref_table (struct die_info *,
1766 struct dwarf2_cu *);
1767
1768 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1769
1770 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1771
1772 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1773 const struct attribute *,
1774 struct dwarf2_cu **);
1775
1776 static struct die_info *follow_die_ref (struct die_info *,
1777 const struct attribute *,
1778 struct dwarf2_cu **);
1779
1780 static struct die_info *follow_die_sig (struct die_info *,
1781 const struct attribute *,
1782 struct dwarf2_cu **);
1783
1784 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1785 struct dwarf2_cu *);
1786
1787 static struct type *get_DW_AT_signature_type (struct die_info *,
1788 const struct attribute *,
1789 struct dwarf2_cu *);
1790
1791 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1792
1793 static void read_signatured_type (struct signatured_type *);
1794
1795 static int attr_to_dynamic_prop (const struct attribute *attr,
1796 struct die_info *die, struct dwarf2_cu *cu,
1797 struct dynamic_prop *prop);
1798
1799 /* memory allocation interface */
1800
1801 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1802
1803 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1804
1805 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1806
1807 static int attr_form_is_block (const struct attribute *);
1808
1809 static int attr_form_is_section_offset (const struct attribute *);
1810
1811 static int attr_form_is_constant (const struct attribute *);
1812
1813 static int attr_form_is_ref (const struct attribute *);
1814
1815 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1816 struct dwarf2_loclist_baton *baton,
1817 const struct attribute *attr);
1818
1819 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1820 struct symbol *sym,
1821 struct dwarf2_cu *cu,
1822 int is_block);
1823
1824 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1825 const gdb_byte *info_ptr,
1826 struct abbrev_info *abbrev);
1827
1828 static hashval_t partial_die_hash (const void *item);
1829
1830 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1831
1832 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1833 (sect_offset sect_off, unsigned int offset_in_dwz,
1834 struct dwarf2_per_objfile *dwarf2_per_objfile);
1835
1836 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1837 struct die_info *comp_unit_die,
1838 enum language pretend_language);
1839
1840 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1841
1842 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1843
1844 static struct type *set_die_type (struct die_info *, struct type *,
1845 struct dwarf2_cu *);
1846
1847 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1848
1849 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1850
1851 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1852 enum language);
1853
1854 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1855 enum language);
1856
1857 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1858 enum language);
1859
1860 static void dwarf2_add_dependence (struct dwarf2_cu *,
1861 struct dwarf2_per_cu_data *);
1862
1863 static void dwarf2_mark (struct dwarf2_cu *);
1864
1865 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1866
1867 static struct type *get_die_type_at_offset (sect_offset,
1868 struct dwarf2_per_cu_data *);
1869
1870 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1871
1872 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1873 enum language pretend_language);
1874
1875 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1876
1877 /* Class, the destructor of which frees all allocated queue entries. This
1878 will only have work to do if an error was thrown while processing the
1879 dwarf. If no error was thrown then the queue entries should have all
1880 been processed, and freed, as we went along. */
1881
1882 class dwarf2_queue_guard
1883 {
1884 public:
1885 dwarf2_queue_guard () = default;
1886
1887 /* Free any entries remaining on the queue. There should only be
1888 entries left if we hit an error while processing the dwarf. */
1889 ~dwarf2_queue_guard ()
1890 {
1891 struct dwarf2_queue_item *item, *last;
1892
1893 item = dwarf2_queue;
1894 while (item)
1895 {
1896 /* Anything still marked queued is likely to be in an
1897 inconsistent state, so discard it. */
1898 if (item->per_cu->queued)
1899 {
1900 if (item->per_cu->cu != NULL)
1901 free_one_cached_comp_unit (item->per_cu);
1902 item->per_cu->queued = 0;
1903 }
1904
1905 last = item;
1906 item = item->next;
1907 xfree (last);
1908 }
1909
1910 dwarf2_queue = dwarf2_queue_tail = NULL;
1911 }
1912 };
1913
1914 /* The return type of find_file_and_directory. Note, the enclosed
1915 string pointers are only valid while this object is valid. */
1916
1917 struct file_and_directory
1918 {
1919 /* The filename. This is never NULL. */
1920 const char *name;
1921
1922 /* The compilation directory. NULL if not known. If we needed to
1923 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1924 points directly to the DW_AT_comp_dir string attribute owned by
1925 the obstack that owns the DIE. */
1926 const char *comp_dir;
1927
1928 /* If we needed to build a new string for comp_dir, this is what
1929 owns the storage. */
1930 std::string comp_dir_storage;
1931 };
1932
1933 static file_and_directory find_file_and_directory (struct die_info *die,
1934 struct dwarf2_cu *cu);
1935
1936 static char *file_full_name (int file, struct line_header *lh,
1937 const char *comp_dir);
1938
1939 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1940 enum class rcuh_kind { COMPILE, TYPE };
1941
1942 static const gdb_byte *read_and_check_comp_unit_head
1943 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1944 struct comp_unit_head *header,
1945 struct dwarf2_section_info *section,
1946 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1947 rcuh_kind section_kind);
1948
1949 static void init_cutu_and_read_dies
1950 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1951 int use_existing_cu, int keep, bool skip_partial,
1952 die_reader_func_ftype *die_reader_func, void *data);
1953
1954 static void init_cutu_and_read_dies_simple
1955 (struct dwarf2_per_cu_data *this_cu,
1956 die_reader_func_ftype *die_reader_func, void *data);
1957
1958 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1959
1960 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1961
1962 static struct dwo_unit *lookup_dwo_unit_in_dwp
1963 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1964 struct dwp_file *dwp_file, const char *comp_dir,
1965 ULONGEST signature, int is_debug_types);
1966
1967 static struct dwp_file *get_dwp_file
1968 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1969
1970 static struct dwo_unit *lookup_dwo_comp_unit
1971 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1972
1973 static struct dwo_unit *lookup_dwo_type_unit
1974 (struct signatured_type *, const char *, const char *);
1975
1976 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1977
1978 static void free_dwo_file (struct dwo_file *);
1979
1980 /* A unique_ptr helper to free a dwo_file. */
1981
1982 struct dwo_file_deleter
1983 {
1984 void operator() (struct dwo_file *df) const
1985 {
1986 free_dwo_file (df);
1987 }
1988 };
1989
1990 /* A unique pointer to a dwo_file. */
1991
1992 typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
1993
1994 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1995
1996 static void check_producer (struct dwarf2_cu *cu);
1997
1998 static void free_line_header_voidp (void *arg);
1999 \f
2000 /* Various complaints about symbol reading that don't abort the process. */
2001
2002 static void
2003 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2004 {
2005 complaint (_("statement list doesn't fit in .debug_line section"));
2006 }
2007
2008 static void
2009 dwarf2_debug_line_missing_file_complaint (void)
2010 {
2011 complaint (_(".debug_line section has line data without a file"));
2012 }
2013
2014 static void
2015 dwarf2_debug_line_missing_end_sequence_complaint (void)
2016 {
2017 complaint (_(".debug_line section has line "
2018 "program sequence without an end"));
2019 }
2020
2021 static void
2022 dwarf2_complex_location_expr_complaint (void)
2023 {
2024 complaint (_("location expression too complex"));
2025 }
2026
2027 static void
2028 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2029 int arg3)
2030 {
2031 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2032 arg1, arg2, arg3);
2033 }
2034
2035 static void
2036 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2037 {
2038 complaint (_("debug info runs off end of %s section"
2039 " [in module %s]"),
2040 get_section_name (section),
2041 get_section_file_name (section));
2042 }
2043
2044 static void
2045 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2046 {
2047 complaint (_("macro debug info contains a "
2048 "malformed macro definition:\n`%s'"),
2049 arg1);
2050 }
2051
2052 static void
2053 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2054 {
2055 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2056 arg1, arg2);
2057 }
2058
2059 /* Hash function for line_header_hash. */
2060
2061 static hashval_t
2062 line_header_hash (const struct line_header *ofs)
2063 {
2064 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2065 }
2066
2067 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2068
2069 static hashval_t
2070 line_header_hash_voidp (const void *item)
2071 {
2072 const struct line_header *ofs = (const struct line_header *) item;
2073
2074 return line_header_hash (ofs);
2075 }
2076
2077 /* Equality function for line_header_hash. */
2078
2079 static int
2080 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2081 {
2082 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2083 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2084
2085 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2086 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2087 }
2088
2089 \f
2090
2091 /* Read the given attribute value as an address, taking the attribute's
2092 form into account. */
2093
2094 static CORE_ADDR
2095 attr_value_as_address (struct attribute *attr)
2096 {
2097 CORE_ADDR addr;
2098
2099 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2100 {
2101 /* Aside from a few clearly defined exceptions, attributes that
2102 contain an address must always be in DW_FORM_addr form.
2103 Unfortunately, some compilers happen to be violating this
2104 requirement by encoding addresses using other forms, such
2105 as DW_FORM_data4 for example. For those broken compilers,
2106 we try to do our best, without any guarantee of success,
2107 to interpret the address correctly. It would also be nice
2108 to generate a complaint, but that would require us to maintain
2109 a list of legitimate cases where a non-address form is allowed,
2110 as well as update callers to pass in at least the CU's DWARF
2111 version. This is more overhead than what we're willing to
2112 expand for a pretty rare case. */
2113 addr = DW_UNSND (attr);
2114 }
2115 else
2116 addr = DW_ADDR (attr);
2117
2118 return addr;
2119 }
2120
2121 /* See declaration. */
2122
2123 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2124 const dwarf2_debug_sections *names)
2125 : objfile (objfile_)
2126 {
2127 if (names == NULL)
2128 names = &dwarf2_elf_names;
2129
2130 bfd *obfd = objfile->obfd;
2131
2132 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2133 locate_sections (obfd, sec, *names);
2134 }
2135
2136 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2137
2138 dwarf2_per_objfile::~dwarf2_per_objfile ()
2139 {
2140 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2141 free_cached_comp_units ();
2142
2143 if (quick_file_names_table)
2144 htab_delete (quick_file_names_table);
2145
2146 if (line_header_hash)
2147 htab_delete (line_header_hash);
2148
2149 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2150 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
2151
2152 for (signatured_type *sig_type : all_type_units)
2153 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
2154
2155 VEC_free (dwarf2_section_info_def, types);
2156
2157 if (dwo_files != NULL)
2158 free_dwo_files (dwo_files, objfile);
2159
2160 /* Everything else should be on the objfile obstack. */
2161 }
2162
2163 /* See declaration. */
2164
2165 void
2166 dwarf2_per_objfile::free_cached_comp_units ()
2167 {
2168 dwarf2_per_cu_data *per_cu = read_in_chain;
2169 dwarf2_per_cu_data **last_chain = &read_in_chain;
2170 while (per_cu != NULL)
2171 {
2172 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2173
2174 delete per_cu->cu;
2175 *last_chain = next_cu;
2176 per_cu = next_cu;
2177 }
2178 }
2179
2180 /* A helper class that calls free_cached_comp_units on
2181 destruction. */
2182
2183 class free_cached_comp_units
2184 {
2185 public:
2186
2187 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2188 : m_per_objfile (per_objfile)
2189 {
2190 }
2191
2192 ~free_cached_comp_units ()
2193 {
2194 m_per_objfile->free_cached_comp_units ();
2195 }
2196
2197 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2198
2199 private:
2200
2201 dwarf2_per_objfile *m_per_objfile;
2202 };
2203
2204 /* Try to locate the sections we need for DWARF 2 debugging
2205 information and return true if we have enough to do something.
2206 NAMES points to the dwarf2 section names, or is NULL if the standard
2207 ELF names are used. */
2208
2209 int
2210 dwarf2_has_info (struct objfile *objfile,
2211 const struct dwarf2_debug_sections *names)
2212 {
2213 if (objfile->flags & OBJF_READNEVER)
2214 return 0;
2215
2216 struct dwarf2_per_objfile *dwarf2_per_objfile
2217 = get_dwarf2_per_objfile (objfile);
2218
2219 if (dwarf2_per_objfile == NULL)
2220 {
2221 /* Initialize per-objfile state. */
2222 dwarf2_per_objfile
2223 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2224 names);
2225 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2226 }
2227 return (!dwarf2_per_objfile->info.is_virtual
2228 && dwarf2_per_objfile->info.s.section != NULL
2229 && !dwarf2_per_objfile->abbrev.is_virtual
2230 && dwarf2_per_objfile->abbrev.s.section != NULL);
2231 }
2232
2233 /* Return the containing section of virtual section SECTION. */
2234
2235 static struct dwarf2_section_info *
2236 get_containing_section (const struct dwarf2_section_info *section)
2237 {
2238 gdb_assert (section->is_virtual);
2239 return section->s.containing_section;
2240 }
2241
2242 /* Return the bfd owner of SECTION. */
2243
2244 static struct bfd *
2245 get_section_bfd_owner (const struct dwarf2_section_info *section)
2246 {
2247 if (section->is_virtual)
2248 {
2249 section = get_containing_section (section);
2250 gdb_assert (!section->is_virtual);
2251 }
2252 return section->s.section->owner;
2253 }
2254
2255 /* Return the bfd section of SECTION.
2256 Returns NULL if the section is not present. */
2257
2258 static asection *
2259 get_section_bfd_section (const struct dwarf2_section_info *section)
2260 {
2261 if (section->is_virtual)
2262 {
2263 section = get_containing_section (section);
2264 gdb_assert (!section->is_virtual);
2265 }
2266 return section->s.section;
2267 }
2268
2269 /* Return the name of SECTION. */
2270
2271 static const char *
2272 get_section_name (const struct dwarf2_section_info *section)
2273 {
2274 asection *sectp = get_section_bfd_section (section);
2275
2276 gdb_assert (sectp != NULL);
2277 return bfd_section_name (get_section_bfd_owner (section), sectp);
2278 }
2279
2280 /* Return the name of the file SECTION is in. */
2281
2282 static const char *
2283 get_section_file_name (const struct dwarf2_section_info *section)
2284 {
2285 bfd *abfd = get_section_bfd_owner (section);
2286
2287 return bfd_get_filename (abfd);
2288 }
2289
2290 /* Return the id of SECTION.
2291 Returns 0 if SECTION doesn't exist. */
2292
2293 static int
2294 get_section_id (const struct dwarf2_section_info *section)
2295 {
2296 asection *sectp = get_section_bfd_section (section);
2297
2298 if (sectp == NULL)
2299 return 0;
2300 return sectp->id;
2301 }
2302
2303 /* Return the flags of SECTION.
2304 SECTION (or containing section if this is a virtual section) must exist. */
2305
2306 static int
2307 get_section_flags (const struct dwarf2_section_info *section)
2308 {
2309 asection *sectp = get_section_bfd_section (section);
2310
2311 gdb_assert (sectp != NULL);
2312 return bfd_get_section_flags (sectp->owner, sectp);
2313 }
2314
2315 /* When loading sections, we look either for uncompressed section or for
2316 compressed section names. */
2317
2318 static int
2319 section_is_p (const char *section_name,
2320 const struct dwarf2_section_names *names)
2321 {
2322 if (names->normal != NULL
2323 && strcmp (section_name, names->normal) == 0)
2324 return 1;
2325 if (names->compressed != NULL
2326 && strcmp (section_name, names->compressed) == 0)
2327 return 1;
2328 return 0;
2329 }
2330
2331 /* See declaration. */
2332
2333 void
2334 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2335 const dwarf2_debug_sections &names)
2336 {
2337 flagword aflag = bfd_get_section_flags (abfd, sectp);
2338
2339 if ((aflag & SEC_HAS_CONTENTS) == 0)
2340 {
2341 }
2342 else if (section_is_p (sectp->name, &names.info))
2343 {
2344 this->info.s.section = sectp;
2345 this->info.size = bfd_get_section_size (sectp);
2346 }
2347 else if (section_is_p (sectp->name, &names.abbrev))
2348 {
2349 this->abbrev.s.section = sectp;
2350 this->abbrev.size = bfd_get_section_size (sectp);
2351 }
2352 else if (section_is_p (sectp->name, &names.line))
2353 {
2354 this->line.s.section = sectp;
2355 this->line.size = bfd_get_section_size (sectp);
2356 }
2357 else if (section_is_p (sectp->name, &names.loc))
2358 {
2359 this->loc.s.section = sectp;
2360 this->loc.size = bfd_get_section_size (sectp);
2361 }
2362 else if (section_is_p (sectp->name, &names.loclists))
2363 {
2364 this->loclists.s.section = sectp;
2365 this->loclists.size = bfd_get_section_size (sectp);
2366 }
2367 else if (section_is_p (sectp->name, &names.macinfo))
2368 {
2369 this->macinfo.s.section = sectp;
2370 this->macinfo.size = bfd_get_section_size (sectp);
2371 }
2372 else if (section_is_p (sectp->name, &names.macro))
2373 {
2374 this->macro.s.section = sectp;
2375 this->macro.size = bfd_get_section_size (sectp);
2376 }
2377 else if (section_is_p (sectp->name, &names.str))
2378 {
2379 this->str.s.section = sectp;
2380 this->str.size = bfd_get_section_size (sectp);
2381 }
2382 else if (section_is_p (sectp->name, &names.line_str))
2383 {
2384 this->line_str.s.section = sectp;
2385 this->line_str.size = bfd_get_section_size (sectp);
2386 }
2387 else if (section_is_p (sectp->name, &names.addr))
2388 {
2389 this->addr.s.section = sectp;
2390 this->addr.size = bfd_get_section_size (sectp);
2391 }
2392 else if (section_is_p (sectp->name, &names.frame))
2393 {
2394 this->frame.s.section = sectp;
2395 this->frame.size = bfd_get_section_size (sectp);
2396 }
2397 else if (section_is_p (sectp->name, &names.eh_frame))
2398 {
2399 this->eh_frame.s.section = sectp;
2400 this->eh_frame.size = bfd_get_section_size (sectp);
2401 }
2402 else if (section_is_p (sectp->name, &names.ranges))
2403 {
2404 this->ranges.s.section = sectp;
2405 this->ranges.size = bfd_get_section_size (sectp);
2406 }
2407 else if (section_is_p (sectp->name, &names.rnglists))
2408 {
2409 this->rnglists.s.section = sectp;
2410 this->rnglists.size = bfd_get_section_size (sectp);
2411 }
2412 else if (section_is_p (sectp->name, &names.types))
2413 {
2414 struct dwarf2_section_info type_section;
2415
2416 memset (&type_section, 0, sizeof (type_section));
2417 type_section.s.section = sectp;
2418 type_section.size = bfd_get_section_size (sectp);
2419
2420 VEC_safe_push (dwarf2_section_info_def, this->types,
2421 &type_section);
2422 }
2423 else if (section_is_p (sectp->name, &names.gdb_index))
2424 {
2425 this->gdb_index.s.section = sectp;
2426 this->gdb_index.size = bfd_get_section_size (sectp);
2427 }
2428 else if (section_is_p (sectp->name, &names.debug_names))
2429 {
2430 this->debug_names.s.section = sectp;
2431 this->debug_names.size = bfd_get_section_size (sectp);
2432 }
2433 else if (section_is_p (sectp->name, &names.debug_aranges))
2434 {
2435 this->debug_aranges.s.section = sectp;
2436 this->debug_aranges.size = bfd_get_section_size (sectp);
2437 }
2438
2439 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2440 && bfd_section_vma (abfd, sectp) == 0)
2441 this->has_section_at_zero = true;
2442 }
2443
2444 /* A helper function that decides whether a section is empty,
2445 or not present. */
2446
2447 static int
2448 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2449 {
2450 if (section->is_virtual)
2451 return section->size == 0;
2452 return section->s.section == NULL || section->size == 0;
2453 }
2454
2455 /* See dwarf2read.h. */
2456
2457 void
2458 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2459 {
2460 asection *sectp;
2461 bfd *abfd;
2462 gdb_byte *buf, *retbuf;
2463
2464 if (info->readin)
2465 return;
2466 info->buffer = NULL;
2467 info->readin = 1;
2468
2469 if (dwarf2_section_empty_p (info))
2470 return;
2471
2472 sectp = get_section_bfd_section (info);
2473
2474 /* If this is a virtual section we need to read in the real one first. */
2475 if (info->is_virtual)
2476 {
2477 struct dwarf2_section_info *containing_section =
2478 get_containing_section (info);
2479
2480 gdb_assert (sectp != NULL);
2481 if ((sectp->flags & SEC_RELOC) != 0)
2482 {
2483 error (_("Dwarf Error: DWP format V2 with relocations is not"
2484 " supported in section %s [in module %s]"),
2485 get_section_name (info), get_section_file_name (info));
2486 }
2487 dwarf2_read_section (objfile, containing_section);
2488 /* Other code should have already caught virtual sections that don't
2489 fit. */
2490 gdb_assert (info->virtual_offset + info->size
2491 <= containing_section->size);
2492 /* If the real section is empty or there was a problem reading the
2493 section we shouldn't get here. */
2494 gdb_assert (containing_section->buffer != NULL);
2495 info->buffer = containing_section->buffer + info->virtual_offset;
2496 return;
2497 }
2498
2499 /* If the section has relocations, we must read it ourselves.
2500 Otherwise we attach it to the BFD. */
2501 if ((sectp->flags & SEC_RELOC) == 0)
2502 {
2503 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2504 return;
2505 }
2506
2507 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2508 info->buffer = buf;
2509
2510 /* When debugging .o files, we may need to apply relocations; see
2511 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2512 We never compress sections in .o files, so we only need to
2513 try this when the section is not compressed. */
2514 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2515 if (retbuf != NULL)
2516 {
2517 info->buffer = retbuf;
2518 return;
2519 }
2520
2521 abfd = get_section_bfd_owner (info);
2522 gdb_assert (abfd != NULL);
2523
2524 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2525 || bfd_bread (buf, info->size, abfd) != info->size)
2526 {
2527 error (_("Dwarf Error: Can't read DWARF data"
2528 " in section %s [in module %s]"),
2529 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2530 }
2531 }
2532
2533 /* A helper function that returns the size of a section in a safe way.
2534 If you are positive that the section has been read before using the
2535 size, then it is safe to refer to the dwarf2_section_info object's
2536 "size" field directly. In other cases, you must call this
2537 function, because for compressed sections the size field is not set
2538 correctly until the section has been read. */
2539
2540 static bfd_size_type
2541 dwarf2_section_size (struct objfile *objfile,
2542 struct dwarf2_section_info *info)
2543 {
2544 if (!info->readin)
2545 dwarf2_read_section (objfile, info);
2546 return info->size;
2547 }
2548
2549 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2550 SECTION_NAME. */
2551
2552 void
2553 dwarf2_get_section_info (struct objfile *objfile,
2554 enum dwarf2_section_enum sect,
2555 asection **sectp, const gdb_byte **bufp,
2556 bfd_size_type *sizep)
2557 {
2558 struct dwarf2_per_objfile *data
2559 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2560 dwarf2_objfile_data_key);
2561 struct dwarf2_section_info *info;
2562
2563 /* We may see an objfile without any DWARF, in which case we just
2564 return nothing. */
2565 if (data == NULL)
2566 {
2567 *sectp = NULL;
2568 *bufp = NULL;
2569 *sizep = 0;
2570 return;
2571 }
2572 switch (sect)
2573 {
2574 case DWARF2_DEBUG_FRAME:
2575 info = &data->frame;
2576 break;
2577 case DWARF2_EH_FRAME:
2578 info = &data->eh_frame;
2579 break;
2580 default:
2581 gdb_assert_not_reached ("unexpected section");
2582 }
2583
2584 dwarf2_read_section (objfile, info);
2585
2586 *sectp = get_section_bfd_section (info);
2587 *bufp = info->buffer;
2588 *sizep = info->size;
2589 }
2590
2591 /* A helper function to find the sections for a .dwz file. */
2592
2593 static void
2594 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2595 {
2596 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2597
2598 /* Note that we only support the standard ELF names, because .dwz
2599 is ELF-only (at the time of writing). */
2600 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2601 {
2602 dwz_file->abbrev.s.section = sectp;
2603 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2604 }
2605 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2606 {
2607 dwz_file->info.s.section = sectp;
2608 dwz_file->info.size = bfd_get_section_size (sectp);
2609 }
2610 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2611 {
2612 dwz_file->str.s.section = sectp;
2613 dwz_file->str.size = bfd_get_section_size (sectp);
2614 }
2615 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2616 {
2617 dwz_file->line.s.section = sectp;
2618 dwz_file->line.size = bfd_get_section_size (sectp);
2619 }
2620 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2621 {
2622 dwz_file->macro.s.section = sectp;
2623 dwz_file->macro.size = bfd_get_section_size (sectp);
2624 }
2625 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2626 {
2627 dwz_file->gdb_index.s.section = sectp;
2628 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2629 }
2630 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2631 {
2632 dwz_file->debug_names.s.section = sectp;
2633 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2634 }
2635 }
2636
2637 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2638 there is no .gnu_debugaltlink section in the file. Error if there
2639 is such a section but the file cannot be found. */
2640
2641 static struct dwz_file *
2642 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2643 {
2644 const char *filename;
2645 bfd_size_type buildid_len_arg;
2646 size_t buildid_len;
2647 bfd_byte *buildid;
2648
2649 if (dwarf2_per_objfile->dwz_file != NULL)
2650 return dwarf2_per_objfile->dwz_file.get ();
2651
2652 bfd_set_error (bfd_error_no_error);
2653 gdb::unique_xmalloc_ptr<char> data
2654 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2655 &buildid_len_arg, &buildid));
2656 if (data == NULL)
2657 {
2658 if (bfd_get_error () == bfd_error_no_error)
2659 return NULL;
2660 error (_("could not read '.gnu_debugaltlink' section: %s"),
2661 bfd_errmsg (bfd_get_error ()));
2662 }
2663
2664 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2665
2666 buildid_len = (size_t) buildid_len_arg;
2667
2668 filename = data.get ();
2669
2670 std::string abs_storage;
2671 if (!IS_ABSOLUTE_PATH (filename))
2672 {
2673 gdb::unique_xmalloc_ptr<char> abs
2674 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2675
2676 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2677 filename = abs_storage.c_str ();
2678 }
2679
2680 /* First try the file name given in the section. If that doesn't
2681 work, try to use the build-id instead. */
2682 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2683 if (dwz_bfd != NULL)
2684 {
2685 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2686 dwz_bfd.release ();
2687 }
2688
2689 if (dwz_bfd == NULL)
2690 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2691
2692 if (dwz_bfd == NULL)
2693 error (_("could not find '.gnu_debugaltlink' file for %s"),
2694 objfile_name (dwarf2_per_objfile->objfile));
2695
2696 std::unique_ptr<struct dwz_file> result
2697 (new struct dwz_file (std::move (dwz_bfd)));
2698
2699 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2700 result.get ());
2701
2702 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2703 result->dwz_bfd.get ());
2704 dwarf2_per_objfile->dwz_file = std::move (result);
2705 return dwarf2_per_objfile->dwz_file.get ();
2706 }
2707 \f
2708 /* DWARF quick_symbols_functions support. */
2709
2710 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2711 unique line tables, so we maintain a separate table of all .debug_line
2712 derived entries to support the sharing.
2713 All the quick functions need is the list of file names. We discard the
2714 line_header when we're done and don't need to record it here. */
2715 struct quick_file_names
2716 {
2717 /* The data used to construct the hash key. */
2718 struct stmt_list_hash hash;
2719
2720 /* The number of entries in file_names, real_names. */
2721 unsigned int num_file_names;
2722
2723 /* The file names from the line table, after being run through
2724 file_full_name. */
2725 const char **file_names;
2726
2727 /* The file names from the line table after being run through
2728 gdb_realpath. These are computed lazily. */
2729 const char **real_names;
2730 };
2731
2732 /* When using the index (and thus not using psymtabs), each CU has an
2733 object of this type. This is used to hold information needed by
2734 the various "quick" methods. */
2735 struct dwarf2_per_cu_quick_data
2736 {
2737 /* The file table. This can be NULL if there was no file table
2738 or it's currently not read in.
2739 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2740 struct quick_file_names *file_names;
2741
2742 /* The corresponding symbol table. This is NULL if symbols for this
2743 CU have not yet been read. */
2744 struct compunit_symtab *compunit_symtab;
2745
2746 /* A temporary mark bit used when iterating over all CUs in
2747 expand_symtabs_matching. */
2748 unsigned int mark : 1;
2749
2750 /* True if we've tried to read the file table and found there isn't one.
2751 There will be no point in trying to read it again next time. */
2752 unsigned int no_file_data : 1;
2753 };
2754
2755 /* Utility hash function for a stmt_list_hash. */
2756
2757 static hashval_t
2758 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2759 {
2760 hashval_t v = 0;
2761
2762 if (stmt_list_hash->dwo_unit != NULL)
2763 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2764 v += to_underlying (stmt_list_hash->line_sect_off);
2765 return v;
2766 }
2767
2768 /* Utility equality function for a stmt_list_hash. */
2769
2770 static int
2771 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2772 const struct stmt_list_hash *rhs)
2773 {
2774 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2775 return 0;
2776 if (lhs->dwo_unit != NULL
2777 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2778 return 0;
2779
2780 return lhs->line_sect_off == rhs->line_sect_off;
2781 }
2782
2783 /* Hash function for a quick_file_names. */
2784
2785 static hashval_t
2786 hash_file_name_entry (const void *e)
2787 {
2788 const struct quick_file_names *file_data
2789 = (const struct quick_file_names *) e;
2790
2791 return hash_stmt_list_entry (&file_data->hash);
2792 }
2793
2794 /* Equality function for a quick_file_names. */
2795
2796 static int
2797 eq_file_name_entry (const void *a, const void *b)
2798 {
2799 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2800 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2801
2802 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2803 }
2804
2805 /* Delete function for a quick_file_names. */
2806
2807 static void
2808 delete_file_name_entry (void *e)
2809 {
2810 struct quick_file_names *file_data = (struct quick_file_names *) e;
2811 int i;
2812
2813 for (i = 0; i < file_data->num_file_names; ++i)
2814 {
2815 xfree ((void*) file_data->file_names[i]);
2816 if (file_data->real_names)
2817 xfree ((void*) file_data->real_names[i]);
2818 }
2819
2820 /* The space for the struct itself lives on objfile_obstack,
2821 so we don't free it here. */
2822 }
2823
2824 /* Create a quick_file_names hash table. */
2825
2826 static htab_t
2827 create_quick_file_names_table (unsigned int nr_initial_entries)
2828 {
2829 return htab_create_alloc (nr_initial_entries,
2830 hash_file_name_entry, eq_file_name_entry,
2831 delete_file_name_entry, xcalloc, xfree);
2832 }
2833
2834 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2835 have to be created afterwards. You should call age_cached_comp_units after
2836 processing PER_CU->CU. dw2_setup must have been already called. */
2837
2838 static void
2839 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2840 {
2841 if (per_cu->is_debug_types)
2842 load_full_type_unit (per_cu);
2843 else
2844 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2845
2846 if (per_cu->cu == NULL)
2847 return; /* Dummy CU. */
2848
2849 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2850 }
2851
2852 /* Read in the symbols for PER_CU. */
2853
2854 static void
2855 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2856 {
2857 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2858
2859 /* Skip type_unit_groups, reading the type units they contain
2860 is handled elsewhere. */
2861 if (IS_TYPE_UNIT_GROUP (per_cu))
2862 return;
2863
2864 /* The destructor of dwarf2_queue_guard frees any entries left on
2865 the queue. After this point we're guaranteed to leave this function
2866 with the dwarf queue empty. */
2867 dwarf2_queue_guard q_guard;
2868
2869 if (dwarf2_per_objfile->using_index
2870 ? per_cu->v.quick->compunit_symtab == NULL
2871 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2872 {
2873 queue_comp_unit (per_cu, language_minimal);
2874 load_cu (per_cu, skip_partial);
2875
2876 /* If we just loaded a CU from a DWO, and we're working with an index
2877 that may badly handle TUs, load all the TUs in that DWO as well.
2878 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2879 if (!per_cu->is_debug_types
2880 && per_cu->cu != NULL
2881 && per_cu->cu->dwo_unit != NULL
2882 && dwarf2_per_objfile->index_table != NULL
2883 && dwarf2_per_objfile->index_table->version <= 7
2884 /* DWP files aren't supported yet. */
2885 && get_dwp_file (dwarf2_per_objfile) == NULL)
2886 queue_and_load_all_dwo_tus (per_cu);
2887 }
2888
2889 process_queue (dwarf2_per_objfile);
2890
2891 /* Age the cache, releasing compilation units that have not
2892 been used recently. */
2893 age_cached_comp_units (dwarf2_per_objfile);
2894 }
2895
2896 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2897 the objfile from which this CU came. Returns the resulting symbol
2898 table. */
2899
2900 static struct compunit_symtab *
2901 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2902 {
2903 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2904
2905 gdb_assert (dwarf2_per_objfile->using_index);
2906 if (!per_cu->v.quick->compunit_symtab)
2907 {
2908 free_cached_comp_units freer (dwarf2_per_objfile);
2909 scoped_restore decrementer = increment_reading_symtab ();
2910 dw2_do_instantiate_symtab (per_cu, skip_partial);
2911 process_cu_includes (dwarf2_per_objfile);
2912 }
2913
2914 return per_cu->v.quick->compunit_symtab;
2915 }
2916
2917 /* See declaration. */
2918
2919 dwarf2_per_cu_data *
2920 dwarf2_per_objfile::get_cutu (int index)
2921 {
2922 if (index >= this->all_comp_units.size ())
2923 {
2924 index -= this->all_comp_units.size ();
2925 gdb_assert (index < this->all_type_units.size ());
2926 return &this->all_type_units[index]->per_cu;
2927 }
2928
2929 return this->all_comp_units[index];
2930 }
2931
2932 /* See declaration. */
2933
2934 dwarf2_per_cu_data *
2935 dwarf2_per_objfile::get_cu (int index)
2936 {
2937 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2938
2939 return this->all_comp_units[index];
2940 }
2941
2942 /* See declaration. */
2943
2944 signatured_type *
2945 dwarf2_per_objfile::get_tu (int index)
2946 {
2947 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2948
2949 return this->all_type_units[index];
2950 }
2951
2952 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2953 objfile_obstack, and constructed with the specified field
2954 values. */
2955
2956 static dwarf2_per_cu_data *
2957 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2958 struct dwarf2_section_info *section,
2959 int is_dwz,
2960 sect_offset sect_off, ULONGEST length)
2961 {
2962 struct objfile *objfile = dwarf2_per_objfile->objfile;
2963 dwarf2_per_cu_data *the_cu
2964 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2965 struct dwarf2_per_cu_data);
2966 the_cu->sect_off = sect_off;
2967 the_cu->length = length;
2968 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2969 the_cu->section = section;
2970 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2971 struct dwarf2_per_cu_quick_data);
2972 the_cu->is_dwz = is_dwz;
2973 return the_cu;
2974 }
2975
2976 /* A helper for create_cus_from_index that handles a given list of
2977 CUs. */
2978
2979 static void
2980 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2981 const gdb_byte *cu_list, offset_type n_elements,
2982 struct dwarf2_section_info *section,
2983 int is_dwz)
2984 {
2985 for (offset_type i = 0; i < n_elements; i += 2)
2986 {
2987 gdb_static_assert (sizeof (ULONGEST) >= 8);
2988
2989 sect_offset sect_off
2990 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2991 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2992 cu_list += 2 * 8;
2993
2994 dwarf2_per_cu_data *per_cu
2995 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2996 sect_off, length);
2997 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2998 }
2999 }
3000
3001 /* Read the CU list from the mapped index, and use it to create all
3002 the CU objects for this objfile. */
3003
3004 static void
3005 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3006 const gdb_byte *cu_list, offset_type cu_list_elements,
3007 const gdb_byte *dwz_list, offset_type dwz_elements)
3008 {
3009 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3010 dwarf2_per_objfile->all_comp_units.reserve
3011 ((cu_list_elements + dwz_elements) / 2);
3012
3013 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3014 &dwarf2_per_objfile->info, 0);
3015
3016 if (dwz_elements == 0)
3017 return;
3018
3019 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3020 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3021 &dwz->info, 1);
3022 }
3023
3024 /* Create the signatured type hash table from the index. */
3025
3026 static void
3027 create_signatured_type_table_from_index
3028 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3029 struct dwarf2_section_info *section,
3030 const gdb_byte *bytes,
3031 offset_type elements)
3032 {
3033 struct objfile *objfile = dwarf2_per_objfile->objfile;
3034
3035 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3036 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3037
3038 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3039
3040 for (offset_type i = 0; i < elements; i += 3)
3041 {
3042 struct signatured_type *sig_type;
3043 ULONGEST signature;
3044 void **slot;
3045 cu_offset type_offset_in_tu;
3046
3047 gdb_static_assert (sizeof (ULONGEST) >= 8);
3048 sect_offset sect_off
3049 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3050 type_offset_in_tu
3051 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3052 BFD_ENDIAN_LITTLE);
3053 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3054 bytes += 3 * 8;
3055
3056 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3057 struct signatured_type);
3058 sig_type->signature = signature;
3059 sig_type->type_offset_in_tu = type_offset_in_tu;
3060 sig_type->per_cu.is_debug_types = 1;
3061 sig_type->per_cu.section = section;
3062 sig_type->per_cu.sect_off = sect_off;
3063 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3064 sig_type->per_cu.v.quick
3065 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3066 struct dwarf2_per_cu_quick_data);
3067
3068 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3069 *slot = sig_type;
3070
3071 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3072 }
3073
3074 dwarf2_per_objfile->signatured_types = sig_types_hash;
3075 }
3076
3077 /* Create the signatured type hash table from .debug_names. */
3078
3079 static void
3080 create_signatured_type_table_from_debug_names
3081 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3082 const mapped_debug_names &map,
3083 struct dwarf2_section_info *section,
3084 struct dwarf2_section_info *abbrev_section)
3085 {
3086 struct objfile *objfile = dwarf2_per_objfile->objfile;
3087
3088 dwarf2_read_section (objfile, section);
3089 dwarf2_read_section (objfile, abbrev_section);
3090
3091 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3092 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3093
3094 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3095
3096 for (uint32_t i = 0; i < map.tu_count; ++i)
3097 {
3098 struct signatured_type *sig_type;
3099 void **slot;
3100
3101 sect_offset sect_off
3102 = (sect_offset) (extract_unsigned_integer
3103 (map.tu_table_reordered + i * map.offset_size,
3104 map.offset_size,
3105 map.dwarf5_byte_order));
3106
3107 comp_unit_head cu_header;
3108 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3109 abbrev_section,
3110 section->buffer + to_underlying (sect_off),
3111 rcuh_kind::TYPE);
3112
3113 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3114 struct signatured_type);
3115 sig_type->signature = cu_header.signature;
3116 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3117 sig_type->per_cu.is_debug_types = 1;
3118 sig_type->per_cu.section = section;
3119 sig_type->per_cu.sect_off = sect_off;
3120 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3121 sig_type->per_cu.v.quick
3122 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3123 struct dwarf2_per_cu_quick_data);
3124
3125 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3126 *slot = sig_type;
3127
3128 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3129 }
3130
3131 dwarf2_per_objfile->signatured_types = sig_types_hash;
3132 }
3133
3134 /* Read the address map data from the mapped index, and use it to
3135 populate the objfile's psymtabs_addrmap. */
3136
3137 static void
3138 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3139 struct mapped_index *index)
3140 {
3141 struct objfile *objfile = dwarf2_per_objfile->objfile;
3142 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3143 const gdb_byte *iter, *end;
3144 struct addrmap *mutable_map;
3145 CORE_ADDR baseaddr;
3146
3147 auto_obstack temp_obstack;
3148
3149 mutable_map = addrmap_create_mutable (&temp_obstack);
3150
3151 iter = index->address_table.data ();
3152 end = iter + index->address_table.size ();
3153
3154 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3155
3156 while (iter < end)
3157 {
3158 ULONGEST hi, lo, cu_index;
3159 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3160 iter += 8;
3161 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3162 iter += 8;
3163 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3164 iter += 4;
3165
3166 if (lo > hi)
3167 {
3168 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3169 hex_string (lo), hex_string (hi));
3170 continue;
3171 }
3172
3173 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3174 {
3175 complaint (_(".gdb_index address table has invalid CU number %u"),
3176 (unsigned) cu_index);
3177 continue;
3178 }
3179
3180 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3181 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3182 addrmap_set_empty (mutable_map, lo, hi - 1,
3183 dwarf2_per_objfile->get_cu (cu_index));
3184 }
3185
3186 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3187 &objfile->objfile_obstack);
3188 }
3189
3190 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3191 populate the objfile's psymtabs_addrmap. */
3192
3193 static void
3194 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3195 struct dwarf2_section_info *section)
3196 {
3197 struct objfile *objfile = dwarf2_per_objfile->objfile;
3198 bfd *abfd = objfile->obfd;
3199 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3200 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3201 SECT_OFF_TEXT (objfile));
3202
3203 auto_obstack temp_obstack;
3204 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3205
3206 std::unordered_map<sect_offset,
3207 dwarf2_per_cu_data *,
3208 gdb::hash_enum<sect_offset>>
3209 debug_info_offset_to_per_cu;
3210 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3211 {
3212 const auto insertpair
3213 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3214 if (!insertpair.second)
3215 {
3216 warning (_("Section .debug_aranges in %s has duplicate "
3217 "debug_info_offset %s, ignoring .debug_aranges."),
3218 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3219 return;
3220 }
3221 }
3222
3223 dwarf2_read_section (objfile, section);
3224
3225 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3226
3227 const gdb_byte *addr = section->buffer;
3228
3229 while (addr < section->buffer + section->size)
3230 {
3231 const gdb_byte *const entry_addr = addr;
3232 unsigned int bytes_read;
3233
3234 const LONGEST entry_length = read_initial_length (abfd, addr,
3235 &bytes_read);
3236 addr += bytes_read;
3237
3238 const gdb_byte *const entry_end = addr + entry_length;
3239 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3240 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3241 if (addr + entry_length > section->buffer + section->size)
3242 {
3243 warning (_("Section .debug_aranges in %s entry at offset %zu "
3244 "length %s exceeds section length %s, "
3245 "ignoring .debug_aranges."),
3246 objfile_name (objfile), entry_addr - section->buffer,
3247 plongest (bytes_read + entry_length),
3248 pulongest (section->size));
3249 return;
3250 }
3251
3252 /* The version number. */
3253 const uint16_t version = read_2_bytes (abfd, addr);
3254 addr += 2;
3255 if (version != 2)
3256 {
3257 warning (_("Section .debug_aranges in %s entry at offset %zu "
3258 "has unsupported version %d, ignoring .debug_aranges."),
3259 objfile_name (objfile), entry_addr - section->buffer,
3260 version);
3261 return;
3262 }
3263
3264 const uint64_t debug_info_offset
3265 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3266 addr += offset_size;
3267 const auto per_cu_it
3268 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3269 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3270 {
3271 warning (_("Section .debug_aranges in %s entry at offset %zu "
3272 "debug_info_offset %s does not exists, "
3273 "ignoring .debug_aranges."),
3274 objfile_name (objfile), entry_addr - section->buffer,
3275 pulongest (debug_info_offset));
3276 return;
3277 }
3278 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3279
3280 const uint8_t address_size = *addr++;
3281 if (address_size < 1 || address_size > 8)
3282 {
3283 warning (_("Section .debug_aranges in %s entry at offset %zu "
3284 "address_size %u is invalid, ignoring .debug_aranges."),
3285 objfile_name (objfile), entry_addr - section->buffer,
3286 address_size);
3287 return;
3288 }
3289
3290 const uint8_t segment_selector_size = *addr++;
3291 if (segment_selector_size != 0)
3292 {
3293 warning (_("Section .debug_aranges in %s entry at offset %zu "
3294 "segment_selector_size %u is not supported, "
3295 "ignoring .debug_aranges."),
3296 objfile_name (objfile), entry_addr - section->buffer,
3297 segment_selector_size);
3298 return;
3299 }
3300
3301 /* Must pad to an alignment boundary that is twice the address
3302 size. It is undocumented by the DWARF standard but GCC does
3303 use it. */
3304 for (size_t padding = ((-(addr - section->buffer))
3305 & (2 * address_size - 1));
3306 padding > 0; padding--)
3307 if (*addr++ != 0)
3308 {
3309 warning (_("Section .debug_aranges in %s entry at offset %zu "
3310 "padding is not zero, ignoring .debug_aranges."),
3311 objfile_name (objfile), entry_addr - section->buffer);
3312 return;
3313 }
3314
3315 for (;;)
3316 {
3317 if (addr + 2 * address_size > entry_end)
3318 {
3319 warning (_("Section .debug_aranges in %s entry at offset %zu "
3320 "address list is not properly terminated, "
3321 "ignoring .debug_aranges."),
3322 objfile_name (objfile), entry_addr - section->buffer);
3323 return;
3324 }
3325 ULONGEST start = extract_unsigned_integer (addr, address_size,
3326 dwarf5_byte_order);
3327 addr += address_size;
3328 ULONGEST length = extract_unsigned_integer (addr, address_size,
3329 dwarf5_byte_order);
3330 addr += address_size;
3331 if (start == 0 && length == 0)
3332 break;
3333 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3334 {
3335 /* Symbol was eliminated due to a COMDAT group. */
3336 continue;
3337 }
3338 ULONGEST end = start + length;
3339 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3340 - baseaddr);
3341 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3342 - baseaddr);
3343 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3344 }
3345 }
3346
3347 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3348 &objfile->objfile_obstack);
3349 }
3350
3351 /* Find a slot in the mapped index INDEX for the object named NAME.
3352 If NAME is found, set *VEC_OUT to point to the CU vector in the
3353 constant pool and return true. If NAME cannot be found, return
3354 false. */
3355
3356 static bool
3357 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3358 offset_type **vec_out)
3359 {
3360 offset_type hash;
3361 offset_type slot, step;
3362 int (*cmp) (const char *, const char *);
3363
3364 gdb::unique_xmalloc_ptr<char> without_params;
3365 if (current_language->la_language == language_cplus
3366 || current_language->la_language == language_fortran
3367 || current_language->la_language == language_d)
3368 {
3369 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3370 not contain any. */
3371
3372 if (strchr (name, '(') != NULL)
3373 {
3374 without_params = cp_remove_params (name);
3375
3376 if (without_params != NULL)
3377 name = without_params.get ();
3378 }
3379 }
3380
3381 /* Index version 4 did not support case insensitive searches. But the
3382 indices for case insensitive languages are built in lowercase, therefore
3383 simulate our NAME being searched is also lowercased. */
3384 hash = mapped_index_string_hash ((index->version == 4
3385 && case_sensitivity == case_sensitive_off
3386 ? 5 : index->version),
3387 name);
3388
3389 slot = hash & (index->symbol_table.size () - 1);
3390 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3391 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3392
3393 for (;;)
3394 {
3395 const char *str;
3396
3397 const auto &bucket = index->symbol_table[slot];
3398 if (bucket.name == 0 && bucket.vec == 0)
3399 return false;
3400
3401 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3402 if (!cmp (name, str))
3403 {
3404 *vec_out = (offset_type *) (index->constant_pool
3405 + MAYBE_SWAP (bucket.vec));
3406 return true;
3407 }
3408
3409 slot = (slot + step) & (index->symbol_table.size () - 1);
3410 }
3411 }
3412
3413 /* A helper function that reads the .gdb_index from SECTION and fills
3414 in MAP. FILENAME is the name of the file containing the section;
3415 it is used for error reporting. DEPRECATED_OK is true if it is
3416 ok to use deprecated sections.
3417
3418 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3419 out parameters that are filled in with information about the CU and
3420 TU lists in the section.
3421
3422 Returns 1 if all went well, 0 otherwise. */
3423
3424 static bool
3425 read_gdb_index_from_section (struct objfile *objfile,
3426 const char *filename,
3427 bool deprecated_ok,
3428 struct dwarf2_section_info *section,
3429 struct mapped_index *map,
3430 const gdb_byte **cu_list,
3431 offset_type *cu_list_elements,
3432 const gdb_byte **types_list,
3433 offset_type *types_list_elements)
3434 {
3435 const gdb_byte *addr;
3436 offset_type version;
3437 offset_type *metadata;
3438 int i;
3439
3440 if (dwarf2_section_empty_p (section))
3441 return 0;
3442
3443 /* Older elfutils strip versions could keep the section in the main
3444 executable while splitting it for the separate debug info file. */
3445 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3446 return 0;
3447
3448 dwarf2_read_section (objfile, section);
3449
3450 addr = section->buffer;
3451 /* Version check. */
3452 version = MAYBE_SWAP (*(offset_type *) addr);
3453 /* Versions earlier than 3 emitted every copy of a psymbol. This
3454 causes the index to behave very poorly for certain requests. Version 3
3455 contained incomplete addrmap. So, it seems better to just ignore such
3456 indices. */
3457 if (version < 4)
3458 {
3459 static int warning_printed = 0;
3460 if (!warning_printed)
3461 {
3462 warning (_("Skipping obsolete .gdb_index section in %s."),
3463 filename);
3464 warning_printed = 1;
3465 }
3466 return 0;
3467 }
3468 /* Index version 4 uses a different hash function than index version
3469 5 and later.
3470
3471 Versions earlier than 6 did not emit psymbols for inlined
3472 functions. Using these files will cause GDB not to be able to
3473 set breakpoints on inlined functions by name, so we ignore these
3474 indices unless the user has done
3475 "set use-deprecated-index-sections on". */
3476 if (version < 6 && !deprecated_ok)
3477 {
3478 static int warning_printed = 0;
3479 if (!warning_printed)
3480 {
3481 warning (_("\
3482 Skipping deprecated .gdb_index section in %s.\n\
3483 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3484 to use the section anyway."),
3485 filename);
3486 warning_printed = 1;
3487 }
3488 return 0;
3489 }
3490 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3491 of the TU (for symbols coming from TUs),
3492 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3493 Plus gold-generated indices can have duplicate entries for global symbols,
3494 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3495 These are just performance bugs, and we can't distinguish gdb-generated
3496 indices from gold-generated ones, so issue no warning here. */
3497
3498 /* Indexes with higher version than the one supported by GDB may be no
3499 longer backward compatible. */
3500 if (version > 8)
3501 return 0;
3502
3503 map->version = version;
3504
3505 metadata = (offset_type *) (addr + sizeof (offset_type));
3506
3507 i = 0;
3508 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3509 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3510 / 8);
3511 ++i;
3512
3513 *types_list = addr + MAYBE_SWAP (metadata[i]);
3514 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3515 - MAYBE_SWAP (metadata[i]))
3516 / 8);
3517 ++i;
3518
3519 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3520 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3521 map->address_table
3522 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3523 ++i;
3524
3525 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3526 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3527 map->symbol_table
3528 = gdb::array_view<mapped_index::symbol_table_slot>
3529 ((mapped_index::symbol_table_slot *) symbol_table,
3530 (mapped_index::symbol_table_slot *) symbol_table_end);
3531
3532 ++i;
3533 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3534
3535 return 1;
3536 }
3537
3538 /* Read .gdb_index. If everything went ok, initialize the "quick"
3539 elements of all the CUs and return 1. Otherwise, return 0. */
3540
3541 static int
3542 dwarf2_read_gdb_index (struct dwarf2_per_objfile *dwarf2_per_objfile)
3543 {
3544 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3545 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3546 struct dwz_file *dwz;
3547 struct objfile *objfile = dwarf2_per_objfile->objfile;
3548
3549 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3550 if (!read_gdb_index_from_section (objfile, objfile_name (objfile),
3551 use_deprecated_index_sections,
3552 &dwarf2_per_objfile->gdb_index, map.get (),
3553 &cu_list, &cu_list_elements,
3554 &types_list, &types_list_elements))
3555 return 0;
3556
3557 /* Don't use the index if it's empty. */
3558 if (map->symbol_table.empty ())
3559 return 0;
3560
3561 /* If there is a .dwz file, read it so we can get its CU list as
3562 well. */
3563 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3564 if (dwz != NULL)
3565 {
3566 struct mapped_index dwz_map;
3567 const gdb_byte *dwz_types_ignore;
3568 offset_type dwz_types_elements_ignore;
3569
3570 if (!read_gdb_index_from_section (objfile,
3571 bfd_get_filename (dwz->dwz_bfd), 1,
3572 &dwz->gdb_index, &dwz_map,
3573 &dwz_list, &dwz_list_elements,
3574 &dwz_types_ignore,
3575 &dwz_types_elements_ignore))
3576 {
3577 warning (_("could not read '.gdb_index' section from %s; skipping"),
3578 bfd_get_filename (dwz->dwz_bfd));
3579 return 0;
3580 }
3581 }
3582
3583 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3584 dwz_list, dwz_list_elements);
3585
3586 if (types_list_elements)
3587 {
3588 struct dwarf2_section_info *section;
3589
3590 /* We can only handle a single .debug_types when we have an
3591 index. */
3592 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3593 return 0;
3594
3595 section = VEC_index (dwarf2_section_info_def,
3596 dwarf2_per_objfile->types, 0);
3597
3598 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3599 types_list, types_list_elements);
3600 }
3601
3602 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3603
3604 dwarf2_per_objfile->index_table = std::move (map);
3605 dwarf2_per_objfile->using_index = 1;
3606 dwarf2_per_objfile->quick_file_names_table =
3607 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3608
3609 return 1;
3610 }
3611
3612 /* die_reader_func for dw2_get_file_names. */
3613
3614 static void
3615 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3616 const gdb_byte *info_ptr,
3617 struct die_info *comp_unit_die,
3618 int has_children,
3619 void *data)
3620 {
3621 struct dwarf2_cu *cu = reader->cu;
3622 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3623 struct dwarf2_per_objfile *dwarf2_per_objfile
3624 = cu->per_cu->dwarf2_per_objfile;
3625 struct objfile *objfile = dwarf2_per_objfile->objfile;
3626 struct dwarf2_per_cu_data *lh_cu;
3627 struct attribute *attr;
3628 int i;
3629 void **slot;
3630 struct quick_file_names *qfn;
3631
3632 gdb_assert (! this_cu->is_debug_types);
3633
3634 /* Our callers never want to match partial units -- instead they
3635 will match the enclosing full CU. */
3636 if (comp_unit_die->tag == DW_TAG_partial_unit)
3637 {
3638 this_cu->v.quick->no_file_data = 1;
3639 return;
3640 }
3641
3642 lh_cu = this_cu;
3643 slot = NULL;
3644
3645 line_header_up lh;
3646 sect_offset line_offset {};
3647
3648 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3649 if (attr)
3650 {
3651 struct quick_file_names find_entry;
3652
3653 line_offset = (sect_offset) DW_UNSND (attr);
3654
3655 /* We may have already read in this line header (TU line header sharing).
3656 If we have we're done. */
3657 find_entry.hash.dwo_unit = cu->dwo_unit;
3658 find_entry.hash.line_sect_off = line_offset;
3659 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3660 &find_entry, INSERT);
3661 if (*slot != NULL)
3662 {
3663 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3664 return;
3665 }
3666
3667 lh = dwarf_decode_line_header (line_offset, cu);
3668 }
3669 if (lh == NULL)
3670 {
3671 lh_cu->v.quick->no_file_data = 1;
3672 return;
3673 }
3674
3675 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3676 qfn->hash.dwo_unit = cu->dwo_unit;
3677 qfn->hash.line_sect_off = line_offset;
3678 gdb_assert (slot != NULL);
3679 *slot = qfn;
3680
3681 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3682
3683 qfn->num_file_names = lh->file_names.size ();
3684 qfn->file_names =
3685 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3686 for (i = 0; i < lh->file_names.size (); ++i)
3687 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3688 qfn->real_names = NULL;
3689
3690 lh_cu->v.quick->file_names = qfn;
3691 }
3692
3693 /* A helper for the "quick" functions which attempts to read the line
3694 table for THIS_CU. */
3695
3696 static struct quick_file_names *
3697 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3698 {
3699 /* This should never be called for TUs. */
3700 gdb_assert (! this_cu->is_debug_types);
3701 /* Nor type unit groups. */
3702 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3703
3704 if (this_cu->v.quick->file_names != NULL)
3705 return this_cu->v.quick->file_names;
3706 /* If we know there is no line data, no point in looking again. */
3707 if (this_cu->v.quick->no_file_data)
3708 return NULL;
3709
3710 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3711
3712 if (this_cu->v.quick->no_file_data)
3713 return NULL;
3714 return this_cu->v.quick->file_names;
3715 }
3716
3717 /* A helper for the "quick" functions which computes and caches the
3718 real path for a given file name from the line table. */
3719
3720 static const char *
3721 dw2_get_real_path (struct objfile *objfile,
3722 struct quick_file_names *qfn, int index)
3723 {
3724 if (qfn->real_names == NULL)
3725 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3726 qfn->num_file_names, const char *);
3727
3728 if (qfn->real_names[index] == NULL)
3729 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3730
3731 return qfn->real_names[index];
3732 }
3733
3734 static struct symtab *
3735 dw2_find_last_source_symtab (struct objfile *objfile)
3736 {
3737 struct dwarf2_per_objfile *dwarf2_per_objfile
3738 = get_dwarf2_per_objfile (objfile);
3739 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3740 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3741
3742 if (cust == NULL)
3743 return NULL;
3744
3745 return compunit_primary_filetab (cust);
3746 }
3747
3748 /* Traversal function for dw2_forget_cached_source_info. */
3749
3750 static int
3751 dw2_free_cached_file_names (void **slot, void *info)
3752 {
3753 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3754
3755 if (file_data->real_names)
3756 {
3757 int i;
3758
3759 for (i = 0; i < file_data->num_file_names; ++i)
3760 {
3761 xfree ((void*) file_data->real_names[i]);
3762 file_data->real_names[i] = NULL;
3763 }
3764 }
3765
3766 return 1;
3767 }
3768
3769 static void
3770 dw2_forget_cached_source_info (struct objfile *objfile)
3771 {
3772 struct dwarf2_per_objfile *dwarf2_per_objfile
3773 = get_dwarf2_per_objfile (objfile);
3774
3775 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3776 dw2_free_cached_file_names, NULL);
3777 }
3778
3779 /* Helper function for dw2_map_symtabs_matching_filename that expands
3780 the symtabs and calls the iterator. */
3781
3782 static int
3783 dw2_map_expand_apply (struct objfile *objfile,
3784 struct dwarf2_per_cu_data *per_cu,
3785 const char *name, const char *real_path,
3786 gdb::function_view<bool (symtab *)> callback)
3787 {
3788 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3789
3790 /* Don't visit already-expanded CUs. */
3791 if (per_cu->v.quick->compunit_symtab)
3792 return 0;
3793
3794 /* This may expand more than one symtab, and we want to iterate over
3795 all of them. */
3796 dw2_instantiate_symtab (per_cu, false);
3797
3798 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3799 last_made, callback);
3800 }
3801
3802 /* Implementation of the map_symtabs_matching_filename method. */
3803
3804 static bool
3805 dw2_map_symtabs_matching_filename
3806 (struct objfile *objfile, const char *name, const char *real_path,
3807 gdb::function_view<bool (symtab *)> callback)
3808 {
3809 const char *name_basename = lbasename (name);
3810 struct dwarf2_per_objfile *dwarf2_per_objfile
3811 = get_dwarf2_per_objfile (objfile);
3812
3813 /* The rule is CUs specify all the files, including those used by
3814 any TU, so there's no need to scan TUs here. */
3815
3816 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3817 {
3818 /* We only need to look at symtabs not already expanded. */
3819 if (per_cu->v.quick->compunit_symtab)
3820 continue;
3821
3822 quick_file_names *file_data = dw2_get_file_names (per_cu);
3823 if (file_data == NULL)
3824 continue;
3825
3826 for (int j = 0; j < file_data->num_file_names; ++j)
3827 {
3828 const char *this_name = file_data->file_names[j];
3829 const char *this_real_name;
3830
3831 if (compare_filenames_for_search (this_name, name))
3832 {
3833 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3834 callback))
3835 return true;
3836 continue;
3837 }
3838
3839 /* Before we invoke realpath, which can get expensive when many
3840 files are involved, do a quick comparison of the basenames. */
3841 if (! basenames_may_differ
3842 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3843 continue;
3844
3845 this_real_name = dw2_get_real_path (objfile, file_data, j);
3846 if (compare_filenames_for_search (this_real_name, name))
3847 {
3848 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3849 callback))
3850 return true;
3851 continue;
3852 }
3853
3854 if (real_path != NULL)
3855 {
3856 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3857 gdb_assert (IS_ABSOLUTE_PATH (name));
3858 if (this_real_name != NULL
3859 && FILENAME_CMP (real_path, this_real_name) == 0)
3860 {
3861 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3862 callback))
3863 return true;
3864 continue;
3865 }
3866 }
3867 }
3868 }
3869
3870 return false;
3871 }
3872
3873 /* Struct used to manage iterating over all CUs looking for a symbol. */
3874
3875 struct dw2_symtab_iterator
3876 {
3877 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3878 struct dwarf2_per_objfile *dwarf2_per_objfile;
3879 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3880 int want_specific_block;
3881 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3882 Unused if !WANT_SPECIFIC_BLOCK. */
3883 int block_index;
3884 /* The kind of symbol we're looking for. */
3885 domain_enum domain;
3886 /* The list of CUs from the index entry of the symbol,
3887 or NULL if not found. */
3888 offset_type *vec;
3889 /* The next element in VEC to look at. */
3890 int next;
3891 /* The number of elements in VEC, or zero if there is no match. */
3892 int length;
3893 /* Have we seen a global version of the symbol?
3894 If so we can ignore all further global instances.
3895 This is to work around gold/15646, inefficient gold-generated
3896 indices. */
3897 int global_seen;
3898 };
3899
3900 /* Initialize the index symtab iterator ITER.
3901 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3902 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3903
3904 static void
3905 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3906 struct dwarf2_per_objfile *dwarf2_per_objfile,
3907 int want_specific_block,
3908 int block_index,
3909 domain_enum domain,
3910 const char *name)
3911 {
3912 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3913 iter->want_specific_block = want_specific_block;
3914 iter->block_index = block_index;
3915 iter->domain = domain;
3916 iter->next = 0;
3917 iter->global_seen = 0;
3918
3919 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3920
3921 /* index is NULL if OBJF_READNOW. */
3922 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3923 iter->length = MAYBE_SWAP (*iter->vec);
3924 else
3925 {
3926 iter->vec = NULL;
3927 iter->length = 0;
3928 }
3929 }
3930
3931 /* Return the next matching CU or NULL if there are no more. */
3932
3933 static struct dwarf2_per_cu_data *
3934 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3935 {
3936 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3937
3938 for ( ; iter->next < iter->length; ++iter->next)
3939 {
3940 offset_type cu_index_and_attrs =
3941 MAYBE_SWAP (iter->vec[iter->next + 1]);
3942 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3943 int want_static = iter->block_index != GLOBAL_BLOCK;
3944 /* This value is only valid for index versions >= 7. */
3945 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3946 gdb_index_symbol_kind symbol_kind =
3947 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3948 /* Only check the symbol attributes if they're present.
3949 Indices prior to version 7 don't record them,
3950 and indices >= 7 may elide them for certain symbols
3951 (gold does this). */
3952 int attrs_valid =
3953 (dwarf2_per_objfile->index_table->version >= 7
3954 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3955
3956 /* Don't crash on bad data. */
3957 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3958 + dwarf2_per_objfile->all_type_units.size ()))
3959 {
3960 complaint (_(".gdb_index entry has bad CU index"
3961 " [in module %s]"),
3962 objfile_name (dwarf2_per_objfile->objfile));
3963 continue;
3964 }
3965
3966 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3967
3968 /* Skip if already read in. */
3969 if (per_cu->v.quick->compunit_symtab)
3970 continue;
3971
3972 /* Check static vs global. */
3973 if (attrs_valid)
3974 {
3975 if (iter->want_specific_block
3976 && want_static != is_static)
3977 continue;
3978 /* Work around gold/15646. */
3979 if (!is_static && iter->global_seen)
3980 continue;
3981 if (!is_static)
3982 iter->global_seen = 1;
3983 }
3984
3985 /* Only check the symbol's kind if it has one. */
3986 if (attrs_valid)
3987 {
3988 switch (iter->domain)
3989 {
3990 case VAR_DOMAIN:
3991 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3992 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3993 /* Some types are also in VAR_DOMAIN. */
3994 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3995 continue;
3996 break;
3997 case STRUCT_DOMAIN:
3998 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3999 continue;
4000 break;
4001 case LABEL_DOMAIN:
4002 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4003 continue;
4004 break;
4005 default:
4006 break;
4007 }
4008 }
4009
4010 ++iter->next;
4011 return per_cu;
4012 }
4013
4014 return NULL;
4015 }
4016
4017 static struct compunit_symtab *
4018 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4019 const char *name, domain_enum domain)
4020 {
4021 struct compunit_symtab *stab_best = NULL;
4022 struct dwarf2_per_objfile *dwarf2_per_objfile
4023 = get_dwarf2_per_objfile (objfile);
4024
4025 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4026
4027 struct dw2_symtab_iterator iter;
4028 struct dwarf2_per_cu_data *per_cu;
4029
4030 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4031
4032 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4033 {
4034 struct symbol *sym, *with_opaque = NULL;
4035 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4036 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4037 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4038
4039 sym = block_find_symbol (block, name, domain,
4040 block_find_non_opaque_type_preferred,
4041 &with_opaque);
4042
4043 /* Some caution must be observed with overloaded functions
4044 and methods, since the index will not contain any overload
4045 information (but NAME might contain it). */
4046
4047 if (sym != NULL
4048 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4049 return stab;
4050 if (with_opaque != NULL
4051 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4052 stab_best = stab;
4053
4054 /* Keep looking through other CUs. */
4055 }
4056
4057 return stab_best;
4058 }
4059
4060 static void
4061 dw2_print_stats (struct objfile *objfile)
4062 {
4063 struct dwarf2_per_objfile *dwarf2_per_objfile
4064 = get_dwarf2_per_objfile (objfile);
4065 int total = (dwarf2_per_objfile->all_comp_units.size ()
4066 + dwarf2_per_objfile->all_type_units.size ());
4067 int count = 0;
4068
4069 for (int i = 0; i < total; ++i)
4070 {
4071 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4072
4073 if (!per_cu->v.quick->compunit_symtab)
4074 ++count;
4075 }
4076 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4077 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4078 }
4079
4080 /* This dumps minimal information about the index.
4081 It is called via "mt print objfiles".
4082 One use is to verify .gdb_index has been loaded by the
4083 gdb.dwarf2/gdb-index.exp testcase. */
4084
4085 static void
4086 dw2_dump (struct objfile *objfile)
4087 {
4088 struct dwarf2_per_objfile *dwarf2_per_objfile
4089 = get_dwarf2_per_objfile (objfile);
4090
4091 gdb_assert (dwarf2_per_objfile->using_index);
4092 printf_filtered (".gdb_index:");
4093 if (dwarf2_per_objfile->index_table != NULL)
4094 {
4095 printf_filtered (" version %d\n",
4096 dwarf2_per_objfile->index_table->version);
4097 }
4098 else
4099 printf_filtered (" faked for \"readnow\"\n");
4100 printf_filtered ("\n");
4101 }
4102
4103 static void
4104 dw2_expand_symtabs_for_function (struct objfile *objfile,
4105 const char *func_name)
4106 {
4107 struct dwarf2_per_objfile *dwarf2_per_objfile
4108 = get_dwarf2_per_objfile (objfile);
4109
4110 struct dw2_symtab_iterator iter;
4111 struct dwarf2_per_cu_data *per_cu;
4112
4113 /* Note: It doesn't matter what we pass for block_index here. */
4114 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4115 func_name);
4116
4117 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4118 dw2_instantiate_symtab (per_cu, false);
4119
4120 }
4121
4122 static void
4123 dw2_expand_all_symtabs (struct objfile *objfile)
4124 {
4125 struct dwarf2_per_objfile *dwarf2_per_objfile
4126 = get_dwarf2_per_objfile (objfile);
4127 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4128 + dwarf2_per_objfile->all_type_units.size ());
4129
4130 for (int i = 0; i < total_units; ++i)
4131 {
4132 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4133
4134 /* We don't want to directly expand a partial CU, because if we
4135 read it with the wrong language, then assertion failures can
4136 be triggered later on. See PR symtab/23010. So, tell
4137 dw2_instantiate_symtab to skip partial CUs -- any important
4138 partial CU will be read via DW_TAG_imported_unit anyway. */
4139 dw2_instantiate_symtab (per_cu, true);
4140 }
4141 }
4142
4143 static void
4144 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4145 const char *fullname)
4146 {
4147 struct dwarf2_per_objfile *dwarf2_per_objfile
4148 = get_dwarf2_per_objfile (objfile);
4149
4150 /* We don't need to consider type units here.
4151 This is only called for examining code, e.g. expand_line_sal.
4152 There can be an order of magnitude (or more) more type units
4153 than comp units, and we avoid them if we can. */
4154
4155 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4156 {
4157 /* We only need to look at symtabs not already expanded. */
4158 if (per_cu->v.quick->compunit_symtab)
4159 continue;
4160
4161 quick_file_names *file_data = dw2_get_file_names (per_cu);
4162 if (file_data == NULL)
4163 continue;
4164
4165 for (int j = 0; j < file_data->num_file_names; ++j)
4166 {
4167 const char *this_fullname = file_data->file_names[j];
4168
4169 if (filename_cmp (this_fullname, fullname) == 0)
4170 {
4171 dw2_instantiate_symtab (per_cu, false);
4172 break;
4173 }
4174 }
4175 }
4176 }
4177
4178 static void
4179 dw2_map_matching_symbols (struct objfile *objfile,
4180 const char * name, domain_enum domain,
4181 int global,
4182 int (*callback) (struct block *,
4183 struct symbol *, void *),
4184 void *data, symbol_name_match_type match,
4185 symbol_compare_ftype *ordered_compare)
4186 {
4187 /* Currently unimplemented; used for Ada. The function can be called if the
4188 current language is Ada for a non-Ada objfile using GNU index. As Ada
4189 does not look for non-Ada symbols this function should just return. */
4190 }
4191
4192 /* Symbol name matcher for .gdb_index names.
4193
4194 Symbol names in .gdb_index have a few particularities:
4195
4196 - There's no indication of which is the language of each symbol.
4197
4198 Since each language has its own symbol name matching algorithm,
4199 and we don't know which language is the right one, we must match
4200 each symbol against all languages. This would be a potential
4201 performance problem if it were not mitigated by the
4202 mapped_index::name_components lookup table, which significantly
4203 reduces the number of times we need to call into this matcher,
4204 making it a non-issue.
4205
4206 - Symbol names in the index have no overload (parameter)
4207 information. I.e., in C++, "foo(int)" and "foo(long)" both
4208 appear as "foo" in the index, for example.
4209
4210 This means that the lookup names passed to the symbol name
4211 matcher functions must have no parameter information either
4212 because (e.g.) symbol search name "foo" does not match
4213 lookup-name "foo(int)" [while swapping search name for lookup
4214 name would match].
4215 */
4216 class gdb_index_symbol_name_matcher
4217 {
4218 public:
4219 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4220 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4221
4222 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4223 Returns true if any matcher matches. */
4224 bool matches (const char *symbol_name);
4225
4226 private:
4227 /* A reference to the lookup name we're matching against. */
4228 const lookup_name_info &m_lookup_name;
4229
4230 /* A vector holding all the different symbol name matchers, for all
4231 languages. */
4232 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4233 };
4234
4235 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4236 (const lookup_name_info &lookup_name)
4237 : m_lookup_name (lookup_name)
4238 {
4239 /* Prepare the vector of comparison functions upfront, to avoid
4240 doing the same work for each symbol. Care is taken to avoid
4241 matching with the same matcher more than once if/when multiple
4242 languages use the same matcher function. */
4243 auto &matchers = m_symbol_name_matcher_funcs;
4244 matchers.reserve (nr_languages);
4245
4246 matchers.push_back (default_symbol_name_matcher);
4247
4248 for (int i = 0; i < nr_languages; i++)
4249 {
4250 const language_defn *lang = language_def ((enum language) i);
4251 symbol_name_matcher_ftype *name_matcher
4252 = get_symbol_name_matcher (lang, m_lookup_name);
4253
4254 /* Don't insert the same comparison routine more than once.
4255 Note that we do this linear walk instead of a seemingly
4256 cheaper sorted insert, or use a std::set or something like
4257 that, because relative order of function addresses is not
4258 stable. This is not a problem in practice because the number
4259 of supported languages is low, and the cost here is tiny
4260 compared to the number of searches we'll do afterwards using
4261 this object. */
4262 if (name_matcher != default_symbol_name_matcher
4263 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4264 == matchers.end ()))
4265 matchers.push_back (name_matcher);
4266 }
4267 }
4268
4269 bool
4270 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4271 {
4272 for (auto matches_name : m_symbol_name_matcher_funcs)
4273 if (matches_name (symbol_name, m_lookup_name, NULL))
4274 return true;
4275
4276 return false;
4277 }
4278
4279 /* Starting from a search name, return the string that finds the upper
4280 bound of all strings that start with SEARCH_NAME in a sorted name
4281 list. Returns the empty string to indicate that the upper bound is
4282 the end of the list. */
4283
4284 static std::string
4285 make_sort_after_prefix_name (const char *search_name)
4286 {
4287 /* When looking to complete "func", we find the upper bound of all
4288 symbols that start with "func" by looking for where we'd insert
4289 the closest string that would follow "func" in lexicographical
4290 order. Usually, that's "func"-with-last-character-incremented,
4291 i.e. "fund". Mind non-ASCII characters, though. Usually those
4292 will be UTF-8 multi-byte sequences, but we can't be certain.
4293 Especially mind the 0xff character, which is a valid character in
4294 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4295 rule out compilers allowing it in identifiers. Note that
4296 conveniently, strcmp/strcasecmp are specified to compare
4297 characters interpreted as unsigned char. So what we do is treat
4298 the whole string as a base 256 number composed of a sequence of
4299 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4300 to 0, and carries 1 to the following more-significant position.
4301 If the very first character in SEARCH_NAME ends up incremented
4302 and carries/overflows, then the upper bound is the end of the
4303 list. The string after the empty string is also the empty
4304 string.
4305
4306 Some examples of this operation:
4307
4308 SEARCH_NAME => "+1" RESULT
4309
4310 "abc" => "abd"
4311 "ab\xff" => "ac"
4312 "\xff" "a" "\xff" => "\xff" "b"
4313 "\xff" => ""
4314 "\xff\xff" => ""
4315 "" => ""
4316
4317 Then, with these symbols for example:
4318
4319 func
4320 func1
4321 fund
4322
4323 completing "func" looks for symbols between "func" and
4324 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4325 which finds "func" and "func1", but not "fund".
4326
4327 And with:
4328
4329 funcÿ (Latin1 'ÿ' [0xff])
4330 funcÿ1
4331 fund
4332
4333 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4334 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4335
4336 And with:
4337
4338 ÿÿ (Latin1 'ÿ' [0xff])
4339 ÿÿ1
4340
4341 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4342 the end of the list.
4343 */
4344 std::string after = search_name;
4345 while (!after.empty () && (unsigned char) after.back () == 0xff)
4346 after.pop_back ();
4347 if (!after.empty ())
4348 after.back () = (unsigned char) after.back () + 1;
4349 return after;
4350 }
4351
4352 /* See declaration. */
4353
4354 std::pair<std::vector<name_component>::const_iterator,
4355 std::vector<name_component>::const_iterator>
4356 mapped_index_base::find_name_components_bounds
4357 (const lookup_name_info &lookup_name_without_params) const
4358 {
4359 auto *name_cmp
4360 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4361
4362 const char *cplus
4363 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4364
4365 /* Comparison function object for lower_bound that matches against a
4366 given symbol name. */
4367 auto lookup_compare_lower = [&] (const name_component &elem,
4368 const char *name)
4369 {
4370 const char *elem_qualified = this->symbol_name_at (elem.idx);
4371 const char *elem_name = elem_qualified + elem.name_offset;
4372 return name_cmp (elem_name, name) < 0;
4373 };
4374
4375 /* Comparison function object for upper_bound that matches against a
4376 given symbol name. */
4377 auto lookup_compare_upper = [&] (const char *name,
4378 const name_component &elem)
4379 {
4380 const char *elem_qualified = this->symbol_name_at (elem.idx);
4381 const char *elem_name = elem_qualified + elem.name_offset;
4382 return name_cmp (name, elem_name) < 0;
4383 };
4384
4385 auto begin = this->name_components.begin ();
4386 auto end = this->name_components.end ();
4387
4388 /* Find the lower bound. */
4389 auto lower = [&] ()
4390 {
4391 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4392 return begin;
4393 else
4394 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4395 } ();
4396
4397 /* Find the upper bound. */
4398 auto upper = [&] ()
4399 {
4400 if (lookup_name_without_params.completion_mode ())
4401 {
4402 /* In completion mode, we want UPPER to point past all
4403 symbols names that have the same prefix. I.e., with
4404 these symbols, and completing "func":
4405
4406 function << lower bound
4407 function1
4408 other_function << upper bound
4409
4410 We find the upper bound by looking for the insertion
4411 point of "func"-with-last-character-incremented,
4412 i.e. "fund". */
4413 std::string after = make_sort_after_prefix_name (cplus);
4414 if (after.empty ())
4415 return end;
4416 return std::lower_bound (lower, end, after.c_str (),
4417 lookup_compare_lower);
4418 }
4419 else
4420 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4421 } ();
4422
4423 return {lower, upper};
4424 }
4425
4426 /* See declaration. */
4427
4428 void
4429 mapped_index_base::build_name_components ()
4430 {
4431 if (!this->name_components.empty ())
4432 return;
4433
4434 this->name_components_casing = case_sensitivity;
4435 auto *name_cmp
4436 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4437
4438 /* The code below only knows how to break apart components of C++
4439 symbol names (and other languages that use '::' as
4440 namespace/module separator). If we add support for wild matching
4441 to some language that uses some other operator (E.g., Ada, Go and
4442 D use '.'), then we'll need to try splitting the symbol name
4443 according to that language too. Note that Ada does support wild
4444 matching, but doesn't currently support .gdb_index. */
4445 auto count = this->symbol_name_count ();
4446 for (offset_type idx = 0; idx < count; idx++)
4447 {
4448 if (this->symbol_name_slot_invalid (idx))
4449 continue;
4450
4451 const char *name = this->symbol_name_at (idx);
4452
4453 /* Add each name component to the name component table. */
4454 unsigned int previous_len = 0;
4455 for (unsigned int current_len = cp_find_first_component (name);
4456 name[current_len] != '\0';
4457 current_len += cp_find_first_component (name + current_len))
4458 {
4459 gdb_assert (name[current_len] == ':');
4460 this->name_components.push_back ({previous_len, idx});
4461 /* Skip the '::'. */
4462 current_len += 2;
4463 previous_len = current_len;
4464 }
4465 this->name_components.push_back ({previous_len, idx});
4466 }
4467
4468 /* Sort name_components elements by name. */
4469 auto name_comp_compare = [&] (const name_component &left,
4470 const name_component &right)
4471 {
4472 const char *left_qualified = this->symbol_name_at (left.idx);
4473 const char *right_qualified = this->symbol_name_at (right.idx);
4474
4475 const char *left_name = left_qualified + left.name_offset;
4476 const char *right_name = right_qualified + right.name_offset;
4477
4478 return name_cmp (left_name, right_name) < 0;
4479 };
4480
4481 std::sort (this->name_components.begin (),
4482 this->name_components.end (),
4483 name_comp_compare);
4484 }
4485
4486 /* Helper for dw2_expand_symtabs_matching that works with a
4487 mapped_index_base instead of the containing objfile. This is split
4488 to a separate function in order to be able to unit test the
4489 name_components matching using a mock mapped_index_base. For each
4490 symbol name that matches, calls MATCH_CALLBACK, passing it the
4491 symbol's index in the mapped_index_base symbol table. */
4492
4493 static void
4494 dw2_expand_symtabs_matching_symbol
4495 (mapped_index_base &index,
4496 const lookup_name_info &lookup_name_in,
4497 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4498 enum search_domain kind,
4499 gdb::function_view<void (offset_type)> match_callback)
4500 {
4501 lookup_name_info lookup_name_without_params
4502 = lookup_name_in.make_ignore_params ();
4503 gdb_index_symbol_name_matcher lookup_name_matcher
4504 (lookup_name_without_params);
4505
4506 /* Build the symbol name component sorted vector, if we haven't
4507 yet. */
4508 index.build_name_components ();
4509
4510 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4511
4512 /* Now for each symbol name in range, check to see if we have a name
4513 match, and if so, call the MATCH_CALLBACK callback. */
4514
4515 /* The same symbol may appear more than once in the range though.
4516 E.g., if we're looking for symbols that complete "w", and we have
4517 a symbol named "w1::w2", we'll find the two name components for
4518 that same symbol in the range. To be sure we only call the
4519 callback once per symbol, we first collect the symbol name
4520 indexes that matched in a temporary vector and ignore
4521 duplicates. */
4522 std::vector<offset_type> matches;
4523 matches.reserve (std::distance (bounds.first, bounds.second));
4524
4525 for (; bounds.first != bounds.second; ++bounds.first)
4526 {
4527 const char *qualified = index.symbol_name_at (bounds.first->idx);
4528
4529 if (!lookup_name_matcher.matches (qualified)
4530 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4531 continue;
4532
4533 matches.push_back (bounds.first->idx);
4534 }
4535
4536 std::sort (matches.begin (), matches.end ());
4537
4538 /* Finally call the callback, once per match. */
4539 ULONGEST prev = -1;
4540 for (offset_type idx : matches)
4541 {
4542 if (prev != idx)
4543 {
4544 match_callback (idx);
4545 prev = idx;
4546 }
4547 }
4548
4549 /* Above we use a type wider than idx's for 'prev', since 0 and
4550 (offset_type)-1 are both possible values. */
4551 static_assert (sizeof (prev) > sizeof (offset_type), "");
4552 }
4553
4554 #if GDB_SELF_TEST
4555
4556 namespace selftests { namespace dw2_expand_symtabs_matching {
4557
4558 /* A mock .gdb_index/.debug_names-like name index table, enough to
4559 exercise dw2_expand_symtabs_matching_symbol, which works with the
4560 mapped_index_base interface. Builds an index from the symbol list
4561 passed as parameter to the constructor. */
4562 class mock_mapped_index : public mapped_index_base
4563 {
4564 public:
4565 mock_mapped_index (gdb::array_view<const char *> symbols)
4566 : m_symbol_table (symbols)
4567 {}
4568
4569 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4570
4571 /* Return the number of names in the symbol table. */
4572 size_t symbol_name_count () const override
4573 {
4574 return m_symbol_table.size ();
4575 }
4576
4577 /* Get the name of the symbol at IDX in the symbol table. */
4578 const char *symbol_name_at (offset_type idx) const override
4579 {
4580 return m_symbol_table[idx];
4581 }
4582
4583 private:
4584 gdb::array_view<const char *> m_symbol_table;
4585 };
4586
4587 /* Convenience function that converts a NULL pointer to a "<null>"
4588 string, to pass to print routines. */
4589
4590 static const char *
4591 string_or_null (const char *str)
4592 {
4593 return str != NULL ? str : "<null>";
4594 }
4595
4596 /* Check if a lookup_name_info built from
4597 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4598 index. EXPECTED_LIST is the list of expected matches, in expected
4599 matching order. If no match expected, then an empty list is
4600 specified. Returns true on success. On failure prints a warning
4601 indicating the file:line that failed, and returns false. */
4602
4603 static bool
4604 check_match (const char *file, int line,
4605 mock_mapped_index &mock_index,
4606 const char *name, symbol_name_match_type match_type,
4607 bool completion_mode,
4608 std::initializer_list<const char *> expected_list)
4609 {
4610 lookup_name_info lookup_name (name, match_type, completion_mode);
4611
4612 bool matched = true;
4613
4614 auto mismatch = [&] (const char *expected_str,
4615 const char *got)
4616 {
4617 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4618 "expected=\"%s\", got=\"%s\"\n"),
4619 file, line,
4620 (match_type == symbol_name_match_type::FULL
4621 ? "FULL" : "WILD"),
4622 name, string_or_null (expected_str), string_or_null (got));
4623 matched = false;
4624 };
4625
4626 auto expected_it = expected_list.begin ();
4627 auto expected_end = expected_list.end ();
4628
4629 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4630 NULL, ALL_DOMAIN,
4631 [&] (offset_type idx)
4632 {
4633 const char *matched_name = mock_index.symbol_name_at (idx);
4634 const char *expected_str
4635 = expected_it == expected_end ? NULL : *expected_it++;
4636
4637 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4638 mismatch (expected_str, matched_name);
4639 });
4640
4641 const char *expected_str
4642 = expected_it == expected_end ? NULL : *expected_it++;
4643 if (expected_str != NULL)
4644 mismatch (expected_str, NULL);
4645
4646 return matched;
4647 }
4648
4649 /* The symbols added to the mock mapped_index for testing (in
4650 canonical form). */
4651 static const char *test_symbols[] = {
4652 "function",
4653 "std::bar",
4654 "std::zfunction",
4655 "std::zfunction2",
4656 "w1::w2",
4657 "ns::foo<char*>",
4658 "ns::foo<int>",
4659 "ns::foo<long>",
4660 "ns2::tmpl<int>::foo2",
4661 "(anonymous namespace)::A::B::C",
4662
4663 /* These are used to check that the increment-last-char in the
4664 matching algorithm for completion doesn't match "t1_fund" when
4665 completing "t1_func". */
4666 "t1_func",
4667 "t1_func1",
4668 "t1_fund",
4669 "t1_fund1",
4670
4671 /* A UTF-8 name with multi-byte sequences to make sure that
4672 cp-name-parser understands this as a single identifier ("função"
4673 is "function" in PT). */
4674 u8"u8função",
4675
4676 /* \377 (0xff) is Latin1 'ÿ'. */
4677 "yfunc\377",
4678
4679 /* \377 (0xff) is Latin1 'ÿ'. */
4680 "\377",
4681 "\377\377123",
4682
4683 /* A name with all sorts of complications. Starts with "z" to make
4684 it easier for the completion tests below. */
4685 #define Z_SYM_NAME \
4686 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4687 "::tuple<(anonymous namespace)::ui*, " \
4688 "std::default_delete<(anonymous namespace)::ui>, void>"
4689
4690 Z_SYM_NAME
4691 };
4692
4693 /* Returns true if the mapped_index_base::find_name_component_bounds
4694 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4695 in completion mode. */
4696
4697 static bool
4698 check_find_bounds_finds (mapped_index_base &index,
4699 const char *search_name,
4700 gdb::array_view<const char *> expected_syms)
4701 {
4702 lookup_name_info lookup_name (search_name,
4703 symbol_name_match_type::FULL, true);
4704
4705 auto bounds = index.find_name_components_bounds (lookup_name);
4706
4707 size_t distance = std::distance (bounds.first, bounds.second);
4708 if (distance != expected_syms.size ())
4709 return false;
4710
4711 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4712 {
4713 auto nc_elem = bounds.first + exp_elem;
4714 const char *qualified = index.symbol_name_at (nc_elem->idx);
4715 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4716 return false;
4717 }
4718
4719 return true;
4720 }
4721
4722 /* Test the lower-level mapped_index::find_name_component_bounds
4723 method. */
4724
4725 static void
4726 test_mapped_index_find_name_component_bounds ()
4727 {
4728 mock_mapped_index mock_index (test_symbols);
4729
4730 mock_index.build_name_components ();
4731
4732 /* Test the lower-level mapped_index::find_name_component_bounds
4733 method in completion mode. */
4734 {
4735 static const char *expected_syms[] = {
4736 "t1_func",
4737 "t1_func1",
4738 };
4739
4740 SELF_CHECK (check_find_bounds_finds (mock_index,
4741 "t1_func", expected_syms));
4742 }
4743
4744 /* Check that the increment-last-char in the name matching algorithm
4745 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4746 {
4747 static const char *expected_syms1[] = {
4748 "\377",
4749 "\377\377123",
4750 };
4751 SELF_CHECK (check_find_bounds_finds (mock_index,
4752 "\377", expected_syms1));
4753
4754 static const char *expected_syms2[] = {
4755 "\377\377123",
4756 };
4757 SELF_CHECK (check_find_bounds_finds (mock_index,
4758 "\377\377", expected_syms2));
4759 }
4760 }
4761
4762 /* Test dw2_expand_symtabs_matching_symbol. */
4763
4764 static void
4765 test_dw2_expand_symtabs_matching_symbol ()
4766 {
4767 mock_mapped_index mock_index (test_symbols);
4768
4769 /* We let all tests run until the end even if some fails, for debug
4770 convenience. */
4771 bool any_mismatch = false;
4772
4773 /* Create the expected symbols list (an initializer_list). Needed
4774 because lists have commas, and we need to pass them to CHECK,
4775 which is a macro. */
4776 #define EXPECT(...) { __VA_ARGS__ }
4777
4778 /* Wrapper for check_match that passes down the current
4779 __FILE__/__LINE__. */
4780 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4781 any_mismatch |= !check_match (__FILE__, __LINE__, \
4782 mock_index, \
4783 NAME, MATCH_TYPE, COMPLETION_MODE, \
4784 EXPECTED_LIST)
4785
4786 /* Identity checks. */
4787 for (const char *sym : test_symbols)
4788 {
4789 /* Should be able to match all existing symbols. */
4790 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4791 EXPECT (sym));
4792
4793 /* Should be able to match all existing symbols with
4794 parameters. */
4795 std::string with_params = std::string (sym) + "(int)";
4796 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4797 EXPECT (sym));
4798
4799 /* Should be able to match all existing symbols with
4800 parameters and qualifiers. */
4801 with_params = std::string (sym) + " ( int ) const";
4802 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4803 EXPECT (sym));
4804
4805 /* This should really find sym, but cp-name-parser.y doesn't
4806 know about lvalue/rvalue qualifiers yet. */
4807 with_params = std::string (sym) + " ( int ) &&";
4808 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4809 {});
4810 }
4811
4812 /* Check that the name matching algorithm for completion doesn't get
4813 confused with Latin1 'ÿ' / 0xff. */
4814 {
4815 static const char str[] = "\377";
4816 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4817 EXPECT ("\377", "\377\377123"));
4818 }
4819
4820 /* Check that the increment-last-char in the matching algorithm for
4821 completion doesn't match "t1_fund" when completing "t1_func". */
4822 {
4823 static const char str[] = "t1_func";
4824 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4825 EXPECT ("t1_func", "t1_func1"));
4826 }
4827
4828 /* Check that completion mode works at each prefix of the expected
4829 symbol name. */
4830 {
4831 static const char str[] = "function(int)";
4832 size_t len = strlen (str);
4833 std::string lookup;
4834
4835 for (size_t i = 1; i < len; i++)
4836 {
4837 lookup.assign (str, i);
4838 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4839 EXPECT ("function"));
4840 }
4841 }
4842
4843 /* While "w" is a prefix of both components, the match function
4844 should still only be called once. */
4845 {
4846 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4847 EXPECT ("w1::w2"));
4848 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4849 EXPECT ("w1::w2"));
4850 }
4851
4852 /* Same, with a "complicated" symbol. */
4853 {
4854 static const char str[] = Z_SYM_NAME;
4855 size_t len = strlen (str);
4856 std::string lookup;
4857
4858 for (size_t i = 1; i < len; i++)
4859 {
4860 lookup.assign (str, i);
4861 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4862 EXPECT (Z_SYM_NAME));
4863 }
4864 }
4865
4866 /* In FULL mode, an incomplete symbol doesn't match. */
4867 {
4868 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4869 {});
4870 }
4871
4872 /* A complete symbol with parameters matches any overload, since the
4873 index has no overload info. */
4874 {
4875 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4876 EXPECT ("std::zfunction", "std::zfunction2"));
4877 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4878 EXPECT ("std::zfunction", "std::zfunction2"));
4879 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4880 EXPECT ("std::zfunction", "std::zfunction2"));
4881 }
4882
4883 /* Check that whitespace is ignored appropriately. A symbol with a
4884 template argument list. */
4885 {
4886 static const char expected[] = "ns::foo<int>";
4887 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4888 EXPECT (expected));
4889 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4890 EXPECT (expected));
4891 }
4892
4893 /* Check that whitespace is ignored appropriately. A symbol with a
4894 template argument list that includes a pointer. */
4895 {
4896 static const char expected[] = "ns::foo<char*>";
4897 /* Try both completion and non-completion modes. */
4898 static const bool completion_mode[2] = {false, true};
4899 for (size_t i = 0; i < 2; i++)
4900 {
4901 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4902 completion_mode[i], EXPECT (expected));
4903 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4904 completion_mode[i], EXPECT (expected));
4905
4906 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4907 completion_mode[i], EXPECT (expected));
4908 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4909 completion_mode[i], EXPECT (expected));
4910 }
4911 }
4912
4913 {
4914 /* Check method qualifiers are ignored. */
4915 static const char expected[] = "ns::foo<char*>";
4916 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4917 symbol_name_match_type::FULL, true, EXPECT (expected));
4918 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4919 symbol_name_match_type::FULL, true, EXPECT (expected));
4920 CHECK_MATCH ("foo < char * > ( int ) const",
4921 symbol_name_match_type::WILD, true, EXPECT (expected));
4922 CHECK_MATCH ("foo < char * > ( int ) &&",
4923 symbol_name_match_type::WILD, true, EXPECT (expected));
4924 }
4925
4926 /* Test lookup names that don't match anything. */
4927 {
4928 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4929 {});
4930
4931 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4932 {});
4933 }
4934
4935 /* Some wild matching tests, exercising "(anonymous namespace)",
4936 which should not be confused with a parameter list. */
4937 {
4938 static const char *syms[] = {
4939 "A::B::C",
4940 "B::C",
4941 "C",
4942 "A :: B :: C ( int )",
4943 "B :: C ( int )",
4944 "C ( int )",
4945 };
4946
4947 for (const char *s : syms)
4948 {
4949 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4950 EXPECT ("(anonymous namespace)::A::B::C"));
4951 }
4952 }
4953
4954 {
4955 static const char expected[] = "ns2::tmpl<int>::foo2";
4956 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4957 EXPECT (expected));
4958 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4959 EXPECT (expected));
4960 }
4961
4962 SELF_CHECK (!any_mismatch);
4963
4964 #undef EXPECT
4965 #undef CHECK_MATCH
4966 }
4967
4968 static void
4969 run_test ()
4970 {
4971 test_mapped_index_find_name_component_bounds ();
4972 test_dw2_expand_symtabs_matching_symbol ();
4973 }
4974
4975 }} // namespace selftests::dw2_expand_symtabs_matching
4976
4977 #endif /* GDB_SELF_TEST */
4978
4979 /* If FILE_MATCHER is NULL or if PER_CU has
4980 dwarf2_per_cu_quick_data::MARK set (see
4981 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4982 EXPANSION_NOTIFY on it. */
4983
4984 static void
4985 dw2_expand_symtabs_matching_one
4986 (struct dwarf2_per_cu_data *per_cu,
4987 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4988 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4989 {
4990 if (file_matcher == NULL || per_cu->v.quick->mark)
4991 {
4992 bool symtab_was_null
4993 = (per_cu->v.quick->compunit_symtab == NULL);
4994
4995 dw2_instantiate_symtab (per_cu, false);
4996
4997 if (expansion_notify != NULL
4998 && symtab_was_null
4999 && per_cu->v.quick->compunit_symtab != NULL)
5000 expansion_notify (per_cu->v.quick->compunit_symtab);
5001 }
5002 }
5003
5004 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5005 matched, to expand corresponding CUs that were marked. IDX is the
5006 index of the symbol name that matched. */
5007
5008 static void
5009 dw2_expand_marked_cus
5010 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5011 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5012 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5013 search_domain kind)
5014 {
5015 offset_type *vec, vec_len, vec_idx;
5016 bool global_seen = false;
5017 mapped_index &index = *dwarf2_per_objfile->index_table;
5018
5019 vec = (offset_type *) (index.constant_pool
5020 + MAYBE_SWAP (index.symbol_table[idx].vec));
5021 vec_len = MAYBE_SWAP (vec[0]);
5022 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5023 {
5024 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5025 /* This value is only valid for index versions >= 7. */
5026 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5027 gdb_index_symbol_kind symbol_kind =
5028 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5029 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5030 /* Only check the symbol attributes if they're present.
5031 Indices prior to version 7 don't record them,
5032 and indices >= 7 may elide them for certain symbols
5033 (gold does this). */
5034 int attrs_valid =
5035 (index.version >= 7
5036 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5037
5038 /* Work around gold/15646. */
5039 if (attrs_valid)
5040 {
5041 if (!is_static && global_seen)
5042 continue;
5043 if (!is_static)
5044 global_seen = true;
5045 }
5046
5047 /* Only check the symbol's kind if it has one. */
5048 if (attrs_valid)
5049 {
5050 switch (kind)
5051 {
5052 case VARIABLES_DOMAIN:
5053 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5054 continue;
5055 break;
5056 case FUNCTIONS_DOMAIN:
5057 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5058 continue;
5059 break;
5060 case TYPES_DOMAIN:
5061 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5062 continue;
5063 break;
5064 default:
5065 break;
5066 }
5067 }
5068
5069 /* Don't crash on bad data. */
5070 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5071 + dwarf2_per_objfile->all_type_units.size ()))
5072 {
5073 complaint (_(".gdb_index entry has bad CU index"
5074 " [in module %s]"),
5075 objfile_name (dwarf2_per_objfile->objfile));
5076 continue;
5077 }
5078
5079 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5080 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5081 expansion_notify);
5082 }
5083 }
5084
5085 /* If FILE_MATCHER is non-NULL, set all the
5086 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5087 that match FILE_MATCHER. */
5088
5089 static void
5090 dw_expand_symtabs_matching_file_matcher
5091 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5092 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5093 {
5094 if (file_matcher == NULL)
5095 return;
5096
5097 objfile *const objfile = dwarf2_per_objfile->objfile;
5098
5099 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5100 htab_eq_pointer,
5101 NULL, xcalloc, xfree));
5102 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5103 htab_eq_pointer,
5104 NULL, xcalloc, xfree));
5105
5106 /* The rule is CUs specify all the files, including those used by
5107 any TU, so there's no need to scan TUs here. */
5108
5109 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5110 {
5111 QUIT;
5112
5113 per_cu->v.quick->mark = 0;
5114
5115 /* We only need to look at symtabs not already expanded. */
5116 if (per_cu->v.quick->compunit_symtab)
5117 continue;
5118
5119 quick_file_names *file_data = dw2_get_file_names (per_cu);
5120 if (file_data == NULL)
5121 continue;
5122
5123 if (htab_find (visited_not_found.get (), file_data) != NULL)
5124 continue;
5125 else if (htab_find (visited_found.get (), file_data) != NULL)
5126 {
5127 per_cu->v.quick->mark = 1;
5128 continue;
5129 }
5130
5131 for (int j = 0; j < file_data->num_file_names; ++j)
5132 {
5133 const char *this_real_name;
5134
5135 if (file_matcher (file_data->file_names[j], false))
5136 {
5137 per_cu->v.quick->mark = 1;
5138 break;
5139 }
5140
5141 /* Before we invoke realpath, which can get expensive when many
5142 files are involved, do a quick comparison of the basenames. */
5143 if (!basenames_may_differ
5144 && !file_matcher (lbasename (file_data->file_names[j]),
5145 true))
5146 continue;
5147
5148 this_real_name = dw2_get_real_path (objfile, file_data, j);
5149 if (file_matcher (this_real_name, false))
5150 {
5151 per_cu->v.quick->mark = 1;
5152 break;
5153 }
5154 }
5155
5156 void **slot = htab_find_slot (per_cu->v.quick->mark
5157 ? visited_found.get ()
5158 : visited_not_found.get (),
5159 file_data, INSERT);
5160 *slot = file_data;
5161 }
5162 }
5163
5164 static void
5165 dw2_expand_symtabs_matching
5166 (struct objfile *objfile,
5167 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5168 const lookup_name_info &lookup_name,
5169 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5170 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5171 enum search_domain kind)
5172 {
5173 struct dwarf2_per_objfile *dwarf2_per_objfile
5174 = get_dwarf2_per_objfile (objfile);
5175
5176 /* index_table is NULL if OBJF_READNOW. */
5177 if (!dwarf2_per_objfile->index_table)
5178 return;
5179
5180 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5181
5182 mapped_index &index = *dwarf2_per_objfile->index_table;
5183
5184 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5185 symbol_matcher,
5186 kind, [&] (offset_type idx)
5187 {
5188 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5189 expansion_notify, kind);
5190 });
5191 }
5192
5193 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5194 symtab. */
5195
5196 static struct compunit_symtab *
5197 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5198 CORE_ADDR pc)
5199 {
5200 int i;
5201
5202 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5203 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5204 return cust;
5205
5206 if (cust->includes == NULL)
5207 return NULL;
5208
5209 for (i = 0; cust->includes[i]; ++i)
5210 {
5211 struct compunit_symtab *s = cust->includes[i];
5212
5213 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5214 if (s != NULL)
5215 return s;
5216 }
5217
5218 return NULL;
5219 }
5220
5221 static struct compunit_symtab *
5222 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5223 struct bound_minimal_symbol msymbol,
5224 CORE_ADDR pc,
5225 struct obj_section *section,
5226 int warn_if_readin)
5227 {
5228 struct dwarf2_per_cu_data *data;
5229 struct compunit_symtab *result;
5230
5231 if (!objfile->psymtabs_addrmap)
5232 return NULL;
5233
5234 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5235 SECT_OFF_TEXT (objfile));
5236 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5237 pc - baseaddr);
5238 if (!data)
5239 return NULL;
5240
5241 if (warn_if_readin && data->v.quick->compunit_symtab)
5242 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5243 paddress (get_objfile_arch (objfile), pc));
5244
5245 result
5246 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5247 false),
5248 pc);
5249 gdb_assert (result != NULL);
5250 return result;
5251 }
5252
5253 static void
5254 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5255 void *data, int need_fullname)
5256 {
5257 struct dwarf2_per_objfile *dwarf2_per_objfile
5258 = get_dwarf2_per_objfile (objfile);
5259
5260 if (!dwarf2_per_objfile->filenames_cache)
5261 {
5262 dwarf2_per_objfile->filenames_cache.emplace ();
5263
5264 htab_up visited (htab_create_alloc (10,
5265 htab_hash_pointer, htab_eq_pointer,
5266 NULL, xcalloc, xfree));
5267
5268 /* The rule is CUs specify all the files, including those used
5269 by any TU, so there's no need to scan TUs here. We can
5270 ignore file names coming from already-expanded CUs. */
5271
5272 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5273 {
5274 if (per_cu->v.quick->compunit_symtab)
5275 {
5276 void **slot = htab_find_slot (visited.get (),
5277 per_cu->v.quick->file_names,
5278 INSERT);
5279
5280 *slot = per_cu->v.quick->file_names;
5281 }
5282 }
5283
5284 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5285 {
5286 /* We only need to look at symtabs not already expanded. */
5287 if (per_cu->v.quick->compunit_symtab)
5288 continue;
5289
5290 quick_file_names *file_data = dw2_get_file_names (per_cu);
5291 if (file_data == NULL)
5292 continue;
5293
5294 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5295 if (*slot)
5296 {
5297 /* Already visited. */
5298 continue;
5299 }
5300 *slot = file_data;
5301
5302 for (int j = 0; j < file_data->num_file_names; ++j)
5303 {
5304 const char *filename = file_data->file_names[j];
5305 dwarf2_per_objfile->filenames_cache->seen (filename);
5306 }
5307 }
5308 }
5309
5310 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5311 {
5312 gdb::unique_xmalloc_ptr<char> this_real_name;
5313
5314 if (need_fullname)
5315 this_real_name = gdb_realpath (filename);
5316 (*fun) (filename, this_real_name.get (), data);
5317 });
5318 }
5319
5320 static int
5321 dw2_has_symbols (struct objfile *objfile)
5322 {
5323 return 1;
5324 }
5325
5326 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5327 {
5328 dw2_has_symbols,
5329 dw2_find_last_source_symtab,
5330 dw2_forget_cached_source_info,
5331 dw2_map_symtabs_matching_filename,
5332 dw2_lookup_symbol,
5333 dw2_print_stats,
5334 dw2_dump,
5335 dw2_expand_symtabs_for_function,
5336 dw2_expand_all_symtabs,
5337 dw2_expand_symtabs_with_fullname,
5338 dw2_map_matching_symbols,
5339 dw2_expand_symtabs_matching,
5340 dw2_find_pc_sect_compunit_symtab,
5341 NULL,
5342 dw2_map_symbol_filenames
5343 };
5344
5345 /* DWARF-5 debug_names reader. */
5346
5347 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5348 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5349
5350 /* A helper function that reads the .debug_names section in SECTION
5351 and fills in MAP. FILENAME is the name of the file containing the
5352 section; it is used for error reporting.
5353
5354 Returns true if all went well, false otherwise. */
5355
5356 static bool
5357 read_debug_names_from_section (struct objfile *objfile,
5358 const char *filename,
5359 struct dwarf2_section_info *section,
5360 mapped_debug_names &map)
5361 {
5362 if (dwarf2_section_empty_p (section))
5363 return false;
5364
5365 /* Older elfutils strip versions could keep the section in the main
5366 executable while splitting it for the separate debug info file. */
5367 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5368 return false;
5369
5370 dwarf2_read_section (objfile, section);
5371
5372 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5373
5374 const gdb_byte *addr = section->buffer;
5375
5376 bfd *const abfd = get_section_bfd_owner (section);
5377
5378 unsigned int bytes_read;
5379 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5380 addr += bytes_read;
5381
5382 map.dwarf5_is_dwarf64 = bytes_read != 4;
5383 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5384 if (bytes_read + length != section->size)
5385 {
5386 /* There may be multiple per-CU indices. */
5387 warning (_("Section .debug_names in %s length %s does not match "
5388 "section length %s, ignoring .debug_names."),
5389 filename, plongest (bytes_read + length),
5390 pulongest (section->size));
5391 return false;
5392 }
5393
5394 /* The version number. */
5395 uint16_t version = read_2_bytes (abfd, addr);
5396 addr += 2;
5397 if (version != 5)
5398 {
5399 warning (_("Section .debug_names in %s has unsupported version %d, "
5400 "ignoring .debug_names."),
5401 filename, version);
5402 return false;
5403 }
5404
5405 /* Padding. */
5406 uint16_t padding = read_2_bytes (abfd, addr);
5407 addr += 2;
5408 if (padding != 0)
5409 {
5410 warning (_("Section .debug_names in %s has unsupported padding %d, "
5411 "ignoring .debug_names."),
5412 filename, padding);
5413 return false;
5414 }
5415
5416 /* comp_unit_count - The number of CUs in the CU list. */
5417 map.cu_count = read_4_bytes (abfd, addr);
5418 addr += 4;
5419
5420 /* local_type_unit_count - The number of TUs in the local TU
5421 list. */
5422 map.tu_count = read_4_bytes (abfd, addr);
5423 addr += 4;
5424
5425 /* foreign_type_unit_count - The number of TUs in the foreign TU
5426 list. */
5427 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5428 addr += 4;
5429 if (foreign_tu_count != 0)
5430 {
5431 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5432 "ignoring .debug_names."),
5433 filename, static_cast<unsigned long> (foreign_tu_count));
5434 return false;
5435 }
5436
5437 /* bucket_count - The number of hash buckets in the hash lookup
5438 table. */
5439 map.bucket_count = read_4_bytes (abfd, addr);
5440 addr += 4;
5441
5442 /* name_count - The number of unique names in the index. */
5443 map.name_count = read_4_bytes (abfd, addr);
5444 addr += 4;
5445
5446 /* abbrev_table_size - The size in bytes of the abbreviations
5447 table. */
5448 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5449 addr += 4;
5450
5451 /* augmentation_string_size - The size in bytes of the augmentation
5452 string. This value is rounded up to a multiple of 4. */
5453 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5454 addr += 4;
5455 map.augmentation_is_gdb = ((augmentation_string_size
5456 == sizeof (dwarf5_augmentation))
5457 && memcmp (addr, dwarf5_augmentation,
5458 sizeof (dwarf5_augmentation)) == 0);
5459 augmentation_string_size += (-augmentation_string_size) & 3;
5460 addr += augmentation_string_size;
5461
5462 /* List of CUs */
5463 map.cu_table_reordered = addr;
5464 addr += map.cu_count * map.offset_size;
5465
5466 /* List of Local TUs */
5467 map.tu_table_reordered = addr;
5468 addr += map.tu_count * map.offset_size;
5469
5470 /* Hash Lookup Table */
5471 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5472 addr += map.bucket_count * 4;
5473 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5474 addr += map.name_count * 4;
5475
5476 /* Name Table */
5477 map.name_table_string_offs_reordered = addr;
5478 addr += map.name_count * map.offset_size;
5479 map.name_table_entry_offs_reordered = addr;
5480 addr += map.name_count * map.offset_size;
5481
5482 const gdb_byte *abbrev_table_start = addr;
5483 for (;;)
5484 {
5485 unsigned int bytes_read;
5486 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5487 addr += bytes_read;
5488 if (index_num == 0)
5489 break;
5490
5491 const auto insertpair
5492 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5493 if (!insertpair.second)
5494 {
5495 warning (_("Section .debug_names in %s has duplicate index %s, "
5496 "ignoring .debug_names."),
5497 filename, pulongest (index_num));
5498 return false;
5499 }
5500 mapped_debug_names::index_val &indexval = insertpair.first->second;
5501 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5502 addr += bytes_read;
5503
5504 for (;;)
5505 {
5506 mapped_debug_names::index_val::attr attr;
5507 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5508 addr += bytes_read;
5509 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5510 addr += bytes_read;
5511 if (attr.form == DW_FORM_implicit_const)
5512 {
5513 attr.implicit_const = read_signed_leb128 (abfd, addr,
5514 &bytes_read);
5515 addr += bytes_read;
5516 }
5517 if (attr.dw_idx == 0 && attr.form == 0)
5518 break;
5519 indexval.attr_vec.push_back (std::move (attr));
5520 }
5521 }
5522 if (addr != abbrev_table_start + abbrev_table_size)
5523 {
5524 warning (_("Section .debug_names in %s has abbreviation_table "
5525 "of size %zu vs. written as %u, ignoring .debug_names."),
5526 filename, addr - abbrev_table_start, abbrev_table_size);
5527 return false;
5528 }
5529 map.entry_pool = addr;
5530
5531 return true;
5532 }
5533
5534 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5535 list. */
5536
5537 static void
5538 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5539 const mapped_debug_names &map,
5540 dwarf2_section_info &section,
5541 bool is_dwz)
5542 {
5543 sect_offset sect_off_prev;
5544 for (uint32_t i = 0; i <= map.cu_count; ++i)
5545 {
5546 sect_offset sect_off_next;
5547 if (i < map.cu_count)
5548 {
5549 sect_off_next
5550 = (sect_offset) (extract_unsigned_integer
5551 (map.cu_table_reordered + i * map.offset_size,
5552 map.offset_size,
5553 map.dwarf5_byte_order));
5554 }
5555 else
5556 sect_off_next = (sect_offset) section.size;
5557 if (i >= 1)
5558 {
5559 const ULONGEST length = sect_off_next - sect_off_prev;
5560 dwarf2_per_cu_data *per_cu
5561 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5562 sect_off_prev, length);
5563 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5564 }
5565 sect_off_prev = sect_off_next;
5566 }
5567 }
5568
5569 /* Read the CU list from the mapped index, and use it to create all
5570 the CU objects for this dwarf2_per_objfile. */
5571
5572 static void
5573 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5574 const mapped_debug_names &map,
5575 const mapped_debug_names &dwz_map)
5576 {
5577 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5578 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5579
5580 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5581 dwarf2_per_objfile->info,
5582 false /* is_dwz */);
5583
5584 if (dwz_map.cu_count == 0)
5585 return;
5586
5587 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5588 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5589 true /* is_dwz */);
5590 }
5591
5592 /* Read .debug_names. If everything went ok, initialize the "quick"
5593 elements of all the CUs and return true. Otherwise, return false. */
5594
5595 static bool
5596 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5597 {
5598 std::unique_ptr<mapped_debug_names> map
5599 (new mapped_debug_names (dwarf2_per_objfile));
5600 mapped_debug_names dwz_map (dwarf2_per_objfile);
5601 struct objfile *objfile = dwarf2_per_objfile->objfile;
5602
5603 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5604 &dwarf2_per_objfile->debug_names,
5605 *map))
5606 return false;
5607
5608 /* Don't use the index if it's empty. */
5609 if (map->name_count == 0)
5610 return false;
5611
5612 /* If there is a .dwz file, read it so we can get its CU list as
5613 well. */
5614 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5615 if (dwz != NULL)
5616 {
5617 if (!read_debug_names_from_section (objfile,
5618 bfd_get_filename (dwz->dwz_bfd),
5619 &dwz->debug_names, dwz_map))
5620 {
5621 warning (_("could not read '.debug_names' section from %s; skipping"),
5622 bfd_get_filename (dwz->dwz_bfd));
5623 return false;
5624 }
5625 }
5626
5627 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5628
5629 if (map->tu_count != 0)
5630 {
5631 /* We can only handle a single .debug_types when we have an
5632 index. */
5633 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5634 return false;
5635
5636 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5637 dwarf2_per_objfile->types, 0);
5638
5639 create_signatured_type_table_from_debug_names
5640 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5641 }
5642
5643 create_addrmap_from_aranges (dwarf2_per_objfile,
5644 &dwarf2_per_objfile->debug_aranges);
5645
5646 dwarf2_per_objfile->debug_names_table = std::move (map);
5647 dwarf2_per_objfile->using_index = 1;
5648 dwarf2_per_objfile->quick_file_names_table =
5649 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5650
5651 return true;
5652 }
5653
5654 /* Type used to manage iterating over all CUs looking for a symbol for
5655 .debug_names. */
5656
5657 class dw2_debug_names_iterator
5658 {
5659 public:
5660 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5661 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5662 dw2_debug_names_iterator (const mapped_debug_names &map,
5663 bool want_specific_block,
5664 block_enum block_index, domain_enum domain,
5665 const char *name)
5666 : m_map (map), m_want_specific_block (want_specific_block),
5667 m_block_index (block_index), m_domain (domain),
5668 m_addr (find_vec_in_debug_names (map, name))
5669 {}
5670
5671 dw2_debug_names_iterator (const mapped_debug_names &map,
5672 search_domain search, uint32_t namei)
5673 : m_map (map),
5674 m_search (search),
5675 m_addr (find_vec_in_debug_names (map, namei))
5676 {}
5677
5678 /* Return the next matching CU or NULL if there are no more. */
5679 dwarf2_per_cu_data *next ();
5680
5681 private:
5682 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5683 const char *name);
5684 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5685 uint32_t namei);
5686
5687 /* The internalized form of .debug_names. */
5688 const mapped_debug_names &m_map;
5689
5690 /* If true, only look for symbols that match BLOCK_INDEX. */
5691 const bool m_want_specific_block = false;
5692
5693 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5694 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5695 value. */
5696 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5697
5698 /* The kind of symbol we're looking for. */
5699 const domain_enum m_domain = UNDEF_DOMAIN;
5700 const search_domain m_search = ALL_DOMAIN;
5701
5702 /* The list of CUs from the index entry of the symbol, or NULL if
5703 not found. */
5704 const gdb_byte *m_addr;
5705 };
5706
5707 const char *
5708 mapped_debug_names::namei_to_name (uint32_t namei) const
5709 {
5710 const ULONGEST namei_string_offs
5711 = extract_unsigned_integer ((name_table_string_offs_reordered
5712 + namei * offset_size),
5713 offset_size,
5714 dwarf5_byte_order);
5715 return read_indirect_string_at_offset
5716 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5717 }
5718
5719 /* Find a slot in .debug_names for the object named NAME. If NAME is
5720 found, return pointer to its pool data. If NAME cannot be found,
5721 return NULL. */
5722
5723 const gdb_byte *
5724 dw2_debug_names_iterator::find_vec_in_debug_names
5725 (const mapped_debug_names &map, const char *name)
5726 {
5727 int (*cmp) (const char *, const char *);
5728
5729 if (current_language->la_language == language_cplus
5730 || current_language->la_language == language_fortran
5731 || current_language->la_language == language_d)
5732 {
5733 /* NAME is already canonical. Drop any qualifiers as
5734 .debug_names does not contain any. */
5735
5736 if (strchr (name, '(') != NULL)
5737 {
5738 gdb::unique_xmalloc_ptr<char> without_params
5739 = cp_remove_params (name);
5740
5741 if (without_params != NULL)
5742 {
5743 name = without_params.get();
5744 }
5745 }
5746 }
5747
5748 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5749
5750 const uint32_t full_hash = dwarf5_djb_hash (name);
5751 uint32_t namei
5752 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5753 (map.bucket_table_reordered
5754 + (full_hash % map.bucket_count)), 4,
5755 map.dwarf5_byte_order);
5756 if (namei == 0)
5757 return NULL;
5758 --namei;
5759 if (namei >= map.name_count)
5760 {
5761 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5762 "[in module %s]"),
5763 namei, map.name_count,
5764 objfile_name (map.dwarf2_per_objfile->objfile));
5765 return NULL;
5766 }
5767
5768 for (;;)
5769 {
5770 const uint32_t namei_full_hash
5771 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5772 (map.hash_table_reordered + namei), 4,
5773 map.dwarf5_byte_order);
5774 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5775 return NULL;
5776
5777 if (full_hash == namei_full_hash)
5778 {
5779 const char *const namei_string = map.namei_to_name (namei);
5780
5781 #if 0 /* An expensive sanity check. */
5782 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5783 {
5784 complaint (_("Wrong .debug_names hash for string at index %u "
5785 "[in module %s]"),
5786 namei, objfile_name (dwarf2_per_objfile->objfile));
5787 return NULL;
5788 }
5789 #endif
5790
5791 if (cmp (namei_string, name) == 0)
5792 {
5793 const ULONGEST namei_entry_offs
5794 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5795 + namei * map.offset_size),
5796 map.offset_size, map.dwarf5_byte_order);
5797 return map.entry_pool + namei_entry_offs;
5798 }
5799 }
5800
5801 ++namei;
5802 if (namei >= map.name_count)
5803 return NULL;
5804 }
5805 }
5806
5807 const gdb_byte *
5808 dw2_debug_names_iterator::find_vec_in_debug_names
5809 (const mapped_debug_names &map, uint32_t namei)
5810 {
5811 if (namei >= map.name_count)
5812 {
5813 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5814 "[in module %s]"),
5815 namei, map.name_count,
5816 objfile_name (map.dwarf2_per_objfile->objfile));
5817 return NULL;
5818 }
5819
5820 const ULONGEST namei_entry_offs
5821 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5822 + namei * map.offset_size),
5823 map.offset_size, map.dwarf5_byte_order);
5824 return map.entry_pool + namei_entry_offs;
5825 }
5826
5827 /* See dw2_debug_names_iterator. */
5828
5829 dwarf2_per_cu_data *
5830 dw2_debug_names_iterator::next ()
5831 {
5832 if (m_addr == NULL)
5833 return NULL;
5834
5835 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5836 struct objfile *objfile = dwarf2_per_objfile->objfile;
5837 bfd *const abfd = objfile->obfd;
5838
5839 again:
5840
5841 unsigned int bytes_read;
5842 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5843 m_addr += bytes_read;
5844 if (abbrev == 0)
5845 return NULL;
5846
5847 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5848 if (indexval_it == m_map.abbrev_map.cend ())
5849 {
5850 complaint (_("Wrong .debug_names undefined abbrev code %s "
5851 "[in module %s]"),
5852 pulongest (abbrev), objfile_name (objfile));
5853 return NULL;
5854 }
5855 const mapped_debug_names::index_val &indexval = indexval_it->second;
5856 bool have_is_static = false;
5857 bool is_static;
5858 dwarf2_per_cu_data *per_cu = NULL;
5859 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5860 {
5861 ULONGEST ull;
5862 switch (attr.form)
5863 {
5864 case DW_FORM_implicit_const:
5865 ull = attr.implicit_const;
5866 break;
5867 case DW_FORM_flag_present:
5868 ull = 1;
5869 break;
5870 case DW_FORM_udata:
5871 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5872 m_addr += bytes_read;
5873 break;
5874 default:
5875 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5876 dwarf_form_name (attr.form),
5877 objfile_name (objfile));
5878 return NULL;
5879 }
5880 switch (attr.dw_idx)
5881 {
5882 case DW_IDX_compile_unit:
5883 /* Don't crash on bad data. */
5884 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5885 {
5886 complaint (_(".debug_names entry has bad CU index %s"
5887 " [in module %s]"),
5888 pulongest (ull),
5889 objfile_name (dwarf2_per_objfile->objfile));
5890 continue;
5891 }
5892 per_cu = dwarf2_per_objfile->get_cutu (ull);
5893 break;
5894 case DW_IDX_type_unit:
5895 /* Don't crash on bad data. */
5896 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5897 {
5898 complaint (_(".debug_names entry has bad TU index %s"
5899 " [in module %s]"),
5900 pulongest (ull),
5901 objfile_name (dwarf2_per_objfile->objfile));
5902 continue;
5903 }
5904 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5905 break;
5906 case DW_IDX_GNU_internal:
5907 if (!m_map.augmentation_is_gdb)
5908 break;
5909 have_is_static = true;
5910 is_static = true;
5911 break;
5912 case DW_IDX_GNU_external:
5913 if (!m_map.augmentation_is_gdb)
5914 break;
5915 have_is_static = true;
5916 is_static = false;
5917 break;
5918 }
5919 }
5920
5921 /* Skip if already read in. */
5922 if (per_cu->v.quick->compunit_symtab)
5923 goto again;
5924
5925 /* Check static vs global. */
5926 if (have_is_static)
5927 {
5928 const bool want_static = m_block_index != GLOBAL_BLOCK;
5929 if (m_want_specific_block && want_static != is_static)
5930 goto again;
5931 }
5932
5933 /* Match dw2_symtab_iter_next, symbol_kind
5934 and debug_names::psymbol_tag. */
5935 switch (m_domain)
5936 {
5937 case VAR_DOMAIN:
5938 switch (indexval.dwarf_tag)
5939 {
5940 case DW_TAG_variable:
5941 case DW_TAG_subprogram:
5942 /* Some types are also in VAR_DOMAIN. */
5943 case DW_TAG_typedef:
5944 case DW_TAG_structure_type:
5945 break;
5946 default:
5947 goto again;
5948 }
5949 break;
5950 case STRUCT_DOMAIN:
5951 switch (indexval.dwarf_tag)
5952 {
5953 case DW_TAG_typedef:
5954 case DW_TAG_structure_type:
5955 break;
5956 default:
5957 goto again;
5958 }
5959 break;
5960 case LABEL_DOMAIN:
5961 switch (indexval.dwarf_tag)
5962 {
5963 case 0:
5964 case DW_TAG_variable:
5965 break;
5966 default:
5967 goto again;
5968 }
5969 break;
5970 default:
5971 break;
5972 }
5973
5974 /* Match dw2_expand_symtabs_matching, symbol_kind and
5975 debug_names::psymbol_tag. */
5976 switch (m_search)
5977 {
5978 case VARIABLES_DOMAIN:
5979 switch (indexval.dwarf_tag)
5980 {
5981 case DW_TAG_variable:
5982 break;
5983 default:
5984 goto again;
5985 }
5986 break;
5987 case FUNCTIONS_DOMAIN:
5988 switch (indexval.dwarf_tag)
5989 {
5990 case DW_TAG_subprogram:
5991 break;
5992 default:
5993 goto again;
5994 }
5995 break;
5996 case TYPES_DOMAIN:
5997 switch (indexval.dwarf_tag)
5998 {
5999 case DW_TAG_typedef:
6000 case DW_TAG_structure_type:
6001 break;
6002 default:
6003 goto again;
6004 }
6005 break;
6006 default:
6007 break;
6008 }
6009
6010 return per_cu;
6011 }
6012
6013 static struct compunit_symtab *
6014 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6015 const char *name, domain_enum domain)
6016 {
6017 const block_enum block_index = static_cast<block_enum> (block_index_int);
6018 struct dwarf2_per_objfile *dwarf2_per_objfile
6019 = get_dwarf2_per_objfile (objfile);
6020
6021 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6022 if (!mapp)
6023 {
6024 /* index is NULL if OBJF_READNOW. */
6025 return NULL;
6026 }
6027 const auto &map = *mapp;
6028
6029 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6030 block_index, domain, name);
6031
6032 struct compunit_symtab *stab_best = NULL;
6033 struct dwarf2_per_cu_data *per_cu;
6034 while ((per_cu = iter.next ()) != NULL)
6035 {
6036 struct symbol *sym, *with_opaque = NULL;
6037 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6038 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6039 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6040
6041 sym = block_find_symbol (block, name, domain,
6042 block_find_non_opaque_type_preferred,
6043 &with_opaque);
6044
6045 /* Some caution must be observed with overloaded functions and
6046 methods, since the index will not contain any overload
6047 information (but NAME might contain it). */
6048
6049 if (sym != NULL
6050 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6051 return stab;
6052 if (with_opaque != NULL
6053 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6054 stab_best = stab;
6055
6056 /* Keep looking through other CUs. */
6057 }
6058
6059 return stab_best;
6060 }
6061
6062 /* This dumps minimal information about .debug_names. It is called
6063 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6064 uses this to verify that .debug_names has been loaded. */
6065
6066 static void
6067 dw2_debug_names_dump (struct objfile *objfile)
6068 {
6069 struct dwarf2_per_objfile *dwarf2_per_objfile
6070 = get_dwarf2_per_objfile (objfile);
6071
6072 gdb_assert (dwarf2_per_objfile->using_index);
6073 printf_filtered (".debug_names:");
6074 if (dwarf2_per_objfile->debug_names_table)
6075 printf_filtered (" exists\n");
6076 else
6077 printf_filtered (" faked for \"readnow\"\n");
6078 printf_filtered ("\n");
6079 }
6080
6081 static void
6082 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6083 const char *func_name)
6084 {
6085 struct dwarf2_per_objfile *dwarf2_per_objfile
6086 = get_dwarf2_per_objfile (objfile);
6087
6088 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6089 if (dwarf2_per_objfile->debug_names_table)
6090 {
6091 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6092
6093 /* Note: It doesn't matter what we pass for block_index here. */
6094 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6095 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6096
6097 struct dwarf2_per_cu_data *per_cu;
6098 while ((per_cu = iter.next ()) != NULL)
6099 dw2_instantiate_symtab (per_cu, false);
6100 }
6101 }
6102
6103 static void
6104 dw2_debug_names_expand_symtabs_matching
6105 (struct objfile *objfile,
6106 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6107 const lookup_name_info &lookup_name,
6108 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6109 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6110 enum search_domain kind)
6111 {
6112 struct dwarf2_per_objfile *dwarf2_per_objfile
6113 = get_dwarf2_per_objfile (objfile);
6114
6115 /* debug_names_table is NULL if OBJF_READNOW. */
6116 if (!dwarf2_per_objfile->debug_names_table)
6117 return;
6118
6119 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6120
6121 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6122
6123 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6124 symbol_matcher,
6125 kind, [&] (offset_type namei)
6126 {
6127 /* The name was matched, now expand corresponding CUs that were
6128 marked. */
6129 dw2_debug_names_iterator iter (map, kind, namei);
6130
6131 struct dwarf2_per_cu_data *per_cu;
6132 while ((per_cu = iter.next ()) != NULL)
6133 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6134 expansion_notify);
6135 });
6136 }
6137
6138 const struct quick_symbol_functions dwarf2_debug_names_functions =
6139 {
6140 dw2_has_symbols,
6141 dw2_find_last_source_symtab,
6142 dw2_forget_cached_source_info,
6143 dw2_map_symtabs_matching_filename,
6144 dw2_debug_names_lookup_symbol,
6145 dw2_print_stats,
6146 dw2_debug_names_dump,
6147 dw2_debug_names_expand_symtabs_for_function,
6148 dw2_expand_all_symtabs,
6149 dw2_expand_symtabs_with_fullname,
6150 dw2_map_matching_symbols,
6151 dw2_debug_names_expand_symtabs_matching,
6152 dw2_find_pc_sect_compunit_symtab,
6153 NULL,
6154 dw2_map_symbol_filenames
6155 };
6156
6157 /* See symfile.h. */
6158
6159 bool
6160 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6161 {
6162 struct dwarf2_per_objfile *dwarf2_per_objfile
6163 = get_dwarf2_per_objfile (objfile);
6164
6165 /* If we're about to read full symbols, don't bother with the
6166 indices. In this case we also don't care if some other debug
6167 format is making psymtabs, because they are all about to be
6168 expanded anyway. */
6169 if ((objfile->flags & OBJF_READNOW))
6170 {
6171 dwarf2_per_objfile->using_index = 1;
6172 create_all_comp_units (dwarf2_per_objfile);
6173 create_all_type_units (dwarf2_per_objfile);
6174 dwarf2_per_objfile->quick_file_names_table
6175 = create_quick_file_names_table
6176 (dwarf2_per_objfile->all_comp_units.size ());
6177
6178 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6179 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6180 {
6181 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6182
6183 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6184 struct dwarf2_per_cu_quick_data);
6185 }
6186
6187 /* Return 1 so that gdb sees the "quick" functions. However,
6188 these functions will be no-ops because we will have expanded
6189 all symtabs. */
6190 *index_kind = dw_index_kind::GDB_INDEX;
6191 return true;
6192 }
6193
6194 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6195 {
6196 *index_kind = dw_index_kind::DEBUG_NAMES;
6197 return true;
6198 }
6199
6200 if (dwarf2_read_gdb_index (dwarf2_per_objfile))
6201 {
6202 *index_kind = dw_index_kind::GDB_INDEX;
6203 return true;
6204 }
6205
6206 return false;
6207 }
6208
6209 \f
6210
6211 /* Build a partial symbol table. */
6212
6213 void
6214 dwarf2_build_psymtabs (struct objfile *objfile)
6215 {
6216 struct dwarf2_per_objfile *dwarf2_per_objfile
6217 = get_dwarf2_per_objfile (objfile);
6218
6219 if (objfile->global_psymbols.capacity () == 0
6220 && objfile->static_psymbols.capacity () == 0)
6221 init_psymbol_list (objfile, 1024);
6222
6223 TRY
6224 {
6225 /* This isn't really ideal: all the data we allocate on the
6226 objfile's obstack is still uselessly kept around. However,
6227 freeing it seems unsafe. */
6228 psymtab_discarder psymtabs (objfile);
6229 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6230 psymtabs.keep ();
6231 }
6232 CATCH (except, RETURN_MASK_ERROR)
6233 {
6234 exception_print (gdb_stderr, except);
6235 }
6236 END_CATCH
6237 }
6238
6239 /* Return the total length of the CU described by HEADER. */
6240
6241 static unsigned int
6242 get_cu_length (const struct comp_unit_head *header)
6243 {
6244 return header->initial_length_size + header->length;
6245 }
6246
6247 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6248
6249 static inline bool
6250 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6251 {
6252 sect_offset bottom = cu_header->sect_off;
6253 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6254
6255 return sect_off >= bottom && sect_off < top;
6256 }
6257
6258 /* Find the base address of the compilation unit for range lists and
6259 location lists. It will normally be specified by DW_AT_low_pc.
6260 In DWARF-3 draft 4, the base address could be overridden by
6261 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6262 compilation units with discontinuous ranges. */
6263
6264 static void
6265 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6266 {
6267 struct attribute *attr;
6268
6269 cu->base_known = 0;
6270 cu->base_address = 0;
6271
6272 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6273 if (attr)
6274 {
6275 cu->base_address = attr_value_as_address (attr);
6276 cu->base_known = 1;
6277 }
6278 else
6279 {
6280 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6281 if (attr)
6282 {
6283 cu->base_address = attr_value_as_address (attr);
6284 cu->base_known = 1;
6285 }
6286 }
6287 }
6288
6289 /* Read in the comp unit header information from the debug_info at info_ptr.
6290 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6291 NOTE: This leaves members offset, first_die_offset to be filled in
6292 by the caller. */
6293
6294 static const gdb_byte *
6295 read_comp_unit_head (struct comp_unit_head *cu_header,
6296 const gdb_byte *info_ptr,
6297 struct dwarf2_section_info *section,
6298 rcuh_kind section_kind)
6299 {
6300 int signed_addr;
6301 unsigned int bytes_read;
6302 const char *filename = get_section_file_name (section);
6303 bfd *abfd = get_section_bfd_owner (section);
6304
6305 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6306 cu_header->initial_length_size = bytes_read;
6307 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6308 info_ptr += bytes_read;
6309 cu_header->version = read_2_bytes (abfd, info_ptr);
6310 if (cu_header->version < 2 || cu_header->version > 5)
6311 error (_("Dwarf Error: wrong version in compilation unit header "
6312 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6313 cu_header->version, filename);
6314 info_ptr += 2;
6315 if (cu_header->version < 5)
6316 switch (section_kind)
6317 {
6318 case rcuh_kind::COMPILE:
6319 cu_header->unit_type = DW_UT_compile;
6320 break;
6321 case rcuh_kind::TYPE:
6322 cu_header->unit_type = DW_UT_type;
6323 break;
6324 default:
6325 internal_error (__FILE__, __LINE__,
6326 _("read_comp_unit_head: invalid section_kind"));
6327 }
6328 else
6329 {
6330 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6331 (read_1_byte (abfd, info_ptr));
6332 info_ptr += 1;
6333 switch (cu_header->unit_type)
6334 {
6335 case DW_UT_compile:
6336 if (section_kind != rcuh_kind::COMPILE)
6337 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6338 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6339 filename);
6340 break;
6341 case DW_UT_type:
6342 section_kind = rcuh_kind::TYPE;
6343 break;
6344 default:
6345 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6346 "(is %d, should be %d or %d) [in module %s]"),
6347 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6348 }
6349
6350 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6351 info_ptr += 1;
6352 }
6353 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6354 cu_header,
6355 &bytes_read);
6356 info_ptr += bytes_read;
6357 if (cu_header->version < 5)
6358 {
6359 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6360 info_ptr += 1;
6361 }
6362 signed_addr = bfd_get_sign_extend_vma (abfd);
6363 if (signed_addr < 0)
6364 internal_error (__FILE__, __LINE__,
6365 _("read_comp_unit_head: dwarf from non elf file"));
6366 cu_header->signed_addr_p = signed_addr;
6367
6368 if (section_kind == rcuh_kind::TYPE)
6369 {
6370 LONGEST type_offset;
6371
6372 cu_header->signature = read_8_bytes (abfd, info_ptr);
6373 info_ptr += 8;
6374
6375 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6376 info_ptr += bytes_read;
6377 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6378 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6379 error (_("Dwarf Error: Too big type_offset in compilation unit "
6380 "header (is %s) [in module %s]"), plongest (type_offset),
6381 filename);
6382 }
6383
6384 return info_ptr;
6385 }
6386
6387 /* Helper function that returns the proper abbrev section for
6388 THIS_CU. */
6389
6390 static struct dwarf2_section_info *
6391 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6392 {
6393 struct dwarf2_section_info *abbrev;
6394 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6395
6396 if (this_cu->is_dwz)
6397 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6398 else
6399 abbrev = &dwarf2_per_objfile->abbrev;
6400
6401 return abbrev;
6402 }
6403
6404 /* Subroutine of read_and_check_comp_unit_head and
6405 read_and_check_type_unit_head to simplify them.
6406 Perform various error checking on the header. */
6407
6408 static void
6409 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6410 struct comp_unit_head *header,
6411 struct dwarf2_section_info *section,
6412 struct dwarf2_section_info *abbrev_section)
6413 {
6414 const char *filename = get_section_file_name (section);
6415
6416 if (to_underlying (header->abbrev_sect_off)
6417 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6418 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6419 "(offset %s + 6) [in module %s]"),
6420 sect_offset_str (header->abbrev_sect_off),
6421 sect_offset_str (header->sect_off),
6422 filename);
6423
6424 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6425 avoid potential 32-bit overflow. */
6426 if (((ULONGEST) header->sect_off + get_cu_length (header))
6427 > section->size)
6428 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6429 "(offset %s + 0) [in module %s]"),
6430 header->length, sect_offset_str (header->sect_off),
6431 filename);
6432 }
6433
6434 /* Read in a CU/TU header and perform some basic error checking.
6435 The contents of the header are stored in HEADER.
6436 The result is a pointer to the start of the first DIE. */
6437
6438 static const gdb_byte *
6439 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6440 struct comp_unit_head *header,
6441 struct dwarf2_section_info *section,
6442 struct dwarf2_section_info *abbrev_section,
6443 const gdb_byte *info_ptr,
6444 rcuh_kind section_kind)
6445 {
6446 const gdb_byte *beg_of_comp_unit = info_ptr;
6447
6448 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6449
6450 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6451
6452 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6453
6454 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6455 abbrev_section);
6456
6457 return info_ptr;
6458 }
6459
6460 /* Fetch the abbreviation table offset from a comp or type unit header. */
6461
6462 static sect_offset
6463 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6464 struct dwarf2_section_info *section,
6465 sect_offset sect_off)
6466 {
6467 bfd *abfd = get_section_bfd_owner (section);
6468 const gdb_byte *info_ptr;
6469 unsigned int initial_length_size, offset_size;
6470 uint16_t version;
6471
6472 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6473 info_ptr = section->buffer + to_underlying (sect_off);
6474 read_initial_length (abfd, info_ptr, &initial_length_size);
6475 offset_size = initial_length_size == 4 ? 4 : 8;
6476 info_ptr += initial_length_size;
6477
6478 version = read_2_bytes (abfd, info_ptr);
6479 info_ptr += 2;
6480 if (version >= 5)
6481 {
6482 /* Skip unit type and address size. */
6483 info_ptr += 2;
6484 }
6485
6486 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6487 }
6488
6489 /* Allocate a new partial symtab for file named NAME and mark this new
6490 partial symtab as being an include of PST. */
6491
6492 static void
6493 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6494 struct objfile *objfile)
6495 {
6496 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6497
6498 if (!IS_ABSOLUTE_PATH (subpst->filename))
6499 {
6500 /* It shares objfile->objfile_obstack. */
6501 subpst->dirname = pst->dirname;
6502 }
6503
6504 subpst->dependencies
6505 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6506 subpst->dependencies[0] = pst;
6507 subpst->number_of_dependencies = 1;
6508
6509 subpst->globals_offset = 0;
6510 subpst->n_global_syms = 0;
6511 subpst->statics_offset = 0;
6512 subpst->n_static_syms = 0;
6513 subpst->compunit_symtab = NULL;
6514 subpst->read_symtab = pst->read_symtab;
6515 subpst->readin = 0;
6516
6517 /* No private part is necessary for include psymtabs. This property
6518 can be used to differentiate between such include psymtabs and
6519 the regular ones. */
6520 subpst->read_symtab_private = NULL;
6521 }
6522
6523 /* Read the Line Number Program data and extract the list of files
6524 included by the source file represented by PST. Build an include
6525 partial symtab for each of these included files. */
6526
6527 static void
6528 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6529 struct die_info *die,
6530 struct partial_symtab *pst)
6531 {
6532 line_header_up lh;
6533 struct attribute *attr;
6534
6535 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6536 if (attr)
6537 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6538 if (lh == NULL)
6539 return; /* No linetable, so no includes. */
6540
6541 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6542 that we pass in the raw text_low here; that is ok because we're
6543 only decoding the line table to make include partial symtabs, and
6544 so the addresses aren't really used. */
6545 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6546 pst->raw_text_low (), 1);
6547 }
6548
6549 static hashval_t
6550 hash_signatured_type (const void *item)
6551 {
6552 const struct signatured_type *sig_type
6553 = (const struct signatured_type *) item;
6554
6555 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6556 return sig_type->signature;
6557 }
6558
6559 static int
6560 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6561 {
6562 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6563 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6564
6565 return lhs->signature == rhs->signature;
6566 }
6567
6568 /* Allocate a hash table for signatured types. */
6569
6570 static htab_t
6571 allocate_signatured_type_table (struct objfile *objfile)
6572 {
6573 return htab_create_alloc_ex (41,
6574 hash_signatured_type,
6575 eq_signatured_type,
6576 NULL,
6577 &objfile->objfile_obstack,
6578 hashtab_obstack_allocate,
6579 dummy_obstack_deallocate);
6580 }
6581
6582 /* A helper function to add a signatured type CU to a table. */
6583
6584 static int
6585 add_signatured_type_cu_to_table (void **slot, void *datum)
6586 {
6587 struct signatured_type *sigt = (struct signatured_type *) *slot;
6588 std::vector<signatured_type *> *all_type_units
6589 = (std::vector<signatured_type *> *) datum;
6590
6591 all_type_units->push_back (sigt);
6592
6593 return 1;
6594 }
6595
6596 /* A helper for create_debug_types_hash_table. Read types from SECTION
6597 and fill them into TYPES_HTAB. It will process only type units,
6598 therefore DW_UT_type. */
6599
6600 static void
6601 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6602 struct dwo_file *dwo_file,
6603 dwarf2_section_info *section, htab_t &types_htab,
6604 rcuh_kind section_kind)
6605 {
6606 struct objfile *objfile = dwarf2_per_objfile->objfile;
6607 struct dwarf2_section_info *abbrev_section;
6608 bfd *abfd;
6609 const gdb_byte *info_ptr, *end_ptr;
6610
6611 abbrev_section = (dwo_file != NULL
6612 ? &dwo_file->sections.abbrev
6613 : &dwarf2_per_objfile->abbrev);
6614
6615 if (dwarf_read_debug)
6616 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6617 get_section_name (section),
6618 get_section_file_name (abbrev_section));
6619
6620 dwarf2_read_section (objfile, section);
6621 info_ptr = section->buffer;
6622
6623 if (info_ptr == NULL)
6624 return;
6625
6626 /* We can't set abfd until now because the section may be empty or
6627 not present, in which case the bfd is unknown. */
6628 abfd = get_section_bfd_owner (section);
6629
6630 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6631 because we don't need to read any dies: the signature is in the
6632 header. */
6633
6634 end_ptr = info_ptr + section->size;
6635 while (info_ptr < end_ptr)
6636 {
6637 struct signatured_type *sig_type;
6638 struct dwo_unit *dwo_tu;
6639 void **slot;
6640 const gdb_byte *ptr = info_ptr;
6641 struct comp_unit_head header;
6642 unsigned int length;
6643
6644 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6645
6646 /* Initialize it due to a false compiler warning. */
6647 header.signature = -1;
6648 header.type_cu_offset_in_tu = (cu_offset) -1;
6649
6650 /* We need to read the type's signature in order to build the hash
6651 table, but we don't need anything else just yet. */
6652
6653 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6654 abbrev_section, ptr, section_kind);
6655
6656 length = get_cu_length (&header);
6657
6658 /* Skip dummy type units. */
6659 if (ptr >= info_ptr + length
6660 || peek_abbrev_code (abfd, ptr) == 0
6661 || header.unit_type != DW_UT_type)
6662 {
6663 info_ptr += length;
6664 continue;
6665 }
6666
6667 if (types_htab == NULL)
6668 {
6669 if (dwo_file)
6670 types_htab = allocate_dwo_unit_table (objfile);
6671 else
6672 types_htab = allocate_signatured_type_table (objfile);
6673 }
6674
6675 if (dwo_file)
6676 {
6677 sig_type = NULL;
6678 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6679 struct dwo_unit);
6680 dwo_tu->dwo_file = dwo_file;
6681 dwo_tu->signature = header.signature;
6682 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6683 dwo_tu->section = section;
6684 dwo_tu->sect_off = sect_off;
6685 dwo_tu->length = length;
6686 }
6687 else
6688 {
6689 /* N.B.: type_offset is not usable if this type uses a DWO file.
6690 The real type_offset is in the DWO file. */
6691 dwo_tu = NULL;
6692 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6693 struct signatured_type);
6694 sig_type->signature = header.signature;
6695 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6696 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6697 sig_type->per_cu.is_debug_types = 1;
6698 sig_type->per_cu.section = section;
6699 sig_type->per_cu.sect_off = sect_off;
6700 sig_type->per_cu.length = length;
6701 }
6702
6703 slot = htab_find_slot (types_htab,
6704 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6705 INSERT);
6706 gdb_assert (slot != NULL);
6707 if (*slot != NULL)
6708 {
6709 sect_offset dup_sect_off;
6710
6711 if (dwo_file)
6712 {
6713 const struct dwo_unit *dup_tu
6714 = (const struct dwo_unit *) *slot;
6715
6716 dup_sect_off = dup_tu->sect_off;
6717 }
6718 else
6719 {
6720 const struct signatured_type *dup_tu
6721 = (const struct signatured_type *) *slot;
6722
6723 dup_sect_off = dup_tu->per_cu.sect_off;
6724 }
6725
6726 complaint (_("debug type entry at offset %s is duplicate to"
6727 " the entry at offset %s, signature %s"),
6728 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6729 hex_string (header.signature));
6730 }
6731 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6732
6733 if (dwarf_read_debug > 1)
6734 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6735 sect_offset_str (sect_off),
6736 hex_string (header.signature));
6737
6738 info_ptr += length;
6739 }
6740 }
6741
6742 /* Create the hash table of all entries in the .debug_types
6743 (or .debug_types.dwo) section(s).
6744 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6745 otherwise it is NULL.
6746
6747 The result is a pointer to the hash table or NULL if there are no types.
6748
6749 Note: This function processes DWO files only, not DWP files. */
6750
6751 static void
6752 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6753 struct dwo_file *dwo_file,
6754 VEC (dwarf2_section_info_def) *types,
6755 htab_t &types_htab)
6756 {
6757 int ix;
6758 struct dwarf2_section_info *section;
6759
6760 if (VEC_empty (dwarf2_section_info_def, types))
6761 return;
6762
6763 for (ix = 0;
6764 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6765 ++ix)
6766 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6767 types_htab, rcuh_kind::TYPE);
6768 }
6769
6770 /* Create the hash table of all entries in the .debug_types section,
6771 and initialize all_type_units.
6772 The result is zero if there is an error (e.g. missing .debug_types section),
6773 otherwise non-zero. */
6774
6775 static int
6776 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6777 {
6778 htab_t types_htab = NULL;
6779
6780 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6781 &dwarf2_per_objfile->info, types_htab,
6782 rcuh_kind::COMPILE);
6783 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6784 dwarf2_per_objfile->types, types_htab);
6785 if (types_htab == NULL)
6786 {
6787 dwarf2_per_objfile->signatured_types = NULL;
6788 return 0;
6789 }
6790
6791 dwarf2_per_objfile->signatured_types = types_htab;
6792
6793 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6794 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6795
6796 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6797 &dwarf2_per_objfile->all_type_units);
6798
6799 return 1;
6800 }
6801
6802 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6803 If SLOT is non-NULL, it is the entry to use in the hash table.
6804 Otherwise we find one. */
6805
6806 static struct signatured_type *
6807 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6808 void **slot)
6809 {
6810 struct objfile *objfile = dwarf2_per_objfile->objfile;
6811
6812 if (dwarf2_per_objfile->all_type_units.size ()
6813 == dwarf2_per_objfile->all_type_units.capacity ())
6814 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6815
6816 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6817 struct signatured_type);
6818
6819 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6820 sig_type->signature = sig;
6821 sig_type->per_cu.is_debug_types = 1;
6822 if (dwarf2_per_objfile->using_index)
6823 {
6824 sig_type->per_cu.v.quick =
6825 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6826 struct dwarf2_per_cu_quick_data);
6827 }
6828
6829 if (slot == NULL)
6830 {
6831 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6832 sig_type, INSERT);
6833 }
6834 gdb_assert (*slot == NULL);
6835 *slot = sig_type;
6836 /* The rest of sig_type must be filled in by the caller. */
6837 return sig_type;
6838 }
6839
6840 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6841 Fill in SIG_ENTRY with DWO_ENTRY. */
6842
6843 static void
6844 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6845 struct signatured_type *sig_entry,
6846 struct dwo_unit *dwo_entry)
6847 {
6848 /* Make sure we're not clobbering something we don't expect to. */
6849 gdb_assert (! sig_entry->per_cu.queued);
6850 gdb_assert (sig_entry->per_cu.cu == NULL);
6851 if (dwarf2_per_objfile->using_index)
6852 {
6853 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6854 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6855 }
6856 else
6857 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6858 gdb_assert (sig_entry->signature == dwo_entry->signature);
6859 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6860 gdb_assert (sig_entry->type_unit_group == NULL);
6861 gdb_assert (sig_entry->dwo_unit == NULL);
6862
6863 sig_entry->per_cu.section = dwo_entry->section;
6864 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6865 sig_entry->per_cu.length = dwo_entry->length;
6866 sig_entry->per_cu.reading_dwo_directly = 1;
6867 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6868 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6869 sig_entry->dwo_unit = dwo_entry;
6870 }
6871
6872 /* Subroutine of lookup_signatured_type.
6873 If we haven't read the TU yet, create the signatured_type data structure
6874 for a TU to be read in directly from a DWO file, bypassing the stub.
6875 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6876 using .gdb_index, then when reading a CU we want to stay in the DWO file
6877 containing that CU. Otherwise we could end up reading several other DWO
6878 files (due to comdat folding) to process the transitive closure of all the
6879 mentioned TUs, and that can be slow. The current DWO file will have every
6880 type signature that it needs.
6881 We only do this for .gdb_index because in the psymtab case we already have
6882 to read all the DWOs to build the type unit groups. */
6883
6884 static struct signatured_type *
6885 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6886 {
6887 struct dwarf2_per_objfile *dwarf2_per_objfile
6888 = cu->per_cu->dwarf2_per_objfile;
6889 struct objfile *objfile = dwarf2_per_objfile->objfile;
6890 struct dwo_file *dwo_file;
6891 struct dwo_unit find_dwo_entry, *dwo_entry;
6892 struct signatured_type find_sig_entry, *sig_entry;
6893 void **slot;
6894
6895 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6896
6897 /* If TU skeletons have been removed then we may not have read in any
6898 TUs yet. */
6899 if (dwarf2_per_objfile->signatured_types == NULL)
6900 {
6901 dwarf2_per_objfile->signatured_types
6902 = allocate_signatured_type_table (objfile);
6903 }
6904
6905 /* We only ever need to read in one copy of a signatured type.
6906 Use the global signatured_types array to do our own comdat-folding
6907 of types. If this is the first time we're reading this TU, and
6908 the TU has an entry in .gdb_index, replace the recorded data from
6909 .gdb_index with this TU. */
6910
6911 find_sig_entry.signature = sig;
6912 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6913 &find_sig_entry, INSERT);
6914 sig_entry = (struct signatured_type *) *slot;
6915
6916 /* We can get here with the TU already read, *or* in the process of being
6917 read. Don't reassign the global entry to point to this DWO if that's
6918 the case. Also note that if the TU is already being read, it may not
6919 have come from a DWO, the program may be a mix of Fission-compiled
6920 code and non-Fission-compiled code. */
6921
6922 /* Have we already tried to read this TU?
6923 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6924 needn't exist in the global table yet). */
6925 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6926 return sig_entry;
6927
6928 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6929 dwo_unit of the TU itself. */
6930 dwo_file = cu->dwo_unit->dwo_file;
6931
6932 /* Ok, this is the first time we're reading this TU. */
6933 if (dwo_file->tus == NULL)
6934 return NULL;
6935 find_dwo_entry.signature = sig;
6936 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
6937 if (dwo_entry == NULL)
6938 return NULL;
6939
6940 /* If the global table doesn't have an entry for this TU, add one. */
6941 if (sig_entry == NULL)
6942 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6943
6944 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6945 sig_entry->per_cu.tu_read = 1;
6946 return sig_entry;
6947 }
6948
6949 /* Subroutine of lookup_signatured_type.
6950 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6951 then try the DWP file. If the TU stub (skeleton) has been removed then
6952 it won't be in .gdb_index. */
6953
6954 static struct signatured_type *
6955 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6956 {
6957 struct dwarf2_per_objfile *dwarf2_per_objfile
6958 = cu->per_cu->dwarf2_per_objfile;
6959 struct objfile *objfile = dwarf2_per_objfile->objfile;
6960 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6961 struct dwo_unit *dwo_entry;
6962 struct signatured_type find_sig_entry, *sig_entry;
6963 void **slot;
6964
6965 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6966 gdb_assert (dwp_file != NULL);
6967
6968 /* If TU skeletons have been removed then we may not have read in any
6969 TUs yet. */
6970 if (dwarf2_per_objfile->signatured_types == NULL)
6971 {
6972 dwarf2_per_objfile->signatured_types
6973 = allocate_signatured_type_table (objfile);
6974 }
6975
6976 find_sig_entry.signature = sig;
6977 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6978 &find_sig_entry, INSERT);
6979 sig_entry = (struct signatured_type *) *slot;
6980
6981 /* Have we already tried to read this TU?
6982 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6983 needn't exist in the global table yet). */
6984 if (sig_entry != NULL)
6985 return sig_entry;
6986
6987 if (dwp_file->tus == NULL)
6988 return NULL;
6989 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6990 sig, 1 /* is_debug_types */);
6991 if (dwo_entry == NULL)
6992 return NULL;
6993
6994 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6995 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6996
6997 return sig_entry;
6998 }
6999
7000 /* Lookup a signature based type for DW_FORM_ref_sig8.
7001 Returns NULL if signature SIG is not present in the table.
7002 It is up to the caller to complain about this. */
7003
7004 static struct signatured_type *
7005 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7006 {
7007 struct dwarf2_per_objfile *dwarf2_per_objfile
7008 = cu->per_cu->dwarf2_per_objfile;
7009
7010 if (cu->dwo_unit
7011 && dwarf2_per_objfile->using_index)
7012 {
7013 /* We're in a DWO/DWP file, and we're using .gdb_index.
7014 These cases require special processing. */
7015 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7016 return lookup_dwo_signatured_type (cu, sig);
7017 else
7018 return lookup_dwp_signatured_type (cu, sig);
7019 }
7020 else
7021 {
7022 struct signatured_type find_entry, *entry;
7023
7024 if (dwarf2_per_objfile->signatured_types == NULL)
7025 return NULL;
7026 find_entry.signature = sig;
7027 entry = ((struct signatured_type *)
7028 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7029 return entry;
7030 }
7031 }
7032 \f
7033 /* Low level DIE reading support. */
7034
7035 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7036
7037 static void
7038 init_cu_die_reader (struct die_reader_specs *reader,
7039 struct dwarf2_cu *cu,
7040 struct dwarf2_section_info *section,
7041 struct dwo_file *dwo_file,
7042 struct abbrev_table *abbrev_table)
7043 {
7044 gdb_assert (section->readin && section->buffer != NULL);
7045 reader->abfd = get_section_bfd_owner (section);
7046 reader->cu = cu;
7047 reader->dwo_file = dwo_file;
7048 reader->die_section = section;
7049 reader->buffer = section->buffer;
7050 reader->buffer_end = section->buffer + section->size;
7051 reader->comp_dir = NULL;
7052 reader->abbrev_table = abbrev_table;
7053 }
7054
7055 /* Subroutine of init_cutu_and_read_dies to simplify it.
7056 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7057 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7058 already.
7059
7060 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7061 from it to the DIE in the DWO. If NULL we are skipping the stub.
7062 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7063 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7064 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7065 STUB_COMP_DIR may be non-NULL.
7066 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7067 are filled in with the info of the DIE from the DWO file.
7068 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7069 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7070 kept around for at least as long as *RESULT_READER.
7071
7072 The result is non-zero if a valid (non-dummy) DIE was found. */
7073
7074 static int
7075 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7076 struct dwo_unit *dwo_unit,
7077 struct die_info *stub_comp_unit_die,
7078 const char *stub_comp_dir,
7079 struct die_reader_specs *result_reader,
7080 const gdb_byte **result_info_ptr,
7081 struct die_info **result_comp_unit_die,
7082 int *result_has_children,
7083 abbrev_table_up *result_dwo_abbrev_table)
7084 {
7085 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7086 struct objfile *objfile = dwarf2_per_objfile->objfile;
7087 struct dwarf2_cu *cu = this_cu->cu;
7088 bfd *abfd;
7089 const gdb_byte *begin_info_ptr, *info_ptr;
7090 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7091 int i,num_extra_attrs;
7092 struct dwarf2_section_info *dwo_abbrev_section;
7093 struct attribute *attr;
7094 struct die_info *comp_unit_die;
7095
7096 /* At most one of these may be provided. */
7097 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7098
7099 /* These attributes aren't processed until later:
7100 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7101 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7102 referenced later. However, these attributes are found in the stub
7103 which we won't have later. In order to not impose this complication
7104 on the rest of the code, we read them here and copy them to the
7105 DWO CU/TU die. */
7106
7107 stmt_list = NULL;
7108 low_pc = NULL;
7109 high_pc = NULL;
7110 ranges = NULL;
7111 comp_dir = NULL;
7112
7113 if (stub_comp_unit_die != NULL)
7114 {
7115 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7116 DWO file. */
7117 if (! this_cu->is_debug_types)
7118 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7119 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7120 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7121 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7122 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7123
7124 /* There should be a DW_AT_addr_base attribute here (if needed).
7125 We need the value before we can process DW_FORM_GNU_addr_index. */
7126 cu->addr_base = 0;
7127 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7128 if (attr)
7129 cu->addr_base = DW_UNSND (attr);
7130
7131 /* There should be a DW_AT_ranges_base attribute here (if needed).
7132 We need the value before we can process DW_AT_ranges. */
7133 cu->ranges_base = 0;
7134 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7135 if (attr)
7136 cu->ranges_base = DW_UNSND (attr);
7137 }
7138 else if (stub_comp_dir != NULL)
7139 {
7140 /* Reconstruct the comp_dir attribute to simplify the code below. */
7141 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7142 comp_dir->name = DW_AT_comp_dir;
7143 comp_dir->form = DW_FORM_string;
7144 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7145 DW_STRING (comp_dir) = stub_comp_dir;
7146 }
7147
7148 /* Set up for reading the DWO CU/TU. */
7149 cu->dwo_unit = dwo_unit;
7150 dwarf2_section_info *section = dwo_unit->section;
7151 dwarf2_read_section (objfile, section);
7152 abfd = get_section_bfd_owner (section);
7153 begin_info_ptr = info_ptr = (section->buffer
7154 + to_underlying (dwo_unit->sect_off));
7155 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7156
7157 if (this_cu->is_debug_types)
7158 {
7159 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7160
7161 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7162 &cu->header, section,
7163 dwo_abbrev_section,
7164 info_ptr, rcuh_kind::TYPE);
7165 /* This is not an assert because it can be caused by bad debug info. */
7166 if (sig_type->signature != cu->header.signature)
7167 {
7168 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7169 " TU at offset %s [in module %s]"),
7170 hex_string (sig_type->signature),
7171 hex_string (cu->header.signature),
7172 sect_offset_str (dwo_unit->sect_off),
7173 bfd_get_filename (abfd));
7174 }
7175 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7176 /* For DWOs coming from DWP files, we don't know the CU length
7177 nor the type's offset in the TU until now. */
7178 dwo_unit->length = get_cu_length (&cu->header);
7179 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7180
7181 /* Establish the type offset that can be used to lookup the type.
7182 For DWO files, we don't know it until now. */
7183 sig_type->type_offset_in_section
7184 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7185 }
7186 else
7187 {
7188 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7189 &cu->header, section,
7190 dwo_abbrev_section,
7191 info_ptr, rcuh_kind::COMPILE);
7192 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7193 /* For DWOs coming from DWP files, we don't know the CU length
7194 until now. */
7195 dwo_unit->length = get_cu_length (&cu->header);
7196 }
7197
7198 *result_dwo_abbrev_table
7199 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7200 cu->header.abbrev_sect_off);
7201 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7202 result_dwo_abbrev_table->get ());
7203
7204 /* Read in the die, but leave space to copy over the attributes
7205 from the stub. This has the benefit of simplifying the rest of
7206 the code - all the work to maintain the illusion of a single
7207 DW_TAG_{compile,type}_unit DIE is done here. */
7208 num_extra_attrs = ((stmt_list != NULL)
7209 + (low_pc != NULL)
7210 + (high_pc != NULL)
7211 + (ranges != NULL)
7212 + (comp_dir != NULL));
7213 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7214 result_has_children, num_extra_attrs);
7215
7216 /* Copy over the attributes from the stub to the DIE we just read in. */
7217 comp_unit_die = *result_comp_unit_die;
7218 i = comp_unit_die->num_attrs;
7219 if (stmt_list != NULL)
7220 comp_unit_die->attrs[i++] = *stmt_list;
7221 if (low_pc != NULL)
7222 comp_unit_die->attrs[i++] = *low_pc;
7223 if (high_pc != NULL)
7224 comp_unit_die->attrs[i++] = *high_pc;
7225 if (ranges != NULL)
7226 comp_unit_die->attrs[i++] = *ranges;
7227 if (comp_dir != NULL)
7228 comp_unit_die->attrs[i++] = *comp_dir;
7229 comp_unit_die->num_attrs += num_extra_attrs;
7230
7231 if (dwarf_die_debug)
7232 {
7233 fprintf_unfiltered (gdb_stdlog,
7234 "Read die from %s@0x%x of %s:\n",
7235 get_section_name (section),
7236 (unsigned) (begin_info_ptr - section->buffer),
7237 bfd_get_filename (abfd));
7238 dump_die (comp_unit_die, dwarf_die_debug);
7239 }
7240
7241 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7242 TUs by skipping the stub and going directly to the entry in the DWO file.
7243 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7244 to get it via circuitous means. Blech. */
7245 if (comp_dir != NULL)
7246 result_reader->comp_dir = DW_STRING (comp_dir);
7247
7248 /* Skip dummy compilation units. */
7249 if (info_ptr >= begin_info_ptr + dwo_unit->length
7250 || peek_abbrev_code (abfd, info_ptr) == 0)
7251 return 0;
7252
7253 *result_info_ptr = info_ptr;
7254 return 1;
7255 }
7256
7257 /* Subroutine of init_cutu_and_read_dies to simplify it.
7258 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7259 Returns NULL if the specified DWO unit cannot be found. */
7260
7261 static struct dwo_unit *
7262 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7263 struct die_info *comp_unit_die)
7264 {
7265 struct dwarf2_cu *cu = this_cu->cu;
7266 ULONGEST signature;
7267 struct dwo_unit *dwo_unit;
7268 const char *comp_dir, *dwo_name;
7269
7270 gdb_assert (cu != NULL);
7271
7272 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7273 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7274 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7275
7276 if (this_cu->is_debug_types)
7277 {
7278 struct signatured_type *sig_type;
7279
7280 /* Since this_cu is the first member of struct signatured_type,
7281 we can go from a pointer to one to a pointer to the other. */
7282 sig_type = (struct signatured_type *) this_cu;
7283 signature = sig_type->signature;
7284 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7285 }
7286 else
7287 {
7288 struct attribute *attr;
7289
7290 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7291 if (! attr)
7292 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7293 " [in module %s]"),
7294 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7295 signature = DW_UNSND (attr);
7296 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7297 signature);
7298 }
7299
7300 return dwo_unit;
7301 }
7302
7303 /* Subroutine of init_cutu_and_read_dies to simplify it.
7304 See it for a description of the parameters.
7305 Read a TU directly from a DWO file, bypassing the stub. */
7306
7307 static void
7308 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7309 int use_existing_cu, int keep,
7310 die_reader_func_ftype *die_reader_func,
7311 void *data)
7312 {
7313 std::unique_ptr<dwarf2_cu> new_cu;
7314 struct signatured_type *sig_type;
7315 struct die_reader_specs reader;
7316 const gdb_byte *info_ptr;
7317 struct die_info *comp_unit_die;
7318 int has_children;
7319 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7320
7321 /* Verify we can do the following downcast, and that we have the
7322 data we need. */
7323 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7324 sig_type = (struct signatured_type *) this_cu;
7325 gdb_assert (sig_type->dwo_unit != NULL);
7326
7327 if (use_existing_cu && this_cu->cu != NULL)
7328 {
7329 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7330 /* There's no need to do the rereading_dwo_cu handling that
7331 init_cutu_and_read_dies does since we don't read the stub. */
7332 }
7333 else
7334 {
7335 /* If !use_existing_cu, this_cu->cu must be NULL. */
7336 gdb_assert (this_cu->cu == NULL);
7337 new_cu.reset (new dwarf2_cu (this_cu));
7338 }
7339
7340 /* A future optimization, if needed, would be to use an existing
7341 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7342 could share abbrev tables. */
7343
7344 /* The abbreviation table used by READER, this must live at least as long as
7345 READER. */
7346 abbrev_table_up dwo_abbrev_table;
7347
7348 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7349 NULL /* stub_comp_unit_die */,
7350 sig_type->dwo_unit->dwo_file->comp_dir,
7351 &reader, &info_ptr,
7352 &comp_unit_die, &has_children,
7353 &dwo_abbrev_table) == 0)
7354 {
7355 /* Dummy die. */
7356 return;
7357 }
7358
7359 /* All the "real" work is done here. */
7360 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7361
7362 /* This duplicates the code in init_cutu_and_read_dies,
7363 but the alternative is making the latter more complex.
7364 This function is only for the special case of using DWO files directly:
7365 no point in overly complicating the general case just to handle this. */
7366 if (new_cu != NULL && keep)
7367 {
7368 /* Link this CU into read_in_chain. */
7369 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7370 dwarf2_per_objfile->read_in_chain = this_cu;
7371 /* The chain owns it now. */
7372 new_cu.release ();
7373 }
7374 }
7375
7376 /* Initialize a CU (or TU) and read its DIEs.
7377 If the CU defers to a DWO file, read the DWO file as well.
7378
7379 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7380 Otherwise the table specified in the comp unit header is read in and used.
7381 This is an optimization for when we already have the abbrev table.
7382
7383 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7384 Otherwise, a new CU is allocated with xmalloc.
7385
7386 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7387 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7388
7389 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7390 linker) then DIE_READER_FUNC will not get called. */
7391
7392 static void
7393 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7394 struct abbrev_table *abbrev_table,
7395 int use_existing_cu, int keep,
7396 bool skip_partial,
7397 die_reader_func_ftype *die_reader_func,
7398 void *data)
7399 {
7400 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7401 struct objfile *objfile = dwarf2_per_objfile->objfile;
7402 struct dwarf2_section_info *section = this_cu->section;
7403 bfd *abfd = get_section_bfd_owner (section);
7404 struct dwarf2_cu *cu;
7405 const gdb_byte *begin_info_ptr, *info_ptr;
7406 struct die_reader_specs reader;
7407 struct die_info *comp_unit_die;
7408 int has_children;
7409 struct attribute *attr;
7410 struct signatured_type *sig_type = NULL;
7411 struct dwarf2_section_info *abbrev_section;
7412 /* Non-zero if CU currently points to a DWO file and we need to
7413 reread it. When this happens we need to reread the skeleton die
7414 before we can reread the DWO file (this only applies to CUs, not TUs). */
7415 int rereading_dwo_cu = 0;
7416
7417 if (dwarf_die_debug)
7418 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7419 this_cu->is_debug_types ? "type" : "comp",
7420 sect_offset_str (this_cu->sect_off));
7421
7422 if (use_existing_cu)
7423 gdb_assert (keep);
7424
7425 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7426 file (instead of going through the stub), short-circuit all of this. */
7427 if (this_cu->reading_dwo_directly)
7428 {
7429 /* Narrow down the scope of possibilities to have to understand. */
7430 gdb_assert (this_cu->is_debug_types);
7431 gdb_assert (abbrev_table == NULL);
7432 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7433 die_reader_func, data);
7434 return;
7435 }
7436
7437 /* This is cheap if the section is already read in. */
7438 dwarf2_read_section (objfile, section);
7439
7440 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7441
7442 abbrev_section = get_abbrev_section_for_cu (this_cu);
7443
7444 std::unique_ptr<dwarf2_cu> new_cu;
7445 if (use_existing_cu && this_cu->cu != NULL)
7446 {
7447 cu = this_cu->cu;
7448 /* If this CU is from a DWO file we need to start over, we need to
7449 refetch the attributes from the skeleton CU.
7450 This could be optimized by retrieving those attributes from when we
7451 were here the first time: the previous comp_unit_die was stored in
7452 comp_unit_obstack. But there's no data yet that we need this
7453 optimization. */
7454 if (cu->dwo_unit != NULL)
7455 rereading_dwo_cu = 1;
7456 }
7457 else
7458 {
7459 /* If !use_existing_cu, this_cu->cu must be NULL. */
7460 gdb_assert (this_cu->cu == NULL);
7461 new_cu.reset (new dwarf2_cu (this_cu));
7462 cu = new_cu.get ();
7463 }
7464
7465 /* Get the header. */
7466 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7467 {
7468 /* We already have the header, there's no need to read it in again. */
7469 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7470 }
7471 else
7472 {
7473 if (this_cu->is_debug_types)
7474 {
7475 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7476 &cu->header, section,
7477 abbrev_section, info_ptr,
7478 rcuh_kind::TYPE);
7479
7480 /* Since per_cu is the first member of struct signatured_type,
7481 we can go from a pointer to one to a pointer to the other. */
7482 sig_type = (struct signatured_type *) this_cu;
7483 gdb_assert (sig_type->signature == cu->header.signature);
7484 gdb_assert (sig_type->type_offset_in_tu
7485 == cu->header.type_cu_offset_in_tu);
7486 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7487
7488 /* LENGTH has not been set yet for type units if we're
7489 using .gdb_index. */
7490 this_cu->length = get_cu_length (&cu->header);
7491
7492 /* Establish the type offset that can be used to lookup the type. */
7493 sig_type->type_offset_in_section =
7494 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7495
7496 this_cu->dwarf_version = cu->header.version;
7497 }
7498 else
7499 {
7500 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7501 &cu->header, section,
7502 abbrev_section,
7503 info_ptr,
7504 rcuh_kind::COMPILE);
7505
7506 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7507 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7508 this_cu->dwarf_version = cu->header.version;
7509 }
7510 }
7511
7512 /* Skip dummy compilation units. */
7513 if (info_ptr >= begin_info_ptr + this_cu->length
7514 || peek_abbrev_code (abfd, info_ptr) == 0)
7515 return;
7516
7517 /* If we don't have them yet, read the abbrevs for this compilation unit.
7518 And if we need to read them now, make sure they're freed when we're
7519 done (own the table through ABBREV_TABLE_HOLDER). */
7520 abbrev_table_up abbrev_table_holder;
7521 if (abbrev_table != NULL)
7522 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7523 else
7524 {
7525 abbrev_table_holder
7526 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7527 cu->header.abbrev_sect_off);
7528 abbrev_table = abbrev_table_holder.get ();
7529 }
7530
7531 /* Read the top level CU/TU die. */
7532 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7533 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7534
7535 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7536 return;
7537
7538 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7539 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7540 table from the DWO file and pass the ownership over to us. It will be
7541 referenced from READER, so we must make sure to free it after we're done
7542 with READER.
7543
7544 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7545 DWO CU, that this test will fail (the attribute will not be present). */
7546 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7547 abbrev_table_up dwo_abbrev_table;
7548 if (attr)
7549 {
7550 struct dwo_unit *dwo_unit;
7551 struct die_info *dwo_comp_unit_die;
7552
7553 if (has_children)
7554 {
7555 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7556 " has children (offset %s) [in module %s]"),
7557 sect_offset_str (this_cu->sect_off),
7558 bfd_get_filename (abfd));
7559 }
7560 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7561 if (dwo_unit != NULL)
7562 {
7563 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7564 comp_unit_die, NULL,
7565 &reader, &info_ptr,
7566 &dwo_comp_unit_die, &has_children,
7567 &dwo_abbrev_table) == 0)
7568 {
7569 /* Dummy die. */
7570 return;
7571 }
7572 comp_unit_die = dwo_comp_unit_die;
7573 }
7574 else
7575 {
7576 /* Yikes, we couldn't find the rest of the DIE, we only have
7577 the stub. A complaint has already been logged. There's
7578 not much more we can do except pass on the stub DIE to
7579 die_reader_func. We don't want to throw an error on bad
7580 debug info. */
7581 }
7582 }
7583
7584 /* All of the above is setup for this call. Yikes. */
7585 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7586
7587 /* Done, clean up. */
7588 if (new_cu != NULL && keep)
7589 {
7590 /* Link this CU into read_in_chain. */
7591 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7592 dwarf2_per_objfile->read_in_chain = this_cu;
7593 /* The chain owns it now. */
7594 new_cu.release ();
7595 }
7596 }
7597
7598 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7599 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7600 to have already done the lookup to find the DWO file).
7601
7602 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7603 THIS_CU->is_debug_types, but nothing else.
7604
7605 We fill in THIS_CU->length.
7606
7607 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7608 linker) then DIE_READER_FUNC will not get called.
7609
7610 THIS_CU->cu is always freed when done.
7611 This is done in order to not leave THIS_CU->cu in a state where we have
7612 to care whether it refers to the "main" CU or the DWO CU. */
7613
7614 static void
7615 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7616 struct dwo_file *dwo_file,
7617 die_reader_func_ftype *die_reader_func,
7618 void *data)
7619 {
7620 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7621 struct objfile *objfile = dwarf2_per_objfile->objfile;
7622 struct dwarf2_section_info *section = this_cu->section;
7623 bfd *abfd = get_section_bfd_owner (section);
7624 struct dwarf2_section_info *abbrev_section;
7625 const gdb_byte *begin_info_ptr, *info_ptr;
7626 struct die_reader_specs reader;
7627 struct die_info *comp_unit_die;
7628 int has_children;
7629
7630 if (dwarf_die_debug)
7631 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7632 this_cu->is_debug_types ? "type" : "comp",
7633 sect_offset_str (this_cu->sect_off));
7634
7635 gdb_assert (this_cu->cu == NULL);
7636
7637 abbrev_section = (dwo_file != NULL
7638 ? &dwo_file->sections.abbrev
7639 : get_abbrev_section_for_cu (this_cu));
7640
7641 /* This is cheap if the section is already read in. */
7642 dwarf2_read_section (objfile, section);
7643
7644 struct dwarf2_cu cu (this_cu);
7645
7646 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7647 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7648 &cu.header, section,
7649 abbrev_section, info_ptr,
7650 (this_cu->is_debug_types
7651 ? rcuh_kind::TYPE
7652 : rcuh_kind::COMPILE));
7653
7654 this_cu->length = get_cu_length (&cu.header);
7655
7656 /* Skip dummy compilation units. */
7657 if (info_ptr >= begin_info_ptr + this_cu->length
7658 || peek_abbrev_code (abfd, info_ptr) == 0)
7659 return;
7660
7661 abbrev_table_up abbrev_table
7662 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7663 cu.header.abbrev_sect_off);
7664
7665 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7666 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7667
7668 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7669 }
7670
7671 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7672 does not lookup the specified DWO file.
7673 This cannot be used to read DWO files.
7674
7675 THIS_CU->cu is always freed when done.
7676 This is done in order to not leave THIS_CU->cu in a state where we have
7677 to care whether it refers to the "main" CU or the DWO CU.
7678 We can revisit this if the data shows there's a performance issue. */
7679
7680 static void
7681 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7682 die_reader_func_ftype *die_reader_func,
7683 void *data)
7684 {
7685 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7686 }
7687 \f
7688 /* Type Unit Groups.
7689
7690 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7691 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7692 so that all types coming from the same compilation (.o file) are grouped
7693 together. A future step could be to put the types in the same symtab as
7694 the CU the types ultimately came from. */
7695
7696 static hashval_t
7697 hash_type_unit_group (const void *item)
7698 {
7699 const struct type_unit_group *tu_group
7700 = (const struct type_unit_group *) item;
7701
7702 return hash_stmt_list_entry (&tu_group->hash);
7703 }
7704
7705 static int
7706 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7707 {
7708 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7709 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7710
7711 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7712 }
7713
7714 /* Allocate a hash table for type unit groups. */
7715
7716 static htab_t
7717 allocate_type_unit_groups_table (struct objfile *objfile)
7718 {
7719 return htab_create_alloc_ex (3,
7720 hash_type_unit_group,
7721 eq_type_unit_group,
7722 NULL,
7723 &objfile->objfile_obstack,
7724 hashtab_obstack_allocate,
7725 dummy_obstack_deallocate);
7726 }
7727
7728 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7729 partial symtabs. We combine several TUs per psymtab to not let the size
7730 of any one psymtab grow too big. */
7731 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7732 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7733
7734 /* Helper routine for get_type_unit_group.
7735 Create the type_unit_group object used to hold one or more TUs. */
7736
7737 static struct type_unit_group *
7738 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7739 {
7740 struct dwarf2_per_objfile *dwarf2_per_objfile
7741 = cu->per_cu->dwarf2_per_objfile;
7742 struct objfile *objfile = dwarf2_per_objfile->objfile;
7743 struct dwarf2_per_cu_data *per_cu;
7744 struct type_unit_group *tu_group;
7745
7746 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7747 struct type_unit_group);
7748 per_cu = &tu_group->per_cu;
7749 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7750
7751 if (dwarf2_per_objfile->using_index)
7752 {
7753 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7754 struct dwarf2_per_cu_quick_data);
7755 }
7756 else
7757 {
7758 unsigned int line_offset = to_underlying (line_offset_struct);
7759 struct partial_symtab *pst;
7760 char *name;
7761
7762 /* Give the symtab a useful name for debug purposes. */
7763 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7764 name = xstrprintf ("<type_units_%d>",
7765 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7766 else
7767 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7768
7769 pst = create_partial_symtab (per_cu, name);
7770 pst->anonymous = 1;
7771
7772 xfree (name);
7773 }
7774
7775 tu_group->hash.dwo_unit = cu->dwo_unit;
7776 tu_group->hash.line_sect_off = line_offset_struct;
7777
7778 return tu_group;
7779 }
7780
7781 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7782 STMT_LIST is a DW_AT_stmt_list attribute. */
7783
7784 static struct type_unit_group *
7785 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7786 {
7787 struct dwarf2_per_objfile *dwarf2_per_objfile
7788 = cu->per_cu->dwarf2_per_objfile;
7789 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7790 struct type_unit_group *tu_group;
7791 void **slot;
7792 unsigned int line_offset;
7793 struct type_unit_group type_unit_group_for_lookup;
7794
7795 if (dwarf2_per_objfile->type_unit_groups == NULL)
7796 {
7797 dwarf2_per_objfile->type_unit_groups =
7798 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7799 }
7800
7801 /* Do we need to create a new group, or can we use an existing one? */
7802
7803 if (stmt_list)
7804 {
7805 line_offset = DW_UNSND (stmt_list);
7806 ++tu_stats->nr_symtab_sharers;
7807 }
7808 else
7809 {
7810 /* Ugh, no stmt_list. Rare, but we have to handle it.
7811 We can do various things here like create one group per TU or
7812 spread them over multiple groups to split up the expansion work.
7813 To avoid worst case scenarios (too many groups or too large groups)
7814 we, umm, group them in bunches. */
7815 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7816 | (tu_stats->nr_stmt_less_type_units
7817 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7818 ++tu_stats->nr_stmt_less_type_units;
7819 }
7820
7821 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7822 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7823 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7824 &type_unit_group_for_lookup, INSERT);
7825 if (*slot != NULL)
7826 {
7827 tu_group = (struct type_unit_group *) *slot;
7828 gdb_assert (tu_group != NULL);
7829 }
7830 else
7831 {
7832 sect_offset line_offset_struct = (sect_offset) line_offset;
7833 tu_group = create_type_unit_group (cu, line_offset_struct);
7834 *slot = tu_group;
7835 ++tu_stats->nr_symtabs;
7836 }
7837
7838 return tu_group;
7839 }
7840 \f
7841 /* Partial symbol tables. */
7842
7843 /* Create a psymtab named NAME and assign it to PER_CU.
7844
7845 The caller must fill in the following details:
7846 dirname, textlow, texthigh. */
7847
7848 static struct partial_symtab *
7849 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7850 {
7851 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7852 struct partial_symtab *pst;
7853
7854 pst = start_psymtab_common (objfile, name, 0,
7855 objfile->global_psymbols,
7856 objfile->static_psymbols);
7857
7858 pst->psymtabs_addrmap_supported = 1;
7859
7860 /* This is the glue that links PST into GDB's symbol API. */
7861 pst->read_symtab_private = per_cu;
7862 pst->read_symtab = dwarf2_read_symtab;
7863 per_cu->v.psymtab = pst;
7864
7865 return pst;
7866 }
7867
7868 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7869 type. */
7870
7871 struct process_psymtab_comp_unit_data
7872 {
7873 /* True if we are reading a DW_TAG_partial_unit. */
7874
7875 int want_partial_unit;
7876
7877 /* The "pretend" language that is used if the CU doesn't declare a
7878 language. */
7879
7880 enum language pretend_language;
7881 };
7882
7883 /* die_reader_func for process_psymtab_comp_unit. */
7884
7885 static void
7886 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7887 const gdb_byte *info_ptr,
7888 struct die_info *comp_unit_die,
7889 int has_children,
7890 void *data)
7891 {
7892 struct dwarf2_cu *cu = reader->cu;
7893 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7894 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7895 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7896 CORE_ADDR baseaddr;
7897 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7898 struct partial_symtab *pst;
7899 enum pc_bounds_kind cu_bounds_kind;
7900 const char *filename;
7901 struct process_psymtab_comp_unit_data *info
7902 = (struct process_psymtab_comp_unit_data *) data;
7903
7904 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7905 return;
7906
7907 gdb_assert (! per_cu->is_debug_types);
7908
7909 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7910
7911 /* Allocate a new partial symbol table structure. */
7912 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7913 if (filename == NULL)
7914 filename = "";
7915
7916 pst = create_partial_symtab (per_cu, filename);
7917
7918 /* This must be done before calling dwarf2_build_include_psymtabs. */
7919 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7920
7921 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7922
7923 dwarf2_find_base_address (comp_unit_die, cu);
7924
7925 /* Possibly set the default values of LOWPC and HIGHPC from
7926 `DW_AT_ranges'. */
7927 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7928 &best_highpc, cu, pst);
7929 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7930 {
7931 CORE_ADDR low
7932 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7933 - baseaddr);
7934 CORE_ADDR high
7935 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7936 - baseaddr - 1);
7937 /* Store the contiguous range if it is not empty; it can be
7938 empty for CUs with no code. */
7939 addrmap_set_empty (objfile->psymtabs_addrmap, low, high, pst);
7940 }
7941
7942 /* Check if comp unit has_children.
7943 If so, read the rest of the partial symbols from this comp unit.
7944 If not, there's no more debug_info for this comp unit. */
7945 if (has_children)
7946 {
7947 struct partial_die_info *first_die;
7948 CORE_ADDR lowpc, highpc;
7949
7950 lowpc = ((CORE_ADDR) -1);
7951 highpc = ((CORE_ADDR) 0);
7952
7953 first_die = load_partial_dies (reader, info_ptr, 1);
7954
7955 scan_partial_symbols (first_die, &lowpc, &highpc,
7956 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7957
7958 /* If we didn't find a lowpc, set it to highpc to avoid
7959 complaints from `maint check'. */
7960 if (lowpc == ((CORE_ADDR) -1))
7961 lowpc = highpc;
7962
7963 /* If the compilation unit didn't have an explicit address range,
7964 then use the information extracted from its child dies. */
7965 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7966 {
7967 best_lowpc = lowpc;
7968 best_highpc = highpc;
7969 }
7970 }
7971 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7972 best_lowpc + baseaddr)
7973 - baseaddr);
7974 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7975 best_highpc + baseaddr)
7976 - baseaddr);
7977
7978 end_psymtab_common (objfile, pst);
7979
7980 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7981 {
7982 int i;
7983 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7984 struct dwarf2_per_cu_data *iter;
7985
7986 /* Fill in 'dependencies' here; we fill in 'users' in a
7987 post-pass. */
7988 pst->number_of_dependencies = len;
7989 pst->dependencies =
7990 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
7991 for (i = 0;
7992 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7993 i, iter);
7994 ++i)
7995 pst->dependencies[i] = iter->v.psymtab;
7996
7997 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7998 }
7999
8000 /* Get the list of files included in the current compilation unit,
8001 and build a psymtab for each of them. */
8002 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8003
8004 if (dwarf_read_debug)
8005 {
8006 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8007
8008 fprintf_unfiltered (gdb_stdlog,
8009 "Psymtab for %s unit @%s: %s - %s"
8010 ", %d global, %d static syms\n",
8011 per_cu->is_debug_types ? "type" : "comp",
8012 sect_offset_str (per_cu->sect_off),
8013 paddress (gdbarch, pst->text_low (objfile)),
8014 paddress (gdbarch, pst->text_high (objfile)),
8015 pst->n_global_syms, pst->n_static_syms);
8016 }
8017 }
8018
8019 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8020 Process compilation unit THIS_CU for a psymtab. */
8021
8022 static void
8023 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8024 int want_partial_unit,
8025 enum language pretend_language)
8026 {
8027 /* If this compilation unit was already read in, free the
8028 cached copy in order to read it in again. This is
8029 necessary because we skipped some symbols when we first
8030 read in the compilation unit (see load_partial_dies).
8031 This problem could be avoided, but the benefit is unclear. */
8032 if (this_cu->cu != NULL)
8033 free_one_cached_comp_unit (this_cu);
8034
8035 if (this_cu->is_debug_types)
8036 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8037 build_type_psymtabs_reader, NULL);
8038 else
8039 {
8040 process_psymtab_comp_unit_data info;
8041 info.want_partial_unit = want_partial_unit;
8042 info.pretend_language = pretend_language;
8043 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8044 process_psymtab_comp_unit_reader, &info);
8045 }
8046
8047 /* Age out any secondary CUs. */
8048 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8049 }
8050
8051 /* Reader function for build_type_psymtabs. */
8052
8053 static void
8054 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8055 const gdb_byte *info_ptr,
8056 struct die_info *type_unit_die,
8057 int has_children,
8058 void *data)
8059 {
8060 struct dwarf2_per_objfile *dwarf2_per_objfile
8061 = reader->cu->per_cu->dwarf2_per_objfile;
8062 struct objfile *objfile = dwarf2_per_objfile->objfile;
8063 struct dwarf2_cu *cu = reader->cu;
8064 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8065 struct signatured_type *sig_type;
8066 struct type_unit_group *tu_group;
8067 struct attribute *attr;
8068 struct partial_die_info *first_die;
8069 CORE_ADDR lowpc, highpc;
8070 struct partial_symtab *pst;
8071
8072 gdb_assert (data == NULL);
8073 gdb_assert (per_cu->is_debug_types);
8074 sig_type = (struct signatured_type *) per_cu;
8075
8076 if (! has_children)
8077 return;
8078
8079 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8080 tu_group = get_type_unit_group (cu, attr);
8081
8082 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8083
8084 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8085 pst = create_partial_symtab (per_cu, "");
8086 pst->anonymous = 1;
8087
8088 first_die = load_partial_dies (reader, info_ptr, 1);
8089
8090 lowpc = (CORE_ADDR) -1;
8091 highpc = (CORE_ADDR) 0;
8092 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8093
8094 end_psymtab_common (objfile, pst);
8095 }
8096
8097 /* Struct used to sort TUs by their abbreviation table offset. */
8098
8099 struct tu_abbrev_offset
8100 {
8101 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8102 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8103 {}
8104
8105 signatured_type *sig_type;
8106 sect_offset abbrev_offset;
8107 };
8108
8109 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8110
8111 static bool
8112 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8113 const struct tu_abbrev_offset &b)
8114 {
8115 return a.abbrev_offset < b.abbrev_offset;
8116 }
8117
8118 /* Efficiently read all the type units.
8119 This does the bulk of the work for build_type_psymtabs.
8120
8121 The efficiency is because we sort TUs by the abbrev table they use and
8122 only read each abbrev table once. In one program there are 200K TUs
8123 sharing 8K abbrev tables.
8124
8125 The main purpose of this function is to support building the
8126 dwarf2_per_objfile->type_unit_groups table.
8127 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8128 can collapse the search space by grouping them by stmt_list.
8129 The savings can be significant, in the same program from above the 200K TUs
8130 share 8K stmt_list tables.
8131
8132 FUNC is expected to call get_type_unit_group, which will create the
8133 struct type_unit_group if necessary and add it to
8134 dwarf2_per_objfile->type_unit_groups. */
8135
8136 static void
8137 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8138 {
8139 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8140 abbrev_table_up abbrev_table;
8141 sect_offset abbrev_offset;
8142
8143 /* It's up to the caller to not call us multiple times. */
8144 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8145
8146 if (dwarf2_per_objfile->all_type_units.empty ())
8147 return;
8148
8149 /* TUs typically share abbrev tables, and there can be way more TUs than
8150 abbrev tables. Sort by abbrev table to reduce the number of times we
8151 read each abbrev table in.
8152 Alternatives are to punt or to maintain a cache of abbrev tables.
8153 This is simpler and efficient enough for now.
8154
8155 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8156 symtab to use). Typically TUs with the same abbrev offset have the same
8157 stmt_list value too so in practice this should work well.
8158
8159 The basic algorithm here is:
8160
8161 sort TUs by abbrev table
8162 for each TU with same abbrev table:
8163 read abbrev table if first user
8164 read TU top level DIE
8165 [IWBN if DWO skeletons had DW_AT_stmt_list]
8166 call FUNC */
8167
8168 if (dwarf_read_debug)
8169 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8170
8171 /* Sort in a separate table to maintain the order of all_type_units
8172 for .gdb_index: TU indices directly index all_type_units. */
8173 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8174 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8175
8176 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8177 sorted_by_abbrev.emplace_back
8178 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8179 sig_type->per_cu.section,
8180 sig_type->per_cu.sect_off));
8181
8182 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8183 sort_tu_by_abbrev_offset);
8184
8185 abbrev_offset = (sect_offset) ~(unsigned) 0;
8186
8187 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8188 {
8189 /* Switch to the next abbrev table if necessary. */
8190 if (abbrev_table == NULL
8191 || tu.abbrev_offset != abbrev_offset)
8192 {
8193 abbrev_offset = tu.abbrev_offset;
8194 abbrev_table =
8195 abbrev_table_read_table (dwarf2_per_objfile,
8196 &dwarf2_per_objfile->abbrev,
8197 abbrev_offset);
8198 ++tu_stats->nr_uniq_abbrev_tables;
8199 }
8200
8201 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8202 0, 0, false, build_type_psymtabs_reader, NULL);
8203 }
8204 }
8205
8206 /* Print collected type unit statistics. */
8207
8208 static void
8209 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8210 {
8211 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8212
8213 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8214 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8215 dwarf2_per_objfile->all_type_units.size ());
8216 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8217 tu_stats->nr_uniq_abbrev_tables);
8218 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8219 tu_stats->nr_symtabs);
8220 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8221 tu_stats->nr_symtab_sharers);
8222 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8223 tu_stats->nr_stmt_less_type_units);
8224 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8225 tu_stats->nr_all_type_units_reallocs);
8226 }
8227
8228 /* Traversal function for build_type_psymtabs. */
8229
8230 static int
8231 build_type_psymtab_dependencies (void **slot, void *info)
8232 {
8233 struct dwarf2_per_objfile *dwarf2_per_objfile
8234 = (struct dwarf2_per_objfile *) info;
8235 struct objfile *objfile = dwarf2_per_objfile->objfile;
8236 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8237 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8238 struct partial_symtab *pst = per_cu->v.psymtab;
8239 int len = VEC_length (sig_type_ptr, tu_group->tus);
8240 struct signatured_type *iter;
8241 int i;
8242
8243 gdb_assert (len > 0);
8244 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8245
8246 pst->number_of_dependencies = len;
8247 pst->dependencies =
8248 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8249 for (i = 0;
8250 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8251 ++i)
8252 {
8253 gdb_assert (iter->per_cu.is_debug_types);
8254 pst->dependencies[i] = iter->per_cu.v.psymtab;
8255 iter->type_unit_group = tu_group;
8256 }
8257
8258 VEC_free (sig_type_ptr, tu_group->tus);
8259
8260 return 1;
8261 }
8262
8263 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8264 Build partial symbol tables for the .debug_types comp-units. */
8265
8266 static void
8267 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8268 {
8269 if (! create_all_type_units (dwarf2_per_objfile))
8270 return;
8271
8272 build_type_psymtabs_1 (dwarf2_per_objfile);
8273 }
8274
8275 /* Traversal function for process_skeletonless_type_unit.
8276 Read a TU in a DWO file and build partial symbols for it. */
8277
8278 static int
8279 process_skeletonless_type_unit (void **slot, void *info)
8280 {
8281 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8282 struct dwarf2_per_objfile *dwarf2_per_objfile
8283 = (struct dwarf2_per_objfile *) info;
8284 struct signatured_type find_entry, *entry;
8285
8286 /* If this TU doesn't exist in the global table, add it and read it in. */
8287
8288 if (dwarf2_per_objfile->signatured_types == NULL)
8289 {
8290 dwarf2_per_objfile->signatured_types
8291 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8292 }
8293
8294 find_entry.signature = dwo_unit->signature;
8295 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8296 INSERT);
8297 /* If we've already seen this type there's nothing to do. What's happening
8298 is we're doing our own version of comdat-folding here. */
8299 if (*slot != NULL)
8300 return 1;
8301
8302 /* This does the job that create_all_type_units would have done for
8303 this TU. */
8304 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8305 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8306 *slot = entry;
8307
8308 /* This does the job that build_type_psymtabs_1 would have done. */
8309 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8310 build_type_psymtabs_reader, NULL);
8311
8312 return 1;
8313 }
8314
8315 /* Traversal function for process_skeletonless_type_units. */
8316
8317 static int
8318 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8319 {
8320 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8321
8322 if (dwo_file->tus != NULL)
8323 {
8324 htab_traverse_noresize (dwo_file->tus,
8325 process_skeletonless_type_unit, info);
8326 }
8327
8328 return 1;
8329 }
8330
8331 /* Scan all TUs of DWO files, verifying we've processed them.
8332 This is needed in case a TU was emitted without its skeleton.
8333 Note: This can't be done until we know what all the DWO files are. */
8334
8335 static void
8336 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8337 {
8338 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8339 if (get_dwp_file (dwarf2_per_objfile) == NULL
8340 && dwarf2_per_objfile->dwo_files != NULL)
8341 {
8342 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8343 process_dwo_file_for_skeletonless_type_units,
8344 dwarf2_per_objfile);
8345 }
8346 }
8347
8348 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8349
8350 static void
8351 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8352 {
8353 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8354 {
8355 struct partial_symtab *pst = per_cu->v.psymtab;
8356
8357 if (pst == NULL)
8358 continue;
8359
8360 for (int j = 0; j < pst->number_of_dependencies; ++j)
8361 {
8362 /* Set the 'user' field only if it is not already set. */
8363 if (pst->dependencies[j]->user == NULL)
8364 pst->dependencies[j]->user = pst;
8365 }
8366 }
8367 }
8368
8369 /* Build the partial symbol table by doing a quick pass through the
8370 .debug_info and .debug_abbrev sections. */
8371
8372 static void
8373 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8374 {
8375 struct objfile *objfile = dwarf2_per_objfile->objfile;
8376
8377 if (dwarf_read_debug)
8378 {
8379 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8380 objfile_name (objfile));
8381 }
8382
8383 dwarf2_per_objfile->reading_partial_symbols = 1;
8384
8385 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8386
8387 /* Any cached compilation units will be linked by the per-objfile
8388 read_in_chain. Make sure to free them when we're done. */
8389 free_cached_comp_units freer (dwarf2_per_objfile);
8390
8391 build_type_psymtabs (dwarf2_per_objfile);
8392
8393 create_all_comp_units (dwarf2_per_objfile);
8394
8395 /* Create a temporary address map on a temporary obstack. We later
8396 copy this to the final obstack. */
8397 auto_obstack temp_obstack;
8398
8399 scoped_restore save_psymtabs_addrmap
8400 = make_scoped_restore (&objfile->psymtabs_addrmap,
8401 addrmap_create_mutable (&temp_obstack));
8402
8403 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8404 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8405
8406 /* This has to wait until we read the CUs, we need the list of DWOs. */
8407 process_skeletonless_type_units (dwarf2_per_objfile);
8408
8409 /* Now that all TUs have been processed we can fill in the dependencies. */
8410 if (dwarf2_per_objfile->type_unit_groups != NULL)
8411 {
8412 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8413 build_type_psymtab_dependencies, dwarf2_per_objfile);
8414 }
8415
8416 if (dwarf_read_debug)
8417 print_tu_stats (dwarf2_per_objfile);
8418
8419 set_partial_user (dwarf2_per_objfile);
8420
8421 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8422 &objfile->objfile_obstack);
8423 /* At this point we want to keep the address map. */
8424 save_psymtabs_addrmap.release ();
8425
8426 if (dwarf_read_debug)
8427 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8428 objfile_name (objfile));
8429 }
8430
8431 /* die_reader_func for load_partial_comp_unit. */
8432
8433 static void
8434 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8435 const gdb_byte *info_ptr,
8436 struct die_info *comp_unit_die,
8437 int has_children,
8438 void *data)
8439 {
8440 struct dwarf2_cu *cu = reader->cu;
8441
8442 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8443
8444 /* Check if comp unit has_children.
8445 If so, read the rest of the partial symbols from this comp unit.
8446 If not, there's no more debug_info for this comp unit. */
8447 if (has_children)
8448 load_partial_dies (reader, info_ptr, 0);
8449 }
8450
8451 /* Load the partial DIEs for a secondary CU into memory.
8452 This is also used when rereading a primary CU with load_all_dies. */
8453
8454 static void
8455 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8456 {
8457 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8458 load_partial_comp_unit_reader, NULL);
8459 }
8460
8461 static void
8462 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8463 struct dwarf2_section_info *section,
8464 struct dwarf2_section_info *abbrev_section,
8465 unsigned int is_dwz)
8466 {
8467 const gdb_byte *info_ptr;
8468 struct objfile *objfile = dwarf2_per_objfile->objfile;
8469
8470 if (dwarf_read_debug)
8471 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8472 get_section_name (section),
8473 get_section_file_name (section));
8474
8475 dwarf2_read_section (objfile, section);
8476
8477 info_ptr = section->buffer;
8478
8479 while (info_ptr < section->buffer + section->size)
8480 {
8481 struct dwarf2_per_cu_data *this_cu;
8482
8483 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8484
8485 comp_unit_head cu_header;
8486 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8487 abbrev_section, info_ptr,
8488 rcuh_kind::COMPILE);
8489
8490 /* Save the compilation unit for later lookup. */
8491 if (cu_header.unit_type != DW_UT_type)
8492 {
8493 this_cu = XOBNEW (&objfile->objfile_obstack,
8494 struct dwarf2_per_cu_data);
8495 memset (this_cu, 0, sizeof (*this_cu));
8496 }
8497 else
8498 {
8499 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8500 struct signatured_type);
8501 memset (sig_type, 0, sizeof (*sig_type));
8502 sig_type->signature = cu_header.signature;
8503 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8504 this_cu = &sig_type->per_cu;
8505 }
8506 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8507 this_cu->sect_off = sect_off;
8508 this_cu->length = cu_header.length + cu_header.initial_length_size;
8509 this_cu->is_dwz = is_dwz;
8510 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8511 this_cu->section = section;
8512
8513 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8514
8515 info_ptr = info_ptr + this_cu->length;
8516 }
8517 }
8518
8519 /* Create a list of all compilation units in OBJFILE.
8520 This is only done for -readnow and building partial symtabs. */
8521
8522 static void
8523 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8524 {
8525 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8526 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8527 &dwarf2_per_objfile->abbrev, 0);
8528
8529 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8530 if (dwz != NULL)
8531 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8532 1);
8533 }
8534
8535 /* Process all loaded DIEs for compilation unit CU, starting at
8536 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8537 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8538 DW_AT_ranges). See the comments of add_partial_subprogram on how
8539 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8540
8541 static void
8542 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8543 CORE_ADDR *highpc, int set_addrmap,
8544 struct dwarf2_cu *cu)
8545 {
8546 struct partial_die_info *pdi;
8547
8548 /* Now, march along the PDI's, descending into ones which have
8549 interesting children but skipping the children of the other ones,
8550 until we reach the end of the compilation unit. */
8551
8552 pdi = first_die;
8553
8554 while (pdi != NULL)
8555 {
8556 pdi->fixup (cu);
8557
8558 /* Anonymous namespaces or modules have no name but have interesting
8559 children, so we need to look at them. Ditto for anonymous
8560 enums. */
8561
8562 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8563 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8564 || pdi->tag == DW_TAG_imported_unit
8565 || pdi->tag == DW_TAG_inlined_subroutine)
8566 {
8567 switch (pdi->tag)
8568 {
8569 case DW_TAG_subprogram:
8570 case DW_TAG_inlined_subroutine:
8571 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8572 break;
8573 case DW_TAG_constant:
8574 case DW_TAG_variable:
8575 case DW_TAG_typedef:
8576 case DW_TAG_union_type:
8577 if (!pdi->is_declaration)
8578 {
8579 add_partial_symbol (pdi, cu);
8580 }
8581 break;
8582 case DW_TAG_class_type:
8583 case DW_TAG_interface_type:
8584 case DW_TAG_structure_type:
8585 if (!pdi->is_declaration)
8586 {
8587 add_partial_symbol (pdi, cu);
8588 }
8589 if ((cu->language == language_rust
8590 || cu->language == language_cplus) && pdi->has_children)
8591 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8592 set_addrmap, cu);
8593 break;
8594 case DW_TAG_enumeration_type:
8595 if (!pdi->is_declaration)
8596 add_partial_enumeration (pdi, cu);
8597 break;
8598 case DW_TAG_base_type:
8599 case DW_TAG_subrange_type:
8600 /* File scope base type definitions are added to the partial
8601 symbol table. */
8602 add_partial_symbol (pdi, cu);
8603 break;
8604 case DW_TAG_namespace:
8605 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8606 break;
8607 case DW_TAG_module:
8608 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8609 break;
8610 case DW_TAG_imported_unit:
8611 {
8612 struct dwarf2_per_cu_data *per_cu;
8613
8614 /* For now we don't handle imported units in type units. */
8615 if (cu->per_cu->is_debug_types)
8616 {
8617 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8618 " supported in type units [in module %s]"),
8619 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8620 }
8621
8622 per_cu = dwarf2_find_containing_comp_unit
8623 (pdi->d.sect_off, pdi->is_dwz,
8624 cu->per_cu->dwarf2_per_objfile);
8625
8626 /* Go read the partial unit, if needed. */
8627 if (per_cu->v.psymtab == NULL)
8628 process_psymtab_comp_unit (per_cu, 1, cu->language);
8629
8630 VEC_safe_push (dwarf2_per_cu_ptr,
8631 cu->per_cu->imported_symtabs, per_cu);
8632 }
8633 break;
8634 case DW_TAG_imported_declaration:
8635 add_partial_symbol (pdi, cu);
8636 break;
8637 default:
8638 break;
8639 }
8640 }
8641
8642 /* If the die has a sibling, skip to the sibling. */
8643
8644 pdi = pdi->die_sibling;
8645 }
8646 }
8647
8648 /* Functions used to compute the fully scoped name of a partial DIE.
8649
8650 Normally, this is simple. For C++, the parent DIE's fully scoped
8651 name is concatenated with "::" and the partial DIE's name.
8652 Enumerators are an exception; they use the scope of their parent
8653 enumeration type, i.e. the name of the enumeration type is not
8654 prepended to the enumerator.
8655
8656 There are two complexities. One is DW_AT_specification; in this
8657 case "parent" means the parent of the target of the specification,
8658 instead of the direct parent of the DIE. The other is compilers
8659 which do not emit DW_TAG_namespace; in this case we try to guess
8660 the fully qualified name of structure types from their members'
8661 linkage names. This must be done using the DIE's children rather
8662 than the children of any DW_AT_specification target. We only need
8663 to do this for structures at the top level, i.e. if the target of
8664 any DW_AT_specification (if any; otherwise the DIE itself) does not
8665 have a parent. */
8666
8667 /* Compute the scope prefix associated with PDI's parent, in
8668 compilation unit CU. The result will be allocated on CU's
8669 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8670 field. NULL is returned if no prefix is necessary. */
8671 static const char *
8672 partial_die_parent_scope (struct partial_die_info *pdi,
8673 struct dwarf2_cu *cu)
8674 {
8675 const char *grandparent_scope;
8676 struct partial_die_info *parent, *real_pdi;
8677
8678 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8679 then this means the parent of the specification DIE. */
8680
8681 real_pdi = pdi;
8682 while (real_pdi->has_specification)
8683 real_pdi = find_partial_die (real_pdi->spec_offset,
8684 real_pdi->spec_is_dwz, cu);
8685
8686 parent = real_pdi->die_parent;
8687 if (parent == NULL)
8688 return NULL;
8689
8690 if (parent->scope_set)
8691 return parent->scope;
8692
8693 parent->fixup (cu);
8694
8695 grandparent_scope = partial_die_parent_scope (parent, cu);
8696
8697 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8698 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8699 Work around this problem here. */
8700 if (cu->language == language_cplus
8701 && parent->tag == DW_TAG_namespace
8702 && strcmp (parent->name, "::") == 0
8703 && grandparent_scope == NULL)
8704 {
8705 parent->scope = NULL;
8706 parent->scope_set = 1;
8707 return NULL;
8708 }
8709
8710 if (pdi->tag == DW_TAG_enumerator)
8711 /* Enumerators should not get the name of the enumeration as a prefix. */
8712 parent->scope = grandparent_scope;
8713 else if (parent->tag == DW_TAG_namespace
8714 || parent->tag == DW_TAG_module
8715 || parent->tag == DW_TAG_structure_type
8716 || parent->tag == DW_TAG_class_type
8717 || parent->tag == DW_TAG_interface_type
8718 || parent->tag == DW_TAG_union_type
8719 || parent->tag == DW_TAG_enumeration_type)
8720 {
8721 if (grandparent_scope == NULL)
8722 parent->scope = parent->name;
8723 else
8724 parent->scope = typename_concat (&cu->comp_unit_obstack,
8725 grandparent_scope,
8726 parent->name, 0, cu);
8727 }
8728 else
8729 {
8730 /* FIXME drow/2004-04-01: What should we be doing with
8731 function-local names? For partial symbols, we should probably be
8732 ignoring them. */
8733 complaint (_("unhandled containing DIE tag %d for DIE at %s"),
8734 parent->tag, sect_offset_str (pdi->sect_off));
8735 parent->scope = grandparent_scope;
8736 }
8737
8738 parent->scope_set = 1;
8739 return parent->scope;
8740 }
8741
8742 /* Return the fully scoped name associated with PDI, from compilation unit
8743 CU. The result will be allocated with malloc. */
8744
8745 static char *
8746 partial_die_full_name (struct partial_die_info *pdi,
8747 struct dwarf2_cu *cu)
8748 {
8749 const char *parent_scope;
8750
8751 /* If this is a template instantiation, we can not work out the
8752 template arguments from partial DIEs. So, unfortunately, we have
8753 to go through the full DIEs. At least any work we do building
8754 types here will be reused if full symbols are loaded later. */
8755 if (pdi->has_template_arguments)
8756 {
8757 pdi->fixup (cu);
8758
8759 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8760 {
8761 struct die_info *die;
8762 struct attribute attr;
8763 struct dwarf2_cu *ref_cu = cu;
8764
8765 /* DW_FORM_ref_addr is using section offset. */
8766 attr.name = (enum dwarf_attribute) 0;
8767 attr.form = DW_FORM_ref_addr;
8768 attr.u.unsnd = to_underlying (pdi->sect_off);
8769 die = follow_die_ref (NULL, &attr, &ref_cu);
8770
8771 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8772 }
8773 }
8774
8775 parent_scope = partial_die_parent_scope (pdi, cu);
8776 if (parent_scope == NULL)
8777 return NULL;
8778 else
8779 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8780 }
8781
8782 static void
8783 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8784 {
8785 struct dwarf2_per_objfile *dwarf2_per_objfile
8786 = cu->per_cu->dwarf2_per_objfile;
8787 struct objfile *objfile = dwarf2_per_objfile->objfile;
8788 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8789 CORE_ADDR addr = 0;
8790 const char *actual_name = NULL;
8791 CORE_ADDR baseaddr;
8792 char *built_actual_name;
8793
8794 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8795
8796 built_actual_name = partial_die_full_name (pdi, cu);
8797 if (built_actual_name != NULL)
8798 actual_name = built_actual_name;
8799
8800 if (actual_name == NULL)
8801 actual_name = pdi->name;
8802
8803 switch (pdi->tag)
8804 {
8805 case DW_TAG_inlined_subroutine:
8806 case DW_TAG_subprogram:
8807 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8808 - baseaddr);
8809 if (pdi->is_external || cu->language == language_ada)
8810 {
8811 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8812 of the global scope. But in Ada, we want to be able to access
8813 nested procedures globally. So all Ada subprograms are stored
8814 in the global scope. */
8815 add_psymbol_to_list (actual_name, strlen (actual_name),
8816 built_actual_name != NULL,
8817 VAR_DOMAIN, LOC_BLOCK,
8818 SECT_OFF_TEXT (objfile),
8819 &objfile->global_psymbols,
8820 addr,
8821 cu->language, objfile);
8822 }
8823 else
8824 {
8825 add_psymbol_to_list (actual_name, strlen (actual_name),
8826 built_actual_name != NULL,
8827 VAR_DOMAIN, LOC_BLOCK,
8828 SECT_OFF_TEXT (objfile),
8829 &objfile->static_psymbols,
8830 addr, cu->language, objfile);
8831 }
8832
8833 if (pdi->main_subprogram && actual_name != NULL)
8834 set_objfile_main_name (objfile, actual_name, cu->language);
8835 break;
8836 case DW_TAG_constant:
8837 {
8838 std::vector<partial_symbol *> *list;
8839
8840 if (pdi->is_external)
8841 list = &objfile->global_psymbols;
8842 else
8843 list = &objfile->static_psymbols;
8844 add_psymbol_to_list (actual_name, strlen (actual_name),
8845 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8846 -1, list, 0, cu->language, objfile);
8847 }
8848 break;
8849 case DW_TAG_variable:
8850 if (pdi->d.locdesc)
8851 addr = decode_locdesc (pdi->d.locdesc, cu);
8852
8853 if (pdi->d.locdesc
8854 && addr == 0
8855 && !dwarf2_per_objfile->has_section_at_zero)
8856 {
8857 /* A global or static variable may also have been stripped
8858 out by the linker if unused, in which case its address
8859 will be nullified; do not add such variables into partial
8860 symbol table then. */
8861 }
8862 else if (pdi->is_external)
8863 {
8864 /* Global Variable.
8865 Don't enter into the minimal symbol tables as there is
8866 a minimal symbol table entry from the ELF symbols already.
8867 Enter into partial symbol table if it has a location
8868 descriptor or a type.
8869 If the location descriptor is missing, new_symbol will create
8870 a LOC_UNRESOLVED symbol, the address of the variable will then
8871 be determined from the minimal symbol table whenever the variable
8872 is referenced.
8873 The address for the partial symbol table entry is not
8874 used by GDB, but it comes in handy for debugging partial symbol
8875 table building. */
8876
8877 if (pdi->d.locdesc || pdi->has_type)
8878 add_psymbol_to_list (actual_name, strlen (actual_name),
8879 built_actual_name != NULL,
8880 VAR_DOMAIN, LOC_STATIC,
8881 SECT_OFF_TEXT (objfile),
8882 &objfile->global_psymbols,
8883 addr, cu->language, objfile);
8884 }
8885 else
8886 {
8887 int has_loc = pdi->d.locdesc != NULL;
8888
8889 /* Static Variable. Skip symbols whose value we cannot know (those
8890 without location descriptors or constant values). */
8891 if (!has_loc && !pdi->has_const_value)
8892 {
8893 xfree (built_actual_name);
8894 return;
8895 }
8896
8897 add_psymbol_to_list (actual_name, strlen (actual_name),
8898 built_actual_name != NULL,
8899 VAR_DOMAIN, LOC_STATIC,
8900 SECT_OFF_TEXT (objfile),
8901 &objfile->static_psymbols,
8902 has_loc ? addr : 0,
8903 cu->language, objfile);
8904 }
8905 break;
8906 case DW_TAG_typedef:
8907 case DW_TAG_base_type:
8908 case DW_TAG_subrange_type:
8909 add_psymbol_to_list (actual_name, strlen (actual_name),
8910 built_actual_name != NULL,
8911 VAR_DOMAIN, LOC_TYPEDEF, -1,
8912 &objfile->static_psymbols,
8913 0, cu->language, objfile);
8914 break;
8915 case DW_TAG_imported_declaration:
8916 case DW_TAG_namespace:
8917 add_psymbol_to_list (actual_name, strlen (actual_name),
8918 built_actual_name != NULL,
8919 VAR_DOMAIN, LOC_TYPEDEF, -1,
8920 &objfile->global_psymbols,
8921 0, cu->language, objfile);
8922 break;
8923 case DW_TAG_module:
8924 add_psymbol_to_list (actual_name, strlen (actual_name),
8925 built_actual_name != NULL,
8926 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8927 &objfile->global_psymbols,
8928 0, cu->language, objfile);
8929 break;
8930 case DW_TAG_class_type:
8931 case DW_TAG_interface_type:
8932 case DW_TAG_structure_type:
8933 case DW_TAG_union_type:
8934 case DW_TAG_enumeration_type:
8935 /* Skip external references. The DWARF standard says in the section
8936 about "Structure, Union, and Class Type Entries": "An incomplete
8937 structure, union or class type is represented by a structure,
8938 union or class entry that does not have a byte size attribute
8939 and that has a DW_AT_declaration attribute." */
8940 if (!pdi->has_byte_size && pdi->is_declaration)
8941 {
8942 xfree (built_actual_name);
8943 return;
8944 }
8945
8946 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8947 static vs. global. */
8948 add_psymbol_to_list (actual_name, strlen (actual_name),
8949 built_actual_name != NULL,
8950 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8951 cu->language == language_cplus
8952 ? &objfile->global_psymbols
8953 : &objfile->static_psymbols,
8954 0, cu->language, objfile);
8955
8956 break;
8957 case DW_TAG_enumerator:
8958 add_psymbol_to_list (actual_name, strlen (actual_name),
8959 built_actual_name != NULL,
8960 VAR_DOMAIN, LOC_CONST, -1,
8961 cu->language == language_cplus
8962 ? &objfile->global_psymbols
8963 : &objfile->static_psymbols,
8964 0, cu->language, objfile);
8965 break;
8966 default:
8967 break;
8968 }
8969
8970 xfree (built_actual_name);
8971 }
8972
8973 /* Read a partial die corresponding to a namespace; also, add a symbol
8974 corresponding to that namespace to the symbol table. NAMESPACE is
8975 the name of the enclosing namespace. */
8976
8977 static void
8978 add_partial_namespace (struct partial_die_info *pdi,
8979 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8980 int set_addrmap, struct dwarf2_cu *cu)
8981 {
8982 /* Add a symbol for the namespace. */
8983
8984 add_partial_symbol (pdi, cu);
8985
8986 /* Now scan partial symbols in that namespace. */
8987
8988 if (pdi->has_children)
8989 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8990 }
8991
8992 /* Read a partial die corresponding to a Fortran module. */
8993
8994 static void
8995 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8996 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8997 {
8998 /* Add a symbol for the namespace. */
8999
9000 add_partial_symbol (pdi, cu);
9001
9002 /* Now scan partial symbols in that module. */
9003
9004 if (pdi->has_children)
9005 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9006 }
9007
9008 /* Read a partial die corresponding to a subprogram or an inlined
9009 subprogram and create a partial symbol for that subprogram.
9010 When the CU language allows it, this routine also defines a partial
9011 symbol for each nested subprogram that this subprogram contains.
9012 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9013 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9014
9015 PDI may also be a lexical block, in which case we simply search
9016 recursively for subprograms defined inside that lexical block.
9017 Again, this is only performed when the CU language allows this
9018 type of definitions. */
9019
9020 static void
9021 add_partial_subprogram (struct partial_die_info *pdi,
9022 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9023 int set_addrmap, struct dwarf2_cu *cu)
9024 {
9025 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9026 {
9027 if (pdi->has_pc_info)
9028 {
9029 if (pdi->lowpc < *lowpc)
9030 *lowpc = pdi->lowpc;
9031 if (pdi->highpc > *highpc)
9032 *highpc = pdi->highpc;
9033 if (set_addrmap)
9034 {
9035 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9036 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9037 CORE_ADDR baseaddr;
9038 CORE_ADDR highpc;
9039 CORE_ADDR lowpc;
9040
9041 baseaddr = ANOFFSET (objfile->section_offsets,
9042 SECT_OFF_TEXT (objfile));
9043 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
9044 pdi->lowpc + baseaddr)
9045 - baseaddr);
9046 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
9047 pdi->highpc + baseaddr)
9048 - baseaddr);
9049 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9050 cu->per_cu->v.psymtab);
9051 }
9052 }
9053
9054 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9055 {
9056 if (!pdi->is_declaration)
9057 /* Ignore subprogram DIEs that do not have a name, they are
9058 illegal. Do not emit a complaint at this point, we will
9059 do so when we convert this psymtab into a symtab. */
9060 if (pdi->name)
9061 add_partial_symbol (pdi, cu);
9062 }
9063 }
9064
9065 if (! pdi->has_children)
9066 return;
9067
9068 if (cu->language == language_ada)
9069 {
9070 pdi = pdi->die_child;
9071 while (pdi != NULL)
9072 {
9073 pdi->fixup (cu);
9074 if (pdi->tag == DW_TAG_subprogram
9075 || pdi->tag == DW_TAG_inlined_subroutine
9076 || pdi->tag == DW_TAG_lexical_block)
9077 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9078 pdi = pdi->die_sibling;
9079 }
9080 }
9081 }
9082
9083 /* Read a partial die corresponding to an enumeration type. */
9084
9085 static void
9086 add_partial_enumeration (struct partial_die_info *enum_pdi,
9087 struct dwarf2_cu *cu)
9088 {
9089 struct partial_die_info *pdi;
9090
9091 if (enum_pdi->name != NULL)
9092 add_partial_symbol (enum_pdi, cu);
9093
9094 pdi = enum_pdi->die_child;
9095 while (pdi)
9096 {
9097 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9098 complaint (_("malformed enumerator DIE ignored"));
9099 else
9100 add_partial_symbol (pdi, cu);
9101 pdi = pdi->die_sibling;
9102 }
9103 }
9104
9105 /* Return the initial uleb128 in the die at INFO_PTR. */
9106
9107 static unsigned int
9108 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9109 {
9110 unsigned int bytes_read;
9111
9112 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9113 }
9114
9115 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9116 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9117
9118 Return the corresponding abbrev, or NULL if the number is zero (indicating
9119 an empty DIE). In either case *BYTES_READ will be set to the length of
9120 the initial number. */
9121
9122 static struct abbrev_info *
9123 peek_die_abbrev (const die_reader_specs &reader,
9124 const gdb_byte *info_ptr, unsigned int *bytes_read)
9125 {
9126 dwarf2_cu *cu = reader.cu;
9127 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9128 unsigned int abbrev_number
9129 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9130
9131 if (abbrev_number == 0)
9132 return NULL;
9133
9134 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9135 if (!abbrev)
9136 {
9137 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9138 " at offset %s [in module %s]"),
9139 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9140 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9141 }
9142
9143 return abbrev;
9144 }
9145
9146 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9147 Returns a pointer to the end of a series of DIEs, terminated by an empty
9148 DIE. Any children of the skipped DIEs will also be skipped. */
9149
9150 static const gdb_byte *
9151 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9152 {
9153 while (1)
9154 {
9155 unsigned int bytes_read;
9156 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9157
9158 if (abbrev == NULL)
9159 return info_ptr + bytes_read;
9160 else
9161 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9162 }
9163 }
9164
9165 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9166 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9167 abbrev corresponding to that skipped uleb128 should be passed in
9168 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9169 children. */
9170
9171 static const gdb_byte *
9172 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9173 struct abbrev_info *abbrev)
9174 {
9175 unsigned int bytes_read;
9176 struct attribute attr;
9177 bfd *abfd = reader->abfd;
9178 struct dwarf2_cu *cu = reader->cu;
9179 const gdb_byte *buffer = reader->buffer;
9180 const gdb_byte *buffer_end = reader->buffer_end;
9181 unsigned int form, i;
9182
9183 for (i = 0; i < abbrev->num_attrs; i++)
9184 {
9185 /* The only abbrev we care about is DW_AT_sibling. */
9186 if (abbrev->attrs[i].name == DW_AT_sibling)
9187 {
9188 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9189 if (attr.form == DW_FORM_ref_addr)
9190 complaint (_("ignoring absolute DW_AT_sibling"));
9191 else
9192 {
9193 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9194 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9195
9196 if (sibling_ptr < info_ptr)
9197 complaint (_("DW_AT_sibling points backwards"));
9198 else if (sibling_ptr > reader->buffer_end)
9199 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9200 else
9201 return sibling_ptr;
9202 }
9203 }
9204
9205 /* If it isn't DW_AT_sibling, skip this attribute. */
9206 form = abbrev->attrs[i].form;
9207 skip_attribute:
9208 switch (form)
9209 {
9210 case DW_FORM_ref_addr:
9211 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9212 and later it is offset sized. */
9213 if (cu->header.version == 2)
9214 info_ptr += cu->header.addr_size;
9215 else
9216 info_ptr += cu->header.offset_size;
9217 break;
9218 case DW_FORM_GNU_ref_alt:
9219 info_ptr += cu->header.offset_size;
9220 break;
9221 case DW_FORM_addr:
9222 info_ptr += cu->header.addr_size;
9223 break;
9224 case DW_FORM_data1:
9225 case DW_FORM_ref1:
9226 case DW_FORM_flag:
9227 info_ptr += 1;
9228 break;
9229 case DW_FORM_flag_present:
9230 case DW_FORM_implicit_const:
9231 break;
9232 case DW_FORM_data2:
9233 case DW_FORM_ref2:
9234 info_ptr += 2;
9235 break;
9236 case DW_FORM_data4:
9237 case DW_FORM_ref4:
9238 info_ptr += 4;
9239 break;
9240 case DW_FORM_data8:
9241 case DW_FORM_ref8:
9242 case DW_FORM_ref_sig8:
9243 info_ptr += 8;
9244 break;
9245 case DW_FORM_data16:
9246 info_ptr += 16;
9247 break;
9248 case DW_FORM_string:
9249 read_direct_string (abfd, info_ptr, &bytes_read);
9250 info_ptr += bytes_read;
9251 break;
9252 case DW_FORM_sec_offset:
9253 case DW_FORM_strp:
9254 case DW_FORM_GNU_strp_alt:
9255 info_ptr += cu->header.offset_size;
9256 break;
9257 case DW_FORM_exprloc:
9258 case DW_FORM_block:
9259 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9260 info_ptr += bytes_read;
9261 break;
9262 case DW_FORM_block1:
9263 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9264 break;
9265 case DW_FORM_block2:
9266 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9267 break;
9268 case DW_FORM_block4:
9269 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9270 break;
9271 case DW_FORM_sdata:
9272 case DW_FORM_udata:
9273 case DW_FORM_ref_udata:
9274 case DW_FORM_GNU_addr_index:
9275 case DW_FORM_GNU_str_index:
9276 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9277 break;
9278 case DW_FORM_indirect:
9279 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9280 info_ptr += bytes_read;
9281 /* We need to continue parsing from here, so just go back to
9282 the top. */
9283 goto skip_attribute;
9284
9285 default:
9286 error (_("Dwarf Error: Cannot handle %s "
9287 "in DWARF reader [in module %s]"),
9288 dwarf_form_name (form),
9289 bfd_get_filename (abfd));
9290 }
9291 }
9292
9293 if (abbrev->has_children)
9294 return skip_children (reader, info_ptr);
9295 else
9296 return info_ptr;
9297 }
9298
9299 /* Locate ORIG_PDI's sibling.
9300 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9301
9302 static const gdb_byte *
9303 locate_pdi_sibling (const struct die_reader_specs *reader,
9304 struct partial_die_info *orig_pdi,
9305 const gdb_byte *info_ptr)
9306 {
9307 /* Do we know the sibling already? */
9308
9309 if (orig_pdi->sibling)
9310 return orig_pdi->sibling;
9311
9312 /* Are there any children to deal with? */
9313
9314 if (!orig_pdi->has_children)
9315 return info_ptr;
9316
9317 /* Skip the children the long way. */
9318
9319 return skip_children (reader, info_ptr);
9320 }
9321
9322 /* Expand this partial symbol table into a full symbol table. SELF is
9323 not NULL. */
9324
9325 static void
9326 dwarf2_read_symtab (struct partial_symtab *self,
9327 struct objfile *objfile)
9328 {
9329 struct dwarf2_per_objfile *dwarf2_per_objfile
9330 = get_dwarf2_per_objfile (objfile);
9331
9332 if (self->readin)
9333 {
9334 warning (_("bug: psymtab for %s is already read in."),
9335 self->filename);
9336 }
9337 else
9338 {
9339 if (info_verbose)
9340 {
9341 printf_filtered (_("Reading in symbols for %s..."),
9342 self->filename);
9343 gdb_flush (gdb_stdout);
9344 }
9345
9346 /* If this psymtab is constructed from a debug-only objfile, the
9347 has_section_at_zero flag will not necessarily be correct. We
9348 can get the correct value for this flag by looking at the data
9349 associated with the (presumably stripped) associated objfile. */
9350 if (objfile->separate_debug_objfile_backlink)
9351 {
9352 struct dwarf2_per_objfile *dpo_backlink
9353 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9354
9355 dwarf2_per_objfile->has_section_at_zero
9356 = dpo_backlink->has_section_at_zero;
9357 }
9358
9359 dwarf2_per_objfile->reading_partial_symbols = 0;
9360
9361 psymtab_to_symtab_1 (self);
9362
9363 /* Finish up the debug error message. */
9364 if (info_verbose)
9365 printf_filtered (_("done.\n"));
9366 }
9367
9368 process_cu_includes (dwarf2_per_objfile);
9369 }
9370 \f
9371 /* Reading in full CUs. */
9372
9373 /* Add PER_CU to the queue. */
9374
9375 static void
9376 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9377 enum language pretend_language)
9378 {
9379 struct dwarf2_queue_item *item;
9380
9381 per_cu->queued = 1;
9382 item = XNEW (struct dwarf2_queue_item);
9383 item->per_cu = per_cu;
9384 item->pretend_language = pretend_language;
9385 item->next = NULL;
9386
9387 if (dwarf2_queue == NULL)
9388 dwarf2_queue = item;
9389 else
9390 dwarf2_queue_tail->next = item;
9391
9392 dwarf2_queue_tail = item;
9393 }
9394
9395 /* If PER_CU is not yet queued, add it to the queue.
9396 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9397 dependency.
9398 The result is non-zero if PER_CU was queued, otherwise the result is zero
9399 meaning either PER_CU is already queued or it is already loaded.
9400
9401 N.B. There is an invariant here that if a CU is queued then it is loaded.
9402 The caller is required to load PER_CU if we return non-zero. */
9403
9404 static int
9405 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9406 struct dwarf2_per_cu_data *per_cu,
9407 enum language pretend_language)
9408 {
9409 /* We may arrive here during partial symbol reading, if we need full
9410 DIEs to process an unusual case (e.g. template arguments). Do
9411 not queue PER_CU, just tell our caller to load its DIEs. */
9412 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9413 {
9414 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9415 return 1;
9416 return 0;
9417 }
9418
9419 /* Mark the dependence relation so that we don't flush PER_CU
9420 too early. */
9421 if (dependent_cu != NULL)
9422 dwarf2_add_dependence (dependent_cu, per_cu);
9423
9424 /* If it's already on the queue, we have nothing to do. */
9425 if (per_cu->queued)
9426 return 0;
9427
9428 /* If the compilation unit is already loaded, just mark it as
9429 used. */
9430 if (per_cu->cu != NULL)
9431 {
9432 per_cu->cu->last_used = 0;
9433 return 0;
9434 }
9435
9436 /* Add it to the queue. */
9437 queue_comp_unit (per_cu, pretend_language);
9438
9439 return 1;
9440 }
9441
9442 /* Process the queue. */
9443
9444 static void
9445 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9446 {
9447 struct dwarf2_queue_item *item, *next_item;
9448
9449 if (dwarf_read_debug)
9450 {
9451 fprintf_unfiltered (gdb_stdlog,
9452 "Expanding one or more symtabs of objfile %s ...\n",
9453 objfile_name (dwarf2_per_objfile->objfile));
9454 }
9455
9456 /* The queue starts out with one item, but following a DIE reference
9457 may load a new CU, adding it to the end of the queue. */
9458 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9459 {
9460 if ((dwarf2_per_objfile->using_index
9461 ? !item->per_cu->v.quick->compunit_symtab
9462 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9463 /* Skip dummy CUs. */
9464 && item->per_cu->cu != NULL)
9465 {
9466 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9467 unsigned int debug_print_threshold;
9468 char buf[100];
9469
9470 if (per_cu->is_debug_types)
9471 {
9472 struct signatured_type *sig_type =
9473 (struct signatured_type *) per_cu;
9474
9475 sprintf (buf, "TU %s at offset %s",
9476 hex_string (sig_type->signature),
9477 sect_offset_str (per_cu->sect_off));
9478 /* There can be 100s of TUs.
9479 Only print them in verbose mode. */
9480 debug_print_threshold = 2;
9481 }
9482 else
9483 {
9484 sprintf (buf, "CU at offset %s",
9485 sect_offset_str (per_cu->sect_off));
9486 debug_print_threshold = 1;
9487 }
9488
9489 if (dwarf_read_debug >= debug_print_threshold)
9490 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9491
9492 if (per_cu->is_debug_types)
9493 process_full_type_unit (per_cu, item->pretend_language);
9494 else
9495 process_full_comp_unit (per_cu, item->pretend_language);
9496
9497 if (dwarf_read_debug >= debug_print_threshold)
9498 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9499 }
9500
9501 item->per_cu->queued = 0;
9502 next_item = item->next;
9503 xfree (item);
9504 }
9505
9506 dwarf2_queue_tail = NULL;
9507
9508 if (dwarf_read_debug)
9509 {
9510 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9511 objfile_name (dwarf2_per_objfile->objfile));
9512 }
9513 }
9514
9515 /* Read in full symbols for PST, and anything it depends on. */
9516
9517 static void
9518 psymtab_to_symtab_1 (struct partial_symtab *pst)
9519 {
9520 struct dwarf2_per_cu_data *per_cu;
9521 int i;
9522
9523 if (pst->readin)
9524 return;
9525
9526 for (i = 0; i < pst->number_of_dependencies; i++)
9527 if (!pst->dependencies[i]->readin
9528 && pst->dependencies[i]->user == NULL)
9529 {
9530 /* Inform about additional files that need to be read in. */
9531 if (info_verbose)
9532 {
9533 /* FIXME: i18n: Need to make this a single string. */
9534 fputs_filtered (" ", gdb_stdout);
9535 wrap_here ("");
9536 fputs_filtered ("and ", gdb_stdout);
9537 wrap_here ("");
9538 printf_filtered ("%s...", pst->dependencies[i]->filename);
9539 wrap_here (""); /* Flush output. */
9540 gdb_flush (gdb_stdout);
9541 }
9542 psymtab_to_symtab_1 (pst->dependencies[i]);
9543 }
9544
9545 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9546
9547 if (per_cu == NULL)
9548 {
9549 /* It's an include file, no symbols to read for it.
9550 Everything is in the parent symtab. */
9551 pst->readin = 1;
9552 return;
9553 }
9554
9555 dw2_do_instantiate_symtab (per_cu, false);
9556 }
9557
9558 /* Trivial hash function for die_info: the hash value of a DIE
9559 is its offset in .debug_info for this objfile. */
9560
9561 static hashval_t
9562 die_hash (const void *item)
9563 {
9564 const struct die_info *die = (const struct die_info *) item;
9565
9566 return to_underlying (die->sect_off);
9567 }
9568
9569 /* Trivial comparison function for die_info structures: two DIEs
9570 are equal if they have the same offset. */
9571
9572 static int
9573 die_eq (const void *item_lhs, const void *item_rhs)
9574 {
9575 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9576 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9577
9578 return die_lhs->sect_off == die_rhs->sect_off;
9579 }
9580
9581 /* die_reader_func for load_full_comp_unit.
9582 This is identical to read_signatured_type_reader,
9583 but is kept separate for now. */
9584
9585 static void
9586 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9587 const gdb_byte *info_ptr,
9588 struct die_info *comp_unit_die,
9589 int has_children,
9590 void *data)
9591 {
9592 struct dwarf2_cu *cu = reader->cu;
9593 enum language *language_ptr = (enum language *) data;
9594
9595 gdb_assert (cu->die_hash == NULL);
9596 cu->die_hash =
9597 htab_create_alloc_ex (cu->header.length / 12,
9598 die_hash,
9599 die_eq,
9600 NULL,
9601 &cu->comp_unit_obstack,
9602 hashtab_obstack_allocate,
9603 dummy_obstack_deallocate);
9604
9605 if (has_children)
9606 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9607 &info_ptr, comp_unit_die);
9608 cu->dies = comp_unit_die;
9609 /* comp_unit_die is not stored in die_hash, no need. */
9610
9611 /* We try not to read any attributes in this function, because not
9612 all CUs needed for references have been loaded yet, and symbol
9613 table processing isn't initialized. But we have to set the CU language,
9614 or we won't be able to build types correctly.
9615 Similarly, if we do not read the producer, we can not apply
9616 producer-specific interpretation. */
9617 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9618 }
9619
9620 /* Load the DIEs associated with PER_CU into memory. */
9621
9622 static void
9623 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9624 bool skip_partial,
9625 enum language pretend_language)
9626 {
9627 gdb_assert (! this_cu->is_debug_types);
9628
9629 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9630 load_full_comp_unit_reader, &pretend_language);
9631 }
9632
9633 /* Add a DIE to the delayed physname list. */
9634
9635 static void
9636 add_to_method_list (struct type *type, int fnfield_index, int index,
9637 const char *name, struct die_info *die,
9638 struct dwarf2_cu *cu)
9639 {
9640 struct delayed_method_info mi;
9641 mi.type = type;
9642 mi.fnfield_index = fnfield_index;
9643 mi.index = index;
9644 mi.name = name;
9645 mi.die = die;
9646 cu->method_list.push_back (mi);
9647 }
9648
9649 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9650 "const" / "volatile". If so, decrements LEN by the length of the
9651 modifier and return true. Otherwise return false. */
9652
9653 template<size_t N>
9654 static bool
9655 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9656 {
9657 size_t mod_len = sizeof (mod) - 1;
9658 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9659 {
9660 len -= mod_len;
9661 return true;
9662 }
9663 return false;
9664 }
9665
9666 /* Compute the physnames of any methods on the CU's method list.
9667
9668 The computation of method physnames is delayed in order to avoid the
9669 (bad) condition that one of the method's formal parameters is of an as yet
9670 incomplete type. */
9671
9672 static void
9673 compute_delayed_physnames (struct dwarf2_cu *cu)
9674 {
9675 /* Only C++ delays computing physnames. */
9676 if (cu->method_list.empty ())
9677 return;
9678 gdb_assert (cu->language == language_cplus);
9679
9680 for (const delayed_method_info &mi : cu->method_list)
9681 {
9682 const char *physname;
9683 struct fn_fieldlist *fn_flp
9684 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9685 physname = dwarf2_physname (mi.name, mi.die, cu);
9686 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9687 = physname ? physname : "";
9688
9689 /* Since there's no tag to indicate whether a method is a
9690 const/volatile overload, extract that information out of the
9691 demangled name. */
9692 if (physname != NULL)
9693 {
9694 size_t len = strlen (physname);
9695
9696 while (1)
9697 {
9698 if (physname[len] == ')') /* shortcut */
9699 break;
9700 else if (check_modifier (physname, len, " const"))
9701 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9702 else if (check_modifier (physname, len, " volatile"))
9703 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9704 else
9705 break;
9706 }
9707 }
9708 }
9709
9710 /* The list is no longer needed. */
9711 cu->method_list.clear ();
9712 }
9713
9714 /* A wrapper for add_symbol_to_list to ensure that SYMBOL's language is
9715 the same as all other symbols in LISTHEAD. If a new symbol is added
9716 with a different language, this function asserts. */
9717
9718 static inline void
9719 dw2_add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
9720 {
9721 /* Only assert if LISTHEAD already contains symbols of a different
9722 language (dict_create_hashed/insert_symbol_hashed requires that all
9723 symbols in this list are of the same language). */
9724 gdb_assert ((*listhead) == NULL
9725 || (SYMBOL_LANGUAGE ((*listhead)->symbol[0])
9726 == SYMBOL_LANGUAGE (symbol)));
9727
9728 add_symbol_to_list (symbol, listhead);
9729 }
9730
9731 /* Go objects should be embedded in a DW_TAG_module DIE,
9732 and it's not clear if/how imported objects will appear.
9733 To keep Go support simple until that's worked out,
9734 go back through what we've read and create something usable.
9735 We could do this while processing each DIE, and feels kinda cleaner,
9736 but that way is more invasive.
9737 This is to, for example, allow the user to type "p var" or "b main"
9738 without having to specify the package name, and allow lookups
9739 of module.object to work in contexts that use the expression
9740 parser. */
9741
9742 static void
9743 fixup_go_packaging (struct dwarf2_cu *cu)
9744 {
9745 char *package_name = NULL;
9746 struct pending *list;
9747 int i;
9748
9749 for (list = *cu->builder->get_global_symbols ();
9750 list != NULL;
9751 list = list->next)
9752 {
9753 for (i = 0; i < list->nsyms; ++i)
9754 {
9755 struct symbol *sym = list->symbol[i];
9756
9757 if (SYMBOL_LANGUAGE (sym) == language_go
9758 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9759 {
9760 char *this_package_name = go_symbol_package_name (sym);
9761
9762 if (this_package_name == NULL)
9763 continue;
9764 if (package_name == NULL)
9765 package_name = this_package_name;
9766 else
9767 {
9768 struct objfile *objfile
9769 = cu->per_cu->dwarf2_per_objfile->objfile;
9770 if (strcmp (package_name, this_package_name) != 0)
9771 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9772 (symbol_symtab (sym) != NULL
9773 ? symtab_to_filename_for_display
9774 (symbol_symtab (sym))
9775 : objfile_name (objfile)),
9776 this_package_name, package_name);
9777 xfree (this_package_name);
9778 }
9779 }
9780 }
9781 }
9782
9783 if (package_name != NULL)
9784 {
9785 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9786 const char *saved_package_name
9787 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9788 package_name,
9789 strlen (package_name));
9790 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9791 saved_package_name);
9792 struct symbol *sym;
9793
9794 sym = allocate_symbol (objfile);
9795 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9796 SYMBOL_SET_NAMES (sym, saved_package_name,
9797 strlen (saved_package_name), 0, objfile);
9798 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9799 e.g., "main" finds the "main" module and not C's main(). */
9800 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9801 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9802 SYMBOL_TYPE (sym) = type;
9803
9804 dw2_add_symbol_to_list (sym, cu->builder->get_global_symbols ());
9805
9806 xfree (package_name);
9807 }
9808 }
9809
9810 /* Allocate a fully-qualified name consisting of the two parts on the
9811 obstack. */
9812
9813 static const char *
9814 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9815 {
9816 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9817 }
9818
9819 /* A helper that allocates a struct discriminant_info to attach to a
9820 union type. */
9821
9822 static struct discriminant_info *
9823 alloc_discriminant_info (struct type *type, int discriminant_index,
9824 int default_index)
9825 {
9826 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9827 gdb_assert (discriminant_index == -1
9828 || (discriminant_index >= 0
9829 && discriminant_index < TYPE_NFIELDS (type)));
9830 gdb_assert (default_index == -1
9831 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9832
9833 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9834
9835 struct discriminant_info *disc
9836 = ((struct discriminant_info *)
9837 TYPE_ZALLOC (type,
9838 offsetof (struct discriminant_info, discriminants)
9839 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9840 disc->default_index = default_index;
9841 disc->discriminant_index = discriminant_index;
9842
9843 struct dynamic_prop prop;
9844 prop.kind = PROP_UNDEFINED;
9845 prop.data.baton = disc;
9846
9847 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9848
9849 return disc;
9850 }
9851
9852 /* Some versions of rustc emitted enums in an unusual way.
9853
9854 Ordinary enums were emitted as unions. The first element of each
9855 structure in the union was named "RUST$ENUM$DISR". This element
9856 held the discriminant.
9857
9858 These versions of Rust also implemented the "non-zero"
9859 optimization. When the enum had two values, and one is empty and
9860 the other holds a pointer that cannot be zero, the pointer is used
9861 as the discriminant, with a zero value meaning the empty variant.
9862 Here, the union's first member is of the form
9863 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9864 where the fieldnos are the indices of the fields that should be
9865 traversed in order to find the field (which may be several fields deep)
9866 and the variantname is the name of the variant of the case when the
9867 field is zero.
9868
9869 This function recognizes whether TYPE is of one of these forms,
9870 and, if so, smashes it to be a variant type. */
9871
9872 static void
9873 quirk_rust_enum (struct type *type, struct objfile *objfile)
9874 {
9875 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9876
9877 /* We don't need to deal with empty enums. */
9878 if (TYPE_NFIELDS (type) == 0)
9879 return;
9880
9881 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9882 if (TYPE_NFIELDS (type) == 1
9883 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9884 {
9885 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9886
9887 /* Decode the field name to find the offset of the
9888 discriminant. */
9889 ULONGEST bit_offset = 0;
9890 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9891 while (name[0] >= '0' && name[0] <= '9')
9892 {
9893 char *tail;
9894 unsigned long index = strtoul (name, &tail, 10);
9895 name = tail;
9896 if (*name != '$'
9897 || index >= TYPE_NFIELDS (field_type)
9898 || (TYPE_FIELD_LOC_KIND (field_type, index)
9899 != FIELD_LOC_KIND_BITPOS))
9900 {
9901 complaint (_("Could not parse Rust enum encoding string \"%s\""
9902 "[in module %s]"),
9903 TYPE_FIELD_NAME (type, 0),
9904 objfile_name (objfile));
9905 return;
9906 }
9907 ++name;
9908
9909 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9910 field_type = TYPE_FIELD_TYPE (field_type, index);
9911 }
9912
9913 /* Make a union to hold the variants. */
9914 struct type *union_type = alloc_type (objfile);
9915 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9916 TYPE_NFIELDS (union_type) = 3;
9917 TYPE_FIELDS (union_type)
9918 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9919 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9920 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9921
9922 /* Put the discriminant must at index 0. */
9923 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9924 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9925 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9926 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9927
9928 /* The order of fields doesn't really matter, so put the real
9929 field at index 1 and the data-less field at index 2. */
9930 struct discriminant_info *disc
9931 = alloc_discriminant_info (union_type, 0, 1);
9932 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9933 TYPE_FIELD_NAME (union_type, 1)
9934 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9935 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9936 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9937 TYPE_FIELD_NAME (union_type, 1));
9938
9939 const char *dataless_name
9940 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9941 name);
9942 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9943 dataless_name);
9944 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9945 /* NAME points into the original discriminant name, which
9946 already has the correct lifetime. */
9947 TYPE_FIELD_NAME (union_type, 2) = name;
9948 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9949 disc->discriminants[2] = 0;
9950
9951 /* Smash this type to be a structure type. We have to do this
9952 because the type has already been recorded. */
9953 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9954 TYPE_NFIELDS (type) = 1;
9955 TYPE_FIELDS (type)
9956 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9957
9958 /* Install the variant part. */
9959 TYPE_FIELD_TYPE (type, 0) = union_type;
9960 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9961 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9962 }
9963 else if (TYPE_NFIELDS (type) == 1)
9964 {
9965 /* We assume that a union with a single field is a univariant
9966 enum. */
9967 /* Smash this type to be a structure type. We have to do this
9968 because the type has already been recorded. */
9969 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9970
9971 /* Make a union to hold the variants. */
9972 struct type *union_type = alloc_type (objfile);
9973 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9974 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9975 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9976 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9977 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9978
9979 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9980 const char *variant_name
9981 = rust_last_path_segment (TYPE_NAME (field_type));
9982 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9983 TYPE_NAME (field_type)
9984 = rust_fully_qualify (&objfile->objfile_obstack,
9985 TYPE_NAME (type), variant_name);
9986
9987 /* Install the union in the outer struct type. */
9988 TYPE_NFIELDS (type) = 1;
9989 TYPE_FIELDS (type)
9990 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9991 TYPE_FIELD_TYPE (type, 0) = union_type;
9992 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9993 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9994
9995 alloc_discriminant_info (union_type, -1, 0);
9996 }
9997 else
9998 {
9999 struct type *disr_type = nullptr;
10000 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10001 {
10002 disr_type = TYPE_FIELD_TYPE (type, i);
10003
10004 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10005 {
10006 /* All fields of a true enum will be structs. */
10007 return;
10008 }
10009 else if (TYPE_NFIELDS (disr_type) == 0)
10010 {
10011 /* Could be data-less variant, so keep going. */
10012 disr_type = nullptr;
10013 }
10014 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10015 "RUST$ENUM$DISR") != 0)
10016 {
10017 /* Not a Rust enum. */
10018 return;
10019 }
10020 else
10021 {
10022 /* Found one. */
10023 break;
10024 }
10025 }
10026
10027 /* If we got here without a discriminant, then it's probably
10028 just a union. */
10029 if (disr_type == nullptr)
10030 return;
10031
10032 /* Smash this type to be a structure type. We have to do this
10033 because the type has already been recorded. */
10034 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10035
10036 /* Make a union to hold the variants. */
10037 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10038 struct type *union_type = alloc_type (objfile);
10039 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10040 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10041 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10042 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10043 TYPE_FIELDS (union_type)
10044 = (struct field *) TYPE_ZALLOC (union_type,
10045 (TYPE_NFIELDS (union_type)
10046 * sizeof (struct field)));
10047
10048 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10049 TYPE_NFIELDS (type) * sizeof (struct field));
10050
10051 /* Install the discriminant at index 0 in the union. */
10052 TYPE_FIELD (union_type, 0) = *disr_field;
10053 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10054 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10055
10056 /* Install the union in the outer struct type. */
10057 TYPE_FIELD_TYPE (type, 0) = union_type;
10058 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10059 TYPE_NFIELDS (type) = 1;
10060
10061 /* Set the size and offset of the union type. */
10062 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10063
10064 /* We need a way to find the correct discriminant given a
10065 variant name. For convenience we build a map here. */
10066 struct type *enum_type = FIELD_TYPE (*disr_field);
10067 std::unordered_map<std::string, ULONGEST> discriminant_map;
10068 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10069 {
10070 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10071 {
10072 const char *name
10073 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10074 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10075 }
10076 }
10077
10078 int n_fields = TYPE_NFIELDS (union_type);
10079 struct discriminant_info *disc
10080 = alloc_discriminant_info (union_type, 0, -1);
10081 /* Skip the discriminant here. */
10082 for (int i = 1; i < n_fields; ++i)
10083 {
10084 /* Find the final word in the name of this variant's type.
10085 That name can be used to look up the correct
10086 discriminant. */
10087 const char *variant_name
10088 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10089 i)));
10090
10091 auto iter = discriminant_map.find (variant_name);
10092 if (iter != discriminant_map.end ())
10093 disc->discriminants[i] = iter->second;
10094
10095 /* Remove the discriminant field, if it exists. */
10096 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10097 if (TYPE_NFIELDS (sub_type) > 0)
10098 {
10099 --TYPE_NFIELDS (sub_type);
10100 ++TYPE_FIELDS (sub_type);
10101 }
10102 TYPE_FIELD_NAME (union_type, i) = variant_name;
10103 TYPE_NAME (sub_type)
10104 = rust_fully_qualify (&objfile->objfile_obstack,
10105 TYPE_NAME (type), variant_name);
10106 }
10107 }
10108 }
10109
10110 /* Rewrite some Rust unions to be structures with variants parts. */
10111
10112 static void
10113 rust_union_quirks (struct dwarf2_cu *cu)
10114 {
10115 gdb_assert (cu->language == language_rust);
10116 for (type *type_ : cu->rust_unions)
10117 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10118 /* We don't need this any more. */
10119 cu->rust_unions.clear ();
10120 }
10121
10122 /* Return the symtab for PER_CU. This works properly regardless of
10123 whether we're using the index or psymtabs. */
10124
10125 static struct compunit_symtab *
10126 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10127 {
10128 return (per_cu->dwarf2_per_objfile->using_index
10129 ? per_cu->v.quick->compunit_symtab
10130 : per_cu->v.psymtab->compunit_symtab);
10131 }
10132
10133 /* A helper function for computing the list of all symbol tables
10134 included by PER_CU. */
10135
10136 static void
10137 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10138 htab_t all_children, htab_t all_type_symtabs,
10139 struct dwarf2_per_cu_data *per_cu,
10140 struct compunit_symtab *immediate_parent)
10141 {
10142 void **slot;
10143 int ix;
10144 struct compunit_symtab *cust;
10145 struct dwarf2_per_cu_data *iter;
10146
10147 slot = htab_find_slot (all_children, per_cu, INSERT);
10148 if (*slot != NULL)
10149 {
10150 /* This inclusion and its children have been processed. */
10151 return;
10152 }
10153
10154 *slot = per_cu;
10155 /* Only add a CU if it has a symbol table. */
10156 cust = get_compunit_symtab (per_cu);
10157 if (cust != NULL)
10158 {
10159 /* If this is a type unit only add its symbol table if we haven't
10160 seen it yet (type unit per_cu's can share symtabs). */
10161 if (per_cu->is_debug_types)
10162 {
10163 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10164 if (*slot == NULL)
10165 {
10166 *slot = cust;
10167 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10168 if (cust->user == NULL)
10169 cust->user = immediate_parent;
10170 }
10171 }
10172 else
10173 {
10174 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10175 if (cust->user == NULL)
10176 cust->user = immediate_parent;
10177 }
10178 }
10179
10180 for (ix = 0;
10181 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10182 ++ix)
10183 {
10184 recursively_compute_inclusions (result, all_children,
10185 all_type_symtabs, iter, cust);
10186 }
10187 }
10188
10189 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10190 PER_CU. */
10191
10192 static void
10193 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10194 {
10195 gdb_assert (! per_cu->is_debug_types);
10196
10197 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10198 {
10199 int ix, len;
10200 struct dwarf2_per_cu_data *per_cu_iter;
10201 struct compunit_symtab *compunit_symtab_iter;
10202 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10203 htab_t all_children, all_type_symtabs;
10204 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10205
10206 /* If we don't have a symtab, we can just skip this case. */
10207 if (cust == NULL)
10208 return;
10209
10210 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10211 NULL, xcalloc, xfree);
10212 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10213 NULL, xcalloc, xfree);
10214
10215 for (ix = 0;
10216 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10217 ix, per_cu_iter);
10218 ++ix)
10219 {
10220 recursively_compute_inclusions (&result_symtabs, all_children,
10221 all_type_symtabs, per_cu_iter,
10222 cust);
10223 }
10224
10225 /* Now we have a transitive closure of all the included symtabs. */
10226 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10227 cust->includes
10228 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10229 struct compunit_symtab *, len + 1);
10230 for (ix = 0;
10231 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10232 compunit_symtab_iter);
10233 ++ix)
10234 cust->includes[ix] = compunit_symtab_iter;
10235 cust->includes[len] = NULL;
10236
10237 VEC_free (compunit_symtab_ptr, result_symtabs);
10238 htab_delete (all_children);
10239 htab_delete (all_type_symtabs);
10240 }
10241 }
10242
10243 /* Compute the 'includes' field for the symtabs of all the CUs we just
10244 read. */
10245
10246 static void
10247 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10248 {
10249 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10250 {
10251 if (! iter->is_debug_types)
10252 compute_compunit_symtab_includes (iter);
10253 }
10254
10255 dwarf2_per_objfile->just_read_cus.clear ();
10256 }
10257
10258 /* Generate full symbol information for PER_CU, whose DIEs have
10259 already been loaded into memory. */
10260
10261 static void
10262 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10263 enum language pretend_language)
10264 {
10265 struct dwarf2_cu *cu = per_cu->cu;
10266 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10267 struct objfile *objfile = dwarf2_per_objfile->objfile;
10268 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10269 CORE_ADDR lowpc, highpc;
10270 struct compunit_symtab *cust;
10271 CORE_ADDR baseaddr;
10272 struct block *static_block;
10273 CORE_ADDR addr;
10274
10275 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10276
10277 /* Clear the list here in case something was left over. */
10278 cu->method_list.clear ();
10279
10280 cu->language = pretend_language;
10281 cu->language_defn = language_def (cu->language);
10282
10283 /* Do line number decoding in read_file_scope () */
10284 process_die (cu->dies, cu);
10285
10286 /* For now fudge the Go package. */
10287 if (cu->language == language_go)
10288 fixup_go_packaging (cu);
10289
10290 /* Now that we have processed all the DIEs in the CU, all the types
10291 should be complete, and it should now be safe to compute all of the
10292 physnames. */
10293 compute_delayed_physnames (cu);
10294
10295 if (cu->language == language_rust)
10296 rust_union_quirks (cu);
10297
10298 /* Some compilers don't define a DW_AT_high_pc attribute for the
10299 compilation unit. If the DW_AT_high_pc is missing, synthesize
10300 it, by scanning the DIE's below the compilation unit. */
10301 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10302
10303 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10304 static_block = cu->builder->end_symtab_get_static_block (addr, 0, 1);
10305
10306 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10307 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10308 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10309 addrmap to help ensure it has an accurate map of pc values belonging to
10310 this comp unit. */
10311 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10312
10313 cust = cu->builder->end_symtab_from_static_block (static_block,
10314 SECT_OFF_TEXT (objfile),
10315 0);
10316
10317 if (cust != NULL)
10318 {
10319 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10320
10321 /* Set symtab language to language from DW_AT_language. If the
10322 compilation is from a C file generated by language preprocessors, do
10323 not set the language if it was already deduced by start_subfile. */
10324 if (!(cu->language == language_c
10325 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10326 COMPUNIT_FILETABS (cust)->language = cu->language;
10327
10328 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10329 produce DW_AT_location with location lists but it can be possibly
10330 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10331 there were bugs in prologue debug info, fixed later in GCC-4.5
10332 by "unwind info for epilogues" patch (which is not directly related).
10333
10334 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10335 needed, it would be wrong due to missing DW_AT_producer there.
10336
10337 Still one can confuse GDB by using non-standard GCC compilation
10338 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10339 */
10340 if (cu->has_loclist && gcc_4_minor >= 5)
10341 cust->locations_valid = 1;
10342
10343 if (gcc_4_minor >= 5)
10344 cust->epilogue_unwind_valid = 1;
10345
10346 cust->call_site_htab = cu->call_site_htab;
10347 }
10348
10349 if (dwarf2_per_objfile->using_index)
10350 per_cu->v.quick->compunit_symtab = cust;
10351 else
10352 {
10353 struct partial_symtab *pst = per_cu->v.psymtab;
10354 pst->compunit_symtab = cust;
10355 pst->readin = 1;
10356 }
10357
10358 /* Push it for inclusion processing later. */
10359 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10360
10361 /* Not needed any more. */
10362 cu->builder.reset ();
10363 }
10364
10365 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10366 already been loaded into memory. */
10367
10368 static void
10369 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10370 enum language pretend_language)
10371 {
10372 struct dwarf2_cu *cu = per_cu->cu;
10373 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10374 struct objfile *objfile = dwarf2_per_objfile->objfile;
10375 struct compunit_symtab *cust;
10376 struct signatured_type *sig_type;
10377
10378 gdb_assert (per_cu->is_debug_types);
10379 sig_type = (struct signatured_type *) per_cu;
10380
10381 /* Clear the list here in case something was left over. */
10382 cu->method_list.clear ();
10383
10384 cu->language = pretend_language;
10385 cu->language_defn = language_def (cu->language);
10386
10387 /* The symbol tables are set up in read_type_unit_scope. */
10388 process_die (cu->dies, cu);
10389
10390 /* For now fudge the Go package. */
10391 if (cu->language == language_go)
10392 fixup_go_packaging (cu);
10393
10394 /* Now that we have processed all the DIEs in the CU, all the types
10395 should be complete, and it should now be safe to compute all of the
10396 physnames. */
10397 compute_delayed_physnames (cu);
10398
10399 if (cu->language == language_rust)
10400 rust_union_quirks (cu);
10401
10402 /* TUs share symbol tables.
10403 If this is the first TU to use this symtab, complete the construction
10404 of it with end_expandable_symtab. Otherwise, complete the addition of
10405 this TU's symbols to the existing symtab. */
10406 if (sig_type->type_unit_group->compunit_symtab == NULL)
10407 {
10408 cust = cu->builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10409 sig_type->type_unit_group->compunit_symtab = cust;
10410
10411 if (cust != NULL)
10412 {
10413 /* Set symtab language to language from DW_AT_language. If the
10414 compilation is from a C file generated by language preprocessors,
10415 do not set the language if it was already deduced by
10416 start_subfile. */
10417 if (!(cu->language == language_c
10418 && COMPUNIT_FILETABS (cust)->language != language_c))
10419 COMPUNIT_FILETABS (cust)->language = cu->language;
10420 }
10421 }
10422 else
10423 {
10424 cu->builder->augment_type_symtab ();
10425 cust = sig_type->type_unit_group->compunit_symtab;
10426 }
10427
10428 if (dwarf2_per_objfile->using_index)
10429 per_cu->v.quick->compunit_symtab = cust;
10430 else
10431 {
10432 struct partial_symtab *pst = per_cu->v.psymtab;
10433 pst->compunit_symtab = cust;
10434 pst->readin = 1;
10435 }
10436
10437 /* Not needed any more. */
10438 cu->builder.reset ();
10439 }
10440
10441 /* Process an imported unit DIE. */
10442
10443 static void
10444 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10445 {
10446 struct attribute *attr;
10447
10448 /* For now we don't handle imported units in type units. */
10449 if (cu->per_cu->is_debug_types)
10450 {
10451 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10452 " supported in type units [in module %s]"),
10453 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10454 }
10455
10456 attr = dwarf2_attr (die, DW_AT_import, cu);
10457 if (attr != NULL)
10458 {
10459 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10460 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10461 dwarf2_per_cu_data *per_cu
10462 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10463 cu->per_cu->dwarf2_per_objfile);
10464
10465 /* If necessary, add it to the queue and load its DIEs. */
10466 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10467 load_full_comp_unit (per_cu, false, cu->language);
10468
10469 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10470 per_cu);
10471 }
10472 }
10473
10474 /* RAII object that represents a process_die scope: i.e.,
10475 starts/finishes processing a DIE. */
10476 class process_die_scope
10477 {
10478 public:
10479 process_die_scope (die_info *die, dwarf2_cu *cu)
10480 : m_die (die), m_cu (cu)
10481 {
10482 /* We should only be processing DIEs not already in process. */
10483 gdb_assert (!m_die->in_process);
10484 m_die->in_process = true;
10485 }
10486
10487 ~process_die_scope ()
10488 {
10489 m_die->in_process = false;
10490
10491 /* If we're done processing the DIE for the CU that owns the line
10492 header, we don't need the line header anymore. */
10493 if (m_cu->line_header_die_owner == m_die)
10494 {
10495 delete m_cu->line_header;
10496 m_cu->line_header = NULL;
10497 m_cu->line_header_die_owner = NULL;
10498 }
10499 }
10500
10501 private:
10502 die_info *m_die;
10503 dwarf2_cu *m_cu;
10504 };
10505
10506 /* Process a die and its children. */
10507
10508 static void
10509 process_die (struct die_info *die, struct dwarf2_cu *cu)
10510 {
10511 process_die_scope scope (die, cu);
10512
10513 switch (die->tag)
10514 {
10515 case DW_TAG_padding:
10516 break;
10517 case DW_TAG_compile_unit:
10518 case DW_TAG_partial_unit:
10519 read_file_scope (die, cu);
10520 break;
10521 case DW_TAG_type_unit:
10522 read_type_unit_scope (die, cu);
10523 break;
10524 case DW_TAG_subprogram:
10525 case DW_TAG_inlined_subroutine:
10526 read_func_scope (die, cu);
10527 break;
10528 case DW_TAG_lexical_block:
10529 case DW_TAG_try_block:
10530 case DW_TAG_catch_block:
10531 read_lexical_block_scope (die, cu);
10532 break;
10533 case DW_TAG_call_site:
10534 case DW_TAG_GNU_call_site:
10535 read_call_site_scope (die, cu);
10536 break;
10537 case DW_TAG_class_type:
10538 case DW_TAG_interface_type:
10539 case DW_TAG_structure_type:
10540 case DW_TAG_union_type:
10541 process_structure_scope (die, cu);
10542 break;
10543 case DW_TAG_enumeration_type:
10544 process_enumeration_scope (die, cu);
10545 break;
10546
10547 /* These dies have a type, but processing them does not create
10548 a symbol or recurse to process the children. Therefore we can
10549 read them on-demand through read_type_die. */
10550 case DW_TAG_subroutine_type:
10551 case DW_TAG_set_type:
10552 case DW_TAG_array_type:
10553 case DW_TAG_pointer_type:
10554 case DW_TAG_ptr_to_member_type:
10555 case DW_TAG_reference_type:
10556 case DW_TAG_rvalue_reference_type:
10557 case DW_TAG_string_type:
10558 break;
10559
10560 case DW_TAG_base_type:
10561 case DW_TAG_subrange_type:
10562 case DW_TAG_typedef:
10563 /* Add a typedef symbol for the type definition, if it has a
10564 DW_AT_name. */
10565 new_symbol (die, read_type_die (die, cu), cu);
10566 break;
10567 case DW_TAG_common_block:
10568 read_common_block (die, cu);
10569 break;
10570 case DW_TAG_common_inclusion:
10571 break;
10572 case DW_TAG_namespace:
10573 cu->processing_has_namespace_info = 1;
10574 read_namespace (die, cu);
10575 break;
10576 case DW_TAG_module:
10577 cu->processing_has_namespace_info = 1;
10578 read_module (die, cu);
10579 break;
10580 case DW_TAG_imported_declaration:
10581 cu->processing_has_namespace_info = 1;
10582 if (read_namespace_alias (die, cu))
10583 break;
10584 /* The declaration is not a global namespace alias. */
10585 /* Fall through. */
10586 case DW_TAG_imported_module:
10587 cu->processing_has_namespace_info = 1;
10588 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10589 || cu->language != language_fortran))
10590 complaint (_("Tag '%s' has unexpected children"),
10591 dwarf_tag_name (die->tag));
10592 read_import_statement (die, cu);
10593 break;
10594
10595 case DW_TAG_imported_unit:
10596 process_imported_unit_die (die, cu);
10597 break;
10598
10599 case DW_TAG_variable:
10600 read_variable (die, cu);
10601 break;
10602
10603 default:
10604 new_symbol (die, NULL, cu);
10605 break;
10606 }
10607 }
10608 \f
10609 /* DWARF name computation. */
10610
10611 /* A helper function for dwarf2_compute_name which determines whether DIE
10612 needs to have the name of the scope prepended to the name listed in the
10613 die. */
10614
10615 static int
10616 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10617 {
10618 struct attribute *attr;
10619
10620 switch (die->tag)
10621 {
10622 case DW_TAG_namespace:
10623 case DW_TAG_typedef:
10624 case DW_TAG_class_type:
10625 case DW_TAG_interface_type:
10626 case DW_TAG_structure_type:
10627 case DW_TAG_union_type:
10628 case DW_TAG_enumeration_type:
10629 case DW_TAG_enumerator:
10630 case DW_TAG_subprogram:
10631 case DW_TAG_inlined_subroutine:
10632 case DW_TAG_member:
10633 case DW_TAG_imported_declaration:
10634 return 1;
10635
10636 case DW_TAG_variable:
10637 case DW_TAG_constant:
10638 /* We only need to prefix "globally" visible variables. These include
10639 any variable marked with DW_AT_external or any variable that
10640 lives in a namespace. [Variables in anonymous namespaces
10641 require prefixing, but they are not DW_AT_external.] */
10642
10643 if (dwarf2_attr (die, DW_AT_specification, cu))
10644 {
10645 struct dwarf2_cu *spec_cu = cu;
10646
10647 return die_needs_namespace (die_specification (die, &spec_cu),
10648 spec_cu);
10649 }
10650
10651 attr = dwarf2_attr (die, DW_AT_external, cu);
10652 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10653 && die->parent->tag != DW_TAG_module)
10654 return 0;
10655 /* A variable in a lexical block of some kind does not need a
10656 namespace, even though in C++ such variables may be external
10657 and have a mangled name. */
10658 if (die->parent->tag == DW_TAG_lexical_block
10659 || die->parent->tag == DW_TAG_try_block
10660 || die->parent->tag == DW_TAG_catch_block
10661 || die->parent->tag == DW_TAG_subprogram)
10662 return 0;
10663 return 1;
10664
10665 default:
10666 return 0;
10667 }
10668 }
10669
10670 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10671 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10672 defined for the given DIE. */
10673
10674 static struct attribute *
10675 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10676 {
10677 struct attribute *attr;
10678
10679 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10680 if (attr == NULL)
10681 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10682
10683 return attr;
10684 }
10685
10686 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10687 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10688 defined for the given DIE. */
10689
10690 static const char *
10691 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10692 {
10693 const char *linkage_name;
10694
10695 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10696 if (linkage_name == NULL)
10697 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10698
10699 return linkage_name;
10700 }
10701
10702 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10703 compute the physname for the object, which include a method's:
10704 - formal parameters (C++),
10705 - receiver type (Go),
10706
10707 The term "physname" is a bit confusing.
10708 For C++, for example, it is the demangled name.
10709 For Go, for example, it's the mangled name.
10710
10711 For Ada, return the DIE's linkage name rather than the fully qualified
10712 name. PHYSNAME is ignored..
10713
10714 The result is allocated on the objfile_obstack and canonicalized. */
10715
10716 static const char *
10717 dwarf2_compute_name (const char *name,
10718 struct die_info *die, struct dwarf2_cu *cu,
10719 int physname)
10720 {
10721 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10722
10723 if (name == NULL)
10724 name = dwarf2_name (die, cu);
10725
10726 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10727 but otherwise compute it by typename_concat inside GDB.
10728 FIXME: Actually this is not really true, or at least not always true.
10729 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10730 Fortran names because there is no mangling standard. So new_symbol
10731 will set the demangled name to the result of dwarf2_full_name, and it is
10732 the demangled name that GDB uses if it exists. */
10733 if (cu->language == language_ada
10734 || (cu->language == language_fortran && physname))
10735 {
10736 /* For Ada unit, we prefer the linkage name over the name, as
10737 the former contains the exported name, which the user expects
10738 to be able to reference. Ideally, we want the user to be able
10739 to reference this entity using either natural or linkage name,
10740 but we haven't started looking at this enhancement yet. */
10741 const char *linkage_name = dw2_linkage_name (die, cu);
10742
10743 if (linkage_name != NULL)
10744 return linkage_name;
10745 }
10746
10747 /* These are the only languages we know how to qualify names in. */
10748 if (name != NULL
10749 && (cu->language == language_cplus
10750 || cu->language == language_fortran || cu->language == language_d
10751 || cu->language == language_rust))
10752 {
10753 if (die_needs_namespace (die, cu))
10754 {
10755 const char *prefix;
10756 const char *canonical_name = NULL;
10757
10758 string_file buf;
10759
10760 prefix = determine_prefix (die, cu);
10761 if (*prefix != '\0')
10762 {
10763 char *prefixed_name = typename_concat (NULL, prefix, name,
10764 physname, cu);
10765
10766 buf.puts (prefixed_name);
10767 xfree (prefixed_name);
10768 }
10769 else
10770 buf.puts (name);
10771
10772 /* Template parameters may be specified in the DIE's DW_AT_name, or
10773 as children with DW_TAG_template_type_param or
10774 DW_TAG_value_type_param. If the latter, add them to the name
10775 here. If the name already has template parameters, then
10776 skip this step; some versions of GCC emit both, and
10777 it is more efficient to use the pre-computed name.
10778
10779 Something to keep in mind about this process: it is very
10780 unlikely, or in some cases downright impossible, to produce
10781 something that will match the mangled name of a function.
10782 If the definition of the function has the same debug info,
10783 we should be able to match up with it anyway. But fallbacks
10784 using the minimal symbol, for instance to find a method
10785 implemented in a stripped copy of libstdc++, will not work.
10786 If we do not have debug info for the definition, we will have to
10787 match them up some other way.
10788
10789 When we do name matching there is a related problem with function
10790 templates; two instantiated function templates are allowed to
10791 differ only by their return types, which we do not add here. */
10792
10793 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10794 {
10795 struct attribute *attr;
10796 struct die_info *child;
10797 int first = 1;
10798
10799 die->building_fullname = 1;
10800
10801 for (child = die->child; child != NULL; child = child->sibling)
10802 {
10803 struct type *type;
10804 LONGEST value;
10805 const gdb_byte *bytes;
10806 struct dwarf2_locexpr_baton *baton;
10807 struct value *v;
10808
10809 if (child->tag != DW_TAG_template_type_param
10810 && child->tag != DW_TAG_template_value_param)
10811 continue;
10812
10813 if (first)
10814 {
10815 buf.puts ("<");
10816 first = 0;
10817 }
10818 else
10819 buf.puts (", ");
10820
10821 attr = dwarf2_attr (child, DW_AT_type, cu);
10822 if (attr == NULL)
10823 {
10824 complaint (_("template parameter missing DW_AT_type"));
10825 buf.puts ("UNKNOWN_TYPE");
10826 continue;
10827 }
10828 type = die_type (child, cu);
10829
10830 if (child->tag == DW_TAG_template_type_param)
10831 {
10832 c_print_type (type, "", &buf, -1, 0, cu->language,
10833 &type_print_raw_options);
10834 continue;
10835 }
10836
10837 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10838 if (attr == NULL)
10839 {
10840 complaint (_("template parameter missing "
10841 "DW_AT_const_value"));
10842 buf.puts ("UNKNOWN_VALUE");
10843 continue;
10844 }
10845
10846 dwarf2_const_value_attr (attr, type, name,
10847 &cu->comp_unit_obstack, cu,
10848 &value, &bytes, &baton);
10849
10850 if (TYPE_NOSIGN (type))
10851 /* GDB prints characters as NUMBER 'CHAR'. If that's
10852 changed, this can use value_print instead. */
10853 c_printchar (value, type, &buf);
10854 else
10855 {
10856 struct value_print_options opts;
10857
10858 if (baton != NULL)
10859 v = dwarf2_evaluate_loc_desc (type, NULL,
10860 baton->data,
10861 baton->size,
10862 baton->per_cu);
10863 else if (bytes != NULL)
10864 {
10865 v = allocate_value (type);
10866 memcpy (value_contents_writeable (v), bytes,
10867 TYPE_LENGTH (type));
10868 }
10869 else
10870 v = value_from_longest (type, value);
10871
10872 /* Specify decimal so that we do not depend on
10873 the radix. */
10874 get_formatted_print_options (&opts, 'd');
10875 opts.raw = 1;
10876 value_print (v, &buf, &opts);
10877 release_value (v);
10878 }
10879 }
10880
10881 die->building_fullname = 0;
10882
10883 if (!first)
10884 {
10885 /* Close the argument list, with a space if necessary
10886 (nested templates). */
10887 if (!buf.empty () && buf.string ().back () == '>')
10888 buf.puts (" >");
10889 else
10890 buf.puts (">");
10891 }
10892 }
10893
10894 /* For C++ methods, append formal parameter type
10895 information, if PHYSNAME. */
10896
10897 if (physname && die->tag == DW_TAG_subprogram
10898 && cu->language == language_cplus)
10899 {
10900 struct type *type = read_type_die (die, cu);
10901
10902 c_type_print_args (type, &buf, 1, cu->language,
10903 &type_print_raw_options);
10904
10905 if (cu->language == language_cplus)
10906 {
10907 /* Assume that an artificial first parameter is
10908 "this", but do not crash if it is not. RealView
10909 marks unnamed (and thus unused) parameters as
10910 artificial; there is no way to differentiate
10911 the two cases. */
10912 if (TYPE_NFIELDS (type) > 0
10913 && TYPE_FIELD_ARTIFICIAL (type, 0)
10914 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10915 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10916 0))))
10917 buf.puts (" const");
10918 }
10919 }
10920
10921 const std::string &intermediate_name = buf.string ();
10922
10923 if (cu->language == language_cplus)
10924 canonical_name
10925 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10926 &objfile->per_bfd->storage_obstack);
10927
10928 /* If we only computed INTERMEDIATE_NAME, or if
10929 INTERMEDIATE_NAME is already canonical, then we need to
10930 copy it to the appropriate obstack. */
10931 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10932 name = ((const char *)
10933 obstack_copy0 (&objfile->per_bfd->storage_obstack,
10934 intermediate_name.c_str (),
10935 intermediate_name.length ()));
10936 else
10937 name = canonical_name;
10938 }
10939 }
10940
10941 return name;
10942 }
10943
10944 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10945 If scope qualifiers are appropriate they will be added. The result
10946 will be allocated on the storage_obstack, or NULL if the DIE does
10947 not have a name. NAME may either be from a previous call to
10948 dwarf2_name or NULL.
10949
10950 The output string will be canonicalized (if C++). */
10951
10952 static const char *
10953 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10954 {
10955 return dwarf2_compute_name (name, die, cu, 0);
10956 }
10957
10958 /* Construct a physname for the given DIE in CU. NAME may either be
10959 from a previous call to dwarf2_name or NULL. The result will be
10960 allocated on the objfile_objstack or NULL if the DIE does not have a
10961 name.
10962
10963 The output string will be canonicalized (if C++). */
10964
10965 static const char *
10966 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10967 {
10968 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10969 const char *retval, *mangled = NULL, *canon = NULL;
10970 int need_copy = 1;
10971
10972 /* In this case dwarf2_compute_name is just a shortcut not building anything
10973 on its own. */
10974 if (!die_needs_namespace (die, cu))
10975 return dwarf2_compute_name (name, die, cu, 1);
10976
10977 mangled = dw2_linkage_name (die, cu);
10978
10979 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10980 See https://github.com/rust-lang/rust/issues/32925. */
10981 if (cu->language == language_rust && mangled != NULL
10982 && strchr (mangled, '{') != NULL)
10983 mangled = NULL;
10984
10985 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10986 has computed. */
10987 gdb::unique_xmalloc_ptr<char> demangled;
10988 if (mangled != NULL)
10989 {
10990
10991 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10992 {
10993 /* Do nothing (do not demangle the symbol name). */
10994 }
10995 else if (cu->language == language_go)
10996 {
10997 /* This is a lie, but we already lie to the caller new_symbol.
10998 new_symbol assumes we return the mangled name.
10999 This just undoes that lie until things are cleaned up. */
11000 }
11001 else
11002 {
11003 /* Use DMGL_RET_DROP for C++ template functions to suppress
11004 their return type. It is easier for GDB users to search
11005 for such functions as `name(params)' than `long name(params)'.
11006 In such case the minimal symbol names do not match the full
11007 symbol names but for template functions there is never a need
11008 to look up their definition from their declaration so
11009 the only disadvantage remains the minimal symbol variant
11010 `long name(params)' does not have the proper inferior type. */
11011 demangled.reset (gdb_demangle (mangled,
11012 (DMGL_PARAMS | DMGL_ANSI
11013 | DMGL_RET_DROP)));
11014 }
11015 if (demangled)
11016 canon = demangled.get ();
11017 else
11018 {
11019 canon = mangled;
11020 need_copy = 0;
11021 }
11022 }
11023
11024 if (canon == NULL || check_physname)
11025 {
11026 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11027
11028 if (canon != NULL && strcmp (physname, canon) != 0)
11029 {
11030 /* It may not mean a bug in GDB. The compiler could also
11031 compute DW_AT_linkage_name incorrectly. But in such case
11032 GDB would need to be bug-to-bug compatible. */
11033
11034 complaint (_("Computed physname <%s> does not match demangled <%s> "
11035 "(from linkage <%s>) - DIE at %s [in module %s]"),
11036 physname, canon, mangled, sect_offset_str (die->sect_off),
11037 objfile_name (objfile));
11038
11039 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11040 is available here - over computed PHYSNAME. It is safer
11041 against both buggy GDB and buggy compilers. */
11042
11043 retval = canon;
11044 }
11045 else
11046 {
11047 retval = physname;
11048 need_copy = 0;
11049 }
11050 }
11051 else
11052 retval = canon;
11053
11054 if (need_copy)
11055 retval = ((const char *)
11056 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11057 retval, strlen (retval)));
11058
11059 return retval;
11060 }
11061
11062 /* Inspect DIE in CU for a namespace alias. If one exists, record
11063 a new symbol for it.
11064
11065 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11066
11067 static int
11068 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11069 {
11070 struct attribute *attr;
11071
11072 /* If the die does not have a name, this is not a namespace
11073 alias. */
11074 attr = dwarf2_attr (die, DW_AT_name, cu);
11075 if (attr != NULL)
11076 {
11077 int num;
11078 struct die_info *d = die;
11079 struct dwarf2_cu *imported_cu = cu;
11080
11081 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11082 keep inspecting DIEs until we hit the underlying import. */
11083 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11084 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11085 {
11086 attr = dwarf2_attr (d, DW_AT_import, cu);
11087 if (attr == NULL)
11088 break;
11089
11090 d = follow_die_ref (d, attr, &imported_cu);
11091 if (d->tag != DW_TAG_imported_declaration)
11092 break;
11093 }
11094
11095 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11096 {
11097 complaint (_("DIE at %s has too many recursively imported "
11098 "declarations"), sect_offset_str (d->sect_off));
11099 return 0;
11100 }
11101
11102 if (attr != NULL)
11103 {
11104 struct type *type;
11105 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11106
11107 type = get_die_type_at_offset (sect_off, cu->per_cu);
11108 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11109 {
11110 /* This declaration is a global namespace alias. Add
11111 a symbol for it whose type is the aliased namespace. */
11112 new_symbol (die, type, cu);
11113 return 1;
11114 }
11115 }
11116 }
11117
11118 return 0;
11119 }
11120
11121 /* Return the using directives repository (global or local?) to use in the
11122 current context for CU.
11123
11124 For Ada, imported declarations can materialize renamings, which *may* be
11125 global. However it is impossible (for now?) in DWARF to distinguish
11126 "external" imported declarations and "static" ones. As all imported
11127 declarations seem to be static in all other languages, make them all CU-wide
11128 global only in Ada. */
11129
11130 static struct using_direct **
11131 using_directives (struct dwarf2_cu *cu)
11132 {
11133 if (cu->language == language_ada && cu->builder->outermost_context_p ())
11134 return cu->builder->get_global_using_directives ();
11135 else
11136 return cu->builder->get_local_using_directives ();
11137 }
11138
11139 /* Read the import statement specified by the given die and record it. */
11140
11141 static void
11142 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11143 {
11144 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11145 struct attribute *import_attr;
11146 struct die_info *imported_die, *child_die;
11147 struct dwarf2_cu *imported_cu;
11148 const char *imported_name;
11149 const char *imported_name_prefix;
11150 const char *canonical_name;
11151 const char *import_alias;
11152 const char *imported_declaration = NULL;
11153 const char *import_prefix;
11154 std::vector<const char *> excludes;
11155
11156 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11157 if (import_attr == NULL)
11158 {
11159 complaint (_("Tag '%s' has no DW_AT_import"),
11160 dwarf_tag_name (die->tag));
11161 return;
11162 }
11163
11164 imported_cu = cu;
11165 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11166 imported_name = dwarf2_name (imported_die, imported_cu);
11167 if (imported_name == NULL)
11168 {
11169 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11170
11171 The import in the following code:
11172 namespace A
11173 {
11174 typedef int B;
11175 }
11176
11177 int main ()
11178 {
11179 using A::B;
11180 B b;
11181 return b;
11182 }
11183
11184 ...
11185 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11186 <52> DW_AT_decl_file : 1
11187 <53> DW_AT_decl_line : 6
11188 <54> DW_AT_import : <0x75>
11189 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11190 <59> DW_AT_name : B
11191 <5b> DW_AT_decl_file : 1
11192 <5c> DW_AT_decl_line : 2
11193 <5d> DW_AT_type : <0x6e>
11194 ...
11195 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11196 <76> DW_AT_byte_size : 4
11197 <77> DW_AT_encoding : 5 (signed)
11198
11199 imports the wrong die ( 0x75 instead of 0x58 ).
11200 This case will be ignored until the gcc bug is fixed. */
11201 return;
11202 }
11203
11204 /* Figure out the local name after import. */
11205 import_alias = dwarf2_name (die, cu);
11206
11207 /* Figure out where the statement is being imported to. */
11208 import_prefix = determine_prefix (die, cu);
11209
11210 /* Figure out what the scope of the imported die is and prepend it
11211 to the name of the imported die. */
11212 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11213
11214 if (imported_die->tag != DW_TAG_namespace
11215 && imported_die->tag != DW_TAG_module)
11216 {
11217 imported_declaration = imported_name;
11218 canonical_name = imported_name_prefix;
11219 }
11220 else if (strlen (imported_name_prefix) > 0)
11221 canonical_name = obconcat (&objfile->objfile_obstack,
11222 imported_name_prefix,
11223 (cu->language == language_d ? "." : "::"),
11224 imported_name, (char *) NULL);
11225 else
11226 canonical_name = imported_name;
11227
11228 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11229 for (child_die = die->child; child_die && child_die->tag;
11230 child_die = sibling_die (child_die))
11231 {
11232 /* DWARF-4: A Fortran use statement with a “rename list” may be
11233 represented by an imported module entry with an import attribute
11234 referring to the module and owned entries corresponding to those
11235 entities that are renamed as part of being imported. */
11236
11237 if (child_die->tag != DW_TAG_imported_declaration)
11238 {
11239 complaint (_("child DW_TAG_imported_declaration expected "
11240 "- DIE at %s [in module %s]"),
11241 sect_offset_str (child_die->sect_off),
11242 objfile_name (objfile));
11243 continue;
11244 }
11245
11246 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11247 if (import_attr == NULL)
11248 {
11249 complaint (_("Tag '%s' has no DW_AT_import"),
11250 dwarf_tag_name (child_die->tag));
11251 continue;
11252 }
11253
11254 imported_cu = cu;
11255 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11256 &imported_cu);
11257 imported_name = dwarf2_name (imported_die, imported_cu);
11258 if (imported_name == NULL)
11259 {
11260 complaint (_("child DW_TAG_imported_declaration has unknown "
11261 "imported name - DIE at %s [in module %s]"),
11262 sect_offset_str (child_die->sect_off),
11263 objfile_name (objfile));
11264 continue;
11265 }
11266
11267 excludes.push_back (imported_name);
11268
11269 process_die (child_die, cu);
11270 }
11271
11272 add_using_directive (using_directives (cu),
11273 import_prefix,
11274 canonical_name,
11275 import_alias,
11276 imported_declaration,
11277 excludes,
11278 0,
11279 &objfile->objfile_obstack);
11280 }
11281
11282 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11283 types, but gives them a size of zero. Starting with version 14,
11284 ICC is compatible with GCC. */
11285
11286 static int
11287 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11288 {
11289 if (!cu->checked_producer)
11290 check_producer (cu);
11291
11292 return cu->producer_is_icc_lt_14;
11293 }
11294
11295 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11296 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11297 this, it was first present in GCC release 4.3.0. */
11298
11299 static int
11300 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11301 {
11302 if (!cu->checked_producer)
11303 check_producer (cu);
11304
11305 return cu->producer_is_gcc_lt_4_3;
11306 }
11307
11308 static file_and_directory
11309 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11310 {
11311 file_and_directory res;
11312
11313 /* Find the filename. Do not use dwarf2_name here, since the filename
11314 is not a source language identifier. */
11315 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11316 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11317
11318 if (res.comp_dir == NULL
11319 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11320 && IS_ABSOLUTE_PATH (res.name))
11321 {
11322 res.comp_dir_storage = ldirname (res.name);
11323 if (!res.comp_dir_storage.empty ())
11324 res.comp_dir = res.comp_dir_storage.c_str ();
11325 }
11326 if (res.comp_dir != NULL)
11327 {
11328 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11329 directory, get rid of it. */
11330 const char *cp = strchr (res.comp_dir, ':');
11331
11332 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11333 res.comp_dir = cp + 1;
11334 }
11335
11336 if (res.name == NULL)
11337 res.name = "<unknown>";
11338
11339 return res;
11340 }
11341
11342 /* Handle DW_AT_stmt_list for a compilation unit.
11343 DIE is the DW_TAG_compile_unit die for CU.
11344 COMP_DIR is the compilation directory. LOWPC is passed to
11345 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11346
11347 static void
11348 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11349 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11350 {
11351 struct dwarf2_per_objfile *dwarf2_per_objfile
11352 = cu->per_cu->dwarf2_per_objfile;
11353 struct objfile *objfile = dwarf2_per_objfile->objfile;
11354 struct attribute *attr;
11355 struct line_header line_header_local;
11356 hashval_t line_header_local_hash;
11357 void **slot;
11358 int decode_mapping;
11359
11360 gdb_assert (! cu->per_cu->is_debug_types);
11361
11362 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11363 if (attr == NULL)
11364 return;
11365
11366 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11367
11368 /* The line header hash table is only created if needed (it exists to
11369 prevent redundant reading of the line table for partial_units).
11370 If we're given a partial_unit, we'll need it. If we're given a
11371 compile_unit, then use the line header hash table if it's already
11372 created, but don't create one just yet. */
11373
11374 if (dwarf2_per_objfile->line_header_hash == NULL
11375 && die->tag == DW_TAG_partial_unit)
11376 {
11377 dwarf2_per_objfile->line_header_hash
11378 = htab_create_alloc_ex (127, line_header_hash_voidp,
11379 line_header_eq_voidp,
11380 free_line_header_voidp,
11381 &objfile->objfile_obstack,
11382 hashtab_obstack_allocate,
11383 dummy_obstack_deallocate);
11384 }
11385
11386 line_header_local.sect_off = line_offset;
11387 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11388 line_header_local_hash = line_header_hash (&line_header_local);
11389 if (dwarf2_per_objfile->line_header_hash != NULL)
11390 {
11391 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11392 &line_header_local,
11393 line_header_local_hash, NO_INSERT);
11394
11395 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11396 is not present in *SLOT (since if there is something in *SLOT then
11397 it will be for a partial_unit). */
11398 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11399 {
11400 gdb_assert (*slot != NULL);
11401 cu->line_header = (struct line_header *) *slot;
11402 return;
11403 }
11404 }
11405
11406 /* dwarf_decode_line_header does not yet provide sufficient information.
11407 We always have to call also dwarf_decode_lines for it. */
11408 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11409 if (lh == NULL)
11410 return;
11411
11412 cu->line_header = lh.release ();
11413 cu->line_header_die_owner = die;
11414
11415 if (dwarf2_per_objfile->line_header_hash == NULL)
11416 slot = NULL;
11417 else
11418 {
11419 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11420 &line_header_local,
11421 line_header_local_hash, INSERT);
11422 gdb_assert (slot != NULL);
11423 }
11424 if (slot != NULL && *slot == NULL)
11425 {
11426 /* This newly decoded line number information unit will be owned
11427 by line_header_hash hash table. */
11428 *slot = cu->line_header;
11429 cu->line_header_die_owner = NULL;
11430 }
11431 else
11432 {
11433 /* We cannot free any current entry in (*slot) as that struct line_header
11434 may be already used by multiple CUs. Create only temporary decoded
11435 line_header for this CU - it may happen at most once for each line
11436 number information unit. And if we're not using line_header_hash
11437 then this is what we want as well. */
11438 gdb_assert (die->tag != DW_TAG_partial_unit);
11439 }
11440 decode_mapping = (die->tag != DW_TAG_partial_unit);
11441 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11442 decode_mapping);
11443
11444 }
11445
11446 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11447
11448 static void
11449 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11450 {
11451 struct dwarf2_per_objfile *dwarf2_per_objfile
11452 = cu->per_cu->dwarf2_per_objfile;
11453 struct objfile *objfile = dwarf2_per_objfile->objfile;
11454 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11455 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11456 CORE_ADDR highpc = ((CORE_ADDR) 0);
11457 struct attribute *attr;
11458 struct die_info *child_die;
11459 CORE_ADDR baseaddr;
11460
11461 prepare_one_comp_unit (cu, die, cu->language);
11462 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11463
11464 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11465
11466 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11467 from finish_block. */
11468 if (lowpc == ((CORE_ADDR) -1))
11469 lowpc = highpc;
11470 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11471
11472 file_and_directory fnd = find_file_and_directory (die, cu);
11473
11474 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11475 standardised yet. As a workaround for the language detection we fall
11476 back to the DW_AT_producer string. */
11477 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11478 cu->language = language_opencl;
11479
11480 /* Similar hack for Go. */
11481 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11482 set_cu_language (DW_LANG_Go, cu);
11483
11484 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11485
11486 /* Decode line number information if present. We do this before
11487 processing child DIEs, so that the line header table is available
11488 for DW_AT_decl_file. */
11489 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11490
11491 /* Process all dies in compilation unit. */
11492 if (die->child != NULL)
11493 {
11494 child_die = die->child;
11495 while (child_die && child_die->tag)
11496 {
11497 process_die (child_die, cu);
11498 child_die = sibling_die (child_die);
11499 }
11500 }
11501
11502 /* Decode macro information, if present. Dwarf 2 macro information
11503 refers to information in the line number info statement program
11504 header, so we can only read it if we've read the header
11505 successfully. */
11506 attr = dwarf2_attr (die, DW_AT_macros, cu);
11507 if (attr == NULL)
11508 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11509 if (attr && cu->line_header)
11510 {
11511 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11512 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11513
11514 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11515 }
11516 else
11517 {
11518 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11519 if (attr && cu->line_header)
11520 {
11521 unsigned int macro_offset = DW_UNSND (attr);
11522
11523 dwarf_decode_macros (cu, macro_offset, 0);
11524 }
11525 }
11526 }
11527
11528 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11529 Create the set of symtabs used by this TU, or if this TU is sharing
11530 symtabs with another TU and the symtabs have already been created
11531 then restore those symtabs in the line header.
11532 We don't need the pc/line-number mapping for type units. */
11533
11534 static void
11535 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11536 {
11537 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11538 struct type_unit_group *tu_group;
11539 int first_time;
11540 struct attribute *attr;
11541 unsigned int i;
11542 struct signatured_type *sig_type;
11543
11544 gdb_assert (per_cu->is_debug_types);
11545 sig_type = (struct signatured_type *) per_cu;
11546
11547 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11548
11549 /* If we're using .gdb_index (includes -readnow) then
11550 per_cu->type_unit_group may not have been set up yet. */
11551 if (sig_type->type_unit_group == NULL)
11552 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11553 tu_group = sig_type->type_unit_group;
11554
11555 /* If we've already processed this stmt_list there's no real need to
11556 do it again, we could fake it and just recreate the part we need
11557 (file name,index -> symtab mapping). If data shows this optimization
11558 is useful we can do it then. */
11559 first_time = tu_group->compunit_symtab == NULL;
11560
11561 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11562 debug info. */
11563 line_header_up lh;
11564 if (attr != NULL)
11565 {
11566 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11567 lh = dwarf_decode_line_header (line_offset, cu);
11568 }
11569 if (lh == NULL)
11570 {
11571 if (first_time)
11572 dwarf2_start_symtab (cu, "", NULL, 0);
11573 else
11574 {
11575 gdb_assert (tu_group->symtabs == NULL);
11576 gdb_assert (cu->builder == nullptr);
11577 struct compunit_symtab *cust = tu_group->compunit_symtab;
11578 cu->builder.reset (new struct buildsym_compunit
11579 (COMPUNIT_OBJFILE (cust), "",
11580 COMPUNIT_DIRNAME (cust),
11581 compunit_language (cust),
11582 0, cust));
11583 }
11584 return;
11585 }
11586
11587 cu->line_header = lh.release ();
11588 cu->line_header_die_owner = die;
11589
11590 if (first_time)
11591 {
11592 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11593
11594 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11595 still initializing it, and our caller (a few levels up)
11596 process_full_type_unit still needs to know if this is the first
11597 time. */
11598
11599 tu_group->num_symtabs = cu->line_header->file_names.size ();
11600 tu_group->symtabs = XNEWVEC (struct symtab *,
11601 cu->line_header->file_names.size ());
11602
11603 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11604 {
11605 file_entry &fe = cu->line_header->file_names[i];
11606
11607 dwarf2_start_subfile (cu, fe.name, fe.include_dir (cu->line_header));
11608
11609 if (cu->builder->get_current_subfile ()->symtab == NULL)
11610 {
11611 /* NOTE: start_subfile will recognize when it's been
11612 passed a file it has already seen. So we can't
11613 assume there's a simple mapping from
11614 cu->line_header->file_names to subfiles, plus
11615 cu->line_header->file_names may contain dups. */
11616 cu->builder->get_current_subfile ()->symtab
11617 = allocate_symtab (cust,
11618 cu->builder->get_current_subfile ()->name);
11619 }
11620
11621 fe.symtab = cu->builder->get_current_subfile ()->symtab;
11622 tu_group->symtabs[i] = fe.symtab;
11623 }
11624 }
11625 else
11626 {
11627 gdb_assert (cu->builder == nullptr);
11628 struct compunit_symtab *cust = tu_group->compunit_symtab;
11629 cu->builder.reset (new struct buildsym_compunit
11630 (COMPUNIT_OBJFILE (cust), "",
11631 COMPUNIT_DIRNAME (cust),
11632 compunit_language (cust),
11633 0, cust));
11634
11635 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11636 {
11637 file_entry &fe = cu->line_header->file_names[i];
11638
11639 fe.symtab = tu_group->symtabs[i];
11640 }
11641 }
11642
11643 /* The main symtab is allocated last. Type units don't have DW_AT_name
11644 so they don't have a "real" (so to speak) symtab anyway.
11645 There is later code that will assign the main symtab to all symbols
11646 that don't have one. We need to handle the case of a symbol with a
11647 missing symtab (DW_AT_decl_file) anyway. */
11648 }
11649
11650 /* Process DW_TAG_type_unit.
11651 For TUs we want to skip the first top level sibling if it's not the
11652 actual type being defined by this TU. In this case the first top
11653 level sibling is there to provide context only. */
11654
11655 static void
11656 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11657 {
11658 struct die_info *child_die;
11659
11660 prepare_one_comp_unit (cu, die, language_minimal);
11661
11662 /* Initialize (or reinitialize) the machinery for building symtabs.
11663 We do this before processing child DIEs, so that the line header table
11664 is available for DW_AT_decl_file. */
11665 setup_type_unit_groups (die, cu);
11666
11667 if (die->child != NULL)
11668 {
11669 child_die = die->child;
11670 while (child_die && child_die->tag)
11671 {
11672 process_die (child_die, cu);
11673 child_die = sibling_die (child_die);
11674 }
11675 }
11676 }
11677 \f
11678 /* DWO/DWP files.
11679
11680 http://gcc.gnu.org/wiki/DebugFission
11681 http://gcc.gnu.org/wiki/DebugFissionDWP
11682
11683 To simplify handling of both DWO files ("object" files with the DWARF info)
11684 and DWP files (a file with the DWOs packaged up into one file), we treat
11685 DWP files as having a collection of virtual DWO files. */
11686
11687 static hashval_t
11688 hash_dwo_file (const void *item)
11689 {
11690 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11691 hashval_t hash;
11692
11693 hash = htab_hash_string (dwo_file->dwo_name);
11694 if (dwo_file->comp_dir != NULL)
11695 hash += htab_hash_string (dwo_file->comp_dir);
11696 return hash;
11697 }
11698
11699 static int
11700 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11701 {
11702 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11703 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11704
11705 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11706 return 0;
11707 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11708 return lhs->comp_dir == rhs->comp_dir;
11709 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11710 }
11711
11712 /* Allocate a hash table for DWO files. */
11713
11714 static htab_t
11715 allocate_dwo_file_hash_table (struct objfile *objfile)
11716 {
11717 return htab_create_alloc_ex (41,
11718 hash_dwo_file,
11719 eq_dwo_file,
11720 NULL,
11721 &objfile->objfile_obstack,
11722 hashtab_obstack_allocate,
11723 dummy_obstack_deallocate);
11724 }
11725
11726 /* Lookup DWO file DWO_NAME. */
11727
11728 static void **
11729 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11730 const char *dwo_name,
11731 const char *comp_dir)
11732 {
11733 struct dwo_file find_entry;
11734 void **slot;
11735
11736 if (dwarf2_per_objfile->dwo_files == NULL)
11737 dwarf2_per_objfile->dwo_files
11738 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11739
11740 memset (&find_entry, 0, sizeof (find_entry));
11741 find_entry.dwo_name = dwo_name;
11742 find_entry.comp_dir = comp_dir;
11743 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11744
11745 return slot;
11746 }
11747
11748 static hashval_t
11749 hash_dwo_unit (const void *item)
11750 {
11751 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11752
11753 /* This drops the top 32 bits of the id, but is ok for a hash. */
11754 return dwo_unit->signature;
11755 }
11756
11757 static int
11758 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11759 {
11760 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11761 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11762
11763 /* The signature is assumed to be unique within the DWO file.
11764 So while object file CU dwo_id's always have the value zero,
11765 that's OK, assuming each object file DWO file has only one CU,
11766 and that's the rule for now. */
11767 return lhs->signature == rhs->signature;
11768 }
11769
11770 /* Allocate a hash table for DWO CUs,TUs.
11771 There is one of these tables for each of CUs,TUs for each DWO file. */
11772
11773 static htab_t
11774 allocate_dwo_unit_table (struct objfile *objfile)
11775 {
11776 /* Start out with a pretty small number.
11777 Generally DWO files contain only one CU and maybe some TUs. */
11778 return htab_create_alloc_ex (3,
11779 hash_dwo_unit,
11780 eq_dwo_unit,
11781 NULL,
11782 &objfile->objfile_obstack,
11783 hashtab_obstack_allocate,
11784 dummy_obstack_deallocate);
11785 }
11786
11787 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11788
11789 struct create_dwo_cu_data
11790 {
11791 struct dwo_file *dwo_file;
11792 struct dwo_unit dwo_unit;
11793 };
11794
11795 /* die_reader_func for create_dwo_cu. */
11796
11797 static void
11798 create_dwo_cu_reader (const struct die_reader_specs *reader,
11799 const gdb_byte *info_ptr,
11800 struct die_info *comp_unit_die,
11801 int has_children,
11802 void *datap)
11803 {
11804 struct dwarf2_cu *cu = reader->cu;
11805 sect_offset sect_off = cu->per_cu->sect_off;
11806 struct dwarf2_section_info *section = cu->per_cu->section;
11807 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11808 struct dwo_file *dwo_file = data->dwo_file;
11809 struct dwo_unit *dwo_unit = &data->dwo_unit;
11810 struct attribute *attr;
11811
11812 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11813 if (attr == NULL)
11814 {
11815 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11816 " its dwo_id [in module %s]"),
11817 sect_offset_str (sect_off), dwo_file->dwo_name);
11818 return;
11819 }
11820
11821 dwo_unit->dwo_file = dwo_file;
11822 dwo_unit->signature = DW_UNSND (attr);
11823 dwo_unit->section = section;
11824 dwo_unit->sect_off = sect_off;
11825 dwo_unit->length = cu->per_cu->length;
11826
11827 if (dwarf_read_debug)
11828 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11829 sect_offset_str (sect_off),
11830 hex_string (dwo_unit->signature));
11831 }
11832
11833 /* Create the dwo_units for the CUs in a DWO_FILE.
11834 Note: This function processes DWO files only, not DWP files. */
11835
11836 static void
11837 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11838 struct dwo_file &dwo_file, dwarf2_section_info &section,
11839 htab_t &cus_htab)
11840 {
11841 struct objfile *objfile = dwarf2_per_objfile->objfile;
11842 const gdb_byte *info_ptr, *end_ptr;
11843
11844 dwarf2_read_section (objfile, &section);
11845 info_ptr = section.buffer;
11846
11847 if (info_ptr == NULL)
11848 return;
11849
11850 if (dwarf_read_debug)
11851 {
11852 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11853 get_section_name (&section),
11854 get_section_file_name (&section));
11855 }
11856
11857 end_ptr = info_ptr + section.size;
11858 while (info_ptr < end_ptr)
11859 {
11860 struct dwarf2_per_cu_data per_cu;
11861 struct create_dwo_cu_data create_dwo_cu_data;
11862 struct dwo_unit *dwo_unit;
11863 void **slot;
11864 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11865
11866 memset (&create_dwo_cu_data.dwo_unit, 0,
11867 sizeof (create_dwo_cu_data.dwo_unit));
11868 memset (&per_cu, 0, sizeof (per_cu));
11869 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11870 per_cu.is_debug_types = 0;
11871 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11872 per_cu.section = &section;
11873 create_dwo_cu_data.dwo_file = &dwo_file;
11874
11875 init_cutu_and_read_dies_no_follow (
11876 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11877 info_ptr += per_cu.length;
11878
11879 // If the unit could not be parsed, skip it.
11880 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11881 continue;
11882
11883 if (cus_htab == NULL)
11884 cus_htab = allocate_dwo_unit_table (objfile);
11885
11886 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11887 *dwo_unit = create_dwo_cu_data.dwo_unit;
11888 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11889 gdb_assert (slot != NULL);
11890 if (*slot != NULL)
11891 {
11892 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11893 sect_offset dup_sect_off = dup_cu->sect_off;
11894
11895 complaint (_("debug cu entry at offset %s is duplicate to"
11896 " the entry at offset %s, signature %s"),
11897 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11898 hex_string (dwo_unit->signature));
11899 }
11900 *slot = (void *)dwo_unit;
11901 }
11902 }
11903
11904 /* DWP file .debug_{cu,tu}_index section format:
11905 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11906
11907 DWP Version 1:
11908
11909 Both index sections have the same format, and serve to map a 64-bit
11910 signature to a set of section numbers. Each section begins with a header,
11911 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11912 indexes, and a pool of 32-bit section numbers. The index sections will be
11913 aligned at 8-byte boundaries in the file.
11914
11915 The index section header consists of:
11916
11917 V, 32 bit version number
11918 -, 32 bits unused
11919 N, 32 bit number of compilation units or type units in the index
11920 M, 32 bit number of slots in the hash table
11921
11922 Numbers are recorded using the byte order of the application binary.
11923
11924 The hash table begins at offset 16 in the section, and consists of an array
11925 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11926 order of the application binary). Unused slots in the hash table are 0.
11927 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11928
11929 The parallel table begins immediately after the hash table
11930 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11931 array of 32-bit indexes (using the byte order of the application binary),
11932 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11933 table contains a 32-bit index into the pool of section numbers. For unused
11934 hash table slots, the corresponding entry in the parallel table will be 0.
11935
11936 The pool of section numbers begins immediately following the hash table
11937 (at offset 16 + 12 * M from the beginning of the section). The pool of
11938 section numbers consists of an array of 32-bit words (using the byte order
11939 of the application binary). Each item in the array is indexed starting
11940 from 0. The hash table entry provides the index of the first section
11941 number in the set. Additional section numbers in the set follow, and the
11942 set is terminated by a 0 entry (section number 0 is not used in ELF).
11943
11944 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11945 section must be the first entry in the set, and the .debug_abbrev.dwo must
11946 be the second entry. Other members of the set may follow in any order.
11947
11948 ---
11949
11950 DWP Version 2:
11951
11952 DWP Version 2 combines all the .debug_info, etc. sections into one,
11953 and the entries in the index tables are now offsets into these sections.
11954 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11955 section.
11956
11957 Index Section Contents:
11958 Header
11959 Hash Table of Signatures dwp_hash_table.hash_table
11960 Parallel Table of Indices dwp_hash_table.unit_table
11961 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11962 Table of Section Sizes dwp_hash_table.v2.sizes
11963
11964 The index section header consists of:
11965
11966 V, 32 bit version number
11967 L, 32 bit number of columns in the table of section offsets
11968 N, 32 bit number of compilation units or type units in the index
11969 M, 32 bit number of slots in the hash table
11970
11971 Numbers are recorded using the byte order of the application binary.
11972
11973 The hash table has the same format as version 1.
11974 The parallel table of indices has the same format as version 1,
11975 except that the entries are origin-1 indices into the table of sections
11976 offsets and the table of section sizes.
11977
11978 The table of offsets begins immediately following the parallel table
11979 (at offset 16 + 12 * M from the beginning of the section). The table is
11980 a two-dimensional array of 32-bit words (using the byte order of the
11981 application binary), with L columns and N+1 rows, in row-major order.
11982 Each row in the array is indexed starting from 0. The first row provides
11983 a key to the remaining rows: each column in this row provides an identifier
11984 for a debug section, and the offsets in the same column of subsequent rows
11985 refer to that section. The section identifiers are:
11986
11987 DW_SECT_INFO 1 .debug_info.dwo
11988 DW_SECT_TYPES 2 .debug_types.dwo
11989 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11990 DW_SECT_LINE 4 .debug_line.dwo
11991 DW_SECT_LOC 5 .debug_loc.dwo
11992 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11993 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11994 DW_SECT_MACRO 8 .debug_macro.dwo
11995
11996 The offsets provided by the CU and TU index sections are the base offsets
11997 for the contributions made by each CU or TU to the corresponding section
11998 in the package file. Each CU and TU header contains an abbrev_offset
11999 field, used to find the abbreviations table for that CU or TU within the
12000 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12001 be interpreted as relative to the base offset given in the index section.
12002 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12003 should be interpreted as relative to the base offset for .debug_line.dwo,
12004 and offsets into other debug sections obtained from DWARF attributes should
12005 also be interpreted as relative to the corresponding base offset.
12006
12007 The table of sizes begins immediately following the table of offsets.
12008 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12009 with L columns and N rows, in row-major order. Each row in the array is
12010 indexed starting from 1 (row 0 is shared by the two tables).
12011
12012 ---
12013
12014 Hash table lookup is handled the same in version 1 and 2:
12015
12016 We assume that N and M will not exceed 2^32 - 1.
12017 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12018
12019 Given a 64-bit compilation unit signature or a type signature S, an entry
12020 in the hash table is located as follows:
12021
12022 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12023 the low-order k bits all set to 1.
12024
12025 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12026
12027 3) If the hash table entry at index H matches the signature, use that
12028 entry. If the hash table entry at index H is unused (all zeroes),
12029 terminate the search: the signature is not present in the table.
12030
12031 4) Let H = (H + H') modulo M. Repeat at Step 3.
12032
12033 Because M > N and H' and M are relatively prime, the search is guaranteed
12034 to stop at an unused slot or find the match. */
12035
12036 /* Create a hash table to map DWO IDs to their CU/TU entry in
12037 .debug_{info,types}.dwo in DWP_FILE.
12038 Returns NULL if there isn't one.
12039 Note: This function processes DWP files only, not DWO files. */
12040
12041 static struct dwp_hash_table *
12042 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12043 struct dwp_file *dwp_file, int is_debug_types)
12044 {
12045 struct objfile *objfile = dwarf2_per_objfile->objfile;
12046 bfd *dbfd = dwp_file->dbfd.get ();
12047 const gdb_byte *index_ptr, *index_end;
12048 struct dwarf2_section_info *index;
12049 uint32_t version, nr_columns, nr_units, nr_slots;
12050 struct dwp_hash_table *htab;
12051
12052 if (is_debug_types)
12053 index = &dwp_file->sections.tu_index;
12054 else
12055 index = &dwp_file->sections.cu_index;
12056
12057 if (dwarf2_section_empty_p (index))
12058 return NULL;
12059 dwarf2_read_section (objfile, index);
12060
12061 index_ptr = index->buffer;
12062 index_end = index_ptr + index->size;
12063
12064 version = read_4_bytes (dbfd, index_ptr);
12065 index_ptr += 4;
12066 if (version == 2)
12067 nr_columns = read_4_bytes (dbfd, index_ptr);
12068 else
12069 nr_columns = 0;
12070 index_ptr += 4;
12071 nr_units = read_4_bytes (dbfd, index_ptr);
12072 index_ptr += 4;
12073 nr_slots = read_4_bytes (dbfd, index_ptr);
12074 index_ptr += 4;
12075
12076 if (version != 1 && version != 2)
12077 {
12078 error (_("Dwarf Error: unsupported DWP file version (%s)"
12079 " [in module %s]"),
12080 pulongest (version), dwp_file->name);
12081 }
12082 if (nr_slots != (nr_slots & -nr_slots))
12083 {
12084 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12085 " is not power of 2 [in module %s]"),
12086 pulongest (nr_slots), dwp_file->name);
12087 }
12088
12089 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12090 htab->version = version;
12091 htab->nr_columns = nr_columns;
12092 htab->nr_units = nr_units;
12093 htab->nr_slots = nr_slots;
12094 htab->hash_table = index_ptr;
12095 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12096
12097 /* Exit early if the table is empty. */
12098 if (nr_slots == 0 || nr_units == 0
12099 || (version == 2 && nr_columns == 0))
12100 {
12101 /* All must be zero. */
12102 if (nr_slots != 0 || nr_units != 0
12103 || (version == 2 && nr_columns != 0))
12104 {
12105 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12106 " all zero [in modules %s]"),
12107 dwp_file->name);
12108 }
12109 return htab;
12110 }
12111
12112 if (version == 1)
12113 {
12114 htab->section_pool.v1.indices =
12115 htab->unit_table + sizeof (uint32_t) * nr_slots;
12116 /* It's harder to decide whether the section is too small in v1.
12117 V1 is deprecated anyway so we punt. */
12118 }
12119 else
12120 {
12121 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12122 int *ids = htab->section_pool.v2.section_ids;
12123 /* Reverse map for error checking. */
12124 int ids_seen[DW_SECT_MAX + 1];
12125 int i;
12126
12127 if (nr_columns < 2)
12128 {
12129 error (_("Dwarf Error: bad DWP hash table, too few columns"
12130 " in section table [in module %s]"),
12131 dwp_file->name);
12132 }
12133 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12134 {
12135 error (_("Dwarf Error: bad DWP hash table, too many columns"
12136 " in section table [in module %s]"),
12137 dwp_file->name);
12138 }
12139 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12140 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12141 for (i = 0; i < nr_columns; ++i)
12142 {
12143 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12144
12145 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12146 {
12147 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12148 " in section table [in module %s]"),
12149 id, dwp_file->name);
12150 }
12151 if (ids_seen[id] != -1)
12152 {
12153 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12154 " id %d in section table [in module %s]"),
12155 id, dwp_file->name);
12156 }
12157 ids_seen[id] = i;
12158 ids[i] = id;
12159 }
12160 /* Must have exactly one info or types section. */
12161 if (((ids_seen[DW_SECT_INFO] != -1)
12162 + (ids_seen[DW_SECT_TYPES] != -1))
12163 != 1)
12164 {
12165 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12166 " DWO info/types section [in module %s]"),
12167 dwp_file->name);
12168 }
12169 /* Must have an abbrev section. */
12170 if (ids_seen[DW_SECT_ABBREV] == -1)
12171 {
12172 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12173 " section [in module %s]"),
12174 dwp_file->name);
12175 }
12176 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12177 htab->section_pool.v2.sizes =
12178 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12179 * nr_units * nr_columns);
12180 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12181 * nr_units * nr_columns))
12182 > index_end)
12183 {
12184 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12185 " [in module %s]"),
12186 dwp_file->name);
12187 }
12188 }
12189
12190 return htab;
12191 }
12192
12193 /* Update SECTIONS with the data from SECTP.
12194
12195 This function is like the other "locate" section routines that are
12196 passed to bfd_map_over_sections, but in this context the sections to
12197 read comes from the DWP V1 hash table, not the full ELF section table.
12198
12199 The result is non-zero for success, or zero if an error was found. */
12200
12201 static int
12202 locate_v1_virtual_dwo_sections (asection *sectp,
12203 struct virtual_v1_dwo_sections *sections)
12204 {
12205 const struct dwop_section_names *names = &dwop_section_names;
12206
12207 if (section_is_p (sectp->name, &names->abbrev_dwo))
12208 {
12209 /* There can be only one. */
12210 if (sections->abbrev.s.section != NULL)
12211 return 0;
12212 sections->abbrev.s.section = sectp;
12213 sections->abbrev.size = bfd_get_section_size (sectp);
12214 }
12215 else if (section_is_p (sectp->name, &names->info_dwo)
12216 || section_is_p (sectp->name, &names->types_dwo))
12217 {
12218 /* There can be only one. */
12219 if (sections->info_or_types.s.section != NULL)
12220 return 0;
12221 sections->info_or_types.s.section = sectp;
12222 sections->info_or_types.size = bfd_get_section_size (sectp);
12223 }
12224 else if (section_is_p (sectp->name, &names->line_dwo))
12225 {
12226 /* There can be only one. */
12227 if (sections->line.s.section != NULL)
12228 return 0;
12229 sections->line.s.section = sectp;
12230 sections->line.size = bfd_get_section_size (sectp);
12231 }
12232 else if (section_is_p (sectp->name, &names->loc_dwo))
12233 {
12234 /* There can be only one. */
12235 if (sections->loc.s.section != NULL)
12236 return 0;
12237 sections->loc.s.section = sectp;
12238 sections->loc.size = bfd_get_section_size (sectp);
12239 }
12240 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12241 {
12242 /* There can be only one. */
12243 if (sections->macinfo.s.section != NULL)
12244 return 0;
12245 sections->macinfo.s.section = sectp;
12246 sections->macinfo.size = bfd_get_section_size (sectp);
12247 }
12248 else if (section_is_p (sectp->name, &names->macro_dwo))
12249 {
12250 /* There can be only one. */
12251 if (sections->macro.s.section != NULL)
12252 return 0;
12253 sections->macro.s.section = sectp;
12254 sections->macro.size = bfd_get_section_size (sectp);
12255 }
12256 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12257 {
12258 /* There can be only one. */
12259 if (sections->str_offsets.s.section != NULL)
12260 return 0;
12261 sections->str_offsets.s.section = sectp;
12262 sections->str_offsets.size = bfd_get_section_size (sectp);
12263 }
12264 else
12265 {
12266 /* No other kind of section is valid. */
12267 return 0;
12268 }
12269
12270 return 1;
12271 }
12272
12273 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12274 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12275 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12276 This is for DWP version 1 files. */
12277
12278 static struct dwo_unit *
12279 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12280 struct dwp_file *dwp_file,
12281 uint32_t unit_index,
12282 const char *comp_dir,
12283 ULONGEST signature, int is_debug_types)
12284 {
12285 struct objfile *objfile = dwarf2_per_objfile->objfile;
12286 const struct dwp_hash_table *dwp_htab =
12287 is_debug_types ? dwp_file->tus : dwp_file->cus;
12288 bfd *dbfd = dwp_file->dbfd.get ();
12289 const char *kind = is_debug_types ? "TU" : "CU";
12290 struct dwo_file *dwo_file;
12291 struct dwo_unit *dwo_unit;
12292 struct virtual_v1_dwo_sections sections;
12293 void **dwo_file_slot;
12294 int i;
12295
12296 gdb_assert (dwp_file->version == 1);
12297
12298 if (dwarf_read_debug)
12299 {
12300 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12301 kind,
12302 pulongest (unit_index), hex_string (signature),
12303 dwp_file->name);
12304 }
12305
12306 /* Fetch the sections of this DWO unit.
12307 Put a limit on the number of sections we look for so that bad data
12308 doesn't cause us to loop forever. */
12309
12310 #define MAX_NR_V1_DWO_SECTIONS \
12311 (1 /* .debug_info or .debug_types */ \
12312 + 1 /* .debug_abbrev */ \
12313 + 1 /* .debug_line */ \
12314 + 1 /* .debug_loc */ \
12315 + 1 /* .debug_str_offsets */ \
12316 + 1 /* .debug_macro or .debug_macinfo */ \
12317 + 1 /* trailing zero */)
12318
12319 memset (&sections, 0, sizeof (sections));
12320
12321 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12322 {
12323 asection *sectp;
12324 uint32_t section_nr =
12325 read_4_bytes (dbfd,
12326 dwp_htab->section_pool.v1.indices
12327 + (unit_index + i) * sizeof (uint32_t));
12328
12329 if (section_nr == 0)
12330 break;
12331 if (section_nr >= dwp_file->num_sections)
12332 {
12333 error (_("Dwarf Error: bad DWP hash table, section number too large"
12334 " [in module %s]"),
12335 dwp_file->name);
12336 }
12337
12338 sectp = dwp_file->elf_sections[section_nr];
12339 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12340 {
12341 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12342 " [in module %s]"),
12343 dwp_file->name);
12344 }
12345 }
12346
12347 if (i < 2
12348 || dwarf2_section_empty_p (&sections.info_or_types)
12349 || dwarf2_section_empty_p (&sections.abbrev))
12350 {
12351 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12352 " [in module %s]"),
12353 dwp_file->name);
12354 }
12355 if (i == MAX_NR_V1_DWO_SECTIONS)
12356 {
12357 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12358 " [in module %s]"),
12359 dwp_file->name);
12360 }
12361
12362 /* It's easier for the rest of the code if we fake a struct dwo_file and
12363 have dwo_unit "live" in that. At least for now.
12364
12365 The DWP file can be made up of a random collection of CUs and TUs.
12366 However, for each CU + set of TUs that came from the same original DWO
12367 file, we can combine them back into a virtual DWO file to save space
12368 (fewer struct dwo_file objects to allocate). Remember that for really
12369 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12370
12371 std::string virtual_dwo_name =
12372 string_printf ("virtual-dwo/%d-%d-%d-%d",
12373 get_section_id (&sections.abbrev),
12374 get_section_id (&sections.line),
12375 get_section_id (&sections.loc),
12376 get_section_id (&sections.str_offsets));
12377 /* Can we use an existing virtual DWO file? */
12378 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12379 virtual_dwo_name.c_str (),
12380 comp_dir);
12381 /* Create one if necessary. */
12382 if (*dwo_file_slot == NULL)
12383 {
12384 if (dwarf_read_debug)
12385 {
12386 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12387 virtual_dwo_name.c_str ());
12388 }
12389 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12390 dwo_file->dwo_name
12391 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12392 virtual_dwo_name.c_str (),
12393 virtual_dwo_name.size ());
12394 dwo_file->comp_dir = comp_dir;
12395 dwo_file->sections.abbrev = sections.abbrev;
12396 dwo_file->sections.line = sections.line;
12397 dwo_file->sections.loc = sections.loc;
12398 dwo_file->sections.macinfo = sections.macinfo;
12399 dwo_file->sections.macro = sections.macro;
12400 dwo_file->sections.str_offsets = sections.str_offsets;
12401 /* The "str" section is global to the entire DWP file. */
12402 dwo_file->sections.str = dwp_file->sections.str;
12403 /* The info or types section is assigned below to dwo_unit,
12404 there's no need to record it in dwo_file.
12405 Also, we can't simply record type sections in dwo_file because
12406 we record a pointer into the vector in dwo_unit. As we collect more
12407 types we'll grow the vector and eventually have to reallocate space
12408 for it, invalidating all copies of pointers into the previous
12409 contents. */
12410 *dwo_file_slot = dwo_file;
12411 }
12412 else
12413 {
12414 if (dwarf_read_debug)
12415 {
12416 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12417 virtual_dwo_name.c_str ());
12418 }
12419 dwo_file = (struct dwo_file *) *dwo_file_slot;
12420 }
12421
12422 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12423 dwo_unit->dwo_file = dwo_file;
12424 dwo_unit->signature = signature;
12425 dwo_unit->section =
12426 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12427 *dwo_unit->section = sections.info_or_types;
12428 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12429
12430 return dwo_unit;
12431 }
12432
12433 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12434 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12435 piece within that section used by a TU/CU, return a virtual section
12436 of just that piece. */
12437
12438 static struct dwarf2_section_info
12439 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12440 struct dwarf2_section_info *section,
12441 bfd_size_type offset, bfd_size_type size)
12442 {
12443 struct dwarf2_section_info result;
12444 asection *sectp;
12445
12446 gdb_assert (section != NULL);
12447 gdb_assert (!section->is_virtual);
12448
12449 memset (&result, 0, sizeof (result));
12450 result.s.containing_section = section;
12451 result.is_virtual = 1;
12452
12453 if (size == 0)
12454 return result;
12455
12456 sectp = get_section_bfd_section (section);
12457
12458 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12459 bounds of the real section. This is a pretty-rare event, so just
12460 flag an error (easier) instead of a warning and trying to cope. */
12461 if (sectp == NULL
12462 || offset + size > bfd_get_section_size (sectp))
12463 {
12464 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12465 " in section %s [in module %s]"),
12466 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12467 objfile_name (dwarf2_per_objfile->objfile));
12468 }
12469
12470 result.virtual_offset = offset;
12471 result.size = size;
12472 return result;
12473 }
12474
12475 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12476 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12477 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12478 This is for DWP version 2 files. */
12479
12480 static struct dwo_unit *
12481 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12482 struct dwp_file *dwp_file,
12483 uint32_t unit_index,
12484 const char *comp_dir,
12485 ULONGEST signature, int is_debug_types)
12486 {
12487 struct objfile *objfile = dwarf2_per_objfile->objfile;
12488 const struct dwp_hash_table *dwp_htab =
12489 is_debug_types ? dwp_file->tus : dwp_file->cus;
12490 bfd *dbfd = dwp_file->dbfd.get ();
12491 const char *kind = is_debug_types ? "TU" : "CU";
12492 struct dwo_file *dwo_file;
12493 struct dwo_unit *dwo_unit;
12494 struct virtual_v2_dwo_sections sections;
12495 void **dwo_file_slot;
12496 int i;
12497
12498 gdb_assert (dwp_file->version == 2);
12499
12500 if (dwarf_read_debug)
12501 {
12502 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12503 kind,
12504 pulongest (unit_index), hex_string (signature),
12505 dwp_file->name);
12506 }
12507
12508 /* Fetch the section offsets of this DWO unit. */
12509
12510 memset (&sections, 0, sizeof (sections));
12511
12512 for (i = 0; i < dwp_htab->nr_columns; ++i)
12513 {
12514 uint32_t offset = read_4_bytes (dbfd,
12515 dwp_htab->section_pool.v2.offsets
12516 + (((unit_index - 1) * dwp_htab->nr_columns
12517 + i)
12518 * sizeof (uint32_t)));
12519 uint32_t size = read_4_bytes (dbfd,
12520 dwp_htab->section_pool.v2.sizes
12521 + (((unit_index - 1) * dwp_htab->nr_columns
12522 + i)
12523 * sizeof (uint32_t)));
12524
12525 switch (dwp_htab->section_pool.v2.section_ids[i])
12526 {
12527 case DW_SECT_INFO:
12528 case DW_SECT_TYPES:
12529 sections.info_or_types_offset = offset;
12530 sections.info_or_types_size = size;
12531 break;
12532 case DW_SECT_ABBREV:
12533 sections.abbrev_offset = offset;
12534 sections.abbrev_size = size;
12535 break;
12536 case DW_SECT_LINE:
12537 sections.line_offset = offset;
12538 sections.line_size = size;
12539 break;
12540 case DW_SECT_LOC:
12541 sections.loc_offset = offset;
12542 sections.loc_size = size;
12543 break;
12544 case DW_SECT_STR_OFFSETS:
12545 sections.str_offsets_offset = offset;
12546 sections.str_offsets_size = size;
12547 break;
12548 case DW_SECT_MACINFO:
12549 sections.macinfo_offset = offset;
12550 sections.macinfo_size = size;
12551 break;
12552 case DW_SECT_MACRO:
12553 sections.macro_offset = offset;
12554 sections.macro_size = size;
12555 break;
12556 }
12557 }
12558
12559 /* It's easier for the rest of the code if we fake a struct dwo_file and
12560 have dwo_unit "live" in that. At least for now.
12561
12562 The DWP file can be made up of a random collection of CUs and TUs.
12563 However, for each CU + set of TUs that came from the same original DWO
12564 file, we can combine them back into a virtual DWO file to save space
12565 (fewer struct dwo_file objects to allocate). Remember that for really
12566 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12567
12568 std::string virtual_dwo_name =
12569 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12570 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12571 (long) (sections.line_size ? sections.line_offset : 0),
12572 (long) (sections.loc_size ? sections.loc_offset : 0),
12573 (long) (sections.str_offsets_size
12574 ? sections.str_offsets_offset : 0));
12575 /* Can we use an existing virtual DWO file? */
12576 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12577 virtual_dwo_name.c_str (),
12578 comp_dir);
12579 /* Create one if necessary. */
12580 if (*dwo_file_slot == NULL)
12581 {
12582 if (dwarf_read_debug)
12583 {
12584 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12585 virtual_dwo_name.c_str ());
12586 }
12587 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12588 dwo_file->dwo_name
12589 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12590 virtual_dwo_name.c_str (),
12591 virtual_dwo_name.size ());
12592 dwo_file->comp_dir = comp_dir;
12593 dwo_file->sections.abbrev =
12594 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12595 sections.abbrev_offset, sections.abbrev_size);
12596 dwo_file->sections.line =
12597 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12598 sections.line_offset, sections.line_size);
12599 dwo_file->sections.loc =
12600 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12601 sections.loc_offset, sections.loc_size);
12602 dwo_file->sections.macinfo =
12603 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12604 sections.macinfo_offset, sections.macinfo_size);
12605 dwo_file->sections.macro =
12606 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12607 sections.macro_offset, sections.macro_size);
12608 dwo_file->sections.str_offsets =
12609 create_dwp_v2_section (dwarf2_per_objfile,
12610 &dwp_file->sections.str_offsets,
12611 sections.str_offsets_offset,
12612 sections.str_offsets_size);
12613 /* The "str" section is global to the entire DWP file. */
12614 dwo_file->sections.str = dwp_file->sections.str;
12615 /* The info or types section is assigned below to dwo_unit,
12616 there's no need to record it in dwo_file.
12617 Also, we can't simply record type sections in dwo_file because
12618 we record a pointer into the vector in dwo_unit. As we collect more
12619 types we'll grow the vector and eventually have to reallocate space
12620 for it, invalidating all copies of pointers into the previous
12621 contents. */
12622 *dwo_file_slot = dwo_file;
12623 }
12624 else
12625 {
12626 if (dwarf_read_debug)
12627 {
12628 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12629 virtual_dwo_name.c_str ());
12630 }
12631 dwo_file = (struct dwo_file *) *dwo_file_slot;
12632 }
12633
12634 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12635 dwo_unit->dwo_file = dwo_file;
12636 dwo_unit->signature = signature;
12637 dwo_unit->section =
12638 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12639 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12640 is_debug_types
12641 ? &dwp_file->sections.types
12642 : &dwp_file->sections.info,
12643 sections.info_or_types_offset,
12644 sections.info_or_types_size);
12645 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12646
12647 return dwo_unit;
12648 }
12649
12650 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12651 Returns NULL if the signature isn't found. */
12652
12653 static struct dwo_unit *
12654 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12655 struct dwp_file *dwp_file, const char *comp_dir,
12656 ULONGEST signature, int is_debug_types)
12657 {
12658 const struct dwp_hash_table *dwp_htab =
12659 is_debug_types ? dwp_file->tus : dwp_file->cus;
12660 bfd *dbfd = dwp_file->dbfd.get ();
12661 uint32_t mask = dwp_htab->nr_slots - 1;
12662 uint32_t hash = signature & mask;
12663 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12664 unsigned int i;
12665 void **slot;
12666 struct dwo_unit find_dwo_cu;
12667
12668 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12669 find_dwo_cu.signature = signature;
12670 slot = htab_find_slot (is_debug_types
12671 ? dwp_file->loaded_tus
12672 : dwp_file->loaded_cus,
12673 &find_dwo_cu, INSERT);
12674
12675 if (*slot != NULL)
12676 return (struct dwo_unit *) *slot;
12677
12678 /* Use a for loop so that we don't loop forever on bad debug info. */
12679 for (i = 0; i < dwp_htab->nr_slots; ++i)
12680 {
12681 ULONGEST signature_in_table;
12682
12683 signature_in_table =
12684 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12685 if (signature_in_table == signature)
12686 {
12687 uint32_t unit_index =
12688 read_4_bytes (dbfd,
12689 dwp_htab->unit_table + hash * sizeof (uint32_t));
12690
12691 if (dwp_file->version == 1)
12692 {
12693 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12694 dwp_file, unit_index,
12695 comp_dir, signature,
12696 is_debug_types);
12697 }
12698 else
12699 {
12700 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12701 dwp_file, unit_index,
12702 comp_dir, signature,
12703 is_debug_types);
12704 }
12705 return (struct dwo_unit *) *slot;
12706 }
12707 if (signature_in_table == 0)
12708 return NULL;
12709 hash = (hash + hash2) & mask;
12710 }
12711
12712 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12713 " [in module %s]"),
12714 dwp_file->name);
12715 }
12716
12717 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12718 Open the file specified by FILE_NAME and hand it off to BFD for
12719 preliminary analysis. Return a newly initialized bfd *, which
12720 includes a canonicalized copy of FILE_NAME.
12721 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12722 SEARCH_CWD is true if the current directory is to be searched.
12723 It will be searched before debug-file-directory.
12724 If successful, the file is added to the bfd include table of the
12725 objfile's bfd (see gdb_bfd_record_inclusion).
12726 If unable to find/open the file, return NULL.
12727 NOTE: This function is derived from symfile_bfd_open. */
12728
12729 static gdb_bfd_ref_ptr
12730 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12731 const char *file_name, int is_dwp, int search_cwd)
12732 {
12733 int desc;
12734 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12735 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12736 to debug_file_directory. */
12737 const char *search_path;
12738 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12739
12740 gdb::unique_xmalloc_ptr<char> search_path_holder;
12741 if (search_cwd)
12742 {
12743 if (*debug_file_directory != '\0')
12744 {
12745 search_path_holder.reset (concat (".", dirname_separator_string,
12746 debug_file_directory,
12747 (char *) NULL));
12748 search_path = search_path_holder.get ();
12749 }
12750 else
12751 search_path = ".";
12752 }
12753 else
12754 search_path = debug_file_directory;
12755
12756 openp_flags flags = OPF_RETURN_REALPATH;
12757 if (is_dwp)
12758 flags |= OPF_SEARCH_IN_PATH;
12759
12760 gdb::unique_xmalloc_ptr<char> absolute_name;
12761 desc = openp (search_path, flags, file_name,
12762 O_RDONLY | O_BINARY, &absolute_name);
12763 if (desc < 0)
12764 return NULL;
12765
12766 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12767 gnutarget, desc));
12768 if (sym_bfd == NULL)
12769 return NULL;
12770 bfd_set_cacheable (sym_bfd.get (), 1);
12771
12772 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12773 return NULL;
12774
12775 /* Success. Record the bfd as having been included by the objfile's bfd.
12776 This is important because things like demangled_names_hash lives in the
12777 objfile's per_bfd space and may have references to things like symbol
12778 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12779 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12780
12781 return sym_bfd;
12782 }
12783
12784 /* Try to open DWO file FILE_NAME.
12785 COMP_DIR is the DW_AT_comp_dir attribute.
12786 The result is the bfd handle of the file.
12787 If there is a problem finding or opening the file, return NULL.
12788 Upon success, the canonicalized path of the file is stored in the bfd,
12789 same as symfile_bfd_open. */
12790
12791 static gdb_bfd_ref_ptr
12792 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12793 const char *file_name, const char *comp_dir)
12794 {
12795 if (IS_ABSOLUTE_PATH (file_name))
12796 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12797 0 /*is_dwp*/, 0 /*search_cwd*/);
12798
12799 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12800
12801 if (comp_dir != NULL)
12802 {
12803 char *path_to_try = concat (comp_dir, SLASH_STRING,
12804 file_name, (char *) NULL);
12805
12806 /* NOTE: If comp_dir is a relative path, this will also try the
12807 search path, which seems useful. */
12808 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12809 path_to_try,
12810 0 /*is_dwp*/,
12811 1 /*search_cwd*/));
12812 xfree (path_to_try);
12813 if (abfd != NULL)
12814 return abfd;
12815 }
12816
12817 /* That didn't work, try debug-file-directory, which, despite its name,
12818 is a list of paths. */
12819
12820 if (*debug_file_directory == '\0')
12821 return NULL;
12822
12823 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12824 0 /*is_dwp*/, 1 /*search_cwd*/);
12825 }
12826
12827 /* This function is mapped across the sections and remembers the offset and
12828 size of each of the DWO debugging sections we are interested in. */
12829
12830 static void
12831 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12832 {
12833 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12834 const struct dwop_section_names *names = &dwop_section_names;
12835
12836 if (section_is_p (sectp->name, &names->abbrev_dwo))
12837 {
12838 dwo_sections->abbrev.s.section = sectp;
12839 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12840 }
12841 else if (section_is_p (sectp->name, &names->info_dwo))
12842 {
12843 dwo_sections->info.s.section = sectp;
12844 dwo_sections->info.size = bfd_get_section_size (sectp);
12845 }
12846 else if (section_is_p (sectp->name, &names->line_dwo))
12847 {
12848 dwo_sections->line.s.section = sectp;
12849 dwo_sections->line.size = bfd_get_section_size (sectp);
12850 }
12851 else if (section_is_p (sectp->name, &names->loc_dwo))
12852 {
12853 dwo_sections->loc.s.section = sectp;
12854 dwo_sections->loc.size = bfd_get_section_size (sectp);
12855 }
12856 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12857 {
12858 dwo_sections->macinfo.s.section = sectp;
12859 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12860 }
12861 else if (section_is_p (sectp->name, &names->macro_dwo))
12862 {
12863 dwo_sections->macro.s.section = sectp;
12864 dwo_sections->macro.size = bfd_get_section_size (sectp);
12865 }
12866 else if (section_is_p (sectp->name, &names->str_dwo))
12867 {
12868 dwo_sections->str.s.section = sectp;
12869 dwo_sections->str.size = bfd_get_section_size (sectp);
12870 }
12871 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12872 {
12873 dwo_sections->str_offsets.s.section = sectp;
12874 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12875 }
12876 else if (section_is_p (sectp->name, &names->types_dwo))
12877 {
12878 struct dwarf2_section_info type_section;
12879
12880 memset (&type_section, 0, sizeof (type_section));
12881 type_section.s.section = sectp;
12882 type_section.size = bfd_get_section_size (sectp);
12883 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12884 &type_section);
12885 }
12886 }
12887
12888 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12889 by PER_CU. This is for the non-DWP case.
12890 The result is NULL if DWO_NAME can't be found. */
12891
12892 static struct dwo_file *
12893 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12894 const char *dwo_name, const char *comp_dir)
12895 {
12896 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12897 struct objfile *objfile = dwarf2_per_objfile->objfile;
12898
12899 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
12900 if (dbfd == NULL)
12901 {
12902 if (dwarf_read_debug)
12903 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12904 return NULL;
12905 }
12906
12907 /* We use a unique pointer here, despite the obstack allocation,
12908 because a dwo_file needs some cleanup if it is abandoned. */
12909 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12910 struct dwo_file));
12911 dwo_file->dwo_name = dwo_name;
12912 dwo_file->comp_dir = comp_dir;
12913 dwo_file->dbfd = dbfd.release ();
12914
12915 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12916 &dwo_file->sections);
12917
12918 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12919 dwo_file->cus);
12920
12921 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12922 dwo_file->sections.types, dwo_file->tus);
12923
12924 if (dwarf_read_debug)
12925 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12926
12927 return dwo_file.release ();
12928 }
12929
12930 /* This function is mapped across the sections and remembers the offset and
12931 size of each of the DWP debugging sections common to version 1 and 2 that
12932 we are interested in. */
12933
12934 static void
12935 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12936 void *dwp_file_ptr)
12937 {
12938 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12939 const struct dwop_section_names *names = &dwop_section_names;
12940 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12941
12942 /* Record the ELF section number for later lookup: this is what the
12943 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12944 gdb_assert (elf_section_nr < dwp_file->num_sections);
12945 dwp_file->elf_sections[elf_section_nr] = sectp;
12946
12947 /* Look for specific sections that we need. */
12948 if (section_is_p (sectp->name, &names->str_dwo))
12949 {
12950 dwp_file->sections.str.s.section = sectp;
12951 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12952 }
12953 else if (section_is_p (sectp->name, &names->cu_index))
12954 {
12955 dwp_file->sections.cu_index.s.section = sectp;
12956 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12957 }
12958 else if (section_is_p (sectp->name, &names->tu_index))
12959 {
12960 dwp_file->sections.tu_index.s.section = sectp;
12961 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12962 }
12963 }
12964
12965 /* This function is mapped across the sections and remembers the offset and
12966 size of each of the DWP version 2 debugging sections that we are interested
12967 in. This is split into a separate function because we don't know if we
12968 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12969
12970 static void
12971 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12972 {
12973 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12974 const struct dwop_section_names *names = &dwop_section_names;
12975 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12976
12977 /* Record the ELF section number for later lookup: this is what the
12978 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12979 gdb_assert (elf_section_nr < dwp_file->num_sections);
12980 dwp_file->elf_sections[elf_section_nr] = sectp;
12981
12982 /* Look for specific sections that we need. */
12983 if (section_is_p (sectp->name, &names->abbrev_dwo))
12984 {
12985 dwp_file->sections.abbrev.s.section = sectp;
12986 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
12987 }
12988 else if (section_is_p (sectp->name, &names->info_dwo))
12989 {
12990 dwp_file->sections.info.s.section = sectp;
12991 dwp_file->sections.info.size = bfd_get_section_size (sectp);
12992 }
12993 else if (section_is_p (sectp->name, &names->line_dwo))
12994 {
12995 dwp_file->sections.line.s.section = sectp;
12996 dwp_file->sections.line.size = bfd_get_section_size (sectp);
12997 }
12998 else if (section_is_p (sectp->name, &names->loc_dwo))
12999 {
13000 dwp_file->sections.loc.s.section = sectp;
13001 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13002 }
13003 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13004 {
13005 dwp_file->sections.macinfo.s.section = sectp;
13006 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13007 }
13008 else if (section_is_p (sectp->name, &names->macro_dwo))
13009 {
13010 dwp_file->sections.macro.s.section = sectp;
13011 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13012 }
13013 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13014 {
13015 dwp_file->sections.str_offsets.s.section = sectp;
13016 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13017 }
13018 else if (section_is_p (sectp->name, &names->types_dwo))
13019 {
13020 dwp_file->sections.types.s.section = sectp;
13021 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13022 }
13023 }
13024
13025 /* Hash function for dwp_file loaded CUs/TUs. */
13026
13027 static hashval_t
13028 hash_dwp_loaded_cutus (const void *item)
13029 {
13030 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13031
13032 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13033 return dwo_unit->signature;
13034 }
13035
13036 /* Equality function for dwp_file loaded CUs/TUs. */
13037
13038 static int
13039 eq_dwp_loaded_cutus (const void *a, const void *b)
13040 {
13041 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13042 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13043
13044 return dua->signature == dub->signature;
13045 }
13046
13047 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13048
13049 static htab_t
13050 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13051 {
13052 return htab_create_alloc_ex (3,
13053 hash_dwp_loaded_cutus,
13054 eq_dwp_loaded_cutus,
13055 NULL,
13056 &objfile->objfile_obstack,
13057 hashtab_obstack_allocate,
13058 dummy_obstack_deallocate);
13059 }
13060
13061 /* Try to open DWP file FILE_NAME.
13062 The result is the bfd handle of the file.
13063 If there is a problem finding or opening the file, return NULL.
13064 Upon success, the canonicalized path of the file is stored in the bfd,
13065 same as symfile_bfd_open. */
13066
13067 static gdb_bfd_ref_ptr
13068 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13069 const char *file_name)
13070 {
13071 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13072 1 /*is_dwp*/,
13073 1 /*search_cwd*/));
13074 if (abfd != NULL)
13075 return abfd;
13076
13077 /* Work around upstream bug 15652.
13078 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13079 [Whether that's a "bug" is debatable, but it is getting in our way.]
13080 We have no real idea where the dwp file is, because gdb's realpath-ing
13081 of the executable's path may have discarded the needed info.
13082 [IWBN if the dwp file name was recorded in the executable, akin to
13083 .gnu_debuglink, but that doesn't exist yet.]
13084 Strip the directory from FILE_NAME and search again. */
13085 if (*debug_file_directory != '\0')
13086 {
13087 /* Don't implicitly search the current directory here.
13088 If the user wants to search "." to handle this case,
13089 it must be added to debug-file-directory. */
13090 return try_open_dwop_file (dwarf2_per_objfile,
13091 lbasename (file_name), 1 /*is_dwp*/,
13092 0 /*search_cwd*/);
13093 }
13094
13095 return NULL;
13096 }
13097
13098 /* Initialize the use of the DWP file for the current objfile.
13099 By convention the name of the DWP file is ${objfile}.dwp.
13100 The result is NULL if it can't be found. */
13101
13102 static std::unique_ptr<struct dwp_file>
13103 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13104 {
13105 struct objfile *objfile = dwarf2_per_objfile->objfile;
13106
13107 /* Try to find first .dwp for the binary file before any symbolic links
13108 resolving. */
13109
13110 /* If the objfile is a debug file, find the name of the real binary
13111 file and get the name of dwp file from there. */
13112 std::string dwp_name;
13113 if (objfile->separate_debug_objfile_backlink != NULL)
13114 {
13115 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13116 const char *backlink_basename = lbasename (backlink->original_name);
13117
13118 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13119 }
13120 else
13121 dwp_name = objfile->original_name;
13122
13123 dwp_name += ".dwp";
13124
13125 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13126 if (dbfd == NULL
13127 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13128 {
13129 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13130 dwp_name = objfile_name (objfile);
13131 dwp_name += ".dwp";
13132 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13133 }
13134
13135 if (dbfd == NULL)
13136 {
13137 if (dwarf_read_debug)
13138 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13139 return std::unique_ptr<dwp_file> ();
13140 }
13141
13142 const char *name = bfd_get_filename (dbfd.get ());
13143 std::unique_ptr<struct dwp_file> dwp_file
13144 (new struct dwp_file (name, std::move (dbfd)));
13145
13146 /* +1: section 0 is unused */
13147 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13148 dwp_file->elf_sections =
13149 OBSTACK_CALLOC (&objfile->objfile_obstack,
13150 dwp_file->num_sections, asection *);
13151
13152 bfd_map_over_sections (dwp_file->dbfd.get (),
13153 dwarf2_locate_common_dwp_sections,
13154 dwp_file.get ());
13155
13156 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13157 0);
13158
13159 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13160 1);
13161
13162 /* The DWP file version is stored in the hash table. Oh well. */
13163 if (dwp_file->cus && dwp_file->tus
13164 && dwp_file->cus->version != dwp_file->tus->version)
13165 {
13166 /* Technically speaking, we should try to limp along, but this is
13167 pretty bizarre. We use pulongest here because that's the established
13168 portability solution (e.g, we cannot use %u for uint32_t). */
13169 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13170 " TU version %s [in DWP file %s]"),
13171 pulongest (dwp_file->cus->version),
13172 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13173 }
13174
13175 if (dwp_file->cus)
13176 dwp_file->version = dwp_file->cus->version;
13177 else if (dwp_file->tus)
13178 dwp_file->version = dwp_file->tus->version;
13179 else
13180 dwp_file->version = 2;
13181
13182 if (dwp_file->version == 2)
13183 bfd_map_over_sections (dwp_file->dbfd.get (),
13184 dwarf2_locate_v2_dwp_sections,
13185 dwp_file.get ());
13186
13187 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13188 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13189
13190 if (dwarf_read_debug)
13191 {
13192 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13193 fprintf_unfiltered (gdb_stdlog,
13194 " %s CUs, %s TUs\n",
13195 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13196 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13197 }
13198
13199 return dwp_file;
13200 }
13201
13202 /* Wrapper around open_and_init_dwp_file, only open it once. */
13203
13204 static struct dwp_file *
13205 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13206 {
13207 if (! dwarf2_per_objfile->dwp_checked)
13208 {
13209 dwarf2_per_objfile->dwp_file
13210 = open_and_init_dwp_file (dwarf2_per_objfile);
13211 dwarf2_per_objfile->dwp_checked = 1;
13212 }
13213 return dwarf2_per_objfile->dwp_file.get ();
13214 }
13215
13216 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13217 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13218 or in the DWP file for the objfile, referenced by THIS_UNIT.
13219 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13220 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13221
13222 This is called, for example, when wanting to read a variable with a
13223 complex location. Therefore we don't want to do file i/o for every call.
13224 Therefore we don't want to look for a DWO file on every call.
13225 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13226 then we check if we've already seen DWO_NAME, and only THEN do we check
13227 for a DWO file.
13228
13229 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13230 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13231
13232 static struct dwo_unit *
13233 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13234 const char *dwo_name, const char *comp_dir,
13235 ULONGEST signature, int is_debug_types)
13236 {
13237 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13238 struct objfile *objfile = dwarf2_per_objfile->objfile;
13239 const char *kind = is_debug_types ? "TU" : "CU";
13240 void **dwo_file_slot;
13241 struct dwo_file *dwo_file;
13242 struct dwp_file *dwp_file;
13243
13244 /* First see if there's a DWP file.
13245 If we have a DWP file but didn't find the DWO inside it, don't
13246 look for the original DWO file. It makes gdb behave differently
13247 depending on whether one is debugging in the build tree. */
13248
13249 dwp_file = get_dwp_file (dwarf2_per_objfile);
13250 if (dwp_file != NULL)
13251 {
13252 const struct dwp_hash_table *dwp_htab =
13253 is_debug_types ? dwp_file->tus : dwp_file->cus;
13254
13255 if (dwp_htab != NULL)
13256 {
13257 struct dwo_unit *dwo_cutu =
13258 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13259 signature, is_debug_types);
13260
13261 if (dwo_cutu != NULL)
13262 {
13263 if (dwarf_read_debug)
13264 {
13265 fprintf_unfiltered (gdb_stdlog,
13266 "Virtual DWO %s %s found: @%s\n",
13267 kind, hex_string (signature),
13268 host_address_to_string (dwo_cutu));
13269 }
13270 return dwo_cutu;
13271 }
13272 }
13273 }
13274 else
13275 {
13276 /* No DWP file, look for the DWO file. */
13277
13278 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13279 dwo_name, comp_dir);
13280 if (*dwo_file_slot == NULL)
13281 {
13282 /* Read in the file and build a table of the CUs/TUs it contains. */
13283 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13284 }
13285 /* NOTE: This will be NULL if unable to open the file. */
13286 dwo_file = (struct dwo_file *) *dwo_file_slot;
13287
13288 if (dwo_file != NULL)
13289 {
13290 struct dwo_unit *dwo_cutu = NULL;
13291
13292 if (is_debug_types && dwo_file->tus)
13293 {
13294 struct dwo_unit find_dwo_cutu;
13295
13296 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13297 find_dwo_cutu.signature = signature;
13298 dwo_cutu
13299 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13300 }
13301 else if (!is_debug_types && dwo_file->cus)
13302 {
13303 struct dwo_unit find_dwo_cutu;
13304
13305 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13306 find_dwo_cutu.signature = signature;
13307 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13308 &find_dwo_cutu);
13309 }
13310
13311 if (dwo_cutu != NULL)
13312 {
13313 if (dwarf_read_debug)
13314 {
13315 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13316 kind, dwo_name, hex_string (signature),
13317 host_address_to_string (dwo_cutu));
13318 }
13319 return dwo_cutu;
13320 }
13321 }
13322 }
13323
13324 /* We didn't find it. This could mean a dwo_id mismatch, or
13325 someone deleted the DWO/DWP file, or the search path isn't set up
13326 correctly to find the file. */
13327
13328 if (dwarf_read_debug)
13329 {
13330 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13331 kind, dwo_name, hex_string (signature));
13332 }
13333
13334 /* This is a warning and not a complaint because it can be caused by
13335 pilot error (e.g., user accidentally deleting the DWO). */
13336 {
13337 /* Print the name of the DWP file if we looked there, helps the user
13338 better diagnose the problem. */
13339 std::string dwp_text;
13340
13341 if (dwp_file != NULL)
13342 dwp_text = string_printf (" [in DWP file %s]",
13343 lbasename (dwp_file->name));
13344
13345 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13346 " [in module %s]"),
13347 kind, dwo_name, hex_string (signature),
13348 dwp_text.c_str (),
13349 this_unit->is_debug_types ? "TU" : "CU",
13350 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13351 }
13352 return NULL;
13353 }
13354
13355 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13356 See lookup_dwo_cutu_unit for details. */
13357
13358 static struct dwo_unit *
13359 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13360 const char *dwo_name, const char *comp_dir,
13361 ULONGEST signature)
13362 {
13363 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13364 }
13365
13366 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13367 See lookup_dwo_cutu_unit for details. */
13368
13369 static struct dwo_unit *
13370 lookup_dwo_type_unit (struct signatured_type *this_tu,
13371 const char *dwo_name, const char *comp_dir)
13372 {
13373 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13374 }
13375
13376 /* Traversal function for queue_and_load_all_dwo_tus. */
13377
13378 static int
13379 queue_and_load_dwo_tu (void **slot, void *info)
13380 {
13381 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13382 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13383 ULONGEST signature = dwo_unit->signature;
13384 struct signatured_type *sig_type =
13385 lookup_dwo_signatured_type (per_cu->cu, signature);
13386
13387 if (sig_type != NULL)
13388 {
13389 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13390
13391 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13392 a real dependency of PER_CU on SIG_TYPE. That is detected later
13393 while processing PER_CU. */
13394 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13395 load_full_type_unit (sig_cu);
13396 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13397 }
13398
13399 return 1;
13400 }
13401
13402 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13403 The DWO may have the only definition of the type, though it may not be
13404 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13405 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13406
13407 static void
13408 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13409 {
13410 struct dwo_unit *dwo_unit;
13411 struct dwo_file *dwo_file;
13412
13413 gdb_assert (!per_cu->is_debug_types);
13414 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13415 gdb_assert (per_cu->cu != NULL);
13416
13417 dwo_unit = per_cu->cu->dwo_unit;
13418 gdb_assert (dwo_unit != NULL);
13419
13420 dwo_file = dwo_unit->dwo_file;
13421 if (dwo_file->tus != NULL)
13422 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13423 }
13424
13425 /* Free all resources associated with DWO_FILE.
13426 Close the DWO file and munmap the sections. */
13427
13428 static void
13429 free_dwo_file (struct dwo_file *dwo_file)
13430 {
13431 /* Note: dbfd is NULL for virtual DWO files. */
13432 gdb_bfd_unref (dwo_file->dbfd);
13433
13434 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13435 }
13436
13437 /* Traversal function for free_dwo_files. */
13438
13439 static int
13440 free_dwo_file_from_slot (void **slot, void *info)
13441 {
13442 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13443
13444 free_dwo_file (dwo_file);
13445
13446 return 1;
13447 }
13448
13449 /* Free all resources associated with DWO_FILES. */
13450
13451 static void
13452 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13453 {
13454 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13455 }
13456 \f
13457 /* Read in various DIEs. */
13458
13459 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13460 Inherit only the children of the DW_AT_abstract_origin DIE not being
13461 already referenced by DW_AT_abstract_origin from the children of the
13462 current DIE. */
13463
13464 static void
13465 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13466 {
13467 struct die_info *child_die;
13468 sect_offset *offsetp;
13469 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13470 struct die_info *origin_die;
13471 /* Iterator of the ORIGIN_DIE children. */
13472 struct die_info *origin_child_die;
13473 struct attribute *attr;
13474 struct dwarf2_cu *origin_cu;
13475 struct pending **origin_previous_list_in_scope;
13476
13477 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13478 if (!attr)
13479 return;
13480
13481 /* Note that following die references may follow to a die in a
13482 different cu. */
13483
13484 origin_cu = cu;
13485 origin_die = follow_die_ref (die, attr, &origin_cu);
13486
13487 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13488 symbols in. */
13489 origin_previous_list_in_scope = origin_cu->list_in_scope;
13490 origin_cu->list_in_scope = cu->list_in_scope;
13491
13492 if (die->tag != origin_die->tag
13493 && !(die->tag == DW_TAG_inlined_subroutine
13494 && origin_die->tag == DW_TAG_subprogram))
13495 complaint (_("DIE %s and its abstract origin %s have different tags"),
13496 sect_offset_str (die->sect_off),
13497 sect_offset_str (origin_die->sect_off));
13498
13499 std::vector<sect_offset> offsets;
13500
13501 for (child_die = die->child;
13502 child_die && child_die->tag;
13503 child_die = sibling_die (child_die))
13504 {
13505 struct die_info *child_origin_die;
13506 struct dwarf2_cu *child_origin_cu;
13507
13508 /* We are trying to process concrete instance entries:
13509 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13510 it's not relevant to our analysis here. i.e. detecting DIEs that are
13511 present in the abstract instance but not referenced in the concrete
13512 one. */
13513 if (child_die->tag == DW_TAG_call_site
13514 || child_die->tag == DW_TAG_GNU_call_site)
13515 continue;
13516
13517 /* For each CHILD_DIE, find the corresponding child of
13518 ORIGIN_DIE. If there is more than one layer of
13519 DW_AT_abstract_origin, follow them all; there shouldn't be,
13520 but GCC versions at least through 4.4 generate this (GCC PR
13521 40573). */
13522 child_origin_die = child_die;
13523 child_origin_cu = cu;
13524 while (1)
13525 {
13526 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13527 child_origin_cu);
13528 if (attr == NULL)
13529 break;
13530 child_origin_die = follow_die_ref (child_origin_die, attr,
13531 &child_origin_cu);
13532 }
13533
13534 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13535 counterpart may exist. */
13536 if (child_origin_die != child_die)
13537 {
13538 if (child_die->tag != child_origin_die->tag
13539 && !(child_die->tag == DW_TAG_inlined_subroutine
13540 && child_origin_die->tag == DW_TAG_subprogram))
13541 complaint (_("Child DIE %s and its abstract origin %s have "
13542 "different tags"),
13543 sect_offset_str (child_die->sect_off),
13544 sect_offset_str (child_origin_die->sect_off));
13545 if (child_origin_die->parent != origin_die)
13546 complaint (_("Child DIE %s and its abstract origin %s have "
13547 "different parents"),
13548 sect_offset_str (child_die->sect_off),
13549 sect_offset_str (child_origin_die->sect_off));
13550 else
13551 offsets.push_back (child_origin_die->sect_off);
13552 }
13553 }
13554 std::sort (offsets.begin (), offsets.end ());
13555 sect_offset *offsets_end = offsets.data () + offsets.size ();
13556 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13557 if (offsetp[-1] == *offsetp)
13558 complaint (_("Multiple children of DIE %s refer "
13559 "to DIE %s as their abstract origin"),
13560 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13561
13562 offsetp = offsets.data ();
13563 origin_child_die = origin_die->child;
13564 while (origin_child_die && origin_child_die->tag)
13565 {
13566 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13567 while (offsetp < offsets_end
13568 && *offsetp < origin_child_die->sect_off)
13569 offsetp++;
13570 if (offsetp >= offsets_end
13571 || *offsetp > origin_child_die->sect_off)
13572 {
13573 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13574 Check whether we're already processing ORIGIN_CHILD_DIE.
13575 This can happen with mutually referenced abstract_origins.
13576 PR 16581. */
13577 if (!origin_child_die->in_process)
13578 process_die (origin_child_die, origin_cu);
13579 }
13580 origin_child_die = sibling_die (origin_child_die);
13581 }
13582 origin_cu->list_in_scope = origin_previous_list_in_scope;
13583 }
13584
13585 static void
13586 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13587 {
13588 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13589 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13590 struct context_stack *newobj;
13591 CORE_ADDR lowpc;
13592 CORE_ADDR highpc;
13593 struct die_info *child_die;
13594 struct attribute *attr, *call_line, *call_file;
13595 const char *name;
13596 CORE_ADDR baseaddr;
13597 struct block *block;
13598 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13599 std::vector<struct symbol *> template_args;
13600 struct template_symbol *templ_func = NULL;
13601
13602 if (inlined_func)
13603 {
13604 /* If we do not have call site information, we can't show the
13605 caller of this inlined function. That's too confusing, so
13606 only use the scope for local variables. */
13607 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13608 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13609 if (call_line == NULL || call_file == NULL)
13610 {
13611 read_lexical_block_scope (die, cu);
13612 return;
13613 }
13614 }
13615
13616 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13617
13618 name = dwarf2_name (die, cu);
13619
13620 /* Ignore functions with missing or empty names. These are actually
13621 illegal according to the DWARF standard. */
13622 if (name == NULL)
13623 {
13624 complaint (_("missing name for subprogram DIE at %s"),
13625 sect_offset_str (die->sect_off));
13626 return;
13627 }
13628
13629 /* Ignore functions with missing or invalid low and high pc attributes. */
13630 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13631 <= PC_BOUNDS_INVALID)
13632 {
13633 attr = dwarf2_attr (die, DW_AT_external, cu);
13634 if (!attr || !DW_UNSND (attr))
13635 complaint (_("cannot get low and high bounds "
13636 "for subprogram DIE at %s"),
13637 sect_offset_str (die->sect_off));
13638 return;
13639 }
13640
13641 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13642 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13643
13644 /* If we have any template arguments, then we must allocate a
13645 different sort of symbol. */
13646 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13647 {
13648 if (child_die->tag == DW_TAG_template_type_param
13649 || child_die->tag == DW_TAG_template_value_param)
13650 {
13651 templ_func = allocate_template_symbol (objfile);
13652 templ_func->subclass = SYMBOL_TEMPLATE;
13653 break;
13654 }
13655 }
13656
13657 newobj = cu->builder->push_context (0, lowpc);
13658 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13659 (struct symbol *) templ_func);
13660
13661 /* If there is a location expression for DW_AT_frame_base, record
13662 it. */
13663 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13664 if (attr)
13665 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13666
13667 /* If there is a location for the static link, record it. */
13668 newobj->static_link = NULL;
13669 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13670 if (attr)
13671 {
13672 newobj->static_link
13673 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13674 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13675 }
13676
13677 cu->list_in_scope = cu->builder->get_local_symbols ();
13678
13679 if (die->child != NULL)
13680 {
13681 child_die = die->child;
13682 while (child_die && child_die->tag)
13683 {
13684 if (child_die->tag == DW_TAG_template_type_param
13685 || child_die->tag == DW_TAG_template_value_param)
13686 {
13687 struct symbol *arg = new_symbol (child_die, NULL, cu);
13688
13689 if (arg != NULL)
13690 template_args.push_back (arg);
13691 }
13692 else
13693 process_die (child_die, cu);
13694 child_die = sibling_die (child_die);
13695 }
13696 }
13697
13698 inherit_abstract_dies (die, cu);
13699
13700 /* If we have a DW_AT_specification, we might need to import using
13701 directives from the context of the specification DIE. See the
13702 comment in determine_prefix. */
13703 if (cu->language == language_cplus
13704 && dwarf2_attr (die, DW_AT_specification, cu))
13705 {
13706 struct dwarf2_cu *spec_cu = cu;
13707 struct die_info *spec_die = die_specification (die, &spec_cu);
13708
13709 while (spec_die)
13710 {
13711 child_die = spec_die->child;
13712 while (child_die && child_die->tag)
13713 {
13714 if (child_die->tag == DW_TAG_imported_module)
13715 process_die (child_die, spec_cu);
13716 child_die = sibling_die (child_die);
13717 }
13718
13719 /* In some cases, GCC generates specification DIEs that
13720 themselves contain DW_AT_specification attributes. */
13721 spec_die = die_specification (spec_die, &spec_cu);
13722 }
13723 }
13724
13725 struct context_stack cstk = cu->builder->pop_context ();
13726 /* Make a block for the local symbols within. */
13727 block = cu->builder->finish_block (cstk.name, cstk.old_blocks,
13728 cstk.static_link, lowpc, highpc);
13729
13730 /* For C++, set the block's scope. */
13731 if ((cu->language == language_cplus
13732 || cu->language == language_fortran
13733 || cu->language == language_d
13734 || cu->language == language_rust)
13735 && cu->processing_has_namespace_info)
13736 block_set_scope (block, determine_prefix (die, cu),
13737 &objfile->objfile_obstack);
13738
13739 /* If we have address ranges, record them. */
13740 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13741
13742 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13743
13744 /* Attach template arguments to function. */
13745 if (!template_args.empty ())
13746 {
13747 gdb_assert (templ_func != NULL);
13748
13749 templ_func->n_template_arguments = template_args.size ();
13750 templ_func->template_arguments
13751 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13752 templ_func->n_template_arguments);
13753 memcpy (templ_func->template_arguments,
13754 template_args.data (),
13755 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13756 }
13757
13758 /* In C++, we can have functions nested inside functions (e.g., when
13759 a function declares a class that has methods). This means that
13760 when we finish processing a function scope, we may need to go
13761 back to building a containing block's symbol lists. */
13762 *cu->builder->get_local_symbols () = cstk.locals;
13763 cu->builder->set_local_using_directives (cstk.local_using_directives);
13764
13765 /* If we've finished processing a top-level function, subsequent
13766 symbols go in the file symbol list. */
13767 if (cu->builder->outermost_context_p ())
13768 cu->list_in_scope = cu->builder->get_file_symbols ();
13769 }
13770
13771 /* Process all the DIES contained within a lexical block scope. Start
13772 a new scope, process the dies, and then close the scope. */
13773
13774 static void
13775 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13776 {
13777 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13778 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13779 CORE_ADDR lowpc, highpc;
13780 struct die_info *child_die;
13781 CORE_ADDR baseaddr;
13782
13783 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13784
13785 /* Ignore blocks with missing or invalid low and high pc attributes. */
13786 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13787 as multiple lexical blocks? Handling children in a sane way would
13788 be nasty. Might be easier to properly extend generic blocks to
13789 describe ranges. */
13790 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13791 {
13792 case PC_BOUNDS_NOT_PRESENT:
13793 /* DW_TAG_lexical_block has no attributes, process its children as if
13794 there was no wrapping by that DW_TAG_lexical_block.
13795 GCC does no longer produces such DWARF since GCC r224161. */
13796 for (child_die = die->child;
13797 child_die != NULL && child_die->tag;
13798 child_die = sibling_die (child_die))
13799 process_die (child_die, cu);
13800 return;
13801 case PC_BOUNDS_INVALID:
13802 return;
13803 }
13804 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13805 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13806
13807 cu->builder->push_context (0, lowpc);
13808 if (die->child != NULL)
13809 {
13810 child_die = die->child;
13811 while (child_die && child_die->tag)
13812 {
13813 process_die (child_die, cu);
13814 child_die = sibling_die (child_die);
13815 }
13816 }
13817 inherit_abstract_dies (die, cu);
13818 struct context_stack cstk = cu->builder->pop_context ();
13819
13820 if (*cu->builder->get_local_symbols () != NULL
13821 || (*cu->builder->get_local_using_directives ()) != NULL)
13822 {
13823 struct block *block
13824 = cu->builder->finish_block (0, cstk.old_blocks, NULL,
13825 cstk.start_addr, highpc);
13826
13827 /* Note that recording ranges after traversing children, as we
13828 do here, means that recording a parent's ranges entails
13829 walking across all its children's ranges as they appear in
13830 the address map, which is quadratic behavior.
13831
13832 It would be nicer to record the parent's ranges before
13833 traversing its children, simply overriding whatever you find
13834 there. But since we don't even decide whether to create a
13835 block until after we've traversed its children, that's hard
13836 to do. */
13837 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13838 }
13839 *cu->builder->get_local_symbols () = cstk.locals;
13840 cu->builder->set_local_using_directives (cstk.local_using_directives);
13841 }
13842
13843 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13844
13845 static void
13846 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13847 {
13848 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13849 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13850 CORE_ADDR pc, baseaddr;
13851 struct attribute *attr;
13852 struct call_site *call_site, call_site_local;
13853 void **slot;
13854 int nparams;
13855 struct die_info *child_die;
13856
13857 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13858
13859 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13860 if (attr == NULL)
13861 {
13862 /* This was a pre-DWARF-5 GNU extension alias
13863 for DW_AT_call_return_pc. */
13864 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13865 }
13866 if (!attr)
13867 {
13868 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13869 "DIE %s [in module %s]"),
13870 sect_offset_str (die->sect_off), objfile_name (objfile));
13871 return;
13872 }
13873 pc = attr_value_as_address (attr) + baseaddr;
13874 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13875
13876 if (cu->call_site_htab == NULL)
13877 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13878 NULL, &objfile->objfile_obstack,
13879 hashtab_obstack_allocate, NULL);
13880 call_site_local.pc = pc;
13881 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13882 if (*slot != NULL)
13883 {
13884 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13885 "DIE %s [in module %s]"),
13886 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13887 objfile_name (objfile));
13888 return;
13889 }
13890
13891 /* Count parameters at the caller. */
13892
13893 nparams = 0;
13894 for (child_die = die->child; child_die && child_die->tag;
13895 child_die = sibling_die (child_die))
13896 {
13897 if (child_die->tag != DW_TAG_call_site_parameter
13898 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13899 {
13900 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13901 "DW_TAG_call_site child DIE %s [in module %s]"),
13902 child_die->tag, sect_offset_str (child_die->sect_off),
13903 objfile_name (objfile));
13904 continue;
13905 }
13906
13907 nparams++;
13908 }
13909
13910 call_site
13911 = ((struct call_site *)
13912 obstack_alloc (&objfile->objfile_obstack,
13913 sizeof (*call_site)
13914 + (sizeof (*call_site->parameter) * (nparams - 1))));
13915 *slot = call_site;
13916 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13917 call_site->pc = pc;
13918
13919 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13920 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13921 {
13922 struct die_info *func_die;
13923
13924 /* Skip also over DW_TAG_inlined_subroutine. */
13925 for (func_die = die->parent;
13926 func_die && func_die->tag != DW_TAG_subprogram
13927 && func_die->tag != DW_TAG_subroutine_type;
13928 func_die = func_die->parent);
13929
13930 /* DW_AT_call_all_calls is a superset
13931 of DW_AT_call_all_tail_calls. */
13932 if (func_die
13933 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13934 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13935 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13936 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13937 {
13938 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13939 not complete. But keep CALL_SITE for look ups via call_site_htab,
13940 both the initial caller containing the real return address PC and
13941 the final callee containing the current PC of a chain of tail
13942 calls do not need to have the tail call list complete. But any
13943 function candidate for a virtual tail call frame searched via
13944 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13945 determined unambiguously. */
13946 }
13947 else
13948 {
13949 struct type *func_type = NULL;
13950
13951 if (func_die)
13952 func_type = get_die_type (func_die, cu);
13953 if (func_type != NULL)
13954 {
13955 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13956
13957 /* Enlist this call site to the function. */
13958 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13959 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13960 }
13961 else
13962 complaint (_("Cannot find function owning DW_TAG_call_site "
13963 "DIE %s [in module %s]"),
13964 sect_offset_str (die->sect_off), objfile_name (objfile));
13965 }
13966 }
13967
13968 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13969 if (attr == NULL)
13970 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13971 if (attr == NULL)
13972 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13973 if (attr == NULL)
13974 {
13975 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13976 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13977 }
13978 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13979 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
13980 /* Keep NULL DWARF_BLOCK. */;
13981 else if (attr_form_is_block (attr))
13982 {
13983 struct dwarf2_locexpr_baton *dlbaton;
13984
13985 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13986 dlbaton->data = DW_BLOCK (attr)->data;
13987 dlbaton->size = DW_BLOCK (attr)->size;
13988 dlbaton->per_cu = cu->per_cu;
13989
13990 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13991 }
13992 else if (attr_form_is_ref (attr))
13993 {
13994 struct dwarf2_cu *target_cu = cu;
13995 struct die_info *target_die;
13996
13997 target_die = follow_die_ref (die, attr, &target_cu);
13998 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13999 if (die_is_declaration (target_die, target_cu))
14000 {
14001 const char *target_physname;
14002
14003 /* Prefer the mangled name; otherwise compute the demangled one. */
14004 target_physname = dw2_linkage_name (target_die, target_cu);
14005 if (target_physname == NULL)
14006 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14007 if (target_physname == NULL)
14008 complaint (_("DW_AT_call_target target DIE has invalid "
14009 "physname, for referencing DIE %s [in module %s]"),
14010 sect_offset_str (die->sect_off), objfile_name (objfile));
14011 else
14012 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14013 }
14014 else
14015 {
14016 CORE_ADDR lowpc;
14017
14018 /* DW_AT_entry_pc should be preferred. */
14019 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14020 <= PC_BOUNDS_INVALID)
14021 complaint (_("DW_AT_call_target target DIE has invalid "
14022 "low pc, for referencing DIE %s [in module %s]"),
14023 sect_offset_str (die->sect_off), objfile_name (objfile));
14024 else
14025 {
14026 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14027 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14028 }
14029 }
14030 }
14031 else
14032 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14033 "block nor reference, for DIE %s [in module %s]"),
14034 sect_offset_str (die->sect_off), objfile_name (objfile));
14035
14036 call_site->per_cu = cu->per_cu;
14037
14038 for (child_die = die->child;
14039 child_die && child_die->tag;
14040 child_die = sibling_die (child_die))
14041 {
14042 struct call_site_parameter *parameter;
14043 struct attribute *loc, *origin;
14044
14045 if (child_die->tag != DW_TAG_call_site_parameter
14046 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14047 {
14048 /* Already printed the complaint above. */
14049 continue;
14050 }
14051
14052 gdb_assert (call_site->parameter_count < nparams);
14053 parameter = &call_site->parameter[call_site->parameter_count];
14054
14055 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14056 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14057 register is contained in DW_AT_call_value. */
14058
14059 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14060 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14061 if (origin == NULL)
14062 {
14063 /* This was a pre-DWARF-5 GNU extension alias
14064 for DW_AT_call_parameter. */
14065 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14066 }
14067 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14068 {
14069 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14070
14071 sect_offset sect_off
14072 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14073 if (!offset_in_cu_p (&cu->header, sect_off))
14074 {
14075 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14076 binding can be done only inside one CU. Such referenced DIE
14077 therefore cannot be even moved to DW_TAG_partial_unit. */
14078 complaint (_("DW_AT_call_parameter offset is not in CU for "
14079 "DW_TAG_call_site child DIE %s [in module %s]"),
14080 sect_offset_str (child_die->sect_off),
14081 objfile_name (objfile));
14082 continue;
14083 }
14084 parameter->u.param_cu_off
14085 = (cu_offset) (sect_off - cu->header.sect_off);
14086 }
14087 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14088 {
14089 complaint (_("No DW_FORM_block* DW_AT_location for "
14090 "DW_TAG_call_site child DIE %s [in module %s]"),
14091 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14092 continue;
14093 }
14094 else
14095 {
14096 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14097 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14098 if (parameter->u.dwarf_reg != -1)
14099 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14100 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14101 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14102 &parameter->u.fb_offset))
14103 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14104 else
14105 {
14106 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14107 "for DW_FORM_block* DW_AT_location is supported for "
14108 "DW_TAG_call_site child DIE %s "
14109 "[in module %s]"),
14110 sect_offset_str (child_die->sect_off),
14111 objfile_name (objfile));
14112 continue;
14113 }
14114 }
14115
14116 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14117 if (attr == NULL)
14118 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14119 if (!attr_form_is_block (attr))
14120 {
14121 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14122 "DW_TAG_call_site child DIE %s [in module %s]"),
14123 sect_offset_str (child_die->sect_off),
14124 objfile_name (objfile));
14125 continue;
14126 }
14127 parameter->value = DW_BLOCK (attr)->data;
14128 parameter->value_size = DW_BLOCK (attr)->size;
14129
14130 /* Parameters are not pre-cleared by memset above. */
14131 parameter->data_value = NULL;
14132 parameter->data_value_size = 0;
14133 call_site->parameter_count++;
14134
14135 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14136 if (attr == NULL)
14137 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14138 if (attr)
14139 {
14140 if (!attr_form_is_block (attr))
14141 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14142 "DW_TAG_call_site child DIE %s [in module %s]"),
14143 sect_offset_str (child_die->sect_off),
14144 objfile_name (objfile));
14145 else
14146 {
14147 parameter->data_value = DW_BLOCK (attr)->data;
14148 parameter->data_value_size = DW_BLOCK (attr)->size;
14149 }
14150 }
14151 }
14152 }
14153
14154 /* Helper function for read_variable. If DIE represents a virtual
14155 table, then return the type of the concrete object that is
14156 associated with the virtual table. Otherwise, return NULL. */
14157
14158 static struct type *
14159 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14160 {
14161 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14162 if (attr == NULL)
14163 return NULL;
14164
14165 /* Find the type DIE. */
14166 struct die_info *type_die = NULL;
14167 struct dwarf2_cu *type_cu = cu;
14168
14169 if (attr_form_is_ref (attr))
14170 type_die = follow_die_ref (die, attr, &type_cu);
14171 if (type_die == NULL)
14172 return NULL;
14173
14174 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14175 return NULL;
14176 return die_containing_type (type_die, type_cu);
14177 }
14178
14179 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14180
14181 static void
14182 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14183 {
14184 struct rust_vtable_symbol *storage = NULL;
14185
14186 if (cu->language == language_rust)
14187 {
14188 struct type *containing_type = rust_containing_type (die, cu);
14189
14190 if (containing_type != NULL)
14191 {
14192 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14193
14194 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14195 struct rust_vtable_symbol);
14196 initialize_objfile_symbol (storage);
14197 storage->concrete_type = containing_type;
14198 storage->subclass = SYMBOL_RUST_VTABLE;
14199 }
14200 }
14201
14202 new_symbol (die, NULL, cu, storage);
14203 }
14204
14205 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14206 reading .debug_rnglists.
14207 Callback's type should be:
14208 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14209 Return true if the attributes are present and valid, otherwise,
14210 return false. */
14211
14212 template <typename Callback>
14213 static bool
14214 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14215 Callback &&callback)
14216 {
14217 struct dwarf2_per_objfile *dwarf2_per_objfile
14218 = cu->per_cu->dwarf2_per_objfile;
14219 struct objfile *objfile = dwarf2_per_objfile->objfile;
14220 bfd *obfd = objfile->obfd;
14221 /* Base address selection entry. */
14222 CORE_ADDR base;
14223 int found_base;
14224 const gdb_byte *buffer;
14225 CORE_ADDR baseaddr;
14226 bool overflow = false;
14227
14228 found_base = cu->base_known;
14229 base = cu->base_address;
14230
14231 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14232 if (offset >= dwarf2_per_objfile->rnglists.size)
14233 {
14234 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14235 offset);
14236 return false;
14237 }
14238 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14239
14240 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14241
14242 while (1)
14243 {
14244 /* Initialize it due to a false compiler warning. */
14245 CORE_ADDR range_beginning = 0, range_end = 0;
14246 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14247 + dwarf2_per_objfile->rnglists.size);
14248 unsigned int bytes_read;
14249
14250 if (buffer == buf_end)
14251 {
14252 overflow = true;
14253 break;
14254 }
14255 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14256 switch (rlet)
14257 {
14258 case DW_RLE_end_of_list:
14259 break;
14260 case DW_RLE_base_address:
14261 if (buffer + cu->header.addr_size > buf_end)
14262 {
14263 overflow = true;
14264 break;
14265 }
14266 base = read_address (obfd, buffer, cu, &bytes_read);
14267 found_base = 1;
14268 buffer += bytes_read;
14269 break;
14270 case DW_RLE_start_length:
14271 if (buffer + cu->header.addr_size > buf_end)
14272 {
14273 overflow = true;
14274 break;
14275 }
14276 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14277 buffer += bytes_read;
14278 range_end = (range_beginning
14279 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14280 buffer += bytes_read;
14281 if (buffer > buf_end)
14282 {
14283 overflow = true;
14284 break;
14285 }
14286 break;
14287 case DW_RLE_offset_pair:
14288 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14289 buffer += bytes_read;
14290 if (buffer > buf_end)
14291 {
14292 overflow = true;
14293 break;
14294 }
14295 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14296 buffer += bytes_read;
14297 if (buffer > buf_end)
14298 {
14299 overflow = true;
14300 break;
14301 }
14302 break;
14303 case DW_RLE_start_end:
14304 if (buffer + 2 * cu->header.addr_size > buf_end)
14305 {
14306 overflow = true;
14307 break;
14308 }
14309 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14310 buffer += bytes_read;
14311 range_end = read_address (obfd, buffer, cu, &bytes_read);
14312 buffer += bytes_read;
14313 break;
14314 default:
14315 complaint (_("Invalid .debug_rnglists data (no base address)"));
14316 return false;
14317 }
14318 if (rlet == DW_RLE_end_of_list || overflow)
14319 break;
14320 if (rlet == DW_RLE_base_address)
14321 continue;
14322
14323 if (!found_base)
14324 {
14325 /* We have no valid base address for the ranges
14326 data. */
14327 complaint (_("Invalid .debug_rnglists data (no base address)"));
14328 return false;
14329 }
14330
14331 if (range_beginning > range_end)
14332 {
14333 /* Inverted range entries are invalid. */
14334 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14335 return false;
14336 }
14337
14338 /* Empty range entries have no effect. */
14339 if (range_beginning == range_end)
14340 continue;
14341
14342 range_beginning += base;
14343 range_end += base;
14344
14345 /* A not-uncommon case of bad debug info.
14346 Don't pollute the addrmap with bad data. */
14347 if (range_beginning + baseaddr == 0
14348 && !dwarf2_per_objfile->has_section_at_zero)
14349 {
14350 complaint (_(".debug_rnglists entry has start address of zero"
14351 " [in module %s]"), objfile_name (objfile));
14352 continue;
14353 }
14354
14355 callback (range_beginning, range_end);
14356 }
14357
14358 if (overflow)
14359 {
14360 complaint (_("Offset %d is not terminated "
14361 "for DW_AT_ranges attribute"),
14362 offset);
14363 return false;
14364 }
14365
14366 return true;
14367 }
14368
14369 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14370 Callback's type should be:
14371 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14372 Return 1 if the attributes are present and valid, otherwise, return 0. */
14373
14374 template <typename Callback>
14375 static int
14376 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14377 Callback &&callback)
14378 {
14379 struct dwarf2_per_objfile *dwarf2_per_objfile
14380 = cu->per_cu->dwarf2_per_objfile;
14381 struct objfile *objfile = dwarf2_per_objfile->objfile;
14382 struct comp_unit_head *cu_header = &cu->header;
14383 bfd *obfd = objfile->obfd;
14384 unsigned int addr_size = cu_header->addr_size;
14385 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14386 /* Base address selection entry. */
14387 CORE_ADDR base;
14388 int found_base;
14389 unsigned int dummy;
14390 const gdb_byte *buffer;
14391 CORE_ADDR baseaddr;
14392
14393 if (cu_header->version >= 5)
14394 return dwarf2_rnglists_process (offset, cu, callback);
14395
14396 found_base = cu->base_known;
14397 base = cu->base_address;
14398
14399 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14400 if (offset >= dwarf2_per_objfile->ranges.size)
14401 {
14402 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14403 offset);
14404 return 0;
14405 }
14406 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14407
14408 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14409
14410 while (1)
14411 {
14412 CORE_ADDR range_beginning, range_end;
14413
14414 range_beginning = read_address (obfd, buffer, cu, &dummy);
14415 buffer += addr_size;
14416 range_end = read_address (obfd, buffer, cu, &dummy);
14417 buffer += addr_size;
14418 offset += 2 * addr_size;
14419
14420 /* An end of list marker is a pair of zero addresses. */
14421 if (range_beginning == 0 && range_end == 0)
14422 /* Found the end of list entry. */
14423 break;
14424
14425 /* Each base address selection entry is a pair of 2 values.
14426 The first is the largest possible address, the second is
14427 the base address. Check for a base address here. */
14428 if ((range_beginning & mask) == mask)
14429 {
14430 /* If we found the largest possible address, then we already
14431 have the base address in range_end. */
14432 base = range_end;
14433 found_base = 1;
14434 continue;
14435 }
14436
14437 if (!found_base)
14438 {
14439 /* We have no valid base address for the ranges
14440 data. */
14441 complaint (_("Invalid .debug_ranges data (no base address)"));
14442 return 0;
14443 }
14444
14445 if (range_beginning > range_end)
14446 {
14447 /* Inverted range entries are invalid. */
14448 complaint (_("Invalid .debug_ranges data (inverted range)"));
14449 return 0;
14450 }
14451
14452 /* Empty range entries have no effect. */
14453 if (range_beginning == range_end)
14454 continue;
14455
14456 range_beginning += base;
14457 range_end += base;
14458
14459 /* A not-uncommon case of bad debug info.
14460 Don't pollute the addrmap with bad data. */
14461 if (range_beginning + baseaddr == 0
14462 && !dwarf2_per_objfile->has_section_at_zero)
14463 {
14464 complaint (_(".debug_ranges entry has start address of zero"
14465 " [in module %s]"), objfile_name (objfile));
14466 continue;
14467 }
14468
14469 callback (range_beginning, range_end);
14470 }
14471
14472 return 1;
14473 }
14474
14475 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14476 Return 1 if the attributes are present and valid, otherwise, return 0.
14477 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14478
14479 static int
14480 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14481 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14482 struct partial_symtab *ranges_pst)
14483 {
14484 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14485 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14486 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14487 SECT_OFF_TEXT (objfile));
14488 int low_set = 0;
14489 CORE_ADDR low = 0;
14490 CORE_ADDR high = 0;
14491 int retval;
14492
14493 retval = dwarf2_ranges_process (offset, cu,
14494 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14495 {
14496 if (ranges_pst != NULL)
14497 {
14498 CORE_ADDR lowpc;
14499 CORE_ADDR highpc;
14500
14501 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14502 range_beginning + baseaddr)
14503 - baseaddr);
14504 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14505 range_end + baseaddr)
14506 - baseaddr);
14507 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14508 ranges_pst);
14509 }
14510
14511 /* FIXME: This is recording everything as a low-high
14512 segment of consecutive addresses. We should have a
14513 data structure for discontiguous block ranges
14514 instead. */
14515 if (! low_set)
14516 {
14517 low = range_beginning;
14518 high = range_end;
14519 low_set = 1;
14520 }
14521 else
14522 {
14523 if (range_beginning < low)
14524 low = range_beginning;
14525 if (range_end > high)
14526 high = range_end;
14527 }
14528 });
14529 if (!retval)
14530 return 0;
14531
14532 if (! low_set)
14533 /* If the first entry is an end-of-list marker, the range
14534 describes an empty scope, i.e. no instructions. */
14535 return 0;
14536
14537 if (low_return)
14538 *low_return = low;
14539 if (high_return)
14540 *high_return = high;
14541 return 1;
14542 }
14543
14544 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14545 definition for the return value. *LOWPC and *HIGHPC are set iff
14546 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14547
14548 static enum pc_bounds_kind
14549 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14550 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14551 struct partial_symtab *pst)
14552 {
14553 struct dwarf2_per_objfile *dwarf2_per_objfile
14554 = cu->per_cu->dwarf2_per_objfile;
14555 struct attribute *attr;
14556 struct attribute *attr_high;
14557 CORE_ADDR low = 0;
14558 CORE_ADDR high = 0;
14559 enum pc_bounds_kind ret;
14560
14561 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14562 if (attr_high)
14563 {
14564 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14565 if (attr)
14566 {
14567 low = attr_value_as_address (attr);
14568 high = attr_value_as_address (attr_high);
14569 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14570 high += low;
14571 }
14572 else
14573 /* Found high w/o low attribute. */
14574 return PC_BOUNDS_INVALID;
14575
14576 /* Found consecutive range of addresses. */
14577 ret = PC_BOUNDS_HIGH_LOW;
14578 }
14579 else
14580 {
14581 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14582 if (attr != NULL)
14583 {
14584 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14585 We take advantage of the fact that DW_AT_ranges does not appear
14586 in DW_TAG_compile_unit of DWO files. */
14587 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14588 unsigned int ranges_offset = (DW_UNSND (attr)
14589 + (need_ranges_base
14590 ? cu->ranges_base
14591 : 0));
14592
14593 /* Value of the DW_AT_ranges attribute is the offset in the
14594 .debug_ranges section. */
14595 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14596 return PC_BOUNDS_INVALID;
14597 /* Found discontinuous range of addresses. */
14598 ret = PC_BOUNDS_RANGES;
14599 }
14600 else
14601 return PC_BOUNDS_NOT_PRESENT;
14602 }
14603
14604 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14605 if (high <= low)
14606 return PC_BOUNDS_INVALID;
14607
14608 /* When using the GNU linker, .gnu.linkonce. sections are used to
14609 eliminate duplicate copies of functions and vtables and such.
14610 The linker will arbitrarily choose one and discard the others.
14611 The AT_*_pc values for such functions refer to local labels in
14612 these sections. If the section from that file was discarded, the
14613 labels are not in the output, so the relocs get a value of 0.
14614 If this is a discarded function, mark the pc bounds as invalid,
14615 so that GDB will ignore it. */
14616 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14617 return PC_BOUNDS_INVALID;
14618
14619 *lowpc = low;
14620 if (highpc)
14621 *highpc = high;
14622 return ret;
14623 }
14624
14625 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14626 its low and high PC addresses. Do nothing if these addresses could not
14627 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14628 and HIGHPC to the high address if greater than HIGHPC. */
14629
14630 static void
14631 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14632 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14633 struct dwarf2_cu *cu)
14634 {
14635 CORE_ADDR low, high;
14636 struct die_info *child = die->child;
14637
14638 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14639 {
14640 *lowpc = std::min (*lowpc, low);
14641 *highpc = std::max (*highpc, high);
14642 }
14643
14644 /* If the language does not allow nested subprograms (either inside
14645 subprograms or lexical blocks), we're done. */
14646 if (cu->language != language_ada)
14647 return;
14648
14649 /* Check all the children of the given DIE. If it contains nested
14650 subprograms, then check their pc bounds. Likewise, we need to
14651 check lexical blocks as well, as they may also contain subprogram
14652 definitions. */
14653 while (child && child->tag)
14654 {
14655 if (child->tag == DW_TAG_subprogram
14656 || child->tag == DW_TAG_lexical_block)
14657 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14658 child = sibling_die (child);
14659 }
14660 }
14661
14662 /* Get the low and high pc's represented by the scope DIE, and store
14663 them in *LOWPC and *HIGHPC. If the correct values can't be
14664 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14665
14666 static void
14667 get_scope_pc_bounds (struct die_info *die,
14668 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14669 struct dwarf2_cu *cu)
14670 {
14671 CORE_ADDR best_low = (CORE_ADDR) -1;
14672 CORE_ADDR best_high = (CORE_ADDR) 0;
14673 CORE_ADDR current_low, current_high;
14674
14675 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14676 >= PC_BOUNDS_RANGES)
14677 {
14678 best_low = current_low;
14679 best_high = current_high;
14680 }
14681 else
14682 {
14683 struct die_info *child = die->child;
14684
14685 while (child && child->tag)
14686 {
14687 switch (child->tag) {
14688 case DW_TAG_subprogram:
14689 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14690 break;
14691 case DW_TAG_namespace:
14692 case DW_TAG_module:
14693 /* FIXME: carlton/2004-01-16: Should we do this for
14694 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14695 that current GCC's always emit the DIEs corresponding
14696 to definitions of methods of classes as children of a
14697 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14698 the DIEs giving the declarations, which could be
14699 anywhere). But I don't see any reason why the
14700 standards says that they have to be there. */
14701 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14702
14703 if (current_low != ((CORE_ADDR) -1))
14704 {
14705 best_low = std::min (best_low, current_low);
14706 best_high = std::max (best_high, current_high);
14707 }
14708 break;
14709 default:
14710 /* Ignore. */
14711 break;
14712 }
14713
14714 child = sibling_die (child);
14715 }
14716 }
14717
14718 *lowpc = best_low;
14719 *highpc = best_high;
14720 }
14721
14722 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14723 in DIE. */
14724
14725 static void
14726 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14727 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14728 {
14729 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14730 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14731 struct attribute *attr;
14732 struct attribute *attr_high;
14733
14734 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14735 if (attr_high)
14736 {
14737 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14738 if (attr)
14739 {
14740 CORE_ADDR low = attr_value_as_address (attr);
14741 CORE_ADDR high = attr_value_as_address (attr_high);
14742
14743 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14744 high += low;
14745
14746 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14747 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14748 cu->builder->record_block_range (block, low, high - 1);
14749 }
14750 }
14751
14752 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14753 if (attr)
14754 {
14755 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14756 We take advantage of the fact that DW_AT_ranges does not appear
14757 in DW_TAG_compile_unit of DWO files. */
14758 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14759
14760 /* The value of the DW_AT_ranges attribute is the offset of the
14761 address range list in the .debug_ranges section. */
14762 unsigned long offset = (DW_UNSND (attr)
14763 + (need_ranges_base ? cu->ranges_base : 0));
14764
14765 dwarf2_ranges_process (offset, cu,
14766 [&] (CORE_ADDR start, CORE_ADDR end)
14767 {
14768 start += baseaddr;
14769 end += baseaddr;
14770 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14771 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14772 cu->builder->record_block_range (block, start, end - 1);
14773 });
14774 }
14775 }
14776
14777 /* Check whether the producer field indicates either of GCC < 4.6, or the
14778 Intel C/C++ compiler, and cache the result in CU. */
14779
14780 static void
14781 check_producer (struct dwarf2_cu *cu)
14782 {
14783 int major, minor;
14784
14785 if (cu->producer == NULL)
14786 {
14787 /* For unknown compilers expect their behavior is DWARF version
14788 compliant.
14789
14790 GCC started to support .debug_types sections by -gdwarf-4 since
14791 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14792 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14793 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14794 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14795 }
14796 else if (producer_is_gcc (cu->producer, &major, &minor))
14797 {
14798 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14799 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14800 }
14801 else if (producer_is_icc (cu->producer, &major, &minor))
14802 cu->producer_is_icc_lt_14 = major < 14;
14803 else
14804 {
14805 /* For other non-GCC compilers, expect their behavior is DWARF version
14806 compliant. */
14807 }
14808
14809 cu->checked_producer = 1;
14810 }
14811
14812 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14813 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14814 during 4.6.0 experimental. */
14815
14816 static int
14817 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14818 {
14819 if (!cu->checked_producer)
14820 check_producer (cu);
14821
14822 return cu->producer_is_gxx_lt_4_6;
14823 }
14824
14825 /* Return the default accessibility type if it is not overriden by
14826 DW_AT_accessibility. */
14827
14828 static enum dwarf_access_attribute
14829 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14830 {
14831 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14832 {
14833 /* The default DWARF 2 accessibility for members is public, the default
14834 accessibility for inheritance is private. */
14835
14836 if (die->tag != DW_TAG_inheritance)
14837 return DW_ACCESS_public;
14838 else
14839 return DW_ACCESS_private;
14840 }
14841 else
14842 {
14843 /* DWARF 3+ defines the default accessibility a different way. The same
14844 rules apply now for DW_TAG_inheritance as for the members and it only
14845 depends on the container kind. */
14846
14847 if (die->parent->tag == DW_TAG_class_type)
14848 return DW_ACCESS_private;
14849 else
14850 return DW_ACCESS_public;
14851 }
14852 }
14853
14854 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14855 offset. If the attribute was not found return 0, otherwise return
14856 1. If it was found but could not properly be handled, set *OFFSET
14857 to 0. */
14858
14859 static int
14860 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14861 LONGEST *offset)
14862 {
14863 struct attribute *attr;
14864
14865 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14866 if (attr != NULL)
14867 {
14868 *offset = 0;
14869
14870 /* Note that we do not check for a section offset first here.
14871 This is because DW_AT_data_member_location is new in DWARF 4,
14872 so if we see it, we can assume that a constant form is really
14873 a constant and not a section offset. */
14874 if (attr_form_is_constant (attr))
14875 *offset = dwarf2_get_attr_constant_value (attr, 0);
14876 else if (attr_form_is_section_offset (attr))
14877 dwarf2_complex_location_expr_complaint ();
14878 else if (attr_form_is_block (attr))
14879 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14880 else
14881 dwarf2_complex_location_expr_complaint ();
14882
14883 return 1;
14884 }
14885
14886 return 0;
14887 }
14888
14889 /* Add an aggregate field to the field list. */
14890
14891 static void
14892 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14893 struct dwarf2_cu *cu)
14894 {
14895 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14896 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14897 struct nextfield *new_field;
14898 struct attribute *attr;
14899 struct field *fp;
14900 const char *fieldname = "";
14901
14902 if (die->tag == DW_TAG_inheritance)
14903 {
14904 fip->baseclasses.emplace_back ();
14905 new_field = &fip->baseclasses.back ();
14906 }
14907 else
14908 {
14909 fip->fields.emplace_back ();
14910 new_field = &fip->fields.back ();
14911 }
14912
14913 fip->nfields++;
14914
14915 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14916 if (attr)
14917 new_field->accessibility = DW_UNSND (attr);
14918 else
14919 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14920 if (new_field->accessibility != DW_ACCESS_public)
14921 fip->non_public_fields = 1;
14922
14923 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14924 if (attr)
14925 new_field->virtuality = DW_UNSND (attr);
14926 else
14927 new_field->virtuality = DW_VIRTUALITY_none;
14928
14929 fp = &new_field->field;
14930
14931 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14932 {
14933 LONGEST offset;
14934
14935 /* Data member other than a C++ static data member. */
14936
14937 /* Get type of field. */
14938 fp->type = die_type (die, cu);
14939
14940 SET_FIELD_BITPOS (*fp, 0);
14941
14942 /* Get bit size of field (zero if none). */
14943 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14944 if (attr)
14945 {
14946 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14947 }
14948 else
14949 {
14950 FIELD_BITSIZE (*fp) = 0;
14951 }
14952
14953 /* Get bit offset of field. */
14954 if (handle_data_member_location (die, cu, &offset))
14955 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14956 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14957 if (attr)
14958 {
14959 if (gdbarch_bits_big_endian (gdbarch))
14960 {
14961 /* For big endian bits, the DW_AT_bit_offset gives the
14962 additional bit offset from the MSB of the containing
14963 anonymous object to the MSB of the field. We don't
14964 have to do anything special since we don't need to
14965 know the size of the anonymous object. */
14966 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14967 }
14968 else
14969 {
14970 /* For little endian bits, compute the bit offset to the
14971 MSB of the anonymous object, subtract off the number of
14972 bits from the MSB of the field to the MSB of the
14973 object, and then subtract off the number of bits of
14974 the field itself. The result is the bit offset of
14975 the LSB of the field. */
14976 int anonymous_size;
14977 int bit_offset = DW_UNSND (attr);
14978
14979 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14980 if (attr)
14981 {
14982 /* The size of the anonymous object containing
14983 the bit field is explicit, so use the
14984 indicated size (in bytes). */
14985 anonymous_size = DW_UNSND (attr);
14986 }
14987 else
14988 {
14989 /* The size of the anonymous object containing
14990 the bit field must be inferred from the type
14991 attribute of the data member containing the
14992 bit field. */
14993 anonymous_size = TYPE_LENGTH (fp->type);
14994 }
14995 SET_FIELD_BITPOS (*fp,
14996 (FIELD_BITPOS (*fp)
14997 + anonymous_size * bits_per_byte
14998 - bit_offset - FIELD_BITSIZE (*fp)));
14999 }
15000 }
15001 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15002 if (attr != NULL)
15003 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15004 + dwarf2_get_attr_constant_value (attr, 0)));
15005
15006 /* Get name of field. */
15007 fieldname = dwarf2_name (die, cu);
15008 if (fieldname == NULL)
15009 fieldname = "";
15010
15011 /* The name is already allocated along with this objfile, so we don't
15012 need to duplicate it for the type. */
15013 fp->name = fieldname;
15014
15015 /* Change accessibility for artificial fields (e.g. virtual table
15016 pointer or virtual base class pointer) to private. */
15017 if (dwarf2_attr (die, DW_AT_artificial, cu))
15018 {
15019 FIELD_ARTIFICIAL (*fp) = 1;
15020 new_field->accessibility = DW_ACCESS_private;
15021 fip->non_public_fields = 1;
15022 }
15023 }
15024 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15025 {
15026 /* C++ static member. */
15027
15028 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15029 is a declaration, but all versions of G++ as of this writing
15030 (so through at least 3.2.1) incorrectly generate
15031 DW_TAG_variable tags. */
15032
15033 const char *physname;
15034
15035 /* Get name of field. */
15036 fieldname = dwarf2_name (die, cu);
15037 if (fieldname == NULL)
15038 return;
15039
15040 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15041 if (attr
15042 /* Only create a symbol if this is an external value.
15043 new_symbol checks this and puts the value in the global symbol
15044 table, which we want. If it is not external, new_symbol
15045 will try to put the value in cu->list_in_scope which is wrong. */
15046 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15047 {
15048 /* A static const member, not much different than an enum as far as
15049 we're concerned, except that we can support more types. */
15050 new_symbol (die, NULL, cu);
15051 }
15052
15053 /* Get physical name. */
15054 physname = dwarf2_physname (fieldname, die, cu);
15055
15056 /* The name is already allocated along with this objfile, so we don't
15057 need to duplicate it for the type. */
15058 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15059 FIELD_TYPE (*fp) = die_type (die, cu);
15060 FIELD_NAME (*fp) = fieldname;
15061 }
15062 else if (die->tag == DW_TAG_inheritance)
15063 {
15064 LONGEST offset;
15065
15066 /* C++ base class field. */
15067 if (handle_data_member_location (die, cu, &offset))
15068 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15069 FIELD_BITSIZE (*fp) = 0;
15070 FIELD_TYPE (*fp) = die_type (die, cu);
15071 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15072 }
15073 else if (die->tag == DW_TAG_variant_part)
15074 {
15075 /* process_structure_scope will treat this DIE as a union. */
15076 process_structure_scope (die, cu);
15077
15078 /* The variant part is relative to the start of the enclosing
15079 structure. */
15080 SET_FIELD_BITPOS (*fp, 0);
15081 fp->type = get_die_type (die, cu);
15082 fp->artificial = 1;
15083 fp->name = "<<variant>>";
15084 }
15085 else
15086 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15087 }
15088
15089 /* Can the type given by DIE define another type? */
15090
15091 static bool
15092 type_can_define_types (const struct die_info *die)
15093 {
15094 switch (die->tag)
15095 {
15096 case DW_TAG_typedef:
15097 case DW_TAG_class_type:
15098 case DW_TAG_structure_type:
15099 case DW_TAG_union_type:
15100 case DW_TAG_enumeration_type:
15101 return true;
15102
15103 default:
15104 return false;
15105 }
15106 }
15107
15108 /* Add a type definition defined in the scope of the FIP's class. */
15109
15110 static void
15111 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15112 struct dwarf2_cu *cu)
15113 {
15114 struct decl_field fp;
15115 memset (&fp, 0, sizeof (fp));
15116
15117 gdb_assert (type_can_define_types (die));
15118
15119 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15120 fp.name = dwarf2_name (die, cu);
15121 fp.type = read_type_die (die, cu);
15122
15123 /* Save accessibility. */
15124 enum dwarf_access_attribute accessibility;
15125 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15126 if (attr != NULL)
15127 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15128 else
15129 accessibility = dwarf2_default_access_attribute (die, cu);
15130 switch (accessibility)
15131 {
15132 case DW_ACCESS_public:
15133 /* The assumed value if neither private nor protected. */
15134 break;
15135 case DW_ACCESS_private:
15136 fp.is_private = 1;
15137 break;
15138 case DW_ACCESS_protected:
15139 fp.is_protected = 1;
15140 break;
15141 default:
15142 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15143 }
15144
15145 if (die->tag == DW_TAG_typedef)
15146 fip->typedef_field_list.push_back (fp);
15147 else
15148 fip->nested_types_list.push_back (fp);
15149 }
15150
15151 /* Create the vector of fields, and attach it to the type. */
15152
15153 static void
15154 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15155 struct dwarf2_cu *cu)
15156 {
15157 int nfields = fip->nfields;
15158
15159 /* Record the field count, allocate space for the array of fields,
15160 and create blank accessibility bitfields if necessary. */
15161 TYPE_NFIELDS (type) = nfields;
15162 TYPE_FIELDS (type) = (struct field *)
15163 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15164
15165 if (fip->non_public_fields && cu->language != language_ada)
15166 {
15167 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15168
15169 TYPE_FIELD_PRIVATE_BITS (type) =
15170 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15171 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15172
15173 TYPE_FIELD_PROTECTED_BITS (type) =
15174 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15175 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15176
15177 TYPE_FIELD_IGNORE_BITS (type) =
15178 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15179 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15180 }
15181
15182 /* If the type has baseclasses, allocate and clear a bit vector for
15183 TYPE_FIELD_VIRTUAL_BITS. */
15184 if (!fip->baseclasses.empty () && cu->language != language_ada)
15185 {
15186 int num_bytes = B_BYTES (fip->baseclasses.size ());
15187 unsigned char *pointer;
15188
15189 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15190 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15191 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15192 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15193 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15194 }
15195
15196 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15197 {
15198 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15199
15200 for (int index = 0; index < nfields; ++index)
15201 {
15202 struct nextfield &field = fip->fields[index];
15203
15204 if (field.variant.is_discriminant)
15205 di->discriminant_index = index;
15206 else if (field.variant.default_branch)
15207 di->default_index = index;
15208 else
15209 di->discriminants[index] = field.variant.discriminant_value;
15210 }
15211 }
15212
15213 /* Copy the saved-up fields into the field vector. */
15214 for (int i = 0; i < nfields; ++i)
15215 {
15216 struct nextfield &field
15217 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15218 : fip->fields[i - fip->baseclasses.size ()]);
15219
15220 TYPE_FIELD (type, i) = field.field;
15221 switch (field.accessibility)
15222 {
15223 case DW_ACCESS_private:
15224 if (cu->language != language_ada)
15225 SET_TYPE_FIELD_PRIVATE (type, i);
15226 break;
15227
15228 case DW_ACCESS_protected:
15229 if (cu->language != language_ada)
15230 SET_TYPE_FIELD_PROTECTED (type, i);
15231 break;
15232
15233 case DW_ACCESS_public:
15234 break;
15235
15236 default:
15237 /* Unknown accessibility. Complain and treat it as public. */
15238 {
15239 complaint (_("unsupported accessibility %d"),
15240 field.accessibility);
15241 }
15242 break;
15243 }
15244 if (i < fip->baseclasses.size ())
15245 {
15246 switch (field.virtuality)
15247 {
15248 case DW_VIRTUALITY_virtual:
15249 case DW_VIRTUALITY_pure_virtual:
15250 if (cu->language == language_ada)
15251 error (_("unexpected virtuality in component of Ada type"));
15252 SET_TYPE_FIELD_VIRTUAL (type, i);
15253 break;
15254 }
15255 }
15256 }
15257 }
15258
15259 /* Return true if this member function is a constructor, false
15260 otherwise. */
15261
15262 static int
15263 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15264 {
15265 const char *fieldname;
15266 const char *type_name;
15267 int len;
15268
15269 if (die->parent == NULL)
15270 return 0;
15271
15272 if (die->parent->tag != DW_TAG_structure_type
15273 && die->parent->tag != DW_TAG_union_type
15274 && die->parent->tag != DW_TAG_class_type)
15275 return 0;
15276
15277 fieldname = dwarf2_name (die, cu);
15278 type_name = dwarf2_name (die->parent, cu);
15279 if (fieldname == NULL || type_name == NULL)
15280 return 0;
15281
15282 len = strlen (fieldname);
15283 return (strncmp (fieldname, type_name, len) == 0
15284 && (type_name[len] == '\0' || type_name[len] == '<'));
15285 }
15286
15287 /* Add a member function to the proper fieldlist. */
15288
15289 static void
15290 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15291 struct type *type, struct dwarf2_cu *cu)
15292 {
15293 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15294 struct attribute *attr;
15295 int i;
15296 struct fnfieldlist *flp = nullptr;
15297 struct fn_field *fnp;
15298 const char *fieldname;
15299 struct type *this_type;
15300 enum dwarf_access_attribute accessibility;
15301
15302 if (cu->language == language_ada)
15303 error (_("unexpected member function in Ada type"));
15304
15305 /* Get name of member function. */
15306 fieldname = dwarf2_name (die, cu);
15307 if (fieldname == NULL)
15308 return;
15309
15310 /* Look up member function name in fieldlist. */
15311 for (i = 0; i < fip->fnfieldlists.size (); i++)
15312 {
15313 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15314 {
15315 flp = &fip->fnfieldlists[i];
15316 break;
15317 }
15318 }
15319
15320 /* Create a new fnfieldlist if necessary. */
15321 if (flp == nullptr)
15322 {
15323 fip->fnfieldlists.emplace_back ();
15324 flp = &fip->fnfieldlists.back ();
15325 flp->name = fieldname;
15326 i = fip->fnfieldlists.size () - 1;
15327 }
15328
15329 /* Create a new member function field and add it to the vector of
15330 fnfieldlists. */
15331 flp->fnfields.emplace_back ();
15332 fnp = &flp->fnfields.back ();
15333
15334 /* Delay processing of the physname until later. */
15335 if (cu->language == language_cplus)
15336 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15337 die, cu);
15338 else
15339 {
15340 const char *physname = dwarf2_physname (fieldname, die, cu);
15341 fnp->physname = physname ? physname : "";
15342 }
15343
15344 fnp->type = alloc_type (objfile);
15345 this_type = read_type_die (die, cu);
15346 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15347 {
15348 int nparams = TYPE_NFIELDS (this_type);
15349
15350 /* TYPE is the domain of this method, and THIS_TYPE is the type
15351 of the method itself (TYPE_CODE_METHOD). */
15352 smash_to_method_type (fnp->type, type,
15353 TYPE_TARGET_TYPE (this_type),
15354 TYPE_FIELDS (this_type),
15355 TYPE_NFIELDS (this_type),
15356 TYPE_VARARGS (this_type));
15357
15358 /* Handle static member functions.
15359 Dwarf2 has no clean way to discern C++ static and non-static
15360 member functions. G++ helps GDB by marking the first
15361 parameter for non-static member functions (which is the this
15362 pointer) as artificial. We obtain this information from
15363 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15364 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15365 fnp->voffset = VOFFSET_STATIC;
15366 }
15367 else
15368 complaint (_("member function type missing for '%s'"),
15369 dwarf2_full_name (fieldname, die, cu));
15370
15371 /* Get fcontext from DW_AT_containing_type if present. */
15372 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15373 fnp->fcontext = die_containing_type (die, cu);
15374
15375 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15376 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15377
15378 /* Get accessibility. */
15379 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15380 if (attr)
15381 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15382 else
15383 accessibility = dwarf2_default_access_attribute (die, cu);
15384 switch (accessibility)
15385 {
15386 case DW_ACCESS_private:
15387 fnp->is_private = 1;
15388 break;
15389 case DW_ACCESS_protected:
15390 fnp->is_protected = 1;
15391 break;
15392 }
15393
15394 /* Check for artificial methods. */
15395 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15396 if (attr && DW_UNSND (attr) != 0)
15397 fnp->is_artificial = 1;
15398
15399 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15400
15401 /* Get index in virtual function table if it is a virtual member
15402 function. For older versions of GCC, this is an offset in the
15403 appropriate virtual table, as specified by DW_AT_containing_type.
15404 For everyone else, it is an expression to be evaluated relative
15405 to the object address. */
15406
15407 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15408 if (attr)
15409 {
15410 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15411 {
15412 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15413 {
15414 /* Old-style GCC. */
15415 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15416 }
15417 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15418 || (DW_BLOCK (attr)->size > 1
15419 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15420 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15421 {
15422 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15423 if ((fnp->voffset % cu->header.addr_size) != 0)
15424 dwarf2_complex_location_expr_complaint ();
15425 else
15426 fnp->voffset /= cu->header.addr_size;
15427 fnp->voffset += 2;
15428 }
15429 else
15430 dwarf2_complex_location_expr_complaint ();
15431
15432 if (!fnp->fcontext)
15433 {
15434 /* If there is no `this' field and no DW_AT_containing_type,
15435 we cannot actually find a base class context for the
15436 vtable! */
15437 if (TYPE_NFIELDS (this_type) == 0
15438 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15439 {
15440 complaint (_("cannot determine context for virtual member "
15441 "function \"%s\" (offset %s)"),
15442 fieldname, sect_offset_str (die->sect_off));
15443 }
15444 else
15445 {
15446 fnp->fcontext
15447 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15448 }
15449 }
15450 }
15451 else if (attr_form_is_section_offset (attr))
15452 {
15453 dwarf2_complex_location_expr_complaint ();
15454 }
15455 else
15456 {
15457 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15458 fieldname);
15459 }
15460 }
15461 else
15462 {
15463 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15464 if (attr && DW_UNSND (attr))
15465 {
15466 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15467 complaint (_("Member function \"%s\" (offset %s) is virtual "
15468 "but the vtable offset is not specified"),
15469 fieldname, sect_offset_str (die->sect_off));
15470 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15471 TYPE_CPLUS_DYNAMIC (type) = 1;
15472 }
15473 }
15474 }
15475
15476 /* Create the vector of member function fields, and attach it to the type. */
15477
15478 static void
15479 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15480 struct dwarf2_cu *cu)
15481 {
15482 if (cu->language == language_ada)
15483 error (_("unexpected member functions in Ada type"));
15484
15485 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15486 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15487 TYPE_ALLOC (type,
15488 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15489
15490 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15491 {
15492 struct fnfieldlist &nf = fip->fnfieldlists[i];
15493 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15494
15495 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15496 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15497 fn_flp->fn_fields = (struct fn_field *)
15498 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15499
15500 for (int k = 0; k < nf.fnfields.size (); ++k)
15501 fn_flp->fn_fields[k] = nf.fnfields[k];
15502 }
15503
15504 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15505 }
15506
15507 /* Returns non-zero if NAME is the name of a vtable member in CU's
15508 language, zero otherwise. */
15509 static int
15510 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15511 {
15512 static const char vptr[] = "_vptr";
15513
15514 /* Look for the C++ form of the vtable. */
15515 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15516 return 1;
15517
15518 return 0;
15519 }
15520
15521 /* GCC outputs unnamed structures that are really pointers to member
15522 functions, with the ABI-specified layout. If TYPE describes
15523 such a structure, smash it into a member function type.
15524
15525 GCC shouldn't do this; it should just output pointer to member DIEs.
15526 This is GCC PR debug/28767. */
15527
15528 static void
15529 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15530 {
15531 struct type *pfn_type, *self_type, *new_type;
15532
15533 /* Check for a structure with no name and two children. */
15534 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15535 return;
15536
15537 /* Check for __pfn and __delta members. */
15538 if (TYPE_FIELD_NAME (type, 0) == NULL
15539 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15540 || TYPE_FIELD_NAME (type, 1) == NULL
15541 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15542 return;
15543
15544 /* Find the type of the method. */
15545 pfn_type = TYPE_FIELD_TYPE (type, 0);
15546 if (pfn_type == NULL
15547 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15548 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15549 return;
15550
15551 /* Look for the "this" argument. */
15552 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15553 if (TYPE_NFIELDS (pfn_type) == 0
15554 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15555 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15556 return;
15557
15558 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15559 new_type = alloc_type (objfile);
15560 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15561 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15562 TYPE_VARARGS (pfn_type));
15563 smash_to_methodptr_type (type, new_type);
15564 }
15565
15566 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15567 appropriate error checking and issuing complaints if there is a
15568 problem. */
15569
15570 static ULONGEST
15571 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15572 {
15573 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15574
15575 if (attr == nullptr)
15576 return 0;
15577
15578 if (!attr_form_is_constant (attr))
15579 {
15580 complaint (_("DW_AT_alignment must have constant form"
15581 " - DIE at %s [in module %s]"),
15582 sect_offset_str (die->sect_off),
15583 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15584 return 0;
15585 }
15586
15587 ULONGEST align;
15588 if (attr->form == DW_FORM_sdata)
15589 {
15590 LONGEST val = DW_SND (attr);
15591 if (val < 0)
15592 {
15593 complaint (_("DW_AT_alignment value must not be negative"
15594 " - DIE at %s [in module %s]"),
15595 sect_offset_str (die->sect_off),
15596 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15597 return 0;
15598 }
15599 align = val;
15600 }
15601 else
15602 align = DW_UNSND (attr);
15603
15604 if (align == 0)
15605 {
15606 complaint (_("DW_AT_alignment value must not be zero"
15607 " - DIE at %s [in module %s]"),
15608 sect_offset_str (die->sect_off),
15609 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15610 return 0;
15611 }
15612 if ((align & (align - 1)) != 0)
15613 {
15614 complaint (_("DW_AT_alignment value must be a power of 2"
15615 " - DIE at %s [in module %s]"),
15616 sect_offset_str (die->sect_off),
15617 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15618 return 0;
15619 }
15620
15621 return align;
15622 }
15623
15624 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15625 the alignment for TYPE. */
15626
15627 static void
15628 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15629 struct type *type)
15630 {
15631 if (!set_type_align (type, get_alignment (cu, die)))
15632 complaint (_("DW_AT_alignment value too large"
15633 " - DIE at %s [in module %s]"),
15634 sect_offset_str (die->sect_off),
15635 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15636 }
15637
15638 /* Called when we find the DIE that starts a structure or union scope
15639 (definition) to create a type for the structure or union. Fill in
15640 the type's name and general properties; the members will not be
15641 processed until process_structure_scope. A symbol table entry for
15642 the type will also not be done until process_structure_scope (assuming
15643 the type has a name).
15644
15645 NOTE: we need to call these functions regardless of whether or not the
15646 DIE has a DW_AT_name attribute, since it might be an anonymous
15647 structure or union. This gets the type entered into our set of
15648 user defined types. */
15649
15650 static struct type *
15651 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15652 {
15653 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15654 struct type *type;
15655 struct attribute *attr;
15656 const char *name;
15657
15658 /* If the definition of this type lives in .debug_types, read that type.
15659 Don't follow DW_AT_specification though, that will take us back up
15660 the chain and we want to go down. */
15661 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15662 if (attr)
15663 {
15664 type = get_DW_AT_signature_type (die, attr, cu);
15665
15666 /* The type's CU may not be the same as CU.
15667 Ensure TYPE is recorded with CU in die_type_hash. */
15668 return set_die_type (die, type, cu);
15669 }
15670
15671 type = alloc_type (objfile);
15672 INIT_CPLUS_SPECIFIC (type);
15673
15674 name = dwarf2_name (die, cu);
15675 if (name != NULL)
15676 {
15677 if (cu->language == language_cplus
15678 || cu->language == language_d
15679 || cu->language == language_rust)
15680 {
15681 const char *full_name = dwarf2_full_name (name, die, cu);
15682
15683 /* dwarf2_full_name might have already finished building the DIE's
15684 type. If so, there is no need to continue. */
15685 if (get_die_type (die, cu) != NULL)
15686 return get_die_type (die, cu);
15687
15688 TYPE_NAME (type) = full_name;
15689 }
15690 else
15691 {
15692 /* The name is already allocated along with this objfile, so
15693 we don't need to duplicate it for the type. */
15694 TYPE_NAME (type) = name;
15695 }
15696 }
15697
15698 if (die->tag == DW_TAG_structure_type)
15699 {
15700 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15701 }
15702 else if (die->tag == DW_TAG_union_type)
15703 {
15704 TYPE_CODE (type) = TYPE_CODE_UNION;
15705 }
15706 else if (die->tag == DW_TAG_variant_part)
15707 {
15708 TYPE_CODE (type) = TYPE_CODE_UNION;
15709 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15710 }
15711 else
15712 {
15713 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15714 }
15715
15716 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15717 TYPE_DECLARED_CLASS (type) = 1;
15718
15719 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15720 if (attr)
15721 {
15722 if (attr_form_is_constant (attr))
15723 TYPE_LENGTH (type) = DW_UNSND (attr);
15724 else
15725 {
15726 /* For the moment, dynamic type sizes are not supported
15727 by GDB's struct type. The actual size is determined
15728 on-demand when resolving the type of a given object,
15729 so set the type's length to zero for now. Otherwise,
15730 we record an expression as the length, and that expression
15731 could lead to a very large value, which could eventually
15732 lead to us trying to allocate that much memory when creating
15733 a value of that type. */
15734 TYPE_LENGTH (type) = 0;
15735 }
15736 }
15737 else
15738 {
15739 TYPE_LENGTH (type) = 0;
15740 }
15741
15742 maybe_set_alignment (cu, die, type);
15743
15744 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15745 {
15746 /* ICC<14 does not output the required DW_AT_declaration on
15747 incomplete types, but gives them a size of zero. */
15748 TYPE_STUB (type) = 1;
15749 }
15750 else
15751 TYPE_STUB_SUPPORTED (type) = 1;
15752
15753 if (die_is_declaration (die, cu))
15754 TYPE_STUB (type) = 1;
15755 else if (attr == NULL && die->child == NULL
15756 && producer_is_realview (cu->producer))
15757 /* RealView does not output the required DW_AT_declaration
15758 on incomplete types. */
15759 TYPE_STUB (type) = 1;
15760
15761 /* We need to add the type field to the die immediately so we don't
15762 infinitely recurse when dealing with pointers to the structure
15763 type within the structure itself. */
15764 set_die_type (die, type, cu);
15765
15766 /* set_die_type should be already done. */
15767 set_descriptive_type (type, die, cu);
15768
15769 return type;
15770 }
15771
15772 /* A helper for process_structure_scope that handles a single member
15773 DIE. */
15774
15775 static void
15776 handle_struct_member_die (struct die_info *child_die, struct type *type,
15777 struct field_info *fi,
15778 std::vector<struct symbol *> *template_args,
15779 struct dwarf2_cu *cu)
15780 {
15781 if (child_die->tag == DW_TAG_member
15782 || child_die->tag == DW_TAG_variable
15783 || child_die->tag == DW_TAG_variant_part)
15784 {
15785 /* NOTE: carlton/2002-11-05: A C++ static data member
15786 should be a DW_TAG_member that is a declaration, but
15787 all versions of G++ as of this writing (so through at
15788 least 3.2.1) incorrectly generate DW_TAG_variable
15789 tags for them instead. */
15790 dwarf2_add_field (fi, child_die, cu);
15791 }
15792 else if (child_die->tag == DW_TAG_subprogram)
15793 {
15794 /* Rust doesn't have member functions in the C++ sense.
15795 However, it does emit ordinary functions as children
15796 of a struct DIE. */
15797 if (cu->language == language_rust)
15798 read_func_scope (child_die, cu);
15799 else
15800 {
15801 /* C++ member function. */
15802 dwarf2_add_member_fn (fi, child_die, type, cu);
15803 }
15804 }
15805 else if (child_die->tag == DW_TAG_inheritance)
15806 {
15807 /* C++ base class field. */
15808 dwarf2_add_field (fi, child_die, cu);
15809 }
15810 else if (type_can_define_types (child_die))
15811 dwarf2_add_type_defn (fi, child_die, cu);
15812 else if (child_die->tag == DW_TAG_template_type_param
15813 || child_die->tag == DW_TAG_template_value_param)
15814 {
15815 struct symbol *arg = new_symbol (child_die, NULL, cu);
15816
15817 if (arg != NULL)
15818 template_args->push_back (arg);
15819 }
15820 else if (child_die->tag == DW_TAG_variant)
15821 {
15822 /* In a variant we want to get the discriminant and also add a
15823 field for our sole member child. */
15824 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15825
15826 for (struct die_info *variant_child = child_die->child;
15827 variant_child != NULL;
15828 variant_child = sibling_die (variant_child))
15829 {
15830 if (variant_child->tag == DW_TAG_member)
15831 {
15832 handle_struct_member_die (variant_child, type, fi,
15833 template_args, cu);
15834 /* Only handle the one. */
15835 break;
15836 }
15837 }
15838
15839 /* We don't handle this but we might as well report it if we see
15840 it. */
15841 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15842 complaint (_("DW_AT_discr_list is not supported yet"
15843 " - DIE at %s [in module %s]"),
15844 sect_offset_str (child_die->sect_off),
15845 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15846
15847 /* The first field was just added, so we can stash the
15848 discriminant there. */
15849 gdb_assert (!fi->fields.empty ());
15850 if (discr == NULL)
15851 fi->fields.back ().variant.default_branch = true;
15852 else
15853 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15854 }
15855 }
15856
15857 /* Finish creating a structure or union type, including filling in
15858 its members and creating a symbol for it. */
15859
15860 static void
15861 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15862 {
15863 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15864 struct die_info *child_die;
15865 struct type *type;
15866
15867 type = get_die_type (die, cu);
15868 if (type == NULL)
15869 type = read_structure_type (die, cu);
15870
15871 /* When reading a DW_TAG_variant_part, we need to notice when we
15872 read the discriminant member, so we can record it later in the
15873 discriminant_info. */
15874 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15875 sect_offset discr_offset;
15876
15877 if (is_variant_part)
15878 {
15879 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15880 if (discr == NULL)
15881 {
15882 /* Maybe it's a univariant form, an extension we support.
15883 In this case arrange not to check the offset. */
15884 is_variant_part = false;
15885 }
15886 else if (attr_form_is_ref (discr))
15887 {
15888 struct dwarf2_cu *target_cu = cu;
15889 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15890
15891 discr_offset = target_die->sect_off;
15892 }
15893 else
15894 {
15895 complaint (_("DW_AT_discr does not have DIE reference form"
15896 " - DIE at %s [in module %s]"),
15897 sect_offset_str (die->sect_off),
15898 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15899 is_variant_part = false;
15900 }
15901 }
15902
15903 if (die->child != NULL && ! die_is_declaration (die, cu))
15904 {
15905 struct field_info fi;
15906 std::vector<struct symbol *> template_args;
15907
15908 child_die = die->child;
15909
15910 while (child_die && child_die->tag)
15911 {
15912 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15913
15914 if (is_variant_part && discr_offset == child_die->sect_off)
15915 fi.fields.back ().variant.is_discriminant = true;
15916
15917 child_die = sibling_die (child_die);
15918 }
15919
15920 /* Attach template arguments to type. */
15921 if (!template_args.empty ())
15922 {
15923 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15924 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15925 TYPE_TEMPLATE_ARGUMENTS (type)
15926 = XOBNEWVEC (&objfile->objfile_obstack,
15927 struct symbol *,
15928 TYPE_N_TEMPLATE_ARGUMENTS (type));
15929 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15930 template_args.data (),
15931 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15932 * sizeof (struct symbol *)));
15933 }
15934
15935 /* Attach fields and member functions to the type. */
15936 if (fi.nfields)
15937 dwarf2_attach_fields_to_type (&fi, type, cu);
15938 if (!fi.fnfieldlists.empty ())
15939 {
15940 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15941
15942 /* Get the type which refers to the base class (possibly this
15943 class itself) which contains the vtable pointer for the current
15944 class from the DW_AT_containing_type attribute. This use of
15945 DW_AT_containing_type is a GNU extension. */
15946
15947 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15948 {
15949 struct type *t = die_containing_type (die, cu);
15950
15951 set_type_vptr_basetype (type, t);
15952 if (type == t)
15953 {
15954 int i;
15955
15956 /* Our own class provides vtbl ptr. */
15957 for (i = TYPE_NFIELDS (t) - 1;
15958 i >= TYPE_N_BASECLASSES (t);
15959 --i)
15960 {
15961 const char *fieldname = TYPE_FIELD_NAME (t, i);
15962
15963 if (is_vtable_name (fieldname, cu))
15964 {
15965 set_type_vptr_fieldno (type, i);
15966 break;
15967 }
15968 }
15969
15970 /* Complain if virtual function table field not found. */
15971 if (i < TYPE_N_BASECLASSES (t))
15972 complaint (_("virtual function table pointer "
15973 "not found when defining class '%s'"),
15974 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15975 }
15976 else
15977 {
15978 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15979 }
15980 }
15981 else if (cu->producer
15982 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15983 {
15984 /* The IBM XLC compiler does not provide direct indication
15985 of the containing type, but the vtable pointer is
15986 always named __vfp. */
15987
15988 int i;
15989
15990 for (i = TYPE_NFIELDS (type) - 1;
15991 i >= TYPE_N_BASECLASSES (type);
15992 --i)
15993 {
15994 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15995 {
15996 set_type_vptr_fieldno (type, i);
15997 set_type_vptr_basetype (type, type);
15998 break;
15999 }
16000 }
16001 }
16002 }
16003
16004 /* Copy fi.typedef_field_list linked list elements content into the
16005 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16006 if (!fi.typedef_field_list.empty ())
16007 {
16008 int count = fi.typedef_field_list.size ();
16009
16010 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16011 TYPE_TYPEDEF_FIELD_ARRAY (type)
16012 = ((struct decl_field *)
16013 TYPE_ALLOC (type,
16014 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16015 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16016
16017 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16018 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16019 }
16020
16021 /* Copy fi.nested_types_list linked list elements content into the
16022 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16023 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16024 {
16025 int count = fi.nested_types_list.size ();
16026
16027 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16028 TYPE_NESTED_TYPES_ARRAY (type)
16029 = ((struct decl_field *)
16030 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16031 TYPE_NESTED_TYPES_COUNT (type) = count;
16032
16033 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16034 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16035 }
16036 }
16037
16038 quirk_gcc_member_function_pointer (type, objfile);
16039 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16040 cu->rust_unions.push_back (type);
16041
16042 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16043 snapshots) has been known to create a die giving a declaration
16044 for a class that has, as a child, a die giving a definition for a
16045 nested class. So we have to process our children even if the
16046 current die is a declaration. Normally, of course, a declaration
16047 won't have any children at all. */
16048
16049 child_die = die->child;
16050
16051 while (child_die != NULL && child_die->tag)
16052 {
16053 if (child_die->tag == DW_TAG_member
16054 || child_die->tag == DW_TAG_variable
16055 || child_die->tag == DW_TAG_inheritance
16056 || child_die->tag == DW_TAG_template_value_param
16057 || child_die->tag == DW_TAG_template_type_param)
16058 {
16059 /* Do nothing. */
16060 }
16061 else
16062 process_die (child_die, cu);
16063
16064 child_die = sibling_die (child_die);
16065 }
16066
16067 /* Do not consider external references. According to the DWARF standard,
16068 these DIEs are identified by the fact that they have no byte_size
16069 attribute, and a declaration attribute. */
16070 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16071 || !die_is_declaration (die, cu))
16072 new_symbol (die, type, cu);
16073 }
16074
16075 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16076 update TYPE using some information only available in DIE's children. */
16077
16078 static void
16079 update_enumeration_type_from_children (struct die_info *die,
16080 struct type *type,
16081 struct dwarf2_cu *cu)
16082 {
16083 struct die_info *child_die;
16084 int unsigned_enum = 1;
16085 int flag_enum = 1;
16086 ULONGEST mask = 0;
16087
16088 auto_obstack obstack;
16089
16090 for (child_die = die->child;
16091 child_die != NULL && child_die->tag;
16092 child_die = sibling_die (child_die))
16093 {
16094 struct attribute *attr;
16095 LONGEST value;
16096 const gdb_byte *bytes;
16097 struct dwarf2_locexpr_baton *baton;
16098 const char *name;
16099
16100 if (child_die->tag != DW_TAG_enumerator)
16101 continue;
16102
16103 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16104 if (attr == NULL)
16105 continue;
16106
16107 name = dwarf2_name (child_die, cu);
16108 if (name == NULL)
16109 name = "<anonymous enumerator>";
16110
16111 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16112 &value, &bytes, &baton);
16113 if (value < 0)
16114 {
16115 unsigned_enum = 0;
16116 flag_enum = 0;
16117 }
16118 else if ((mask & value) != 0)
16119 flag_enum = 0;
16120 else
16121 mask |= value;
16122
16123 /* If we already know that the enum type is neither unsigned, nor
16124 a flag type, no need to look at the rest of the enumerates. */
16125 if (!unsigned_enum && !flag_enum)
16126 break;
16127 }
16128
16129 if (unsigned_enum)
16130 TYPE_UNSIGNED (type) = 1;
16131 if (flag_enum)
16132 TYPE_FLAG_ENUM (type) = 1;
16133 }
16134
16135 /* Given a DW_AT_enumeration_type die, set its type. We do not
16136 complete the type's fields yet, or create any symbols. */
16137
16138 static struct type *
16139 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16140 {
16141 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16142 struct type *type;
16143 struct attribute *attr;
16144 const char *name;
16145
16146 /* If the definition of this type lives in .debug_types, read that type.
16147 Don't follow DW_AT_specification though, that will take us back up
16148 the chain and we want to go down. */
16149 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16150 if (attr)
16151 {
16152 type = get_DW_AT_signature_type (die, attr, cu);
16153
16154 /* The type's CU may not be the same as CU.
16155 Ensure TYPE is recorded with CU in die_type_hash. */
16156 return set_die_type (die, type, cu);
16157 }
16158
16159 type = alloc_type (objfile);
16160
16161 TYPE_CODE (type) = TYPE_CODE_ENUM;
16162 name = dwarf2_full_name (NULL, die, cu);
16163 if (name != NULL)
16164 TYPE_NAME (type) = name;
16165
16166 attr = dwarf2_attr (die, DW_AT_type, cu);
16167 if (attr != NULL)
16168 {
16169 struct type *underlying_type = die_type (die, cu);
16170
16171 TYPE_TARGET_TYPE (type) = underlying_type;
16172 }
16173
16174 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16175 if (attr)
16176 {
16177 TYPE_LENGTH (type) = DW_UNSND (attr);
16178 }
16179 else
16180 {
16181 TYPE_LENGTH (type) = 0;
16182 }
16183
16184 maybe_set_alignment (cu, die, type);
16185
16186 /* The enumeration DIE can be incomplete. In Ada, any type can be
16187 declared as private in the package spec, and then defined only
16188 inside the package body. Such types are known as Taft Amendment
16189 Types. When another package uses such a type, an incomplete DIE
16190 may be generated by the compiler. */
16191 if (die_is_declaration (die, cu))
16192 TYPE_STUB (type) = 1;
16193
16194 /* Finish the creation of this type by using the enum's children.
16195 We must call this even when the underlying type has been provided
16196 so that we can determine if we're looking at a "flag" enum. */
16197 update_enumeration_type_from_children (die, type, cu);
16198
16199 /* If this type has an underlying type that is not a stub, then we
16200 may use its attributes. We always use the "unsigned" attribute
16201 in this situation, because ordinarily we guess whether the type
16202 is unsigned -- but the guess can be wrong and the underlying type
16203 can tell us the reality. However, we defer to a local size
16204 attribute if one exists, because this lets the compiler override
16205 the underlying type if needed. */
16206 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16207 {
16208 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16209 if (TYPE_LENGTH (type) == 0)
16210 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16211 if (TYPE_RAW_ALIGN (type) == 0
16212 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16213 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16214 }
16215
16216 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16217
16218 return set_die_type (die, type, cu);
16219 }
16220
16221 /* Given a pointer to a die which begins an enumeration, process all
16222 the dies that define the members of the enumeration, and create the
16223 symbol for the enumeration type.
16224
16225 NOTE: We reverse the order of the element list. */
16226
16227 static void
16228 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16229 {
16230 struct type *this_type;
16231
16232 this_type = get_die_type (die, cu);
16233 if (this_type == NULL)
16234 this_type = read_enumeration_type (die, cu);
16235
16236 if (die->child != NULL)
16237 {
16238 struct die_info *child_die;
16239 struct symbol *sym;
16240 struct field *fields = NULL;
16241 int num_fields = 0;
16242 const char *name;
16243
16244 child_die = die->child;
16245 while (child_die && child_die->tag)
16246 {
16247 if (child_die->tag != DW_TAG_enumerator)
16248 {
16249 process_die (child_die, cu);
16250 }
16251 else
16252 {
16253 name = dwarf2_name (child_die, cu);
16254 if (name)
16255 {
16256 sym = new_symbol (child_die, this_type, cu);
16257
16258 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16259 {
16260 fields = (struct field *)
16261 xrealloc (fields,
16262 (num_fields + DW_FIELD_ALLOC_CHUNK)
16263 * sizeof (struct field));
16264 }
16265
16266 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16267 FIELD_TYPE (fields[num_fields]) = NULL;
16268 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16269 FIELD_BITSIZE (fields[num_fields]) = 0;
16270
16271 num_fields++;
16272 }
16273 }
16274
16275 child_die = sibling_die (child_die);
16276 }
16277
16278 if (num_fields)
16279 {
16280 TYPE_NFIELDS (this_type) = num_fields;
16281 TYPE_FIELDS (this_type) = (struct field *)
16282 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16283 memcpy (TYPE_FIELDS (this_type), fields,
16284 sizeof (struct field) * num_fields);
16285 xfree (fields);
16286 }
16287 }
16288
16289 /* If we are reading an enum from a .debug_types unit, and the enum
16290 is a declaration, and the enum is not the signatured type in the
16291 unit, then we do not want to add a symbol for it. Adding a
16292 symbol would in some cases obscure the true definition of the
16293 enum, giving users an incomplete type when the definition is
16294 actually available. Note that we do not want to do this for all
16295 enums which are just declarations, because C++0x allows forward
16296 enum declarations. */
16297 if (cu->per_cu->is_debug_types
16298 && die_is_declaration (die, cu))
16299 {
16300 struct signatured_type *sig_type;
16301
16302 sig_type = (struct signatured_type *) cu->per_cu;
16303 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16304 if (sig_type->type_offset_in_section != die->sect_off)
16305 return;
16306 }
16307
16308 new_symbol (die, this_type, cu);
16309 }
16310
16311 /* Extract all information from a DW_TAG_array_type DIE and put it in
16312 the DIE's type field. For now, this only handles one dimensional
16313 arrays. */
16314
16315 static struct type *
16316 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16317 {
16318 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16319 struct die_info *child_die;
16320 struct type *type;
16321 struct type *element_type, *range_type, *index_type;
16322 struct attribute *attr;
16323 const char *name;
16324 struct dynamic_prop *byte_stride_prop = NULL;
16325 unsigned int bit_stride = 0;
16326
16327 element_type = die_type (die, cu);
16328
16329 /* The die_type call above may have already set the type for this DIE. */
16330 type = get_die_type (die, cu);
16331 if (type)
16332 return type;
16333
16334 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16335 if (attr != NULL)
16336 {
16337 int stride_ok;
16338
16339 byte_stride_prop
16340 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16341 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16342 if (!stride_ok)
16343 {
16344 complaint (_("unable to read array DW_AT_byte_stride "
16345 " - DIE at %s [in module %s]"),
16346 sect_offset_str (die->sect_off),
16347 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16348 /* Ignore this attribute. We will likely not be able to print
16349 arrays of this type correctly, but there is little we can do
16350 to help if we cannot read the attribute's value. */
16351 byte_stride_prop = NULL;
16352 }
16353 }
16354
16355 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16356 if (attr != NULL)
16357 bit_stride = DW_UNSND (attr);
16358
16359 /* Irix 6.2 native cc creates array types without children for
16360 arrays with unspecified length. */
16361 if (die->child == NULL)
16362 {
16363 index_type = objfile_type (objfile)->builtin_int;
16364 range_type = create_static_range_type (NULL, index_type, 0, -1);
16365 type = create_array_type_with_stride (NULL, element_type, range_type,
16366 byte_stride_prop, bit_stride);
16367 return set_die_type (die, type, cu);
16368 }
16369
16370 std::vector<struct type *> range_types;
16371 child_die = die->child;
16372 while (child_die && child_die->tag)
16373 {
16374 if (child_die->tag == DW_TAG_subrange_type)
16375 {
16376 struct type *child_type = read_type_die (child_die, cu);
16377
16378 if (child_type != NULL)
16379 {
16380 /* The range type was succesfully read. Save it for the
16381 array type creation. */
16382 range_types.push_back (child_type);
16383 }
16384 }
16385 child_die = sibling_die (child_die);
16386 }
16387
16388 /* Dwarf2 dimensions are output from left to right, create the
16389 necessary array types in backwards order. */
16390
16391 type = element_type;
16392
16393 if (read_array_order (die, cu) == DW_ORD_col_major)
16394 {
16395 int i = 0;
16396
16397 while (i < range_types.size ())
16398 type = create_array_type_with_stride (NULL, type, range_types[i++],
16399 byte_stride_prop, bit_stride);
16400 }
16401 else
16402 {
16403 size_t ndim = range_types.size ();
16404 while (ndim-- > 0)
16405 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16406 byte_stride_prop, bit_stride);
16407 }
16408
16409 /* Understand Dwarf2 support for vector types (like they occur on
16410 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16411 array type. This is not part of the Dwarf2/3 standard yet, but a
16412 custom vendor extension. The main difference between a regular
16413 array and the vector variant is that vectors are passed by value
16414 to functions. */
16415 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16416 if (attr)
16417 make_vector_type (type);
16418
16419 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16420 implementation may choose to implement triple vectors using this
16421 attribute. */
16422 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16423 if (attr)
16424 {
16425 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16426 TYPE_LENGTH (type) = DW_UNSND (attr);
16427 else
16428 complaint (_("DW_AT_byte_size for array type smaller "
16429 "than the total size of elements"));
16430 }
16431
16432 name = dwarf2_name (die, cu);
16433 if (name)
16434 TYPE_NAME (type) = name;
16435
16436 maybe_set_alignment (cu, die, type);
16437
16438 /* Install the type in the die. */
16439 set_die_type (die, type, cu);
16440
16441 /* set_die_type should be already done. */
16442 set_descriptive_type (type, die, cu);
16443
16444 return type;
16445 }
16446
16447 static enum dwarf_array_dim_ordering
16448 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16449 {
16450 struct attribute *attr;
16451
16452 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16453
16454 if (attr)
16455 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16456
16457 /* GNU F77 is a special case, as at 08/2004 array type info is the
16458 opposite order to the dwarf2 specification, but data is still
16459 laid out as per normal fortran.
16460
16461 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16462 version checking. */
16463
16464 if (cu->language == language_fortran
16465 && cu->producer && strstr (cu->producer, "GNU F77"))
16466 {
16467 return DW_ORD_row_major;
16468 }
16469
16470 switch (cu->language_defn->la_array_ordering)
16471 {
16472 case array_column_major:
16473 return DW_ORD_col_major;
16474 case array_row_major:
16475 default:
16476 return DW_ORD_row_major;
16477 };
16478 }
16479
16480 /* Extract all information from a DW_TAG_set_type DIE and put it in
16481 the DIE's type field. */
16482
16483 static struct type *
16484 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16485 {
16486 struct type *domain_type, *set_type;
16487 struct attribute *attr;
16488
16489 domain_type = die_type (die, cu);
16490
16491 /* The die_type call above may have already set the type for this DIE. */
16492 set_type = get_die_type (die, cu);
16493 if (set_type)
16494 return set_type;
16495
16496 set_type = create_set_type (NULL, domain_type);
16497
16498 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16499 if (attr)
16500 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16501
16502 maybe_set_alignment (cu, die, set_type);
16503
16504 return set_die_type (die, set_type, cu);
16505 }
16506
16507 /* A helper for read_common_block that creates a locexpr baton.
16508 SYM is the symbol which we are marking as computed.
16509 COMMON_DIE is the DIE for the common block.
16510 COMMON_LOC is the location expression attribute for the common
16511 block itself.
16512 MEMBER_LOC is the location expression attribute for the particular
16513 member of the common block that we are processing.
16514 CU is the CU from which the above come. */
16515
16516 static void
16517 mark_common_block_symbol_computed (struct symbol *sym,
16518 struct die_info *common_die,
16519 struct attribute *common_loc,
16520 struct attribute *member_loc,
16521 struct dwarf2_cu *cu)
16522 {
16523 struct dwarf2_per_objfile *dwarf2_per_objfile
16524 = cu->per_cu->dwarf2_per_objfile;
16525 struct objfile *objfile = dwarf2_per_objfile->objfile;
16526 struct dwarf2_locexpr_baton *baton;
16527 gdb_byte *ptr;
16528 unsigned int cu_off;
16529 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16530 LONGEST offset = 0;
16531
16532 gdb_assert (common_loc && member_loc);
16533 gdb_assert (attr_form_is_block (common_loc));
16534 gdb_assert (attr_form_is_block (member_loc)
16535 || attr_form_is_constant (member_loc));
16536
16537 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16538 baton->per_cu = cu->per_cu;
16539 gdb_assert (baton->per_cu);
16540
16541 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16542
16543 if (attr_form_is_constant (member_loc))
16544 {
16545 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16546 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16547 }
16548 else
16549 baton->size += DW_BLOCK (member_loc)->size;
16550
16551 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16552 baton->data = ptr;
16553
16554 *ptr++ = DW_OP_call4;
16555 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16556 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16557 ptr += 4;
16558
16559 if (attr_form_is_constant (member_loc))
16560 {
16561 *ptr++ = DW_OP_addr;
16562 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16563 ptr += cu->header.addr_size;
16564 }
16565 else
16566 {
16567 /* We have to copy the data here, because DW_OP_call4 will only
16568 use a DW_AT_location attribute. */
16569 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16570 ptr += DW_BLOCK (member_loc)->size;
16571 }
16572
16573 *ptr++ = DW_OP_plus;
16574 gdb_assert (ptr - baton->data == baton->size);
16575
16576 SYMBOL_LOCATION_BATON (sym) = baton;
16577 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16578 }
16579
16580 /* Create appropriate locally-scoped variables for all the
16581 DW_TAG_common_block entries. Also create a struct common_block
16582 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16583 is used to sepate the common blocks name namespace from regular
16584 variable names. */
16585
16586 static void
16587 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16588 {
16589 struct attribute *attr;
16590
16591 attr = dwarf2_attr (die, DW_AT_location, cu);
16592 if (attr)
16593 {
16594 /* Support the .debug_loc offsets. */
16595 if (attr_form_is_block (attr))
16596 {
16597 /* Ok. */
16598 }
16599 else if (attr_form_is_section_offset (attr))
16600 {
16601 dwarf2_complex_location_expr_complaint ();
16602 attr = NULL;
16603 }
16604 else
16605 {
16606 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16607 "common block member");
16608 attr = NULL;
16609 }
16610 }
16611
16612 if (die->child != NULL)
16613 {
16614 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16615 struct die_info *child_die;
16616 size_t n_entries = 0, size;
16617 struct common_block *common_block;
16618 struct symbol *sym;
16619
16620 for (child_die = die->child;
16621 child_die && child_die->tag;
16622 child_die = sibling_die (child_die))
16623 ++n_entries;
16624
16625 size = (sizeof (struct common_block)
16626 + (n_entries - 1) * sizeof (struct symbol *));
16627 common_block
16628 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16629 size);
16630 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16631 common_block->n_entries = 0;
16632
16633 for (child_die = die->child;
16634 child_die && child_die->tag;
16635 child_die = sibling_die (child_die))
16636 {
16637 /* Create the symbol in the DW_TAG_common_block block in the current
16638 symbol scope. */
16639 sym = new_symbol (child_die, NULL, cu);
16640 if (sym != NULL)
16641 {
16642 struct attribute *member_loc;
16643
16644 common_block->contents[common_block->n_entries++] = sym;
16645
16646 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16647 cu);
16648 if (member_loc)
16649 {
16650 /* GDB has handled this for a long time, but it is
16651 not specified by DWARF. It seems to have been
16652 emitted by gfortran at least as recently as:
16653 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16654 complaint (_("Variable in common block has "
16655 "DW_AT_data_member_location "
16656 "- DIE at %s [in module %s]"),
16657 sect_offset_str (child_die->sect_off),
16658 objfile_name (objfile));
16659
16660 if (attr_form_is_section_offset (member_loc))
16661 dwarf2_complex_location_expr_complaint ();
16662 else if (attr_form_is_constant (member_loc)
16663 || attr_form_is_block (member_loc))
16664 {
16665 if (attr)
16666 mark_common_block_symbol_computed (sym, die, attr,
16667 member_loc, cu);
16668 }
16669 else
16670 dwarf2_complex_location_expr_complaint ();
16671 }
16672 }
16673 }
16674
16675 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16676 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16677 }
16678 }
16679
16680 /* Create a type for a C++ namespace. */
16681
16682 static struct type *
16683 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16684 {
16685 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16686 const char *previous_prefix, *name;
16687 int is_anonymous;
16688 struct type *type;
16689
16690 /* For extensions, reuse the type of the original namespace. */
16691 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16692 {
16693 struct die_info *ext_die;
16694 struct dwarf2_cu *ext_cu = cu;
16695
16696 ext_die = dwarf2_extension (die, &ext_cu);
16697 type = read_type_die (ext_die, ext_cu);
16698
16699 /* EXT_CU may not be the same as CU.
16700 Ensure TYPE is recorded with CU in die_type_hash. */
16701 return set_die_type (die, type, cu);
16702 }
16703
16704 name = namespace_name (die, &is_anonymous, cu);
16705
16706 /* Now build the name of the current namespace. */
16707
16708 previous_prefix = determine_prefix (die, cu);
16709 if (previous_prefix[0] != '\0')
16710 name = typename_concat (&objfile->objfile_obstack,
16711 previous_prefix, name, 0, cu);
16712
16713 /* Create the type. */
16714 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16715
16716 return set_die_type (die, type, cu);
16717 }
16718
16719 /* Read a namespace scope. */
16720
16721 static void
16722 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16723 {
16724 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16725 int is_anonymous;
16726
16727 /* Add a symbol associated to this if we haven't seen the namespace
16728 before. Also, add a using directive if it's an anonymous
16729 namespace. */
16730
16731 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16732 {
16733 struct type *type;
16734
16735 type = read_type_die (die, cu);
16736 new_symbol (die, type, cu);
16737
16738 namespace_name (die, &is_anonymous, cu);
16739 if (is_anonymous)
16740 {
16741 const char *previous_prefix = determine_prefix (die, cu);
16742
16743 std::vector<const char *> excludes;
16744 add_using_directive (using_directives (cu),
16745 previous_prefix, TYPE_NAME (type), NULL,
16746 NULL, excludes, 0, &objfile->objfile_obstack);
16747 }
16748 }
16749
16750 if (die->child != NULL)
16751 {
16752 struct die_info *child_die = die->child;
16753
16754 while (child_die && child_die->tag)
16755 {
16756 process_die (child_die, cu);
16757 child_die = sibling_die (child_die);
16758 }
16759 }
16760 }
16761
16762 /* Read a Fortran module as type. This DIE can be only a declaration used for
16763 imported module. Still we need that type as local Fortran "use ... only"
16764 declaration imports depend on the created type in determine_prefix. */
16765
16766 static struct type *
16767 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16768 {
16769 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16770 const char *module_name;
16771 struct type *type;
16772
16773 module_name = dwarf2_name (die, cu);
16774 if (!module_name)
16775 complaint (_("DW_TAG_module has no name, offset %s"),
16776 sect_offset_str (die->sect_off));
16777 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16778
16779 return set_die_type (die, type, cu);
16780 }
16781
16782 /* Read a Fortran module. */
16783
16784 static void
16785 read_module (struct die_info *die, struct dwarf2_cu *cu)
16786 {
16787 struct die_info *child_die = die->child;
16788 struct type *type;
16789
16790 type = read_type_die (die, cu);
16791 new_symbol (die, type, cu);
16792
16793 while (child_die && child_die->tag)
16794 {
16795 process_die (child_die, cu);
16796 child_die = sibling_die (child_die);
16797 }
16798 }
16799
16800 /* Return the name of the namespace represented by DIE. Set
16801 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16802 namespace. */
16803
16804 static const char *
16805 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16806 {
16807 struct die_info *current_die;
16808 const char *name = NULL;
16809
16810 /* Loop through the extensions until we find a name. */
16811
16812 for (current_die = die;
16813 current_die != NULL;
16814 current_die = dwarf2_extension (die, &cu))
16815 {
16816 /* We don't use dwarf2_name here so that we can detect the absence
16817 of a name -> anonymous namespace. */
16818 name = dwarf2_string_attr (die, DW_AT_name, cu);
16819
16820 if (name != NULL)
16821 break;
16822 }
16823
16824 /* Is it an anonymous namespace? */
16825
16826 *is_anonymous = (name == NULL);
16827 if (*is_anonymous)
16828 name = CP_ANONYMOUS_NAMESPACE_STR;
16829
16830 return name;
16831 }
16832
16833 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16834 the user defined type vector. */
16835
16836 static struct type *
16837 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16838 {
16839 struct gdbarch *gdbarch
16840 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16841 struct comp_unit_head *cu_header = &cu->header;
16842 struct type *type;
16843 struct attribute *attr_byte_size;
16844 struct attribute *attr_address_class;
16845 int byte_size, addr_class;
16846 struct type *target_type;
16847
16848 target_type = die_type (die, cu);
16849
16850 /* The die_type call above may have already set the type for this DIE. */
16851 type = get_die_type (die, cu);
16852 if (type)
16853 return type;
16854
16855 type = lookup_pointer_type (target_type);
16856
16857 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16858 if (attr_byte_size)
16859 byte_size = DW_UNSND (attr_byte_size);
16860 else
16861 byte_size = cu_header->addr_size;
16862
16863 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16864 if (attr_address_class)
16865 addr_class = DW_UNSND (attr_address_class);
16866 else
16867 addr_class = DW_ADDR_none;
16868
16869 ULONGEST alignment = get_alignment (cu, die);
16870
16871 /* If the pointer size, alignment, or address class is different
16872 than the default, create a type variant marked as such and set
16873 the length accordingly. */
16874 if (TYPE_LENGTH (type) != byte_size
16875 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16876 && alignment != TYPE_RAW_ALIGN (type))
16877 || addr_class != DW_ADDR_none)
16878 {
16879 if (gdbarch_address_class_type_flags_p (gdbarch))
16880 {
16881 int type_flags;
16882
16883 type_flags = gdbarch_address_class_type_flags
16884 (gdbarch, byte_size, addr_class);
16885 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16886 == 0);
16887 type = make_type_with_address_space (type, type_flags);
16888 }
16889 else if (TYPE_LENGTH (type) != byte_size)
16890 {
16891 complaint (_("invalid pointer size %d"), byte_size);
16892 }
16893 else if (TYPE_RAW_ALIGN (type) != alignment)
16894 {
16895 complaint (_("Invalid DW_AT_alignment"
16896 " - DIE at %s [in module %s]"),
16897 sect_offset_str (die->sect_off),
16898 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16899 }
16900 else
16901 {
16902 /* Should we also complain about unhandled address classes? */
16903 }
16904 }
16905
16906 TYPE_LENGTH (type) = byte_size;
16907 set_type_align (type, alignment);
16908 return set_die_type (die, type, cu);
16909 }
16910
16911 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16912 the user defined type vector. */
16913
16914 static struct type *
16915 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16916 {
16917 struct type *type;
16918 struct type *to_type;
16919 struct type *domain;
16920
16921 to_type = die_type (die, cu);
16922 domain = die_containing_type (die, cu);
16923
16924 /* The calls above may have already set the type for this DIE. */
16925 type = get_die_type (die, cu);
16926 if (type)
16927 return type;
16928
16929 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16930 type = lookup_methodptr_type (to_type);
16931 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16932 {
16933 struct type *new_type
16934 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16935
16936 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16937 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16938 TYPE_VARARGS (to_type));
16939 type = lookup_methodptr_type (new_type);
16940 }
16941 else
16942 type = lookup_memberptr_type (to_type, domain);
16943
16944 return set_die_type (die, type, cu);
16945 }
16946
16947 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16948 the user defined type vector. */
16949
16950 static struct type *
16951 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16952 enum type_code refcode)
16953 {
16954 struct comp_unit_head *cu_header = &cu->header;
16955 struct type *type, *target_type;
16956 struct attribute *attr;
16957
16958 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16959
16960 target_type = die_type (die, cu);
16961
16962 /* The die_type call above may have already set the type for this DIE. */
16963 type = get_die_type (die, cu);
16964 if (type)
16965 return type;
16966
16967 type = lookup_reference_type (target_type, refcode);
16968 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16969 if (attr)
16970 {
16971 TYPE_LENGTH (type) = DW_UNSND (attr);
16972 }
16973 else
16974 {
16975 TYPE_LENGTH (type) = cu_header->addr_size;
16976 }
16977 maybe_set_alignment (cu, die, type);
16978 return set_die_type (die, type, cu);
16979 }
16980
16981 /* Add the given cv-qualifiers to the element type of the array. GCC
16982 outputs DWARF type qualifiers that apply to an array, not the
16983 element type. But GDB relies on the array element type to carry
16984 the cv-qualifiers. This mimics section 6.7.3 of the C99
16985 specification. */
16986
16987 static struct type *
16988 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16989 struct type *base_type, int cnst, int voltl)
16990 {
16991 struct type *el_type, *inner_array;
16992
16993 base_type = copy_type (base_type);
16994 inner_array = base_type;
16995
16996 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16997 {
16998 TYPE_TARGET_TYPE (inner_array) =
16999 copy_type (TYPE_TARGET_TYPE (inner_array));
17000 inner_array = TYPE_TARGET_TYPE (inner_array);
17001 }
17002
17003 el_type = TYPE_TARGET_TYPE (inner_array);
17004 cnst |= TYPE_CONST (el_type);
17005 voltl |= TYPE_VOLATILE (el_type);
17006 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17007
17008 return set_die_type (die, base_type, cu);
17009 }
17010
17011 static struct type *
17012 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17013 {
17014 struct type *base_type, *cv_type;
17015
17016 base_type = die_type (die, cu);
17017
17018 /* The die_type call above may have already set the type for this DIE. */
17019 cv_type = get_die_type (die, cu);
17020 if (cv_type)
17021 return cv_type;
17022
17023 /* In case the const qualifier is applied to an array type, the element type
17024 is so qualified, not the array type (section 6.7.3 of C99). */
17025 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17026 return add_array_cv_type (die, cu, base_type, 1, 0);
17027
17028 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17029 return set_die_type (die, cv_type, cu);
17030 }
17031
17032 static struct type *
17033 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17034 {
17035 struct type *base_type, *cv_type;
17036
17037 base_type = die_type (die, cu);
17038
17039 /* The die_type call above may have already set the type for this DIE. */
17040 cv_type = get_die_type (die, cu);
17041 if (cv_type)
17042 return cv_type;
17043
17044 /* In case the volatile qualifier is applied to an array type, the
17045 element type is so qualified, not the array type (section 6.7.3
17046 of C99). */
17047 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17048 return add_array_cv_type (die, cu, base_type, 0, 1);
17049
17050 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17051 return set_die_type (die, cv_type, cu);
17052 }
17053
17054 /* Handle DW_TAG_restrict_type. */
17055
17056 static struct type *
17057 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17058 {
17059 struct type *base_type, *cv_type;
17060
17061 base_type = die_type (die, cu);
17062
17063 /* The die_type call above may have already set the type for this DIE. */
17064 cv_type = get_die_type (die, cu);
17065 if (cv_type)
17066 return cv_type;
17067
17068 cv_type = make_restrict_type (base_type);
17069 return set_die_type (die, cv_type, cu);
17070 }
17071
17072 /* Handle DW_TAG_atomic_type. */
17073
17074 static struct type *
17075 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17076 {
17077 struct type *base_type, *cv_type;
17078
17079 base_type = die_type (die, cu);
17080
17081 /* The die_type call above may have already set the type for this DIE. */
17082 cv_type = get_die_type (die, cu);
17083 if (cv_type)
17084 return cv_type;
17085
17086 cv_type = make_atomic_type (base_type);
17087 return set_die_type (die, cv_type, cu);
17088 }
17089
17090 /* Extract all information from a DW_TAG_string_type DIE and add to
17091 the user defined type vector. It isn't really a user defined type,
17092 but it behaves like one, with other DIE's using an AT_user_def_type
17093 attribute to reference it. */
17094
17095 static struct type *
17096 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17097 {
17098 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17099 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17100 struct type *type, *range_type, *index_type, *char_type;
17101 struct attribute *attr;
17102 unsigned int length;
17103
17104 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17105 if (attr)
17106 {
17107 length = DW_UNSND (attr);
17108 }
17109 else
17110 {
17111 /* Check for the DW_AT_byte_size attribute. */
17112 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17113 if (attr)
17114 {
17115 length = DW_UNSND (attr);
17116 }
17117 else
17118 {
17119 length = 1;
17120 }
17121 }
17122
17123 index_type = objfile_type (objfile)->builtin_int;
17124 range_type = create_static_range_type (NULL, index_type, 1, length);
17125 char_type = language_string_char_type (cu->language_defn, gdbarch);
17126 type = create_string_type (NULL, char_type, range_type);
17127
17128 return set_die_type (die, type, cu);
17129 }
17130
17131 /* Assuming that DIE corresponds to a function, returns nonzero
17132 if the function is prototyped. */
17133
17134 static int
17135 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17136 {
17137 struct attribute *attr;
17138
17139 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17140 if (attr && (DW_UNSND (attr) != 0))
17141 return 1;
17142
17143 /* The DWARF standard implies that the DW_AT_prototyped attribute
17144 is only meaninful for C, but the concept also extends to other
17145 languages that allow unprototyped functions (Eg: Objective C).
17146 For all other languages, assume that functions are always
17147 prototyped. */
17148 if (cu->language != language_c
17149 && cu->language != language_objc
17150 && cu->language != language_opencl)
17151 return 1;
17152
17153 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17154 prototyped and unprototyped functions; default to prototyped,
17155 since that is more common in modern code (and RealView warns
17156 about unprototyped functions). */
17157 if (producer_is_realview (cu->producer))
17158 return 1;
17159
17160 return 0;
17161 }
17162
17163 /* Handle DIES due to C code like:
17164
17165 struct foo
17166 {
17167 int (*funcp)(int a, long l);
17168 int b;
17169 };
17170
17171 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17172
17173 static struct type *
17174 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17175 {
17176 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17177 struct type *type; /* Type that this function returns. */
17178 struct type *ftype; /* Function that returns above type. */
17179 struct attribute *attr;
17180
17181 type = die_type (die, cu);
17182
17183 /* The die_type call above may have already set the type for this DIE. */
17184 ftype = get_die_type (die, cu);
17185 if (ftype)
17186 return ftype;
17187
17188 ftype = lookup_function_type (type);
17189
17190 if (prototyped_function_p (die, cu))
17191 TYPE_PROTOTYPED (ftype) = 1;
17192
17193 /* Store the calling convention in the type if it's available in
17194 the subroutine die. Otherwise set the calling convention to
17195 the default value DW_CC_normal. */
17196 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17197 if (attr)
17198 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17199 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17200 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17201 else
17202 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17203
17204 /* Record whether the function returns normally to its caller or not
17205 if the DWARF producer set that information. */
17206 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17207 if (attr && (DW_UNSND (attr) != 0))
17208 TYPE_NO_RETURN (ftype) = 1;
17209
17210 /* We need to add the subroutine type to the die immediately so
17211 we don't infinitely recurse when dealing with parameters
17212 declared as the same subroutine type. */
17213 set_die_type (die, ftype, cu);
17214
17215 if (die->child != NULL)
17216 {
17217 struct type *void_type = objfile_type (objfile)->builtin_void;
17218 struct die_info *child_die;
17219 int nparams, iparams;
17220
17221 /* Count the number of parameters.
17222 FIXME: GDB currently ignores vararg functions, but knows about
17223 vararg member functions. */
17224 nparams = 0;
17225 child_die = die->child;
17226 while (child_die && child_die->tag)
17227 {
17228 if (child_die->tag == DW_TAG_formal_parameter)
17229 nparams++;
17230 else if (child_die->tag == DW_TAG_unspecified_parameters)
17231 TYPE_VARARGS (ftype) = 1;
17232 child_die = sibling_die (child_die);
17233 }
17234
17235 /* Allocate storage for parameters and fill them in. */
17236 TYPE_NFIELDS (ftype) = nparams;
17237 TYPE_FIELDS (ftype) = (struct field *)
17238 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17239
17240 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17241 even if we error out during the parameters reading below. */
17242 for (iparams = 0; iparams < nparams; iparams++)
17243 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17244
17245 iparams = 0;
17246 child_die = die->child;
17247 while (child_die && child_die->tag)
17248 {
17249 if (child_die->tag == DW_TAG_formal_parameter)
17250 {
17251 struct type *arg_type;
17252
17253 /* DWARF version 2 has no clean way to discern C++
17254 static and non-static member functions. G++ helps
17255 GDB by marking the first parameter for non-static
17256 member functions (which is the this pointer) as
17257 artificial. We pass this information to
17258 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17259
17260 DWARF version 3 added DW_AT_object_pointer, which GCC
17261 4.5 does not yet generate. */
17262 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17263 if (attr)
17264 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17265 else
17266 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17267 arg_type = die_type (child_die, cu);
17268
17269 /* RealView does not mark THIS as const, which the testsuite
17270 expects. GCC marks THIS as const in method definitions,
17271 but not in the class specifications (GCC PR 43053). */
17272 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17273 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17274 {
17275 int is_this = 0;
17276 struct dwarf2_cu *arg_cu = cu;
17277 const char *name = dwarf2_name (child_die, cu);
17278
17279 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17280 if (attr)
17281 {
17282 /* If the compiler emits this, use it. */
17283 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17284 is_this = 1;
17285 }
17286 else if (name && strcmp (name, "this") == 0)
17287 /* Function definitions will have the argument names. */
17288 is_this = 1;
17289 else if (name == NULL && iparams == 0)
17290 /* Declarations may not have the names, so like
17291 elsewhere in GDB, assume an artificial first
17292 argument is "this". */
17293 is_this = 1;
17294
17295 if (is_this)
17296 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17297 arg_type, 0);
17298 }
17299
17300 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17301 iparams++;
17302 }
17303 child_die = sibling_die (child_die);
17304 }
17305 }
17306
17307 return ftype;
17308 }
17309
17310 static struct type *
17311 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17312 {
17313 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17314 const char *name = NULL;
17315 struct type *this_type, *target_type;
17316
17317 name = dwarf2_full_name (NULL, die, cu);
17318 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17319 TYPE_TARGET_STUB (this_type) = 1;
17320 set_die_type (die, this_type, cu);
17321 target_type = die_type (die, cu);
17322 if (target_type != this_type)
17323 TYPE_TARGET_TYPE (this_type) = target_type;
17324 else
17325 {
17326 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17327 spec and cause infinite loops in GDB. */
17328 complaint (_("Self-referential DW_TAG_typedef "
17329 "- DIE at %s [in module %s]"),
17330 sect_offset_str (die->sect_off), objfile_name (objfile));
17331 TYPE_TARGET_TYPE (this_type) = NULL;
17332 }
17333 return this_type;
17334 }
17335
17336 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17337 (which may be different from NAME) to the architecture back-end to allow
17338 it to guess the correct format if necessary. */
17339
17340 static struct type *
17341 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17342 const char *name_hint)
17343 {
17344 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17345 const struct floatformat **format;
17346 struct type *type;
17347
17348 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17349 if (format)
17350 type = init_float_type (objfile, bits, name, format);
17351 else
17352 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17353
17354 return type;
17355 }
17356
17357 /* Find a representation of a given base type and install
17358 it in the TYPE field of the die. */
17359
17360 static struct type *
17361 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17362 {
17363 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17364 struct type *type;
17365 struct attribute *attr;
17366 int encoding = 0, bits = 0;
17367 const char *name;
17368
17369 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17370 if (attr)
17371 {
17372 encoding = DW_UNSND (attr);
17373 }
17374 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17375 if (attr)
17376 {
17377 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17378 }
17379 name = dwarf2_name (die, cu);
17380 if (!name)
17381 {
17382 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17383 }
17384
17385 switch (encoding)
17386 {
17387 case DW_ATE_address:
17388 /* Turn DW_ATE_address into a void * pointer. */
17389 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17390 type = init_pointer_type (objfile, bits, name, type);
17391 break;
17392 case DW_ATE_boolean:
17393 type = init_boolean_type (objfile, bits, 1, name);
17394 break;
17395 case DW_ATE_complex_float:
17396 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17397 type = init_complex_type (objfile, name, type);
17398 break;
17399 case DW_ATE_decimal_float:
17400 type = init_decfloat_type (objfile, bits, name);
17401 break;
17402 case DW_ATE_float:
17403 type = dwarf2_init_float_type (objfile, bits, name, name);
17404 break;
17405 case DW_ATE_signed:
17406 type = init_integer_type (objfile, bits, 0, name);
17407 break;
17408 case DW_ATE_unsigned:
17409 if (cu->language == language_fortran
17410 && name
17411 && startswith (name, "character("))
17412 type = init_character_type (objfile, bits, 1, name);
17413 else
17414 type = init_integer_type (objfile, bits, 1, name);
17415 break;
17416 case DW_ATE_signed_char:
17417 if (cu->language == language_ada || cu->language == language_m2
17418 || cu->language == language_pascal
17419 || cu->language == language_fortran)
17420 type = init_character_type (objfile, bits, 0, name);
17421 else
17422 type = init_integer_type (objfile, bits, 0, name);
17423 break;
17424 case DW_ATE_unsigned_char:
17425 if (cu->language == language_ada || cu->language == language_m2
17426 || cu->language == language_pascal
17427 || cu->language == language_fortran
17428 || cu->language == language_rust)
17429 type = init_character_type (objfile, bits, 1, name);
17430 else
17431 type = init_integer_type (objfile, bits, 1, name);
17432 break;
17433 case DW_ATE_UTF:
17434 {
17435 gdbarch *arch = get_objfile_arch (objfile);
17436
17437 if (bits == 16)
17438 type = builtin_type (arch)->builtin_char16;
17439 else if (bits == 32)
17440 type = builtin_type (arch)->builtin_char32;
17441 else
17442 {
17443 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17444 bits);
17445 type = init_integer_type (objfile, bits, 1, name);
17446 }
17447 return set_die_type (die, type, cu);
17448 }
17449 break;
17450
17451 default:
17452 complaint (_("unsupported DW_AT_encoding: '%s'"),
17453 dwarf_type_encoding_name (encoding));
17454 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17455 break;
17456 }
17457
17458 if (name && strcmp (name, "char") == 0)
17459 TYPE_NOSIGN (type) = 1;
17460
17461 maybe_set_alignment (cu, die, type);
17462
17463 return set_die_type (die, type, cu);
17464 }
17465
17466 /* Parse dwarf attribute if it's a block, reference or constant and put the
17467 resulting value of the attribute into struct bound_prop.
17468 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17469
17470 static int
17471 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17472 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17473 {
17474 struct dwarf2_property_baton *baton;
17475 struct obstack *obstack
17476 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17477
17478 if (attr == NULL || prop == NULL)
17479 return 0;
17480
17481 if (attr_form_is_block (attr))
17482 {
17483 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17484 baton->referenced_type = NULL;
17485 baton->locexpr.per_cu = cu->per_cu;
17486 baton->locexpr.size = DW_BLOCK (attr)->size;
17487 baton->locexpr.data = DW_BLOCK (attr)->data;
17488 prop->data.baton = baton;
17489 prop->kind = PROP_LOCEXPR;
17490 gdb_assert (prop->data.baton != NULL);
17491 }
17492 else if (attr_form_is_ref (attr))
17493 {
17494 struct dwarf2_cu *target_cu = cu;
17495 struct die_info *target_die;
17496 struct attribute *target_attr;
17497
17498 target_die = follow_die_ref (die, attr, &target_cu);
17499 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17500 if (target_attr == NULL)
17501 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17502 target_cu);
17503 if (target_attr == NULL)
17504 return 0;
17505
17506 switch (target_attr->name)
17507 {
17508 case DW_AT_location:
17509 if (attr_form_is_section_offset (target_attr))
17510 {
17511 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17512 baton->referenced_type = die_type (target_die, target_cu);
17513 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17514 prop->data.baton = baton;
17515 prop->kind = PROP_LOCLIST;
17516 gdb_assert (prop->data.baton != NULL);
17517 }
17518 else if (attr_form_is_block (target_attr))
17519 {
17520 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17521 baton->referenced_type = die_type (target_die, target_cu);
17522 baton->locexpr.per_cu = cu->per_cu;
17523 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17524 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17525 prop->data.baton = baton;
17526 prop->kind = PROP_LOCEXPR;
17527 gdb_assert (prop->data.baton != NULL);
17528 }
17529 else
17530 {
17531 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17532 "dynamic property");
17533 return 0;
17534 }
17535 break;
17536 case DW_AT_data_member_location:
17537 {
17538 LONGEST offset;
17539
17540 if (!handle_data_member_location (target_die, target_cu,
17541 &offset))
17542 return 0;
17543
17544 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17545 baton->referenced_type = read_type_die (target_die->parent,
17546 target_cu);
17547 baton->offset_info.offset = offset;
17548 baton->offset_info.type = die_type (target_die, target_cu);
17549 prop->data.baton = baton;
17550 prop->kind = PROP_ADDR_OFFSET;
17551 break;
17552 }
17553 }
17554 }
17555 else if (attr_form_is_constant (attr))
17556 {
17557 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17558 prop->kind = PROP_CONST;
17559 }
17560 else
17561 {
17562 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17563 dwarf2_name (die, cu));
17564 return 0;
17565 }
17566
17567 return 1;
17568 }
17569
17570 /* Read the given DW_AT_subrange DIE. */
17571
17572 static struct type *
17573 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17574 {
17575 struct type *base_type, *orig_base_type;
17576 struct type *range_type;
17577 struct attribute *attr;
17578 struct dynamic_prop low, high;
17579 int low_default_is_valid;
17580 int high_bound_is_count = 0;
17581 const char *name;
17582 LONGEST negative_mask;
17583
17584 orig_base_type = die_type (die, cu);
17585 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17586 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17587 creating the range type, but we use the result of check_typedef
17588 when examining properties of the type. */
17589 base_type = check_typedef (orig_base_type);
17590
17591 /* The die_type call above may have already set the type for this DIE. */
17592 range_type = get_die_type (die, cu);
17593 if (range_type)
17594 return range_type;
17595
17596 low.kind = PROP_CONST;
17597 high.kind = PROP_CONST;
17598 high.data.const_val = 0;
17599
17600 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17601 omitting DW_AT_lower_bound. */
17602 switch (cu->language)
17603 {
17604 case language_c:
17605 case language_cplus:
17606 low.data.const_val = 0;
17607 low_default_is_valid = 1;
17608 break;
17609 case language_fortran:
17610 low.data.const_val = 1;
17611 low_default_is_valid = 1;
17612 break;
17613 case language_d:
17614 case language_objc:
17615 case language_rust:
17616 low.data.const_val = 0;
17617 low_default_is_valid = (cu->header.version >= 4);
17618 break;
17619 case language_ada:
17620 case language_m2:
17621 case language_pascal:
17622 low.data.const_val = 1;
17623 low_default_is_valid = (cu->header.version >= 4);
17624 break;
17625 default:
17626 low.data.const_val = 0;
17627 low_default_is_valid = 0;
17628 break;
17629 }
17630
17631 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17632 if (attr)
17633 attr_to_dynamic_prop (attr, die, cu, &low);
17634 else if (!low_default_is_valid)
17635 complaint (_("Missing DW_AT_lower_bound "
17636 "- DIE at %s [in module %s]"),
17637 sect_offset_str (die->sect_off),
17638 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17639
17640 struct attribute *attr_ub, *attr_count;
17641 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17642 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17643 {
17644 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17645 if (attr_to_dynamic_prop (attr, die, cu, &high))
17646 {
17647 /* If bounds are constant do the final calculation here. */
17648 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17649 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17650 else
17651 high_bound_is_count = 1;
17652 }
17653 else
17654 {
17655 if (attr_ub != NULL)
17656 complaint (_("Unresolved DW_AT_upper_bound "
17657 "- DIE at %s [in module %s]"),
17658 sect_offset_str (die->sect_off),
17659 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17660 if (attr_count != NULL)
17661 complaint (_("Unresolved DW_AT_count "
17662 "- DIE at %s [in module %s]"),
17663 sect_offset_str (die->sect_off),
17664 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17665 }
17666
17667 }
17668
17669 /* Dwarf-2 specifications explicitly allows to create subrange types
17670 without specifying a base type.
17671 In that case, the base type must be set to the type of
17672 the lower bound, upper bound or count, in that order, if any of these
17673 three attributes references an object that has a type.
17674 If no base type is found, the Dwarf-2 specifications say that
17675 a signed integer type of size equal to the size of an address should
17676 be used.
17677 For the following C code: `extern char gdb_int [];'
17678 GCC produces an empty range DIE.
17679 FIXME: muller/2010-05-28: Possible references to object for low bound,
17680 high bound or count are not yet handled by this code. */
17681 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17682 {
17683 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17684 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17685 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17686 struct type *int_type = objfile_type (objfile)->builtin_int;
17687
17688 /* Test "int", "long int", and "long long int" objfile types,
17689 and select the first one having a size above or equal to the
17690 architecture address size. */
17691 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17692 base_type = int_type;
17693 else
17694 {
17695 int_type = objfile_type (objfile)->builtin_long;
17696 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17697 base_type = int_type;
17698 else
17699 {
17700 int_type = objfile_type (objfile)->builtin_long_long;
17701 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17702 base_type = int_type;
17703 }
17704 }
17705 }
17706
17707 /* Normally, the DWARF producers are expected to use a signed
17708 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17709 But this is unfortunately not always the case, as witnessed
17710 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17711 is used instead. To work around that ambiguity, we treat
17712 the bounds as signed, and thus sign-extend their values, when
17713 the base type is signed. */
17714 negative_mask =
17715 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17716 if (low.kind == PROP_CONST
17717 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17718 low.data.const_val |= negative_mask;
17719 if (high.kind == PROP_CONST
17720 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17721 high.data.const_val |= negative_mask;
17722
17723 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17724
17725 if (high_bound_is_count)
17726 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17727
17728 /* Ada expects an empty array on no boundary attributes. */
17729 if (attr == NULL && cu->language != language_ada)
17730 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17731
17732 name = dwarf2_name (die, cu);
17733 if (name)
17734 TYPE_NAME (range_type) = name;
17735
17736 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17737 if (attr)
17738 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17739
17740 maybe_set_alignment (cu, die, range_type);
17741
17742 set_die_type (die, range_type, cu);
17743
17744 /* set_die_type should be already done. */
17745 set_descriptive_type (range_type, die, cu);
17746
17747 return range_type;
17748 }
17749
17750 static struct type *
17751 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17752 {
17753 struct type *type;
17754
17755 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17756 NULL);
17757 TYPE_NAME (type) = dwarf2_name (die, cu);
17758
17759 /* In Ada, an unspecified type is typically used when the description
17760 of the type is defered to a different unit. When encountering
17761 such a type, we treat it as a stub, and try to resolve it later on,
17762 when needed. */
17763 if (cu->language == language_ada)
17764 TYPE_STUB (type) = 1;
17765
17766 return set_die_type (die, type, cu);
17767 }
17768
17769 /* Read a single die and all its descendents. Set the die's sibling
17770 field to NULL; set other fields in the die correctly, and set all
17771 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17772 location of the info_ptr after reading all of those dies. PARENT
17773 is the parent of the die in question. */
17774
17775 static struct die_info *
17776 read_die_and_children (const struct die_reader_specs *reader,
17777 const gdb_byte *info_ptr,
17778 const gdb_byte **new_info_ptr,
17779 struct die_info *parent)
17780 {
17781 struct die_info *die;
17782 const gdb_byte *cur_ptr;
17783 int has_children;
17784
17785 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17786 if (die == NULL)
17787 {
17788 *new_info_ptr = cur_ptr;
17789 return NULL;
17790 }
17791 store_in_ref_table (die, reader->cu);
17792
17793 if (has_children)
17794 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17795 else
17796 {
17797 die->child = NULL;
17798 *new_info_ptr = cur_ptr;
17799 }
17800
17801 die->sibling = NULL;
17802 die->parent = parent;
17803 return die;
17804 }
17805
17806 /* Read a die, all of its descendents, and all of its siblings; set
17807 all of the fields of all of the dies correctly. Arguments are as
17808 in read_die_and_children. */
17809
17810 static struct die_info *
17811 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17812 const gdb_byte *info_ptr,
17813 const gdb_byte **new_info_ptr,
17814 struct die_info *parent)
17815 {
17816 struct die_info *first_die, *last_sibling;
17817 const gdb_byte *cur_ptr;
17818
17819 cur_ptr = info_ptr;
17820 first_die = last_sibling = NULL;
17821
17822 while (1)
17823 {
17824 struct die_info *die
17825 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17826
17827 if (die == NULL)
17828 {
17829 *new_info_ptr = cur_ptr;
17830 return first_die;
17831 }
17832
17833 if (!first_die)
17834 first_die = die;
17835 else
17836 last_sibling->sibling = die;
17837
17838 last_sibling = die;
17839 }
17840 }
17841
17842 /* Read a die, all of its descendents, and all of its siblings; set
17843 all of the fields of all of the dies correctly. Arguments are as
17844 in read_die_and_children.
17845 This the main entry point for reading a DIE and all its children. */
17846
17847 static struct die_info *
17848 read_die_and_siblings (const struct die_reader_specs *reader,
17849 const gdb_byte *info_ptr,
17850 const gdb_byte **new_info_ptr,
17851 struct die_info *parent)
17852 {
17853 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17854 new_info_ptr, parent);
17855
17856 if (dwarf_die_debug)
17857 {
17858 fprintf_unfiltered (gdb_stdlog,
17859 "Read die from %s@0x%x of %s:\n",
17860 get_section_name (reader->die_section),
17861 (unsigned) (info_ptr - reader->die_section->buffer),
17862 bfd_get_filename (reader->abfd));
17863 dump_die (die, dwarf_die_debug);
17864 }
17865
17866 return die;
17867 }
17868
17869 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17870 attributes.
17871 The caller is responsible for filling in the extra attributes
17872 and updating (*DIEP)->num_attrs.
17873 Set DIEP to point to a newly allocated die with its information,
17874 except for its child, sibling, and parent fields.
17875 Set HAS_CHILDREN to tell whether the die has children or not. */
17876
17877 static const gdb_byte *
17878 read_full_die_1 (const struct die_reader_specs *reader,
17879 struct die_info **diep, const gdb_byte *info_ptr,
17880 int *has_children, int num_extra_attrs)
17881 {
17882 unsigned int abbrev_number, bytes_read, i;
17883 struct abbrev_info *abbrev;
17884 struct die_info *die;
17885 struct dwarf2_cu *cu = reader->cu;
17886 bfd *abfd = reader->abfd;
17887
17888 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17889 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17890 info_ptr += bytes_read;
17891 if (!abbrev_number)
17892 {
17893 *diep = NULL;
17894 *has_children = 0;
17895 return info_ptr;
17896 }
17897
17898 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17899 if (!abbrev)
17900 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17901 abbrev_number,
17902 bfd_get_filename (abfd));
17903
17904 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17905 die->sect_off = sect_off;
17906 die->tag = abbrev->tag;
17907 die->abbrev = abbrev_number;
17908
17909 /* Make the result usable.
17910 The caller needs to update num_attrs after adding the extra
17911 attributes. */
17912 die->num_attrs = abbrev->num_attrs;
17913
17914 for (i = 0; i < abbrev->num_attrs; ++i)
17915 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17916 info_ptr);
17917
17918 *diep = die;
17919 *has_children = abbrev->has_children;
17920 return info_ptr;
17921 }
17922
17923 /* Read a die and all its attributes.
17924 Set DIEP to point to a newly allocated die with its information,
17925 except for its child, sibling, and parent fields.
17926 Set HAS_CHILDREN to tell whether the die has children or not. */
17927
17928 static const gdb_byte *
17929 read_full_die (const struct die_reader_specs *reader,
17930 struct die_info **diep, const gdb_byte *info_ptr,
17931 int *has_children)
17932 {
17933 const gdb_byte *result;
17934
17935 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17936
17937 if (dwarf_die_debug)
17938 {
17939 fprintf_unfiltered (gdb_stdlog,
17940 "Read die from %s@0x%x of %s:\n",
17941 get_section_name (reader->die_section),
17942 (unsigned) (info_ptr - reader->die_section->buffer),
17943 bfd_get_filename (reader->abfd));
17944 dump_die (*diep, dwarf_die_debug);
17945 }
17946
17947 return result;
17948 }
17949 \f
17950 /* Abbreviation tables.
17951
17952 In DWARF version 2, the description of the debugging information is
17953 stored in a separate .debug_abbrev section. Before we read any
17954 dies from a section we read in all abbreviations and install them
17955 in a hash table. */
17956
17957 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17958
17959 struct abbrev_info *
17960 abbrev_table::alloc_abbrev ()
17961 {
17962 struct abbrev_info *abbrev;
17963
17964 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
17965 memset (abbrev, 0, sizeof (struct abbrev_info));
17966
17967 return abbrev;
17968 }
17969
17970 /* Add an abbreviation to the table. */
17971
17972 void
17973 abbrev_table::add_abbrev (unsigned int abbrev_number,
17974 struct abbrev_info *abbrev)
17975 {
17976 unsigned int hash_number;
17977
17978 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17979 abbrev->next = m_abbrevs[hash_number];
17980 m_abbrevs[hash_number] = abbrev;
17981 }
17982
17983 /* Look up an abbrev in the table.
17984 Returns NULL if the abbrev is not found. */
17985
17986 struct abbrev_info *
17987 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
17988 {
17989 unsigned int hash_number;
17990 struct abbrev_info *abbrev;
17991
17992 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17993 abbrev = m_abbrevs[hash_number];
17994
17995 while (abbrev)
17996 {
17997 if (abbrev->number == abbrev_number)
17998 return abbrev;
17999 abbrev = abbrev->next;
18000 }
18001 return NULL;
18002 }
18003
18004 /* Read in an abbrev table. */
18005
18006 static abbrev_table_up
18007 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18008 struct dwarf2_section_info *section,
18009 sect_offset sect_off)
18010 {
18011 struct objfile *objfile = dwarf2_per_objfile->objfile;
18012 bfd *abfd = get_section_bfd_owner (section);
18013 const gdb_byte *abbrev_ptr;
18014 struct abbrev_info *cur_abbrev;
18015 unsigned int abbrev_number, bytes_read, abbrev_name;
18016 unsigned int abbrev_form;
18017 struct attr_abbrev *cur_attrs;
18018 unsigned int allocated_attrs;
18019
18020 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18021
18022 dwarf2_read_section (objfile, section);
18023 abbrev_ptr = section->buffer + to_underlying (sect_off);
18024 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18025 abbrev_ptr += bytes_read;
18026
18027 allocated_attrs = ATTR_ALLOC_CHUNK;
18028 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18029
18030 /* Loop until we reach an abbrev number of 0. */
18031 while (abbrev_number)
18032 {
18033 cur_abbrev = abbrev_table->alloc_abbrev ();
18034
18035 /* read in abbrev header */
18036 cur_abbrev->number = abbrev_number;
18037 cur_abbrev->tag
18038 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18039 abbrev_ptr += bytes_read;
18040 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18041 abbrev_ptr += 1;
18042
18043 /* now read in declarations */
18044 for (;;)
18045 {
18046 LONGEST implicit_const;
18047
18048 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18049 abbrev_ptr += bytes_read;
18050 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18051 abbrev_ptr += bytes_read;
18052 if (abbrev_form == DW_FORM_implicit_const)
18053 {
18054 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18055 &bytes_read);
18056 abbrev_ptr += bytes_read;
18057 }
18058 else
18059 {
18060 /* Initialize it due to a false compiler warning. */
18061 implicit_const = -1;
18062 }
18063
18064 if (abbrev_name == 0)
18065 break;
18066
18067 if (cur_abbrev->num_attrs == allocated_attrs)
18068 {
18069 allocated_attrs += ATTR_ALLOC_CHUNK;
18070 cur_attrs
18071 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18072 }
18073
18074 cur_attrs[cur_abbrev->num_attrs].name
18075 = (enum dwarf_attribute) abbrev_name;
18076 cur_attrs[cur_abbrev->num_attrs].form
18077 = (enum dwarf_form) abbrev_form;
18078 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18079 ++cur_abbrev->num_attrs;
18080 }
18081
18082 cur_abbrev->attrs =
18083 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18084 cur_abbrev->num_attrs);
18085 memcpy (cur_abbrev->attrs, cur_attrs,
18086 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18087
18088 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18089
18090 /* Get next abbreviation.
18091 Under Irix6 the abbreviations for a compilation unit are not
18092 always properly terminated with an abbrev number of 0.
18093 Exit loop if we encounter an abbreviation which we have
18094 already read (which means we are about to read the abbreviations
18095 for the next compile unit) or if the end of the abbreviation
18096 table is reached. */
18097 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18098 break;
18099 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18100 abbrev_ptr += bytes_read;
18101 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18102 break;
18103 }
18104
18105 xfree (cur_attrs);
18106 return abbrev_table;
18107 }
18108
18109 /* Returns nonzero if TAG represents a type that we might generate a partial
18110 symbol for. */
18111
18112 static int
18113 is_type_tag_for_partial (int tag)
18114 {
18115 switch (tag)
18116 {
18117 #if 0
18118 /* Some types that would be reasonable to generate partial symbols for,
18119 that we don't at present. */
18120 case DW_TAG_array_type:
18121 case DW_TAG_file_type:
18122 case DW_TAG_ptr_to_member_type:
18123 case DW_TAG_set_type:
18124 case DW_TAG_string_type:
18125 case DW_TAG_subroutine_type:
18126 #endif
18127 case DW_TAG_base_type:
18128 case DW_TAG_class_type:
18129 case DW_TAG_interface_type:
18130 case DW_TAG_enumeration_type:
18131 case DW_TAG_structure_type:
18132 case DW_TAG_subrange_type:
18133 case DW_TAG_typedef:
18134 case DW_TAG_union_type:
18135 return 1;
18136 default:
18137 return 0;
18138 }
18139 }
18140
18141 /* Load all DIEs that are interesting for partial symbols into memory. */
18142
18143 static struct partial_die_info *
18144 load_partial_dies (const struct die_reader_specs *reader,
18145 const gdb_byte *info_ptr, int building_psymtab)
18146 {
18147 struct dwarf2_cu *cu = reader->cu;
18148 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18149 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18150 unsigned int bytes_read;
18151 unsigned int load_all = 0;
18152 int nesting_level = 1;
18153
18154 parent_die = NULL;
18155 last_die = NULL;
18156
18157 gdb_assert (cu->per_cu != NULL);
18158 if (cu->per_cu->load_all_dies)
18159 load_all = 1;
18160
18161 cu->partial_dies
18162 = htab_create_alloc_ex (cu->header.length / 12,
18163 partial_die_hash,
18164 partial_die_eq,
18165 NULL,
18166 &cu->comp_unit_obstack,
18167 hashtab_obstack_allocate,
18168 dummy_obstack_deallocate);
18169
18170 while (1)
18171 {
18172 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18173
18174 /* A NULL abbrev means the end of a series of children. */
18175 if (abbrev == NULL)
18176 {
18177 if (--nesting_level == 0)
18178 return first_die;
18179
18180 info_ptr += bytes_read;
18181 last_die = parent_die;
18182 parent_die = parent_die->die_parent;
18183 continue;
18184 }
18185
18186 /* Check for template arguments. We never save these; if
18187 they're seen, we just mark the parent, and go on our way. */
18188 if (parent_die != NULL
18189 && cu->language == language_cplus
18190 && (abbrev->tag == DW_TAG_template_type_param
18191 || abbrev->tag == DW_TAG_template_value_param))
18192 {
18193 parent_die->has_template_arguments = 1;
18194
18195 if (!load_all)
18196 {
18197 /* We don't need a partial DIE for the template argument. */
18198 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18199 continue;
18200 }
18201 }
18202
18203 /* We only recurse into c++ subprograms looking for template arguments.
18204 Skip their other children. */
18205 if (!load_all
18206 && cu->language == language_cplus
18207 && parent_die != NULL
18208 && parent_die->tag == DW_TAG_subprogram)
18209 {
18210 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18211 continue;
18212 }
18213
18214 /* Check whether this DIE is interesting enough to save. Normally
18215 we would not be interested in members here, but there may be
18216 later variables referencing them via DW_AT_specification (for
18217 static members). */
18218 if (!load_all
18219 && !is_type_tag_for_partial (abbrev->tag)
18220 && abbrev->tag != DW_TAG_constant
18221 && abbrev->tag != DW_TAG_enumerator
18222 && abbrev->tag != DW_TAG_subprogram
18223 && abbrev->tag != DW_TAG_inlined_subroutine
18224 && abbrev->tag != DW_TAG_lexical_block
18225 && abbrev->tag != DW_TAG_variable
18226 && abbrev->tag != DW_TAG_namespace
18227 && abbrev->tag != DW_TAG_module
18228 && abbrev->tag != DW_TAG_member
18229 && abbrev->tag != DW_TAG_imported_unit
18230 && abbrev->tag != DW_TAG_imported_declaration)
18231 {
18232 /* Otherwise we skip to the next sibling, if any. */
18233 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18234 continue;
18235 }
18236
18237 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18238 abbrev);
18239
18240 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18241
18242 /* This two-pass algorithm for processing partial symbols has a
18243 high cost in cache pressure. Thus, handle some simple cases
18244 here which cover the majority of C partial symbols. DIEs
18245 which neither have specification tags in them, nor could have
18246 specification tags elsewhere pointing at them, can simply be
18247 processed and discarded.
18248
18249 This segment is also optional; scan_partial_symbols and
18250 add_partial_symbol will handle these DIEs if we chain
18251 them in normally. When compilers which do not emit large
18252 quantities of duplicate debug information are more common,
18253 this code can probably be removed. */
18254
18255 /* Any complete simple types at the top level (pretty much all
18256 of them, for a language without namespaces), can be processed
18257 directly. */
18258 if (parent_die == NULL
18259 && pdi.has_specification == 0
18260 && pdi.is_declaration == 0
18261 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18262 || pdi.tag == DW_TAG_base_type
18263 || pdi.tag == DW_TAG_subrange_type))
18264 {
18265 if (building_psymtab && pdi.name != NULL)
18266 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18267 VAR_DOMAIN, LOC_TYPEDEF, -1,
18268 &objfile->static_psymbols,
18269 0, cu->language, objfile);
18270 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18271 continue;
18272 }
18273
18274 /* The exception for DW_TAG_typedef with has_children above is
18275 a workaround of GCC PR debug/47510. In the case of this complaint
18276 type_name_or_error will error on such types later.
18277
18278 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18279 it could not find the child DIEs referenced later, this is checked
18280 above. In correct DWARF DW_TAG_typedef should have no children. */
18281
18282 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18283 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18284 "- DIE at %s [in module %s]"),
18285 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18286
18287 /* If we're at the second level, and we're an enumerator, and
18288 our parent has no specification (meaning possibly lives in a
18289 namespace elsewhere), then we can add the partial symbol now
18290 instead of queueing it. */
18291 if (pdi.tag == DW_TAG_enumerator
18292 && parent_die != NULL
18293 && parent_die->die_parent == NULL
18294 && parent_die->tag == DW_TAG_enumeration_type
18295 && parent_die->has_specification == 0)
18296 {
18297 if (pdi.name == NULL)
18298 complaint (_("malformed enumerator DIE ignored"));
18299 else if (building_psymtab)
18300 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18301 VAR_DOMAIN, LOC_CONST, -1,
18302 cu->language == language_cplus
18303 ? &objfile->global_psymbols
18304 : &objfile->static_psymbols,
18305 0, cu->language, objfile);
18306
18307 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18308 continue;
18309 }
18310
18311 struct partial_die_info *part_die
18312 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18313
18314 /* We'll save this DIE so link it in. */
18315 part_die->die_parent = parent_die;
18316 part_die->die_sibling = NULL;
18317 part_die->die_child = NULL;
18318
18319 if (last_die && last_die == parent_die)
18320 last_die->die_child = part_die;
18321 else if (last_die)
18322 last_die->die_sibling = part_die;
18323
18324 last_die = part_die;
18325
18326 if (first_die == NULL)
18327 first_die = part_die;
18328
18329 /* Maybe add the DIE to the hash table. Not all DIEs that we
18330 find interesting need to be in the hash table, because we
18331 also have the parent/sibling/child chains; only those that we
18332 might refer to by offset later during partial symbol reading.
18333
18334 For now this means things that might have be the target of a
18335 DW_AT_specification, DW_AT_abstract_origin, or
18336 DW_AT_extension. DW_AT_extension will refer only to
18337 namespaces; DW_AT_abstract_origin refers to functions (and
18338 many things under the function DIE, but we do not recurse
18339 into function DIEs during partial symbol reading) and
18340 possibly variables as well; DW_AT_specification refers to
18341 declarations. Declarations ought to have the DW_AT_declaration
18342 flag. It happens that GCC forgets to put it in sometimes, but
18343 only for functions, not for types.
18344
18345 Adding more things than necessary to the hash table is harmless
18346 except for the performance cost. Adding too few will result in
18347 wasted time in find_partial_die, when we reread the compilation
18348 unit with load_all_dies set. */
18349
18350 if (load_all
18351 || abbrev->tag == DW_TAG_constant
18352 || abbrev->tag == DW_TAG_subprogram
18353 || abbrev->tag == DW_TAG_variable
18354 || abbrev->tag == DW_TAG_namespace
18355 || part_die->is_declaration)
18356 {
18357 void **slot;
18358
18359 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18360 to_underlying (part_die->sect_off),
18361 INSERT);
18362 *slot = part_die;
18363 }
18364
18365 /* For some DIEs we want to follow their children (if any). For C
18366 we have no reason to follow the children of structures; for other
18367 languages we have to, so that we can get at method physnames
18368 to infer fully qualified class names, for DW_AT_specification,
18369 and for C++ template arguments. For C++, we also look one level
18370 inside functions to find template arguments (if the name of the
18371 function does not already contain the template arguments).
18372
18373 For Ada, we need to scan the children of subprograms and lexical
18374 blocks as well because Ada allows the definition of nested
18375 entities that could be interesting for the debugger, such as
18376 nested subprograms for instance. */
18377 if (last_die->has_children
18378 && (load_all
18379 || last_die->tag == DW_TAG_namespace
18380 || last_die->tag == DW_TAG_module
18381 || last_die->tag == DW_TAG_enumeration_type
18382 || (cu->language == language_cplus
18383 && last_die->tag == DW_TAG_subprogram
18384 && (last_die->name == NULL
18385 || strchr (last_die->name, '<') == NULL))
18386 || (cu->language != language_c
18387 && (last_die->tag == DW_TAG_class_type
18388 || last_die->tag == DW_TAG_interface_type
18389 || last_die->tag == DW_TAG_structure_type
18390 || last_die->tag == DW_TAG_union_type))
18391 || (cu->language == language_ada
18392 && (last_die->tag == DW_TAG_subprogram
18393 || last_die->tag == DW_TAG_lexical_block))))
18394 {
18395 nesting_level++;
18396 parent_die = last_die;
18397 continue;
18398 }
18399
18400 /* Otherwise we skip to the next sibling, if any. */
18401 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18402
18403 /* Back to the top, do it again. */
18404 }
18405 }
18406
18407 partial_die_info::partial_die_info (sect_offset sect_off_,
18408 struct abbrev_info *abbrev)
18409 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18410 {
18411 }
18412
18413 /* Read a minimal amount of information into the minimal die structure.
18414 INFO_PTR should point just after the initial uleb128 of a DIE. */
18415
18416 const gdb_byte *
18417 partial_die_info::read (const struct die_reader_specs *reader,
18418 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18419 {
18420 struct dwarf2_cu *cu = reader->cu;
18421 struct dwarf2_per_objfile *dwarf2_per_objfile
18422 = cu->per_cu->dwarf2_per_objfile;
18423 unsigned int i;
18424 int has_low_pc_attr = 0;
18425 int has_high_pc_attr = 0;
18426 int high_pc_relative = 0;
18427
18428 for (i = 0; i < abbrev.num_attrs; ++i)
18429 {
18430 struct attribute attr;
18431
18432 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18433
18434 /* Store the data if it is of an attribute we want to keep in a
18435 partial symbol table. */
18436 switch (attr.name)
18437 {
18438 case DW_AT_name:
18439 switch (tag)
18440 {
18441 case DW_TAG_compile_unit:
18442 case DW_TAG_partial_unit:
18443 case DW_TAG_type_unit:
18444 /* Compilation units have a DW_AT_name that is a filename, not
18445 a source language identifier. */
18446 case DW_TAG_enumeration_type:
18447 case DW_TAG_enumerator:
18448 /* These tags always have simple identifiers already; no need
18449 to canonicalize them. */
18450 name = DW_STRING (&attr);
18451 break;
18452 default:
18453 {
18454 struct objfile *objfile = dwarf2_per_objfile->objfile;
18455
18456 name
18457 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18458 &objfile->per_bfd->storage_obstack);
18459 }
18460 break;
18461 }
18462 break;
18463 case DW_AT_linkage_name:
18464 case DW_AT_MIPS_linkage_name:
18465 /* Note that both forms of linkage name might appear. We
18466 assume they will be the same, and we only store the last
18467 one we see. */
18468 if (cu->language == language_ada)
18469 name = DW_STRING (&attr);
18470 linkage_name = DW_STRING (&attr);
18471 break;
18472 case DW_AT_low_pc:
18473 has_low_pc_attr = 1;
18474 lowpc = attr_value_as_address (&attr);
18475 break;
18476 case DW_AT_high_pc:
18477 has_high_pc_attr = 1;
18478 highpc = attr_value_as_address (&attr);
18479 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18480 high_pc_relative = 1;
18481 break;
18482 case DW_AT_location:
18483 /* Support the .debug_loc offsets. */
18484 if (attr_form_is_block (&attr))
18485 {
18486 d.locdesc = DW_BLOCK (&attr);
18487 }
18488 else if (attr_form_is_section_offset (&attr))
18489 {
18490 dwarf2_complex_location_expr_complaint ();
18491 }
18492 else
18493 {
18494 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18495 "partial symbol information");
18496 }
18497 break;
18498 case DW_AT_external:
18499 is_external = DW_UNSND (&attr);
18500 break;
18501 case DW_AT_declaration:
18502 is_declaration = DW_UNSND (&attr);
18503 break;
18504 case DW_AT_type:
18505 has_type = 1;
18506 break;
18507 case DW_AT_abstract_origin:
18508 case DW_AT_specification:
18509 case DW_AT_extension:
18510 has_specification = 1;
18511 spec_offset = dwarf2_get_ref_die_offset (&attr);
18512 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18513 || cu->per_cu->is_dwz);
18514 break;
18515 case DW_AT_sibling:
18516 /* Ignore absolute siblings, they might point outside of
18517 the current compile unit. */
18518 if (attr.form == DW_FORM_ref_addr)
18519 complaint (_("ignoring absolute DW_AT_sibling"));
18520 else
18521 {
18522 const gdb_byte *buffer = reader->buffer;
18523 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18524 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18525
18526 if (sibling_ptr < info_ptr)
18527 complaint (_("DW_AT_sibling points backwards"));
18528 else if (sibling_ptr > reader->buffer_end)
18529 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18530 else
18531 sibling = sibling_ptr;
18532 }
18533 break;
18534 case DW_AT_byte_size:
18535 has_byte_size = 1;
18536 break;
18537 case DW_AT_const_value:
18538 has_const_value = 1;
18539 break;
18540 case DW_AT_calling_convention:
18541 /* DWARF doesn't provide a way to identify a program's source-level
18542 entry point. DW_AT_calling_convention attributes are only meant
18543 to describe functions' calling conventions.
18544
18545 However, because it's a necessary piece of information in
18546 Fortran, and before DWARF 4 DW_CC_program was the only
18547 piece of debugging information whose definition refers to
18548 a 'main program' at all, several compilers marked Fortran
18549 main programs with DW_CC_program --- even when those
18550 functions use the standard calling conventions.
18551
18552 Although DWARF now specifies a way to provide this
18553 information, we support this practice for backward
18554 compatibility. */
18555 if (DW_UNSND (&attr) == DW_CC_program
18556 && cu->language == language_fortran)
18557 main_subprogram = 1;
18558 break;
18559 case DW_AT_inline:
18560 if (DW_UNSND (&attr) == DW_INL_inlined
18561 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18562 may_be_inlined = 1;
18563 break;
18564
18565 case DW_AT_import:
18566 if (tag == DW_TAG_imported_unit)
18567 {
18568 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18569 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18570 || cu->per_cu->is_dwz);
18571 }
18572 break;
18573
18574 case DW_AT_main_subprogram:
18575 main_subprogram = DW_UNSND (&attr);
18576 break;
18577
18578 default:
18579 break;
18580 }
18581 }
18582
18583 if (high_pc_relative)
18584 highpc += lowpc;
18585
18586 if (has_low_pc_attr && has_high_pc_attr)
18587 {
18588 /* When using the GNU linker, .gnu.linkonce. sections are used to
18589 eliminate duplicate copies of functions and vtables and such.
18590 The linker will arbitrarily choose one and discard the others.
18591 The AT_*_pc values for such functions refer to local labels in
18592 these sections. If the section from that file was discarded, the
18593 labels are not in the output, so the relocs get a value of 0.
18594 If this is a discarded function, mark the pc bounds as invalid,
18595 so that GDB will ignore it. */
18596 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18597 {
18598 struct objfile *objfile = dwarf2_per_objfile->objfile;
18599 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18600
18601 complaint (_("DW_AT_low_pc %s is zero "
18602 "for DIE at %s [in module %s]"),
18603 paddress (gdbarch, lowpc),
18604 sect_offset_str (sect_off),
18605 objfile_name (objfile));
18606 }
18607 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18608 else if (lowpc >= highpc)
18609 {
18610 struct objfile *objfile = dwarf2_per_objfile->objfile;
18611 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18612
18613 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18614 "for DIE at %s [in module %s]"),
18615 paddress (gdbarch, lowpc),
18616 paddress (gdbarch, highpc),
18617 sect_offset_str (sect_off),
18618 objfile_name (objfile));
18619 }
18620 else
18621 has_pc_info = 1;
18622 }
18623
18624 return info_ptr;
18625 }
18626
18627 /* Find a cached partial DIE at OFFSET in CU. */
18628
18629 struct partial_die_info *
18630 dwarf2_cu::find_partial_die (sect_offset sect_off)
18631 {
18632 struct partial_die_info *lookup_die = NULL;
18633 struct partial_die_info part_die (sect_off);
18634
18635 lookup_die = ((struct partial_die_info *)
18636 htab_find_with_hash (partial_dies, &part_die,
18637 to_underlying (sect_off)));
18638
18639 return lookup_die;
18640 }
18641
18642 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18643 except in the case of .debug_types DIEs which do not reference
18644 outside their CU (they do however referencing other types via
18645 DW_FORM_ref_sig8). */
18646
18647 static struct partial_die_info *
18648 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18649 {
18650 struct dwarf2_per_objfile *dwarf2_per_objfile
18651 = cu->per_cu->dwarf2_per_objfile;
18652 struct objfile *objfile = dwarf2_per_objfile->objfile;
18653 struct dwarf2_per_cu_data *per_cu = NULL;
18654 struct partial_die_info *pd = NULL;
18655
18656 if (offset_in_dwz == cu->per_cu->is_dwz
18657 && offset_in_cu_p (&cu->header, sect_off))
18658 {
18659 pd = cu->find_partial_die (sect_off);
18660 if (pd != NULL)
18661 return pd;
18662 /* We missed recording what we needed.
18663 Load all dies and try again. */
18664 per_cu = cu->per_cu;
18665 }
18666 else
18667 {
18668 /* TUs don't reference other CUs/TUs (except via type signatures). */
18669 if (cu->per_cu->is_debug_types)
18670 {
18671 error (_("Dwarf Error: Type Unit at offset %s contains"
18672 " external reference to offset %s [in module %s].\n"),
18673 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18674 bfd_get_filename (objfile->obfd));
18675 }
18676 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18677 dwarf2_per_objfile);
18678
18679 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18680 load_partial_comp_unit (per_cu);
18681
18682 per_cu->cu->last_used = 0;
18683 pd = per_cu->cu->find_partial_die (sect_off);
18684 }
18685
18686 /* If we didn't find it, and not all dies have been loaded,
18687 load them all and try again. */
18688
18689 if (pd == NULL && per_cu->load_all_dies == 0)
18690 {
18691 per_cu->load_all_dies = 1;
18692
18693 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18694 THIS_CU->cu may already be in use. So we can't just free it and
18695 replace its DIEs with the ones we read in. Instead, we leave those
18696 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18697 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18698 set. */
18699 load_partial_comp_unit (per_cu);
18700
18701 pd = per_cu->cu->find_partial_die (sect_off);
18702 }
18703
18704 if (pd == NULL)
18705 internal_error (__FILE__, __LINE__,
18706 _("could not find partial DIE %s "
18707 "in cache [from module %s]\n"),
18708 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18709 return pd;
18710 }
18711
18712 /* See if we can figure out if the class lives in a namespace. We do
18713 this by looking for a member function; its demangled name will
18714 contain namespace info, if there is any. */
18715
18716 static void
18717 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18718 struct dwarf2_cu *cu)
18719 {
18720 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18721 what template types look like, because the demangler
18722 frequently doesn't give the same name as the debug info. We
18723 could fix this by only using the demangled name to get the
18724 prefix (but see comment in read_structure_type). */
18725
18726 struct partial_die_info *real_pdi;
18727 struct partial_die_info *child_pdi;
18728
18729 /* If this DIE (this DIE's specification, if any) has a parent, then
18730 we should not do this. We'll prepend the parent's fully qualified
18731 name when we create the partial symbol. */
18732
18733 real_pdi = struct_pdi;
18734 while (real_pdi->has_specification)
18735 real_pdi = find_partial_die (real_pdi->spec_offset,
18736 real_pdi->spec_is_dwz, cu);
18737
18738 if (real_pdi->die_parent != NULL)
18739 return;
18740
18741 for (child_pdi = struct_pdi->die_child;
18742 child_pdi != NULL;
18743 child_pdi = child_pdi->die_sibling)
18744 {
18745 if (child_pdi->tag == DW_TAG_subprogram
18746 && child_pdi->linkage_name != NULL)
18747 {
18748 char *actual_class_name
18749 = language_class_name_from_physname (cu->language_defn,
18750 child_pdi->linkage_name);
18751 if (actual_class_name != NULL)
18752 {
18753 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18754 struct_pdi->name
18755 = ((const char *)
18756 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18757 actual_class_name,
18758 strlen (actual_class_name)));
18759 xfree (actual_class_name);
18760 }
18761 break;
18762 }
18763 }
18764 }
18765
18766 void
18767 partial_die_info::fixup (struct dwarf2_cu *cu)
18768 {
18769 /* Once we've fixed up a die, there's no point in doing so again.
18770 This also avoids a memory leak if we were to call
18771 guess_partial_die_structure_name multiple times. */
18772 if (fixup_called)
18773 return;
18774
18775 /* If we found a reference attribute and the DIE has no name, try
18776 to find a name in the referred to DIE. */
18777
18778 if (name == NULL && has_specification)
18779 {
18780 struct partial_die_info *spec_die;
18781
18782 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
18783
18784 spec_die->fixup (cu);
18785
18786 if (spec_die->name)
18787 {
18788 name = spec_die->name;
18789
18790 /* Copy DW_AT_external attribute if it is set. */
18791 if (spec_die->is_external)
18792 is_external = spec_die->is_external;
18793 }
18794 }
18795
18796 /* Set default names for some unnamed DIEs. */
18797
18798 if (name == NULL && tag == DW_TAG_namespace)
18799 name = CP_ANONYMOUS_NAMESPACE_STR;
18800
18801 /* If there is no parent die to provide a namespace, and there are
18802 children, see if we can determine the namespace from their linkage
18803 name. */
18804 if (cu->language == language_cplus
18805 && !VEC_empty (dwarf2_section_info_def,
18806 cu->per_cu->dwarf2_per_objfile->types)
18807 && die_parent == NULL
18808 && has_children
18809 && (tag == DW_TAG_class_type
18810 || tag == DW_TAG_structure_type
18811 || tag == DW_TAG_union_type))
18812 guess_partial_die_structure_name (this, cu);
18813
18814 /* GCC might emit a nameless struct or union that has a linkage
18815 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18816 if (name == NULL
18817 && (tag == DW_TAG_class_type
18818 || tag == DW_TAG_interface_type
18819 || tag == DW_TAG_structure_type
18820 || tag == DW_TAG_union_type)
18821 && linkage_name != NULL)
18822 {
18823 char *demangled;
18824
18825 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
18826 if (demangled)
18827 {
18828 const char *base;
18829
18830 /* Strip any leading namespaces/classes, keep only the base name.
18831 DW_AT_name for named DIEs does not contain the prefixes. */
18832 base = strrchr (demangled, ':');
18833 if (base && base > demangled && base[-1] == ':')
18834 base++;
18835 else
18836 base = demangled;
18837
18838 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18839 name
18840 = ((const char *)
18841 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18842 base, strlen (base)));
18843 xfree (demangled);
18844 }
18845 }
18846
18847 fixup_called = 1;
18848 }
18849
18850 /* Read an attribute value described by an attribute form. */
18851
18852 static const gdb_byte *
18853 read_attribute_value (const struct die_reader_specs *reader,
18854 struct attribute *attr, unsigned form,
18855 LONGEST implicit_const, const gdb_byte *info_ptr)
18856 {
18857 struct dwarf2_cu *cu = reader->cu;
18858 struct dwarf2_per_objfile *dwarf2_per_objfile
18859 = cu->per_cu->dwarf2_per_objfile;
18860 struct objfile *objfile = dwarf2_per_objfile->objfile;
18861 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18862 bfd *abfd = reader->abfd;
18863 struct comp_unit_head *cu_header = &cu->header;
18864 unsigned int bytes_read;
18865 struct dwarf_block *blk;
18866
18867 attr->form = (enum dwarf_form) form;
18868 switch (form)
18869 {
18870 case DW_FORM_ref_addr:
18871 if (cu->header.version == 2)
18872 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18873 else
18874 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18875 &cu->header, &bytes_read);
18876 info_ptr += bytes_read;
18877 break;
18878 case DW_FORM_GNU_ref_alt:
18879 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18880 info_ptr += bytes_read;
18881 break;
18882 case DW_FORM_addr:
18883 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18884 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18885 info_ptr += bytes_read;
18886 break;
18887 case DW_FORM_block2:
18888 blk = dwarf_alloc_block (cu);
18889 blk->size = read_2_bytes (abfd, info_ptr);
18890 info_ptr += 2;
18891 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18892 info_ptr += blk->size;
18893 DW_BLOCK (attr) = blk;
18894 break;
18895 case DW_FORM_block4:
18896 blk = dwarf_alloc_block (cu);
18897 blk->size = read_4_bytes (abfd, info_ptr);
18898 info_ptr += 4;
18899 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18900 info_ptr += blk->size;
18901 DW_BLOCK (attr) = blk;
18902 break;
18903 case DW_FORM_data2:
18904 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18905 info_ptr += 2;
18906 break;
18907 case DW_FORM_data4:
18908 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18909 info_ptr += 4;
18910 break;
18911 case DW_FORM_data8:
18912 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18913 info_ptr += 8;
18914 break;
18915 case DW_FORM_data16:
18916 blk = dwarf_alloc_block (cu);
18917 blk->size = 16;
18918 blk->data = read_n_bytes (abfd, info_ptr, 16);
18919 info_ptr += 16;
18920 DW_BLOCK (attr) = blk;
18921 break;
18922 case DW_FORM_sec_offset:
18923 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18924 info_ptr += bytes_read;
18925 break;
18926 case DW_FORM_string:
18927 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18928 DW_STRING_IS_CANONICAL (attr) = 0;
18929 info_ptr += bytes_read;
18930 break;
18931 case DW_FORM_strp:
18932 if (!cu->per_cu->is_dwz)
18933 {
18934 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18935 abfd, info_ptr, cu_header,
18936 &bytes_read);
18937 DW_STRING_IS_CANONICAL (attr) = 0;
18938 info_ptr += bytes_read;
18939 break;
18940 }
18941 /* FALLTHROUGH */
18942 case DW_FORM_line_strp:
18943 if (!cu->per_cu->is_dwz)
18944 {
18945 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18946 abfd, info_ptr,
18947 cu_header, &bytes_read);
18948 DW_STRING_IS_CANONICAL (attr) = 0;
18949 info_ptr += bytes_read;
18950 break;
18951 }
18952 /* FALLTHROUGH */
18953 case DW_FORM_GNU_strp_alt:
18954 {
18955 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18956 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18957 &bytes_read);
18958
18959 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18960 dwz, str_offset);
18961 DW_STRING_IS_CANONICAL (attr) = 0;
18962 info_ptr += bytes_read;
18963 }
18964 break;
18965 case DW_FORM_exprloc:
18966 case DW_FORM_block:
18967 blk = dwarf_alloc_block (cu);
18968 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18969 info_ptr += bytes_read;
18970 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18971 info_ptr += blk->size;
18972 DW_BLOCK (attr) = blk;
18973 break;
18974 case DW_FORM_block1:
18975 blk = dwarf_alloc_block (cu);
18976 blk->size = read_1_byte (abfd, info_ptr);
18977 info_ptr += 1;
18978 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18979 info_ptr += blk->size;
18980 DW_BLOCK (attr) = blk;
18981 break;
18982 case DW_FORM_data1:
18983 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18984 info_ptr += 1;
18985 break;
18986 case DW_FORM_flag:
18987 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18988 info_ptr += 1;
18989 break;
18990 case DW_FORM_flag_present:
18991 DW_UNSND (attr) = 1;
18992 break;
18993 case DW_FORM_sdata:
18994 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18995 info_ptr += bytes_read;
18996 break;
18997 case DW_FORM_udata:
18998 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18999 info_ptr += bytes_read;
19000 break;
19001 case DW_FORM_ref1:
19002 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19003 + read_1_byte (abfd, info_ptr));
19004 info_ptr += 1;
19005 break;
19006 case DW_FORM_ref2:
19007 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19008 + read_2_bytes (abfd, info_ptr));
19009 info_ptr += 2;
19010 break;
19011 case DW_FORM_ref4:
19012 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19013 + read_4_bytes (abfd, info_ptr));
19014 info_ptr += 4;
19015 break;
19016 case DW_FORM_ref8:
19017 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19018 + read_8_bytes (abfd, info_ptr));
19019 info_ptr += 8;
19020 break;
19021 case DW_FORM_ref_sig8:
19022 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19023 info_ptr += 8;
19024 break;
19025 case DW_FORM_ref_udata:
19026 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19027 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19028 info_ptr += bytes_read;
19029 break;
19030 case DW_FORM_indirect:
19031 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19032 info_ptr += bytes_read;
19033 if (form == DW_FORM_implicit_const)
19034 {
19035 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19036 info_ptr += bytes_read;
19037 }
19038 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19039 info_ptr);
19040 break;
19041 case DW_FORM_implicit_const:
19042 DW_SND (attr) = implicit_const;
19043 break;
19044 case DW_FORM_GNU_addr_index:
19045 if (reader->dwo_file == NULL)
19046 {
19047 /* For now flag a hard error.
19048 Later we can turn this into a complaint. */
19049 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19050 dwarf_form_name (form),
19051 bfd_get_filename (abfd));
19052 }
19053 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19054 info_ptr += bytes_read;
19055 break;
19056 case DW_FORM_GNU_str_index:
19057 if (reader->dwo_file == NULL)
19058 {
19059 /* For now flag a hard error.
19060 Later we can turn this into a complaint if warranted. */
19061 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19062 dwarf_form_name (form),
19063 bfd_get_filename (abfd));
19064 }
19065 {
19066 ULONGEST str_index =
19067 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19068
19069 DW_STRING (attr) = read_str_index (reader, str_index);
19070 DW_STRING_IS_CANONICAL (attr) = 0;
19071 info_ptr += bytes_read;
19072 }
19073 break;
19074 default:
19075 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19076 dwarf_form_name (form),
19077 bfd_get_filename (abfd));
19078 }
19079
19080 /* Super hack. */
19081 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19082 attr->form = DW_FORM_GNU_ref_alt;
19083
19084 /* We have seen instances where the compiler tried to emit a byte
19085 size attribute of -1 which ended up being encoded as an unsigned
19086 0xffffffff. Although 0xffffffff is technically a valid size value,
19087 an object of this size seems pretty unlikely so we can relatively
19088 safely treat these cases as if the size attribute was invalid and
19089 treat them as zero by default. */
19090 if (attr->name == DW_AT_byte_size
19091 && form == DW_FORM_data4
19092 && DW_UNSND (attr) >= 0xffffffff)
19093 {
19094 complaint
19095 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19096 hex_string (DW_UNSND (attr)));
19097 DW_UNSND (attr) = 0;
19098 }
19099
19100 return info_ptr;
19101 }
19102
19103 /* Read an attribute described by an abbreviated attribute. */
19104
19105 static const gdb_byte *
19106 read_attribute (const struct die_reader_specs *reader,
19107 struct attribute *attr, struct attr_abbrev *abbrev,
19108 const gdb_byte *info_ptr)
19109 {
19110 attr->name = abbrev->name;
19111 return read_attribute_value (reader, attr, abbrev->form,
19112 abbrev->implicit_const, info_ptr);
19113 }
19114
19115 /* Read dwarf information from a buffer. */
19116
19117 static unsigned int
19118 read_1_byte (bfd *abfd, const gdb_byte *buf)
19119 {
19120 return bfd_get_8 (abfd, buf);
19121 }
19122
19123 static int
19124 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19125 {
19126 return bfd_get_signed_8 (abfd, buf);
19127 }
19128
19129 static unsigned int
19130 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19131 {
19132 return bfd_get_16 (abfd, buf);
19133 }
19134
19135 static int
19136 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19137 {
19138 return bfd_get_signed_16 (abfd, buf);
19139 }
19140
19141 static unsigned int
19142 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19143 {
19144 return bfd_get_32 (abfd, buf);
19145 }
19146
19147 static int
19148 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19149 {
19150 return bfd_get_signed_32 (abfd, buf);
19151 }
19152
19153 static ULONGEST
19154 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19155 {
19156 return bfd_get_64 (abfd, buf);
19157 }
19158
19159 static CORE_ADDR
19160 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19161 unsigned int *bytes_read)
19162 {
19163 struct comp_unit_head *cu_header = &cu->header;
19164 CORE_ADDR retval = 0;
19165
19166 if (cu_header->signed_addr_p)
19167 {
19168 switch (cu_header->addr_size)
19169 {
19170 case 2:
19171 retval = bfd_get_signed_16 (abfd, buf);
19172 break;
19173 case 4:
19174 retval = bfd_get_signed_32 (abfd, buf);
19175 break;
19176 case 8:
19177 retval = bfd_get_signed_64 (abfd, buf);
19178 break;
19179 default:
19180 internal_error (__FILE__, __LINE__,
19181 _("read_address: bad switch, signed [in module %s]"),
19182 bfd_get_filename (abfd));
19183 }
19184 }
19185 else
19186 {
19187 switch (cu_header->addr_size)
19188 {
19189 case 2:
19190 retval = bfd_get_16 (abfd, buf);
19191 break;
19192 case 4:
19193 retval = bfd_get_32 (abfd, buf);
19194 break;
19195 case 8:
19196 retval = bfd_get_64 (abfd, buf);
19197 break;
19198 default:
19199 internal_error (__FILE__, __LINE__,
19200 _("read_address: bad switch, "
19201 "unsigned [in module %s]"),
19202 bfd_get_filename (abfd));
19203 }
19204 }
19205
19206 *bytes_read = cu_header->addr_size;
19207 return retval;
19208 }
19209
19210 /* Read the initial length from a section. The (draft) DWARF 3
19211 specification allows the initial length to take up either 4 bytes
19212 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19213 bytes describe the length and all offsets will be 8 bytes in length
19214 instead of 4.
19215
19216 An older, non-standard 64-bit format is also handled by this
19217 function. The older format in question stores the initial length
19218 as an 8-byte quantity without an escape value. Lengths greater
19219 than 2^32 aren't very common which means that the initial 4 bytes
19220 is almost always zero. Since a length value of zero doesn't make
19221 sense for the 32-bit format, this initial zero can be considered to
19222 be an escape value which indicates the presence of the older 64-bit
19223 format. As written, the code can't detect (old format) lengths
19224 greater than 4GB. If it becomes necessary to handle lengths
19225 somewhat larger than 4GB, we could allow other small values (such
19226 as the non-sensical values of 1, 2, and 3) to also be used as
19227 escape values indicating the presence of the old format.
19228
19229 The value returned via bytes_read should be used to increment the
19230 relevant pointer after calling read_initial_length().
19231
19232 [ Note: read_initial_length() and read_offset() are based on the
19233 document entitled "DWARF Debugging Information Format", revision
19234 3, draft 8, dated November 19, 2001. This document was obtained
19235 from:
19236
19237 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19238
19239 This document is only a draft and is subject to change. (So beware.)
19240
19241 Details regarding the older, non-standard 64-bit format were
19242 determined empirically by examining 64-bit ELF files produced by
19243 the SGI toolchain on an IRIX 6.5 machine.
19244
19245 - Kevin, July 16, 2002
19246 ] */
19247
19248 static LONGEST
19249 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19250 {
19251 LONGEST length = bfd_get_32 (abfd, buf);
19252
19253 if (length == 0xffffffff)
19254 {
19255 length = bfd_get_64 (abfd, buf + 4);
19256 *bytes_read = 12;
19257 }
19258 else if (length == 0)
19259 {
19260 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19261 length = bfd_get_64 (abfd, buf);
19262 *bytes_read = 8;
19263 }
19264 else
19265 {
19266 *bytes_read = 4;
19267 }
19268
19269 return length;
19270 }
19271
19272 /* Cover function for read_initial_length.
19273 Returns the length of the object at BUF, and stores the size of the
19274 initial length in *BYTES_READ and stores the size that offsets will be in
19275 *OFFSET_SIZE.
19276 If the initial length size is not equivalent to that specified in
19277 CU_HEADER then issue a complaint.
19278 This is useful when reading non-comp-unit headers. */
19279
19280 static LONGEST
19281 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19282 const struct comp_unit_head *cu_header,
19283 unsigned int *bytes_read,
19284 unsigned int *offset_size)
19285 {
19286 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19287
19288 gdb_assert (cu_header->initial_length_size == 4
19289 || cu_header->initial_length_size == 8
19290 || cu_header->initial_length_size == 12);
19291
19292 if (cu_header->initial_length_size != *bytes_read)
19293 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19294
19295 *offset_size = (*bytes_read == 4) ? 4 : 8;
19296 return length;
19297 }
19298
19299 /* Read an offset from the data stream. The size of the offset is
19300 given by cu_header->offset_size. */
19301
19302 static LONGEST
19303 read_offset (bfd *abfd, const gdb_byte *buf,
19304 const struct comp_unit_head *cu_header,
19305 unsigned int *bytes_read)
19306 {
19307 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19308
19309 *bytes_read = cu_header->offset_size;
19310 return offset;
19311 }
19312
19313 /* Read an offset from the data stream. */
19314
19315 static LONGEST
19316 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19317 {
19318 LONGEST retval = 0;
19319
19320 switch (offset_size)
19321 {
19322 case 4:
19323 retval = bfd_get_32 (abfd, buf);
19324 break;
19325 case 8:
19326 retval = bfd_get_64 (abfd, buf);
19327 break;
19328 default:
19329 internal_error (__FILE__, __LINE__,
19330 _("read_offset_1: bad switch [in module %s]"),
19331 bfd_get_filename (abfd));
19332 }
19333
19334 return retval;
19335 }
19336
19337 static const gdb_byte *
19338 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19339 {
19340 /* If the size of a host char is 8 bits, we can return a pointer
19341 to the buffer, otherwise we have to copy the data to a buffer
19342 allocated on the temporary obstack. */
19343 gdb_assert (HOST_CHAR_BIT == 8);
19344 return buf;
19345 }
19346
19347 static const char *
19348 read_direct_string (bfd *abfd, const gdb_byte *buf,
19349 unsigned int *bytes_read_ptr)
19350 {
19351 /* If the size of a host char is 8 bits, we can return a pointer
19352 to the string, otherwise we have to copy the string to a buffer
19353 allocated on the temporary obstack. */
19354 gdb_assert (HOST_CHAR_BIT == 8);
19355 if (*buf == '\0')
19356 {
19357 *bytes_read_ptr = 1;
19358 return NULL;
19359 }
19360 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19361 return (const char *) buf;
19362 }
19363
19364 /* Return pointer to string at section SECT offset STR_OFFSET with error
19365 reporting strings FORM_NAME and SECT_NAME. */
19366
19367 static const char *
19368 read_indirect_string_at_offset_from (struct objfile *objfile,
19369 bfd *abfd, LONGEST str_offset,
19370 struct dwarf2_section_info *sect,
19371 const char *form_name,
19372 const char *sect_name)
19373 {
19374 dwarf2_read_section (objfile, sect);
19375 if (sect->buffer == NULL)
19376 error (_("%s used without %s section [in module %s]"),
19377 form_name, sect_name, bfd_get_filename (abfd));
19378 if (str_offset >= sect->size)
19379 error (_("%s pointing outside of %s section [in module %s]"),
19380 form_name, sect_name, bfd_get_filename (abfd));
19381 gdb_assert (HOST_CHAR_BIT == 8);
19382 if (sect->buffer[str_offset] == '\0')
19383 return NULL;
19384 return (const char *) (sect->buffer + str_offset);
19385 }
19386
19387 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19388
19389 static const char *
19390 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19391 bfd *abfd, LONGEST str_offset)
19392 {
19393 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19394 abfd, str_offset,
19395 &dwarf2_per_objfile->str,
19396 "DW_FORM_strp", ".debug_str");
19397 }
19398
19399 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19400
19401 static const char *
19402 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19403 bfd *abfd, LONGEST str_offset)
19404 {
19405 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19406 abfd, str_offset,
19407 &dwarf2_per_objfile->line_str,
19408 "DW_FORM_line_strp",
19409 ".debug_line_str");
19410 }
19411
19412 /* Read a string at offset STR_OFFSET in the .debug_str section from
19413 the .dwz file DWZ. Throw an error if the offset is too large. If
19414 the string consists of a single NUL byte, return NULL; otherwise
19415 return a pointer to the string. */
19416
19417 static const char *
19418 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19419 LONGEST str_offset)
19420 {
19421 dwarf2_read_section (objfile, &dwz->str);
19422
19423 if (dwz->str.buffer == NULL)
19424 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19425 "section [in module %s]"),
19426 bfd_get_filename (dwz->dwz_bfd));
19427 if (str_offset >= dwz->str.size)
19428 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19429 ".debug_str section [in module %s]"),
19430 bfd_get_filename (dwz->dwz_bfd));
19431 gdb_assert (HOST_CHAR_BIT == 8);
19432 if (dwz->str.buffer[str_offset] == '\0')
19433 return NULL;
19434 return (const char *) (dwz->str.buffer + str_offset);
19435 }
19436
19437 /* Return pointer to string at .debug_str offset as read from BUF.
19438 BUF is assumed to be in a compilation unit described by CU_HEADER.
19439 Return *BYTES_READ_PTR count of bytes read from BUF. */
19440
19441 static const char *
19442 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19443 const gdb_byte *buf,
19444 const struct comp_unit_head *cu_header,
19445 unsigned int *bytes_read_ptr)
19446 {
19447 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19448
19449 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19450 }
19451
19452 /* Return pointer to string at .debug_line_str offset as read from BUF.
19453 BUF is assumed to be in a compilation unit described by CU_HEADER.
19454 Return *BYTES_READ_PTR count of bytes read from BUF. */
19455
19456 static const char *
19457 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19458 bfd *abfd, const gdb_byte *buf,
19459 const struct comp_unit_head *cu_header,
19460 unsigned int *bytes_read_ptr)
19461 {
19462 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19463
19464 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19465 str_offset);
19466 }
19467
19468 ULONGEST
19469 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19470 unsigned int *bytes_read_ptr)
19471 {
19472 ULONGEST result;
19473 unsigned int num_read;
19474 int shift;
19475 unsigned char byte;
19476
19477 result = 0;
19478 shift = 0;
19479 num_read = 0;
19480 while (1)
19481 {
19482 byte = bfd_get_8 (abfd, buf);
19483 buf++;
19484 num_read++;
19485 result |= ((ULONGEST) (byte & 127) << shift);
19486 if ((byte & 128) == 0)
19487 {
19488 break;
19489 }
19490 shift += 7;
19491 }
19492 *bytes_read_ptr = num_read;
19493 return result;
19494 }
19495
19496 static LONGEST
19497 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19498 unsigned int *bytes_read_ptr)
19499 {
19500 LONGEST result;
19501 int shift, num_read;
19502 unsigned char byte;
19503
19504 result = 0;
19505 shift = 0;
19506 num_read = 0;
19507 while (1)
19508 {
19509 byte = bfd_get_8 (abfd, buf);
19510 buf++;
19511 num_read++;
19512 result |= ((LONGEST) (byte & 127) << shift);
19513 shift += 7;
19514 if ((byte & 128) == 0)
19515 {
19516 break;
19517 }
19518 }
19519 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19520 result |= -(((LONGEST) 1) << shift);
19521 *bytes_read_ptr = num_read;
19522 return result;
19523 }
19524
19525 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19526 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19527 ADDR_SIZE is the size of addresses from the CU header. */
19528
19529 static CORE_ADDR
19530 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19531 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19532 {
19533 struct objfile *objfile = dwarf2_per_objfile->objfile;
19534 bfd *abfd = objfile->obfd;
19535 const gdb_byte *info_ptr;
19536
19537 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19538 if (dwarf2_per_objfile->addr.buffer == NULL)
19539 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19540 objfile_name (objfile));
19541 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19542 error (_("DW_FORM_addr_index pointing outside of "
19543 ".debug_addr section [in module %s]"),
19544 objfile_name (objfile));
19545 info_ptr = (dwarf2_per_objfile->addr.buffer
19546 + addr_base + addr_index * addr_size);
19547 if (addr_size == 4)
19548 return bfd_get_32 (abfd, info_ptr);
19549 else
19550 return bfd_get_64 (abfd, info_ptr);
19551 }
19552
19553 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19554
19555 static CORE_ADDR
19556 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19557 {
19558 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19559 cu->addr_base, cu->header.addr_size);
19560 }
19561
19562 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19563
19564 static CORE_ADDR
19565 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19566 unsigned int *bytes_read)
19567 {
19568 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19569 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19570
19571 return read_addr_index (cu, addr_index);
19572 }
19573
19574 /* Data structure to pass results from dwarf2_read_addr_index_reader
19575 back to dwarf2_read_addr_index. */
19576
19577 struct dwarf2_read_addr_index_data
19578 {
19579 ULONGEST addr_base;
19580 int addr_size;
19581 };
19582
19583 /* die_reader_func for dwarf2_read_addr_index. */
19584
19585 static void
19586 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19587 const gdb_byte *info_ptr,
19588 struct die_info *comp_unit_die,
19589 int has_children,
19590 void *data)
19591 {
19592 struct dwarf2_cu *cu = reader->cu;
19593 struct dwarf2_read_addr_index_data *aidata =
19594 (struct dwarf2_read_addr_index_data *) data;
19595
19596 aidata->addr_base = cu->addr_base;
19597 aidata->addr_size = cu->header.addr_size;
19598 }
19599
19600 /* Given an index in .debug_addr, fetch the value.
19601 NOTE: This can be called during dwarf expression evaluation,
19602 long after the debug information has been read, and thus per_cu->cu
19603 may no longer exist. */
19604
19605 CORE_ADDR
19606 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19607 unsigned int addr_index)
19608 {
19609 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19610 struct dwarf2_cu *cu = per_cu->cu;
19611 ULONGEST addr_base;
19612 int addr_size;
19613
19614 /* We need addr_base and addr_size.
19615 If we don't have PER_CU->cu, we have to get it.
19616 Nasty, but the alternative is storing the needed info in PER_CU,
19617 which at this point doesn't seem justified: it's not clear how frequently
19618 it would get used and it would increase the size of every PER_CU.
19619 Entry points like dwarf2_per_cu_addr_size do a similar thing
19620 so we're not in uncharted territory here.
19621 Alas we need to be a bit more complicated as addr_base is contained
19622 in the DIE.
19623
19624 We don't need to read the entire CU(/TU).
19625 We just need the header and top level die.
19626
19627 IWBN to use the aging mechanism to let us lazily later discard the CU.
19628 For now we skip this optimization. */
19629
19630 if (cu != NULL)
19631 {
19632 addr_base = cu->addr_base;
19633 addr_size = cu->header.addr_size;
19634 }
19635 else
19636 {
19637 struct dwarf2_read_addr_index_data aidata;
19638
19639 /* Note: We can't use init_cutu_and_read_dies_simple here,
19640 we need addr_base. */
19641 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
19642 dwarf2_read_addr_index_reader, &aidata);
19643 addr_base = aidata.addr_base;
19644 addr_size = aidata.addr_size;
19645 }
19646
19647 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19648 addr_size);
19649 }
19650
19651 /* Given a DW_FORM_GNU_str_index, fetch the string.
19652 This is only used by the Fission support. */
19653
19654 static const char *
19655 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19656 {
19657 struct dwarf2_cu *cu = reader->cu;
19658 struct dwarf2_per_objfile *dwarf2_per_objfile
19659 = cu->per_cu->dwarf2_per_objfile;
19660 struct objfile *objfile = dwarf2_per_objfile->objfile;
19661 const char *objf_name = objfile_name (objfile);
19662 bfd *abfd = objfile->obfd;
19663 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19664 struct dwarf2_section_info *str_offsets_section =
19665 &reader->dwo_file->sections.str_offsets;
19666 const gdb_byte *info_ptr;
19667 ULONGEST str_offset;
19668 static const char form_name[] = "DW_FORM_GNU_str_index";
19669
19670 dwarf2_read_section (objfile, str_section);
19671 dwarf2_read_section (objfile, str_offsets_section);
19672 if (str_section->buffer == NULL)
19673 error (_("%s used without .debug_str.dwo section"
19674 " in CU at offset %s [in module %s]"),
19675 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19676 if (str_offsets_section->buffer == NULL)
19677 error (_("%s used without .debug_str_offsets.dwo section"
19678 " in CU at offset %s [in module %s]"),
19679 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19680 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19681 error (_("%s pointing outside of .debug_str_offsets.dwo"
19682 " section in CU at offset %s [in module %s]"),
19683 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19684 info_ptr = (str_offsets_section->buffer
19685 + str_index * cu->header.offset_size);
19686 if (cu->header.offset_size == 4)
19687 str_offset = bfd_get_32 (abfd, info_ptr);
19688 else
19689 str_offset = bfd_get_64 (abfd, info_ptr);
19690 if (str_offset >= str_section->size)
19691 error (_("Offset from %s pointing outside of"
19692 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19693 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19694 return (const char *) (str_section->buffer + str_offset);
19695 }
19696
19697 /* Return the length of an LEB128 number in BUF. */
19698
19699 static int
19700 leb128_size (const gdb_byte *buf)
19701 {
19702 const gdb_byte *begin = buf;
19703 gdb_byte byte;
19704
19705 while (1)
19706 {
19707 byte = *buf++;
19708 if ((byte & 128) == 0)
19709 return buf - begin;
19710 }
19711 }
19712
19713 static void
19714 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19715 {
19716 switch (lang)
19717 {
19718 case DW_LANG_C89:
19719 case DW_LANG_C99:
19720 case DW_LANG_C11:
19721 case DW_LANG_C:
19722 case DW_LANG_UPC:
19723 cu->language = language_c;
19724 break;
19725 case DW_LANG_Java:
19726 case DW_LANG_C_plus_plus:
19727 case DW_LANG_C_plus_plus_11:
19728 case DW_LANG_C_plus_plus_14:
19729 cu->language = language_cplus;
19730 break;
19731 case DW_LANG_D:
19732 cu->language = language_d;
19733 break;
19734 case DW_LANG_Fortran77:
19735 case DW_LANG_Fortran90:
19736 case DW_LANG_Fortran95:
19737 case DW_LANG_Fortran03:
19738 case DW_LANG_Fortran08:
19739 cu->language = language_fortran;
19740 break;
19741 case DW_LANG_Go:
19742 cu->language = language_go;
19743 break;
19744 case DW_LANG_Mips_Assembler:
19745 cu->language = language_asm;
19746 break;
19747 case DW_LANG_Ada83:
19748 case DW_LANG_Ada95:
19749 cu->language = language_ada;
19750 break;
19751 case DW_LANG_Modula2:
19752 cu->language = language_m2;
19753 break;
19754 case DW_LANG_Pascal83:
19755 cu->language = language_pascal;
19756 break;
19757 case DW_LANG_ObjC:
19758 cu->language = language_objc;
19759 break;
19760 case DW_LANG_Rust:
19761 case DW_LANG_Rust_old:
19762 cu->language = language_rust;
19763 break;
19764 case DW_LANG_Cobol74:
19765 case DW_LANG_Cobol85:
19766 default:
19767 cu->language = language_minimal;
19768 break;
19769 }
19770 cu->language_defn = language_def (cu->language);
19771 }
19772
19773 /* Return the named attribute or NULL if not there. */
19774
19775 static struct attribute *
19776 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19777 {
19778 for (;;)
19779 {
19780 unsigned int i;
19781 struct attribute *spec = NULL;
19782
19783 for (i = 0; i < die->num_attrs; ++i)
19784 {
19785 if (die->attrs[i].name == name)
19786 return &die->attrs[i];
19787 if (die->attrs[i].name == DW_AT_specification
19788 || die->attrs[i].name == DW_AT_abstract_origin)
19789 spec = &die->attrs[i];
19790 }
19791
19792 if (!spec)
19793 break;
19794
19795 die = follow_die_ref (die, spec, &cu);
19796 }
19797
19798 return NULL;
19799 }
19800
19801 /* Return the named attribute or NULL if not there,
19802 but do not follow DW_AT_specification, etc.
19803 This is for use in contexts where we're reading .debug_types dies.
19804 Following DW_AT_specification, DW_AT_abstract_origin will take us
19805 back up the chain, and we want to go down. */
19806
19807 static struct attribute *
19808 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19809 {
19810 unsigned int i;
19811
19812 for (i = 0; i < die->num_attrs; ++i)
19813 if (die->attrs[i].name == name)
19814 return &die->attrs[i];
19815
19816 return NULL;
19817 }
19818
19819 /* Return the string associated with a string-typed attribute, or NULL if it
19820 is either not found or is of an incorrect type. */
19821
19822 static const char *
19823 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19824 {
19825 struct attribute *attr;
19826 const char *str = NULL;
19827
19828 attr = dwarf2_attr (die, name, cu);
19829
19830 if (attr != NULL)
19831 {
19832 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19833 || attr->form == DW_FORM_string
19834 || attr->form == DW_FORM_GNU_str_index
19835 || attr->form == DW_FORM_GNU_strp_alt)
19836 str = DW_STRING (attr);
19837 else
19838 complaint (_("string type expected for attribute %s for "
19839 "DIE at %s in module %s"),
19840 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19841 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19842 }
19843
19844 return str;
19845 }
19846
19847 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19848 and holds a non-zero value. This function should only be used for
19849 DW_FORM_flag or DW_FORM_flag_present attributes. */
19850
19851 static int
19852 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19853 {
19854 struct attribute *attr = dwarf2_attr (die, name, cu);
19855
19856 return (attr && DW_UNSND (attr));
19857 }
19858
19859 static int
19860 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19861 {
19862 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19863 which value is non-zero. However, we have to be careful with
19864 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19865 (via dwarf2_flag_true_p) follows this attribute. So we may
19866 end up accidently finding a declaration attribute that belongs
19867 to a different DIE referenced by the specification attribute,
19868 even though the given DIE does not have a declaration attribute. */
19869 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19870 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19871 }
19872
19873 /* Return the die giving the specification for DIE, if there is
19874 one. *SPEC_CU is the CU containing DIE on input, and the CU
19875 containing the return value on output. If there is no
19876 specification, but there is an abstract origin, that is
19877 returned. */
19878
19879 static struct die_info *
19880 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19881 {
19882 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19883 *spec_cu);
19884
19885 if (spec_attr == NULL)
19886 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19887
19888 if (spec_attr == NULL)
19889 return NULL;
19890 else
19891 return follow_die_ref (die, spec_attr, spec_cu);
19892 }
19893
19894 /* Stub for free_line_header to match void * callback types. */
19895
19896 static void
19897 free_line_header_voidp (void *arg)
19898 {
19899 struct line_header *lh = (struct line_header *) arg;
19900
19901 delete lh;
19902 }
19903
19904 void
19905 line_header::add_include_dir (const char *include_dir)
19906 {
19907 if (dwarf_line_debug >= 2)
19908 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19909 include_dirs.size () + 1, include_dir);
19910
19911 include_dirs.push_back (include_dir);
19912 }
19913
19914 void
19915 line_header::add_file_name (const char *name,
19916 dir_index d_index,
19917 unsigned int mod_time,
19918 unsigned int length)
19919 {
19920 if (dwarf_line_debug >= 2)
19921 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19922 (unsigned) file_names.size () + 1, name);
19923
19924 file_names.emplace_back (name, d_index, mod_time, length);
19925 }
19926
19927 /* A convenience function to find the proper .debug_line section for a CU. */
19928
19929 static struct dwarf2_section_info *
19930 get_debug_line_section (struct dwarf2_cu *cu)
19931 {
19932 struct dwarf2_section_info *section;
19933 struct dwarf2_per_objfile *dwarf2_per_objfile
19934 = cu->per_cu->dwarf2_per_objfile;
19935
19936 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19937 DWO file. */
19938 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19939 section = &cu->dwo_unit->dwo_file->sections.line;
19940 else if (cu->per_cu->is_dwz)
19941 {
19942 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19943
19944 section = &dwz->line;
19945 }
19946 else
19947 section = &dwarf2_per_objfile->line;
19948
19949 return section;
19950 }
19951
19952 /* Read directory or file name entry format, starting with byte of
19953 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19954 entries count and the entries themselves in the described entry
19955 format. */
19956
19957 static void
19958 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19959 bfd *abfd, const gdb_byte **bufp,
19960 struct line_header *lh,
19961 const struct comp_unit_head *cu_header,
19962 void (*callback) (struct line_header *lh,
19963 const char *name,
19964 dir_index d_index,
19965 unsigned int mod_time,
19966 unsigned int length))
19967 {
19968 gdb_byte format_count, formati;
19969 ULONGEST data_count, datai;
19970 const gdb_byte *buf = *bufp;
19971 const gdb_byte *format_header_data;
19972 unsigned int bytes_read;
19973
19974 format_count = read_1_byte (abfd, buf);
19975 buf += 1;
19976 format_header_data = buf;
19977 for (formati = 0; formati < format_count; formati++)
19978 {
19979 read_unsigned_leb128 (abfd, buf, &bytes_read);
19980 buf += bytes_read;
19981 read_unsigned_leb128 (abfd, buf, &bytes_read);
19982 buf += bytes_read;
19983 }
19984
19985 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19986 buf += bytes_read;
19987 for (datai = 0; datai < data_count; datai++)
19988 {
19989 const gdb_byte *format = format_header_data;
19990 struct file_entry fe;
19991
19992 for (formati = 0; formati < format_count; formati++)
19993 {
19994 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19995 format += bytes_read;
19996
19997 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19998 format += bytes_read;
19999
20000 gdb::optional<const char *> string;
20001 gdb::optional<unsigned int> uint;
20002
20003 switch (form)
20004 {
20005 case DW_FORM_string:
20006 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20007 buf += bytes_read;
20008 break;
20009
20010 case DW_FORM_line_strp:
20011 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20012 abfd, buf,
20013 cu_header,
20014 &bytes_read));
20015 buf += bytes_read;
20016 break;
20017
20018 case DW_FORM_data1:
20019 uint.emplace (read_1_byte (abfd, buf));
20020 buf += 1;
20021 break;
20022
20023 case DW_FORM_data2:
20024 uint.emplace (read_2_bytes (abfd, buf));
20025 buf += 2;
20026 break;
20027
20028 case DW_FORM_data4:
20029 uint.emplace (read_4_bytes (abfd, buf));
20030 buf += 4;
20031 break;
20032
20033 case DW_FORM_data8:
20034 uint.emplace (read_8_bytes (abfd, buf));
20035 buf += 8;
20036 break;
20037
20038 case DW_FORM_udata:
20039 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20040 buf += bytes_read;
20041 break;
20042
20043 case DW_FORM_block:
20044 /* It is valid only for DW_LNCT_timestamp which is ignored by
20045 current GDB. */
20046 break;
20047 }
20048
20049 switch (content_type)
20050 {
20051 case DW_LNCT_path:
20052 if (string.has_value ())
20053 fe.name = *string;
20054 break;
20055 case DW_LNCT_directory_index:
20056 if (uint.has_value ())
20057 fe.d_index = (dir_index) *uint;
20058 break;
20059 case DW_LNCT_timestamp:
20060 if (uint.has_value ())
20061 fe.mod_time = *uint;
20062 break;
20063 case DW_LNCT_size:
20064 if (uint.has_value ())
20065 fe.length = *uint;
20066 break;
20067 case DW_LNCT_MD5:
20068 break;
20069 default:
20070 complaint (_("Unknown format content type %s"),
20071 pulongest (content_type));
20072 }
20073 }
20074
20075 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20076 }
20077
20078 *bufp = buf;
20079 }
20080
20081 /* Read the statement program header starting at OFFSET in
20082 .debug_line, or .debug_line.dwo. Return a pointer
20083 to a struct line_header, allocated using xmalloc.
20084 Returns NULL if there is a problem reading the header, e.g., if it
20085 has a version we don't understand.
20086
20087 NOTE: the strings in the include directory and file name tables of
20088 the returned object point into the dwarf line section buffer,
20089 and must not be freed. */
20090
20091 static line_header_up
20092 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20093 {
20094 const gdb_byte *line_ptr;
20095 unsigned int bytes_read, offset_size;
20096 int i;
20097 const char *cur_dir, *cur_file;
20098 struct dwarf2_section_info *section;
20099 bfd *abfd;
20100 struct dwarf2_per_objfile *dwarf2_per_objfile
20101 = cu->per_cu->dwarf2_per_objfile;
20102
20103 section = get_debug_line_section (cu);
20104 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20105 if (section->buffer == NULL)
20106 {
20107 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20108 complaint (_("missing .debug_line.dwo section"));
20109 else
20110 complaint (_("missing .debug_line section"));
20111 return 0;
20112 }
20113
20114 /* We can't do this until we know the section is non-empty.
20115 Only then do we know we have such a section. */
20116 abfd = get_section_bfd_owner (section);
20117
20118 /* Make sure that at least there's room for the total_length field.
20119 That could be 12 bytes long, but we're just going to fudge that. */
20120 if (to_underlying (sect_off) + 4 >= section->size)
20121 {
20122 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20123 return 0;
20124 }
20125
20126 line_header_up lh (new line_header ());
20127
20128 lh->sect_off = sect_off;
20129 lh->offset_in_dwz = cu->per_cu->is_dwz;
20130
20131 line_ptr = section->buffer + to_underlying (sect_off);
20132
20133 /* Read in the header. */
20134 lh->total_length =
20135 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20136 &bytes_read, &offset_size);
20137 line_ptr += bytes_read;
20138 if (line_ptr + lh->total_length > (section->buffer + section->size))
20139 {
20140 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20141 return 0;
20142 }
20143 lh->statement_program_end = line_ptr + lh->total_length;
20144 lh->version = read_2_bytes (abfd, line_ptr);
20145 line_ptr += 2;
20146 if (lh->version > 5)
20147 {
20148 /* This is a version we don't understand. The format could have
20149 changed in ways we don't handle properly so just punt. */
20150 complaint (_("unsupported version in .debug_line section"));
20151 return NULL;
20152 }
20153 if (lh->version >= 5)
20154 {
20155 gdb_byte segment_selector_size;
20156
20157 /* Skip address size. */
20158 read_1_byte (abfd, line_ptr);
20159 line_ptr += 1;
20160
20161 segment_selector_size = read_1_byte (abfd, line_ptr);
20162 line_ptr += 1;
20163 if (segment_selector_size != 0)
20164 {
20165 complaint (_("unsupported segment selector size %u "
20166 "in .debug_line section"),
20167 segment_selector_size);
20168 return NULL;
20169 }
20170 }
20171 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20172 line_ptr += offset_size;
20173 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20174 line_ptr += 1;
20175 if (lh->version >= 4)
20176 {
20177 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20178 line_ptr += 1;
20179 }
20180 else
20181 lh->maximum_ops_per_instruction = 1;
20182
20183 if (lh->maximum_ops_per_instruction == 0)
20184 {
20185 lh->maximum_ops_per_instruction = 1;
20186 complaint (_("invalid maximum_ops_per_instruction "
20187 "in `.debug_line' section"));
20188 }
20189
20190 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20191 line_ptr += 1;
20192 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20193 line_ptr += 1;
20194 lh->line_range = read_1_byte (abfd, line_ptr);
20195 line_ptr += 1;
20196 lh->opcode_base = read_1_byte (abfd, line_ptr);
20197 line_ptr += 1;
20198 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20199
20200 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20201 for (i = 1; i < lh->opcode_base; ++i)
20202 {
20203 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20204 line_ptr += 1;
20205 }
20206
20207 if (lh->version >= 5)
20208 {
20209 /* Read directory table. */
20210 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20211 &cu->header,
20212 [] (struct line_header *lh, const char *name,
20213 dir_index d_index, unsigned int mod_time,
20214 unsigned int length)
20215 {
20216 lh->add_include_dir (name);
20217 });
20218
20219 /* Read file name table. */
20220 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20221 &cu->header,
20222 [] (struct line_header *lh, const char *name,
20223 dir_index d_index, unsigned int mod_time,
20224 unsigned int length)
20225 {
20226 lh->add_file_name (name, d_index, mod_time, length);
20227 });
20228 }
20229 else
20230 {
20231 /* Read directory table. */
20232 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20233 {
20234 line_ptr += bytes_read;
20235 lh->add_include_dir (cur_dir);
20236 }
20237 line_ptr += bytes_read;
20238
20239 /* Read file name table. */
20240 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20241 {
20242 unsigned int mod_time, length;
20243 dir_index d_index;
20244
20245 line_ptr += bytes_read;
20246 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20247 line_ptr += bytes_read;
20248 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20249 line_ptr += bytes_read;
20250 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20251 line_ptr += bytes_read;
20252
20253 lh->add_file_name (cur_file, d_index, mod_time, length);
20254 }
20255 line_ptr += bytes_read;
20256 }
20257 lh->statement_program_start = line_ptr;
20258
20259 if (line_ptr > (section->buffer + section->size))
20260 complaint (_("line number info header doesn't "
20261 "fit in `.debug_line' section"));
20262
20263 return lh;
20264 }
20265
20266 /* Subroutine of dwarf_decode_lines to simplify it.
20267 Return the file name of the psymtab for included file FILE_INDEX
20268 in line header LH of PST.
20269 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20270 If space for the result is malloc'd, *NAME_HOLDER will be set.
20271 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20272
20273 static const char *
20274 psymtab_include_file_name (const struct line_header *lh, int file_index,
20275 const struct partial_symtab *pst,
20276 const char *comp_dir,
20277 gdb::unique_xmalloc_ptr<char> *name_holder)
20278 {
20279 const file_entry &fe = lh->file_names[file_index];
20280 const char *include_name = fe.name;
20281 const char *include_name_to_compare = include_name;
20282 const char *pst_filename;
20283 int file_is_pst;
20284
20285 const char *dir_name = fe.include_dir (lh);
20286
20287 gdb::unique_xmalloc_ptr<char> hold_compare;
20288 if (!IS_ABSOLUTE_PATH (include_name)
20289 && (dir_name != NULL || comp_dir != NULL))
20290 {
20291 /* Avoid creating a duplicate psymtab for PST.
20292 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20293 Before we do the comparison, however, we need to account
20294 for DIR_NAME and COMP_DIR.
20295 First prepend dir_name (if non-NULL). If we still don't
20296 have an absolute path prepend comp_dir (if non-NULL).
20297 However, the directory we record in the include-file's
20298 psymtab does not contain COMP_DIR (to match the
20299 corresponding symtab(s)).
20300
20301 Example:
20302
20303 bash$ cd /tmp
20304 bash$ gcc -g ./hello.c
20305 include_name = "hello.c"
20306 dir_name = "."
20307 DW_AT_comp_dir = comp_dir = "/tmp"
20308 DW_AT_name = "./hello.c"
20309
20310 */
20311
20312 if (dir_name != NULL)
20313 {
20314 name_holder->reset (concat (dir_name, SLASH_STRING,
20315 include_name, (char *) NULL));
20316 include_name = name_holder->get ();
20317 include_name_to_compare = include_name;
20318 }
20319 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20320 {
20321 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20322 include_name, (char *) NULL));
20323 include_name_to_compare = hold_compare.get ();
20324 }
20325 }
20326
20327 pst_filename = pst->filename;
20328 gdb::unique_xmalloc_ptr<char> copied_name;
20329 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20330 {
20331 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20332 pst_filename, (char *) NULL));
20333 pst_filename = copied_name.get ();
20334 }
20335
20336 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20337
20338 if (file_is_pst)
20339 return NULL;
20340 return include_name;
20341 }
20342
20343 /* State machine to track the state of the line number program. */
20344
20345 class lnp_state_machine
20346 {
20347 public:
20348 /* Initialize a machine state for the start of a line number
20349 program. */
20350 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20351 bool record_lines_p);
20352
20353 file_entry *current_file ()
20354 {
20355 /* lh->file_names is 0-based, but the file name numbers in the
20356 statement program are 1-based. */
20357 return m_line_header->file_name_at (m_file);
20358 }
20359
20360 /* Record the line in the state machine. END_SEQUENCE is true if
20361 we're processing the end of a sequence. */
20362 void record_line (bool end_sequence);
20363
20364 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20365 nop-out rest of the lines in this sequence. */
20366 void check_line_address (struct dwarf2_cu *cu,
20367 const gdb_byte *line_ptr,
20368 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20369
20370 void handle_set_discriminator (unsigned int discriminator)
20371 {
20372 m_discriminator = discriminator;
20373 m_line_has_non_zero_discriminator |= discriminator != 0;
20374 }
20375
20376 /* Handle DW_LNE_set_address. */
20377 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20378 {
20379 m_op_index = 0;
20380 address += baseaddr;
20381 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20382 }
20383
20384 /* Handle DW_LNS_advance_pc. */
20385 void handle_advance_pc (CORE_ADDR adjust);
20386
20387 /* Handle a special opcode. */
20388 void handle_special_opcode (unsigned char op_code);
20389
20390 /* Handle DW_LNS_advance_line. */
20391 void handle_advance_line (int line_delta)
20392 {
20393 advance_line (line_delta);
20394 }
20395
20396 /* Handle DW_LNS_set_file. */
20397 void handle_set_file (file_name_index file);
20398
20399 /* Handle DW_LNS_negate_stmt. */
20400 void handle_negate_stmt ()
20401 {
20402 m_is_stmt = !m_is_stmt;
20403 }
20404
20405 /* Handle DW_LNS_const_add_pc. */
20406 void handle_const_add_pc ();
20407
20408 /* Handle DW_LNS_fixed_advance_pc. */
20409 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20410 {
20411 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20412 m_op_index = 0;
20413 }
20414
20415 /* Handle DW_LNS_copy. */
20416 void handle_copy ()
20417 {
20418 record_line (false);
20419 m_discriminator = 0;
20420 }
20421
20422 /* Handle DW_LNE_end_sequence. */
20423 void handle_end_sequence ()
20424 {
20425 m_currently_recording_lines = true;
20426 }
20427
20428 private:
20429 /* Advance the line by LINE_DELTA. */
20430 void advance_line (int line_delta)
20431 {
20432 m_line += line_delta;
20433
20434 if (line_delta != 0)
20435 m_line_has_non_zero_discriminator = m_discriminator != 0;
20436 }
20437
20438 struct dwarf2_cu *m_cu;
20439
20440 gdbarch *m_gdbarch;
20441
20442 /* True if we're recording lines.
20443 Otherwise we're building partial symtabs and are just interested in
20444 finding include files mentioned by the line number program. */
20445 bool m_record_lines_p;
20446
20447 /* The line number header. */
20448 line_header *m_line_header;
20449
20450 /* These are part of the standard DWARF line number state machine,
20451 and initialized according to the DWARF spec. */
20452
20453 unsigned char m_op_index = 0;
20454 /* The line table index (1-based) of the current file. */
20455 file_name_index m_file = (file_name_index) 1;
20456 unsigned int m_line = 1;
20457
20458 /* These are initialized in the constructor. */
20459
20460 CORE_ADDR m_address;
20461 bool m_is_stmt;
20462 unsigned int m_discriminator;
20463
20464 /* Additional bits of state we need to track. */
20465
20466 /* The last file that we called dwarf2_start_subfile for.
20467 This is only used for TLLs. */
20468 unsigned int m_last_file = 0;
20469 /* The last file a line number was recorded for. */
20470 struct subfile *m_last_subfile = NULL;
20471
20472 /* When true, record the lines we decode. */
20473 bool m_currently_recording_lines = false;
20474
20475 /* The last line number that was recorded, used to coalesce
20476 consecutive entries for the same line. This can happen, for
20477 example, when discriminators are present. PR 17276. */
20478 unsigned int m_last_line = 0;
20479 bool m_line_has_non_zero_discriminator = false;
20480 };
20481
20482 void
20483 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20484 {
20485 CORE_ADDR addr_adj = (((m_op_index + adjust)
20486 / m_line_header->maximum_ops_per_instruction)
20487 * m_line_header->minimum_instruction_length);
20488 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20489 m_op_index = ((m_op_index + adjust)
20490 % m_line_header->maximum_ops_per_instruction);
20491 }
20492
20493 void
20494 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20495 {
20496 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20497 CORE_ADDR addr_adj = (((m_op_index
20498 + (adj_opcode / m_line_header->line_range))
20499 / m_line_header->maximum_ops_per_instruction)
20500 * m_line_header->minimum_instruction_length);
20501 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20502 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20503 % m_line_header->maximum_ops_per_instruction);
20504
20505 int line_delta = (m_line_header->line_base
20506 + (adj_opcode % m_line_header->line_range));
20507 advance_line (line_delta);
20508 record_line (false);
20509 m_discriminator = 0;
20510 }
20511
20512 void
20513 lnp_state_machine::handle_set_file (file_name_index file)
20514 {
20515 m_file = file;
20516
20517 const file_entry *fe = current_file ();
20518 if (fe == NULL)
20519 dwarf2_debug_line_missing_file_complaint ();
20520 else if (m_record_lines_p)
20521 {
20522 const char *dir = fe->include_dir (m_line_header);
20523
20524 m_last_subfile = m_cu->builder->get_current_subfile ();
20525 m_line_has_non_zero_discriminator = m_discriminator != 0;
20526 dwarf2_start_subfile (m_cu, fe->name, dir);
20527 }
20528 }
20529
20530 void
20531 lnp_state_machine::handle_const_add_pc ()
20532 {
20533 CORE_ADDR adjust
20534 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20535
20536 CORE_ADDR addr_adj
20537 = (((m_op_index + adjust)
20538 / m_line_header->maximum_ops_per_instruction)
20539 * m_line_header->minimum_instruction_length);
20540
20541 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20542 m_op_index = ((m_op_index + adjust)
20543 % m_line_header->maximum_ops_per_instruction);
20544 }
20545
20546 /* Return non-zero if we should add LINE to the line number table.
20547 LINE is the line to add, LAST_LINE is the last line that was added,
20548 LAST_SUBFILE is the subfile for LAST_LINE.
20549 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20550 had a non-zero discriminator.
20551
20552 We have to be careful in the presence of discriminators.
20553 E.g., for this line:
20554
20555 for (i = 0; i < 100000; i++);
20556
20557 clang can emit four line number entries for that one line,
20558 each with a different discriminator.
20559 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20560
20561 However, we want gdb to coalesce all four entries into one.
20562 Otherwise the user could stepi into the middle of the line and
20563 gdb would get confused about whether the pc really was in the
20564 middle of the line.
20565
20566 Things are further complicated by the fact that two consecutive
20567 line number entries for the same line is a heuristic used by gcc
20568 to denote the end of the prologue. So we can't just discard duplicate
20569 entries, we have to be selective about it. The heuristic we use is
20570 that we only collapse consecutive entries for the same line if at least
20571 one of those entries has a non-zero discriminator. PR 17276.
20572
20573 Note: Addresses in the line number state machine can never go backwards
20574 within one sequence, thus this coalescing is ok. */
20575
20576 static int
20577 dwarf_record_line_p (struct dwarf2_cu *cu,
20578 unsigned int line, unsigned int last_line,
20579 int line_has_non_zero_discriminator,
20580 struct subfile *last_subfile)
20581 {
20582 if (cu->builder->get_current_subfile () != last_subfile)
20583 return 1;
20584 if (line != last_line)
20585 return 1;
20586 /* Same line for the same file that we've seen already.
20587 As a last check, for pr 17276, only record the line if the line
20588 has never had a non-zero discriminator. */
20589 if (!line_has_non_zero_discriminator)
20590 return 1;
20591 return 0;
20592 }
20593
20594 /* Use the CU's builder to record line number LINE beginning at
20595 address ADDRESS in the line table of subfile SUBFILE. */
20596
20597 static void
20598 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20599 unsigned int line, CORE_ADDR address,
20600 struct dwarf2_cu *cu)
20601 {
20602 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20603
20604 if (dwarf_line_debug)
20605 {
20606 fprintf_unfiltered (gdb_stdlog,
20607 "Recording line %u, file %s, address %s\n",
20608 line, lbasename (subfile->name),
20609 paddress (gdbarch, address));
20610 }
20611
20612 if (cu != nullptr)
20613 cu->builder->record_line (subfile, line, addr);
20614 }
20615
20616 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20617 Mark the end of a set of line number records.
20618 The arguments are the same as for dwarf_record_line_1.
20619 If SUBFILE is NULL the request is ignored. */
20620
20621 static void
20622 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20623 CORE_ADDR address, struct dwarf2_cu *cu)
20624 {
20625 if (subfile == NULL)
20626 return;
20627
20628 if (dwarf_line_debug)
20629 {
20630 fprintf_unfiltered (gdb_stdlog,
20631 "Finishing current line, file %s, address %s\n",
20632 lbasename (subfile->name),
20633 paddress (gdbarch, address));
20634 }
20635
20636 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
20637 }
20638
20639 void
20640 lnp_state_machine::record_line (bool end_sequence)
20641 {
20642 if (dwarf_line_debug)
20643 {
20644 fprintf_unfiltered (gdb_stdlog,
20645 "Processing actual line %u: file %u,"
20646 " address %s, is_stmt %u, discrim %u\n",
20647 m_line, to_underlying (m_file),
20648 paddress (m_gdbarch, m_address),
20649 m_is_stmt, m_discriminator);
20650 }
20651
20652 file_entry *fe = current_file ();
20653
20654 if (fe == NULL)
20655 dwarf2_debug_line_missing_file_complaint ();
20656 /* For now we ignore lines not starting on an instruction boundary.
20657 But not when processing end_sequence for compatibility with the
20658 previous version of the code. */
20659 else if (m_op_index == 0 || end_sequence)
20660 {
20661 fe->included_p = 1;
20662 if (m_record_lines_p && m_is_stmt)
20663 {
20664 if (m_last_subfile != m_cu->builder->get_current_subfile ()
20665 || end_sequence)
20666 {
20667 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20668 m_currently_recording_lines ? m_cu : nullptr);
20669 }
20670
20671 if (!end_sequence)
20672 {
20673 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20674 m_line_has_non_zero_discriminator,
20675 m_last_subfile))
20676 {
20677 dwarf_record_line_1 (m_gdbarch,
20678 m_cu->builder->get_current_subfile (),
20679 m_line, m_address,
20680 m_currently_recording_lines ? m_cu : nullptr);
20681 }
20682 m_last_subfile = m_cu->builder->get_current_subfile ();
20683 m_last_line = m_line;
20684 }
20685 }
20686 }
20687 }
20688
20689 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20690 line_header *lh, bool record_lines_p)
20691 {
20692 m_cu = cu;
20693 m_gdbarch = arch;
20694 m_record_lines_p = record_lines_p;
20695 m_line_header = lh;
20696
20697 m_currently_recording_lines = true;
20698
20699 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20700 was a line entry for it so that the backend has a chance to adjust it
20701 and also record it in case it needs it. This is currently used by MIPS
20702 code, cf. `mips_adjust_dwarf2_line'. */
20703 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20704 m_is_stmt = lh->default_is_stmt;
20705 m_discriminator = 0;
20706 }
20707
20708 void
20709 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20710 const gdb_byte *line_ptr,
20711 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20712 {
20713 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20714 the pc range of the CU. However, we restrict the test to only ADDRESS
20715 values of zero to preserve GDB's previous behaviour which is to handle
20716 the specific case of a function being GC'd by the linker. */
20717
20718 if (address == 0 && address < unrelocated_lowpc)
20719 {
20720 /* This line table is for a function which has been
20721 GCd by the linker. Ignore it. PR gdb/12528 */
20722
20723 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20724 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20725
20726 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20727 line_offset, objfile_name (objfile));
20728 m_currently_recording_lines = false;
20729 /* Note: m_currently_recording_lines is left as false until we see
20730 DW_LNE_end_sequence. */
20731 }
20732 }
20733
20734 /* Subroutine of dwarf_decode_lines to simplify it.
20735 Process the line number information in LH.
20736 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20737 program in order to set included_p for every referenced header. */
20738
20739 static void
20740 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20741 const int decode_for_pst_p, CORE_ADDR lowpc)
20742 {
20743 const gdb_byte *line_ptr, *extended_end;
20744 const gdb_byte *line_end;
20745 unsigned int bytes_read, extended_len;
20746 unsigned char op_code, extended_op;
20747 CORE_ADDR baseaddr;
20748 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20749 bfd *abfd = objfile->obfd;
20750 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20751 /* True if we're recording line info (as opposed to building partial
20752 symtabs and just interested in finding include files mentioned by
20753 the line number program). */
20754 bool record_lines_p = !decode_for_pst_p;
20755
20756 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20757
20758 line_ptr = lh->statement_program_start;
20759 line_end = lh->statement_program_end;
20760
20761 /* Read the statement sequences until there's nothing left. */
20762 while (line_ptr < line_end)
20763 {
20764 /* The DWARF line number program state machine. Reset the state
20765 machine at the start of each sequence. */
20766 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20767 bool end_sequence = false;
20768
20769 if (record_lines_p)
20770 {
20771 /* Start a subfile for the current file of the state
20772 machine. */
20773 const file_entry *fe = state_machine.current_file ();
20774
20775 if (fe != NULL)
20776 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20777 }
20778
20779 /* Decode the table. */
20780 while (line_ptr < line_end && !end_sequence)
20781 {
20782 op_code = read_1_byte (abfd, line_ptr);
20783 line_ptr += 1;
20784
20785 if (op_code >= lh->opcode_base)
20786 {
20787 /* Special opcode. */
20788 state_machine.handle_special_opcode (op_code);
20789 }
20790 else switch (op_code)
20791 {
20792 case DW_LNS_extended_op:
20793 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20794 &bytes_read);
20795 line_ptr += bytes_read;
20796 extended_end = line_ptr + extended_len;
20797 extended_op = read_1_byte (abfd, line_ptr);
20798 line_ptr += 1;
20799 switch (extended_op)
20800 {
20801 case DW_LNE_end_sequence:
20802 state_machine.handle_end_sequence ();
20803 end_sequence = true;
20804 break;
20805 case DW_LNE_set_address:
20806 {
20807 CORE_ADDR address
20808 = read_address (abfd, line_ptr, cu, &bytes_read);
20809 line_ptr += bytes_read;
20810
20811 state_machine.check_line_address (cu, line_ptr,
20812 lowpc - baseaddr, address);
20813 state_machine.handle_set_address (baseaddr, address);
20814 }
20815 break;
20816 case DW_LNE_define_file:
20817 {
20818 const char *cur_file;
20819 unsigned int mod_time, length;
20820 dir_index dindex;
20821
20822 cur_file = read_direct_string (abfd, line_ptr,
20823 &bytes_read);
20824 line_ptr += bytes_read;
20825 dindex = (dir_index)
20826 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20827 line_ptr += bytes_read;
20828 mod_time =
20829 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20830 line_ptr += bytes_read;
20831 length =
20832 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20833 line_ptr += bytes_read;
20834 lh->add_file_name (cur_file, dindex, mod_time, length);
20835 }
20836 break;
20837 case DW_LNE_set_discriminator:
20838 {
20839 /* The discriminator is not interesting to the
20840 debugger; just ignore it. We still need to
20841 check its value though:
20842 if there are consecutive entries for the same
20843 (non-prologue) line we want to coalesce them.
20844 PR 17276. */
20845 unsigned int discr
20846 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20847 line_ptr += bytes_read;
20848
20849 state_machine.handle_set_discriminator (discr);
20850 }
20851 break;
20852 default:
20853 complaint (_("mangled .debug_line section"));
20854 return;
20855 }
20856 /* Make sure that we parsed the extended op correctly. If e.g.
20857 we expected a different address size than the producer used,
20858 we may have read the wrong number of bytes. */
20859 if (line_ptr != extended_end)
20860 {
20861 complaint (_("mangled .debug_line section"));
20862 return;
20863 }
20864 break;
20865 case DW_LNS_copy:
20866 state_machine.handle_copy ();
20867 break;
20868 case DW_LNS_advance_pc:
20869 {
20870 CORE_ADDR adjust
20871 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20872 line_ptr += bytes_read;
20873
20874 state_machine.handle_advance_pc (adjust);
20875 }
20876 break;
20877 case DW_LNS_advance_line:
20878 {
20879 int line_delta
20880 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20881 line_ptr += bytes_read;
20882
20883 state_machine.handle_advance_line (line_delta);
20884 }
20885 break;
20886 case DW_LNS_set_file:
20887 {
20888 file_name_index file
20889 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20890 &bytes_read);
20891 line_ptr += bytes_read;
20892
20893 state_machine.handle_set_file (file);
20894 }
20895 break;
20896 case DW_LNS_set_column:
20897 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20898 line_ptr += bytes_read;
20899 break;
20900 case DW_LNS_negate_stmt:
20901 state_machine.handle_negate_stmt ();
20902 break;
20903 case DW_LNS_set_basic_block:
20904 break;
20905 /* Add to the address register of the state machine the
20906 address increment value corresponding to special opcode
20907 255. I.e., this value is scaled by the minimum
20908 instruction length since special opcode 255 would have
20909 scaled the increment. */
20910 case DW_LNS_const_add_pc:
20911 state_machine.handle_const_add_pc ();
20912 break;
20913 case DW_LNS_fixed_advance_pc:
20914 {
20915 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20916 line_ptr += 2;
20917
20918 state_machine.handle_fixed_advance_pc (addr_adj);
20919 }
20920 break;
20921 default:
20922 {
20923 /* Unknown standard opcode, ignore it. */
20924 int i;
20925
20926 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20927 {
20928 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20929 line_ptr += bytes_read;
20930 }
20931 }
20932 }
20933 }
20934
20935 if (!end_sequence)
20936 dwarf2_debug_line_missing_end_sequence_complaint ();
20937
20938 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20939 in which case we still finish recording the last line). */
20940 state_machine.record_line (true);
20941 }
20942 }
20943
20944 /* Decode the Line Number Program (LNP) for the given line_header
20945 structure and CU. The actual information extracted and the type
20946 of structures created from the LNP depends on the value of PST.
20947
20948 1. If PST is NULL, then this procedure uses the data from the program
20949 to create all necessary symbol tables, and their linetables.
20950
20951 2. If PST is not NULL, this procedure reads the program to determine
20952 the list of files included by the unit represented by PST, and
20953 builds all the associated partial symbol tables.
20954
20955 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20956 It is used for relative paths in the line table.
20957 NOTE: When processing partial symtabs (pst != NULL),
20958 comp_dir == pst->dirname.
20959
20960 NOTE: It is important that psymtabs have the same file name (via strcmp)
20961 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20962 symtab we don't use it in the name of the psymtabs we create.
20963 E.g. expand_line_sal requires this when finding psymtabs to expand.
20964 A good testcase for this is mb-inline.exp.
20965
20966 LOWPC is the lowest address in CU (or 0 if not known).
20967
20968 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20969 for its PC<->lines mapping information. Otherwise only the filename
20970 table is read in. */
20971
20972 static void
20973 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20974 struct dwarf2_cu *cu, struct partial_symtab *pst,
20975 CORE_ADDR lowpc, int decode_mapping)
20976 {
20977 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20978 const int decode_for_pst_p = (pst != NULL);
20979
20980 if (decode_mapping)
20981 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20982
20983 if (decode_for_pst_p)
20984 {
20985 int file_index;
20986
20987 /* Now that we're done scanning the Line Header Program, we can
20988 create the psymtab of each included file. */
20989 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
20990 if (lh->file_names[file_index].included_p == 1)
20991 {
20992 gdb::unique_xmalloc_ptr<char> name_holder;
20993 const char *include_name =
20994 psymtab_include_file_name (lh, file_index, pst, comp_dir,
20995 &name_holder);
20996 if (include_name != NULL)
20997 dwarf2_create_include_psymtab (include_name, pst, objfile);
20998 }
20999 }
21000 else
21001 {
21002 /* Make sure a symtab is created for every file, even files
21003 which contain only variables (i.e. no code with associated
21004 line numbers). */
21005 struct compunit_symtab *cust = cu->builder->get_compunit_symtab ();
21006 int i;
21007
21008 for (i = 0; i < lh->file_names.size (); i++)
21009 {
21010 file_entry &fe = lh->file_names[i];
21011
21012 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21013
21014 if (cu->builder->get_current_subfile ()->symtab == NULL)
21015 {
21016 cu->builder->get_current_subfile ()->symtab
21017 = allocate_symtab (cust,
21018 cu->builder->get_current_subfile ()->name);
21019 }
21020 fe.symtab = cu->builder->get_current_subfile ()->symtab;
21021 }
21022 }
21023 }
21024
21025 /* Start a subfile for DWARF. FILENAME is the name of the file and
21026 DIRNAME the name of the source directory which contains FILENAME
21027 or NULL if not known.
21028 This routine tries to keep line numbers from identical absolute and
21029 relative file names in a common subfile.
21030
21031 Using the `list' example from the GDB testsuite, which resides in
21032 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21033 of /srcdir/list0.c yields the following debugging information for list0.c:
21034
21035 DW_AT_name: /srcdir/list0.c
21036 DW_AT_comp_dir: /compdir
21037 files.files[0].name: list0.h
21038 files.files[0].dir: /srcdir
21039 files.files[1].name: list0.c
21040 files.files[1].dir: /srcdir
21041
21042 The line number information for list0.c has to end up in a single
21043 subfile, so that `break /srcdir/list0.c:1' works as expected.
21044 start_subfile will ensure that this happens provided that we pass the
21045 concatenation of files.files[1].dir and files.files[1].name as the
21046 subfile's name. */
21047
21048 static void
21049 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21050 const char *dirname)
21051 {
21052 char *copy = NULL;
21053
21054 /* In order not to lose the line information directory,
21055 we concatenate it to the filename when it makes sense.
21056 Note that the Dwarf3 standard says (speaking of filenames in line
21057 information): ``The directory index is ignored for file names
21058 that represent full path names''. Thus ignoring dirname in the
21059 `else' branch below isn't an issue. */
21060
21061 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21062 {
21063 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21064 filename = copy;
21065 }
21066
21067 cu->builder->start_subfile (filename);
21068
21069 if (copy != NULL)
21070 xfree (copy);
21071 }
21072
21073 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21074 buildsym_compunit constructor. */
21075
21076 static struct compunit_symtab *
21077 dwarf2_start_symtab (struct dwarf2_cu *cu,
21078 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21079 {
21080 gdb_assert (cu->builder == nullptr);
21081
21082 cu->builder.reset (new struct buildsym_compunit
21083 (cu->per_cu->dwarf2_per_objfile->objfile,
21084 name, comp_dir, cu->language, low_pc));
21085
21086 cu->list_in_scope = cu->builder->get_file_symbols ();
21087
21088 cu->builder->record_debugformat ("DWARF 2");
21089 cu->builder->record_producer (cu->producer);
21090
21091 cu->processing_has_namespace_info = 0;
21092
21093 return cu->builder->get_compunit_symtab ();
21094 }
21095
21096 static void
21097 var_decode_location (struct attribute *attr, struct symbol *sym,
21098 struct dwarf2_cu *cu)
21099 {
21100 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21101 struct comp_unit_head *cu_header = &cu->header;
21102
21103 /* NOTE drow/2003-01-30: There used to be a comment and some special
21104 code here to turn a symbol with DW_AT_external and a
21105 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21106 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21107 with some versions of binutils) where shared libraries could have
21108 relocations against symbols in their debug information - the
21109 minimal symbol would have the right address, but the debug info
21110 would not. It's no longer necessary, because we will explicitly
21111 apply relocations when we read in the debug information now. */
21112
21113 /* A DW_AT_location attribute with no contents indicates that a
21114 variable has been optimized away. */
21115 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21116 {
21117 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21118 return;
21119 }
21120
21121 /* Handle one degenerate form of location expression specially, to
21122 preserve GDB's previous behavior when section offsets are
21123 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21124 then mark this symbol as LOC_STATIC. */
21125
21126 if (attr_form_is_block (attr)
21127 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21128 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21129 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21130 && (DW_BLOCK (attr)->size
21131 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21132 {
21133 unsigned int dummy;
21134
21135 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21136 SYMBOL_VALUE_ADDRESS (sym) =
21137 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21138 else
21139 SYMBOL_VALUE_ADDRESS (sym) =
21140 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21141 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21142 fixup_symbol_section (sym, objfile);
21143 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21144 SYMBOL_SECTION (sym));
21145 return;
21146 }
21147
21148 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21149 expression evaluator, and use LOC_COMPUTED only when necessary
21150 (i.e. when the value of a register or memory location is
21151 referenced, or a thread-local block, etc.). Then again, it might
21152 not be worthwhile. I'm assuming that it isn't unless performance
21153 or memory numbers show me otherwise. */
21154
21155 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21156
21157 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21158 cu->has_loclist = 1;
21159 }
21160
21161 /* Given a pointer to a DWARF information entry, figure out if we need
21162 to make a symbol table entry for it, and if so, create a new entry
21163 and return a pointer to it.
21164 If TYPE is NULL, determine symbol type from the die, otherwise
21165 used the passed type.
21166 If SPACE is not NULL, use it to hold the new symbol. If it is
21167 NULL, allocate a new symbol on the objfile's obstack. */
21168
21169 static struct symbol *
21170 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21171 struct symbol *space)
21172 {
21173 struct dwarf2_per_objfile *dwarf2_per_objfile
21174 = cu->per_cu->dwarf2_per_objfile;
21175 struct objfile *objfile = dwarf2_per_objfile->objfile;
21176 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21177 struct symbol *sym = NULL;
21178 const char *name;
21179 struct attribute *attr = NULL;
21180 struct attribute *attr2 = NULL;
21181 CORE_ADDR baseaddr;
21182 struct pending **list_to_add = NULL;
21183
21184 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21185
21186 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21187
21188 name = dwarf2_name (die, cu);
21189 if (name)
21190 {
21191 const char *linkagename;
21192 int suppress_add = 0;
21193
21194 if (space)
21195 sym = space;
21196 else
21197 sym = allocate_symbol (objfile);
21198 OBJSTAT (objfile, n_syms++);
21199
21200 /* Cache this symbol's name and the name's demangled form (if any). */
21201 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21202 linkagename = dwarf2_physname (name, die, cu);
21203 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21204
21205 /* Fortran does not have mangling standard and the mangling does differ
21206 between gfortran, iFort etc. */
21207 if (cu->language == language_fortran
21208 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21209 symbol_set_demangled_name (&(sym->ginfo),
21210 dwarf2_full_name (name, die, cu),
21211 NULL);
21212
21213 /* Default assumptions.
21214 Use the passed type or decode it from the die. */
21215 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21216 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21217 if (type != NULL)
21218 SYMBOL_TYPE (sym) = type;
21219 else
21220 SYMBOL_TYPE (sym) = die_type (die, cu);
21221 attr = dwarf2_attr (die,
21222 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21223 cu);
21224 if (attr)
21225 {
21226 SYMBOL_LINE (sym) = DW_UNSND (attr);
21227 }
21228
21229 attr = dwarf2_attr (die,
21230 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21231 cu);
21232 if (attr)
21233 {
21234 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21235 struct file_entry *fe;
21236
21237 if (cu->line_header != NULL)
21238 fe = cu->line_header->file_name_at (file_index);
21239 else
21240 fe = NULL;
21241
21242 if (fe == NULL)
21243 complaint (_("file index out of range"));
21244 else
21245 symbol_set_symtab (sym, fe->symtab);
21246 }
21247
21248 switch (die->tag)
21249 {
21250 case DW_TAG_label:
21251 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21252 if (attr)
21253 {
21254 CORE_ADDR addr;
21255
21256 addr = attr_value_as_address (attr);
21257 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21258 SYMBOL_VALUE_ADDRESS (sym) = addr;
21259 }
21260 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21261 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21262 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21263 dw2_add_symbol_to_list (sym, cu->list_in_scope);
21264 break;
21265 case DW_TAG_subprogram:
21266 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21267 finish_block. */
21268 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21269 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21270 if ((attr2 && (DW_UNSND (attr2) != 0))
21271 || cu->language == language_ada)
21272 {
21273 /* Subprograms marked external are stored as a global symbol.
21274 Ada subprograms, whether marked external or not, are always
21275 stored as a global symbol, because we want to be able to
21276 access them globally. For instance, we want to be able
21277 to break on a nested subprogram without having to
21278 specify the context. */
21279 list_to_add = cu->builder->get_global_symbols ();
21280 }
21281 else
21282 {
21283 list_to_add = cu->list_in_scope;
21284 }
21285 break;
21286 case DW_TAG_inlined_subroutine:
21287 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21288 finish_block. */
21289 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21290 SYMBOL_INLINED (sym) = 1;
21291 list_to_add = cu->list_in_scope;
21292 break;
21293 case DW_TAG_template_value_param:
21294 suppress_add = 1;
21295 /* Fall through. */
21296 case DW_TAG_constant:
21297 case DW_TAG_variable:
21298 case DW_TAG_member:
21299 /* Compilation with minimal debug info may result in
21300 variables with missing type entries. Change the
21301 misleading `void' type to something sensible. */
21302 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21303 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21304
21305 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21306 /* In the case of DW_TAG_member, we should only be called for
21307 static const members. */
21308 if (die->tag == DW_TAG_member)
21309 {
21310 /* dwarf2_add_field uses die_is_declaration,
21311 so we do the same. */
21312 gdb_assert (die_is_declaration (die, cu));
21313 gdb_assert (attr);
21314 }
21315 if (attr)
21316 {
21317 dwarf2_const_value (attr, sym, cu);
21318 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21319 if (!suppress_add)
21320 {
21321 if (attr2 && (DW_UNSND (attr2) != 0))
21322 list_to_add = cu->builder->get_global_symbols ();
21323 else
21324 list_to_add = cu->list_in_scope;
21325 }
21326 break;
21327 }
21328 attr = dwarf2_attr (die, DW_AT_location, cu);
21329 if (attr)
21330 {
21331 var_decode_location (attr, sym, cu);
21332 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21333
21334 /* Fortran explicitly imports any global symbols to the local
21335 scope by DW_TAG_common_block. */
21336 if (cu->language == language_fortran && die->parent
21337 && die->parent->tag == DW_TAG_common_block)
21338 attr2 = NULL;
21339
21340 if (SYMBOL_CLASS (sym) == LOC_STATIC
21341 && SYMBOL_VALUE_ADDRESS (sym) == 0
21342 && !dwarf2_per_objfile->has_section_at_zero)
21343 {
21344 /* When a static variable is eliminated by the linker,
21345 the corresponding debug information is not stripped
21346 out, but the variable address is set to null;
21347 do not add such variables into symbol table. */
21348 }
21349 else if (attr2 && (DW_UNSND (attr2) != 0))
21350 {
21351 /* Workaround gfortran PR debug/40040 - it uses
21352 DW_AT_location for variables in -fPIC libraries which may
21353 get overriden by other libraries/executable and get
21354 a different address. Resolve it by the minimal symbol
21355 which may come from inferior's executable using copy
21356 relocation. Make this workaround only for gfortran as for
21357 other compilers GDB cannot guess the minimal symbol
21358 Fortran mangling kind. */
21359 if (cu->language == language_fortran && die->parent
21360 && die->parent->tag == DW_TAG_module
21361 && cu->producer
21362 && startswith (cu->producer, "GNU Fortran"))
21363 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21364
21365 /* A variable with DW_AT_external is never static,
21366 but it may be block-scoped. */
21367 list_to_add
21368 = (cu->list_in_scope == cu->builder->get_file_symbols ()
21369 ? cu->builder->get_global_symbols ()
21370 : cu->list_in_scope);
21371 }
21372 else
21373 list_to_add = cu->list_in_scope;
21374 }
21375 else
21376 {
21377 /* We do not know the address of this symbol.
21378 If it is an external symbol and we have type information
21379 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21380 The address of the variable will then be determined from
21381 the minimal symbol table whenever the variable is
21382 referenced. */
21383 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21384
21385 /* Fortran explicitly imports any global symbols to the local
21386 scope by DW_TAG_common_block. */
21387 if (cu->language == language_fortran && die->parent
21388 && die->parent->tag == DW_TAG_common_block)
21389 {
21390 /* SYMBOL_CLASS doesn't matter here because
21391 read_common_block is going to reset it. */
21392 if (!suppress_add)
21393 list_to_add = cu->list_in_scope;
21394 }
21395 else if (attr2 && (DW_UNSND (attr2) != 0)
21396 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21397 {
21398 /* A variable with DW_AT_external is never static, but it
21399 may be block-scoped. */
21400 list_to_add
21401 = (cu->list_in_scope == cu->builder->get_file_symbols ()
21402 ? cu->builder->get_global_symbols ()
21403 : cu->list_in_scope);
21404
21405 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21406 }
21407 else if (!die_is_declaration (die, cu))
21408 {
21409 /* Use the default LOC_OPTIMIZED_OUT class. */
21410 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21411 if (!suppress_add)
21412 list_to_add = cu->list_in_scope;
21413 }
21414 }
21415 break;
21416 case DW_TAG_formal_parameter:
21417 {
21418 /* If we are inside a function, mark this as an argument. If
21419 not, we might be looking at an argument to an inlined function
21420 when we do not have enough information to show inlined frames;
21421 pretend it's a local variable in that case so that the user can
21422 still see it. */
21423 struct context_stack *curr
21424 = cu->builder->get_current_context_stack ();
21425 if (curr != nullptr && curr->name != nullptr)
21426 SYMBOL_IS_ARGUMENT (sym) = 1;
21427 attr = dwarf2_attr (die, DW_AT_location, cu);
21428 if (attr)
21429 {
21430 var_decode_location (attr, sym, cu);
21431 }
21432 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21433 if (attr)
21434 {
21435 dwarf2_const_value (attr, sym, cu);
21436 }
21437
21438 list_to_add = cu->list_in_scope;
21439 }
21440 break;
21441 case DW_TAG_unspecified_parameters:
21442 /* From varargs functions; gdb doesn't seem to have any
21443 interest in this information, so just ignore it for now.
21444 (FIXME?) */
21445 break;
21446 case DW_TAG_template_type_param:
21447 suppress_add = 1;
21448 /* Fall through. */
21449 case DW_TAG_class_type:
21450 case DW_TAG_interface_type:
21451 case DW_TAG_structure_type:
21452 case DW_TAG_union_type:
21453 case DW_TAG_set_type:
21454 case DW_TAG_enumeration_type:
21455 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21456 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21457
21458 {
21459 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21460 really ever be static objects: otherwise, if you try
21461 to, say, break of a class's method and you're in a file
21462 which doesn't mention that class, it won't work unless
21463 the check for all static symbols in lookup_symbol_aux
21464 saves you. See the OtherFileClass tests in
21465 gdb.c++/namespace.exp. */
21466
21467 if (!suppress_add)
21468 {
21469 list_to_add
21470 = (cu->list_in_scope == cu->builder->get_file_symbols ()
21471 && cu->language == language_cplus
21472 ? cu->builder->get_global_symbols ()
21473 : cu->list_in_scope);
21474
21475 /* The semantics of C++ state that "struct foo {
21476 ... }" also defines a typedef for "foo". */
21477 if (cu->language == language_cplus
21478 || cu->language == language_ada
21479 || cu->language == language_d
21480 || cu->language == language_rust)
21481 {
21482 /* The symbol's name is already allocated along
21483 with this objfile, so we don't need to
21484 duplicate it for the type. */
21485 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21486 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21487 }
21488 }
21489 }
21490 break;
21491 case DW_TAG_typedef:
21492 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21493 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21494 list_to_add = cu->list_in_scope;
21495 break;
21496 case DW_TAG_base_type:
21497 case DW_TAG_subrange_type:
21498 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21499 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21500 list_to_add = cu->list_in_scope;
21501 break;
21502 case DW_TAG_enumerator:
21503 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21504 if (attr)
21505 {
21506 dwarf2_const_value (attr, sym, cu);
21507 }
21508 {
21509 /* NOTE: carlton/2003-11-10: See comment above in the
21510 DW_TAG_class_type, etc. block. */
21511
21512 list_to_add
21513 = (cu->list_in_scope == cu->builder->get_file_symbols ()
21514 && cu->language == language_cplus
21515 ? cu->builder->get_global_symbols ()
21516 : cu->list_in_scope);
21517 }
21518 break;
21519 case DW_TAG_imported_declaration:
21520 case DW_TAG_namespace:
21521 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21522 list_to_add = cu->builder->get_global_symbols ();
21523 break;
21524 case DW_TAG_module:
21525 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21526 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21527 list_to_add = cu->builder->get_global_symbols ();
21528 break;
21529 case DW_TAG_common_block:
21530 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21531 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21532 dw2_add_symbol_to_list (sym, cu->list_in_scope);
21533 break;
21534 default:
21535 /* Not a tag we recognize. Hopefully we aren't processing
21536 trash data, but since we must specifically ignore things
21537 we don't recognize, there is nothing else we should do at
21538 this point. */
21539 complaint (_("unsupported tag: '%s'"),
21540 dwarf_tag_name (die->tag));
21541 break;
21542 }
21543
21544 if (suppress_add)
21545 {
21546 sym->hash_next = objfile->template_symbols;
21547 objfile->template_symbols = sym;
21548 list_to_add = NULL;
21549 }
21550
21551 if (list_to_add != NULL)
21552 dw2_add_symbol_to_list (sym, list_to_add);
21553
21554 /* For the benefit of old versions of GCC, check for anonymous
21555 namespaces based on the demangled name. */
21556 if (!cu->processing_has_namespace_info
21557 && cu->language == language_cplus)
21558 cp_scan_for_anonymous_namespaces (cu->builder.get (), sym, objfile);
21559 }
21560 return (sym);
21561 }
21562
21563 /* Given an attr with a DW_FORM_dataN value in host byte order,
21564 zero-extend it as appropriate for the symbol's type. The DWARF
21565 standard (v4) is not entirely clear about the meaning of using
21566 DW_FORM_dataN for a constant with a signed type, where the type is
21567 wider than the data. The conclusion of a discussion on the DWARF
21568 list was that this is unspecified. We choose to always zero-extend
21569 because that is the interpretation long in use by GCC. */
21570
21571 static gdb_byte *
21572 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21573 struct dwarf2_cu *cu, LONGEST *value, int bits)
21574 {
21575 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21576 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21577 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21578 LONGEST l = DW_UNSND (attr);
21579
21580 if (bits < sizeof (*value) * 8)
21581 {
21582 l &= ((LONGEST) 1 << bits) - 1;
21583 *value = l;
21584 }
21585 else if (bits == sizeof (*value) * 8)
21586 *value = l;
21587 else
21588 {
21589 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21590 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21591 return bytes;
21592 }
21593
21594 return NULL;
21595 }
21596
21597 /* Read a constant value from an attribute. Either set *VALUE, or if
21598 the value does not fit in *VALUE, set *BYTES - either already
21599 allocated on the objfile obstack, or newly allocated on OBSTACK,
21600 or, set *BATON, if we translated the constant to a location
21601 expression. */
21602
21603 static void
21604 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21605 const char *name, struct obstack *obstack,
21606 struct dwarf2_cu *cu,
21607 LONGEST *value, const gdb_byte **bytes,
21608 struct dwarf2_locexpr_baton **baton)
21609 {
21610 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21611 struct comp_unit_head *cu_header = &cu->header;
21612 struct dwarf_block *blk;
21613 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21614 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21615
21616 *value = 0;
21617 *bytes = NULL;
21618 *baton = NULL;
21619
21620 switch (attr->form)
21621 {
21622 case DW_FORM_addr:
21623 case DW_FORM_GNU_addr_index:
21624 {
21625 gdb_byte *data;
21626
21627 if (TYPE_LENGTH (type) != cu_header->addr_size)
21628 dwarf2_const_value_length_mismatch_complaint (name,
21629 cu_header->addr_size,
21630 TYPE_LENGTH (type));
21631 /* Symbols of this form are reasonably rare, so we just
21632 piggyback on the existing location code rather than writing
21633 a new implementation of symbol_computed_ops. */
21634 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21635 (*baton)->per_cu = cu->per_cu;
21636 gdb_assert ((*baton)->per_cu);
21637
21638 (*baton)->size = 2 + cu_header->addr_size;
21639 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21640 (*baton)->data = data;
21641
21642 data[0] = DW_OP_addr;
21643 store_unsigned_integer (&data[1], cu_header->addr_size,
21644 byte_order, DW_ADDR (attr));
21645 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21646 }
21647 break;
21648 case DW_FORM_string:
21649 case DW_FORM_strp:
21650 case DW_FORM_GNU_str_index:
21651 case DW_FORM_GNU_strp_alt:
21652 /* DW_STRING is already allocated on the objfile obstack, point
21653 directly to it. */
21654 *bytes = (const gdb_byte *) DW_STRING (attr);
21655 break;
21656 case DW_FORM_block1:
21657 case DW_FORM_block2:
21658 case DW_FORM_block4:
21659 case DW_FORM_block:
21660 case DW_FORM_exprloc:
21661 case DW_FORM_data16:
21662 blk = DW_BLOCK (attr);
21663 if (TYPE_LENGTH (type) != blk->size)
21664 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21665 TYPE_LENGTH (type));
21666 *bytes = blk->data;
21667 break;
21668
21669 /* The DW_AT_const_value attributes are supposed to carry the
21670 symbol's value "represented as it would be on the target
21671 architecture." By the time we get here, it's already been
21672 converted to host endianness, so we just need to sign- or
21673 zero-extend it as appropriate. */
21674 case DW_FORM_data1:
21675 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21676 break;
21677 case DW_FORM_data2:
21678 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21679 break;
21680 case DW_FORM_data4:
21681 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21682 break;
21683 case DW_FORM_data8:
21684 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21685 break;
21686
21687 case DW_FORM_sdata:
21688 case DW_FORM_implicit_const:
21689 *value = DW_SND (attr);
21690 break;
21691
21692 case DW_FORM_udata:
21693 *value = DW_UNSND (attr);
21694 break;
21695
21696 default:
21697 complaint (_("unsupported const value attribute form: '%s'"),
21698 dwarf_form_name (attr->form));
21699 *value = 0;
21700 break;
21701 }
21702 }
21703
21704
21705 /* Copy constant value from an attribute to a symbol. */
21706
21707 static void
21708 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21709 struct dwarf2_cu *cu)
21710 {
21711 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21712 LONGEST value;
21713 const gdb_byte *bytes;
21714 struct dwarf2_locexpr_baton *baton;
21715
21716 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21717 SYMBOL_PRINT_NAME (sym),
21718 &objfile->objfile_obstack, cu,
21719 &value, &bytes, &baton);
21720
21721 if (baton != NULL)
21722 {
21723 SYMBOL_LOCATION_BATON (sym) = baton;
21724 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21725 }
21726 else if (bytes != NULL)
21727 {
21728 SYMBOL_VALUE_BYTES (sym) = bytes;
21729 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21730 }
21731 else
21732 {
21733 SYMBOL_VALUE (sym) = value;
21734 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21735 }
21736 }
21737
21738 /* Return the type of the die in question using its DW_AT_type attribute. */
21739
21740 static struct type *
21741 die_type (struct die_info *die, struct dwarf2_cu *cu)
21742 {
21743 struct attribute *type_attr;
21744
21745 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21746 if (!type_attr)
21747 {
21748 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21749 /* A missing DW_AT_type represents a void type. */
21750 return objfile_type (objfile)->builtin_void;
21751 }
21752
21753 return lookup_die_type (die, type_attr, cu);
21754 }
21755
21756 /* True iff CU's producer generates GNAT Ada auxiliary information
21757 that allows to find parallel types through that information instead
21758 of having to do expensive parallel lookups by type name. */
21759
21760 static int
21761 need_gnat_info (struct dwarf2_cu *cu)
21762 {
21763 /* Assume that the Ada compiler was GNAT, which always produces
21764 the auxiliary information. */
21765 return (cu->language == language_ada);
21766 }
21767
21768 /* Return the auxiliary type of the die in question using its
21769 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21770 attribute is not present. */
21771
21772 static struct type *
21773 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21774 {
21775 struct attribute *type_attr;
21776
21777 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21778 if (!type_attr)
21779 return NULL;
21780
21781 return lookup_die_type (die, type_attr, cu);
21782 }
21783
21784 /* If DIE has a descriptive_type attribute, then set the TYPE's
21785 descriptive type accordingly. */
21786
21787 static void
21788 set_descriptive_type (struct type *type, struct die_info *die,
21789 struct dwarf2_cu *cu)
21790 {
21791 struct type *descriptive_type = die_descriptive_type (die, cu);
21792
21793 if (descriptive_type)
21794 {
21795 ALLOCATE_GNAT_AUX_TYPE (type);
21796 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21797 }
21798 }
21799
21800 /* Return the containing type of the die in question using its
21801 DW_AT_containing_type attribute. */
21802
21803 static struct type *
21804 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21805 {
21806 struct attribute *type_attr;
21807 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21808
21809 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21810 if (!type_attr)
21811 error (_("Dwarf Error: Problem turning containing type into gdb type "
21812 "[in module %s]"), objfile_name (objfile));
21813
21814 return lookup_die_type (die, type_attr, cu);
21815 }
21816
21817 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21818
21819 static struct type *
21820 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21821 {
21822 struct dwarf2_per_objfile *dwarf2_per_objfile
21823 = cu->per_cu->dwarf2_per_objfile;
21824 struct objfile *objfile = dwarf2_per_objfile->objfile;
21825 char *message, *saved;
21826
21827 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
21828 objfile_name (objfile),
21829 sect_offset_str (cu->header.sect_off),
21830 sect_offset_str (die->sect_off));
21831 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21832 message, strlen (message));
21833 xfree (message);
21834
21835 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21836 }
21837
21838 /* Look up the type of DIE in CU using its type attribute ATTR.
21839 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21840 DW_AT_containing_type.
21841 If there is no type substitute an error marker. */
21842
21843 static struct type *
21844 lookup_die_type (struct die_info *die, const struct attribute *attr,
21845 struct dwarf2_cu *cu)
21846 {
21847 struct dwarf2_per_objfile *dwarf2_per_objfile
21848 = cu->per_cu->dwarf2_per_objfile;
21849 struct objfile *objfile = dwarf2_per_objfile->objfile;
21850 struct type *this_type;
21851
21852 gdb_assert (attr->name == DW_AT_type
21853 || attr->name == DW_AT_GNAT_descriptive_type
21854 || attr->name == DW_AT_containing_type);
21855
21856 /* First see if we have it cached. */
21857
21858 if (attr->form == DW_FORM_GNU_ref_alt)
21859 {
21860 struct dwarf2_per_cu_data *per_cu;
21861 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21862
21863 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21864 dwarf2_per_objfile);
21865 this_type = get_die_type_at_offset (sect_off, per_cu);
21866 }
21867 else if (attr_form_is_ref (attr))
21868 {
21869 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21870
21871 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21872 }
21873 else if (attr->form == DW_FORM_ref_sig8)
21874 {
21875 ULONGEST signature = DW_SIGNATURE (attr);
21876
21877 return get_signatured_type (die, signature, cu);
21878 }
21879 else
21880 {
21881 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21882 " at %s [in module %s]"),
21883 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21884 objfile_name (objfile));
21885 return build_error_marker_type (cu, die);
21886 }
21887
21888 /* If not cached we need to read it in. */
21889
21890 if (this_type == NULL)
21891 {
21892 struct die_info *type_die = NULL;
21893 struct dwarf2_cu *type_cu = cu;
21894
21895 if (attr_form_is_ref (attr))
21896 type_die = follow_die_ref (die, attr, &type_cu);
21897 if (type_die == NULL)
21898 return build_error_marker_type (cu, die);
21899 /* If we find the type now, it's probably because the type came
21900 from an inter-CU reference and the type's CU got expanded before
21901 ours. */
21902 this_type = read_type_die (type_die, type_cu);
21903 }
21904
21905 /* If we still don't have a type use an error marker. */
21906
21907 if (this_type == NULL)
21908 return build_error_marker_type (cu, die);
21909
21910 return this_type;
21911 }
21912
21913 /* Return the type in DIE, CU.
21914 Returns NULL for invalid types.
21915
21916 This first does a lookup in die_type_hash,
21917 and only reads the die in if necessary.
21918
21919 NOTE: This can be called when reading in partial or full symbols. */
21920
21921 static struct type *
21922 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21923 {
21924 struct type *this_type;
21925
21926 this_type = get_die_type (die, cu);
21927 if (this_type)
21928 return this_type;
21929
21930 return read_type_die_1 (die, cu);
21931 }
21932
21933 /* Read the type in DIE, CU.
21934 Returns NULL for invalid types. */
21935
21936 static struct type *
21937 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21938 {
21939 struct type *this_type = NULL;
21940
21941 switch (die->tag)
21942 {
21943 case DW_TAG_class_type:
21944 case DW_TAG_interface_type:
21945 case DW_TAG_structure_type:
21946 case DW_TAG_union_type:
21947 this_type = read_structure_type (die, cu);
21948 break;
21949 case DW_TAG_enumeration_type:
21950 this_type = read_enumeration_type (die, cu);
21951 break;
21952 case DW_TAG_subprogram:
21953 case DW_TAG_subroutine_type:
21954 case DW_TAG_inlined_subroutine:
21955 this_type = read_subroutine_type (die, cu);
21956 break;
21957 case DW_TAG_array_type:
21958 this_type = read_array_type (die, cu);
21959 break;
21960 case DW_TAG_set_type:
21961 this_type = read_set_type (die, cu);
21962 break;
21963 case DW_TAG_pointer_type:
21964 this_type = read_tag_pointer_type (die, cu);
21965 break;
21966 case DW_TAG_ptr_to_member_type:
21967 this_type = read_tag_ptr_to_member_type (die, cu);
21968 break;
21969 case DW_TAG_reference_type:
21970 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21971 break;
21972 case DW_TAG_rvalue_reference_type:
21973 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21974 break;
21975 case DW_TAG_const_type:
21976 this_type = read_tag_const_type (die, cu);
21977 break;
21978 case DW_TAG_volatile_type:
21979 this_type = read_tag_volatile_type (die, cu);
21980 break;
21981 case DW_TAG_restrict_type:
21982 this_type = read_tag_restrict_type (die, cu);
21983 break;
21984 case DW_TAG_string_type:
21985 this_type = read_tag_string_type (die, cu);
21986 break;
21987 case DW_TAG_typedef:
21988 this_type = read_typedef (die, cu);
21989 break;
21990 case DW_TAG_subrange_type:
21991 this_type = read_subrange_type (die, cu);
21992 break;
21993 case DW_TAG_base_type:
21994 this_type = read_base_type (die, cu);
21995 break;
21996 case DW_TAG_unspecified_type:
21997 this_type = read_unspecified_type (die, cu);
21998 break;
21999 case DW_TAG_namespace:
22000 this_type = read_namespace_type (die, cu);
22001 break;
22002 case DW_TAG_module:
22003 this_type = read_module_type (die, cu);
22004 break;
22005 case DW_TAG_atomic_type:
22006 this_type = read_tag_atomic_type (die, cu);
22007 break;
22008 default:
22009 complaint (_("unexpected tag in read_type_die: '%s'"),
22010 dwarf_tag_name (die->tag));
22011 break;
22012 }
22013
22014 return this_type;
22015 }
22016
22017 /* See if we can figure out if the class lives in a namespace. We do
22018 this by looking for a member function; its demangled name will
22019 contain namespace info, if there is any.
22020 Return the computed name or NULL.
22021 Space for the result is allocated on the objfile's obstack.
22022 This is the full-die version of guess_partial_die_structure_name.
22023 In this case we know DIE has no useful parent. */
22024
22025 static char *
22026 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22027 {
22028 struct die_info *spec_die;
22029 struct dwarf2_cu *spec_cu;
22030 struct die_info *child;
22031 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22032
22033 spec_cu = cu;
22034 spec_die = die_specification (die, &spec_cu);
22035 if (spec_die != NULL)
22036 {
22037 die = spec_die;
22038 cu = spec_cu;
22039 }
22040
22041 for (child = die->child;
22042 child != NULL;
22043 child = child->sibling)
22044 {
22045 if (child->tag == DW_TAG_subprogram)
22046 {
22047 const char *linkage_name = dw2_linkage_name (child, cu);
22048
22049 if (linkage_name != NULL)
22050 {
22051 char *actual_name
22052 = language_class_name_from_physname (cu->language_defn,
22053 linkage_name);
22054 char *name = NULL;
22055
22056 if (actual_name != NULL)
22057 {
22058 const char *die_name = dwarf2_name (die, cu);
22059
22060 if (die_name != NULL
22061 && strcmp (die_name, actual_name) != 0)
22062 {
22063 /* Strip off the class name from the full name.
22064 We want the prefix. */
22065 int die_name_len = strlen (die_name);
22066 int actual_name_len = strlen (actual_name);
22067
22068 /* Test for '::' as a sanity check. */
22069 if (actual_name_len > die_name_len + 2
22070 && actual_name[actual_name_len
22071 - die_name_len - 1] == ':')
22072 name = (char *) obstack_copy0 (
22073 &objfile->per_bfd->storage_obstack,
22074 actual_name, actual_name_len - die_name_len - 2);
22075 }
22076 }
22077 xfree (actual_name);
22078 return name;
22079 }
22080 }
22081 }
22082
22083 return NULL;
22084 }
22085
22086 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22087 prefix part in such case. See
22088 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22089
22090 static const char *
22091 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22092 {
22093 struct attribute *attr;
22094 const char *base;
22095
22096 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22097 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22098 return NULL;
22099
22100 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22101 return NULL;
22102
22103 attr = dw2_linkage_name_attr (die, cu);
22104 if (attr == NULL || DW_STRING (attr) == NULL)
22105 return NULL;
22106
22107 /* dwarf2_name had to be already called. */
22108 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22109
22110 /* Strip the base name, keep any leading namespaces/classes. */
22111 base = strrchr (DW_STRING (attr), ':');
22112 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22113 return "";
22114
22115 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22116 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22117 DW_STRING (attr),
22118 &base[-1] - DW_STRING (attr));
22119 }
22120
22121 /* Return the name of the namespace/class that DIE is defined within,
22122 or "" if we can't tell. The caller should not xfree the result.
22123
22124 For example, if we're within the method foo() in the following
22125 code:
22126
22127 namespace N {
22128 class C {
22129 void foo () {
22130 }
22131 };
22132 }
22133
22134 then determine_prefix on foo's die will return "N::C". */
22135
22136 static const char *
22137 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22138 {
22139 struct dwarf2_per_objfile *dwarf2_per_objfile
22140 = cu->per_cu->dwarf2_per_objfile;
22141 struct die_info *parent, *spec_die;
22142 struct dwarf2_cu *spec_cu;
22143 struct type *parent_type;
22144 const char *retval;
22145
22146 if (cu->language != language_cplus
22147 && cu->language != language_fortran && cu->language != language_d
22148 && cu->language != language_rust)
22149 return "";
22150
22151 retval = anonymous_struct_prefix (die, cu);
22152 if (retval)
22153 return retval;
22154
22155 /* We have to be careful in the presence of DW_AT_specification.
22156 For example, with GCC 3.4, given the code
22157
22158 namespace N {
22159 void foo() {
22160 // Definition of N::foo.
22161 }
22162 }
22163
22164 then we'll have a tree of DIEs like this:
22165
22166 1: DW_TAG_compile_unit
22167 2: DW_TAG_namespace // N
22168 3: DW_TAG_subprogram // declaration of N::foo
22169 4: DW_TAG_subprogram // definition of N::foo
22170 DW_AT_specification // refers to die #3
22171
22172 Thus, when processing die #4, we have to pretend that we're in
22173 the context of its DW_AT_specification, namely the contex of die
22174 #3. */
22175 spec_cu = cu;
22176 spec_die = die_specification (die, &spec_cu);
22177 if (spec_die == NULL)
22178 parent = die->parent;
22179 else
22180 {
22181 parent = spec_die->parent;
22182 cu = spec_cu;
22183 }
22184
22185 if (parent == NULL)
22186 return "";
22187 else if (parent->building_fullname)
22188 {
22189 const char *name;
22190 const char *parent_name;
22191
22192 /* It has been seen on RealView 2.2 built binaries,
22193 DW_TAG_template_type_param types actually _defined_ as
22194 children of the parent class:
22195
22196 enum E {};
22197 template class <class Enum> Class{};
22198 Class<enum E> class_e;
22199
22200 1: DW_TAG_class_type (Class)
22201 2: DW_TAG_enumeration_type (E)
22202 3: DW_TAG_enumerator (enum1:0)
22203 3: DW_TAG_enumerator (enum2:1)
22204 ...
22205 2: DW_TAG_template_type_param
22206 DW_AT_type DW_FORM_ref_udata (E)
22207
22208 Besides being broken debug info, it can put GDB into an
22209 infinite loop. Consider:
22210
22211 When we're building the full name for Class<E>, we'll start
22212 at Class, and go look over its template type parameters,
22213 finding E. We'll then try to build the full name of E, and
22214 reach here. We're now trying to build the full name of E,
22215 and look over the parent DIE for containing scope. In the
22216 broken case, if we followed the parent DIE of E, we'd again
22217 find Class, and once again go look at its template type
22218 arguments, etc., etc. Simply don't consider such parent die
22219 as source-level parent of this die (it can't be, the language
22220 doesn't allow it), and break the loop here. */
22221 name = dwarf2_name (die, cu);
22222 parent_name = dwarf2_name (parent, cu);
22223 complaint (_("template param type '%s' defined within parent '%s'"),
22224 name ? name : "<unknown>",
22225 parent_name ? parent_name : "<unknown>");
22226 return "";
22227 }
22228 else
22229 switch (parent->tag)
22230 {
22231 case DW_TAG_namespace:
22232 parent_type = read_type_die (parent, cu);
22233 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22234 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22235 Work around this problem here. */
22236 if (cu->language == language_cplus
22237 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22238 return "";
22239 /* We give a name to even anonymous namespaces. */
22240 return TYPE_NAME (parent_type);
22241 case DW_TAG_class_type:
22242 case DW_TAG_interface_type:
22243 case DW_TAG_structure_type:
22244 case DW_TAG_union_type:
22245 case DW_TAG_module:
22246 parent_type = read_type_die (parent, cu);
22247 if (TYPE_NAME (parent_type) != NULL)
22248 return TYPE_NAME (parent_type);
22249 else
22250 /* An anonymous structure is only allowed non-static data
22251 members; no typedefs, no member functions, et cetera.
22252 So it does not need a prefix. */
22253 return "";
22254 case DW_TAG_compile_unit:
22255 case DW_TAG_partial_unit:
22256 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22257 if (cu->language == language_cplus
22258 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22259 && die->child != NULL
22260 && (die->tag == DW_TAG_class_type
22261 || die->tag == DW_TAG_structure_type
22262 || die->tag == DW_TAG_union_type))
22263 {
22264 char *name = guess_full_die_structure_name (die, cu);
22265 if (name != NULL)
22266 return name;
22267 }
22268 return "";
22269 case DW_TAG_enumeration_type:
22270 parent_type = read_type_die (parent, cu);
22271 if (TYPE_DECLARED_CLASS (parent_type))
22272 {
22273 if (TYPE_NAME (parent_type) != NULL)
22274 return TYPE_NAME (parent_type);
22275 return "";
22276 }
22277 /* Fall through. */
22278 default:
22279 return determine_prefix (parent, cu);
22280 }
22281 }
22282
22283 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22284 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22285 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22286 an obconcat, otherwise allocate storage for the result. The CU argument is
22287 used to determine the language and hence, the appropriate separator. */
22288
22289 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22290
22291 static char *
22292 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22293 int physname, struct dwarf2_cu *cu)
22294 {
22295 const char *lead = "";
22296 const char *sep;
22297
22298 if (suffix == NULL || suffix[0] == '\0'
22299 || prefix == NULL || prefix[0] == '\0')
22300 sep = "";
22301 else if (cu->language == language_d)
22302 {
22303 /* For D, the 'main' function could be defined in any module, but it
22304 should never be prefixed. */
22305 if (strcmp (suffix, "D main") == 0)
22306 {
22307 prefix = "";
22308 sep = "";
22309 }
22310 else
22311 sep = ".";
22312 }
22313 else if (cu->language == language_fortran && physname)
22314 {
22315 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22316 DW_AT_MIPS_linkage_name is preferred and used instead. */
22317
22318 lead = "__";
22319 sep = "_MOD_";
22320 }
22321 else
22322 sep = "::";
22323
22324 if (prefix == NULL)
22325 prefix = "";
22326 if (suffix == NULL)
22327 suffix = "";
22328
22329 if (obs == NULL)
22330 {
22331 char *retval
22332 = ((char *)
22333 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22334
22335 strcpy (retval, lead);
22336 strcat (retval, prefix);
22337 strcat (retval, sep);
22338 strcat (retval, suffix);
22339 return retval;
22340 }
22341 else
22342 {
22343 /* We have an obstack. */
22344 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22345 }
22346 }
22347
22348 /* Return sibling of die, NULL if no sibling. */
22349
22350 static struct die_info *
22351 sibling_die (struct die_info *die)
22352 {
22353 return die->sibling;
22354 }
22355
22356 /* Get name of a die, return NULL if not found. */
22357
22358 static const char *
22359 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22360 struct obstack *obstack)
22361 {
22362 if (name && cu->language == language_cplus)
22363 {
22364 std::string canon_name = cp_canonicalize_string (name);
22365
22366 if (!canon_name.empty ())
22367 {
22368 if (canon_name != name)
22369 name = (const char *) obstack_copy0 (obstack,
22370 canon_name.c_str (),
22371 canon_name.length ());
22372 }
22373 }
22374
22375 return name;
22376 }
22377
22378 /* Get name of a die, return NULL if not found.
22379 Anonymous namespaces are converted to their magic string. */
22380
22381 static const char *
22382 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22383 {
22384 struct attribute *attr;
22385 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22386
22387 attr = dwarf2_attr (die, DW_AT_name, cu);
22388 if ((!attr || !DW_STRING (attr))
22389 && die->tag != DW_TAG_namespace
22390 && die->tag != DW_TAG_class_type
22391 && die->tag != DW_TAG_interface_type
22392 && die->tag != DW_TAG_structure_type
22393 && die->tag != DW_TAG_union_type)
22394 return NULL;
22395
22396 switch (die->tag)
22397 {
22398 case DW_TAG_compile_unit:
22399 case DW_TAG_partial_unit:
22400 /* Compilation units have a DW_AT_name that is a filename, not
22401 a source language identifier. */
22402 case DW_TAG_enumeration_type:
22403 case DW_TAG_enumerator:
22404 /* These tags always have simple identifiers already; no need
22405 to canonicalize them. */
22406 return DW_STRING (attr);
22407
22408 case DW_TAG_namespace:
22409 if (attr != NULL && DW_STRING (attr) != NULL)
22410 return DW_STRING (attr);
22411 return CP_ANONYMOUS_NAMESPACE_STR;
22412
22413 case DW_TAG_class_type:
22414 case DW_TAG_interface_type:
22415 case DW_TAG_structure_type:
22416 case DW_TAG_union_type:
22417 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22418 structures or unions. These were of the form "._%d" in GCC 4.1,
22419 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22420 and GCC 4.4. We work around this problem by ignoring these. */
22421 if (attr && DW_STRING (attr)
22422 && (startswith (DW_STRING (attr), "._")
22423 || startswith (DW_STRING (attr), "<anonymous")))
22424 return NULL;
22425
22426 /* GCC might emit a nameless typedef that has a linkage name. See
22427 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22428 if (!attr || DW_STRING (attr) == NULL)
22429 {
22430 char *demangled = NULL;
22431
22432 attr = dw2_linkage_name_attr (die, cu);
22433 if (attr == NULL || DW_STRING (attr) == NULL)
22434 return NULL;
22435
22436 /* Avoid demangling DW_STRING (attr) the second time on a second
22437 call for the same DIE. */
22438 if (!DW_STRING_IS_CANONICAL (attr))
22439 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22440
22441 if (demangled)
22442 {
22443 const char *base;
22444
22445 /* FIXME: we already did this for the partial symbol... */
22446 DW_STRING (attr)
22447 = ((const char *)
22448 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22449 demangled, strlen (demangled)));
22450 DW_STRING_IS_CANONICAL (attr) = 1;
22451 xfree (demangled);
22452
22453 /* Strip any leading namespaces/classes, keep only the base name.
22454 DW_AT_name for named DIEs does not contain the prefixes. */
22455 base = strrchr (DW_STRING (attr), ':');
22456 if (base && base > DW_STRING (attr) && base[-1] == ':')
22457 return &base[1];
22458 else
22459 return DW_STRING (attr);
22460 }
22461 }
22462 break;
22463
22464 default:
22465 break;
22466 }
22467
22468 if (!DW_STRING_IS_CANONICAL (attr))
22469 {
22470 DW_STRING (attr)
22471 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22472 &objfile->per_bfd->storage_obstack);
22473 DW_STRING_IS_CANONICAL (attr) = 1;
22474 }
22475 return DW_STRING (attr);
22476 }
22477
22478 /* Return the die that this die in an extension of, or NULL if there
22479 is none. *EXT_CU is the CU containing DIE on input, and the CU
22480 containing the return value on output. */
22481
22482 static struct die_info *
22483 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22484 {
22485 struct attribute *attr;
22486
22487 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22488 if (attr == NULL)
22489 return NULL;
22490
22491 return follow_die_ref (die, attr, ext_cu);
22492 }
22493
22494 /* Convert a DIE tag into its string name. */
22495
22496 static const char *
22497 dwarf_tag_name (unsigned tag)
22498 {
22499 const char *name = get_DW_TAG_name (tag);
22500
22501 if (name == NULL)
22502 return "DW_TAG_<unknown>";
22503
22504 return name;
22505 }
22506
22507 /* Convert a DWARF attribute code into its string name. */
22508
22509 static const char *
22510 dwarf_attr_name (unsigned attr)
22511 {
22512 const char *name;
22513
22514 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22515 if (attr == DW_AT_MIPS_fde)
22516 return "DW_AT_MIPS_fde";
22517 #else
22518 if (attr == DW_AT_HP_block_index)
22519 return "DW_AT_HP_block_index";
22520 #endif
22521
22522 name = get_DW_AT_name (attr);
22523
22524 if (name == NULL)
22525 return "DW_AT_<unknown>";
22526
22527 return name;
22528 }
22529
22530 /* Convert a DWARF value form code into its string name. */
22531
22532 static const char *
22533 dwarf_form_name (unsigned form)
22534 {
22535 const char *name = get_DW_FORM_name (form);
22536
22537 if (name == NULL)
22538 return "DW_FORM_<unknown>";
22539
22540 return name;
22541 }
22542
22543 static const char *
22544 dwarf_bool_name (unsigned mybool)
22545 {
22546 if (mybool)
22547 return "TRUE";
22548 else
22549 return "FALSE";
22550 }
22551
22552 /* Convert a DWARF type code into its string name. */
22553
22554 static const char *
22555 dwarf_type_encoding_name (unsigned enc)
22556 {
22557 const char *name = get_DW_ATE_name (enc);
22558
22559 if (name == NULL)
22560 return "DW_ATE_<unknown>";
22561
22562 return name;
22563 }
22564
22565 static void
22566 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22567 {
22568 unsigned int i;
22569
22570 print_spaces (indent, f);
22571 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22572 dwarf_tag_name (die->tag), die->abbrev,
22573 sect_offset_str (die->sect_off));
22574
22575 if (die->parent != NULL)
22576 {
22577 print_spaces (indent, f);
22578 fprintf_unfiltered (f, " parent at offset: %s\n",
22579 sect_offset_str (die->parent->sect_off));
22580 }
22581
22582 print_spaces (indent, f);
22583 fprintf_unfiltered (f, " has children: %s\n",
22584 dwarf_bool_name (die->child != NULL));
22585
22586 print_spaces (indent, f);
22587 fprintf_unfiltered (f, " attributes:\n");
22588
22589 for (i = 0; i < die->num_attrs; ++i)
22590 {
22591 print_spaces (indent, f);
22592 fprintf_unfiltered (f, " %s (%s) ",
22593 dwarf_attr_name (die->attrs[i].name),
22594 dwarf_form_name (die->attrs[i].form));
22595
22596 switch (die->attrs[i].form)
22597 {
22598 case DW_FORM_addr:
22599 case DW_FORM_GNU_addr_index:
22600 fprintf_unfiltered (f, "address: ");
22601 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22602 break;
22603 case DW_FORM_block2:
22604 case DW_FORM_block4:
22605 case DW_FORM_block:
22606 case DW_FORM_block1:
22607 fprintf_unfiltered (f, "block: size %s",
22608 pulongest (DW_BLOCK (&die->attrs[i])->size));
22609 break;
22610 case DW_FORM_exprloc:
22611 fprintf_unfiltered (f, "expression: size %s",
22612 pulongest (DW_BLOCK (&die->attrs[i])->size));
22613 break;
22614 case DW_FORM_data16:
22615 fprintf_unfiltered (f, "constant of 16 bytes");
22616 break;
22617 case DW_FORM_ref_addr:
22618 fprintf_unfiltered (f, "ref address: ");
22619 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22620 break;
22621 case DW_FORM_GNU_ref_alt:
22622 fprintf_unfiltered (f, "alt ref address: ");
22623 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22624 break;
22625 case DW_FORM_ref1:
22626 case DW_FORM_ref2:
22627 case DW_FORM_ref4:
22628 case DW_FORM_ref8:
22629 case DW_FORM_ref_udata:
22630 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22631 (long) (DW_UNSND (&die->attrs[i])));
22632 break;
22633 case DW_FORM_data1:
22634 case DW_FORM_data2:
22635 case DW_FORM_data4:
22636 case DW_FORM_data8:
22637 case DW_FORM_udata:
22638 case DW_FORM_sdata:
22639 fprintf_unfiltered (f, "constant: %s",
22640 pulongest (DW_UNSND (&die->attrs[i])));
22641 break;
22642 case DW_FORM_sec_offset:
22643 fprintf_unfiltered (f, "section offset: %s",
22644 pulongest (DW_UNSND (&die->attrs[i])));
22645 break;
22646 case DW_FORM_ref_sig8:
22647 fprintf_unfiltered (f, "signature: %s",
22648 hex_string (DW_SIGNATURE (&die->attrs[i])));
22649 break;
22650 case DW_FORM_string:
22651 case DW_FORM_strp:
22652 case DW_FORM_line_strp:
22653 case DW_FORM_GNU_str_index:
22654 case DW_FORM_GNU_strp_alt:
22655 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22656 DW_STRING (&die->attrs[i])
22657 ? DW_STRING (&die->attrs[i]) : "",
22658 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22659 break;
22660 case DW_FORM_flag:
22661 if (DW_UNSND (&die->attrs[i]))
22662 fprintf_unfiltered (f, "flag: TRUE");
22663 else
22664 fprintf_unfiltered (f, "flag: FALSE");
22665 break;
22666 case DW_FORM_flag_present:
22667 fprintf_unfiltered (f, "flag: TRUE");
22668 break;
22669 case DW_FORM_indirect:
22670 /* The reader will have reduced the indirect form to
22671 the "base form" so this form should not occur. */
22672 fprintf_unfiltered (f,
22673 "unexpected attribute form: DW_FORM_indirect");
22674 break;
22675 case DW_FORM_implicit_const:
22676 fprintf_unfiltered (f, "constant: %s",
22677 plongest (DW_SND (&die->attrs[i])));
22678 break;
22679 default:
22680 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22681 die->attrs[i].form);
22682 break;
22683 }
22684 fprintf_unfiltered (f, "\n");
22685 }
22686 }
22687
22688 static void
22689 dump_die_for_error (struct die_info *die)
22690 {
22691 dump_die_shallow (gdb_stderr, 0, die);
22692 }
22693
22694 static void
22695 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22696 {
22697 int indent = level * 4;
22698
22699 gdb_assert (die != NULL);
22700
22701 if (level >= max_level)
22702 return;
22703
22704 dump_die_shallow (f, indent, die);
22705
22706 if (die->child != NULL)
22707 {
22708 print_spaces (indent, f);
22709 fprintf_unfiltered (f, " Children:");
22710 if (level + 1 < max_level)
22711 {
22712 fprintf_unfiltered (f, "\n");
22713 dump_die_1 (f, level + 1, max_level, die->child);
22714 }
22715 else
22716 {
22717 fprintf_unfiltered (f,
22718 " [not printed, max nesting level reached]\n");
22719 }
22720 }
22721
22722 if (die->sibling != NULL && level > 0)
22723 {
22724 dump_die_1 (f, level, max_level, die->sibling);
22725 }
22726 }
22727
22728 /* This is called from the pdie macro in gdbinit.in.
22729 It's not static so gcc will keep a copy callable from gdb. */
22730
22731 void
22732 dump_die (struct die_info *die, int max_level)
22733 {
22734 dump_die_1 (gdb_stdlog, 0, max_level, die);
22735 }
22736
22737 static void
22738 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22739 {
22740 void **slot;
22741
22742 slot = htab_find_slot_with_hash (cu->die_hash, die,
22743 to_underlying (die->sect_off),
22744 INSERT);
22745
22746 *slot = die;
22747 }
22748
22749 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22750 required kind. */
22751
22752 static sect_offset
22753 dwarf2_get_ref_die_offset (const struct attribute *attr)
22754 {
22755 if (attr_form_is_ref (attr))
22756 return (sect_offset) DW_UNSND (attr);
22757
22758 complaint (_("unsupported die ref attribute form: '%s'"),
22759 dwarf_form_name (attr->form));
22760 return {};
22761 }
22762
22763 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22764 * the value held by the attribute is not constant. */
22765
22766 static LONGEST
22767 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22768 {
22769 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22770 return DW_SND (attr);
22771 else if (attr->form == DW_FORM_udata
22772 || attr->form == DW_FORM_data1
22773 || attr->form == DW_FORM_data2
22774 || attr->form == DW_FORM_data4
22775 || attr->form == DW_FORM_data8)
22776 return DW_UNSND (attr);
22777 else
22778 {
22779 /* For DW_FORM_data16 see attr_form_is_constant. */
22780 complaint (_("Attribute value is not a constant (%s)"),
22781 dwarf_form_name (attr->form));
22782 return default_value;
22783 }
22784 }
22785
22786 /* Follow reference or signature attribute ATTR of SRC_DIE.
22787 On entry *REF_CU is the CU of SRC_DIE.
22788 On exit *REF_CU is the CU of the result. */
22789
22790 static struct die_info *
22791 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22792 struct dwarf2_cu **ref_cu)
22793 {
22794 struct die_info *die;
22795
22796 if (attr_form_is_ref (attr))
22797 die = follow_die_ref (src_die, attr, ref_cu);
22798 else if (attr->form == DW_FORM_ref_sig8)
22799 die = follow_die_sig (src_die, attr, ref_cu);
22800 else
22801 {
22802 dump_die_for_error (src_die);
22803 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22804 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22805 }
22806
22807 return die;
22808 }
22809
22810 /* Follow reference OFFSET.
22811 On entry *REF_CU is the CU of the source die referencing OFFSET.
22812 On exit *REF_CU is the CU of the result.
22813 Returns NULL if OFFSET is invalid. */
22814
22815 static struct die_info *
22816 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22817 struct dwarf2_cu **ref_cu)
22818 {
22819 struct die_info temp_die;
22820 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22821 struct dwarf2_per_objfile *dwarf2_per_objfile
22822 = cu->per_cu->dwarf2_per_objfile;
22823
22824 gdb_assert (cu->per_cu != NULL);
22825
22826 target_cu = cu;
22827
22828 if (cu->per_cu->is_debug_types)
22829 {
22830 /* .debug_types CUs cannot reference anything outside their CU.
22831 If they need to, they have to reference a signatured type via
22832 DW_FORM_ref_sig8. */
22833 if (!offset_in_cu_p (&cu->header, sect_off))
22834 return NULL;
22835 }
22836 else if (offset_in_dwz != cu->per_cu->is_dwz
22837 || !offset_in_cu_p (&cu->header, sect_off))
22838 {
22839 struct dwarf2_per_cu_data *per_cu;
22840
22841 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22842 dwarf2_per_objfile);
22843
22844 /* If necessary, add it to the queue and load its DIEs. */
22845 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22846 load_full_comp_unit (per_cu, false, cu->language);
22847
22848 target_cu = per_cu->cu;
22849 }
22850 else if (cu->dies == NULL)
22851 {
22852 /* We're loading full DIEs during partial symbol reading. */
22853 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22854 load_full_comp_unit (cu->per_cu, false, language_minimal);
22855 }
22856
22857 *ref_cu = target_cu;
22858 temp_die.sect_off = sect_off;
22859 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22860 &temp_die,
22861 to_underlying (sect_off));
22862 }
22863
22864 /* Follow reference attribute ATTR of SRC_DIE.
22865 On entry *REF_CU is the CU of SRC_DIE.
22866 On exit *REF_CU is the CU of the result. */
22867
22868 static struct die_info *
22869 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22870 struct dwarf2_cu **ref_cu)
22871 {
22872 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22873 struct dwarf2_cu *cu = *ref_cu;
22874 struct die_info *die;
22875
22876 die = follow_die_offset (sect_off,
22877 (attr->form == DW_FORM_GNU_ref_alt
22878 || cu->per_cu->is_dwz),
22879 ref_cu);
22880 if (!die)
22881 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22882 "at %s [in module %s]"),
22883 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22884 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22885
22886 return die;
22887 }
22888
22889 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22890 Returned value is intended for DW_OP_call*. Returned
22891 dwarf2_locexpr_baton->data has lifetime of
22892 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22893
22894 struct dwarf2_locexpr_baton
22895 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22896 struct dwarf2_per_cu_data *per_cu,
22897 CORE_ADDR (*get_frame_pc) (void *baton),
22898 void *baton)
22899 {
22900 struct dwarf2_cu *cu;
22901 struct die_info *die;
22902 struct attribute *attr;
22903 struct dwarf2_locexpr_baton retval;
22904 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22905 struct objfile *objfile = dwarf2_per_objfile->objfile;
22906
22907 if (per_cu->cu == NULL)
22908 load_cu (per_cu, false);
22909 cu = per_cu->cu;
22910 if (cu == NULL)
22911 {
22912 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22913 Instead just throw an error, not much else we can do. */
22914 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22915 sect_offset_str (sect_off), objfile_name (objfile));
22916 }
22917
22918 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22919 if (!die)
22920 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22921 sect_offset_str (sect_off), objfile_name (objfile));
22922
22923 attr = dwarf2_attr (die, DW_AT_location, cu);
22924 if (!attr)
22925 {
22926 /* DWARF: "If there is no such attribute, then there is no effect.".
22927 DATA is ignored if SIZE is 0. */
22928
22929 retval.data = NULL;
22930 retval.size = 0;
22931 }
22932 else if (attr_form_is_section_offset (attr))
22933 {
22934 struct dwarf2_loclist_baton loclist_baton;
22935 CORE_ADDR pc = (*get_frame_pc) (baton);
22936 size_t size;
22937
22938 fill_in_loclist_baton (cu, &loclist_baton, attr);
22939
22940 retval.data = dwarf2_find_location_expression (&loclist_baton,
22941 &size, pc);
22942 retval.size = size;
22943 }
22944 else
22945 {
22946 if (!attr_form_is_block (attr))
22947 error (_("Dwarf Error: DIE at %s referenced in module %s "
22948 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22949 sect_offset_str (sect_off), objfile_name (objfile));
22950
22951 retval.data = DW_BLOCK (attr)->data;
22952 retval.size = DW_BLOCK (attr)->size;
22953 }
22954 retval.per_cu = cu->per_cu;
22955
22956 age_cached_comp_units (dwarf2_per_objfile);
22957
22958 return retval;
22959 }
22960
22961 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22962 offset. */
22963
22964 struct dwarf2_locexpr_baton
22965 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22966 struct dwarf2_per_cu_data *per_cu,
22967 CORE_ADDR (*get_frame_pc) (void *baton),
22968 void *baton)
22969 {
22970 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22971
22972 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22973 }
22974
22975 /* Write a constant of a given type as target-ordered bytes into
22976 OBSTACK. */
22977
22978 static const gdb_byte *
22979 write_constant_as_bytes (struct obstack *obstack,
22980 enum bfd_endian byte_order,
22981 struct type *type,
22982 ULONGEST value,
22983 LONGEST *len)
22984 {
22985 gdb_byte *result;
22986
22987 *len = TYPE_LENGTH (type);
22988 result = (gdb_byte *) obstack_alloc (obstack, *len);
22989 store_unsigned_integer (result, *len, byte_order, value);
22990
22991 return result;
22992 }
22993
22994 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22995 pointer to the constant bytes and set LEN to the length of the
22996 data. If memory is needed, allocate it on OBSTACK. If the DIE
22997 does not have a DW_AT_const_value, return NULL. */
22998
22999 const gdb_byte *
23000 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23001 struct dwarf2_per_cu_data *per_cu,
23002 struct obstack *obstack,
23003 LONGEST *len)
23004 {
23005 struct dwarf2_cu *cu;
23006 struct die_info *die;
23007 struct attribute *attr;
23008 const gdb_byte *result = NULL;
23009 struct type *type;
23010 LONGEST value;
23011 enum bfd_endian byte_order;
23012 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23013
23014 if (per_cu->cu == NULL)
23015 load_cu (per_cu, false);
23016 cu = per_cu->cu;
23017 if (cu == NULL)
23018 {
23019 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23020 Instead just throw an error, not much else we can do. */
23021 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23022 sect_offset_str (sect_off), objfile_name (objfile));
23023 }
23024
23025 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23026 if (!die)
23027 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23028 sect_offset_str (sect_off), objfile_name (objfile));
23029
23030 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23031 if (attr == NULL)
23032 return NULL;
23033
23034 byte_order = (bfd_big_endian (objfile->obfd)
23035 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23036
23037 switch (attr->form)
23038 {
23039 case DW_FORM_addr:
23040 case DW_FORM_GNU_addr_index:
23041 {
23042 gdb_byte *tem;
23043
23044 *len = cu->header.addr_size;
23045 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23046 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23047 result = tem;
23048 }
23049 break;
23050 case DW_FORM_string:
23051 case DW_FORM_strp:
23052 case DW_FORM_GNU_str_index:
23053 case DW_FORM_GNU_strp_alt:
23054 /* DW_STRING is already allocated on the objfile obstack, point
23055 directly to it. */
23056 result = (const gdb_byte *) DW_STRING (attr);
23057 *len = strlen (DW_STRING (attr));
23058 break;
23059 case DW_FORM_block1:
23060 case DW_FORM_block2:
23061 case DW_FORM_block4:
23062 case DW_FORM_block:
23063 case DW_FORM_exprloc:
23064 case DW_FORM_data16:
23065 result = DW_BLOCK (attr)->data;
23066 *len = DW_BLOCK (attr)->size;
23067 break;
23068
23069 /* The DW_AT_const_value attributes are supposed to carry the
23070 symbol's value "represented as it would be on the target
23071 architecture." By the time we get here, it's already been
23072 converted to host endianness, so we just need to sign- or
23073 zero-extend it as appropriate. */
23074 case DW_FORM_data1:
23075 type = die_type (die, cu);
23076 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23077 if (result == NULL)
23078 result = write_constant_as_bytes (obstack, byte_order,
23079 type, value, len);
23080 break;
23081 case DW_FORM_data2:
23082 type = die_type (die, cu);
23083 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23084 if (result == NULL)
23085 result = write_constant_as_bytes (obstack, byte_order,
23086 type, value, len);
23087 break;
23088 case DW_FORM_data4:
23089 type = die_type (die, cu);
23090 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23091 if (result == NULL)
23092 result = write_constant_as_bytes (obstack, byte_order,
23093 type, value, len);
23094 break;
23095 case DW_FORM_data8:
23096 type = die_type (die, cu);
23097 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23098 if (result == NULL)
23099 result = write_constant_as_bytes (obstack, byte_order,
23100 type, value, len);
23101 break;
23102
23103 case DW_FORM_sdata:
23104 case DW_FORM_implicit_const:
23105 type = die_type (die, cu);
23106 result = write_constant_as_bytes (obstack, byte_order,
23107 type, DW_SND (attr), len);
23108 break;
23109
23110 case DW_FORM_udata:
23111 type = die_type (die, cu);
23112 result = write_constant_as_bytes (obstack, byte_order,
23113 type, DW_UNSND (attr), len);
23114 break;
23115
23116 default:
23117 complaint (_("unsupported const value attribute form: '%s'"),
23118 dwarf_form_name (attr->form));
23119 break;
23120 }
23121
23122 return result;
23123 }
23124
23125 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23126 valid type for this die is found. */
23127
23128 struct type *
23129 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23130 struct dwarf2_per_cu_data *per_cu)
23131 {
23132 struct dwarf2_cu *cu;
23133 struct die_info *die;
23134
23135 if (per_cu->cu == NULL)
23136 load_cu (per_cu, false);
23137 cu = per_cu->cu;
23138 if (!cu)
23139 return NULL;
23140
23141 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23142 if (!die)
23143 return NULL;
23144
23145 return die_type (die, cu);
23146 }
23147
23148 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23149 PER_CU. */
23150
23151 struct type *
23152 dwarf2_get_die_type (cu_offset die_offset,
23153 struct dwarf2_per_cu_data *per_cu)
23154 {
23155 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23156 return get_die_type_at_offset (die_offset_sect, per_cu);
23157 }
23158
23159 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23160 On entry *REF_CU is the CU of SRC_DIE.
23161 On exit *REF_CU is the CU of the result.
23162 Returns NULL if the referenced DIE isn't found. */
23163
23164 static struct die_info *
23165 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23166 struct dwarf2_cu **ref_cu)
23167 {
23168 struct die_info temp_die;
23169 struct dwarf2_cu *sig_cu;
23170 struct die_info *die;
23171
23172 /* While it might be nice to assert sig_type->type == NULL here,
23173 we can get here for DW_AT_imported_declaration where we need
23174 the DIE not the type. */
23175
23176 /* If necessary, add it to the queue and load its DIEs. */
23177
23178 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23179 read_signatured_type (sig_type);
23180
23181 sig_cu = sig_type->per_cu.cu;
23182 gdb_assert (sig_cu != NULL);
23183 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23184 temp_die.sect_off = sig_type->type_offset_in_section;
23185 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23186 to_underlying (temp_die.sect_off));
23187 if (die)
23188 {
23189 struct dwarf2_per_objfile *dwarf2_per_objfile
23190 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23191
23192 /* For .gdb_index version 7 keep track of included TUs.
23193 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23194 if (dwarf2_per_objfile->index_table != NULL
23195 && dwarf2_per_objfile->index_table->version <= 7)
23196 {
23197 VEC_safe_push (dwarf2_per_cu_ptr,
23198 (*ref_cu)->per_cu->imported_symtabs,
23199 sig_cu->per_cu);
23200 }
23201
23202 *ref_cu = sig_cu;
23203 return die;
23204 }
23205
23206 return NULL;
23207 }
23208
23209 /* Follow signatured type referenced by ATTR in SRC_DIE.
23210 On entry *REF_CU is the CU of SRC_DIE.
23211 On exit *REF_CU is the CU of the result.
23212 The result is the DIE of the type.
23213 If the referenced type cannot be found an error is thrown. */
23214
23215 static struct die_info *
23216 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23217 struct dwarf2_cu **ref_cu)
23218 {
23219 ULONGEST signature = DW_SIGNATURE (attr);
23220 struct signatured_type *sig_type;
23221 struct die_info *die;
23222
23223 gdb_assert (attr->form == DW_FORM_ref_sig8);
23224
23225 sig_type = lookup_signatured_type (*ref_cu, signature);
23226 /* sig_type will be NULL if the signatured type is missing from
23227 the debug info. */
23228 if (sig_type == NULL)
23229 {
23230 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23231 " from DIE at %s [in module %s]"),
23232 hex_string (signature), sect_offset_str (src_die->sect_off),
23233 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23234 }
23235
23236 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23237 if (die == NULL)
23238 {
23239 dump_die_for_error (src_die);
23240 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23241 " from DIE at %s [in module %s]"),
23242 hex_string (signature), sect_offset_str (src_die->sect_off),
23243 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23244 }
23245
23246 return die;
23247 }
23248
23249 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23250 reading in and processing the type unit if necessary. */
23251
23252 static struct type *
23253 get_signatured_type (struct die_info *die, ULONGEST signature,
23254 struct dwarf2_cu *cu)
23255 {
23256 struct dwarf2_per_objfile *dwarf2_per_objfile
23257 = cu->per_cu->dwarf2_per_objfile;
23258 struct signatured_type *sig_type;
23259 struct dwarf2_cu *type_cu;
23260 struct die_info *type_die;
23261 struct type *type;
23262
23263 sig_type = lookup_signatured_type (cu, signature);
23264 /* sig_type will be NULL if the signatured type is missing from
23265 the debug info. */
23266 if (sig_type == NULL)
23267 {
23268 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23269 " from DIE at %s [in module %s]"),
23270 hex_string (signature), sect_offset_str (die->sect_off),
23271 objfile_name (dwarf2_per_objfile->objfile));
23272 return build_error_marker_type (cu, die);
23273 }
23274
23275 /* If we already know the type we're done. */
23276 if (sig_type->type != NULL)
23277 return sig_type->type;
23278
23279 type_cu = cu;
23280 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23281 if (type_die != NULL)
23282 {
23283 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23284 is created. This is important, for example, because for c++ classes
23285 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23286 type = read_type_die (type_die, type_cu);
23287 if (type == NULL)
23288 {
23289 complaint (_("Dwarf Error: Cannot build signatured type %s"
23290 " referenced from DIE at %s [in module %s]"),
23291 hex_string (signature), sect_offset_str (die->sect_off),
23292 objfile_name (dwarf2_per_objfile->objfile));
23293 type = build_error_marker_type (cu, die);
23294 }
23295 }
23296 else
23297 {
23298 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23299 " from DIE at %s [in module %s]"),
23300 hex_string (signature), sect_offset_str (die->sect_off),
23301 objfile_name (dwarf2_per_objfile->objfile));
23302 type = build_error_marker_type (cu, die);
23303 }
23304 sig_type->type = type;
23305
23306 return type;
23307 }
23308
23309 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23310 reading in and processing the type unit if necessary. */
23311
23312 static struct type *
23313 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23314 struct dwarf2_cu *cu) /* ARI: editCase function */
23315 {
23316 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23317 if (attr_form_is_ref (attr))
23318 {
23319 struct dwarf2_cu *type_cu = cu;
23320 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23321
23322 return read_type_die (type_die, type_cu);
23323 }
23324 else if (attr->form == DW_FORM_ref_sig8)
23325 {
23326 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23327 }
23328 else
23329 {
23330 struct dwarf2_per_objfile *dwarf2_per_objfile
23331 = cu->per_cu->dwarf2_per_objfile;
23332
23333 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23334 " at %s [in module %s]"),
23335 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23336 objfile_name (dwarf2_per_objfile->objfile));
23337 return build_error_marker_type (cu, die);
23338 }
23339 }
23340
23341 /* Load the DIEs associated with type unit PER_CU into memory. */
23342
23343 static void
23344 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23345 {
23346 struct signatured_type *sig_type;
23347
23348 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23349 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23350
23351 /* We have the per_cu, but we need the signatured_type.
23352 Fortunately this is an easy translation. */
23353 gdb_assert (per_cu->is_debug_types);
23354 sig_type = (struct signatured_type *) per_cu;
23355
23356 gdb_assert (per_cu->cu == NULL);
23357
23358 read_signatured_type (sig_type);
23359
23360 gdb_assert (per_cu->cu != NULL);
23361 }
23362
23363 /* die_reader_func for read_signatured_type.
23364 This is identical to load_full_comp_unit_reader,
23365 but is kept separate for now. */
23366
23367 static void
23368 read_signatured_type_reader (const struct die_reader_specs *reader,
23369 const gdb_byte *info_ptr,
23370 struct die_info *comp_unit_die,
23371 int has_children,
23372 void *data)
23373 {
23374 struct dwarf2_cu *cu = reader->cu;
23375
23376 gdb_assert (cu->die_hash == NULL);
23377 cu->die_hash =
23378 htab_create_alloc_ex (cu->header.length / 12,
23379 die_hash,
23380 die_eq,
23381 NULL,
23382 &cu->comp_unit_obstack,
23383 hashtab_obstack_allocate,
23384 dummy_obstack_deallocate);
23385
23386 if (has_children)
23387 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23388 &info_ptr, comp_unit_die);
23389 cu->dies = comp_unit_die;
23390 /* comp_unit_die is not stored in die_hash, no need. */
23391
23392 /* We try not to read any attributes in this function, because not
23393 all CUs needed for references have been loaded yet, and symbol
23394 table processing isn't initialized. But we have to set the CU language,
23395 or we won't be able to build types correctly.
23396 Similarly, if we do not read the producer, we can not apply
23397 producer-specific interpretation. */
23398 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23399 }
23400
23401 /* Read in a signatured type and build its CU and DIEs.
23402 If the type is a stub for the real type in a DWO file,
23403 read in the real type from the DWO file as well. */
23404
23405 static void
23406 read_signatured_type (struct signatured_type *sig_type)
23407 {
23408 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23409
23410 gdb_assert (per_cu->is_debug_types);
23411 gdb_assert (per_cu->cu == NULL);
23412
23413 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
23414 read_signatured_type_reader, NULL);
23415 sig_type->per_cu.tu_read = 1;
23416 }
23417
23418 /* Decode simple location descriptions.
23419 Given a pointer to a dwarf block that defines a location, compute
23420 the location and return the value.
23421
23422 NOTE drow/2003-11-18: This function is called in two situations
23423 now: for the address of static or global variables (partial symbols
23424 only) and for offsets into structures which are expected to be
23425 (more or less) constant. The partial symbol case should go away,
23426 and only the constant case should remain. That will let this
23427 function complain more accurately. A few special modes are allowed
23428 without complaint for global variables (for instance, global
23429 register values and thread-local values).
23430
23431 A location description containing no operations indicates that the
23432 object is optimized out. The return value is 0 for that case.
23433 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23434 callers will only want a very basic result and this can become a
23435 complaint.
23436
23437 Note that stack[0] is unused except as a default error return. */
23438
23439 static CORE_ADDR
23440 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23441 {
23442 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23443 size_t i;
23444 size_t size = blk->size;
23445 const gdb_byte *data = blk->data;
23446 CORE_ADDR stack[64];
23447 int stacki;
23448 unsigned int bytes_read, unsnd;
23449 gdb_byte op;
23450
23451 i = 0;
23452 stacki = 0;
23453 stack[stacki] = 0;
23454 stack[++stacki] = 0;
23455
23456 while (i < size)
23457 {
23458 op = data[i++];
23459 switch (op)
23460 {
23461 case DW_OP_lit0:
23462 case DW_OP_lit1:
23463 case DW_OP_lit2:
23464 case DW_OP_lit3:
23465 case DW_OP_lit4:
23466 case DW_OP_lit5:
23467 case DW_OP_lit6:
23468 case DW_OP_lit7:
23469 case DW_OP_lit8:
23470 case DW_OP_lit9:
23471 case DW_OP_lit10:
23472 case DW_OP_lit11:
23473 case DW_OP_lit12:
23474 case DW_OP_lit13:
23475 case DW_OP_lit14:
23476 case DW_OP_lit15:
23477 case DW_OP_lit16:
23478 case DW_OP_lit17:
23479 case DW_OP_lit18:
23480 case DW_OP_lit19:
23481 case DW_OP_lit20:
23482 case DW_OP_lit21:
23483 case DW_OP_lit22:
23484 case DW_OP_lit23:
23485 case DW_OP_lit24:
23486 case DW_OP_lit25:
23487 case DW_OP_lit26:
23488 case DW_OP_lit27:
23489 case DW_OP_lit28:
23490 case DW_OP_lit29:
23491 case DW_OP_lit30:
23492 case DW_OP_lit31:
23493 stack[++stacki] = op - DW_OP_lit0;
23494 break;
23495
23496 case DW_OP_reg0:
23497 case DW_OP_reg1:
23498 case DW_OP_reg2:
23499 case DW_OP_reg3:
23500 case DW_OP_reg4:
23501 case DW_OP_reg5:
23502 case DW_OP_reg6:
23503 case DW_OP_reg7:
23504 case DW_OP_reg8:
23505 case DW_OP_reg9:
23506 case DW_OP_reg10:
23507 case DW_OP_reg11:
23508 case DW_OP_reg12:
23509 case DW_OP_reg13:
23510 case DW_OP_reg14:
23511 case DW_OP_reg15:
23512 case DW_OP_reg16:
23513 case DW_OP_reg17:
23514 case DW_OP_reg18:
23515 case DW_OP_reg19:
23516 case DW_OP_reg20:
23517 case DW_OP_reg21:
23518 case DW_OP_reg22:
23519 case DW_OP_reg23:
23520 case DW_OP_reg24:
23521 case DW_OP_reg25:
23522 case DW_OP_reg26:
23523 case DW_OP_reg27:
23524 case DW_OP_reg28:
23525 case DW_OP_reg29:
23526 case DW_OP_reg30:
23527 case DW_OP_reg31:
23528 stack[++stacki] = op - DW_OP_reg0;
23529 if (i < size)
23530 dwarf2_complex_location_expr_complaint ();
23531 break;
23532
23533 case DW_OP_regx:
23534 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23535 i += bytes_read;
23536 stack[++stacki] = unsnd;
23537 if (i < size)
23538 dwarf2_complex_location_expr_complaint ();
23539 break;
23540
23541 case DW_OP_addr:
23542 stack[++stacki] = read_address (objfile->obfd, &data[i],
23543 cu, &bytes_read);
23544 i += bytes_read;
23545 break;
23546
23547 case DW_OP_const1u:
23548 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23549 i += 1;
23550 break;
23551
23552 case DW_OP_const1s:
23553 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23554 i += 1;
23555 break;
23556
23557 case DW_OP_const2u:
23558 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23559 i += 2;
23560 break;
23561
23562 case DW_OP_const2s:
23563 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23564 i += 2;
23565 break;
23566
23567 case DW_OP_const4u:
23568 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23569 i += 4;
23570 break;
23571
23572 case DW_OP_const4s:
23573 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23574 i += 4;
23575 break;
23576
23577 case DW_OP_const8u:
23578 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23579 i += 8;
23580 break;
23581
23582 case DW_OP_constu:
23583 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23584 &bytes_read);
23585 i += bytes_read;
23586 break;
23587
23588 case DW_OP_consts:
23589 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23590 i += bytes_read;
23591 break;
23592
23593 case DW_OP_dup:
23594 stack[stacki + 1] = stack[stacki];
23595 stacki++;
23596 break;
23597
23598 case DW_OP_plus:
23599 stack[stacki - 1] += stack[stacki];
23600 stacki--;
23601 break;
23602
23603 case DW_OP_plus_uconst:
23604 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23605 &bytes_read);
23606 i += bytes_read;
23607 break;
23608
23609 case DW_OP_minus:
23610 stack[stacki - 1] -= stack[stacki];
23611 stacki--;
23612 break;
23613
23614 case DW_OP_deref:
23615 /* If we're not the last op, then we definitely can't encode
23616 this using GDB's address_class enum. This is valid for partial
23617 global symbols, although the variable's address will be bogus
23618 in the psymtab. */
23619 if (i < size)
23620 dwarf2_complex_location_expr_complaint ();
23621 break;
23622
23623 case DW_OP_GNU_push_tls_address:
23624 case DW_OP_form_tls_address:
23625 /* The top of the stack has the offset from the beginning
23626 of the thread control block at which the variable is located. */
23627 /* Nothing should follow this operator, so the top of stack would
23628 be returned. */
23629 /* This is valid for partial global symbols, but the variable's
23630 address will be bogus in the psymtab. Make it always at least
23631 non-zero to not look as a variable garbage collected by linker
23632 which have DW_OP_addr 0. */
23633 if (i < size)
23634 dwarf2_complex_location_expr_complaint ();
23635 stack[stacki]++;
23636 break;
23637
23638 case DW_OP_GNU_uninit:
23639 break;
23640
23641 case DW_OP_GNU_addr_index:
23642 case DW_OP_GNU_const_index:
23643 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23644 &bytes_read);
23645 i += bytes_read;
23646 break;
23647
23648 default:
23649 {
23650 const char *name = get_DW_OP_name (op);
23651
23652 if (name)
23653 complaint (_("unsupported stack op: '%s'"),
23654 name);
23655 else
23656 complaint (_("unsupported stack op: '%02x'"),
23657 op);
23658 }
23659
23660 return (stack[stacki]);
23661 }
23662
23663 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23664 outside of the allocated space. Also enforce minimum>0. */
23665 if (stacki >= ARRAY_SIZE (stack) - 1)
23666 {
23667 complaint (_("location description stack overflow"));
23668 return 0;
23669 }
23670
23671 if (stacki <= 0)
23672 {
23673 complaint (_("location description stack underflow"));
23674 return 0;
23675 }
23676 }
23677 return (stack[stacki]);
23678 }
23679
23680 /* memory allocation interface */
23681
23682 static struct dwarf_block *
23683 dwarf_alloc_block (struct dwarf2_cu *cu)
23684 {
23685 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23686 }
23687
23688 static struct die_info *
23689 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23690 {
23691 struct die_info *die;
23692 size_t size = sizeof (struct die_info);
23693
23694 if (num_attrs > 1)
23695 size += (num_attrs - 1) * sizeof (struct attribute);
23696
23697 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23698 memset (die, 0, sizeof (struct die_info));
23699 return (die);
23700 }
23701
23702 \f
23703 /* Macro support. */
23704
23705 /* Return file name relative to the compilation directory of file number I in
23706 *LH's file name table. The result is allocated using xmalloc; the caller is
23707 responsible for freeing it. */
23708
23709 static char *
23710 file_file_name (int file, struct line_header *lh)
23711 {
23712 /* Is the file number a valid index into the line header's file name
23713 table? Remember that file numbers start with one, not zero. */
23714 if (1 <= file && file <= lh->file_names.size ())
23715 {
23716 const file_entry &fe = lh->file_names[file - 1];
23717
23718 if (!IS_ABSOLUTE_PATH (fe.name))
23719 {
23720 const char *dir = fe.include_dir (lh);
23721 if (dir != NULL)
23722 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23723 }
23724 return xstrdup (fe.name);
23725 }
23726 else
23727 {
23728 /* The compiler produced a bogus file number. We can at least
23729 record the macro definitions made in the file, even if we
23730 won't be able to find the file by name. */
23731 char fake_name[80];
23732
23733 xsnprintf (fake_name, sizeof (fake_name),
23734 "<bad macro file number %d>", file);
23735
23736 complaint (_("bad file number in macro information (%d)"),
23737 file);
23738
23739 return xstrdup (fake_name);
23740 }
23741 }
23742
23743 /* Return the full name of file number I in *LH's file name table.
23744 Use COMP_DIR as the name of the current directory of the
23745 compilation. The result is allocated using xmalloc; the caller is
23746 responsible for freeing it. */
23747 static char *
23748 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23749 {
23750 /* Is the file number a valid index into the line header's file name
23751 table? Remember that file numbers start with one, not zero. */
23752 if (1 <= file && file <= lh->file_names.size ())
23753 {
23754 char *relative = file_file_name (file, lh);
23755
23756 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23757 return relative;
23758 return reconcat (relative, comp_dir, SLASH_STRING,
23759 relative, (char *) NULL);
23760 }
23761 else
23762 return file_file_name (file, lh);
23763 }
23764
23765
23766 static struct macro_source_file *
23767 macro_start_file (struct dwarf2_cu *cu,
23768 int file, int line,
23769 struct macro_source_file *current_file,
23770 struct line_header *lh)
23771 {
23772 /* File name relative to the compilation directory of this source file. */
23773 char *file_name = file_file_name (file, lh);
23774
23775 if (! current_file)
23776 {
23777 /* Note: We don't create a macro table for this compilation unit
23778 at all until we actually get a filename. */
23779 struct macro_table *macro_table = cu->builder->get_macro_table ();
23780
23781 /* If we have no current file, then this must be the start_file
23782 directive for the compilation unit's main source file. */
23783 current_file = macro_set_main (macro_table, file_name);
23784 macro_define_special (macro_table);
23785 }
23786 else
23787 current_file = macro_include (current_file, line, file_name);
23788
23789 xfree (file_name);
23790
23791 return current_file;
23792 }
23793
23794 static const char *
23795 consume_improper_spaces (const char *p, const char *body)
23796 {
23797 if (*p == ' ')
23798 {
23799 complaint (_("macro definition contains spaces "
23800 "in formal argument list:\n`%s'"),
23801 body);
23802
23803 while (*p == ' ')
23804 p++;
23805 }
23806
23807 return p;
23808 }
23809
23810
23811 static void
23812 parse_macro_definition (struct macro_source_file *file, int line,
23813 const char *body)
23814 {
23815 const char *p;
23816
23817 /* The body string takes one of two forms. For object-like macro
23818 definitions, it should be:
23819
23820 <macro name> " " <definition>
23821
23822 For function-like macro definitions, it should be:
23823
23824 <macro name> "() " <definition>
23825 or
23826 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23827
23828 Spaces may appear only where explicitly indicated, and in the
23829 <definition>.
23830
23831 The Dwarf 2 spec says that an object-like macro's name is always
23832 followed by a space, but versions of GCC around March 2002 omit
23833 the space when the macro's definition is the empty string.
23834
23835 The Dwarf 2 spec says that there should be no spaces between the
23836 formal arguments in a function-like macro's formal argument list,
23837 but versions of GCC around March 2002 include spaces after the
23838 commas. */
23839
23840
23841 /* Find the extent of the macro name. The macro name is terminated
23842 by either a space or null character (for an object-like macro) or
23843 an opening paren (for a function-like macro). */
23844 for (p = body; *p; p++)
23845 if (*p == ' ' || *p == '(')
23846 break;
23847
23848 if (*p == ' ' || *p == '\0')
23849 {
23850 /* It's an object-like macro. */
23851 int name_len = p - body;
23852 char *name = savestring (body, name_len);
23853 const char *replacement;
23854
23855 if (*p == ' ')
23856 replacement = body + name_len + 1;
23857 else
23858 {
23859 dwarf2_macro_malformed_definition_complaint (body);
23860 replacement = body + name_len;
23861 }
23862
23863 macro_define_object (file, line, name, replacement);
23864
23865 xfree (name);
23866 }
23867 else if (*p == '(')
23868 {
23869 /* It's a function-like macro. */
23870 char *name = savestring (body, p - body);
23871 int argc = 0;
23872 int argv_size = 1;
23873 char **argv = XNEWVEC (char *, argv_size);
23874
23875 p++;
23876
23877 p = consume_improper_spaces (p, body);
23878
23879 /* Parse the formal argument list. */
23880 while (*p && *p != ')')
23881 {
23882 /* Find the extent of the current argument name. */
23883 const char *arg_start = p;
23884
23885 while (*p && *p != ',' && *p != ')' && *p != ' ')
23886 p++;
23887
23888 if (! *p || p == arg_start)
23889 dwarf2_macro_malformed_definition_complaint (body);
23890 else
23891 {
23892 /* Make sure argv has room for the new argument. */
23893 if (argc >= argv_size)
23894 {
23895 argv_size *= 2;
23896 argv = XRESIZEVEC (char *, argv, argv_size);
23897 }
23898
23899 argv[argc++] = savestring (arg_start, p - arg_start);
23900 }
23901
23902 p = consume_improper_spaces (p, body);
23903
23904 /* Consume the comma, if present. */
23905 if (*p == ',')
23906 {
23907 p++;
23908
23909 p = consume_improper_spaces (p, body);
23910 }
23911 }
23912
23913 if (*p == ')')
23914 {
23915 p++;
23916
23917 if (*p == ' ')
23918 /* Perfectly formed definition, no complaints. */
23919 macro_define_function (file, line, name,
23920 argc, (const char **) argv,
23921 p + 1);
23922 else if (*p == '\0')
23923 {
23924 /* Complain, but do define it. */
23925 dwarf2_macro_malformed_definition_complaint (body);
23926 macro_define_function (file, line, name,
23927 argc, (const char **) argv,
23928 p);
23929 }
23930 else
23931 /* Just complain. */
23932 dwarf2_macro_malformed_definition_complaint (body);
23933 }
23934 else
23935 /* Just complain. */
23936 dwarf2_macro_malformed_definition_complaint (body);
23937
23938 xfree (name);
23939 {
23940 int i;
23941
23942 for (i = 0; i < argc; i++)
23943 xfree (argv[i]);
23944 }
23945 xfree (argv);
23946 }
23947 else
23948 dwarf2_macro_malformed_definition_complaint (body);
23949 }
23950
23951 /* Skip some bytes from BYTES according to the form given in FORM.
23952 Returns the new pointer. */
23953
23954 static const gdb_byte *
23955 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23956 enum dwarf_form form,
23957 unsigned int offset_size,
23958 struct dwarf2_section_info *section)
23959 {
23960 unsigned int bytes_read;
23961
23962 switch (form)
23963 {
23964 case DW_FORM_data1:
23965 case DW_FORM_flag:
23966 ++bytes;
23967 break;
23968
23969 case DW_FORM_data2:
23970 bytes += 2;
23971 break;
23972
23973 case DW_FORM_data4:
23974 bytes += 4;
23975 break;
23976
23977 case DW_FORM_data8:
23978 bytes += 8;
23979 break;
23980
23981 case DW_FORM_data16:
23982 bytes += 16;
23983 break;
23984
23985 case DW_FORM_string:
23986 read_direct_string (abfd, bytes, &bytes_read);
23987 bytes += bytes_read;
23988 break;
23989
23990 case DW_FORM_sec_offset:
23991 case DW_FORM_strp:
23992 case DW_FORM_GNU_strp_alt:
23993 bytes += offset_size;
23994 break;
23995
23996 case DW_FORM_block:
23997 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23998 bytes += bytes_read;
23999 break;
24000
24001 case DW_FORM_block1:
24002 bytes += 1 + read_1_byte (abfd, bytes);
24003 break;
24004 case DW_FORM_block2:
24005 bytes += 2 + read_2_bytes (abfd, bytes);
24006 break;
24007 case DW_FORM_block4:
24008 bytes += 4 + read_4_bytes (abfd, bytes);
24009 break;
24010
24011 case DW_FORM_sdata:
24012 case DW_FORM_udata:
24013 case DW_FORM_GNU_addr_index:
24014 case DW_FORM_GNU_str_index:
24015 bytes = gdb_skip_leb128 (bytes, buffer_end);
24016 if (bytes == NULL)
24017 {
24018 dwarf2_section_buffer_overflow_complaint (section);
24019 return NULL;
24020 }
24021 break;
24022
24023 case DW_FORM_implicit_const:
24024 break;
24025
24026 default:
24027 {
24028 complaint (_("invalid form 0x%x in `%s'"),
24029 form, get_section_name (section));
24030 return NULL;
24031 }
24032 }
24033
24034 return bytes;
24035 }
24036
24037 /* A helper for dwarf_decode_macros that handles skipping an unknown
24038 opcode. Returns an updated pointer to the macro data buffer; or,
24039 on error, issues a complaint and returns NULL. */
24040
24041 static const gdb_byte *
24042 skip_unknown_opcode (unsigned int opcode,
24043 const gdb_byte **opcode_definitions,
24044 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24045 bfd *abfd,
24046 unsigned int offset_size,
24047 struct dwarf2_section_info *section)
24048 {
24049 unsigned int bytes_read, i;
24050 unsigned long arg;
24051 const gdb_byte *defn;
24052
24053 if (opcode_definitions[opcode] == NULL)
24054 {
24055 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24056 opcode);
24057 return NULL;
24058 }
24059
24060 defn = opcode_definitions[opcode];
24061 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24062 defn += bytes_read;
24063
24064 for (i = 0; i < arg; ++i)
24065 {
24066 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24067 (enum dwarf_form) defn[i], offset_size,
24068 section);
24069 if (mac_ptr == NULL)
24070 {
24071 /* skip_form_bytes already issued the complaint. */
24072 return NULL;
24073 }
24074 }
24075
24076 return mac_ptr;
24077 }
24078
24079 /* A helper function which parses the header of a macro section.
24080 If the macro section is the extended (for now called "GNU") type,
24081 then this updates *OFFSET_SIZE. Returns a pointer to just after
24082 the header, or issues a complaint and returns NULL on error. */
24083
24084 static const gdb_byte *
24085 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24086 bfd *abfd,
24087 const gdb_byte *mac_ptr,
24088 unsigned int *offset_size,
24089 int section_is_gnu)
24090 {
24091 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24092
24093 if (section_is_gnu)
24094 {
24095 unsigned int version, flags;
24096
24097 version = read_2_bytes (abfd, mac_ptr);
24098 if (version != 4 && version != 5)
24099 {
24100 complaint (_("unrecognized version `%d' in .debug_macro section"),
24101 version);
24102 return NULL;
24103 }
24104 mac_ptr += 2;
24105
24106 flags = read_1_byte (abfd, mac_ptr);
24107 ++mac_ptr;
24108 *offset_size = (flags & 1) ? 8 : 4;
24109
24110 if ((flags & 2) != 0)
24111 /* We don't need the line table offset. */
24112 mac_ptr += *offset_size;
24113
24114 /* Vendor opcode descriptions. */
24115 if ((flags & 4) != 0)
24116 {
24117 unsigned int i, count;
24118
24119 count = read_1_byte (abfd, mac_ptr);
24120 ++mac_ptr;
24121 for (i = 0; i < count; ++i)
24122 {
24123 unsigned int opcode, bytes_read;
24124 unsigned long arg;
24125
24126 opcode = read_1_byte (abfd, mac_ptr);
24127 ++mac_ptr;
24128 opcode_definitions[opcode] = mac_ptr;
24129 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24130 mac_ptr += bytes_read;
24131 mac_ptr += arg;
24132 }
24133 }
24134 }
24135
24136 return mac_ptr;
24137 }
24138
24139 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24140 including DW_MACRO_import. */
24141
24142 static void
24143 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24144 bfd *abfd,
24145 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24146 struct macro_source_file *current_file,
24147 struct line_header *lh,
24148 struct dwarf2_section_info *section,
24149 int section_is_gnu, int section_is_dwz,
24150 unsigned int offset_size,
24151 htab_t include_hash)
24152 {
24153 struct dwarf2_per_objfile *dwarf2_per_objfile
24154 = cu->per_cu->dwarf2_per_objfile;
24155 struct objfile *objfile = dwarf2_per_objfile->objfile;
24156 enum dwarf_macro_record_type macinfo_type;
24157 int at_commandline;
24158 const gdb_byte *opcode_definitions[256];
24159
24160 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24161 &offset_size, section_is_gnu);
24162 if (mac_ptr == NULL)
24163 {
24164 /* We already issued a complaint. */
24165 return;
24166 }
24167
24168 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24169 GDB is still reading the definitions from command line. First
24170 DW_MACINFO_start_file will need to be ignored as it was already executed
24171 to create CURRENT_FILE for the main source holding also the command line
24172 definitions. On first met DW_MACINFO_start_file this flag is reset to
24173 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24174
24175 at_commandline = 1;
24176
24177 do
24178 {
24179 /* Do we at least have room for a macinfo type byte? */
24180 if (mac_ptr >= mac_end)
24181 {
24182 dwarf2_section_buffer_overflow_complaint (section);
24183 break;
24184 }
24185
24186 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24187 mac_ptr++;
24188
24189 /* Note that we rely on the fact that the corresponding GNU and
24190 DWARF constants are the same. */
24191 DIAGNOSTIC_PUSH
24192 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24193 switch (macinfo_type)
24194 {
24195 /* A zero macinfo type indicates the end of the macro
24196 information. */
24197 case 0:
24198 break;
24199
24200 case DW_MACRO_define:
24201 case DW_MACRO_undef:
24202 case DW_MACRO_define_strp:
24203 case DW_MACRO_undef_strp:
24204 case DW_MACRO_define_sup:
24205 case DW_MACRO_undef_sup:
24206 {
24207 unsigned int bytes_read;
24208 int line;
24209 const char *body;
24210 int is_define;
24211
24212 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24213 mac_ptr += bytes_read;
24214
24215 if (macinfo_type == DW_MACRO_define
24216 || macinfo_type == DW_MACRO_undef)
24217 {
24218 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24219 mac_ptr += bytes_read;
24220 }
24221 else
24222 {
24223 LONGEST str_offset;
24224
24225 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24226 mac_ptr += offset_size;
24227
24228 if (macinfo_type == DW_MACRO_define_sup
24229 || macinfo_type == DW_MACRO_undef_sup
24230 || section_is_dwz)
24231 {
24232 struct dwz_file *dwz
24233 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24234
24235 body = read_indirect_string_from_dwz (objfile,
24236 dwz, str_offset);
24237 }
24238 else
24239 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24240 abfd, str_offset);
24241 }
24242
24243 is_define = (macinfo_type == DW_MACRO_define
24244 || macinfo_type == DW_MACRO_define_strp
24245 || macinfo_type == DW_MACRO_define_sup);
24246 if (! current_file)
24247 {
24248 /* DWARF violation as no main source is present. */
24249 complaint (_("debug info with no main source gives macro %s "
24250 "on line %d: %s"),
24251 is_define ? _("definition") : _("undefinition"),
24252 line, body);
24253 break;
24254 }
24255 if ((line == 0 && !at_commandline)
24256 || (line != 0 && at_commandline))
24257 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24258 at_commandline ? _("command-line") : _("in-file"),
24259 is_define ? _("definition") : _("undefinition"),
24260 line == 0 ? _("zero") : _("non-zero"), line, body);
24261
24262 if (is_define)
24263 parse_macro_definition (current_file, line, body);
24264 else
24265 {
24266 gdb_assert (macinfo_type == DW_MACRO_undef
24267 || macinfo_type == DW_MACRO_undef_strp
24268 || macinfo_type == DW_MACRO_undef_sup);
24269 macro_undef (current_file, line, body);
24270 }
24271 }
24272 break;
24273
24274 case DW_MACRO_start_file:
24275 {
24276 unsigned int bytes_read;
24277 int line, file;
24278
24279 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24280 mac_ptr += bytes_read;
24281 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24282 mac_ptr += bytes_read;
24283
24284 if ((line == 0 && !at_commandline)
24285 || (line != 0 && at_commandline))
24286 complaint (_("debug info gives source %d included "
24287 "from %s at %s line %d"),
24288 file, at_commandline ? _("command-line") : _("file"),
24289 line == 0 ? _("zero") : _("non-zero"), line);
24290
24291 if (at_commandline)
24292 {
24293 /* This DW_MACRO_start_file was executed in the
24294 pass one. */
24295 at_commandline = 0;
24296 }
24297 else
24298 current_file = macro_start_file (cu, file, line, current_file,
24299 lh);
24300 }
24301 break;
24302
24303 case DW_MACRO_end_file:
24304 if (! current_file)
24305 complaint (_("macro debug info has an unmatched "
24306 "`close_file' directive"));
24307 else
24308 {
24309 current_file = current_file->included_by;
24310 if (! current_file)
24311 {
24312 enum dwarf_macro_record_type next_type;
24313
24314 /* GCC circa March 2002 doesn't produce the zero
24315 type byte marking the end of the compilation
24316 unit. Complain if it's not there, but exit no
24317 matter what. */
24318
24319 /* Do we at least have room for a macinfo type byte? */
24320 if (mac_ptr >= mac_end)
24321 {
24322 dwarf2_section_buffer_overflow_complaint (section);
24323 return;
24324 }
24325
24326 /* We don't increment mac_ptr here, so this is just
24327 a look-ahead. */
24328 next_type
24329 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24330 mac_ptr);
24331 if (next_type != 0)
24332 complaint (_("no terminating 0-type entry for "
24333 "macros in `.debug_macinfo' section"));
24334
24335 return;
24336 }
24337 }
24338 break;
24339
24340 case DW_MACRO_import:
24341 case DW_MACRO_import_sup:
24342 {
24343 LONGEST offset;
24344 void **slot;
24345 bfd *include_bfd = abfd;
24346 struct dwarf2_section_info *include_section = section;
24347 const gdb_byte *include_mac_end = mac_end;
24348 int is_dwz = section_is_dwz;
24349 const gdb_byte *new_mac_ptr;
24350
24351 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24352 mac_ptr += offset_size;
24353
24354 if (macinfo_type == DW_MACRO_import_sup)
24355 {
24356 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24357
24358 dwarf2_read_section (objfile, &dwz->macro);
24359
24360 include_section = &dwz->macro;
24361 include_bfd = get_section_bfd_owner (include_section);
24362 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24363 is_dwz = 1;
24364 }
24365
24366 new_mac_ptr = include_section->buffer + offset;
24367 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24368
24369 if (*slot != NULL)
24370 {
24371 /* This has actually happened; see
24372 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24373 complaint (_("recursive DW_MACRO_import in "
24374 ".debug_macro section"));
24375 }
24376 else
24377 {
24378 *slot = (void *) new_mac_ptr;
24379
24380 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24381 include_mac_end, current_file, lh,
24382 section, section_is_gnu, is_dwz,
24383 offset_size, include_hash);
24384
24385 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24386 }
24387 }
24388 break;
24389
24390 case DW_MACINFO_vendor_ext:
24391 if (!section_is_gnu)
24392 {
24393 unsigned int bytes_read;
24394
24395 /* This reads the constant, but since we don't recognize
24396 any vendor extensions, we ignore it. */
24397 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24398 mac_ptr += bytes_read;
24399 read_direct_string (abfd, mac_ptr, &bytes_read);
24400 mac_ptr += bytes_read;
24401
24402 /* We don't recognize any vendor extensions. */
24403 break;
24404 }
24405 /* FALLTHROUGH */
24406
24407 default:
24408 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24409 mac_ptr, mac_end, abfd, offset_size,
24410 section);
24411 if (mac_ptr == NULL)
24412 return;
24413 break;
24414 }
24415 DIAGNOSTIC_POP
24416 } while (macinfo_type != 0);
24417 }
24418
24419 static void
24420 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24421 int section_is_gnu)
24422 {
24423 struct dwarf2_per_objfile *dwarf2_per_objfile
24424 = cu->per_cu->dwarf2_per_objfile;
24425 struct objfile *objfile = dwarf2_per_objfile->objfile;
24426 struct line_header *lh = cu->line_header;
24427 bfd *abfd;
24428 const gdb_byte *mac_ptr, *mac_end;
24429 struct macro_source_file *current_file = 0;
24430 enum dwarf_macro_record_type macinfo_type;
24431 unsigned int offset_size = cu->header.offset_size;
24432 const gdb_byte *opcode_definitions[256];
24433 void **slot;
24434 struct dwarf2_section_info *section;
24435 const char *section_name;
24436
24437 if (cu->dwo_unit != NULL)
24438 {
24439 if (section_is_gnu)
24440 {
24441 section = &cu->dwo_unit->dwo_file->sections.macro;
24442 section_name = ".debug_macro.dwo";
24443 }
24444 else
24445 {
24446 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24447 section_name = ".debug_macinfo.dwo";
24448 }
24449 }
24450 else
24451 {
24452 if (section_is_gnu)
24453 {
24454 section = &dwarf2_per_objfile->macro;
24455 section_name = ".debug_macro";
24456 }
24457 else
24458 {
24459 section = &dwarf2_per_objfile->macinfo;
24460 section_name = ".debug_macinfo";
24461 }
24462 }
24463
24464 dwarf2_read_section (objfile, section);
24465 if (section->buffer == NULL)
24466 {
24467 complaint (_("missing %s section"), section_name);
24468 return;
24469 }
24470 abfd = get_section_bfd_owner (section);
24471
24472 /* First pass: Find the name of the base filename.
24473 This filename is needed in order to process all macros whose definition
24474 (or undefinition) comes from the command line. These macros are defined
24475 before the first DW_MACINFO_start_file entry, and yet still need to be
24476 associated to the base file.
24477
24478 To determine the base file name, we scan the macro definitions until we
24479 reach the first DW_MACINFO_start_file entry. We then initialize
24480 CURRENT_FILE accordingly so that any macro definition found before the
24481 first DW_MACINFO_start_file can still be associated to the base file. */
24482
24483 mac_ptr = section->buffer + offset;
24484 mac_end = section->buffer + section->size;
24485
24486 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24487 &offset_size, section_is_gnu);
24488 if (mac_ptr == NULL)
24489 {
24490 /* We already issued a complaint. */
24491 return;
24492 }
24493
24494 do
24495 {
24496 /* Do we at least have room for a macinfo type byte? */
24497 if (mac_ptr >= mac_end)
24498 {
24499 /* Complaint is printed during the second pass as GDB will probably
24500 stop the first pass earlier upon finding
24501 DW_MACINFO_start_file. */
24502 break;
24503 }
24504
24505 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24506 mac_ptr++;
24507
24508 /* Note that we rely on the fact that the corresponding GNU and
24509 DWARF constants are the same. */
24510 DIAGNOSTIC_PUSH
24511 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24512 switch (macinfo_type)
24513 {
24514 /* A zero macinfo type indicates the end of the macro
24515 information. */
24516 case 0:
24517 break;
24518
24519 case DW_MACRO_define:
24520 case DW_MACRO_undef:
24521 /* Only skip the data by MAC_PTR. */
24522 {
24523 unsigned int bytes_read;
24524
24525 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24526 mac_ptr += bytes_read;
24527 read_direct_string (abfd, mac_ptr, &bytes_read);
24528 mac_ptr += bytes_read;
24529 }
24530 break;
24531
24532 case DW_MACRO_start_file:
24533 {
24534 unsigned int bytes_read;
24535 int line, file;
24536
24537 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24538 mac_ptr += bytes_read;
24539 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24540 mac_ptr += bytes_read;
24541
24542 current_file = macro_start_file (cu, file, line, current_file, lh);
24543 }
24544 break;
24545
24546 case DW_MACRO_end_file:
24547 /* No data to skip by MAC_PTR. */
24548 break;
24549
24550 case DW_MACRO_define_strp:
24551 case DW_MACRO_undef_strp:
24552 case DW_MACRO_define_sup:
24553 case DW_MACRO_undef_sup:
24554 {
24555 unsigned int bytes_read;
24556
24557 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24558 mac_ptr += bytes_read;
24559 mac_ptr += offset_size;
24560 }
24561 break;
24562
24563 case DW_MACRO_import:
24564 case DW_MACRO_import_sup:
24565 /* Note that, according to the spec, a transparent include
24566 chain cannot call DW_MACRO_start_file. So, we can just
24567 skip this opcode. */
24568 mac_ptr += offset_size;
24569 break;
24570
24571 case DW_MACINFO_vendor_ext:
24572 /* Only skip the data by MAC_PTR. */
24573 if (!section_is_gnu)
24574 {
24575 unsigned int bytes_read;
24576
24577 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24578 mac_ptr += bytes_read;
24579 read_direct_string (abfd, mac_ptr, &bytes_read);
24580 mac_ptr += bytes_read;
24581 }
24582 /* FALLTHROUGH */
24583
24584 default:
24585 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24586 mac_ptr, mac_end, abfd, offset_size,
24587 section);
24588 if (mac_ptr == NULL)
24589 return;
24590 break;
24591 }
24592 DIAGNOSTIC_POP
24593 } while (macinfo_type != 0 && current_file == NULL);
24594
24595 /* Second pass: Process all entries.
24596
24597 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24598 command-line macro definitions/undefinitions. This flag is unset when we
24599 reach the first DW_MACINFO_start_file entry. */
24600
24601 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24602 htab_eq_pointer,
24603 NULL, xcalloc, xfree));
24604 mac_ptr = section->buffer + offset;
24605 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24606 *slot = (void *) mac_ptr;
24607 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
24608 current_file, lh, section,
24609 section_is_gnu, 0, offset_size,
24610 include_hash.get ());
24611 }
24612
24613 /* Check if the attribute's form is a DW_FORM_block*
24614 if so return true else false. */
24615
24616 static int
24617 attr_form_is_block (const struct attribute *attr)
24618 {
24619 return (attr == NULL ? 0 :
24620 attr->form == DW_FORM_block1
24621 || attr->form == DW_FORM_block2
24622 || attr->form == DW_FORM_block4
24623 || attr->form == DW_FORM_block
24624 || attr->form == DW_FORM_exprloc);
24625 }
24626
24627 /* Return non-zero if ATTR's value is a section offset --- classes
24628 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24629 You may use DW_UNSND (attr) to retrieve such offsets.
24630
24631 Section 7.5.4, "Attribute Encodings", explains that no attribute
24632 may have a value that belongs to more than one of these classes; it
24633 would be ambiguous if we did, because we use the same forms for all
24634 of them. */
24635
24636 static int
24637 attr_form_is_section_offset (const struct attribute *attr)
24638 {
24639 return (attr->form == DW_FORM_data4
24640 || attr->form == DW_FORM_data8
24641 || attr->form == DW_FORM_sec_offset);
24642 }
24643
24644 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24645 zero otherwise. When this function returns true, you can apply
24646 dwarf2_get_attr_constant_value to it.
24647
24648 However, note that for some attributes you must check
24649 attr_form_is_section_offset before using this test. DW_FORM_data4
24650 and DW_FORM_data8 are members of both the constant class, and of
24651 the classes that contain offsets into other debug sections
24652 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24653 that, if an attribute's can be either a constant or one of the
24654 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24655 taken as section offsets, not constants.
24656
24657 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24658 cannot handle that. */
24659
24660 static int
24661 attr_form_is_constant (const struct attribute *attr)
24662 {
24663 switch (attr->form)
24664 {
24665 case DW_FORM_sdata:
24666 case DW_FORM_udata:
24667 case DW_FORM_data1:
24668 case DW_FORM_data2:
24669 case DW_FORM_data4:
24670 case DW_FORM_data8:
24671 case DW_FORM_implicit_const:
24672 return 1;
24673 default:
24674 return 0;
24675 }
24676 }
24677
24678
24679 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24680 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24681
24682 static int
24683 attr_form_is_ref (const struct attribute *attr)
24684 {
24685 switch (attr->form)
24686 {
24687 case DW_FORM_ref_addr:
24688 case DW_FORM_ref1:
24689 case DW_FORM_ref2:
24690 case DW_FORM_ref4:
24691 case DW_FORM_ref8:
24692 case DW_FORM_ref_udata:
24693 case DW_FORM_GNU_ref_alt:
24694 return 1;
24695 default:
24696 return 0;
24697 }
24698 }
24699
24700 /* Return the .debug_loc section to use for CU.
24701 For DWO files use .debug_loc.dwo. */
24702
24703 static struct dwarf2_section_info *
24704 cu_debug_loc_section (struct dwarf2_cu *cu)
24705 {
24706 struct dwarf2_per_objfile *dwarf2_per_objfile
24707 = cu->per_cu->dwarf2_per_objfile;
24708
24709 if (cu->dwo_unit)
24710 {
24711 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24712
24713 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24714 }
24715 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24716 : &dwarf2_per_objfile->loc);
24717 }
24718
24719 /* A helper function that fills in a dwarf2_loclist_baton. */
24720
24721 static void
24722 fill_in_loclist_baton (struct dwarf2_cu *cu,
24723 struct dwarf2_loclist_baton *baton,
24724 const struct attribute *attr)
24725 {
24726 struct dwarf2_per_objfile *dwarf2_per_objfile
24727 = cu->per_cu->dwarf2_per_objfile;
24728 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24729
24730 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24731
24732 baton->per_cu = cu->per_cu;
24733 gdb_assert (baton->per_cu);
24734 /* We don't know how long the location list is, but make sure we
24735 don't run off the edge of the section. */
24736 baton->size = section->size - DW_UNSND (attr);
24737 baton->data = section->buffer + DW_UNSND (attr);
24738 baton->base_address = cu->base_address;
24739 baton->from_dwo = cu->dwo_unit != NULL;
24740 }
24741
24742 static void
24743 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24744 struct dwarf2_cu *cu, int is_block)
24745 {
24746 struct dwarf2_per_objfile *dwarf2_per_objfile
24747 = cu->per_cu->dwarf2_per_objfile;
24748 struct objfile *objfile = dwarf2_per_objfile->objfile;
24749 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24750
24751 if (attr_form_is_section_offset (attr)
24752 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24753 the section. If so, fall through to the complaint in the
24754 other branch. */
24755 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24756 {
24757 struct dwarf2_loclist_baton *baton;
24758
24759 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24760
24761 fill_in_loclist_baton (cu, baton, attr);
24762
24763 if (cu->base_known == 0)
24764 complaint (_("Location list used without "
24765 "specifying the CU base address."));
24766
24767 SYMBOL_ACLASS_INDEX (sym) = (is_block
24768 ? dwarf2_loclist_block_index
24769 : dwarf2_loclist_index);
24770 SYMBOL_LOCATION_BATON (sym) = baton;
24771 }
24772 else
24773 {
24774 struct dwarf2_locexpr_baton *baton;
24775
24776 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24777 baton->per_cu = cu->per_cu;
24778 gdb_assert (baton->per_cu);
24779
24780 if (attr_form_is_block (attr))
24781 {
24782 /* Note that we're just copying the block's data pointer
24783 here, not the actual data. We're still pointing into the
24784 info_buffer for SYM's objfile; right now we never release
24785 that buffer, but when we do clean up properly this may
24786 need to change. */
24787 baton->size = DW_BLOCK (attr)->size;
24788 baton->data = DW_BLOCK (attr)->data;
24789 }
24790 else
24791 {
24792 dwarf2_invalid_attrib_class_complaint ("location description",
24793 SYMBOL_NATURAL_NAME (sym));
24794 baton->size = 0;
24795 }
24796
24797 SYMBOL_ACLASS_INDEX (sym) = (is_block
24798 ? dwarf2_locexpr_block_index
24799 : dwarf2_locexpr_index);
24800 SYMBOL_LOCATION_BATON (sym) = baton;
24801 }
24802 }
24803
24804 /* Return the OBJFILE associated with the compilation unit CU. If CU
24805 came from a separate debuginfo file, then the master objfile is
24806 returned. */
24807
24808 struct objfile *
24809 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24810 {
24811 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24812
24813 /* Return the master objfile, so that we can report and look up the
24814 correct file containing this variable. */
24815 if (objfile->separate_debug_objfile_backlink)
24816 objfile = objfile->separate_debug_objfile_backlink;
24817
24818 return objfile;
24819 }
24820
24821 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24822 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24823 CU_HEADERP first. */
24824
24825 static const struct comp_unit_head *
24826 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24827 struct dwarf2_per_cu_data *per_cu)
24828 {
24829 const gdb_byte *info_ptr;
24830
24831 if (per_cu->cu)
24832 return &per_cu->cu->header;
24833
24834 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24835
24836 memset (cu_headerp, 0, sizeof (*cu_headerp));
24837 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24838 rcuh_kind::COMPILE);
24839
24840 return cu_headerp;
24841 }
24842
24843 /* Return the address size given in the compilation unit header for CU. */
24844
24845 int
24846 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24847 {
24848 struct comp_unit_head cu_header_local;
24849 const struct comp_unit_head *cu_headerp;
24850
24851 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24852
24853 return cu_headerp->addr_size;
24854 }
24855
24856 /* Return the offset size given in the compilation unit header for CU. */
24857
24858 int
24859 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24860 {
24861 struct comp_unit_head cu_header_local;
24862 const struct comp_unit_head *cu_headerp;
24863
24864 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24865
24866 return cu_headerp->offset_size;
24867 }
24868
24869 /* See its dwarf2loc.h declaration. */
24870
24871 int
24872 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24873 {
24874 struct comp_unit_head cu_header_local;
24875 const struct comp_unit_head *cu_headerp;
24876
24877 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24878
24879 if (cu_headerp->version == 2)
24880 return cu_headerp->addr_size;
24881 else
24882 return cu_headerp->offset_size;
24883 }
24884
24885 /* Return the text offset of the CU. The returned offset comes from
24886 this CU's objfile. If this objfile came from a separate debuginfo
24887 file, then the offset may be different from the corresponding
24888 offset in the parent objfile. */
24889
24890 CORE_ADDR
24891 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24892 {
24893 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24894
24895 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24896 }
24897
24898 /* Return DWARF version number of PER_CU. */
24899
24900 short
24901 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24902 {
24903 return per_cu->dwarf_version;
24904 }
24905
24906 /* Locate the .debug_info compilation unit from CU's objfile which contains
24907 the DIE at OFFSET. Raises an error on failure. */
24908
24909 static struct dwarf2_per_cu_data *
24910 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24911 unsigned int offset_in_dwz,
24912 struct dwarf2_per_objfile *dwarf2_per_objfile)
24913 {
24914 struct dwarf2_per_cu_data *this_cu;
24915 int low, high;
24916 const sect_offset *cu_off;
24917
24918 low = 0;
24919 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24920 while (high > low)
24921 {
24922 struct dwarf2_per_cu_data *mid_cu;
24923 int mid = low + (high - low) / 2;
24924
24925 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24926 cu_off = &mid_cu->sect_off;
24927 if (mid_cu->is_dwz > offset_in_dwz
24928 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
24929 high = mid;
24930 else
24931 low = mid + 1;
24932 }
24933 gdb_assert (low == high);
24934 this_cu = dwarf2_per_objfile->all_comp_units[low];
24935 cu_off = &this_cu->sect_off;
24936 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
24937 {
24938 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24939 error (_("Dwarf Error: could not find partial DIE containing "
24940 "offset %s [in module %s]"),
24941 sect_offset_str (sect_off),
24942 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24943
24944 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24945 <= sect_off);
24946 return dwarf2_per_objfile->all_comp_units[low-1];
24947 }
24948 else
24949 {
24950 this_cu = dwarf2_per_objfile->all_comp_units[low];
24951 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24952 && sect_off >= this_cu->sect_off + this_cu->length)
24953 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24954 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24955 return this_cu;
24956 }
24957 }
24958
24959 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24960
24961 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24962 : per_cu (per_cu_),
24963 mark (0),
24964 has_loclist (0),
24965 checked_producer (0),
24966 producer_is_gxx_lt_4_6 (0),
24967 producer_is_gcc_lt_4_3 (0),
24968 producer_is_icc_lt_14 (0),
24969 processing_has_namespace_info (0)
24970 {
24971 per_cu->cu = this;
24972 }
24973
24974 /* Destroy a dwarf2_cu. */
24975
24976 dwarf2_cu::~dwarf2_cu ()
24977 {
24978 per_cu->cu = NULL;
24979 }
24980
24981 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24982
24983 static void
24984 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24985 enum language pretend_language)
24986 {
24987 struct attribute *attr;
24988
24989 /* Set the language we're debugging. */
24990 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24991 if (attr)
24992 set_cu_language (DW_UNSND (attr), cu);
24993 else
24994 {
24995 cu->language = pretend_language;
24996 cu->language_defn = language_def (cu->language);
24997 }
24998
24999 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25000 }
25001
25002 /* Increase the age counter on each cached compilation unit, and free
25003 any that are too old. */
25004
25005 static void
25006 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25007 {
25008 struct dwarf2_per_cu_data *per_cu, **last_chain;
25009
25010 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25011 per_cu = dwarf2_per_objfile->read_in_chain;
25012 while (per_cu != NULL)
25013 {
25014 per_cu->cu->last_used ++;
25015 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25016 dwarf2_mark (per_cu->cu);
25017 per_cu = per_cu->cu->read_in_chain;
25018 }
25019
25020 per_cu = dwarf2_per_objfile->read_in_chain;
25021 last_chain = &dwarf2_per_objfile->read_in_chain;
25022 while (per_cu != NULL)
25023 {
25024 struct dwarf2_per_cu_data *next_cu;
25025
25026 next_cu = per_cu->cu->read_in_chain;
25027
25028 if (!per_cu->cu->mark)
25029 {
25030 delete per_cu->cu;
25031 *last_chain = next_cu;
25032 }
25033 else
25034 last_chain = &per_cu->cu->read_in_chain;
25035
25036 per_cu = next_cu;
25037 }
25038 }
25039
25040 /* Remove a single compilation unit from the cache. */
25041
25042 static void
25043 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25044 {
25045 struct dwarf2_per_cu_data *per_cu, **last_chain;
25046 struct dwarf2_per_objfile *dwarf2_per_objfile
25047 = target_per_cu->dwarf2_per_objfile;
25048
25049 per_cu = dwarf2_per_objfile->read_in_chain;
25050 last_chain = &dwarf2_per_objfile->read_in_chain;
25051 while (per_cu != NULL)
25052 {
25053 struct dwarf2_per_cu_data *next_cu;
25054
25055 next_cu = per_cu->cu->read_in_chain;
25056
25057 if (per_cu == target_per_cu)
25058 {
25059 delete per_cu->cu;
25060 per_cu->cu = NULL;
25061 *last_chain = next_cu;
25062 break;
25063 }
25064 else
25065 last_chain = &per_cu->cu->read_in_chain;
25066
25067 per_cu = next_cu;
25068 }
25069 }
25070
25071 /* Cleanup function for the dwarf2_per_objfile data. */
25072
25073 static void
25074 dwarf2_free_objfile (struct objfile *objfile, void *datum)
25075 {
25076 struct dwarf2_per_objfile *dwarf2_per_objfile
25077 = static_cast<struct dwarf2_per_objfile *> (datum);
25078
25079 delete dwarf2_per_objfile;
25080 }
25081
25082 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25083 We store these in a hash table separate from the DIEs, and preserve them
25084 when the DIEs are flushed out of cache.
25085
25086 The CU "per_cu" pointer is needed because offset alone is not enough to
25087 uniquely identify the type. A file may have multiple .debug_types sections,
25088 or the type may come from a DWO file. Furthermore, while it's more logical
25089 to use per_cu->section+offset, with Fission the section with the data is in
25090 the DWO file but we don't know that section at the point we need it.
25091 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25092 because we can enter the lookup routine, get_die_type_at_offset, from
25093 outside this file, and thus won't necessarily have PER_CU->cu.
25094 Fortunately, PER_CU is stable for the life of the objfile. */
25095
25096 struct dwarf2_per_cu_offset_and_type
25097 {
25098 const struct dwarf2_per_cu_data *per_cu;
25099 sect_offset sect_off;
25100 struct type *type;
25101 };
25102
25103 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25104
25105 static hashval_t
25106 per_cu_offset_and_type_hash (const void *item)
25107 {
25108 const struct dwarf2_per_cu_offset_and_type *ofs
25109 = (const struct dwarf2_per_cu_offset_and_type *) item;
25110
25111 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25112 }
25113
25114 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25115
25116 static int
25117 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25118 {
25119 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25120 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25121 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25122 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25123
25124 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25125 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25126 }
25127
25128 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25129 table if necessary. For convenience, return TYPE.
25130
25131 The DIEs reading must have careful ordering to:
25132 * Not cause infite loops trying to read in DIEs as a prerequisite for
25133 reading current DIE.
25134 * Not trying to dereference contents of still incompletely read in types
25135 while reading in other DIEs.
25136 * Enable referencing still incompletely read in types just by a pointer to
25137 the type without accessing its fields.
25138
25139 Therefore caller should follow these rules:
25140 * Try to fetch any prerequisite types we may need to build this DIE type
25141 before building the type and calling set_die_type.
25142 * After building type call set_die_type for current DIE as soon as
25143 possible before fetching more types to complete the current type.
25144 * Make the type as complete as possible before fetching more types. */
25145
25146 static struct type *
25147 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25148 {
25149 struct dwarf2_per_objfile *dwarf2_per_objfile
25150 = cu->per_cu->dwarf2_per_objfile;
25151 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25152 struct objfile *objfile = dwarf2_per_objfile->objfile;
25153 struct attribute *attr;
25154 struct dynamic_prop prop;
25155
25156 /* For Ada types, make sure that the gnat-specific data is always
25157 initialized (if not already set). There are a few types where
25158 we should not be doing so, because the type-specific area is
25159 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25160 where the type-specific area is used to store the floatformat).
25161 But this is not a problem, because the gnat-specific information
25162 is actually not needed for these types. */
25163 if (need_gnat_info (cu)
25164 && TYPE_CODE (type) != TYPE_CODE_FUNC
25165 && TYPE_CODE (type) != TYPE_CODE_FLT
25166 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25167 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25168 && TYPE_CODE (type) != TYPE_CODE_METHOD
25169 && !HAVE_GNAT_AUX_INFO (type))
25170 INIT_GNAT_SPECIFIC (type);
25171
25172 /* Read DW_AT_allocated and set in type. */
25173 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25174 if (attr_form_is_block (attr))
25175 {
25176 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25177 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25178 }
25179 else if (attr != NULL)
25180 {
25181 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25182 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25183 sect_offset_str (die->sect_off));
25184 }
25185
25186 /* Read DW_AT_associated and set in type. */
25187 attr = dwarf2_attr (die, DW_AT_associated, cu);
25188 if (attr_form_is_block (attr))
25189 {
25190 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25191 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25192 }
25193 else if (attr != NULL)
25194 {
25195 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25196 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25197 sect_offset_str (die->sect_off));
25198 }
25199
25200 /* Read DW_AT_data_location and set in type. */
25201 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25202 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25203 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25204
25205 if (dwarf2_per_objfile->die_type_hash == NULL)
25206 {
25207 dwarf2_per_objfile->die_type_hash =
25208 htab_create_alloc_ex (127,
25209 per_cu_offset_and_type_hash,
25210 per_cu_offset_and_type_eq,
25211 NULL,
25212 &objfile->objfile_obstack,
25213 hashtab_obstack_allocate,
25214 dummy_obstack_deallocate);
25215 }
25216
25217 ofs.per_cu = cu->per_cu;
25218 ofs.sect_off = die->sect_off;
25219 ofs.type = type;
25220 slot = (struct dwarf2_per_cu_offset_and_type **)
25221 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25222 if (*slot)
25223 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25224 sect_offset_str (die->sect_off));
25225 *slot = XOBNEW (&objfile->objfile_obstack,
25226 struct dwarf2_per_cu_offset_and_type);
25227 **slot = ofs;
25228 return type;
25229 }
25230
25231 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25232 or return NULL if the die does not have a saved type. */
25233
25234 static struct type *
25235 get_die_type_at_offset (sect_offset sect_off,
25236 struct dwarf2_per_cu_data *per_cu)
25237 {
25238 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25239 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25240
25241 if (dwarf2_per_objfile->die_type_hash == NULL)
25242 return NULL;
25243
25244 ofs.per_cu = per_cu;
25245 ofs.sect_off = sect_off;
25246 slot = ((struct dwarf2_per_cu_offset_and_type *)
25247 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25248 if (slot)
25249 return slot->type;
25250 else
25251 return NULL;
25252 }
25253
25254 /* Look up the type for DIE in CU in die_type_hash,
25255 or return NULL if DIE does not have a saved type. */
25256
25257 static struct type *
25258 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25259 {
25260 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25261 }
25262
25263 /* Add a dependence relationship from CU to REF_PER_CU. */
25264
25265 static void
25266 dwarf2_add_dependence (struct dwarf2_cu *cu,
25267 struct dwarf2_per_cu_data *ref_per_cu)
25268 {
25269 void **slot;
25270
25271 if (cu->dependencies == NULL)
25272 cu->dependencies
25273 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25274 NULL, &cu->comp_unit_obstack,
25275 hashtab_obstack_allocate,
25276 dummy_obstack_deallocate);
25277
25278 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25279 if (*slot == NULL)
25280 *slot = ref_per_cu;
25281 }
25282
25283 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25284 Set the mark field in every compilation unit in the
25285 cache that we must keep because we are keeping CU. */
25286
25287 static int
25288 dwarf2_mark_helper (void **slot, void *data)
25289 {
25290 struct dwarf2_per_cu_data *per_cu;
25291
25292 per_cu = (struct dwarf2_per_cu_data *) *slot;
25293
25294 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25295 reading of the chain. As such dependencies remain valid it is not much
25296 useful to track and undo them during QUIT cleanups. */
25297 if (per_cu->cu == NULL)
25298 return 1;
25299
25300 if (per_cu->cu->mark)
25301 return 1;
25302 per_cu->cu->mark = 1;
25303
25304 if (per_cu->cu->dependencies != NULL)
25305 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25306
25307 return 1;
25308 }
25309
25310 /* Set the mark field in CU and in every other compilation unit in the
25311 cache that we must keep because we are keeping CU. */
25312
25313 static void
25314 dwarf2_mark (struct dwarf2_cu *cu)
25315 {
25316 if (cu->mark)
25317 return;
25318 cu->mark = 1;
25319 if (cu->dependencies != NULL)
25320 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25321 }
25322
25323 static void
25324 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25325 {
25326 while (per_cu)
25327 {
25328 per_cu->cu->mark = 0;
25329 per_cu = per_cu->cu->read_in_chain;
25330 }
25331 }
25332
25333 /* Trivial hash function for partial_die_info: the hash value of a DIE
25334 is its offset in .debug_info for this objfile. */
25335
25336 static hashval_t
25337 partial_die_hash (const void *item)
25338 {
25339 const struct partial_die_info *part_die
25340 = (const struct partial_die_info *) item;
25341
25342 return to_underlying (part_die->sect_off);
25343 }
25344
25345 /* Trivial comparison function for partial_die_info structures: two DIEs
25346 are equal if they have the same offset. */
25347
25348 static int
25349 partial_die_eq (const void *item_lhs, const void *item_rhs)
25350 {
25351 const struct partial_die_info *part_die_lhs
25352 = (const struct partial_die_info *) item_lhs;
25353 const struct partial_die_info *part_die_rhs
25354 = (const struct partial_die_info *) item_rhs;
25355
25356 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25357 }
25358
25359 struct cmd_list_element *set_dwarf_cmdlist;
25360 struct cmd_list_element *show_dwarf_cmdlist;
25361
25362 static void
25363 set_dwarf_cmd (const char *args, int from_tty)
25364 {
25365 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25366 gdb_stdout);
25367 }
25368
25369 static void
25370 show_dwarf_cmd (const char *args, int from_tty)
25371 {
25372 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25373 }
25374
25375 int dwarf_always_disassemble;
25376
25377 static void
25378 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25379 struct cmd_list_element *c, const char *value)
25380 {
25381 fprintf_filtered (file,
25382 _("Whether to always disassemble "
25383 "DWARF expressions is %s.\n"),
25384 value);
25385 }
25386
25387 static void
25388 show_check_physname (struct ui_file *file, int from_tty,
25389 struct cmd_list_element *c, const char *value)
25390 {
25391 fprintf_filtered (file,
25392 _("Whether to check \"physname\" is %s.\n"),
25393 value);
25394 }
25395
25396 void
25397 _initialize_dwarf2_read (void)
25398 {
25399 dwarf2_objfile_data_key
25400 = register_objfile_data_with_cleanup (nullptr, dwarf2_free_objfile);
25401
25402 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25403 Set DWARF specific variables.\n\
25404 Configure DWARF variables such as the cache size"),
25405 &set_dwarf_cmdlist, "maintenance set dwarf ",
25406 0/*allow-unknown*/, &maintenance_set_cmdlist);
25407
25408 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25409 Show DWARF specific variables\n\
25410 Show DWARF variables such as the cache size"),
25411 &show_dwarf_cmdlist, "maintenance show dwarf ",
25412 0/*allow-unknown*/, &maintenance_show_cmdlist);
25413
25414 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25415 &dwarf_max_cache_age, _("\
25416 Set the upper bound on the age of cached DWARF compilation units."), _("\
25417 Show the upper bound on the age of cached DWARF compilation units."), _("\
25418 A higher limit means that cached compilation units will be stored\n\
25419 in memory longer, and more total memory will be used. Zero disables\n\
25420 caching, which can slow down startup."),
25421 NULL,
25422 show_dwarf_max_cache_age,
25423 &set_dwarf_cmdlist,
25424 &show_dwarf_cmdlist);
25425
25426 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25427 &dwarf_always_disassemble, _("\
25428 Set whether `info address' always disassembles DWARF expressions."), _("\
25429 Show whether `info address' always disassembles DWARF expressions."), _("\
25430 When enabled, DWARF expressions are always printed in an assembly-like\n\
25431 syntax. When disabled, expressions will be printed in a more\n\
25432 conversational style, when possible."),
25433 NULL,
25434 show_dwarf_always_disassemble,
25435 &set_dwarf_cmdlist,
25436 &show_dwarf_cmdlist);
25437
25438 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25439 Set debugging of the DWARF reader."), _("\
25440 Show debugging of the DWARF reader."), _("\
25441 When enabled (non-zero), debugging messages are printed during DWARF\n\
25442 reading and symtab expansion. A value of 1 (one) provides basic\n\
25443 information. A value greater than 1 provides more verbose information."),
25444 NULL,
25445 NULL,
25446 &setdebuglist, &showdebuglist);
25447
25448 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25449 Set debugging of the DWARF DIE reader."), _("\
25450 Show debugging of the DWARF DIE reader."), _("\
25451 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25452 The value is the maximum depth to print."),
25453 NULL,
25454 NULL,
25455 &setdebuglist, &showdebuglist);
25456
25457 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25458 Set debugging of the dwarf line reader."), _("\
25459 Show debugging of the dwarf line reader."), _("\
25460 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25461 A value of 1 (one) provides basic information.\n\
25462 A value greater than 1 provides more verbose information."),
25463 NULL,
25464 NULL,
25465 &setdebuglist, &showdebuglist);
25466
25467 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25468 Set cross-checking of \"physname\" code against demangler."), _("\
25469 Show cross-checking of \"physname\" code against demangler."), _("\
25470 When enabled, GDB's internal \"physname\" code is checked against\n\
25471 the demangler."),
25472 NULL, show_check_physname,
25473 &setdebuglist, &showdebuglist);
25474
25475 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25476 no_class, &use_deprecated_index_sections, _("\
25477 Set whether to use deprecated gdb_index sections."), _("\
25478 Show whether to use deprecated gdb_index sections."), _("\
25479 When enabled, deprecated .gdb_index sections are used anyway.\n\
25480 Normally they are ignored either because of a missing feature or\n\
25481 performance issue.\n\
25482 Warning: This option must be enabled before gdb reads the file."),
25483 NULL,
25484 NULL,
25485 &setlist, &showlist);
25486
25487 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25488 &dwarf2_locexpr_funcs);
25489 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25490 &dwarf2_loclist_funcs);
25491
25492 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25493 &dwarf2_block_frame_base_locexpr_funcs);
25494 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25495 &dwarf2_block_frame_base_loclist_funcs);
25496
25497 #if GDB_SELF_TEST
25498 selftests::register_test ("dw2_expand_symtabs_matching",
25499 selftests::dw2_expand_symtabs_matching::run_test);
25500 #endif
25501 }
This page took 0.545902 seconds and 5 git commands to generate.