Use std::unique_ptr in dwarf2_read_debug_names
[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 generic symbol table building routines have separate lists for
439 file scope symbols and all all other scopes (local scopes). So
440 we need to select the right one to pass to add_symbol_to_list().
441 We do it by keeping a pointer to the correct list in list_in_scope.
442
443 FIXME: The original dwarf code just treated the file scope as the
444 first local scope, and all other local scopes as nested local
445 scopes, and worked fine. Check to see if we really need to
446 distinguish these in buildsym.c. */
447 struct pending **list_in_scope = nullptr;
448
449 /* Hash table holding all the loaded partial DIEs
450 with partial_die->offset.SECT_OFF as hash. */
451 htab_t partial_dies = nullptr;
452
453 /* Storage for things with the same lifetime as this read-in compilation
454 unit, including partial DIEs. */
455 auto_obstack comp_unit_obstack;
456
457 /* When multiple dwarf2_cu structures are living in memory, this field
458 chains them all together, so that they can be released efficiently.
459 We will probably also want a generation counter so that most-recently-used
460 compilation units are cached... */
461 struct dwarf2_per_cu_data *read_in_chain = nullptr;
462
463 /* Backlink to our per_cu entry. */
464 struct dwarf2_per_cu_data *per_cu;
465
466 /* How many compilation units ago was this CU last referenced? */
467 int last_used = 0;
468
469 /* A hash table of DIE cu_offset for following references with
470 die_info->offset.sect_off as hash. */
471 htab_t die_hash = nullptr;
472
473 /* Full DIEs if read in. */
474 struct die_info *dies = nullptr;
475
476 /* A set of pointers to dwarf2_per_cu_data objects for compilation
477 units referenced by this one. Only set during full symbol processing;
478 partial symbol tables do not have dependencies. */
479 htab_t dependencies = nullptr;
480
481 /* Header data from the line table, during full symbol processing. */
482 struct line_header *line_header = nullptr;
483 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
484 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
485 this is the DW_TAG_compile_unit die for this CU. We'll hold on
486 to the line header as long as this DIE is being processed. See
487 process_die_scope. */
488 die_info *line_header_die_owner = nullptr;
489
490 /* A list of methods which need to have physnames computed
491 after all type information has been read. */
492 std::vector<delayed_method_info> method_list;
493
494 /* To be copied to symtab->call_site_htab. */
495 htab_t call_site_htab = nullptr;
496
497 /* Non-NULL if this CU came from a DWO file.
498 There is an invariant here that is important to remember:
499 Except for attributes copied from the top level DIE in the "main"
500 (or "stub") file in preparation for reading the DWO file
501 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
502 Either there isn't a DWO file (in which case this is NULL and the point
503 is moot), or there is and either we're not going to read it (in which
504 case this is NULL) or there is and we are reading it (in which case this
505 is non-NULL). */
506 struct dwo_unit *dwo_unit = nullptr;
507
508 /* The DW_AT_addr_base attribute if present, zero otherwise
509 (zero is a valid value though).
510 Note this value comes from the Fission stub CU/TU's DIE. */
511 ULONGEST addr_base = 0;
512
513 /* The DW_AT_ranges_base attribute if present, zero otherwise
514 (zero is a valid value though).
515 Note this value comes from the Fission stub CU/TU's DIE.
516 Also note that the value is zero in the non-DWO case so this value can
517 be used without needing to know whether DWO files are in use or not.
518 N.B. This does not apply to DW_AT_ranges appearing in
519 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
520 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
521 DW_AT_ranges_base *would* have to be applied, and we'd have to care
522 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
523 ULONGEST ranges_base = 0;
524
525 /* When reading debug info generated by older versions of rustc, we
526 have to rewrite some union types to be struct types with a
527 variant part. This rewriting must be done after the CU is fully
528 read in, because otherwise at the point of rewriting some struct
529 type might not have been fully processed. So, we keep a list of
530 all such types here and process them after expansion. */
531 std::vector<struct type *> rust_unions;
532
533 /* Mark used when releasing cached dies. */
534 unsigned int mark : 1;
535
536 /* This CU references .debug_loc. See the symtab->locations_valid field.
537 This test is imperfect as there may exist optimized debug code not using
538 any location list and still facing inlining issues if handled as
539 unoptimized code. For a future better test see GCC PR other/32998. */
540 unsigned int has_loclist : 1;
541
542 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
543 if all the producer_is_* fields are valid. This information is cached
544 because profiling CU expansion showed excessive time spent in
545 producer_is_gxx_lt_4_6. */
546 unsigned int checked_producer : 1;
547 unsigned int producer_is_gxx_lt_4_6 : 1;
548 unsigned int producer_is_gcc_lt_4_3 : 1;
549 unsigned int producer_is_icc_lt_14 : 1;
550
551 /* When set, the file that we're processing is known to have
552 debugging info for C++ namespaces. GCC 3.3.x did not produce
553 this information, but later versions do. */
554
555 unsigned int processing_has_namespace_info : 1;
556
557 struct partial_die_info *find_partial_die (sect_offset sect_off);
558 };
559
560 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
561 This includes type_unit_group and quick_file_names. */
562
563 struct stmt_list_hash
564 {
565 /* The DWO unit this table is from or NULL if there is none. */
566 struct dwo_unit *dwo_unit;
567
568 /* Offset in .debug_line or .debug_line.dwo. */
569 sect_offset line_sect_off;
570 };
571
572 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
573 an object of this type. */
574
575 struct type_unit_group
576 {
577 /* dwarf2read.c's main "handle" on a TU symtab.
578 To simplify things we create an artificial CU that "includes" all the
579 type units using this stmt_list so that the rest of the code still has
580 a "per_cu" handle on the symtab.
581 This PER_CU is recognized by having no section. */
582 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
583 struct dwarf2_per_cu_data per_cu;
584
585 /* The TUs that share this DW_AT_stmt_list entry.
586 This is added to while parsing type units to build partial symtabs,
587 and is deleted afterwards and not used again. */
588 VEC (sig_type_ptr) *tus;
589
590 /* The compunit symtab.
591 Type units in a group needn't all be defined in the same source file,
592 so we create an essentially anonymous symtab as the compunit symtab. */
593 struct compunit_symtab *compunit_symtab;
594
595 /* The data used to construct the hash key. */
596 struct stmt_list_hash hash;
597
598 /* The number of symtabs from the line header.
599 The value here must match line_header.num_file_names. */
600 unsigned int num_symtabs;
601
602 /* The symbol tables for this TU (obtained from the files listed in
603 DW_AT_stmt_list).
604 WARNING: The order of entries here must match the order of entries
605 in the line header. After the first TU using this type_unit_group, the
606 line header for the subsequent TUs is recreated from this. This is done
607 because we need to use the same symtabs for each TU using the same
608 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
609 there's no guarantee the line header doesn't have duplicate entries. */
610 struct symtab **symtabs;
611 };
612
613 /* These sections are what may appear in a (real or virtual) DWO file. */
614
615 struct dwo_sections
616 {
617 struct dwarf2_section_info abbrev;
618 struct dwarf2_section_info line;
619 struct dwarf2_section_info loc;
620 struct dwarf2_section_info loclists;
621 struct dwarf2_section_info macinfo;
622 struct dwarf2_section_info macro;
623 struct dwarf2_section_info str;
624 struct dwarf2_section_info str_offsets;
625 /* In the case of a virtual DWO file, these two are unused. */
626 struct dwarf2_section_info info;
627 VEC (dwarf2_section_info_def) *types;
628 };
629
630 /* CUs/TUs in DWP/DWO files. */
631
632 struct dwo_unit
633 {
634 /* Backlink to the containing struct dwo_file. */
635 struct dwo_file *dwo_file;
636
637 /* The "id" that distinguishes this CU/TU.
638 .debug_info calls this "dwo_id", .debug_types calls this "signature".
639 Since signatures came first, we stick with it for consistency. */
640 ULONGEST signature;
641
642 /* The section this CU/TU lives in, in the DWO file. */
643 struct dwarf2_section_info *section;
644
645 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
646 sect_offset sect_off;
647 unsigned int length;
648
649 /* For types, offset in the type's DIE of the type defined by this TU. */
650 cu_offset type_offset_in_tu;
651 };
652
653 /* include/dwarf2.h defines the DWP section codes.
654 It defines a max value but it doesn't define a min value, which we
655 use for error checking, so provide one. */
656
657 enum dwp_v2_section_ids
658 {
659 DW_SECT_MIN = 1
660 };
661
662 /* Data for one DWO file.
663
664 This includes virtual DWO files (a virtual DWO file is a DWO file as it
665 appears in a DWP file). DWP files don't really have DWO files per se -
666 comdat folding of types "loses" the DWO file they came from, and from
667 a high level view DWP files appear to contain a mass of random types.
668 However, to maintain consistency with the non-DWP case we pretend DWP
669 files contain virtual DWO files, and we assign each TU with one virtual
670 DWO file (generally based on the line and abbrev section offsets -
671 a heuristic that seems to work in practice). */
672
673 struct dwo_file
674 {
675 /* The DW_AT_GNU_dwo_name attribute.
676 For virtual DWO files the name is constructed from the section offsets
677 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
678 from related CU+TUs. */
679 const char *dwo_name;
680
681 /* The DW_AT_comp_dir attribute. */
682 const char *comp_dir;
683
684 /* The bfd, when the file is open. Otherwise this is NULL.
685 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
686 bfd *dbfd;
687
688 /* The sections that make up this DWO file.
689 Remember that for virtual DWO files in DWP V2, these are virtual
690 sections (for lack of a better name). */
691 struct dwo_sections sections;
692
693 /* The CUs in the file.
694 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
695 an extension to handle LLVM's Link Time Optimization output (where
696 multiple source files may be compiled into a single object/dwo pair). */
697 htab_t cus;
698
699 /* Table of TUs in the file.
700 Each element is a struct dwo_unit. */
701 htab_t tus;
702 };
703
704 /* These sections are what may appear in a DWP file. */
705
706 struct dwp_sections
707 {
708 /* These are used by both DWP version 1 and 2. */
709 struct dwarf2_section_info str;
710 struct dwarf2_section_info cu_index;
711 struct dwarf2_section_info tu_index;
712
713 /* These are only used by DWP version 2 files.
714 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
715 sections are referenced by section number, and are not recorded here.
716 In DWP version 2 there is at most one copy of all these sections, each
717 section being (effectively) comprised of the concatenation of all of the
718 individual sections that exist in the version 1 format.
719 To keep the code simple we treat each of these concatenated pieces as a
720 section itself (a virtual section?). */
721 struct dwarf2_section_info abbrev;
722 struct dwarf2_section_info info;
723 struct dwarf2_section_info line;
724 struct dwarf2_section_info loc;
725 struct dwarf2_section_info macinfo;
726 struct dwarf2_section_info macro;
727 struct dwarf2_section_info str_offsets;
728 struct dwarf2_section_info types;
729 };
730
731 /* These sections are what may appear in a virtual DWO file in DWP version 1.
732 A virtual DWO file is a DWO file as it appears in a DWP file. */
733
734 struct virtual_v1_dwo_sections
735 {
736 struct dwarf2_section_info abbrev;
737 struct dwarf2_section_info line;
738 struct dwarf2_section_info loc;
739 struct dwarf2_section_info macinfo;
740 struct dwarf2_section_info macro;
741 struct dwarf2_section_info str_offsets;
742 /* Each DWP hash table entry records one CU or one TU.
743 That is recorded here, and copied to dwo_unit.section. */
744 struct dwarf2_section_info info_or_types;
745 };
746
747 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
748 In version 2, the sections of the DWO files are concatenated together
749 and stored in one section of that name. Thus each ELF section contains
750 several "virtual" sections. */
751
752 struct virtual_v2_dwo_sections
753 {
754 bfd_size_type abbrev_offset;
755 bfd_size_type abbrev_size;
756
757 bfd_size_type line_offset;
758 bfd_size_type line_size;
759
760 bfd_size_type loc_offset;
761 bfd_size_type loc_size;
762
763 bfd_size_type macinfo_offset;
764 bfd_size_type macinfo_size;
765
766 bfd_size_type macro_offset;
767 bfd_size_type macro_size;
768
769 bfd_size_type str_offsets_offset;
770 bfd_size_type str_offsets_size;
771
772 /* Each DWP hash table entry records one CU or one TU.
773 That is recorded here, and copied to dwo_unit.section. */
774 bfd_size_type info_or_types_offset;
775 bfd_size_type info_or_types_size;
776 };
777
778 /* Contents of DWP hash tables. */
779
780 struct dwp_hash_table
781 {
782 uint32_t version, nr_columns;
783 uint32_t nr_units, nr_slots;
784 const gdb_byte *hash_table, *unit_table;
785 union
786 {
787 struct
788 {
789 const gdb_byte *indices;
790 } v1;
791 struct
792 {
793 /* This is indexed by column number and gives the id of the section
794 in that column. */
795 #define MAX_NR_V2_DWO_SECTIONS \
796 (1 /* .debug_info or .debug_types */ \
797 + 1 /* .debug_abbrev */ \
798 + 1 /* .debug_line */ \
799 + 1 /* .debug_loc */ \
800 + 1 /* .debug_str_offsets */ \
801 + 1 /* .debug_macro or .debug_macinfo */)
802 int section_ids[MAX_NR_V2_DWO_SECTIONS];
803 const gdb_byte *offsets;
804 const gdb_byte *sizes;
805 } v2;
806 } section_pool;
807 };
808
809 /* Data for one DWP file. */
810
811 struct dwp_file
812 {
813 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
814 : name (name_),
815 dbfd (std::move (abfd))
816 {
817 }
818
819 /* Name of the file. */
820 const char *name;
821
822 /* File format version. */
823 int version = 0;
824
825 /* The bfd. */
826 gdb_bfd_ref_ptr dbfd;
827
828 /* Section info for this file. */
829 struct dwp_sections sections {};
830
831 /* Table of CUs in the file. */
832 const struct dwp_hash_table *cus = nullptr;
833
834 /* Table of TUs in the file. */
835 const struct dwp_hash_table *tus = nullptr;
836
837 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
838 htab_t loaded_cus {};
839 htab_t loaded_tus {};
840
841 /* Table to map ELF section numbers to their sections.
842 This is only needed for the DWP V1 file format. */
843 unsigned int num_sections = 0;
844 asection **elf_sections = nullptr;
845 };
846
847 /* This represents a '.dwz' file. */
848
849 struct dwz_file
850 {
851 dwz_file (gdb_bfd_ref_ptr &&bfd)
852 : dwz_bfd (std::move (bfd))
853 {
854 }
855
856 /* A dwz file can only contain a few sections. */
857 struct dwarf2_section_info abbrev {};
858 struct dwarf2_section_info info {};
859 struct dwarf2_section_info str {};
860 struct dwarf2_section_info line {};
861 struct dwarf2_section_info macro {};
862 struct dwarf2_section_info gdb_index {};
863 struct dwarf2_section_info debug_names {};
864
865 /* The dwz's BFD. */
866 gdb_bfd_ref_ptr dwz_bfd;
867 };
868
869 /* Struct used to pass misc. parameters to read_die_and_children, et
870 al. which are used for both .debug_info and .debug_types dies.
871 All parameters here are unchanging for the life of the call. This
872 struct exists to abstract away the constant parameters of die reading. */
873
874 struct die_reader_specs
875 {
876 /* The bfd of die_section. */
877 bfd* abfd;
878
879 /* The CU of the DIE we are parsing. */
880 struct dwarf2_cu *cu;
881
882 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
883 struct dwo_file *dwo_file;
884
885 /* The section the die comes from.
886 This is either .debug_info or .debug_types, or the .dwo variants. */
887 struct dwarf2_section_info *die_section;
888
889 /* die_section->buffer. */
890 const gdb_byte *buffer;
891
892 /* The end of the buffer. */
893 const gdb_byte *buffer_end;
894
895 /* The value of the DW_AT_comp_dir attribute. */
896 const char *comp_dir;
897
898 /* The abbreviation table to use when reading the DIEs. */
899 struct abbrev_table *abbrev_table;
900 };
901
902 /* Type of function passed to init_cutu_and_read_dies, et.al. */
903 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
904 const gdb_byte *info_ptr,
905 struct die_info *comp_unit_die,
906 int has_children,
907 void *data);
908
909 /* A 1-based directory index. This is a strong typedef to prevent
910 accidentally using a directory index as a 0-based index into an
911 array/vector. */
912 enum class dir_index : unsigned int {};
913
914 /* Likewise, a 1-based file name index. */
915 enum class file_name_index : unsigned int {};
916
917 struct file_entry
918 {
919 file_entry () = default;
920
921 file_entry (const char *name_, dir_index d_index_,
922 unsigned int mod_time_, unsigned int length_)
923 : name (name_),
924 d_index (d_index_),
925 mod_time (mod_time_),
926 length (length_)
927 {}
928
929 /* Return the include directory at D_INDEX stored in LH. Returns
930 NULL if D_INDEX is out of bounds. */
931 const char *include_dir (const line_header *lh) const;
932
933 /* The file name. Note this is an observing pointer. The memory is
934 owned by debug_line_buffer. */
935 const char *name {};
936
937 /* The directory index (1-based). */
938 dir_index d_index {};
939
940 unsigned int mod_time {};
941
942 unsigned int length {};
943
944 /* True if referenced by the Line Number Program. */
945 bool included_p {};
946
947 /* The associated symbol table, if any. */
948 struct symtab *symtab {};
949 };
950
951 /* The line number information for a compilation unit (found in the
952 .debug_line section) begins with a "statement program header",
953 which contains the following information. */
954 struct line_header
955 {
956 line_header ()
957 : offset_in_dwz {}
958 {}
959
960 /* Add an entry to the include directory table. */
961 void add_include_dir (const char *include_dir);
962
963 /* Add an entry to the file name table. */
964 void add_file_name (const char *name, dir_index d_index,
965 unsigned int mod_time, unsigned int length);
966
967 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
968 is out of bounds. */
969 const char *include_dir_at (dir_index index) const
970 {
971 /* Convert directory index number (1-based) to vector index
972 (0-based). */
973 size_t vec_index = to_underlying (index) - 1;
974
975 if (vec_index >= include_dirs.size ())
976 return NULL;
977 return include_dirs[vec_index];
978 }
979
980 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
981 is out of bounds. */
982 file_entry *file_name_at (file_name_index index)
983 {
984 /* Convert file name index number (1-based) to vector index
985 (0-based). */
986 size_t vec_index = to_underlying (index) - 1;
987
988 if (vec_index >= file_names.size ())
989 return NULL;
990 return &file_names[vec_index];
991 }
992
993 /* Const version of the above. */
994 const file_entry *file_name_at (unsigned int index) const
995 {
996 if (index >= file_names.size ())
997 return NULL;
998 return &file_names[index];
999 }
1000
1001 /* Offset of line number information in .debug_line section. */
1002 sect_offset sect_off {};
1003
1004 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1005 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1006
1007 unsigned int total_length {};
1008 unsigned short version {};
1009 unsigned int header_length {};
1010 unsigned char minimum_instruction_length {};
1011 unsigned char maximum_ops_per_instruction {};
1012 unsigned char default_is_stmt {};
1013 int line_base {};
1014 unsigned char line_range {};
1015 unsigned char opcode_base {};
1016
1017 /* standard_opcode_lengths[i] is the number of operands for the
1018 standard opcode whose value is i. This means that
1019 standard_opcode_lengths[0] is unused, and the last meaningful
1020 element is standard_opcode_lengths[opcode_base - 1]. */
1021 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1022
1023 /* The include_directories table. Note these are observing
1024 pointers. The memory is owned by debug_line_buffer. */
1025 std::vector<const char *> include_dirs;
1026
1027 /* The file_names table. */
1028 std::vector<file_entry> file_names;
1029
1030 /* The start and end of the statement program following this
1031 header. These point into dwarf2_per_objfile->line_buffer. */
1032 const gdb_byte *statement_program_start {}, *statement_program_end {};
1033 };
1034
1035 typedef std::unique_ptr<line_header> line_header_up;
1036
1037 const char *
1038 file_entry::include_dir (const line_header *lh) const
1039 {
1040 return lh->include_dir_at (d_index);
1041 }
1042
1043 /* When we construct a partial symbol table entry we only
1044 need this much information. */
1045 struct partial_die_info : public allocate_on_obstack
1046 {
1047 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1048
1049 /* Disable assign but still keep copy ctor, which is needed
1050 load_partial_dies. */
1051 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1052
1053 /* Adjust the partial die before generating a symbol for it. This
1054 function may set the is_external flag or change the DIE's
1055 name. */
1056 void fixup (struct dwarf2_cu *cu);
1057
1058 /* Read a minimal amount of information into the minimal die
1059 structure. */
1060 const gdb_byte *read (const struct die_reader_specs *reader,
1061 const struct abbrev_info &abbrev,
1062 const gdb_byte *info_ptr);
1063
1064 /* Offset of this DIE. */
1065 const sect_offset sect_off;
1066
1067 /* DWARF-2 tag for this DIE. */
1068 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1069
1070 /* Assorted flags describing the data found in this DIE. */
1071 const unsigned int has_children : 1;
1072
1073 unsigned int is_external : 1;
1074 unsigned int is_declaration : 1;
1075 unsigned int has_type : 1;
1076 unsigned int has_specification : 1;
1077 unsigned int has_pc_info : 1;
1078 unsigned int may_be_inlined : 1;
1079
1080 /* This DIE has been marked DW_AT_main_subprogram. */
1081 unsigned int main_subprogram : 1;
1082
1083 /* Flag set if the SCOPE field of this structure has been
1084 computed. */
1085 unsigned int scope_set : 1;
1086
1087 /* Flag set if the DIE has a byte_size attribute. */
1088 unsigned int has_byte_size : 1;
1089
1090 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1091 unsigned int has_const_value : 1;
1092
1093 /* Flag set if any of the DIE's children are template arguments. */
1094 unsigned int has_template_arguments : 1;
1095
1096 /* Flag set if fixup has been called on this die. */
1097 unsigned int fixup_called : 1;
1098
1099 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1100 unsigned int is_dwz : 1;
1101
1102 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1103 unsigned int spec_is_dwz : 1;
1104
1105 /* The name of this DIE. Normally the value of DW_AT_name, but
1106 sometimes a default name for unnamed DIEs. */
1107 const char *name = nullptr;
1108
1109 /* The linkage name, if present. */
1110 const char *linkage_name = nullptr;
1111
1112 /* The scope to prepend to our children. This is generally
1113 allocated on the comp_unit_obstack, so will disappear
1114 when this compilation unit leaves the cache. */
1115 const char *scope = nullptr;
1116
1117 /* Some data associated with the partial DIE. The tag determines
1118 which field is live. */
1119 union
1120 {
1121 /* The location description associated with this DIE, if any. */
1122 struct dwarf_block *locdesc;
1123 /* The offset of an import, for DW_TAG_imported_unit. */
1124 sect_offset sect_off;
1125 } d {};
1126
1127 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1128 CORE_ADDR lowpc = 0;
1129 CORE_ADDR highpc = 0;
1130
1131 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1132 DW_AT_sibling, if any. */
1133 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1134 could return DW_AT_sibling values to its caller load_partial_dies. */
1135 const gdb_byte *sibling = nullptr;
1136
1137 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1138 DW_AT_specification (or DW_AT_abstract_origin or
1139 DW_AT_extension). */
1140 sect_offset spec_offset {};
1141
1142 /* Pointers to this DIE's parent, first child, and next sibling,
1143 if any. */
1144 struct partial_die_info *die_parent = nullptr;
1145 struct partial_die_info *die_child = nullptr;
1146 struct partial_die_info *die_sibling = nullptr;
1147
1148 friend struct partial_die_info *
1149 dwarf2_cu::find_partial_die (sect_offset sect_off);
1150
1151 private:
1152 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1153 partial_die_info (sect_offset sect_off)
1154 : partial_die_info (sect_off, DW_TAG_padding, 0)
1155 {
1156 }
1157
1158 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1159 int has_children_)
1160 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1161 {
1162 is_external = 0;
1163 is_declaration = 0;
1164 has_type = 0;
1165 has_specification = 0;
1166 has_pc_info = 0;
1167 may_be_inlined = 0;
1168 main_subprogram = 0;
1169 scope_set = 0;
1170 has_byte_size = 0;
1171 has_const_value = 0;
1172 has_template_arguments = 0;
1173 fixup_called = 0;
1174 is_dwz = 0;
1175 spec_is_dwz = 0;
1176 }
1177 };
1178
1179 /* This data structure holds the information of an abbrev. */
1180 struct abbrev_info
1181 {
1182 unsigned int number; /* number identifying abbrev */
1183 enum dwarf_tag tag; /* dwarf tag */
1184 unsigned short has_children; /* boolean */
1185 unsigned short num_attrs; /* number of attributes */
1186 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1187 struct abbrev_info *next; /* next in chain */
1188 };
1189
1190 struct attr_abbrev
1191 {
1192 ENUM_BITFIELD(dwarf_attribute) name : 16;
1193 ENUM_BITFIELD(dwarf_form) form : 16;
1194
1195 /* It is valid only if FORM is DW_FORM_implicit_const. */
1196 LONGEST implicit_const;
1197 };
1198
1199 /* Size of abbrev_table.abbrev_hash_table. */
1200 #define ABBREV_HASH_SIZE 121
1201
1202 /* Top level data structure to contain an abbreviation table. */
1203
1204 struct abbrev_table
1205 {
1206 explicit abbrev_table (sect_offset off)
1207 : sect_off (off)
1208 {
1209 m_abbrevs =
1210 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1211 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1212 }
1213
1214 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1215
1216 /* Allocate space for a struct abbrev_info object in
1217 ABBREV_TABLE. */
1218 struct abbrev_info *alloc_abbrev ();
1219
1220 /* Add an abbreviation to the table. */
1221 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1222
1223 /* Look up an abbrev in the table.
1224 Returns NULL if the abbrev is not found. */
1225
1226 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1227
1228
1229 /* Where the abbrev table came from.
1230 This is used as a sanity check when the table is used. */
1231 const sect_offset sect_off;
1232
1233 /* Storage for the abbrev table. */
1234 auto_obstack abbrev_obstack;
1235
1236 private:
1237
1238 /* Hash table of abbrevs.
1239 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1240 It could be statically allocated, but the previous code didn't so we
1241 don't either. */
1242 struct abbrev_info **m_abbrevs;
1243 };
1244
1245 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1246
1247 /* Attributes have a name and a value. */
1248 struct attribute
1249 {
1250 ENUM_BITFIELD(dwarf_attribute) name : 16;
1251 ENUM_BITFIELD(dwarf_form) form : 15;
1252
1253 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1254 field should be in u.str (existing only for DW_STRING) but it is kept
1255 here for better struct attribute alignment. */
1256 unsigned int string_is_canonical : 1;
1257
1258 union
1259 {
1260 const char *str;
1261 struct dwarf_block *blk;
1262 ULONGEST unsnd;
1263 LONGEST snd;
1264 CORE_ADDR addr;
1265 ULONGEST signature;
1266 }
1267 u;
1268 };
1269
1270 /* This data structure holds a complete die structure. */
1271 struct die_info
1272 {
1273 /* DWARF-2 tag for this DIE. */
1274 ENUM_BITFIELD(dwarf_tag) tag : 16;
1275
1276 /* Number of attributes */
1277 unsigned char num_attrs;
1278
1279 /* True if we're presently building the full type name for the
1280 type derived from this DIE. */
1281 unsigned char building_fullname : 1;
1282
1283 /* True if this die is in process. PR 16581. */
1284 unsigned char in_process : 1;
1285
1286 /* Abbrev number */
1287 unsigned int abbrev;
1288
1289 /* Offset in .debug_info or .debug_types section. */
1290 sect_offset sect_off;
1291
1292 /* The dies in a compilation unit form an n-ary tree. PARENT
1293 points to this die's parent; CHILD points to the first child of
1294 this node; and all the children of a given node are chained
1295 together via their SIBLING fields. */
1296 struct die_info *child; /* Its first child, if any. */
1297 struct die_info *sibling; /* Its next sibling, if any. */
1298 struct die_info *parent; /* Its parent, if any. */
1299
1300 /* An array of attributes, with NUM_ATTRS elements. There may be
1301 zero, but it's not common and zero-sized arrays are not
1302 sufficiently portable C. */
1303 struct attribute attrs[1];
1304 };
1305
1306 /* Get at parts of an attribute structure. */
1307
1308 #define DW_STRING(attr) ((attr)->u.str)
1309 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1310 #define DW_UNSND(attr) ((attr)->u.unsnd)
1311 #define DW_BLOCK(attr) ((attr)->u.blk)
1312 #define DW_SND(attr) ((attr)->u.snd)
1313 #define DW_ADDR(attr) ((attr)->u.addr)
1314 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1315
1316 /* Blocks are a bunch of untyped bytes. */
1317 struct dwarf_block
1318 {
1319 size_t size;
1320
1321 /* Valid only if SIZE is not zero. */
1322 const gdb_byte *data;
1323 };
1324
1325 #ifndef ATTR_ALLOC_CHUNK
1326 #define ATTR_ALLOC_CHUNK 4
1327 #endif
1328
1329 /* Allocate fields for structs, unions and enums in this size. */
1330 #ifndef DW_FIELD_ALLOC_CHUNK
1331 #define DW_FIELD_ALLOC_CHUNK 4
1332 #endif
1333
1334 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1335 but this would require a corresponding change in unpack_field_as_long
1336 and friends. */
1337 static int bits_per_byte = 8;
1338
1339 /* When reading a variant or variant part, we track a bit more
1340 information about the field, and store it in an object of this
1341 type. */
1342
1343 struct variant_field
1344 {
1345 /* If we see a DW_TAG_variant, then this will be the discriminant
1346 value. */
1347 ULONGEST discriminant_value;
1348 /* If we see a DW_TAG_variant, then this will be set if this is the
1349 default branch. */
1350 bool default_branch;
1351 /* While reading a DW_TAG_variant_part, this will be set if this
1352 field is the discriminant. */
1353 bool is_discriminant;
1354 };
1355
1356 struct nextfield
1357 {
1358 int accessibility = 0;
1359 int virtuality = 0;
1360 /* Extra information to describe a variant or variant part. */
1361 struct variant_field variant {};
1362 struct field field {};
1363 };
1364
1365 struct fnfieldlist
1366 {
1367 const char *name = nullptr;
1368 std::vector<struct fn_field> fnfields;
1369 };
1370
1371 /* The routines that read and process dies for a C struct or C++ class
1372 pass lists of data member fields and lists of member function fields
1373 in an instance of a field_info structure, as defined below. */
1374 struct field_info
1375 {
1376 /* List of data member and baseclasses fields. */
1377 std::vector<struct nextfield> fields;
1378 std::vector<struct nextfield> baseclasses;
1379
1380 /* Number of fields (including baseclasses). */
1381 int nfields = 0;
1382
1383 /* Set if the accesibility of one of the fields is not public. */
1384 int non_public_fields = 0;
1385
1386 /* Member function fieldlist array, contains name of possibly overloaded
1387 member function, number of overloaded member functions and a pointer
1388 to the head of the member function field chain. */
1389 std::vector<struct fnfieldlist> fnfieldlists;
1390
1391 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1392 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1393 std::vector<struct decl_field> typedef_field_list;
1394
1395 /* Nested types defined by this class and the number of elements in this
1396 list. */
1397 std::vector<struct decl_field> nested_types_list;
1398 };
1399
1400 /* One item on the queue of compilation units to read in full symbols
1401 for. */
1402 struct dwarf2_queue_item
1403 {
1404 struct dwarf2_per_cu_data *per_cu;
1405 enum language pretend_language;
1406 struct dwarf2_queue_item *next;
1407 };
1408
1409 /* The current queue. */
1410 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1411
1412 /* Loaded secondary compilation units are kept in memory until they
1413 have not been referenced for the processing of this many
1414 compilation units. Set this to zero to disable caching. Cache
1415 sizes of up to at least twenty will improve startup time for
1416 typical inter-CU-reference binaries, at an obvious memory cost. */
1417 static int dwarf_max_cache_age = 5;
1418 static void
1419 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1420 struct cmd_list_element *c, const char *value)
1421 {
1422 fprintf_filtered (file, _("The upper bound on the age of cached "
1423 "DWARF compilation units is %s.\n"),
1424 value);
1425 }
1426 \f
1427 /* local function prototypes */
1428
1429 static const char *get_section_name (const struct dwarf2_section_info *);
1430
1431 static const char *get_section_file_name (const struct dwarf2_section_info *);
1432
1433 static void dwarf2_find_base_address (struct die_info *die,
1434 struct dwarf2_cu *cu);
1435
1436 static struct partial_symtab *create_partial_symtab
1437 (struct dwarf2_per_cu_data *per_cu, const char *name);
1438
1439 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1440 const gdb_byte *info_ptr,
1441 struct die_info *type_unit_die,
1442 int has_children, void *data);
1443
1444 static void dwarf2_build_psymtabs_hard
1445 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1446
1447 static void scan_partial_symbols (struct partial_die_info *,
1448 CORE_ADDR *, CORE_ADDR *,
1449 int, struct dwarf2_cu *);
1450
1451 static void add_partial_symbol (struct partial_die_info *,
1452 struct dwarf2_cu *);
1453
1454 static void add_partial_namespace (struct partial_die_info *pdi,
1455 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1456 int set_addrmap, struct dwarf2_cu *cu);
1457
1458 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1459 CORE_ADDR *highpc, int set_addrmap,
1460 struct dwarf2_cu *cu);
1461
1462 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1463 struct dwarf2_cu *cu);
1464
1465 static void add_partial_subprogram (struct partial_die_info *pdi,
1466 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1467 int need_pc, struct dwarf2_cu *cu);
1468
1469 static void dwarf2_read_symtab (struct partial_symtab *,
1470 struct objfile *);
1471
1472 static void psymtab_to_symtab_1 (struct partial_symtab *);
1473
1474 static abbrev_table_up abbrev_table_read_table
1475 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1476 sect_offset);
1477
1478 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1479
1480 static struct partial_die_info *load_partial_dies
1481 (const struct die_reader_specs *, const gdb_byte *, int);
1482
1483 static struct partial_die_info *find_partial_die (sect_offset, int,
1484 struct dwarf2_cu *);
1485
1486 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1487 struct attribute *, struct attr_abbrev *,
1488 const gdb_byte *);
1489
1490 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1491
1492 static int read_1_signed_byte (bfd *, const gdb_byte *);
1493
1494 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1495
1496 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1497
1498 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1499
1500 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1501 unsigned int *);
1502
1503 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1504
1505 static LONGEST read_checked_initial_length_and_offset
1506 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1507 unsigned int *, unsigned int *);
1508
1509 static LONGEST read_offset (bfd *, const gdb_byte *,
1510 const struct comp_unit_head *,
1511 unsigned int *);
1512
1513 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1514
1515 static sect_offset read_abbrev_offset
1516 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1517 struct dwarf2_section_info *, sect_offset);
1518
1519 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1520
1521 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1522
1523 static const char *read_indirect_string
1524 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1525 const struct comp_unit_head *, unsigned int *);
1526
1527 static const char *read_indirect_line_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_string_at_offset
1532 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1533 LONGEST str_offset);
1534
1535 static const char *read_indirect_string_from_dwz
1536 (struct objfile *objfile, struct dwz_file *, LONGEST);
1537
1538 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1539
1540 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1541 const gdb_byte *,
1542 unsigned int *);
1543
1544 static const char *read_str_index (const struct die_reader_specs *reader,
1545 ULONGEST str_index);
1546
1547 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1548
1549 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1550 struct dwarf2_cu *);
1551
1552 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1553 unsigned int);
1554
1555 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1556 struct dwarf2_cu *cu);
1557
1558 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1559 struct dwarf2_cu *cu);
1560
1561 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1562
1563 static struct die_info *die_specification (struct die_info *die,
1564 struct dwarf2_cu **);
1565
1566 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1567 struct dwarf2_cu *cu);
1568
1569 static void dwarf_decode_lines (struct line_header *, const char *,
1570 struct dwarf2_cu *, struct partial_symtab *,
1571 CORE_ADDR, int decode_mapping);
1572
1573 static void dwarf2_start_subfile (const char *, const char *);
1574
1575 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1576 const char *, const char *,
1577 CORE_ADDR);
1578
1579 static struct symbol *new_symbol (struct die_info *, struct type *,
1580 struct dwarf2_cu *, struct symbol * = NULL);
1581
1582 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1583 struct dwarf2_cu *);
1584
1585 static void dwarf2_const_value_attr (const struct attribute *attr,
1586 struct type *type,
1587 const char *name,
1588 struct obstack *obstack,
1589 struct dwarf2_cu *cu, LONGEST *value,
1590 const gdb_byte **bytes,
1591 struct dwarf2_locexpr_baton **baton);
1592
1593 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1594
1595 static int need_gnat_info (struct dwarf2_cu *);
1596
1597 static struct type *die_descriptive_type (struct die_info *,
1598 struct dwarf2_cu *);
1599
1600 static void set_descriptive_type (struct type *, struct die_info *,
1601 struct dwarf2_cu *);
1602
1603 static struct type *die_containing_type (struct die_info *,
1604 struct dwarf2_cu *);
1605
1606 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1607 struct dwarf2_cu *);
1608
1609 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1610
1611 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1612
1613 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1614
1615 static char *typename_concat (struct obstack *obs, const char *prefix,
1616 const char *suffix, int physname,
1617 struct dwarf2_cu *cu);
1618
1619 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1620
1621 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1622
1623 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1624
1625 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1626
1627 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1628
1629 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1630
1631 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1632 struct dwarf2_cu *, struct partial_symtab *);
1633
1634 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1635 values. Keep the items ordered with increasing constraints compliance. */
1636 enum pc_bounds_kind
1637 {
1638 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1639 PC_BOUNDS_NOT_PRESENT,
1640
1641 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1642 were present but they do not form a valid range of PC addresses. */
1643 PC_BOUNDS_INVALID,
1644
1645 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1646 PC_BOUNDS_RANGES,
1647
1648 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1649 PC_BOUNDS_HIGH_LOW,
1650 };
1651
1652 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1653 CORE_ADDR *, CORE_ADDR *,
1654 struct dwarf2_cu *,
1655 struct partial_symtab *);
1656
1657 static void get_scope_pc_bounds (struct die_info *,
1658 CORE_ADDR *, CORE_ADDR *,
1659 struct dwarf2_cu *);
1660
1661 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1662 CORE_ADDR, struct dwarf2_cu *);
1663
1664 static void dwarf2_add_field (struct field_info *, struct die_info *,
1665 struct dwarf2_cu *);
1666
1667 static void dwarf2_attach_fields_to_type (struct field_info *,
1668 struct type *, struct dwarf2_cu *);
1669
1670 static void dwarf2_add_member_fn (struct field_info *,
1671 struct die_info *, struct type *,
1672 struct dwarf2_cu *);
1673
1674 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1675 struct type *,
1676 struct dwarf2_cu *);
1677
1678 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1679
1680 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1681
1682 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1683
1684 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1685
1686 static struct using_direct **using_directives (enum language);
1687
1688 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1689
1690 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1691
1692 static struct type *read_module_type (struct die_info *die,
1693 struct dwarf2_cu *cu);
1694
1695 static const char *namespace_name (struct die_info *die,
1696 int *is_anonymous, struct dwarf2_cu *);
1697
1698 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1699
1700 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1701
1702 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1703 struct dwarf2_cu *);
1704
1705 static struct die_info *read_die_and_siblings_1
1706 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1707 struct die_info *);
1708
1709 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1710 const gdb_byte *info_ptr,
1711 const gdb_byte **new_info_ptr,
1712 struct die_info *parent);
1713
1714 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1715 struct die_info **, const gdb_byte *,
1716 int *, int);
1717
1718 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1719 struct die_info **, const gdb_byte *,
1720 int *);
1721
1722 static void process_die (struct die_info *, struct dwarf2_cu *);
1723
1724 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1725 struct obstack *);
1726
1727 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1728
1729 static const char *dwarf2_full_name (const char *name,
1730 struct die_info *die,
1731 struct dwarf2_cu *cu);
1732
1733 static const char *dwarf2_physname (const char *name, struct die_info *die,
1734 struct dwarf2_cu *cu);
1735
1736 static struct die_info *dwarf2_extension (struct die_info *die,
1737 struct dwarf2_cu **);
1738
1739 static const char *dwarf_tag_name (unsigned int);
1740
1741 static const char *dwarf_attr_name (unsigned int);
1742
1743 static const char *dwarf_form_name (unsigned int);
1744
1745 static const char *dwarf_bool_name (unsigned int);
1746
1747 static const char *dwarf_type_encoding_name (unsigned int);
1748
1749 static struct die_info *sibling_die (struct die_info *);
1750
1751 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1752
1753 static void dump_die_for_error (struct die_info *);
1754
1755 static void dump_die_1 (struct ui_file *, int level, int max_level,
1756 struct die_info *);
1757
1758 /*static*/ void dump_die (struct die_info *, int max_level);
1759
1760 static void store_in_ref_table (struct die_info *,
1761 struct dwarf2_cu *);
1762
1763 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1764
1765 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1766
1767 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1768 const struct attribute *,
1769 struct dwarf2_cu **);
1770
1771 static struct die_info *follow_die_ref (struct die_info *,
1772 const struct attribute *,
1773 struct dwarf2_cu **);
1774
1775 static struct die_info *follow_die_sig (struct die_info *,
1776 const struct attribute *,
1777 struct dwarf2_cu **);
1778
1779 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1780 struct dwarf2_cu *);
1781
1782 static struct type *get_DW_AT_signature_type (struct die_info *,
1783 const struct attribute *,
1784 struct dwarf2_cu *);
1785
1786 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1787
1788 static void read_signatured_type (struct signatured_type *);
1789
1790 static int attr_to_dynamic_prop (const struct attribute *attr,
1791 struct die_info *die, struct dwarf2_cu *cu,
1792 struct dynamic_prop *prop);
1793
1794 /* memory allocation interface */
1795
1796 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1797
1798 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1799
1800 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1801
1802 static int attr_form_is_block (const struct attribute *);
1803
1804 static int attr_form_is_section_offset (const struct attribute *);
1805
1806 static int attr_form_is_constant (const struct attribute *);
1807
1808 static int attr_form_is_ref (const struct attribute *);
1809
1810 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1811 struct dwarf2_loclist_baton *baton,
1812 const struct attribute *attr);
1813
1814 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1815 struct symbol *sym,
1816 struct dwarf2_cu *cu,
1817 int is_block);
1818
1819 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1820 const gdb_byte *info_ptr,
1821 struct abbrev_info *abbrev);
1822
1823 static hashval_t partial_die_hash (const void *item);
1824
1825 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1826
1827 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1828 (sect_offset sect_off, unsigned int offset_in_dwz,
1829 struct dwarf2_per_objfile *dwarf2_per_objfile);
1830
1831 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1832 struct die_info *comp_unit_die,
1833 enum language pretend_language);
1834
1835 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1836
1837 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1838
1839 static struct type *set_die_type (struct die_info *, struct type *,
1840 struct dwarf2_cu *);
1841
1842 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1843
1844 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1845
1846 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1847 enum language);
1848
1849 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1850 enum language);
1851
1852 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1853 enum language);
1854
1855 static void dwarf2_add_dependence (struct dwarf2_cu *,
1856 struct dwarf2_per_cu_data *);
1857
1858 static void dwarf2_mark (struct dwarf2_cu *);
1859
1860 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1861
1862 static struct type *get_die_type_at_offset (sect_offset,
1863 struct dwarf2_per_cu_data *);
1864
1865 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1866
1867 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1868 enum language pretend_language);
1869
1870 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1871
1872 /* Class, the destructor of which frees all allocated queue entries. This
1873 will only have work to do if an error was thrown while processing the
1874 dwarf. If no error was thrown then the queue entries should have all
1875 been processed, and freed, as we went along. */
1876
1877 class dwarf2_queue_guard
1878 {
1879 public:
1880 dwarf2_queue_guard () = default;
1881
1882 /* Free any entries remaining on the queue. There should only be
1883 entries left if we hit an error while processing the dwarf. */
1884 ~dwarf2_queue_guard ()
1885 {
1886 struct dwarf2_queue_item *item, *last;
1887
1888 item = dwarf2_queue;
1889 while (item)
1890 {
1891 /* Anything still marked queued is likely to be in an
1892 inconsistent state, so discard it. */
1893 if (item->per_cu->queued)
1894 {
1895 if (item->per_cu->cu != NULL)
1896 free_one_cached_comp_unit (item->per_cu);
1897 item->per_cu->queued = 0;
1898 }
1899
1900 last = item;
1901 item = item->next;
1902 xfree (last);
1903 }
1904
1905 dwarf2_queue = dwarf2_queue_tail = NULL;
1906 }
1907 };
1908
1909 /* The return type of find_file_and_directory. Note, the enclosed
1910 string pointers are only valid while this object is valid. */
1911
1912 struct file_and_directory
1913 {
1914 /* The filename. This is never NULL. */
1915 const char *name;
1916
1917 /* The compilation directory. NULL if not known. If we needed to
1918 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1919 points directly to the DW_AT_comp_dir string attribute owned by
1920 the obstack that owns the DIE. */
1921 const char *comp_dir;
1922
1923 /* If we needed to build a new string for comp_dir, this is what
1924 owns the storage. */
1925 std::string comp_dir_storage;
1926 };
1927
1928 static file_and_directory find_file_and_directory (struct die_info *die,
1929 struct dwarf2_cu *cu);
1930
1931 static char *file_full_name (int file, struct line_header *lh,
1932 const char *comp_dir);
1933
1934 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1935 enum class rcuh_kind { COMPILE, TYPE };
1936
1937 static const gdb_byte *read_and_check_comp_unit_head
1938 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1939 struct comp_unit_head *header,
1940 struct dwarf2_section_info *section,
1941 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1942 rcuh_kind section_kind);
1943
1944 static void init_cutu_and_read_dies
1945 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1946 int use_existing_cu, int keep, bool skip_partial,
1947 die_reader_func_ftype *die_reader_func, void *data);
1948
1949 static void init_cutu_and_read_dies_simple
1950 (struct dwarf2_per_cu_data *this_cu,
1951 die_reader_func_ftype *die_reader_func, void *data);
1952
1953 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1954
1955 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1956
1957 static struct dwo_unit *lookup_dwo_unit_in_dwp
1958 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1959 struct dwp_file *dwp_file, const char *comp_dir,
1960 ULONGEST signature, int is_debug_types);
1961
1962 static struct dwp_file *get_dwp_file
1963 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1964
1965 static struct dwo_unit *lookup_dwo_comp_unit
1966 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1967
1968 static struct dwo_unit *lookup_dwo_type_unit
1969 (struct signatured_type *, const char *, const char *);
1970
1971 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1972
1973 static void free_dwo_file (struct dwo_file *);
1974
1975 /* A unique_ptr helper to free a dwo_file. */
1976
1977 struct dwo_file_deleter
1978 {
1979 void operator() (struct dwo_file *df) const
1980 {
1981 free_dwo_file (df);
1982 }
1983 };
1984
1985 /* A unique pointer to a dwo_file. */
1986
1987 typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
1988
1989 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1990
1991 static void check_producer (struct dwarf2_cu *cu);
1992
1993 static void free_line_header_voidp (void *arg);
1994 \f
1995 /* Various complaints about symbol reading that don't abort the process. */
1996
1997 static void
1998 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1999 {
2000 complaint (&symfile_complaints,
2001 _("statement list doesn't fit in .debug_line section"));
2002 }
2003
2004 static void
2005 dwarf2_debug_line_missing_file_complaint (void)
2006 {
2007 complaint (&symfile_complaints,
2008 _(".debug_line section has line data without a file"));
2009 }
2010
2011 static void
2012 dwarf2_debug_line_missing_end_sequence_complaint (void)
2013 {
2014 complaint (&symfile_complaints,
2015 _(".debug_line section has line "
2016 "program sequence without an end"));
2017 }
2018
2019 static void
2020 dwarf2_complex_location_expr_complaint (void)
2021 {
2022 complaint (&symfile_complaints, _("location expression too complex"));
2023 }
2024
2025 static void
2026 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2027 int arg3)
2028 {
2029 complaint (&symfile_complaints,
2030 _("const value length mismatch for '%s', got %d, expected %d"),
2031 arg1, arg2, arg3);
2032 }
2033
2034 static void
2035 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2036 {
2037 complaint (&symfile_complaints,
2038 _("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 (&symfile_complaints,
2048 _("macro debug info contains a "
2049 "malformed macro definition:\n`%s'"),
2050 arg1);
2051 }
2052
2053 static void
2054 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2055 {
2056 complaint (&symfile_complaints,
2057 _("invalid attribute class or form for '%s' in '%s'"),
2058 arg1, arg2);
2059 }
2060
2061 /* Hash function for line_header_hash. */
2062
2063 static hashval_t
2064 line_header_hash (const struct line_header *ofs)
2065 {
2066 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2067 }
2068
2069 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2070
2071 static hashval_t
2072 line_header_hash_voidp (const void *item)
2073 {
2074 const struct line_header *ofs = (const struct line_header *) item;
2075
2076 return line_header_hash (ofs);
2077 }
2078
2079 /* Equality function for line_header_hash. */
2080
2081 static int
2082 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2083 {
2084 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2085 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2086
2087 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2088 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2089 }
2090
2091 \f
2092
2093 /* Read the given attribute value as an address, taking the attribute's
2094 form into account. */
2095
2096 static CORE_ADDR
2097 attr_value_as_address (struct attribute *attr)
2098 {
2099 CORE_ADDR addr;
2100
2101 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2102 {
2103 /* Aside from a few clearly defined exceptions, attributes that
2104 contain an address must always be in DW_FORM_addr form.
2105 Unfortunately, some compilers happen to be violating this
2106 requirement by encoding addresses using other forms, such
2107 as DW_FORM_data4 for example. For those broken compilers,
2108 we try to do our best, without any guarantee of success,
2109 to interpret the address correctly. It would also be nice
2110 to generate a complaint, but that would require us to maintain
2111 a list of legitimate cases where a non-address form is allowed,
2112 as well as update callers to pass in at least the CU's DWARF
2113 version. This is more overhead than what we're willing to
2114 expand for a pretty rare case. */
2115 addr = DW_UNSND (attr);
2116 }
2117 else
2118 addr = DW_ADDR (attr);
2119
2120 return addr;
2121 }
2122
2123 /* See declaration. */
2124
2125 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2126 const dwarf2_debug_sections *names)
2127 : objfile (objfile_)
2128 {
2129 if (names == NULL)
2130 names = &dwarf2_elf_names;
2131
2132 bfd *obfd = objfile->obfd;
2133
2134 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2135 locate_sections (obfd, sec, *names);
2136 }
2137
2138 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2139
2140 dwarf2_per_objfile::~dwarf2_per_objfile ()
2141 {
2142 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2143 free_cached_comp_units ();
2144
2145 if (quick_file_names_table)
2146 htab_delete (quick_file_names_table);
2147
2148 if (line_header_hash)
2149 htab_delete (line_header_hash);
2150
2151 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2152 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
2153
2154 for (signatured_type *sig_type : all_type_units)
2155 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
2156
2157 VEC_free (dwarf2_section_info_def, types);
2158
2159 if (dwo_files != NULL)
2160 free_dwo_files (dwo_files, objfile);
2161
2162 /* Everything else should be on the objfile obstack. */
2163 }
2164
2165 /* See declaration. */
2166
2167 void
2168 dwarf2_per_objfile::free_cached_comp_units ()
2169 {
2170 dwarf2_per_cu_data *per_cu = read_in_chain;
2171 dwarf2_per_cu_data **last_chain = &read_in_chain;
2172 while (per_cu != NULL)
2173 {
2174 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2175
2176 delete per_cu->cu;
2177 *last_chain = next_cu;
2178 per_cu = next_cu;
2179 }
2180 }
2181
2182 /* A helper class that calls free_cached_comp_units on
2183 destruction. */
2184
2185 class free_cached_comp_units
2186 {
2187 public:
2188
2189 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2190 : m_per_objfile (per_objfile)
2191 {
2192 }
2193
2194 ~free_cached_comp_units ()
2195 {
2196 m_per_objfile->free_cached_comp_units ();
2197 }
2198
2199 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2200
2201 private:
2202
2203 dwarf2_per_objfile *m_per_objfile;
2204 };
2205
2206 /* Try to locate the sections we need for DWARF 2 debugging
2207 information and return true if we have enough to do something.
2208 NAMES points to the dwarf2 section names, or is NULL if the standard
2209 ELF names are used. */
2210
2211 int
2212 dwarf2_has_info (struct objfile *objfile,
2213 const struct dwarf2_debug_sections *names)
2214 {
2215 if (objfile->flags & OBJF_READNEVER)
2216 return 0;
2217
2218 struct dwarf2_per_objfile *dwarf2_per_objfile
2219 = get_dwarf2_per_objfile (objfile);
2220
2221 if (dwarf2_per_objfile == NULL)
2222 {
2223 /* Initialize per-objfile state. */
2224 dwarf2_per_objfile
2225 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2226 names);
2227 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2228 }
2229 return (!dwarf2_per_objfile->info.is_virtual
2230 && dwarf2_per_objfile->info.s.section != NULL
2231 && !dwarf2_per_objfile->abbrev.is_virtual
2232 && dwarf2_per_objfile->abbrev.s.section != NULL);
2233 }
2234
2235 /* Return the containing section of virtual section SECTION. */
2236
2237 static struct dwarf2_section_info *
2238 get_containing_section (const struct dwarf2_section_info *section)
2239 {
2240 gdb_assert (section->is_virtual);
2241 return section->s.containing_section;
2242 }
2243
2244 /* Return the bfd owner of SECTION. */
2245
2246 static struct bfd *
2247 get_section_bfd_owner (const struct dwarf2_section_info *section)
2248 {
2249 if (section->is_virtual)
2250 {
2251 section = get_containing_section (section);
2252 gdb_assert (!section->is_virtual);
2253 }
2254 return section->s.section->owner;
2255 }
2256
2257 /* Return the bfd section of SECTION.
2258 Returns NULL if the section is not present. */
2259
2260 static asection *
2261 get_section_bfd_section (const struct dwarf2_section_info *section)
2262 {
2263 if (section->is_virtual)
2264 {
2265 section = get_containing_section (section);
2266 gdb_assert (!section->is_virtual);
2267 }
2268 return section->s.section;
2269 }
2270
2271 /* Return the name of SECTION. */
2272
2273 static const char *
2274 get_section_name (const struct dwarf2_section_info *section)
2275 {
2276 asection *sectp = get_section_bfd_section (section);
2277
2278 gdb_assert (sectp != NULL);
2279 return bfd_section_name (get_section_bfd_owner (section), sectp);
2280 }
2281
2282 /* Return the name of the file SECTION is in. */
2283
2284 static const char *
2285 get_section_file_name (const struct dwarf2_section_info *section)
2286 {
2287 bfd *abfd = get_section_bfd_owner (section);
2288
2289 return bfd_get_filename (abfd);
2290 }
2291
2292 /* Return the id of SECTION.
2293 Returns 0 if SECTION doesn't exist. */
2294
2295 static int
2296 get_section_id (const struct dwarf2_section_info *section)
2297 {
2298 asection *sectp = get_section_bfd_section (section);
2299
2300 if (sectp == NULL)
2301 return 0;
2302 return sectp->id;
2303 }
2304
2305 /* Return the flags of SECTION.
2306 SECTION (or containing section if this is a virtual section) must exist. */
2307
2308 static int
2309 get_section_flags (const struct dwarf2_section_info *section)
2310 {
2311 asection *sectp = get_section_bfd_section (section);
2312
2313 gdb_assert (sectp != NULL);
2314 return bfd_get_section_flags (sectp->owner, sectp);
2315 }
2316
2317 /* When loading sections, we look either for uncompressed section or for
2318 compressed section names. */
2319
2320 static int
2321 section_is_p (const char *section_name,
2322 const struct dwarf2_section_names *names)
2323 {
2324 if (names->normal != NULL
2325 && strcmp (section_name, names->normal) == 0)
2326 return 1;
2327 if (names->compressed != NULL
2328 && strcmp (section_name, names->compressed) == 0)
2329 return 1;
2330 return 0;
2331 }
2332
2333 /* See declaration. */
2334
2335 void
2336 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2337 const dwarf2_debug_sections &names)
2338 {
2339 flagword aflag = bfd_get_section_flags (abfd, sectp);
2340
2341 if ((aflag & SEC_HAS_CONTENTS) == 0)
2342 {
2343 }
2344 else if (section_is_p (sectp->name, &names.info))
2345 {
2346 this->info.s.section = sectp;
2347 this->info.size = bfd_get_section_size (sectp);
2348 }
2349 else if (section_is_p (sectp->name, &names.abbrev))
2350 {
2351 this->abbrev.s.section = sectp;
2352 this->abbrev.size = bfd_get_section_size (sectp);
2353 }
2354 else if (section_is_p (sectp->name, &names.line))
2355 {
2356 this->line.s.section = sectp;
2357 this->line.size = bfd_get_section_size (sectp);
2358 }
2359 else if (section_is_p (sectp->name, &names.loc))
2360 {
2361 this->loc.s.section = sectp;
2362 this->loc.size = bfd_get_section_size (sectp);
2363 }
2364 else if (section_is_p (sectp->name, &names.loclists))
2365 {
2366 this->loclists.s.section = sectp;
2367 this->loclists.size = bfd_get_section_size (sectp);
2368 }
2369 else if (section_is_p (sectp->name, &names.macinfo))
2370 {
2371 this->macinfo.s.section = sectp;
2372 this->macinfo.size = bfd_get_section_size (sectp);
2373 }
2374 else if (section_is_p (sectp->name, &names.macro))
2375 {
2376 this->macro.s.section = sectp;
2377 this->macro.size = bfd_get_section_size (sectp);
2378 }
2379 else if (section_is_p (sectp->name, &names.str))
2380 {
2381 this->str.s.section = sectp;
2382 this->str.size = bfd_get_section_size (sectp);
2383 }
2384 else if (section_is_p (sectp->name, &names.line_str))
2385 {
2386 this->line_str.s.section = sectp;
2387 this->line_str.size = bfd_get_section_size (sectp);
2388 }
2389 else if (section_is_p (sectp->name, &names.addr))
2390 {
2391 this->addr.s.section = sectp;
2392 this->addr.size = bfd_get_section_size (sectp);
2393 }
2394 else if (section_is_p (sectp->name, &names.frame))
2395 {
2396 this->frame.s.section = sectp;
2397 this->frame.size = bfd_get_section_size (sectp);
2398 }
2399 else if (section_is_p (sectp->name, &names.eh_frame))
2400 {
2401 this->eh_frame.s.section = sectp;
2402 this->eh_frame.size = bfd_get_section_size (sectp);
2403 }
2404 else if (section_is_p (sectp->name, &names.ranges))
2405 {
2406 this->ranges.s.section = sectp;
2407 this->ranges.size = bfd_get_section_size (sectp);
2408 }
2409 else if (section_is_p (sectp->name, &names.rnglists))
2410 {
2411 this->rnglists.s.section = sectp;
2412 this->rnglists.size = bfd_get_section_size (sectp);
2413 }
2414 else if (section_is_p (sectp->name, &names.types))
2415 {
2416 struct dwarf2_section_info type_section;
2417
2418 memset (&type_section, 0, sizeof (type_section));
2419 type_section.s.section = sectp;
2420 type_section.size = bfd_get_section_size (sectp);
2421
2422 VEC_safe_push (dwarf2_section_info_def, this->types,
2423 &type_section);
2424 }
2425 else if (section_is_p (sectp->name, &names.gdb_index))
2426 {
2427 this->gdb_index.s.section = sectp;
2428 this->gdb_index.size = bfd_get_section_size (sectp);
2429 }
2430 else if (section_is_p (sectp->name, &names.debug_names))
2431 {
2432 this->debug_names.s.section = sectp;
2433 this->debug_names.size = bfd_get_section_size (sectp);
2434 }
2435 else if (section_is_p (sectp->name, &names.debug_aranges))
2436 {
2437 this->debug_aranges.s.section = sectp;
2438 this->debug_aranges.size = bfd_get_section_size (sectp);
2439 }
2440
2441 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2442 && bfd_section_vma (abfd, sectp) == 0)
2443 this->has_section_at_zero = true;
2444 }
2445
2446 /* A helper function that decides whether a section is empty,
2447 or not present. */
2448
2449 static int
2450 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2451 {
2452 if (section->is_virtual)
2453 return section->size == 0;
2454 return section->s.section == NULL || section->size == 0;
2455 }
2456
2457 /* See dwarf2read.h. */
2458
2459 void
2460 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2461 {
2462 asection *sectp;
2463 bfd *abfd;
2464 gdb_byte *buf, *retbuf;
2465
2466 if (info->readin)
2467 return;
2468 info->buffer = NULL;
2469 info->readin = 1;
2470
2471 if (dwarf2_section_empty_p (info))
2472 return;
2473
2474 sectp = get_section_bfd_section (info);
2475
2476 /* If this is a virtual section we need to read in the real one first. */
2477 if (info->is_virtual)
2478 {
2479 struct dwarf2_section_info *containing_section =
2480 get_containing_section (info);
2481
2482 gdb_assert (sectp != NULL);
2483 if ((sectp->flags & SEC_RELOC) != 0)
2484 {
2485 error (_("Dwarf Error: DWP format V2 with relocations is not"
2486 " supported in section %s [in module %s]"),
2487 get_section_name (info), get_section_file_name (info));
2488 }
2489 dwarf2_read_section (objfile, containing_section);
2490 /* Other code should have already caught virtual sections that don't
2491 fit. */
2492 gdb_assert (info->virtual_offset + info->size
2493 <= containing_section->size);
2494 /* If the real section is empty or there was a problem reading the
2495 section we shouldn't get here. */
2496 gdb_assert (containing_section->buffer != NULL);
2497 info->buffer = containing_section->buffer + info->virtual_offset;
2498 return;
2499 }
2500
2501 /* If the section has relocations, we must read it ourselves.
2502 Otherwise we attach it to the BFD. */
2503 if ((sectp->flags & SEC_RELOC) == 0)
2504 {
2505 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2506 return;
2507 }
2508
2509 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2510 info->buffer = buf;
2511
2512 /* When debugging .o files, we may need to apply relocations; see
2513 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2514 We never compress sections in .o files, so we only need to
2515 try this when the section is not compressed. */
2516 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2517 if (retbuf != NULL)
2518 {
2519 info->buffer = retbuf;
2520 return;
2521 }
2522
2523 abfd = get_section_bfd_owner (info);
2524 gdb_assert (abfd != NULL);
2525
2526 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2527 || bfd_bread (buf, info->size, abfd) != info->size)
2528 {
2529 error (_("Dwarf Error: Can't read DWARF data"
2530 " in section %s [in module %s]"),
2531 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2532 }
2533 }
2534
2535 /* A helper function that returns the size of a section in a safe way.
2536 If you are positive that the section has been read before using the
2537 size, then it is safe to refer to the dwarf2_section_info object's
2538 "size" field directly. In other cases, you must call this
2539 function, because for compressed sections the size field is not set
2540 correctly until the section has been read. */
2541
2542 static bfd_size_type
2543 dwarf2_section_size (struct objfile *objfile,
2544 struct dwarf2_section_info *info)
2545 {
2546 if (!info->readin)
2547 dwarf2_read_section (objfile, info);
2548 return info->size;
2549 }
2550
2551 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2552 SECTION_NAME. */
2553
2554 void
2555 dwarf2_get_section_info (struct objfile *objfile,
2556 enum dwarf2_section_enum sect,
2557 asection **sectp, const gdb_byte **bufp,
2558 bfd_size_type *sizep)
2559 {
2560 struct dwarf2_per_objfile *data
2561 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2562 dwarf2_objfile_data_key);
2563 struct dwarf2_section_info *info;
2564
2565 /* We may see an objfile without any DWARF, in which case we just
2566 return nothing. */
2567 if (data == NULL)
2568 {
2569 *sectp = NULL;
2570 *bufp = NULL;
2571 *sizep = 0;
2572 return;
2573 }
2574 switch (sect)
2575 {
2576 case DWARF2_DEBUG_FRAME:
2577 info = &data->frame;
2578 break;
2579 case DWARF2_EH_FRAME:
2580 info = &data->eh_frame;
2581 break;
2582 default:
2583 gdb_assert_not_reached ("unexpected section");
2584 }
2585
2586 dwarf2_read_section (objfile, info);
2587
2588 *sectp = get_section_bfd_section (info);
2589 *bufp = info->buffer;
2590 *sizep = info->size;
2591 }
2592
2593 /* A helper function to find the sections for a .dwz file. */
2594
2595 static void
2596 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2597 {
2598 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2599
2600 /* Note that we only support the standard ELF names, because .dwz
2601 is ELF-only (at the time of writing). */
2602 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2603 {
2604 dwz_file->abbrev.s.section = sectp;
2605 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2606 }
2607 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2608 {
2609 dwz_file->info.s.section = sectp;
2610 dwz_file->info.size = bfd_get_section_size (sectp);
2611 }
2612 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2613 {
2614 dwz_file->str.s.section = sectp;
2615 dwz_file->str.size = bfd_get_section_size (sectp);
2616 }
2617 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2618 {
2619 dwz_file->line.s.section = sectp;
2620 dwz_file->line.size = bfd_get_section_size (sectp);
2621 }
2622 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2623 {
2624 dwz_file->macro.s.section = sectp;
2625 dwz_file->macro.size = bfd_get_section_size (sectp);
2626 }
2627 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2628 {
2629 dwz_file->gdb_index.s.section = sectp;
2630 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2631 }
2632 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2633 {
2634 dwz_file->debug_names.s.section = sectp;
2635 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2636 }
2637 }
2638
2639 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2640 there is no .gnu_debugaltlink section in the file. Error if there
2641 is such a section but the file cannot be found. */
2642
2643 static struct dwz_file *
2644 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2645 {
2646 const char *filename;
2647 bfd_size_type buildid_len_arg;
2648 size_t buildid_len;
2649 bfd_byte *buildid;
2650
2651 if (dwarf2_per_objfile->dwz_file != NULL)
2652 return dwarf2_per_objfile->dwz_file.get ();
2653
2654 bfd_set_error (bfd_error_no_error);
2655 gdb::unique_xmalloc_ptr<char> data
2656 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2657 &buildid_len_arg, &buildid));
2658 if (data == NULL)
2659 {
2660 if (bfd_get_error () == bfd_error_no_error)
2661 return NULL;
2662 error (_("could not read '.gnu_debugaltlink' section: %s"),
2663 bfd_errmsg (bfd_get_error ()));
2664 }
2665
2666 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2667
2668 buildid_len = (size_t) buildid_len_arg;
2669
2670 filename = data.get ();
2671
2672 std::string abs_storage;
2673 if (!IS_ABSOLUTE_PATH (filename))
2674 {
2675 gdb::unique_xmalloc_ptr<char> abs
2676 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2677
2678 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2679 filename = abs_storage.c_str ();
2680 }
2681
2682 /* First try the file name given in the section. If that doesn't
2683 work, try to use the build-id instead. */
2684 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2685 if (dwz_bfd != NULL)
2686 {
2687 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2688 dwz_bfd.release ();
2689 }
2690
2691 if (dwz_bfd == NULL)
2692 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2693
2694 if (dwz_bfd == NULL)
2695 error (_("could not find '.gnu_debugaltlink' file for %s"),
2696 objfile_name (dwarf2_per_objfile->objfile));
2697
2698 std::unique_ptr<struct dwz_file> result
2699 (new struct dwz_file (std::move (dwz_bfd)));
2700
2701 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2702 result.get ());
2703
2704 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2705 result->dwz_bfd.get ());
2706 dwarf2_per_objfile->dwz_file = std::move (result);
2707 return dwarf2_per_objfile->dwz_file.get ();
2708 }
2709 \f
2710 /* DWARF quick_symbols_functions support. */
2711
2712 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2713 unique line tables, so we maintain a separate table of all .debug_line
2714 derived entries to support the sharing.
2715 All the quick functions need is the list of file names. We discard the
2716 line_header when we're done and don't need to record it here. */
2717 struct quick_file_names
2718 {
2719 /* The data used to construct the hash key. */
2720 struct stmt_list_hash hash;
2721
2722 /* The number of entries in file_names, real_names. */
2723 unsigned int num_file_names;
2724
2725 /* The file names from the line table, after being run through
2726 file_full_name. */
2727 const char **file_names;
2728
2729 /* The file names from the line table after being run through
2730 gdb_realpath. These are computed lazily. */
2731 const char **real_names;
2732 };
2733
2734 /* When using the index (and thus not using psymtabs), each CU has an
2735 object of this type. This is used to hold information needed by
2736 the various "quick" methods. */
2737 struct dwarf2_per_cu_quick_data
2738 {
2739 /* The file table. This can be NULL if there was no file table
2740 or it's currently not read in.
2741 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2742 struct quick_file_names *file_names;
2743
2744 /* The corresponding symbol table. This is NULL if symbols for this
2745 CU have not yet been read. */
2746 struct compunit_symtab *compunit_symtab;
2747
2748 /* A temporary mark bit used when iterating over all CUs in
2749 expand_symtabs_matching. */
2750 unsigned int mark : 1;
2751
2752 /* True if we've tried to read the file table and found there isn't one.
2753 There will be no point in trying to read it again next time. */
2754 unsigned int no_file_data : 1;
2755 };
2756
2757 /* Utility hash function for a stmt_list_hash. */
2758
2759 static hashval_t
2760 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2761 {
2762 hashval_t v = 0;
2763
2764 if (stmt_list_hash->dwo_unit != NULL)
2765 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2766 v += to_underlying (stmt_list_hash->line_sect_off);
2767 return v;
2768 }
2769
2770 /* Utility equality function for a stmt_list_hash. */
2771
2772 static int
2773 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2774 const struct stmt_list_hash *rhs)
2775 {
2776 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2777 return 0;
2778 if (lhs->dwo_unit != NULL
2779 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2780 return 0;
2781
2782 return lhs->line_sect_off == rhs->line_sect_off;
2783 }
2784
2785 /* Hash function for a quick_file_names. */
2786
2787 static hashval_t
2788 hash_file_name_entry (const void *e)
2789 {
2790 const struct quick_file_names *file_data
2791 = (const struct quick_file_names *) e;
2792
2793 return hash_stmt_list_entry (&file_data->hash);
2794 }
2795
2796 /* Equality function for a quick_file_names. */
2797
2798 static int
2799 eq_file_name_entry (const void *a, const void *b)
2800 {
2801 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2802 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2803
2804 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2805 }
2806
2807 /* Delete function for a quick_file_names. */
2808
2809 static void
2810 delete_file_name_entry (void *e)
2811 {
2812 struct quick_file_names *file_data = (struct quick_file_names *) e;
2813 int i;
2814
2815 for (i = 0; i < file_data->num_file_names; ++i)
2816 {
2817 xfree ((void*) file_data->file_names[i]);
2818 if (file_data->real_names)
2819 xfree ((void*) file_data->real_names[i]);
2820 }
2821
2822 /* The space for the struct itself lives on objfile_obstack,
2823 so we don't free it here. */
2824 }
2825
2826 /* Create a quick_file_names hash table. */
2827
2828 static htab_t
2829 create_quick_file_names_table (unsigned int nr_initial_entries)
2830 {
2831 return htab_create_alloc (nr_initial_entries,
2832 hash_file_name_entry, eq_file_name_entry,
2833 delete_file_name_entry, xcalloc, xfree);
2834 }
2835
2836 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2837 have to be created afterwards. You should call age_cached_comp_units after
2838 processing PER_CU->CU. dw2_setup must have been already called. */
2839
2840 static void
2841 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2842 {
2843 if (per_cu->is_debug_types)
2844 load_full_type_unit (per_cu);
2845 else
2846 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2847
2848 if (per_cu->cu == NULL)
2849 return; /* Dummy CU. */
2850
2851 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2852 }
2853
2854 /* Read in the symbols for PER_CU. */
2855
2856 static void
2857 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2858 {
2859 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2860
2861 /* Skip type_unit_groups, reading the type units they contain
2862 is handled elsewhere. */
2863 if (IS_TYPE_UNIT_GROUP (per_cu))
2864 return;
2865
2866 /* The destructor of dwarf2_queue_guard frees any entries left on
2867 the queue. After this point we're guaranteed to leave this function
2868 with the dwarf queue empty. */
2869 dwarf2_queue_guard q_guard;
2870
2871 if (dwarf2_per_objfile->using_index
2872 ? per_cu->v.quick->compunit_symtab == NULL
2873 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2874 {
2875 queue_comp_unit (per_cu, language_minimal);
2876 load_cu (per_cu, skip_partial);
2877
2878 /* If we just loaded a CU from a DWO, and we're working with an index
2879 that may badly handle TUs, load all the TUs in that DWO as well.
2880 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2881 if (!per_cu->is_debug_types
2882 && per_cu->cu != NULL
2883 && per_cu->cu->dwo_unit != NULL
2884 && dwarf2_per_objfile->index_table != NULL
2885 && dwarf2_per_objfile->index_table->version <= 7
2886 /* DWP files aren't supported yet. */
2887 && get_dwp_file (dwarf2_per_objfile) == NULL)
2888 queue_and_load_all_dwo_tus (per_cu);
2889 }
2890
2891 process_queue (dwarf2_per_objfile);
2892
2893 /* Age the cache, releasing compilation units that have not
2894 been used recently. */
2895 age_cached_comp_units (dwarf2_per_objfile);
2896 }
2897
2898 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2899 the objfile from which this CU came. Returns the resulting symbol
2900 table. */
2901
2902 static struct compunit_symtab *
2903 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2904 {
2905 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2906
2907 gdb_assert (dwarf2_per_objfile->using_index);
2908 if (!per_cu->v.quick->compunit_symtab)
2909 {
2910 free_cached_comp_units freer (dwarf2_per_objfile);
2911 scoped_restore decrementer = increment_reading_symtab ();
2912 dw2_do_instantiate_symtab (per_cu, skip_partial);
2913 process_cu_includes (dwarf2_per_objfile);
2914 }
2915
2916 return per_cu->v.quick->compunit_symtab;
2917 }
2918
2919 /* See declaration. */
2920
2921 dwarf2_per_cu_data *
2922 dwarf2_per_objfile::get_cutu (int index)
2923 {
2924 if (index >= this->all_comp_units.size ())
2925 {
2926 index -= this->all_comp_units.size ();
2927 gdb_assert (index < this->all_type_units.size ());
2928 return &this->all_type_units[index]->per_cu;
2929 }
2930
2931 return this->all_comp_units[index];
2932 }
2933
2934 /* See declaration. */
2935
2936 dwarf2_per_cu_data *
2937 dwarf2_per_objfile::get_cu (int index)
2938 {
2939 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2940
2941 return this->all_comp_units[index];
2942 }
2943
2944 /* See declaration. */
2945
2946 signatured_type *
2947 dwarf2_per_objfile::get_tu (int index)
2948 {
2949 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2950
2951 return this->all_type_units[index];
2952 }
2953
2954 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2955 objfile_obstack, and constructed with the specified field
2956 values. */
2957
2958 static dwarf2_per_cu_data *
2959 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2960 struct dwarf2_section_info *section,
2961 int is_dwz,
2962 sect_offset sect_off, ULONGEST length)
2963 {
2964 struct objfile *objfile = dwarf2_per_objfile->objfile;
2965 dwarf2_per_cu_data *the_cu
2966 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2967 struct dwarf2_per_cu_data);
2968 the_cu->sect_off = sect_off;
2969 the_cu->length = length;
2970 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2971 the_cu->section = section;
2972 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2973 struct dwarf2_per_cu_quick_data);
2974 the_cu->is_dwz = is_dwz;
2975 return the_cu;
2976 }
2977
2978 /* A helper for create_cus_from_index that handles a given list of
2979 CUs. */
2980
2981 static void
2982 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2983 const gdb_byte *cu_list, offset_type n_elements,
2984 struct dwarf2_section_info *section,
2985 int is_dwz)
2986 {
2987 for (offset_type i = 0; i < n_elements; i += 2)
2988 {
2989 gdb_static_assert (sizeof (ULONGEST) >= 8);
2990
2991 sect_offset sect_off
2992 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2993 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2994 cu_list += 2 * 8;
2995
2996 dwarf2_per_cu_data *per_cu
2997 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2998 sect_off, length);
2999 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3000 }
3001 }
3002
3003 /* Read the CU list from the mapped index, and use it to create all
3004 the CU objects for this objfile. */
3005
3006 static void
3007 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3008 const gdb_byte *cu_list, offset_type cu_list_elements,
3009 const gdb_byte *dwz_list, offset_type dwz_elements)
3010 {
3011 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3012 dwarf2_per_objfile->all_comp_units.reserve
3013 ((cu_list_elements + dwz_elements) / 2);
3014
3015 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3016 &dwarf2_per_objfile->info, 0);
3017
3018 if (dwz_elements == 0)
3019 return;
3020
3021 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3022 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3023 &dwz->info, 1);
3024 }
3025
3026 /* Create the signatured type hash table from the index. */
3027
3028 static void
3029 create_signatured_type_table_from_index
3030 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3031 struct dwarf2_section_info *section,
3032 const gdb_byte *bytes,
3033 offset_type elements)
3034 {
3035 struct objfile *objfile = dwarf2_per_objfile->objfile;
3036
3037 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3038 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3039
3040 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3041
3042 for (offset_type i = 0; i < elements; i += 3)
3043 {
3044 struct signatured_type *sig_type;
3045 ULONGEST signature;
3046 void **slot;
3047 cu_offset type_offset_in_tu;
3048
3049 gdb_static_assert (sizeof (ULONGEST) >= 8);
3050 sect_offset sect_off
3051 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3052 type_offset_in_tu
3053 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3054 BFD_ENDIAN_LITTLE);
3055 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3056 bytes += 3 * 8;
3057
3058 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3059 struct signatured_type);
3060 sig_type->signature = signature;
3061 sig_type->type_offset_in_tu = type_offset_in_tu;
3062 sig_type->per_cu.is_debug_types = 1;
3063 sig_type->per_cu.section = section;
3064 sig_type->per_cu.sect_off = sect_off;
3065 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3066 sig_type->per_cu.v.quick
3067 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3068 struct dwarf2_per_cu_quick_data);
3069
3070 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3071 *slot = sig_type;
3072
3073 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3074 }
3075
3076 dwarf2_per_objfile->signatured_types = sig_types_hash;
3077 }
3078
3079 /* Create the signatured type hash table from .debug_names. */
3080
3081 static void
3082 create_signatured_type_table_from_debug_names
3083 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3084 const mapped_debug_names &map,
3085 struct dwarf2_section_info *section,
3086 struct dwarf2_section_info *abbrev_section)
3087 {
3088 struct objfile *objfile = dwarf2_per_objfile->objfile;
3089
3090 dwarf2_read_section (objfile, section);
3091 dwarf2_read_section (objfile, abbrev_section);
3092
3093 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3094 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3095
3096 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3097
3098 for (uint32_t i = 0; i < map.tu_count; ++i)
3099 {
3100 struct signatured_type *sig_type;
3101 void **slot;
3102
3103 sect_offset sect_off
3104 = (sect_offset) (extract_unsigned_integer
3105 (map.tu_table_reordered + i * map.offset_size,
3106 map.offset_size,
3107 map.dwarf5_byte_order));
3108
3109 comp_unit_head cu_header;
3110 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3111 abbrev_section,
3112 section->buffer + to_underlying (sect_off),
3113 rcuh_kind::TYPE);
3114
3115 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3116 struct signatured_type);
3117 sig_type->signature = cu_header.signature;
3118 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3119 sig_type->per_cu.is_debug_types = 1;
3120 sig_type->per_cu.section = section;
3121 sig_type->per_cu.sect_off = sect_off;
3122 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3123 sig_type->per_cu.v.quick
3124 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3125 struct dwarf2_per_cu_quick_data);
3126
3127 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3128 *slot = sig_type;
3129
3130 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3131 }
3132
3133 dwarf2_per_objfile->signatured_types = sig_types_hash;
3134 }
3135
3136 /* Read the address map data from the mapped index, and use it to
3137 populate the objfile's psymtabs_addrmap. */
3138
3139 static void
3140 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3141 struct mapped_index *index)
3142 {
3143 struct objfile *objfile = dwarf2_per_objfile->objfile;
3144 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3145 const gdb_byte *iter, *end;
3146 struct addrmap *mutable_map;
3147 CORE_ADDR baseaddr;
3148
3149 auto_obstack temp_obstack;
3150
3151 mutable_map = addrmap_create_mutable (&temp_obstack);
3152
3153 iter = index->address_table.data ();
3154 end = iter + index->address_table.size ();
3155
3156 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3157
3158 while (iter < end)
3159 {
3160 ULONGEST hi, lo, cu_index;
3161 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3162 iter += 8;
3163 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3164 iter += 8;
3165 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3166 iter += 4;
3167
3168 if (lo > hi)
3169 {
3170 complaint (&symfile_complaints,
3171 _(".gdb_index address table has invalid range (%s - %s)"),
3172 hex_string (lo), hex_string (hi));
3173 continue;
3174 }
3175
3176 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3177 {
3178 complaint (&symfile_complaints,
3179 _(".gdb_index address table has invalid CU number %u"),
3180 (unsigned) cu_index);
3181 continue;
3182 }
3183
3184 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3185 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3186 addrmap_set_empty (mutable_map, lo, hi - 1,
3187 dwarf2_per_objfile->get_cu (cu_index));
3188 }
3189
3190 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3191 &objfile->objfile_obstack);
3192 }
3193
3194 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3195 populate the objfile's psymtabs_addrmap. */
3196
3197 static void
3198 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3199 struct dwarf2_section_info *section)
3200 {
3201 struct objfile *objfile = dwarf2_per_objfile->objfile;
3202 bfd *abfd = objfile->obfd;
3203 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3204 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3205 SECT_OFF_TEXT (objfile));
3206
3207 auto_obstack temp_obstack;
3208 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3209
3210 std::unordered_map<sect_offset,
3211 dwarf2_per_cu_data *,
3212 gdb::hash_enum<sect_offset>>
3213 debug_info_offset_to_per_cu;
3214 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3215 {
3216 const auto insertpair
3217 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3218 if (!insertpair.second)
3219 {
3220 warning (_("Section .debug_aranges in %s has duplicate "
3221 "debug_info_offset %s, ignoring .debug_aranges."),
3222 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3223 return;
3224 }
3225 }
3226
3227 dwarf2_read_section (objfile, section);
3228
3229 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3230
3231 const gdb_byte *addr = section->buffer;
3232
3233 while (addr < section->buffer + section->size)
3234 {
3235 const gdb_byte *const entry_addr = addr;
3236 unsigned int bytes_read;
3237
3238 const LONGEST entry_length = read_initial_length (abfd, addr,
3239 &bytes_read);
3240 addr += bytes_read;
3241
3242 const gdb_byte *const entry_end = addr + entry_length;
3243 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3244 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3245 if (addr + entry_length > section->buffer + section->size)
3246 {
3247 warning (_("Section .debug_aranges in %s entry at offset %zu "
3248 "length %s exceeds section length %s, "
3249 "ignoring .debug_aranges."),
3250 objfile_name (objfile), entry_addr - section->buffer,
3251 plongest (bytes_read + entry_length),
3252 pulongest (section->size));
3253 return;
3254 }
3255
3256 /* The version number. */
3257 const uint16_t version = read_2_bytes (abfd, addr);
3258 addr += 2;
3259 if (version != 2)
3260 {
3261 warning (_("Section .debug_aranges in %s entry at offset %zu "
3262 "has unsupported version %d, ignoring .debug_aranges."),
3263 objfile_name (objfile), entry_addr - section->buffer,
3264 version);
3265 return;
3266 }
3267
3268 const uint64_t debug_info_offset
3269 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3270 addr += offset_size;
3271 const auto per_cu_it
3272 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3273 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3274 {
3275 warning (_("Section .debug_aranges in %s entry at offset %zu "
3276 "debug_info_offset %s does not exists, "
3277 "ignoring .debug_aranges."),
3278 objfile_name (objfile), entry_addr - section->buffer,
3279 pulongest (debug_info_offset));
3280 return;
3281 }
3282 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3283
3284 const uint8_t address_size = *addr++;
3285 if (address_size < 1 || address_size > 8)
3286 {
3287 warning (_("Section .debug_aranges in %s entry at offset %zu "
3288 "address_size %u is invalid, ignoring .debug_aranges."),
3289 objfile_name (objfile), entry_addr - section->buffer,
3290 address_size);
3291 return;
3292 }
3293
3294 const uint8_t segment_selector_size = *addr++;
3295 if (segment_selector_size != 0)
3296 {
3297 warning (_("Section .debug_aranges in %s entry at offset %zu "
3298 "segment_selector_size %u is not supported, "
3299 "ignoring .debug_aranges."),
3300 objfile_name (objfile), entry_addr - section->buffer,
3301 segment_selector_size);
3302 return;
3303 }
3304
3305 /* Must pad to an alignment boundary that is twice the address
3306 size. It is undocumented by the DWARF standard but GCC does
3307 use it. */
3308 for (size_t padding = ((-(addr - section->buffer))
3309 & (2 * address_size - 1));
3310 padding > 0; padding--)
3311 if (*addr++ != 0)
3312 {
3313 warning (_("Section .debug_aranges in %s entry at offset %zu "
3314 "padding is not zero, ignoring .debug_aranges."),
3315 objfile_name (objfile), entry_addr - section->buffer);
3316 return;
3317 }
3318
3319 for (;;)
3320 {
3321 if (addr + 2 * address_size > entry_end)
3322 {
3323 warning (_("Section .debug_aranges in %s entry at offset %zu "
3324 "address list is not properly terminated, "
3325 "ignoring .debug_aranges."),
3326 objfile_name (objfile), entry_addr - section->buffer);
3327 return;
3328 }
3329 ULONGEST start = extract_unsigned_integer (addr, address_size,
3330 dwarf5_byte_order);
3331 addr += address_size;
3332 ULONGEST length = extract_unsigned_integer (addr, address_size,
3333 dwarf5_byte_order);
3334 addr += address_size;
3335 if (start == 0 && length == 0)
3336 break;
3337 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3338 {
3339 /* Symbol was eliminated due to a COMDAT group. */
3340 continue;
3341 }
3342 ULONGEST end = start + length;
3343 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3344 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3345 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3346 }
3347 }
3348
3349 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3350 &objfile->objfile_obstack);
3351 }
3352
3353 /* Find a slot in the mapped index INDEX for the object named NAME.
3354 If NAME is found, set *VEC_OUT to point to the CU vector in the
3355 constant pool and return true. If NAME cannot be found, return
3356 false. */
3357
3358 static bool
3359 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3360 offset_type **vec_out)
3361 {
3362 offset_type hash;
3363 offset_type slot, step;
3364 int (*cmp) (const char *, const char *);
3365
3366 gdb::unique_xmalloc_ptr<char> without_params;
3367 if (current_language->la_language == language_cplus
3368 || current_language->la_language == language_fortran
3369 || current_language->la_language == language_d)
3370 {
3371 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3372 not contain any. */
3373
3374 if (strchr (name, '(') != NULL)
3375 {
3376 without_params = cp_remove_params (name);
3377
3378 if (without_params != NULL)
3379 name = without_params.get ();
3380 }
3381 }
3382
3383 /* Index version 4 did not support case insensitive searches. But the
3384 indices for case insensitive languages are built in lowercase, therefore
3385 simulate our NAME being searched is also lowercased. */
3386 hash = mapped_index_string_hash ((index->version == 4
3387 && case_sensitivity == case_sensitive_off
3388 ? 5 : index->version),
3389 name);
3390
3391 slot = hash & (index->symbol_table.size () - 1);
3392 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3393 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3394
3395 for (;;)
3396 {
3397 const char *str;
3398
3399 const auto &bucket = index->symbol_table[slot];
3400 if (bucket.name == 0 && bucket.vec == 0)
3401 return false;
3402
3403 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3404 if (!cmp (name, str))
3405 {
3406 *vec_out = (offset_type *) (index->constant_pool
3407 + MAYBE_SWAP (bucket.vec));
3408 return true;
3409 }
3410
3411 slot = (slot + step) & (index->symbol_table.size () - 1);
3412 }
3413 }
3414
3415 /* A helper function that reads the .gdb_index from SECTION and fills
3416 in MAP. FILENAME is the name of the file containing the section;
3417 it is used for error reporting. DEPRECATED_OK is true if it is
3418 ok to use deprecated sections.
3419
3420 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3421 out parameters that are filled in with information about the CU and
3422 TU lists in the section.
3423
3424 Returns 1 if all went well, 0 otherwise. */
3425
3426 static bool
3427 read_index_from_section (struct objfile *objfile,
3428 const char *filename,
3429 bool deprecated_ok,
3430 struct dwarf2_section_info *section,
3431 struct mapped_index *map,
3432 const gdb_byte **cu_list,
3433 offset_type *cu_list_elements,
3434 const gdb_byte **types_list,
3435 offset_type *types_list_elements)
3436 {
3437 const gdb_byte *addr;
3438 offset_type version;
3439 offset_type *metadata;
3440 int i;
3441
3442 if (dwarf2_section_empty_p (section))
3443 return 0;
3444
3445 /* Older elfutils strip versions could keep the section in the main
3446 executable while splitting it for the separate debug info file. */
3447 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3448 return 0;
3449
3450 dwarf2_read_section (objfile, section);
3451
3452 addr = section->buffer;
3453 /* Version check. */
3454 version = MAYBE_SWAP (*(offset_type *) addr);
3455 /* Versions earlier than 3 emitted every copy of a psymbol. This
3456 causes the index to behave very poorly for certain requests. Version 3
3457 contained incomplete addrmap. So, it seems better to just ignore such
3458 indices. */
3459 if (version < 4)
3460 {
3461 static int warning_printed = 0;
3462 if (!warning_printed)
3463 {
3464 warning (_("Skipping obsolete .gdb_index section in %s."),
3465 filename);
3466 warning_printed = 1;
3467 }
3468 return 0;
3469 }
3470 /* Index version 4 uses a different hash function than index version
3471 5 and later.
3472
3473 Versions earlier than 6 did not emit psymbols for inlined
3474 functions. Using these files will cause GDB not to be able to
3475 set breakpoints on inlined functions by name, so we ignore these
3476 indices unless the user has done
3477 "set use-deprecated-index-sections on". */
3478 if (version < 6 && !deprecated_ok)
3479 {
3480 static int warning_printed = 0;
3481 if (!warning_printed)
3482 {
3483 warning (_("\
3484 Skipping deprecated .gdb_index section in %s.\n\
3485 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3486 to use the section anyway."),
3487 filename);
3488 warning_printed = 1;
3489 }
3490 return 0;
3491 }
3492 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3493 of the TU (for symbols coming from TUs),
3494 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3495 Plus gold-generated indices can have duplicate entries for global symbols,
3496 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3497 These are just performance bugs, and we can't distinguish gdb-generated
3498 indices from gold-generated ones, so issue no warning here. */
3499
3500 /* Indexes with higher version than the one supported by GDB may be no
3501 longer backward compatible. */
3502 if (version > 8)
3503 return 0;
3504
3505 map->version = version;
3506
3507 metadata = (offset_type *) (addr + sizeof (offset_type));
3508
3509 i = 0;
3510 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3511 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3512 / 8);
3513 ++i;
3514
3515 *types_list = addr + MAYBE_SWAP (metadata[i]);
3516 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3517 - MAYBE_SWAP (metadata[i]))
3518 / 8);
3519 ++i;
3520
3521 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3522 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3523 map->address_table
3524 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3525 ++i;
3526
3527 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3528 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3529 map->symbol_table
3530 = gdb::array_view<mapped_index::symbol_table_slot>
3531 ((mapped_index::symbol_table_slot *) symbol_table,
3532 (mapped_index::symbol_table_slot *) symbol_table_end);
3533
3534 ++i;
3535 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3536
3537 return 1;
3538 }
3539
3540 /* Read .gdb_index. If everything went ok, initialize the "quick"
3541 elements of all the CUs and return 1. Otherwise, return 0. */
3542
3543 static int
3544 dwarf2_read_index (struct dwarf2_per_objfile *dwarf2_per_objfile)
3545 {
3546 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3547 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3548 struct dwz_file *dwz;
3549 struct objfile *objfile = dwarf2_per_objfile->objfile;
3550
3551 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3552 if (!read_index_from_section (objfile, objfile_name (objfile),
3553 use_deprecated_index_sections,
3554 &dwarf2_per_objfile->gdb_index, map.get (),
3555 &cu_list, &cu_list_elements,
3556 &types_list, &types_list_elements))
3557 return 0;
3558
3559 /* Don't use the index if it's empty. */
3560 if (map->symbol_table.empty ())
3561 return 0;
3562
3563 /* If there is a .dwz file, read it so we can get its CU list as
3564 well. */
3565 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3566 if (dwz != NULL)
3567 {
3568 struct mapped_index dwz_map;
3569 const gdb_byte *dwz_types_ignore;
3570 offset_type dwz_types_elements_ignore;
3571
3572 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3573 1,
3574 &dwz->gdb_index, &dwz_map,
3575 &dwz_list, &dwz_list_elements,
3576 &dwz_types_ignore,
3577 &dwz_types_elements_ignore))
3578 {
3579 warning (_("could not read '.gdb_index' section from %s; skipping"),
3580 bfd_get_filename (dwz->dwz_bfd));
3581 return 0;
3582 }
3583 }
3584
3585 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3586 dwz_list, dwz_list_elements);
3587
3588 if (types_list_elements)
3589 {
3590 struct dwarf2_section_info *section;
3591
3592 /* We can only handle a single .debug_types when we have an
3593 index. */
3594 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3595 return 0;
3596
3597 section = VEC_index (dwarf2_section_info_def,
3598 dwarf2_per_objfile->types, 0);
3599
3600 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3601 types_list, types_list_elements);
3602 }
3603
3604 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3605
3606 dwarf2_per_objfile->index_table = std::move (map);
3607 dwarf2_per_objfile->using_index = 1;
3608 dwarf2_per_objfile->quick_file_names_table =
3609 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3610
3611 return 1;
3612 }
3613
3614 /* die_reader_func for dw2_get_file_names. */
3615
3616 static void
3617 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3618 const gdb_byte *info_ptr,
3619 struct die_info *comp_unit_die,
3620 int has_children,
3621 void *data)
3622 {
3623 struct dwarf2_cu *cu = reader->cu;
3624 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3625 struct dwarf2_per_objfile *dwarf2_per_objfile
3626 = cu->per_cu->dwarf2_per_objfile;
3627 struct objfile *objfile = dwarf2_per_objfile->objfile;
3628 struct dwarf2_per_cu_data *lh_cu;
3629 struct attribute *attr;
3630 int i;
3631 void **slot;
3632 struct quick_file_names *qfn;
3633
3634 gdb_assert (! this_cu->is_debug_types);
3635
3636 /* Our callers never want to match partial units -- instead they
3637 will match the enclosing full CU. */
3638 if (comp_unit_die->tag == DW_TAG_partial_unit)
3639 {
3640 this_cu->v.quick->no_file_data = 1;
3641 return;
3642 }
3643
3644 lh_cu = this_cu;
3645 slot = NULL;
3646
3647 line_header_up lh;
3648 sect_offset line_offset {};
3649
3650 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3651 if (attr)
3652 {
3653 struct quick_file_names find_entry;
3654
3655 line_offset = (sect_offset) DW_UNSND (attr);
3656
3657 /* We may have already read in this line header (TU line header sharing).
3658 If we have we're done. */
3659 find_entry.hash.dwo_unit = cu->dwo_unit;
3660 find_entry.hash.line_sect_off = line_offset;
3661 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3662 &find_entry, INSERT);
3663 if (*slot != NULL)
3664 {
3665 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3666 return;
3667 }
3668
3669 lh = dwarf_decode_line_header (line_offset, cu);
3670 }
3671 if (lh == NULL)
3672 {
3673 lh_cu->v.quick->no_file_data = 1;
3674 return;
3675 }
3676
3677 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3678 qfn->hash.dwo_unit = cu->dwo_unit;
3679 qfn->hash.line_sect_off = line_offset;
3680 gdb_assert (slot != NULL);
3681 *slot = qfn;
3682
3683 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3684
3685 qfn->num_file_names = lh->file_names.size ();
3686 qfn->file_names =
3687 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3688 for (i = 0; i < lh->file_names.size (); ++i)
3689 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3690 qfn->real_names = NULL;
3691
3692 lh_cu->v.quick->file_names = qfn;
3693 }
3694
3695 /* A helper for the "quick" functions which attempts to read the line
3696 table for THIS_CU. */
3697
3698 static struct quick_file_names *
3699 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3700 {
3701 /* This should never be called for TUs. */
3702 gdb_assert (! this_cu->is_debug_types);
3703 /* Nor type unit groups. */
3704 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3705
3706 if (this_cu->v.quick->file_names != NULL)
3707 return this_cu->v.quick->file_names;
3708 /* If we know there is no line data, no point in looking again. */
3709 if (this_cu->v.quick->no_file_data)
3710 return NULL;
3711
3712 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3713
3714 if (this_cu->v.quick->no_file_data)
3715 return NULL;
3716 return this_cu->v.quick->file_names;
3717 }
3718
3719 /* A helper for the "quick" functions which computes and caches the
3720 real path for a given file name from the line table. */
3721
3722 static const char *
3723 dw2_get_real_path (struct objfile *objfile,
3724 struct quick_file_names *qfn, int index)
3725 {
3726 if (qfn->real_names == NULL)
3727 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3728 qfn->num_file_names, const char *);
3729
3730 if (qfn->real_names[index] == NULL)
3731 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3732
3733 return qfn->real_names[index];
3734 }
3735
3736 static struct symtab *
3737 dw2_find_last_source_symtab (struct objfile *objfile)
3738 {
3739 struct dwarf2_per_objfile *dwarf2_per_objfile
3740 = get_dwarf2_per_objfile (objfile);
3741 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3742 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3743
3744 if (cust == NULL)
3745 return NULL;
3746
3747 return compunit_primary_filetab (cust);
3748 }
3749
3750 /* Traversal function for dw2_forget_cached_source_info. */
3751
3752 static int
3753 dw2_free_cached_file_names (void **slot, void *info)
3754 {
3755 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3756
3757 if (file_data->real_names)
3758 {
3759 int i;
3760
3761 for (i = 0; i < file_data->num_file_names; ++i)
3762 {
3763 xfree ((void*) file_data->real_names[i]);
3764 file_data->real_names[i] = NULL;
3765 }
3766 }
3767
3768 return 1;
3769 }
3770
3771 static void
3772 dw2_forget_cached_source_info (struct objfile *objfile)
3773 {
3774 struct dwarf2_per_objfile *dwarf2_per_objfile
3775 = get_dwarf2_per_objfile (objfile);
3776
3777 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3778 dw2_free_cached_file_names, NULL);
3779 }
3780
3781 /* Helper function for dw2_map_symtabs_matching_filename that expands
3782 the symtabs and calls the iterator. */
3783
3784 static int
3785 dw2_map_expand_apply (struct objfile *objfile,
3786 struct dwarf2_per_cu_data *per_cu,
3787 const char *name, const char *real_path,
3788 gdb::function_view<bool (symtab *)> callback)
3789 {
3790 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3791
3792 /* Don't visit already-expanded CUs. */
3793 if (per_cu->v.quick->compunit_symtab)
3794 return 0;
3795
3796 /* This may expand more than one symtab, and we want to iterate over
3797 all of them. */
3798 dw2_instantiate_symtab (per_cu, false);
3799
3800 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3801 last_made, callback);
3802 }
3803
3804 /* Implementation of the map_symtabs_matching_filename method. */
3805
3806 static bool
3807 dw2_map_symtabs_matching_filename
3808 (struct objfile *objfile, const char *name, const char *real_path,
3809 gdb::function_view<bool (symtab *)> callback)
3810 {
3811 const char *name_basename = lbasename (name);
3812 struct dwarf2_per_objfile *dwarf2_per_objfile
3813 = get_dwarf2_per_objfile (objfile);
3814
3815 /* The rule is CUs specify all the files, including those used by
3816 any TU, so there's no need to scan TUs here. */
3817
3818 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3819 {
3820 /* We only need to look at symtabs not already expanded. */
3821 if (per_cu->v.quick->compunit_symtab)
3822 continue;
3823
3824 quick_file_names *file_data = dw2_get_file_names (per_cu);
3825 if (file_data == NULL)
3826 continue;
3827
3828 for (int j = 0; j < file_data->num_file_names; ++j)
3829 {
3830 const char *this_name = file_data->file_names[j];
3831 const char *this_real_name;
3832
3833 if (compare_filenames_for_search (this_name, name))
3834 {
3835 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3836 callback))
3837 return true;
3838 continue;
3839 }
3840
3841 /* Before we invoke realpath, which can get expensive when many
3842 files are involved, do a quick comparison of the basenames. */
3843 if (! basenames_may_differ
3844 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3845 continue;
3846
3847 this_real_name = dw2_get_real_path (objfile, file_data, j);
3848 if (compare_filenames_for_search (this_real_name, name))
3849 {
3850 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3851 callback))
3852 return true;
3853 continue;
3854 }
3855
3856 if (real_path != NULL)
3857 {
3858 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3859 gdb_assert (IS_ABSOLUTE_PATH (name));
3860 if (this_real_name != NULL
3861 && FILENAME_CMP (real_path, this_real_name) == 0)
3862 {
3863 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3864 callback))
3865 return true;
3866 continue;
3867 }
3868 }
3869 }
3870 }
3871
3872 return false;
3873 }
3874
3875 /* Struct used to manage iterating over all CUs looking for a symbol. */
3876
3877 struct dw2_symtab_iterator
3878 {
3879 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3880 struct dwarf2_per_objfile *dwarf2_per_objfile;
3881 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3882 int want_specific_block;
3883 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3884 Unused if !WANT_SPECIFIC_BLOCK. */
3885 int block_index;
3886 /* The kind of symbol we're looking for. */
3887 domain_enum domain;
3888 /* The list of CUs from the index entry of the symbol,
3889 or NULL if not found. */
3890 offset_type *vec;
3891 /* The next element in VEC to look at. */
3892 int next;
3893 /* The number of elements in VEC, or zero if there is no match. */
3894 int length;
3895 /* Have we seen a global version of the symbol?
3896 If so we can ignore all further global instances.
3897 This is to work around gold/15646, inefficient gold-generated
3898 indices. */
3899 int global_seen;
3900 };
3901
3902 /* Initialize the index symtab iterator ITER.
3903 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3904 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3905
3906 static void
3907 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3908 struct dwarf2_per_objfile *dwarf2_per_objfile,
3909 int want_specific_block,
3910 int block_index,
3911 domain_enum domain,
3912 const char *name)
3913 {
3914 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3915 iter->want_specific_block = want_specific_block;
3916 iter->block_index = block_index;
3917 iter->domain = domain;
3918 iter->next = 0;
3919 iter->global_seen = 0;
3920
3921 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3922
3923 /* index is NULL if OBJF_READNOW. */
3924 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3925 iter->length = MAYBE_SWAP (*iter->vec);
3926 else
3927 {
3928 iter->vec = NULL;
3929 iter->length = 0;
3930 }
3931 }
3932
3933 /* Return the next matching CU or NULL if there are no more. */
3934
3935 static struct dwarf2_per_cu_data *
3936 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3937 {
3938 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3939
3940 for ( ; iter->next < iter->length; ++iter->next)
3941 {
3942 offset_type cu_index_and_attrs =
3943 MAYBE_SWAP (iter->vec[iter->next + 1]);
3944 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3945 int want_static = iter->block_index != GLOBAL_BLOCK;
3946 /* This value is only valid for index versions >= 7. */
3947 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3948 gdb_index_symbol_kind symbol_kind =
3949 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3950 /* Only check the symbol attributes if they're present.
3951 Indices prior to version 7 don't record them,
3952 and indices >= 7 may elide them for certain symbols
3953 (gold does this). */
3954 int attrs_valid =
3955 (dwarf2_per_objfile->index_table->version >= 7
3956 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3957
3958 /* Don't crash on bad data. */
3959 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3960 + dwarf2_per_objfile->all_type_units.size ()))
3961 {
3962 complaint (&symfile_complaints,
3963 _(".gdb_index entry has bad CU index"
3964 " [in module %s]"),
3965 objfile_name (dwarf2_per_objfile->objfile));
3966 continue;
3967 }
3968
3969 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3970
3971 /* Skip if already read in. */
3972 if (per_cu->v.quick->compunit_symtab)
3973 continue;
3974
3975 /* Check static vs global. */
3976 if (attrs_valid)
3977 {
3978 if (iter->want_specific_block
3979 && want_static != is_static)
3980 continue;
3981 /* Work around gold/15646. */
3982 if (!is_static && iter->global_seen)
3983 continue;
3984 if (!is_static)
3985 iter->global_seen = 1;
3986 }
3987
3988 /* Only check the symbol's kind if it has one. */
3989 if (attrs_valid)
3990 {
3991 switch (iter->domain)
3992 {
3993 case VAR_DOMAIN:
3994 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3995 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3996 /* Some types are also in VAR_DOMAIN. */
3997 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3998 continue;
3999 break;
4000 case STRUCT_DOMAIN:
4001 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4002 continue;
4003 break;
4004 case LABEL_DOMAIN:
4005 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4006 continue;
4007 break;
4008 default:
4009 break;
4010 }
4011 }
4012
4013 ++iter->next;
4014 return per_cu;
4015 }
4016
4017 return NULL;
4018 }
4019
4020 static struct compunit_symtab *
4021 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4022 const char *name, domain_enum domain)
4023 {
4024 struct compunit_symtab *stab_best = NULL;
4025 struct dwarf2_per_objfile *dwarf2_per_objfile
4026 = get_dwarf2_per_objfile (objfile);
4027
4028 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4029
4030 struct dw2_symtab_iterator iter;
4031 struct dwarf2_per_cu_data *per_cu;
4032
4033 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4034
4035 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4036 {
4037 struct symbol *sym, *with_opaque = NULL;
4038 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4039 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4040 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4041
4042 sym = block_find_symbol (block, name, domain,
4043 block_find_non_opaque_type_preferred,
4044 &with_opaque);
4045
4046 /* Some caution must be observed with overloaded functions
4047 and methods, since the index will not contain any overload
4048 information (but NAME might contain it). */
4049
4050 if (sym != NULL
4051 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4052 return stab;
4053 if (with_opaque != NULL
4054 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4055 stab_best = stab;
4056
4057 /* Keep looking through other CUs. */
4058 }
4059
4060 return stab_best;
4061 }
4062
4063 static void
4064 dw2_print_stats (struct objfile *objfile)
4065 {
4066 struct dwarf2_per_objfile *dwarf2_per_objfile
4067 = get_dwarf2_per_objfile (objfile);
4068 int total = (dwarf2_per_objfile->all_comp_units.size ()
4069 + dwarf2_per_objfile->all_type_units.size ());
4070 int count = 0;
4071
4072 for (int i = 0; i < total; ++i)
4073 {
4074 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4075
4076 if (!per_cu->v.quick->compunit_symtab)
4077 ++count;
4078 }
4079 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4080 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4081 }
4082
4083 /* This dumps minimal information about the index.
4084 It is called via "mt print objfiles".
4085 One use is to verify .gdb_index has been loaded by the
4086 gdb.dwarf2/gdb-index.exp testcase. */
4087
4088 static void
4089 dw2_dump (struct objfile *objfile)
4090 {
4091 struct dwarf2_per_objfile *dwarf2_per_objfile
4092 = get_dwarf2_per_objfile (objfile);
4093
4094 gdb_assert (dwarf2_per_objfile->using_index);
4095 printf_filtered (".gdb_index:");
4096 if (dwarf2_per_objfile->index_table != NULL)
4097 {
4098 printf_filtered (" version %d\n",
4099 dwarf2_per_objfile->index_table->version);
4100 }
4101 else
4102 printf_filtered (" faked for \"readnow\"\n");
4103 printf_filtered ("\n");
4104 }
4105
4106 static void
4107 dw2_relocate (struct objfile *objfile,
4108 const struct section_offsets *new_offsets,
4109 const struct section_offsets *delta)
4110 {
4111 /* There's nothing to relocate here. */
4112 }
4113
4114 static void
4115 dw2_expand_symtabs_for_function (struct objfile *objfile,
4116 const char *func_name)
4117 {
4118 struct dwarf2_per_objfile *dwarf2_per_objfile
4119 = get_dwarf2_per_objfile (objfile);
4120
4121 struct dw2_symtab_iterator iter;
4122 struct dwarf2_per_cu_data *per_cu;
4123
4124 /* Note: It doesn't matter what we pass for block_index here. */
4125 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4126 func_name);
4127
4128 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4129 dw2_instantiate_symtab (per_cu, false);
4130
4131 }
4132
4133 static void
4134 dw2_expand_all_symtabs (struct objfile *objfile)
4135 {
4136 struct dwarf2_per_objfile *dwarf2_per_objfile
4137 = get_dwarf2_per_objfile (objfile);
4138 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4139 + dwarf2_per_objfile->all_type_units.size ());
4140
4141 for (int i = 0; i < total_units; ++i)
4142 {
4143 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4144
4145 /* We don't want to directly expand a partial CU, because if we
4146 read it with the wrong language, then assertion failures can
4147 be triggered later on. See PR symtab/23010. So, tell
4148 dw2_instantiate_symtab to skip partial CUs -- any important
4149 partial CU will be read via DW_TAG_imported_unit anyway. */
4150 dw2_instantiate_symtab (per_cu, true);
4151 }
4152 }
4153
4154 static void
4155 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4156 const char *fullname)
4157 {
4158 struct dwarf2_per_objfile *dwarf2_per_objfile
4159 = get_dwarf2_per_objfile (objfile);
4160
4161 /* We don't need to consider type units here.
4162 This is only called for examining code, e.g. expand_line_sal.
4163 There can be an order of magnitude (or more) more type units
4164 than comp units, and we avoid them if we can. */
4165
4166 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4167 {
4168 /* We only need to look at symtabs not already expanded. */
4169 if (per_cu->v.quick->compunit_symtab)
4170 continue;
4171
4172 quick_file_names *file_data = dw2_get_file_names (per_cu);
4173 if (file_data == NULL)
4174 continue;
4175
4176 for (int j = 0; j < file_data->num_file_names; ++j)
4177 {
4178 const char *this_fullname = file_data->file_names[j];
4179
4180 if (filename_cmp (this_fullname, fullname) == 0)
4181 {
4182 dw2_instantiate_symtab (per_cu, false);
4183 break;
4184 }
4185 }
4186 }
4187 }
4188
4189 static void
4190 dw2_map_matching_symbols (struct objfile *objfile,
4191 const char * name, domain_enum domain,
4192 int global,
4193 int (*callback) (struct block *,
4194 struct symbol *, void *),
4195 void *data, symbol_name_match_type match,
4196 symbol_compare_ftype *ordered_compare)
4197 {
4198 /* Currently unimplemented; used for Ada. The function can be called if the
4199 current language is Ada for a non-Ada objfile using GNU index. As Ada
4200 does not look for non-Ada symbols this function should just return. */
4201 }
4202
4203 /* Symbol name matcher for .gdb_index names.
4204
4205 Symbol names in .gdb_index have a few particularities:
4206
4207 - There's no indication of which is the language of each symbol.
4208
4209 Since each language has its own symbol name matching algorithm,
4210 and we don't know which language is the right one, we must match
4211 each symbol against all languages. This would be a potential
4212 performance problem if it were not mitigated by the
4213 mapped_index::name_components lookup table, which significantly
4214 reduces the number of times we need to call into this matcher,
4215 making it a non-issue.
4216
4217 - Symbol names in the index have no overload (parameter)
4218 information. I.e., in C++, "foo(int)" and "foo(long)" both
4219 appear as "foo" in the index, for example.
4220
4221 This means that the lookup names passed to the symbol name
4222 matcher functions must have no parameter information either
4223 because (e.g.) symbol search name "foo" does not match
4224 lookup-name "foo(int)" [while swapping search name for lookup
4225 name would match].
4226 */
4227 class gdb_index_symbol_name_matcher
4228 {
4229 public:
4230 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4231 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4232
4233 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4234 Returns true if any matcher matches. */
4235 bool matches (const char *symbol_name);
4236
4237 private:
4238 /* A reference to the lookup name we're matching against. */
4239 const lookup_name_info &m_lookup_name;
4240
4241 /* A vector holding all the different symbol name matchers, for all
4242 languages. */
4243 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4244 };
4245
4246 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4247 (const lookup_name_info &lookup_name)
4248 : m_lookup_name (lookup_name)
4249 {
4250 /* Prepare the vector of comparison functions upfront, to avoid
4251 doing the same work for each symbol. Care is taken to avoid
4252 matching with the same matcher more than once if/when multiple
4253 languages use the same matcher function. */
4254 auto &matchers = m_symbol_name_matcher_funcs;
4255 matchers.reserve (nr_languages);
4256
4257 matchers.push_back (default_symbol_name_matcher);
4258
4259 for (int i = 0; i < nr_languages; i++)
4260 {
4261 const language_defn *lang = language_def ((enum language) i);
4262 symbol_name_matcher_ftype *name_matcher
4263 = get_symbol_name_matcher (lang, m_lookup_name);
4264
4265 /* Don't insert the same comparison routine more than once.
4266 Note that we do this linear walk instead of a seemingly
4267 cheaper sorted insert, or use a std::set or something like
4268 that, because relative order of function addresses is not
4269 stable. This is not a problem in practice because the number
4270 of supported languages is low, and the cost here is tiny
4271 compared to the number of searches we'll do afterwards using
4272 this object. */
4273 if (name_matcher != default_symbol_name_matcher
4274 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4275 == matchers.end ()))
4276 matchers.push_back (name_matcher);
4277 }
4278 }
4279
4280 bool
4281 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4282 {
4283 for (auto matches_name : m_symbol_name_matcher_funcs)
4284 if (matches_name (symbol_name, m_lookup_name, NULL))
4285 return true;
4286
4287 return false;
4288 }
4289
4290 /* Starting from a search name, return the string that finds the upper
4291 bound of all strings that start with SEARCH_NAME in a sorted name
4292 list. Returns the empty string to indicate that the upper bound is
4293 the end of the list. */
4294
4295 static std::string
4296 make_sort_after_prefix_name (const char *search_name)
4297 {
4298 /* When looking to complete "func", we find the upper bound of all
4299 symbols that start with "func" by looking for where we'd insert
4300 the closest string that would follow "func" in lexicographical
4301 order. Usually, that's "func"-with-last-character-incremented,
4302 i.e. "fund". Mind non-ASCII characters, though. Usually those
4303 will be UTF-8 multi-byte sequences, but we can't be certain.
4304 Especially mind the 0xff character, which is a valid character in
4305 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4306 rule out compilers allowing it in identifiers. Note that
4307 conveniently, strcmp/strcasecmp are specified to compare
4308 characters interpreted as unsigned char. So what we do is treat
4309 the whole string as a base 256 number composed of a sequence of
4310 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4311 to 0, and carries 1 to the following more-significant position.
4312 If the very first character in SEARCH_NAME ends up incremented
4313 and carries/overflows, then the upper bound is the end of the
4314 list. The string after the empty string is also the empty
4315 string.
4316
4317 Some examples of this operation:
4318
4319 SEARCH_NAME => "+1" RESULT
4320
4321 "abc" => "abd"
4322 "ab\xff" => "ac"
4323 "\xff" "a" "\xff" => "\xff" "b"
4324 "\xff" => ""
4325 "\xff\xff" => ""
4326 "" => ""
4327
4328 Then, with these symbols for example:
4329
4330 func
4331 func1
4332 fund
4333
4334 completing "func" looks for symbols between "func" and
4335 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4336 which finds "func" and "func1", but not "fund".
4337
4338 And with:
4339
4340 funcÿ (Latin1 'ÿ' [0xff])
4341 funcÿ1
4342 fund
4343
4344 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4345 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4346
4347 And with:
4348
4349 ÿÿ (Latin1 'ÿ' [0xff])
4350 ÿÿ1
4351
4352 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4353 the end of the list.
4354 */
4355 std::string after = search_name;
4356 while (!after.empty () && (unsigned char) after.back () == 0xff)
4357 after.pop_back ();
4358 if (!after.empty ())
4359 after.back () = (unsigned char) after.back () + 1;
4360 return after;
4361 }
4362
4363 /* See declaration. */
4364
4365 std::pair<std::vector<name_component>::const_iterator,
4366 std::vector<name_component>::const_iterator>
4367 mapped_index_base::find_name_components_bounds
4368 (const lookup_name_info &lookup_name_without_params) const
4369 {
4370 auto *name_cmp
4371 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4372
4373 const char *cplus
4374 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4375
4376 /* Comparison function object for lower_bound that matches against a
4377 given symbol name. */
4378 auto lookup_compare_lower = [&] (const name_component &elem,
4379 const char *name)
4380 {
4381 const char *elem_qualified = this->symbol_name_at (elem.idx);
4382 const char *elem_name = elem_qualified + elem.name_offset;
4383 return name_cmp (elem_name, name) < 0;
4384 };
4385
4386 /* Comparison function object for upper_bound that matches against a
4387 given symbol name. */
4388 auto lookup_compare_upper = [&] (const char *name,
4389 const name_component &elem)
4390 {
4391 const char *elem_qualified = this->symbol_name_at (elem.idx);
4392 const char *elem_name = elem_qualified + elem.name_offset;
4393 return name_cmp (name, elem_name) < 0;
4394 };
4395
4396 auto begin = this->name_components.begin ();
4397 auto end = this->name_components.end ();
4398
4399 /* Find the lower bound. */
4400 auto lower = [&] ()
4401 {
4402 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4403 return begin;
4404 else
4405 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4406 } ();
4407
4408 /* Find the upper bound. */
4409 auto upper = [&] ()
4410 {
4411 if (lookup_name_without_params.completion_mode ())
4412 {
4413 /* In completion mode, we want UPPER to point past all
4414 symbols names that have the same prefix. I.e., with
4415 these symbols, and completing "func":
4416
4417 function << lower bound
4418 function1
4419 other_function << upper bound
4420
4421 We find the upper bound by looking for the insertion
4422 point of "func"-with-last-character-incremented,
4423 i.e. "fund". */
4424 std::string after = make_sort_after_prefix_name (cplus);
4425 if (after.empty ())
4426 return end;
4427 return std::lower_bound (lower, end, after.c_str (),
4428 lookup_compare_lower);
4429 }
4430 else
4431 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4432 } ();
4433
4434 return {lower, upper};
4435 }
4436
4437 /* See declaration. */
4438
4439 void
4440 mapped_index_base::build_name_components ()
4441 {
4442 if (!this->name_components.empty ())
4443 return;
4444
4445 this->name_components_casing = case_sensitivity;
4446 auto *name_cmp
4447 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4448
4449 /* The code below only knows how to break apart components of C++
4450 symbol names (and other languages that use '::' as
4451 namespace/module separator). If we add support for wild matching
4452 to some language that uses some other operator (E.g., Ada, Go and
4453 D use '.'), then we'll need to try splitting the symbol name
4454 according to that language too. Note that Ada does support wild
4455 matching, but doesn't currently support .gdb_index. */
4456 auto count = this->symbol_name_count ();
4457 for (offset_type idx = 0; idx < count; idx++)
4458 {
4459 if (this->symbol_name_slot_invalid (idx))
4460 continue;
4461
4462 const char *name = this->symbol_name_at (idx);
4463
4464 /* Add each name component to the name component table. */
4465 unsigned int previous_len = 0;
4466 for (unsigned int current_len = cp_find_first_component (name);
4467 name[current_len] != '\0';
4468 current_len += cp_find_first_component (name + current_len))
4469 {
4470 gdb_assert (name[current_len] == ':');
4471 this->name_components.push_back ({previous_len, idx});
4472 /* Skip the '::'. */
4473 current_len += 2;
4474 previous_len = current_len;
4475 }
4476 this->name_components.push_back ({previous_len, idx});
4477 }
4478
4479 /* Sort name_components elements by name. */
4480 auto name_comp_compare = [&] (const name_component &left,
4481 const name_component &right)
4482 {
4483 const char *left_qualified = this->symbol_name_at (left.idx);
4484 const char *right_qualified = this->symbol_name_at (right.idx);
4485
4486 const char *left_name = left_qualified + left.name_offset;
4487 const char *right_name = right_qualified + right.name_offset;
4488
4489 return name_cmp (left_name, right_name) < 0;
4490 };
4491
4492 std::sort (this->name_components.begin (),
4493 this->name_components.end (),
4494 name_comp_compare);
4495 }
4496
4497 /* Helper for dw2_expand_symtabs_matching that works with a
4498 mapped_index_base instead of the containing objfile. This is split
4499 to a separate function in order to be able to unit test the
4500 name_components matching using a mock mapped_index_base. For each
4501 symbol name that matches, calls MATCH_CALLBACK, passing it the
4502 symbol's index in the mapped_index_base symbol table. */
4503
4504 static void
4505 dw2_expand_symtabs_matching_symbol
4506 (mapped_index_base &index,
4507 const lookup_name_info &lookup_name_in,
4508 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4509 enum search_domain kind,
4510 gdb::function_view<void (offset_type)> match_callback)
4511 {
4512 lookup_name_info lookup_name_without_params
4513 = lookup_name_in.make_ignore_params ();
4514 gdb_index_symbol_name_matcher lookup_name_matcher
4515 (lookup_name_without_params);
4516
4517 /* Build the symbol name component sorted vector, if we haven't
4518 yet. */
4519 index.build_name_components ();
4520
4521 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4522
4523 /* Now for each symbol name in range, check to see if we have a name
4524 match, and if so, call the MATCH_CALLBACK callback. */
4525
4526 /* The same symbol may appear more than once in the range though.
4527 E.g., if we're looking for symbols that complete "w", and we have
4528 a symbol named "w1::w2", we'll find the two name components for
4529 that same symbol in the range. To be sure we only call the
4530 callback once per symbol, we first collect the symbol name
4531 indexes that matched in a temporary vector and ignore
4532 duplicates. */
4533 std::vector<offset_type> matches;
4534 matches.reserve (std::distance (bounds.first, bounds.second));
4535
4536 for (; bounds.first != bounds.second; ++bounds.first)
4537 {
4538 const char *qualified = index.symbol_name_at (bounds.first->idx);
4539
4540 if (!lookup_name_matcher.matches (qualified)
4541 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4542 continue;
4543
4544 matches.push_back (bounds.first->idx);
4545 }
4546
4547 std::sort (matches.begin (), matches.end ());
4548
4549 /* Finally call the callback, once per match. */
4550 ULONGEST prev = -1;
4551 for (offset_type idx : matches)
4552 {
4553 if (prev != idx)
4554 {
4555 match_callback (idx);
4556 prev = idx;
4557 }
4558 }
4559
4560 /* Above we use a type wider than idx's for 'prev', since 0 and
4561 (offset_type)-1 are both possible values. */
4562 static_assert (sizeof (prev) > sizeof (offset_type), "");
4563 }
4564
4565 #if GDB_SELF_TEST
4566
4567 namespace selftests { namespace dw2_expand_symtabs_matching {
4568
4569 /* A mock .gdb_index/.debug_names-like name index table, enough to
4570 exercise dw2_expand_symtabs_matching_symbol, which works with the
4571 mapped_index_base interface. Builds an index from the symbol list
4572 passed as parameter to the constructor. */
4573 class mock_mapped_index : public mapped_index_base
4574 {
4575 public:
4576 mock_mapped_index (gdb::array_view<const char *> symbols)
4577 : m_symbol_table (symbols)
4578 {}
4579
4580 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4581
4582 /* Return the number of names in the symbol table. */
4583 size_t symbol_name_count () const override
4584 {
4585 return m_symbol_table.size ();
4586 }
4587
4588 /* Get the name of the symbol at IDX in the symbol table. */
4589 const char *symbol_name_at (offset_type idx) const override
4590 {
4591 return m_symbol_table[idx];
4592 }
4593
4594 private:
4595 gdb::array_view<const char *> m_symbol_table;
4596 };
4597
4598 /* Convenience function that converts a NULL pointer to a "<null>"
4599 string, to pass to print routines. */
4600
4601 static const char *
4602 string_or_null (const char *str)
4603 {
4604 return str != NULL ? str : "<null>";
4605 }
4606
4607 /* Check if a lookup_name_info built from
4608 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4609 index. EXPECTED_LIST is the list of expected matches, in expected
4610 matching order. If no match expected, then an empty list is
4611 specified. Returns true on success. On failure prints a warning
4612 indicating the file:line that failed, and returns false. */
4613
4614 static bool
4615 check_match (const char *file, int line,
4616 mock_mapped_index &mock_index,
4617 const char *name, symbol_name_match_type match_type,
4618 bool completion_mode,
4619 std::initializer_list<const char *> expected_list)
4620 {
4621 lookup_name_info lookup_name (name, match_type, completion_mode);
4622
4623 bool matched = true;
4624
4625 auto mismatch = [&] (const char *expected_str,
4626 const char *got)
4627 {
4628 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4629 "expected=\"%s\", got=\"%s\"\n"),
4630 file, line,
4631 (match_type == symbol_name_match_type::FULL
4632 ? "FULL" : "WILD"),
4633 name, string_or_null (expected_str), string_or_null (got));
4634 matched = false;
4635 };
4636
4637 auto expected_it = expected_list.begin ();
4638 auto expected_end = expected_list.end ();
4639
4640 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4641 NULL, ALL_DOMAIN,
4642 [&] (offset_type idx)
4643 {
4644 const char *matched_name = mock_index.symbol_name_at (idx);
4645 const char *expected_str
4646 = expected_it == expected_end ? NULL : *expected_it++;
4647
4648 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4649 mismatch (expected_str, matched_name);
4650 });
4651
4652 const char *expected_str
4653 = expected_it == expected_end ? NULL : *expected_it++;
4654 if (expected_str != NULL)
4655 mismatch (expected_str, NULL);
4656
4657 return matched;
4658 }
4659
4660 /* The symbols added to the mock mapped_index for testing (in
4661 canonical form). */
4662 static const char *test_symbols[] = {
4663 "function",
4664 "std::bar",
4665 "std::zfunction",
4666 "std::zfunction2",
4667 "w1::w2",
4668 "ns::foo<char*>",
4669 "ns::foo<int>",
4670 "ns::foo<long>",
4671 "ns2::tmpl<int>::foo2",
4672 "(anonymous namespace)::A::B::C",
4673
4674 /* These are used to check that the increment-last-char in the
4675 matching algorithm for completion doesn't match "t1_fund" when
4676 completing "t1_func". */
4677 "t1_func",
4678 "t1_func1",
4679 "t1_fund",
4680 "t1_fund1",
4681
4682 /* A UTF-8 name with multi-byte sequences to make sure that
4683 cp-name-parser understands this as a single identifier ("função"
4684 is "function" in PT). */
4685 u8"u8função",
4686
4687 /* \377 (0xff) is Latin1 'ÿ'. */
4688 "yfunc\377",
4689
4690 /* \377 (0xff) is Latin1 'ÿ'. */
4691 "\377",
4692 "\377\377123",
4693
4694 /* A name with all sorts of complications. Starts with "z" to make
4695 it easier for the completion tests below. */
4696 #define Z_SYM_NAME \
4697 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4698 "::tuple<(anonymous namespace)::ui*, " \
4699 "std::default_delete<(anonymous namespace)::ui>, void>"
4700
4701 Z_SYM_NAME
4702 };
4703
4704 /* Returns true if the mapped_index_base::find_name_component_bounds
4705 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4706 in completion mode. */
4707
4708 static bool
4709 check_find_bounds_finds (mapped_index_base &index,
4710 const char *search_name,
4711 gdb::array_view<const char *> expected_syms)
4712 {
4713 lookup_name_info lookup_name (search_name,
4714 symbol_name_match_type::FULL, true);
4715
4716 auto bounds = index.find_name_components_bounds (lookup_name);
4717
4718 size_t distance = std::distance (bounds.first, bounds.second);
4719 if (distance != expected_syms.size ())
4720 return false;
4721
4722 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4723 {
4724 auto nc_elem = bounds.first + exp_elem;
4725 const char *qualified = index.symbol_name_at (nc_elem->idx);
4726 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4727 return false;
4728 }
4729
4730 return true;
4731 }
4732
4733 /* Test the lower-level mapped_index::find_name_component_bounds
4734 method. */
4735
4736 static void
4737 test_mapped_index_find_name_component_bounds ()
4738 {
4739 mock_mapped_index mock_index (test_symbols);
4740
4741 mock_index.build_name_components ();
4742
4743 /* Test the lower-level mapped_index::find_name_component_bounds
4744 method in completion mode. */
4745 {
4746 static const char *expected_syms[] = {
4747 "t1_func",
4748 "t1_func1",
4749 };
4750
4751 SELF_CHECK (check_find_bounds_finds (mock_index,
4752 "t1_func", expected_syms));
4753 }
4754
4755 /* Check that the increment-last-char in the name matching algorithm
4756 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4757 {
4758 static const char *expected_syms1[] = {
4759 "\377",
4760 "\377\377123",
4761 };
4762 SELF_CHECK (check_find_bounds_finds (mock_index,
4763 "\377", expected_syms1));
4764
4765 static const char *expected_syms2[] = {
4766 "\377\377123",
4767 };
4768 SELF_CHECK (check_find_bounds_finds (mock_index,
4769 "\377\377", expected_syms2));
4770 }
4771 }
4772
4773 /* Test dw2_expand_symtabs_matching_symbol. */
4774
4775 static void
4776 test_dw2_expand_symtabs_matching_symbol ()
4777 {
4778 mock_mapped_index mock_index (test_symbols);
4779
4780 /* We let all tests run until the end even if some fails, for debug
4781 convenience. */
4782 bool any_mismatch = false;
4783
4784 /* Create the expected symbols list (an initializer_list). Needed
4785 because lists have commas, and we need to pass them to CHECK,
4786 which is a macro. */
4787 #define EXPECT(...) { __VA_ARGS__ }
4788
4789 /* Wrapper for check_match that passes down the current
4790 __FILE__/__LINE__. */
4791 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4792 any_mismatch |= !check_match (__FILE__, __LINE__, \
4793 mock_index, \
4794 NAME, MATCH_TYPE, COMPLETION_MODE, \
4795 EXPECTED_LIST)
4796
4797 /* Identity checks. */
4798 for (const char *sym : test_symbols)
4799 {
4800 /* Should be able to match all existing symbols. */
4801 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4802 EXPECT (sym));
4803
4804 /* Should be able to match all existing symbols with
4805 parameters. */
4806 std::string with_params = std::string (sym) + "(int)";
4807 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4808 EXPECT (sym));
4809
4810 /* Should be able to match all existing symbols with
4811 parameters and qualifiers. */
4812 with_params = std::string (sym) + " ( int ) const";
4813 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4814 EXPECT (sym));
4815
4816 /* This should really find sym, but cp-name-parser.y doesn't
4817 know about lvalue/rvalue qualifiers yet. */
4818 with_params = std::string (sym) + " ( int ) &&";
4819 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4820 {});
4821 }
4822
4823 /* Check that the name matching algorithm for completion doesn't get
4824 confused with Latin1 'ÿ' / 0xff. */
4825 {
4826 static const char str[] = "\377";
4827 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4828 EXPECT ("\377", "\377\377123"));
4829 }
4830
4831 /* Check that the increment-last-char in the matching algorithm for
4832 completion doesn't match "t1_fund" when completing "t1_func". */
4833 {
4834 static const char str[] = "t1_func";
4835 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4836 EXPECT ("t1_func", "t1_func1"));
4837 }
4838
4839 /* Check that completion mode works at each prefix of the expected
4840 symbol name. */
4841 {
4842 static const char str[] = "function(int)";
4843 size_t len = strlen (str);
4844 std::string lookup;
4845
4846 for (size_t i = 1; i < len; i++)
4847 {
4848 lookup.assign (str, i);
4849 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4850 EXPECT ("function"));
4851 }
4852 }
4853
4854 /* While "w" is a prefix of both components, the match function
4855 should still only be called once. */
4856 {
4857 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4858 EXPECT ("w1::w2"));
4859 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4860 EXPECT ("w1::w2"));
4861 }
4862
4863 /* Same, with a "complicated" symbol. */
4864 {
4865 static const char str[] = Z_SYM_NAME;
4866 size_t len = strlen (str);
4867 std::string lookup;
4868
4869 for (size_t i = 1; i < len; i++)
4870 {
4871 lookup.assign (str, i);
4872 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4873 EXPECT (Z_SYM_NAME));
4874 }
4875 }
4876
4877 /* In FULL mode, an incomplete symbol doesn't match. */
4878 {
4879 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4880 {});
4881 }
4882
4883 /* A complete symbol with parameters matches any overload, since the
4884 index has no overload info. */
4885 {
4886 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4887 EXPECT ("std::zfunction", "std::zfunction2"));
4888 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4889 EXPECT ("std::zfunction", "std::zfunction2"));
4890 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4891 EXPECT ("std::zfunction", "std::zfunction2"));
4892 }
4893
4894 /* Check that whitespace is ignored appropriately. A symbol with a
4895 template argument list. */
4896 {
4897 static const char expected[] = "ns::foo<int>";
4898 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4899 EXPECT (expected));
4900 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4901 EXPECT (expected));
4902 }
4903
4904 /* Check that whitespace is ignored appropriately. A symbol with a
4905 template argument list that includes a pointer. */
4906 {
4907 static const char expected[] = "ns::foo<char*>";
4908 /* Try both completion and non-completion modes. */
4909 static const bool completion_mode[2] = {false, true};
4910 for (size_t i = 0; i < 2; i++)
4911 {
4912 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4913 completion_mode[i], EXPECT (expected));
4914 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4915 completion_mode[i], EXPECT (expected));
4916
4917 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4918 completion_mode[i], EXPECT (expected));
4919 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4920 completion_mode[i], EXPECT (expected));
4921 }
4922 }
4923
4924 {
4925 /* Check method qualifiers are ignored. */
4926 static const char expected[] = "ns::foo<char*>";
4927 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4928 symbol_name_match_type::FULL, true, EXPECT (expected));
4929 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4930 symbol_name_match_type::FULL, true, EXPECT (expected));
4931 CHECK_MATCH ("foo < char * > ( int ) const",
4932 symbol_name_match_type::WILD, true, EXPECT (expected));
4933 CHECK_MATCH ("foo < char * > ( int ) &&",
4934 symbol_name_match_type::WILD, true, EXPECT (expected));
4935 }
4936
4937 /* Test lookup names that don't match anything. */
4938 {
4939 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4940 {});
4941
4942 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4943 {});
4944 }
4945
4946 /* Some wild matching tests, exercising "(anonymous namespace)",
4947 which should not be confused with a parameter list. */
4948 {
4949 static const char *syms[] = {
4950 "A::B::C",
4951 "B::C",
4952 "C",
4953 "A :: B :: C ( int )",
4954 "B :: C ( int )",
4955 "C ( int )",
4956 };
4957
4958 for (const char *s : syms)
4959 {
4960 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4961 EXPECT ("(anonymous namespace)::A::B::C"));
4962 }
4963 }
4964
4965 {
4966 static const char expected[] = "ns2::tmpl<int>::foo2";
4967 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4968 EXPECT (expected));
4969 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4970 EXPECT (expected));
4971 }
4972
4973 SELF_CHECK (!any_mismatch);
4974
4975 #undef EXPECT
4976 #undef CHECK_MATCH
4977 }
4978
4979 static void
4980 run_test ()
4981 {
4982 test_mapped_index_find_name_component_bounds ();
4983 test_dw2_expand_symtabs_matching_symbol ();
4984 }
4985
4986 }} // namespace selftests::dw2_expand_symtabs_matching
4987
4988 #endif /* GDB_SELF_TEST */
4989
4990 /* If FILE_MATCHER is NULL or if PER_CU has
4991 dwarf2_per_cu_quick_data::MARK set (see
4992 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4993 EXPANSION_NOTIFY on it. */
4994
4995 static void
4996 dw2_expand_symtabs_matching_one
4997 (struct dwarf2_per_cu_data *per_cu,
4998 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4999 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5000 {
5001 if (file_matcher == NULL || per_cu->v.quick->mark)
5002 {
5003 bool symtab_was_null
5004 = (per_cu->v.quick->compunit_symtab == NULL);
5005
5006 dw2_instantiate_symtab (per_cu, false);
5007
5008 if (expansion_notify != NULL
5009 && symtab_was_null
5010 && per_cu->v.quick->compunit_symtab != NULL)
5011 expansion_notify (per_cu->v.quick->compunit_symtab);
5012 }
5013 }
5014
5015 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5016 matched, to expand corresponding CUs that were marked. IDX is the
5017 index of the symbol name that matched. */
5018
5019 static void
5020 dw2_expand_marked_cus
5021 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5022 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5023 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5024 search_domain kind)
5025 {
5026 offset_type *vec, vec_len, vec_idx;
5027 bool global_seen = false;
5028 mapped_index &index = *dwarf2_per_objfile->index_table;
5029
5030 vec = (offset_type *) (index.constant_pool
5031 + MAYBE_SWAP (index.symbol_table[idx].vec));
5032 vec_len = MAYBE_SWAP (vec[0]);
5033 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5034 {
5035 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5036 /* This value is only valid for index versions >= 7. */
5037 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5038 gdb_index_symbol_kind symbol_kind =
5039 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5040 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5041 /* Only check the symbol attributes if they're present.
5042 Indices prior to version 7 don't record them,
5043 and indices >= 7 may elide them for certain symbols
5044 (gold does this). */
5045 int attrs_valid =
5046 (index.version >= 7
5047 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5048
5049 /* Work around gold/15646. */
5050 if (attrs_valid)
5051 {
5052 if (!is_static && global_seen)
5053 continue;
5054 if (!is_static)
5055 global_seen = true;
5056 }
5057
5058 /* Only check the symbol's kind if it has one. */
5059 if (attrs_valid)
5060 {
5061 switch (kind)
5062 {
5063 case VARIABLES_DOMAIN:
5064 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5065 continue;
5066 break;
5067 case FUNCTIONS_DOMAIN:
5068 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5069 continue;
5070 break;
5071 case TYPES_DOMAIN:
5072 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5073 continue;
5074 break;
5075 default:
5076 break;
5077 }
5078 }
5079
5080 /* Don't crash on bad data. */
5081 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5082 + dwarf2_per_objfile->all_type_units.size ()))
5083 {
5084 complaint (&symfile_complaints,
5085 _(".gdb_index entry has bad CU index"
5086 " [in module %s]"),
5087 objfile_name (dwarf2_per_objfile->objfile));
5088 continue;
5089 }
5090
5091 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5092 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5093 expansion_notify);
5094 }
5095 }
5096
5097 /* If FILE_MATCHER is non-NULL, set all the
5098 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5099 that match FILE_MATCHER. */
5100
5101 static void
5102 dw_expand_symtabs_matching_file_matcher
5103 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5104 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5105 {
5106 if (file_matcher == NULL)
5107 return;
5108
5109 objfile *const objfile = dwarf2_per_objfile->objfile;
5110
5111 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5112 htab_eq_pointer,
5113 NULL, xcalloc, xfree));
5114 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5115 htab_eq_pointer,
5116 NULL, xcalloc, xfree));
5117
5118 /* The rule is CUs specify all the files, including those used by
5119 any TU, so there's no need to scan TUs here. */
5120
5121 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5122 {
5123 QUIT;
5124
5125 per_cu->v.quick->mark = 0;
5126
5127 /* We only need to look at symtabs not already expanded. */
5128 if (per_cu->v.quick->compunit_symtab)
5129 continue;
5130
5131 quick_file_names *file_data = dw2_get_file_names (per_cu);
5132 if (file_data == NULL)
5133 continue;
5134
5135 if (htab_find (visited_not_found.get (), file_data) != NULL)
5136 continue;
5137 else if (htab_find (visited_found.get (), file_data) != NULL)
5138 {
5139 per_cu->v.quick->mark = 1;
5140 continue;
5141 }
5142
5143 for (int j = 0; j < file_data->num_file_names; ++j)
5144 {
5145 const char *this_real_name;
5146
5147 if (file_matcher (file_data->file_names[j], false))
5148 {
5149 per_cu->v.quick->mark = 1;
5150 break;
5151 }
5152
5153 /* Before we invoke realpath, which can get expensive when many
5154 files are involved, do a quick comparison of the basenames. */
5155 if (!basenames_may_differ
5156 && !file_matcher (lbasename (file_data->file_names[j]),
5157 true))
5158 continue;
5159
5160 this_real_name = dw2_get_real_path (objfile, file_data, j);
5161 if (file_matcher (this_real_name, false))
5162 {
5163 per_cu->v.quick->mark = 1;
5164 break;
5165 }
5166 }
5167
5168 void **slot = htab_find_slot (per_cu->v.quick->mark
5169 ? visited_found.get ()
5170 : visited_not_found.get (),
5171 file_data, INSERT);
5172 *slot = file_data;
5173 }
5174 }
5175
5176 static void
5177 dw2_expand_symtabs_matching
5178 (struct objfile *objfile,
5179 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5180 const lookup_name_info &lookup_name,
5181 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5182 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5183 enum search_domain kind)
5184 {
5185 struct dwarf2_per_objfile *dwarf2_per_objfile
5186 = get_dwarf2_per_objfile (objfile);
5187
5188 /* index_table is NULL if OBJF_READNOW. */
5189 if (!dwarf2_per_objfile->index_table)
5190 return;
5191
5192 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5193
5194 mapped_index &index = *dwarf2_per_objfile->index_table;
5195
5196 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5197 symbol_matcher,
5198 kind, [&] (offset_type idx)
5199 {
5200 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5201 expansion_notify, kind);
5202 });
5203 }
5204
5205 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5206 symtab. */
5207
5208 static struct compunit_symtab *
5209 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5210 CORE_ADDR pc)
5211 {
5212 int i;
5213
5214 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5215 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5216 return cust;
5217
5218 if (cust->includes == NULL)
5219 return NULL;
5220
5221 for (i = 0; cust->includes[i]; ++i)
5222 {
5223 struct compunit_symtab *s = cust->includes[i];
5224
5225 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5226 if (s != NULL)
5227 return s;
5228 }
5229
5230 return NULL;
5231 }
5232
5233 static struct compunit_symtab *
5234 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5235 struct bound_minimal_symbol msymbol,
5236 CORE_ADDR pc,
5237 struct obj_section *section,
5238 int warn_if_readin)
5239 {
5240 struct dwarf2_per_cu_data *data;
5241 struct compunit_symtab *result;
5242
5243 if (!objfile->psymtabs_addrmap)
5244 return NULL;
5245
5246 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5247 pc);
5248 if (!data)
5249 return NULL;
5250
5251 if (warn_if_readin && data->v.quick->compunit_symtab)
5252 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5253 paddress (get_objfile_arch (objfile), pc));
5254
5255 result
5256 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5257 false),
5258 pc);
5259 gdb_assert (result != NULL);
5260 return result;
5261 }
5262
5263 static void
5264 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5265 void *data, int need_fullname)
5266 {
5267 struct dwarf2_per_objfile *dwarf2_per_objfile
5268 = get_dwarf2_per_objfile (objfile);
5269
5270 if (!dwarf2_per_objfile->filenames_cache)
5271 {
5272 dwarf2_per_objfile->filenames_cache.emplace ();
5273
5274 htab_up visited (htab_create_alloc (10,
5275 htab_hash_pointer, htab_eq_pointer,
5276 NULL, xcalloc, xfree));
5277
5278 /* The rule is CUs specify all the files, including those used
5279 by any TU, so there's no need to scan TUs here. We can
5280 ignore file names coming from already-expanded CUs. */
5281
5282 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5283 {
5284 if (per_cu->v.quick->compunit_symtab)
5285 {
5286 void **slot = htab_find_slot (visited.get (),
5287 per_cu->v.quick->file_names,
5288 INSERT);
5289
5290 *slot = per_cu->v.quick->file_names;
5291 }
5292 }
5293
5294 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5295 {
5296 /* We only need to look at symtabs not already expanded. */
5297 if (per_cu->v.quick->compunit_symtab)
5298 continue;
5299
5300 quick_file_names *file_data = dw2_get_file_names (per_cu);
5301 if (file_data == NULL)
5302 continue;
5303
5304 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5305 if (*slot)
5306 {
5307 /* Already visited. */
5308 continue;
5309 }
5310 *slot = file_data;
5311
5312 for (int j = 0; j < file_data->num_file_names; ++j)
5313 {
5314 const char *filename = file_data->file_names[j];
5315 dwarf2_per_objfile->filenames_cache->seen (filename);
5316 }
5317 }
5318 }
5319
5320 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5321 {
5322 gdb::unique_xmalloc_ptr<char> this_real_name;
5323
5324 if (need_fullname)
5325 this_real_name = gdb_realpath (filename);
5326 (*fun) (filename, this_real_name.get (), data);
5327 });
5328 }
5329
5330 static int
5331 dw2_has_symbols (struct objfile *objfile)
5332 {
5333 return 1;
5334 }
5335
5336 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5337 {
5338 dw2_has_symbols,
5339 dw2_find_last_source_symtab,
5340 dw2_forget_cached_source_info,
5341 dw2_map_symtabs_matching_filename,
5342 dw2_lookup_symbol,
5343 dw2_print_stats,
5344 dw2_dump,
5345 dw2_relocate,
5346 dw2_expand_symtabs_for_function,
5347 dw2_expand_all_symtabs,
5348 dw2_expand_symtabs_with_fullname,
5349 dw2_map_matching_symbols,
5350 dw2_expand_symtabs_matching,
5351 dw2_find_pc_sect_compunit_symtab,
5352 NULL,
5353 dw2_map_symbol_filenames
5354 };
5355
5356 /* DWARF-5 debug_names reader. */
5357
5358 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5359 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5360
5361 /* A helper function that reads the .debug_names section in SECTION
5362 and fills in MAP. FILENAME is the name of the file containing the
5363 section; it is used for error reporting.
5364
5365 Returns true if all went well, false otherwise. */
5366
5367 static bool
5368 read_debug_names_from_section (struct objfile *objfile,
5369 const char *filename,
5370 struct dwarf2_section_info *section,
5371 mapped_debug_names &map)
5372 {
5373 if (dwarf2_section_empty_p (section))
5374 return false;
5375
5376 /* Older elfutils strip versions could keep the section in the main
5377 executable while splitting it for the separate debug info file. */
5378 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5379 return false;
5380
5381 dwarf2_read_section (objfile, section);
5382
5383 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5384
5385 const gdb_byte *addr = section->buffer;
5386
5387 bfd *const abfd = get_section_bfd_owner (section);
5388
5389 unsigned int bytes_read;
5390 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5391 addr += bytes_read;
5392
5393 map.dwarf5_is_dwarf64 = bytes_read != 4;
5394 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5395 if (bytes_read + length != section->size)
5396 {
5397 /* There may be multiple per-CU indices. */
5398 warning (_("Section .debug_names in %s length %s does not match "
5399 "section length %s, ignoring .debug_names."),
5400 filename, plongest (bytes_read + length),
5401 pulongest (section->size));
5402 return false;
5403 }
5404
5405 /* The version number. */
5406 uint16_t version = read_2_bytes (abfd, addr);
5407 addr += 2;
5408 if (version != 5)
5409 {
5410 warning (_("Section .debug_names in %s has unsupported version %d, "
5411 "ignoring .debug_names."),
5412 filename, version);
5413 return false;
5414 }
5415
5416 /* Padding. */
5417 uint16_t padding = read_2_bytes (abfd, addr);
5418 addr += 2;
5419 if (padding != 0)
5420 {
5421 warning (_("Section .debug_names in %s has unsupported padding %d, "
5422 "ignoring .debug_names."),
5423 filename, padding);
5424 return false;
5425 }
5426
5427 /* comp_unit_count - The number of CUs in the CU list. */
5428 map.cu_count = read_4_bytes (abfd, addr);
5429 addr += 4;
5430
5431 /* local_type_unit_count - The number of TUs in the local TU
5432 list. */
5433 map.tu_count = read_4_bytes (abfd, addr);
5434 addr += 4;
5435
5436 /* foreign_type_unit_count - The number of TUs in the foreign TU
5437 list. */
5438 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5439 addr += 4;
5440 if (foreign_tu_count != 0)
5441 {
5442 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5443 "ignoring .debug_names."),
5444 filename, static_cast<unsigned long> (foreign_tu_count));
5445 return false;
5446 }
5447
5448 /* bucket_count - The number of hash buckets in the hash lookup
5449 table. */
5450 map.bucket_count = read_4_bytes (abfd, addr);
5451 addr += 4;
5452
5453 /* name_count - The number of unique names in the index. */
5454 map.name_count = read_4_bytes (abfd, addr);
5455 addr += 4;
5456
5457 /* abbrev_table_size - The size in bytes of the abbreviations
5458 table. */
5459 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5460 addr += 4;
5461
5462 /* augmentation_string_size - The size in bytes of the augmentation
5463 string. This value is rounded up to a multiple of 4. */
5464 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5465 addr += 4;
5466 map.augmentation_is_gdb = ((augmentation_string_size
5467 == sizeof (dwarf5_augmentation))
5468 && memcmp (addr, dwarf5_augmentation,
5469 sizeof (dwarf5_augmentation)) == 0);
5470 augmentation_string_size += (-augmentation_string_size) & 3;
5471 addr += augmentation_string_size;
5472
5473 /* List of CUs */
5474 map.cu_table_reordered = addr;
5475 addr += map.cu_count * map.offset_size;
5476
5477 /* List of Local TUs */
5478 map.tu_table_reordered = addr;
5479 addr += map.tu_count * map.offset_size;
5480
5481 /* Hash Lookup Table */
5482 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5483 addr += map.bucket_count * 4;
5484 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5485 addr += map.name_count * 4;
5486
5487 /* Name Table */
5488 map.name_table_string_offs_reordered = addr;
5489 addr += map.name_count * map.offset_size;
5490 map.name_table_entry_offs_reordered = addr;
5491 addr += map.name_count * map.offset_size;
5492
5493 const gdb_byte *abbrev_table_start = addr;
5494 for (;;)
5495 {
5496 unsigned int bytes_read;
5497 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5498 addr += bytes_read;
5499 if (index_num == 0)
5500 break;
5501
5502 const auto insertpair
5503 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5504 if (!insertpair.second)
5505 {
5506 warning (_("Section .debug_names in %s has duplicate index %s, "
5507 "ignoring .debug_names."),
5508 filename, pulongest (index_num));
5509 return false;
5510 }
5511 mapped_debug_names::index_val &indexval = insertpair.first->second;
5512 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5513 addr += bytes_read;
5514
5515 for (;;)
5516 {
5517 mapped_debug_names::index_val::attr attr;
5518 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5519 addr += bytes_read;
5520 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5521 addr += bytes_read;
5522 if (attr.form == DW_FORM_implicit_const)
5523 {
5524 attr.implicit_const = read_signed_leb128 (abfd, addr,
5525 &bytes_read);
5526 addr += bytes_read;
5527 }
5528 if (attr.dw_idx == 0 && attr.form == 0)
5529 break;
5530 indexval.attr_vec.push_back (std::move (attr));
5531 }
5532 }
5533 if (addr != abbrev_table_start + abbrev_table_size)
5534 {
5535 warning (_("Section .debug_names in %s has abbreviation_table "
5536 "of size %zu vs. written as %u, ignoring .debug_names."),
5537 filename, addr - abbrev_table_start, abbrev_table_size);
5538 return false;
5539 }
5540 map.entry_pool = addr;
5541
5542 return true;
5543 }
5544
5545 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5546 list. */
5547
5548 static void
5549 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5550 const mapped_debug_names &map,
5551 dwarf2_section_info &section,
5552 bool is_dwz)
5553 {
5554 sect_offset sect_off_prev;
5555 for (uint32_t i = 0; i <= map.cu_count; ++i)
5556 {
5557 sect_offset sect_off_next;
5558 if (i < map.cu_count)
5559 {
5560 sect_off_next
5561 = (sect_offset) (extract_unsigned_integer
5562 (map.cu_table_reordered + i * map.offset_size,
5563 map.offset_size,
5564 map.dwarf5_byte_order));
5565 }
5566 else
5567 sect_off_next = (sect_offset) section.size;
5568 if (i >= 1)
5569 {
5570 const ULONGEST length = sect_off_next - sect_off_prev;
5571 dwarf2_per_cu_data *per_cu
5572 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5573 sect_off_prev, length);
5574 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5575 }
5576 sect_off_prev = sect_off_next;
5577 }
5578 }
5579
5580 /* Read the CU list from the mapped index, and use it to create all
5581 the CU objects for this dwarf2_per_objfile. */
5582
5583 static void
5584 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5585 const mapped_debug_names &map,
5586 const mapped_debug_names &dwz_map)
5587 {
5588 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5589 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5590
5591 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5592 dwarf2_per_objfile->info,
5593 false /* is_dwz */);
5594
5595 if (dwz_map.cu_count == 0)
5596 return;
5597
5598 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5599 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5600 true /* is_dwz */);
5601 }
5602
5603 /* Read .debug_names. If everything went ok, initialize the "quick"
5604 elements of all the CUs and return true. Otherwise, return false. */
5605
5606 static bool
5607 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5608 {
5609 std::unique_ptr<mapped_debug_names> map
5610 (new mapped_debug_names (dwarf2_per_objfile));
5611 mapped_debug_names dwz_map (dwarf2_per_objfile);
5612 struct objfile *objfile = dwarf2_per_objfile->objfile;
5613
5614 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5615 &dwarf2_per_objfile->debug_names,
5616 *map))
5617 return false;
5618
5619 /* Don't use the index if it's empty. */
5620 if (map->name_count == 0)
5621 return false;
5622
5623 /* If there is a .dwz file, read it so we can get its CU list as
5624 well. */
5625 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5626 if (dwz != NULL)
5627 {
5628 if (!read_debug_names_from_section (objfile,
5629 bfd_get_filename (dwz->dwz_bfd),
5630 &dwz->debug_names, dwz_map))
5631 {
5632 warning (_("could not read '.debug_names' section from %s; skipping"),
5633 bfd_get_filename (dwz->dwz_bfd));
5634 return false;
5635 }
5636 }
5637
5638 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5639
5640 if (map->tu_count != 0)
5641 {
5642 /* We can only handle a single .debug_types when we have an
5643 index. */
5644 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5645 return false;
5646
5647 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5648 dwarf2_per_objfile->types, 0);
5649
5650 create_signatured_type_table_from_debug_names
5651 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5652 }
5653
5654 create_addrmap_from_aranges (dwarf2_per_objfile,
5655 &dwarf2_per_objfile->debug_aranges);
5656
5657 dwarf2_per_objfile->debug_names_table = std::move (map);
5658 dwarf2_per_objfile->using_index = 1;
5659 dwarf2_per_objfile->quick_file_names_table =
5660 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5661
5662 return true;
5663 }
5664
5665 /* Type used to manage iterating over all CUs looking for a symbol for
5666 .debug_names. */
5667
5668 class dw2_debug_names_iterator
5669 {
5670 public:
5671 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5672 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5673 dw2_debug_names_iterator (const mapped_debug_names &map,
5674 bool want_specific_block,
5675 block_enum block_index, domain_enum domain,
5676 const char *name)
5677 : m_map (map), m_want_specific_block (want_specific_block),
5678 m_block_index (block_index), m_domain (domain),
5679 m_addr (find_vec_in_debug_names (map, name))
5680 {}
5681
5682 dw2_debug_names_iterator (const mapped_debug_names &map,
5683 search_domain search, uint32_t namei)
5684 : m_map (map),
5685 m_search (search),
5686 m_addr (find_vec_in_debug_names (map, namei))
5687 {}
5688
5689 /* Return the next matching CU or NULL if there are no more. */
5690 dwarf2_per_cu_data *next ();
5691
5692 private:
5693 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5694 const char *name);
5695 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5696 uint32_t namei);
5697
5698 /* The internalized form of .debug_names. */
5699 const mapped_debug_names &m_map;
5700
5701 /* If true, only look for symbols that match BLOCK_INDEX. */
5702 const bool m_want_specific_block = false;
5703
5704 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5705 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5706 value. */
5707 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5708
5709 /* The kind of symbol we're looking for. */
5710 const domain_enum m_domain = UNDEF_DOMAIN;
5711 const search_domain m_search = ALL_DOMAIN;
5712
5713 /* The list of CUs from the index entry of the symbol, or NULL if
5714 not found. */
5715 const gdb_byte *m_addr;
5716 };
5717
5718 const char *
5719 mapped_debug_names::namei_to_name (uint32_t namei) const
5720 {
5721 const ULONGEST namei_string_offs
5722 = extract_unsigned_integer ((name_table_string_offs_reordered
5723 + namei * offset_size),
5724 offset_size,
5725 dwarf5_byte_order);
5726 return read_indirect_string_at_offset
5727 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5728 }
5729
5730 /* Find a slot in .debug_names for the object named NAME. If NAME is
5731 found, return pointer to its pool data. If NAME cannot be found,
5732 return NULL. */
5733
5734 const gdb_byte *
5735 dw2_debug_names_iterator::find_vec_in_debug_names
5736 (const mapped_debug_names &map, const char *name)
5737 {
5738 int (*cmp) (const char *, const char *);
5739
5740 if (current_language->la_language == language_cplus
5741 || current_language->la_language == language_fortran
5742 || current_language->la_language == language_d)
5743 {
5744 /* NAME is already canonical. Drop any qualifiers as
5745 .debug_names does not contain any. */
5746
5747 if (strchr (name, '(') != NULL)
5748 {
5749 gdb::unique_xmalloc_ptr<char> without_params
5750 = cp_remove_params (name);
5751
5752 if (without_params != NULL)
5753 {
5754 name = without_params.get();
5755 }
5756 }
5757 }
5758
5759 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5760
5761 const uint32_t full_hash = dwarf5_djb_hash (name);
5762 uint32_t namei
5763 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5764 (map.bucket_table_reordered
5765 + (full_hash % map.bucket_count)), 4,
5766 map.dwarf5_byte_order);
5767 if (namei == 0)
5768 return NULL;
5769 --namei;
5770 if (namei >= map.name_count)
5771 {
5772 complaint (&symfile_complaints,
5773 _("Wrong .debug_names with name index %u but name_count=%u "
5774 "[in module %s]"),
5775 namei, map.name_count,
5776 objfile_name (map.dwarf2_per_objfile->objfile));
5777 return NULL;
5778 }
5779
5780 for (;;)
5781 {
5782 const uint32_t namei_full_hash
5783 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5784 (map.hash_table_reordered + namei), 4,
5785 map.dwarf5_byte_order);
5786 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5787 return NULL;
5788
5789 if (full_hash == namei_full_hash)
5790 {
5791 const char *const namei_string = map.namei_to_name (namei);
5792
5793 #if 0 /* An expensive sanity check. */
5794 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5795 {
5796 complaint (&symfile_complaints,
5797 _("Wrong .debug_names hash for string at index %u "
5798 "[in module %s]"),
5799 namei, objfile_name (dwarf2_per_objfile->objfile));
5800 return NULL;
5801 }
5802 #endif
5803
5804 if (cmp (namei_string, name) == 0)
5805 {
5806 const ULONGEST namei_entry_offs
5807 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5808 + namei * map.offset_size),
5809 map.offset_size, map.dwarf5_byte_order);
5810 return map.entry_pool + namei_entry_offs;
5811 }
5812 }
5813
5814 ++namei;
5815 if (namei >= map.name_count)
5816 return NULL;
5817 }
5818 }
5819
5820 const gdb_byte *
5821 dw2_debug_names_iterator::find_vec_in_debug_names
5822 (const mapped_debug_names &map, uint32_t namei)
5823 {
5824 if (namei >= map.name_count)
5825 {
5826 complaint (&symfile_complaints,
5827 _("Wrong .debug_names with name index %u but name_count=%u "
5828 "[in module %s]"),
5829 namei, map.name_count,
5830 objfile_name (map.dwarf2_per_objfile->objfile));
5831 return NULL;
5832 }
5833
5834 const ULONGEST namei_entry_offs
5835 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5836 + namei * map.offset_size),
5837 map.offset_size, map.dwarf5_byte_order);
5838 return map.entry_pool + namei_entry_offs;
5839 }
5840
5841 /* See dw2_debug_names_iterator. */
5842
5843 dwarf2_per_cu_data *
5844 dw2_debug_names_iterator::next ()
5845 {
5846 if (m_addr == NULL)
5847 return NULL;
5848
5849 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5850 struct objfile *objfile = dwarf2_per_objfile->objfile;
5851 bfd *const abfd = objfile->obfd;
5852
5853 again:
5854
5855 unsigned int bytes_read;
5856 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5857 m_addr += bytes_read;
5858 if (abbrev == 0)
5859 return NULL;
5860
5861 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5862 if (indexval_it == m_map.abbrev_map.cend ())
5863 {
5864 complaint (&symfile_complaints,
5865 _("Wrong .debug_names undefined abbrev code %s "
5866 "[in module %s]"),
5867 pulongest (abbrev), objfile_name (objfile));
5868 return NULL;
5869 }
5870 const mapped_debug_names::index_val &indexval = indexval_it->second;
5871 bool have_is_static = false;
5872 bool is_static;
5873 dwarf2_per_cu_data *per_cu = NULL;
5874 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5875 {
5876 ULONGEST ull;
5877 switch (attr.form)
5878 {
5879 case DW_FORM_implicit_const:
5880 ull = attr.implicit_const;
5881 break;
5882 case DW_FORM_flag_present:
5883 ull = 1;
5884 break;
5885 case DW_FORM_udata:
5886 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5887 m_addr += bytes_read;
5888 break;
5889 default:
5890 complaint (&symfile_complaints,
5891 _("Unsupported .debug_names form %s [in module %s]"),
5892 dwarf_form_name (attr.form),
5893 objfile_name (objfile));
5894 return NULL;
5895 }
5896 switch (attr.dw_idx)
5897 {
5898 case DW_IDX_compile_unit:
5899 /* Don't crash on bad data. */
5900 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5901 {
5902 complaint (&symfile_complaints,
5903 _(".debug_names entry has bad CU index %s"
5904 " [in module %s]"),
5905 pulongest (ull),
5906 objfile_name (dwarf2_per_objfile->objfile));
5907 continue;
5908 }
5909 per_cu = dwarf2_per_objfile->get_cutu (ull);
5910 break;
5911 case DW_IDX_type_unit:
5912 /* Don't crash on bad data. */
5913 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5914 {
5915 complaint (&symfile_complaints,
5916 _(".debug_names entry has bad TU index %s"
5917 " [in module %s]"),
5918 pulongest (ull),
5919 objfile_name (dwarf2_per_objfile->objfile));
5920 continue;
5921 }
5922 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5923 break;
5924 case DW_IDX_GNU_internal:
5925 if (!m_map.augmentation_is_gdb)
5926 break;
5927 have_is_static = true;
5928 is_static = true;
5929 break;
5930 case DW_IDX_GNU_external:
5931 if (!m_map.augmentation_is_gdb)
5932 break;
5933 have_is_static = true;
5934 is_static = false;
5935 break;
5936 }
5937 }
5938
5939 /* Skip if already read in. */
5940 if (per_cu->v.quick->compunit_symtab)
5941 goto again;
5942
5943 /* Check static vs global. */
5944 if (have_is_static)
5945 {
5946 const bool want_static = m_block_index != GLOBAL_BLOCK;
5947 if (m_want_specific_block && want_static != is_static)
5948 goto again;
5949 }
5950
5951 /* Match dw2_symtab_iter_next, symbol_kind
5952 and debug_names::psymbol_tag. */
5953 switch (m_domain)
5954 {
5955 case VAR_DOMAIN:
5956 switch (indexval.dwarf_tag)
5957 {
5958 case DW_TAG_variable:
5959 case DW_TAG_subprogram:
5960 /* Some types are also in VAR_DOMAIN. */
5961 case DW_TAG_typedef:
5962 case DW_TAG_structure_type:
5963 break;
5964 default:
5965 goto again;
5966 }
5967 break;
5968 case STRUCT_DOMAIN:
5969 switch (indexval.dwarf_tag)
5970 {
5971 case DW_TAG_typedef:
5972 case DW_TAG_structure_type:
5973 break;
5974 default:
5975 goto again;
5976 }
5977 break;
5978 case LABEL_DOMAIN:
5979 switch (indexval.dwarf_tag)
5980 {
5981 case 0:
5982 case DW_TAG_variable:
5983 break;
5984 default:
5985 goto again;
5986 }
5987 break;
5988 default:
5989 break;
5990 }
5991
5992 /* Match dw2_expand_symtabs_matching, symbol_kind and
5993 debug_names::psymbol_tag. */
5994 switch (m_search)
5995 {
5996 case VARIABLES_DOMAIN:
5997 switch (indexval.dwarf_tag)
5998 {
5999 case DW_TAG_variable:
6000 break;
6001 default:
6002 goto again;
6003 }
6004 break;
6005 case FUNCTIONS_DOMAIN:
6006 switch (indexval.dwarf_tag)
6007 {
6008 case DW_TAG_subprogram:
6009 break;
6010 default:
6011 goto again;
6012 }
6013 break;
6014 case TYPES_DOMAIN:
6015 switch (indexval.dwarf_tag)
6016 {
6017 case DW_TAG_typedef:
6018 case DW_TAG_structure_type:
6019 break;
6020 default:
6021 goto again;
6022 }
6023 break;
6024 default:
6025 break;
6026 }
6027
6028 return per_cu;
6029 }
6030
6031 static struct compunit_symtab *
6032 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6033 const char *name, domain_enum domain)
6034 {
6035 const block_enum block_index = static_cast<block_enum> (block_index_int);
6036 struct dwarf2_per_objfile *dwarf2_per_objfile
6037 = get_dwarf2_per_objfile (objfile);
6038
6039 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6040 if (!mapp)
6041 {
6042 /* index is NULL if OBJF_READNOW. */
6043 return NULL;
6044 }
6045 const auto &map = *mapp;
6046
6047 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6048 block_index, domain, name);
6049
6050 struct compunit_symtab *stab_best = NULL;
6051 struct dwarf2_per_cu_data *per_cu;
6052 while ((per_cu = iter.next ()) != NULL)
6053 {
6054 struct symbol *sym, *with_opaque = NULL;
6055 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6056 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6057 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6058
6059 sym = block_find_symbol (block, name, domain,
6060 block_find_non_opaque_type_preferred,
6061 &with_opaque);
6062
6063 /* Some caution must be observed with overloaded functions and
6064 methods, since the index will not contain any overload
6065 information (but NAME might contain it). */
6066
6067 if (sym != NULL
6068 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6069 return stab;
6070 if (with_opaque != NULL
6071 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6072 stab_best = stab;
6073
6074 /* Keep looking through other CUs. */
6075 }
6076
6077 return stab_best;
6078 }
6079
6080 /* This dumps minimal information about .debug_names. It is called
6081 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6082 uses this to verify that .debug_names has been loaded. */
6083
6084 static void
6085 dw2_debug_names_dump (struct objfile *objfile)
6086 {
6087 struct dwarf2_per_objfile *dwarf2_per_objfile
6088 = get_dwarf2_per_objfile (objfile);
6089
6090 gdb_assert (dwarf2_per_objfile->using_index);
6091 printf_filtered (".debug_names:");
6092 if (dwarf2_per_objfile->debug_names_table)
6093 printf_filtered (" exists\n");
6094 else
6095 printf_filtered (" faked for \"readnow\"\n");
6096 printf_filtered ("\n");
6097 }
6098
6099 static void
6100 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6101 const char *func_name)
6102 {
6103 struct dwarf2_per_objfile *dwarf2_per_objfile
6104 = get_dwarf2_per_objfile (objfile);
6105
6106 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6107 if (dwarf2_per_objfile->debug_names_table)
6108 {
6109 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6110
6111 /* Note: It doesn't matter what we pass for block_index here. */
6112 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6113 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6114
6115 struct dwarf2_per_cu_data *per_cu;
6116 while ((per_cu = iter.next ()) != NULL)
6117 dw2_instantiate_symtab (per_cu, false);
6118 }
6119 }
6120
6121 static void
6122 dw2_debug_names_expand_symtabs_matching
6123 (struct objfile *objfile,
6124 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6125 const lookup_name_info &lookup_name,
6126 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6127 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6128 enum search_domain kind)
6129 {
6130 struct dwarf2_per_objfile *dwarf2_per_objfile
6131 = get_dwarf2_per_objfile (objfile);
6132
6133 /* debug_names_table is NULL if OBJF_READNOW. */
6134 if (!dwarf2_per_objfile->debug_names_table)
6135 return;
6136
6137 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6138
6139 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6140
6141 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6142 symbol_matcher,
6143 kind, [&] (offset_type namei)
6144 {
6145 /* The name was matched, now expand corresponding CUs that were
6146 marked. */
6147 dw2_debug_names_iterator iter (map, kind, namei);
6148
6149 struct dwarf2_per_cu_data *per_cu;
6150 while ((per_cu = iter.next ()) != NULL)
6151 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6152 expansion_notify);
6153 });
6154 }
6155
6156 const struct quick_symbol_functions dwarf2_debug_names_functions =
6157 {
6158 dw2_has_symbols,
6159 dw2_find_last_source_symtab,
6160 dw2_forget_cached_source_info,
6161 dw2_map_symtabs_matching_filename,
6162 dw2_debug_names_lookup_symbol,
6163 dw2_print_stats,
6164 dw2_debug_names_dump,
6165 dw2_relocate,
6166 dw2_debug_names_expand_symtabs_for_function,
6167 dw2_expand_all_symtabs,
6168 dw2_expand_symtabs_with_fullname,
6169 dw2_map_matching_symbols,
6170 dw2_debug_names_expand_symtabs_matching,
6171 dw2_find_pc_sect_compunit_symtab,
6172 NULL,
6173 dw2_map_symbol_filenames
6174 };
6175
6176 /* See symfile.h. */
6177
6178 bool
6179 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6180 {
6181 struct dwarf2_per_objfile *dwarf2_per_objfile
6182 = get_dwarf2_per_objfile (objfile);
6183
6184 /* If we're about to read full symbols, don't bother with the
6185 indices. In this case we also don't care if some other debug
6186 format is making psymtabs, because they are all about to be
6187 expanded anyway. */
6188 if ((objfile->flags & OBJF_READNOW))
6189 {
6190 dwarf2_per_objfile->using_index = 1;
6191 create_all_comp_units (dwarf2_per_objfile);
6192 create_all_type_units (dwarf2_per_objfile);
6193 dwarf2_per_objfile->quick_file_names_table
6194 = create_quick_file_names_table
6195 (dwarf2_per_objfile->all_comp_units.size ());
6196
6197 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6198 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6199 {
6200 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6201
6202 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6203 struct dwarf2_per_cu_quick_data);
6204 }
6205
6206 /* Return 1 so that gdb sees the "quick" functions. However,
6207 these functions will be no-ops because we will have expanded
6208 all symtabs. */
6209 *index_kind = dw_index_kind::GDB_INDEX;
6210 return true;
6211 }
6212
6213 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6214 {
6215 *index_kind = dw_index_kind::DEBUG_NAMES;
6216 return true;
6217 }
6218
6219 if (dwarf2_read_index (dwarf2_per_objfile))
6220 {
6221 *index_kind = dw_index_kind::GDB_INDEX;
6222 return true;
6223 }
6224
6225 return false;
6226 }
6227
6228 \f
6229
6230 /* Build a partial symbol table. */
6231
6232 void
6233 dwarf2_build_psymtabs (struct objfile *objfile)
6234 {
6235 struct dwarf2_per_objfile *dwarf2_per_objfile
6236 = get_dwarf2_per_objfile (objfile);
6237
6238 if (objfile->global_psymbols.capacity () == 0
6239 && objfile->static_psymbols.capacity () == 0)
6240 init_psymbol_list (objfile, 1024);
6241
6242 TRY
6243 {
6244 /* This isn't really ideal: all the data we allocate on the
6245 objfile's obstack is still uselessly kept around. However,
6246 freeing it seems unsafe. */
6247 psymtab_discarder psymtabs (objfile);
6248 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6249 psymtabs.keep ();
6250 }
6251 CATCH (except, RETURN_MASK_ERROR)
6252 {
6253 exception_print (gdb_stderr, except);
6254 }
6255 END_CATCH
6256 }
6257
6258 /* Return the total length of the CU described by HEADER. */
6259
6260 static unsigned int
6261 get_cu_length (const struct comp_unit_head *header)
6262 {
6263 return header->initial_length_size + header->length;
6264 }
6265
6266 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6267
6268 static inline bool
6269 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6270 {
6271 sect_offset bottom = cu_header->sect_off;
6272 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6273
6274 return sect_off >= bottom && sect_off < top;
6275 }
6276
6277 /* Find the base address of the compilation unit for range lists and
6278 location lists. It will normally be specified by DW_AT_low_pc.
6279 In DWARF-3 draft 4, the base address could be overridden by
6280 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6281 compilation units with discontinuous ranges. */
6282
6283 static void
6284 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6285 {
6286 struct attribute *attr;
6287
6288 cu->base_known = 0;
6289 cu->base_address = 0;
6290
6291 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6292 if (attr)
6293 {
6294 cu->base_address = attr_value_as_address (attr);
6295 cu->base_known = 1;
6296 }
6297 else
6298 {
6299 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6300 if (attr)
6301 {
6302 cu->base_address = attr_value_as_address (attr);
6303 cu->base_known = 1;
6304 }
6305 }
6306 }
6307
6308 /* Read in the comp unit header information from the debug_info at info_ptr.
6309 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6310 NOTE: This leaves members offset, first_die_offset to be filled in
6311 by the caller. */
6312
6313 static const gdb_byte *
6314 read_comp_unit_head (struct comp_unit_head *cu_header,
6315 const gdb_byte *info_ptr,
6316 struct dwarf2_section_info *section,
6317 rcuh_kind section_kind)
6318 {
6319 int signed_addr;
6320 unsigned int bytes_read;
6321 const char *filename = get_section_file_name (section);
6322 bfd *abfd = get_section_bfd_owner (section);
6323
6324 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6325 cu_header->initial_length_size = bytes_read;
6326 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6327 info_ptr += bytes_read;
6328 cu_header->version = read_2_bytes (abfd, info_ptr);
6329 info_ptr += 2;
6330 if (cu_header->version < 5)
6331 switch (section_kind)
6332 {
6333 case rcuh_kind::COMPILE:
6334 cu_header->unit_type = DW_UT_compile;
6335 break;
6336 case rcuh_kind::TYPE:
6337 cu_header->unit_type = DW_UT_type;
6338 break;
6339 default:
6340 internal_error (__FILE__, __LINE__,
6341 _("read_comp_unit_head: invalid section_kind"));
6342 }
6343 else
6344 {
6345 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6346 (read_1_byte (abfd, info_ptr));
6347 info_ptr += 1;
6348 switch (cu_header->unit_type)
6349 {
6350 case DW_UT_compile:
6351 if (section_kind != rcuh_kind::COMPILE)
6352 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6353 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6354 filename);
6355 break;
6356 case DW_UT_type:
6357 section_kind = rcuh_kind::TYPE;
6358 break;
6359 default:
6360 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6361 "(is %d, should be %d or %d) [in module %s]"),
6362 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6363 }
6364
6365 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6366 info_ptr += 1;
6367 }
6368 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6369 cu_header,
6370 &bytes_read);
6371 info_ptr += bytes_read;
6372 if (cu_header->version < 5)
6373 {
6374 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6375 info_ptr += 1;
6376 }
6377 signed_addr = bfd_get_sign_extend_vma (abfd);
6378 if (signed_addr < 0)
6379 internal_error (__FILE__, __LINE__,
6380 _("read_comp_unit_head: dwarf from non elf file"));
6381 cu_header->signed_addr_p = signed_addr;
6382
6383 if (section_kind == rcuh_kind::TYPE)
6384 {
6385 LONGEST type_offset;
6386
6387 cu_header->signature = read_8_bytes (abfd, info_ptr);
6388 info_ptr += 8;
6389
6390 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6391 info_ptr += bytes_read;
6392 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6393 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6394 error (_("Dwarf Error: Too big type_offset in compilation unit "
6395 "header (is %s) [in module %s]"), plongest (type_offset),
6396 filename);
6397 }
6398
6399 return info_ptr;
6400 }
6401
6402 /* Helper function that returns the proper abbrev section for
6403 THIS_CU. */
6404
6405 static struct dwarf2_section_info *
6406 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6407 {
6408 struct dwarf2_section_info *abbrev;
6409 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6410
6411 if (this_cu->is_dwz)
6412 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6413 else
6414 abbrev = &dwarf2_per_objfile->abbrev;
6415
6416 return abbrev;
6417 }
6418
6419 /* Subroutine of read_and_check_comp_unit_head and
6420 read_and_check_type_unit_head to simplify them.
6421 Perform various error checking on the header. */
6422
6423 static void
6424 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6425 struct comp_unit_head *header,
6426 struct dwarf2_section_info *section,
6427 struct dwarf2_section_info *abbrev_section)
6428 {
6429 const char *filename = get_section_file_name (section);
6430
6431 if (header->version < 2 || header->version > 5)
6432 error (_("Dwarf Error: wrong version in compilation unit header "
6433 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6434 filename);
6435
6436 if (to_underlying (header->abbrev_sect_off)
6437 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6438 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6439 "(offset %s + 6) [in module %s]"),
6440 sect_offset_str (header->abbrev_sect_off),
6441 sect_offset_str (header->sect_off),
6442 filename);
6443
6444 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6445 avoid potential 32-bit overflow. */
6446 if (((ULONGEST) header->sect_off + get_cu_length (header))
6447 > section->size)
6448 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6449 "(offset %s + 0) [in module %s]"),
6450 header->length, sect_offset_str (header->sect_off),
6451 filename);
6452 }
6453
6454 /* Read in a CU/TU header and perform some basic error checking.
6455 The contents of the header are stored in HEADER.
6456 The result is a pointer to the start of the first DIE. */
6457
6458 static const gdb_byte *
6459 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6460 struct comp_unit_head *header,
6461 struct dwarf2_section_info *section,
6462 struct dwarf2_section_info *abbrev_section,
6463 const gdb_byte *info_ptr,
6464 rcuh_kind section_kind)
6465 {
6466 const gdb_byte *beg_of_comp_unit = info_ptr;
6467
6468 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6469
6470 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6471
6472 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6473
6474 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6475 abbrev_section);
6476
6477 return info_ptr;
6478 }
6479
6480 /* Fetch the abbreviation table offset from a comp or type unit header. */
6481
6482 static sect_offset
6483 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6484 struct dwarf2_section_info *section,
6485 sect_offset sect_off)
6486 {
6487 bfd *abfd = get_section_bfd_owner (section);
6488 const gdb_byte *info_ptr;
6489 unsigned int initial_length_size, offset_size;
6490 uint16_t version;
6491
6492 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6493 info_ptr = section->buffer + to_underlying (sect_off);
6494 read_initial_length (abfd, info_ptr, &initial_length_size);
6495 offset_size = initial_length_size == 4 ? 4 : 8;
6496 info_ptr += initial_length_size;
6497
6498 version = read_2_bytes (abfd, info_ptr);
6499 info_ptr += 2;
6500 if (version >= 5)
6501 {
6502 /* Skip unit type and address size. */
6503 info_ptr += 2;
6504 }
6505
6506 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6507 }
6508
6509 /* Allocate a new partial symtab for file named NAME and mark this new
6510 partial symtab as being an include of PST. */
6511
6512 static void
6513 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6514 struct objfile *objfile)
6515 {
6516 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6517
6518 if (!IS_ABSOLUTE_PATH (subpst->filename))
6519 {
6520 /* It shares objfile->objfile_obstack. */
6521 subpst->dirname = pst->dirname;
6522 }
6523
6524 subpst->textlow = 0;
6525 subpst->texthigh = 0;
6526
6527 subpst->dependencies
6528 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6529 subpst->dependencies[0] = pst;
6530 subpst->number_of_dependencies = 1;
6531
6532 subpst->globals_offset = 0;
6533 subpst->n_global_syms = 0;
6534 subpst->statics_offset = 0;
6535 subpst->n_static_syms = 0;
6536 subpst->compunit_symtab = NULL;
6537 subpst->read_symtab = pst->read_symtab;
6538 subpst->readin = 0;
6539
6540 /* No private part is necessary for include psymtabs. This property
6541 can be used to differentiate between such include psymtabs and
6542 the regular ones. */
6543 subpst->read_symtab_private = NULL;
6544 }
6545
6546 /* Read the Line Number Program data and extract the list of files
6547 included by the source file represented by PST. Build an include
6548 partial symtab for each of these included files. */
6549
6550 static void
6551 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6552 struct die_info *die,
6553 struct partial_symtab *pst)
6554 {
6555 line_header_up lh;
6556 struct attribute *attr;
6557
6558 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6559 if (attr)
6560 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6561 if (lh == NULL)
6562 return; /* No linetable, so no includes. */
6563
6564 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6565 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6566 }
6567
6568 static hashval_t
6569 hash_signatured_type (const void *item)
6570 {
6571 const struct signatured_type *sig_type
6572 = (const struct signatured_type *) item;
6573
6574 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6575 return sig_type->signature;
6576 }
6577
6578 static int
6579 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6580 {
6581 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6582 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6583
6584 return lhs->signature == rhs->signature;
6585 }
6586
6587 /* Allocate a hash table for signatured types. */
6588
6589 static htab_t
6590 allocate_signatured_type_table (struct objfile *objfile)
6591 {
6592 return htab_create_alloc_ex (41,
6593 hash_signatured_type,
6594 eq_signatured_type,
6595 NULL,
6596 &objfile->objfile_obstack,
6597 hashtab_obstack_allocate,
6598 dummy_obstack_deallocate);
6599 }
6600
6601 /* A helper function to add a signatured type CU to a table. */
6602
6603 static int
6604 add_signatured_type_cu_to_table (void **slot, void *datum)
6605 {
6606 struct signatured_type *sigt = (struct signatured_type *) *slot;
6607 std::vector<signatured_type *> *all_type_units
6608 = (std::vector<signatured_type *> *) datum;
6609
6610 all_type_units->push_back (sigt);
6611
6612 return 1;
6613 }
6614
6615 /* A helper for create_debug_types_hash_table. Read types from SECTION
6616 and fill them into TYPES_HTAB. It will process only type units,
6617 therefore DW_UT_type. */
6618
6619 static void
6620 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6621 struct dwo_file *dwo_file,
6622 dwarf2_section_info *section, htab_t &types_htab,
6623 rcuh_kind section_kind)
6624 {
6625 struct objfile *objfile = dwarf2_per_objfile->objfile;
6626 struct dwarf2_section_info *abbrev_section;
6627 bfd *abfd;
6628 const gdb_byte *info_ptr, *end_ptr;
6629
6630 abbrev_section = (dwo_file != NULL
6631 ? &dwo_file->sections.abbrev
6632 : &dwarf2_per_objfile->abbrev);
6633
6634 if (dwarf_read_debug)
6635 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6636 get_section_name (section),
6637 get_section_file_name (abbrev_section));
6638
6639 dwarf2_read_section (objfile, section);
6640 info_ptr = section->buffer;
6641
6642 if (info_ptr == NULL)
6643 return;
6644
6645 /* We can't set abfd until now because the section may be empty or
6646 not present, in which case the bfd is unknown. */
6647 abfd = get_section_bfd_owner (section);
6648
6649 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6650 because we don't need to read any dies: the signature is in the
6651 header. */
6652
6653 end_ptr = info_ptr + section->size;
6654 while (info_ptr < end_ptr)
6655 {
6656 struct signatured_type *sig_type;
6657 struct dwo_unit *dwo_tu;
6658 void **slot;
6659 const gdb_byte *ptr = info_ptr;
6660 struct comp_unit_head header;
6661 unsigned int length;
6662
6663 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6664
6665 /* Initialize it due to a false compiler warning. */
6666 header.signature = -1;
6667 header.type_cu_offset_in_tu = (cu_offset) -1;
6668
6669 /* We need to read the type's signature in order to build the hash
6670 table, but we don't need anything else just yet. */
6671
6672 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6673 abbrev_section, ptr, section_kind);
6674
6675 length = get_cu_length (&header);
6676
6677 /* Skip dummy type units. */
6678 if (ptr >= info_ptr + length
6679 || peek_abbrev_code (abfd, ptr) == 0
6680 || header.unit_type != DW_UT_type)
6681 {
6682 info_ptr += length;
6683 continue;
6684 }
6685
6686 if (types_htab == NULL)
6687 {
6688 if (dwo_file)
6689 types_htab = allocate_dwo_unit_table (objfile);
6690 else
6691 types_htab = allocate_signatured_type_table (objfile);
6692 }
6693
6694 if (dwo_file)
6695 {
6696 sig_type = NULL;
6697 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6698 struct dwo_unit);
6699 dwo_tu->dwo_file = dwo_file;
6700 dwo_tu->signature = header.signature;
6701 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6702 dwo_tu->section = section;
6703 dwo_tu->sect_off = sect_off;
6704 dwo_tu->length = length;
6705 }
6706 else
6707 {
6708 /* N.B.: type_offset is not usable if this type uses a DWO file.
6709 The real type_offset is in the DWO file. */
6710 dwo_tu = NULL;
6711 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6712 struct signatured_type);
6713 sig_type->signature = header.signature;
6714 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6715 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6716 sig_type->per_cu.is_debug_types = 1;
6717 sig_type->per_cu.section = section;
6718 sig_type->per_cu.sect_off = sect_off;
6719 sig_type->per_cu.length = length;
6720 }
6721
6722 slot = htab_find_slot (types_htab,
6723 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6724 INSERT);
6725 gdb_assert (slot != NULL);
6726 if (*slot != NULL)
6727 {
6728 sect_offset dup_sect_off;
6729
6730 if (dwo_file)
6731 {
6732 const struct dwo_unit *dup_tu
6733 = (const struct dwo_unit *) *slot;
6734
6735 dup_sect_off = dup_tu->sect_off;
6736 }
6737 else
6738 {
6739 const struct signatured_type *dup_tu
6740 = (const struct signatured_type *) *slot;
6741
6742 dup_sect_off = dup_tu->per_cu.sect_off;
6743 }
6744
6745 complaint (&symfile_complaints,
6746 _("debug type entry at offset %s is duplicate to"
6747 " the entry at offset %s, signature %s"),
6748 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6749 hex_string (header.signature));
6750 }
6751 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6752
6753 if (dwarf_read_debug > 1)
6754 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6755 sect_offset_str (sect_off),
6756 hex_string (header.signature));
6757
6758 info_ptr += length;
6759 }
6760 }
6761
6762 /* Create the hash table of all entries in the .debug_types
6763 (or .debug_types.dwo) section(s).
6764 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6765 otherwise it is NULL.
6766
6767 The result is a pointer to the hash table or NULL if there are no types.
6768
6769 Note: This function processes DWO files only, not DWP files. */
6770
6771 static void
6772 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6773 struct dwo_file *dwo_file,
6774 VEC (dwarf2_section_info_def) *types,
6775 htab_t &types_htab)
6776 {
6777 int ix;
6778 struct dwarf2_section_info *section;
6779
6780 if (VEC_empty (dwarf2_section_info_def, types))
6781 return;
6782
6783 for (ix = 0;
6784 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6785 ++ix)
6786 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6787 types_htab, rcuh_kind::TYPE);
6788 }
6789
6790 /* Create the hash table of all entries in the .debug_types section,
6791 and initialize all_type_units.
6792 The result is zero if there is an error (e.g. missing .debug_types section),
6793 otherwise non-zero. */
6794
6795 static int
6796 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6797 {
6798 htab_t types_htab = NULL;
6799
6800 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6801 &dwarf2_per_objfile->info, types_htab,
6802 rcuh_kind::COMPILE);
6803 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6804 dwarf2_per_objfile->types, types_htab);
6805 if (types_htab == NULL)
6806 {
6807 dwarf2_per_objfile->signatured_types = NULL;
6808 return 0;
6809 }
6810
6811 dwarf2_per_objfile->signatured_types = types_htab;
6812
6813 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6814 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6815
6816 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6817 &dwarf2_per_objfile->all_type_units);
6818
6819 return 1;
6820 }
6821
6822 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6823 If SLOT is non-NULL, it is the entry to use in the hash table.
6824 Otherwise we find one. */
6825
6826 static struct signatured_type *
6827 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6828 void **slot)
6829 {
6830 struct objfile *objfile = dwarf2_per_objfile->objfile;
6831
6832 if (dwarf2_per_objfile->all_type_units.size ()
6833 == dwarf2_per_objfile->all_type_units.capacity ())
6834 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6835
6836 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6837 struct signatured_type);
6838
6839 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6840 sig_type->signature = sig;
6841 sig_type->per_cu.is_debug_types = 1;
6842 if (dwarf2_per_objfile->using_index)
6843 {
6844 sig_type->per_cu.v.quick =
6845 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6846 struct dwarf2_per_cu_quick_data);
6847 }
6848
6849 if (slot == NULL)
6850 {
6851 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6852 sig_type, INSERT);
6853 }
6854 gdb_assert (*slot == NULL);
6855 *slot = sig_type;
6856 /* The rest of sig_type must be filled in by the caller. */
6857 return sig_type;
6858 }
6859
6860 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6861 Fill in SIG_ENTRY with DWO_ENTRY. */
6862
6863 static void
6864 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6865 struct signatured_type *sig_entry,
6866 struct dwo_unit *dwo_entry)
6867 {
6868 /* Make sure we're not clobbering something we don't expect to. */
6869 gdb_assert (! sig_entry->per_cu.queued);
6870 gdb_assert (sig_entry->per_cu.cu == NULL);
6871 if (dwarf2_per_objfile->using_index)
6872 {
6873 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6874 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6875 }
6876 else
6877 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6878 gdb_assert (sig_entry->signature == dwo_entry->signature);
6879 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6880 gdb_assert (sig_entry->type_unit_group == NULL);
6881 gdb_assert (sig_entry->dwo_unit == NULL);
6882
6883 sig_entry->per_cu.section = dwo_entry->section;
6884 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6885 sig_entry->per_cu.length = dwo_entry->length;
6886 sig_entry->per_cu.reading_dwo_directly = 1;
6887 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6888 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6889 sig_entry->dwo_unit = dwo_entry;
6890 }
6891
6892 /* Subroutine of lookup_signatured_type.
6893 If we haven't read the TU yet, create the signatured_type data structure
6894 for a TU to be read in directly from a DWO file, bypassing the stub.
6895 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6896 using .gdb_index, then when reading a CU we want to stay in the DWO file
6897 containing that CU. Otherwise we could end up reading several other DWO
6898 files (due to comdat folding) to process the transitive closure of all the
6899 mentioned TUs, and that can be slow. The current DWO file will have every
6900 type signature that it needs.
6901 We only do this for .gdb_index because in the psymtab case we already have
6902 to read all the DWOs to build the type unit groups. */
6903
6904 static struct signatured_type *
6905 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6906 {
6907 struct dwarf2_per_objfile *dwarf2_per_objfile
6908 = cu->per_cu->dwarf2_per_objfile;
6909 struct objfile *objfile = dwarf2_per_objfile->objfile;
6910 struct dwo_file *dwo_file;
6911 struct dwo_unit find_dwo_entry, *dwo_entry;
6912 struct signatured_type find_sig_entry, *sig_entry;
6913 void **slot;
6914
6915 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6916
6917 /* If TU skeletons have been removed then we may not have read in any
6918 TUs yet. */
6919 if (dwarf2_per_objfile->signatured_types == NULL)
6920 {
6921 dwarf2_per_objfile->signatured_types
6922 = allocate_signatured_type_table (objfile);
6923 }
6924
6925 /* We only ever need to read in one copy of a signatured type.
6926 Use the global signatured_types array to do our own comdat-folding
6927 of types. If this is the first time we're reading this TU, and
6928 the TU has an entry in .gdb_index, replace the recorded data from
6929 .gdb_index with this TU. */
6930
6931 find_sig_entry.signature = sig;
6932 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6933 &find_sig_entry, INSERT);
6934 sig_entry = (struct signatured_type *) *slot;
6935
6936 /* We can get here with the TU already read, *or* in the process of being
6937 read. Don't reassign the global entry to point to this DWO if that's
6938 the case. Also note that if the TU is already being read, it may not
6939 have come from a DWO, the program may be a mix of Fission-compiled
6940 code and non-Fission-compiled code. */
6941
6942 /* Have we already tried to read this TU?
6943 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6944 needn't exist in the global table yet). */
6945 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6946 return sig_entry;
6947
6948 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6949 dwo_unit of the TU itself. */
6950 dwo_file = cu->dwo_unit->dwo_file;
6951
6952 /* Ok, this is the first time we're reading this TU. */
6953 if (dwo_file->tus == NULL)
6954 return NULL;
6955 find_dwo_entry.signature = sig;
6956 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
6957 if (dwo_entry == NULL)
6958 return NULL;
6959
6960 /* If the global table doesn't have an entry for this TU, add one. */
6961 if (sig_entry == NULL)
6962 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6963
6964 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6965 sig_entry->per_cu.tu_read = 1;
6966 return sig_entry;
6967 }
6968
6969 /* Subroutine of lookup_signatured_type.
6970 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6971 then try the DWP file. If the TU stub (skeleton) has been removed then
6972 it won't be in .gdb_index. */
6973
6974 static struct signatured_type *
6975 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6976 {
6977 struct dwarf2_per_objfile *dwarf2_per_objfile
6978 = cu->per_cu->dwarf2_per_objfile;
6979 struct objfile *objfile = dwarf2_per_objfile->objfile;
6980 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6981 struct dwo_unit *dwo_entry;
6982 struct signatured_type find_sig_entry, *sig_entry;
6983 void **slot;
6984
6985 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6986 gdb_assert (dwp_file != NULL);
6987
6988 /* If TU skeletons have been removed then we may not have read in any
6989 TUs yet. */
6990 if (dwarf2_per_objfile->signatured_types == NULL)
6991 {
6992 dwarf2_per_objfile->signatured_types
6993 = allocate_signatured_type_table (objfile);
6994 }
6995
6996 find_sig_entry.signature = sig;
6997 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6998 &find_sig_entry, INSERT);
6999 sig_entry = (struct signatured_type *) *slot;
7000
7001 /* Have we already tried to read this TU?
7002 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7003 needn't exist in the global table yet). */
7004 if (sig_entry != NULL)
7005 return sig_entry;
7006
7007 if (dwp_file->tus == NULL)
7008 return NULL;
7009 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7010 sig, 1 /* is_debug_types */);
7011 if (dwo_entry == NULL)
7012 return NULL;
7013
7014 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7015 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7016
7017 return sig_entry;
7018 }
7019
7020 /* Lookup a signature based type for DW_FORM_ref_sig8.
7021 Returns NULL if signature SIG is not present in the table.
7022 It is up to the caller to complain about this. */
7023
7024 static struct signatured_type *
7025 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7026 {
7027 struct dwarf2_per_objfile *dwarf2_per_objfile
7028 = cu->per_cu->dwarf2_per_objfile;
7029
7030 if (cu->dwo_unit
7031 && dwarf2_per_objfile->using_index)
7032 {
7033 /* We're in a DWO/DWP file, and we're using .gdb_index.
7034 These cases require special processing. */
7035 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7036 return lookup_dwo_signatured_type (cu, sig);
7037 else
7038 return lookup_dwp_signatured_type (cu, sig);
7039 }
7040 else
7041 {
7042 struct signatured_type find_entry, *entry;
7043
7044 if (dwarf2_per_objfile->signatured_types == NULL)
7045 return NULL;
7046 find_entry.signature = sig;
7047 entry = ((struct signatured_type *)
7048 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7049 return entry;
7050 }
7051 }
7052 \f
7053 /* Low level DIE reading support. */
7054
7055 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7056
7057 static void
7058 init_cu_die_reader (struct die_reader_specs *reader,
7059 struct dwarf2_cu *cu,
7060 struct dwarf2_section_info *section,
7061 struct dwo_file *dwo_file,
7062 struct abbrev_table *abbrev_table)
7063 {
7064 gdb_assert (section->readin && section->buffer != NULL);
7065 reader->abfd = get_section_bfd_owner (section);
7066 reader->cu = cu;
7067 reader->dwo_file = dwo_file;
7068 reader->die_section = section;
7069 reader->buffer = section->buffer;
7070 reader->buffer_end = section->buffer + section->size;
7071 reader->comp_dir = NULL;
7072 reader->abbrev_table = abbrev_table;
7073 }
7074
7075 /* Subroutine of init_cutu_and_read_dies to simplify it.
7076 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7077 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7078 already.
7079
7080 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7081 from it to the DIE in the DWO. If NULL we are skipping the stub.
7082 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7083 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7084 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7085 STUB_COMP_DIR may be non-NULL.
7086 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7087 are filled in with the info of the DIE from the DWO file.
7088 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7089 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7090 kept around for at least as long as *RESULT_READER.
7091
7092 The result is non-zero if a valid (non-dummy) DIE was found. */
7093
7094 static int
7095 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7096 struct dwo_unit *dwo_unit,
7097 struct die_info *stub_comp_unit_die,
7098 const char *stub_comp_dir,
7099 struct die_reader_specs *result_reader,
7100 const gdb_byte **result_info_ptr,
7101 struct die_info **result_comp_unit_die,
7102 int *result_has_children,
7103 abbrev_table_up *result_dwo_abbrev_table)
7104 {
7105 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7106 struct objfile *objfile = dwarf2_per_objfile->objfile;
7107 struct dwarf2_cu *cu = this_cu->cu;
7108 bfd *abfd;
7109 const gdb_byte *begin_info_ptr, *info_ptr;
7110 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7111 int i,num_extra_attrs;
7112 struct dwarf2_section_info *dwo_abbrev_section;
7113 struct attribute *attr;
7114 struct die_info *comp_unit_die;
7115
7116 /* At most one of these may be provided. */
7117 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7118
7119 /* These attributes aren't processed until later:
7120 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7121 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7122 referenced later. However, these attributes are found in the stub
7123 which we won't have later. In order to not impose this complication
7124 on the rest of the code, we read them here and copy them to the
7125 DWO CU/TU die. */
7126
7127 stmt_list = NULL;
7128 low_pc = NULL;
7129 high_pc = NULL;
7130 ranges = NULL;
7131 comp_dir = NULL;
7132
7133 if (stub_comp_unit_die != NULL)
7134 {
7135 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7136 DWO file. */
7137 if (! this_cu->is_debug_types)
7138 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7139 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7140 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7141 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7142 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7143
7144 /* There should be a DW_AT_addr_base attribute here (if needed).
7145 We need the value before we can process DW_FORM_GNU_addr_index. */
7146 cu->addr_base = 0;
7147 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7148 if (attr)
7149 cu->addr_base = DW_UNSND (attr);
7150
7151 /* There should be a DW_AT_ranges_base attribute here (if needed).
7152 We need the value before we can process DW_AT_ranges. */
7153 cu->ranges_base = 0;
7154 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7155 if (attr)
7156 cu->ranges_base = DW_UNSND (attr);
7157 }
7158 else if (stub_comp_dir != NULL)
7159 {
7160 /* Reconstruct the comp_dir attribute to simplify the code below. */
7161 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7162 comp_dir->name = DW_AT_comp_dir;
7163 comp_dir->form = DW_FORM_string;
7164 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7165 DW_STRING (comp_dir) = stub_comp_dir;
7166 }
7167
7168 /* Set up for reading the DWO CU/TU. */
7169 cu->dwo_unit = dwo_unit;
7170 dwarf2_section_info *section = dwo_unit->section;
7171 dwarf2_read_section (objfile, section);
7172 abfd = get_section_bfd_owner (section);
7173 begin_info_ptr = info_ptr = (section->buffer
7174 + to_underlying (dwo_unit->sect_off));
7175 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7176
7177 if (this_cu->is_debug_types)
7178 {
7179 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7180
7181 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7182 &cu->header, section,
7183 dwo_abbrev_section,
7184 info_ptr, rcuh_kind::TYPE);
7185 /* This is not an assert because it can be caused by bad debug info. */
7186 if (sig_type->signature != cu->header.signature)
7187 {
7188 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7189 " TU at offset %s [in module %s]"),
7190 hex_string (sig_type->signature),
7191 hex_string (cu->header.signature),
7192 sect_offset_str (dwo_unit->sect_off),
7193 bfd_get_filename (abfd));
7194 }
7195 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7196 /* For DWOs coming from DWP files, we don't know the CU length
7197 nor the type's offset in the TU until now. */
7198 dwo_unit->length = get_cu_length (&cu->header);
7199 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7200
7201 /* Establish the type offset that can be used to lookup the type.
7202 For DWO files, we don't know it until now. */
7203 sig_type->type_offset_in_section
7204 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7205 }
7206 else
7207 {
7208 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7209 &cu->header, section,
7210 dwo_abbrev_section,
7211 info_ptr, rcuh_kind::COMPILE);
7212 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7213 /* For DWOs coming from DWP files, we don't know the CU length
7214 until now. */
7215 dwo_unit->length = get_cu_length (&cu->header);
7216 }
7217
7218 *result_dwo_abbrev_table
7219 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7220 cu->header.abbrev_sect_off);
7221 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7222 result_dwo_abbrev_table->get ());
7223
7224 /* Read in the die, but leave space to copy over the attributes
7225 from the stub. This has the benefit of simplifying the rest of
7226 the code - all the work to maintain the illusion of a single
7227 DW_TAG_{compile,type}_unit DIE is done here. */
7228 num_extra_attrs = ((stmt_list != NULL)
7229 + (low_pc != NULL)
7230 + (high_pc != NULL)
7231 + (ranges != NULL)
7232 + (comp_dir != NULL));
7233 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7234 result_has_children, num_extra_attrs);
7235
7236 /* Copy over the attributes from the stub to the DIE we just read in. */
7237 comp_unit_die = *result_comp_unit_die;
7238 i = comp_unit_die->num_attrs;
7239 if (stmt_list != NULL)
7240 comp_unit_die->attrs[i++] = *stmt_list;
7241 if (low_pc != NULL)
7242 comp_unit_die->attrs[i++] = *low_pc;
7243 if (high_pc != NULL)
7244 comp_unit_die->attrs[i++] = *high_pc;
7245 if (ranges != NULL)
7246 comp_unit_die->attrs[i++] = *ranges;
7247 if (comp_dir != NULL)
7248 comp_unit_die->attrs[i++] = *comp_dir;
7249 comp_unit_die->num_attrs += num_extra_attrs;
7250
7251 if (dwarf_die_debug)
7252 {
7253 fprintf_unfiltered (gdb_stdlog,
7254 "Read die from %s@0x%x of %s:\n",
7255 get_section_name (section),
7256 (unsigned) (begin_info_ptr - section->buffer),
7257 bfd_get_filename (abfd));
7258 dump_die (comp_unit_die, dwarf_die_debug);
7259 }
7260
7261 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7262 TUs by skipping the stub and going directly to the entry in the DWO file.
7263 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7264 to get it via circuitous means. Blech. */
7265 if (comp_dir != NULL)
7266 result_reader->comp_dir = DW_STRING (comp_dir);
7267
7268 /* Skip dummy compilation units. */
7269 if (info_ptr >= begin_info_ptr + dwo_unit->length
7270 || peek_abbrev_code (abfd, info_ptr) == 0)
7271 return 0;
7272
7273 *result_info_ptr = info_ptr;
7274 return 1;
7275 }
7276
7277 /* Subroutine of init_cutu_and_read_dies to simplify it.
7278 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7279 Returns NULL if the specified DWO unit cannot be found. */
7280
7281 static struct dwo_unit *
7282 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7283 struct die_info *comp_unit_die)
7284 {
7285 struct dwarf2_cu *cu = this_cu->cu;
7286 ULONGEST signature;
7287 struct dwo_unit *dwo_unit;
7288 const char *comp_dir, *dwo_name;
7289
7290 gdb_assert (cu != NULL);
7291
7292 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7293 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7294 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7295
7296 if (this_cu->is_debug_types)
7297 {
7298 struct signatured_type *sig_type;
7299
7300 /* Since this_cu is the first member of struct signatured_type,
7301 we can go from a pointer to one to a pointer to the other. */
7302 sig_type = (struct signatured_type *) this_cu;
7303 signature = sig_type->signature;
7304 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7305 }
7306 else
7307 {
7308 struct attribute *attr;
7309
7310 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7311 if (! attr)
7312 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7313 " [in module %s]"),
7314 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7315 signature = DW_UNSND (attr);
7316 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7317 signature);
7318 }
7319
7320 return dwo_unit;
7321 }
7322
7323 /* Subroutine of init_cutu_and_read_dies to simplify it.
7324 See it for a description of the parameters.
7325 Read a TU directly from a DWO file, bypassing the stub. */
7326
7327 static void
7328 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7329 int use_existing_cu, int keep,
7330 die_reader_func_ftype *die_reader_func,
7331 void *data)
7332 {
7333 std::unique_ptr<dwarf2_cu> new_cu;
7334 struct signatured_type *sig_type;
7335 struct die_reader_specs reader;
7336 const gdb_byte *info_ptr;
7337 struct die_info *comp_unit_die;
7338 int has_children;
7339 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7340
7341 /* Verify we can do the following downcast, and that we have the
7342 data we need. */
7343 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7344 sig_type = (struct signatured_type *) this_cu;
7345 gdb_assert (sig_type->dwo_unit != NULL);
7346
7347 if (use_existing_cu && this_cu->cu != NULL)
7348 {
7349 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7350 /* There's no need to do the rereading_dwo_cu handling that
7351 init_cutu_and_read_dies does since we don't read the stub. */
7352 }
7353 else
7354 {
7355 /* If !use_existing_cu, this_cu->cu must be NULL. */
7356 gdb_assert (this_cu->cu == NULL);
7357 new_cu.reset (new dwarf2_cu (this_cu));
7358 }
7359
7360 /* A future optimization, if needed, would be to use an existing
7361 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7362 could share abbrev tables. */
7363
7364 /* The abbreviation table used by READER, this must live at least as long as
7365 READER. */
7366 abbrev_table_up dwo_abbrev_table;
7367
7368 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7369 NULL /* stub_comp_unit_die */,
7370 sig_type->dwo_unit->dwo_file->comp_dir,
7371 &reader, &info_ptr,
7372 &comp_unit_die, &has_children,
7373 &dwo_abbrev_table) == 0)
7374 {
7375 /* Dummy die. */
7376 return;
7377 }
7378
7379 /* All the "real" work is done here. */
7380 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7381
7382 /* This duplicates the code in init_cutu_and_read_dies,
7383 but the alternative is making the latter more complex.
7384 This function is only for the special case of using DWO files directly:
7385 no point in overly complicating the general case just to handle this. */
7386 if (new_cu != NULL && keep)
7387 {
7388 /* Link this CU into read_in_chain. */
7389 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7390 dwarf2_per_objfile->read_in_chain = this_cu;
7391 /* The chain owns it now. */
7392 new_cu.release ();
7393 }
7394 }
7395
7396 /* Initialize a CU (or TU) and read its DIEs.
7397 If the CU defers to a DWO file, read the DWO file as well.
7398
7399 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7400 Otherwise the table specified in the comp unit header is read in and used.
7401 This is an optimization for when we already have the abbrev table.
7402
7403 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7404 Otherwise, a new CU is allocated with xmalloc.
7405
7406 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7407 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7408
7409 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7410 linker) then DIE_READER_FUNC will not get called. */
7411
7412 static void
7413 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7414 struct abbrev_table *abbrev_table,
7415 int use_existing_cu, int keep,
7416 bool skip_partial,
7417 die_reader_func_ftype *die_reader_func,
7418 void *data)
7419 {
7420 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7421 struct objfile *objfile = dwarf2_per_objfile->objfile;
7422 struct dwarf2_section_info *section = this_cu->section;
7423 bfd *abfd = get_section_bfd_owner (section);
7424 struct dwarf2_cu *cu;
7425 const gdb_byte *begin_info_ptr, *info_ptr;
7426 struct die_reader_specs reader;
7427 struct die_info *comp_unit_die;
7428 int has_children;
7429 struct attribute *attr;
7430 struct signatured_type *sig_type = NULL;
7431 struct dwarf2_section_info *abbrev_section;
7432 /* Non-zero if CU currently points to a DWO file and we need to
7433 reread it. When this happens we need to reread the skeleton die
7434 before we can reread the DWO file (this only applies to CUs, not TUs). */
7435 int rereading_dwo_cu = 0;
7436
7437 if (dwarf_die_debug)
7438 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7439 this_cu->is_debug_types ? "type" : "comp",
7440 sect_offset_str (this_cu->sect_off));
7441
7442 if (use_existing_cu)
7443 gdb_assert (keep);
7444
7445 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7446 file (instead of going through the stub), short-circuit all of this. */
7447 if (this_cu->reading_dwo_directly)
7448 {
7449 /* Narrow down the scope of possibilities to have to understand. */
7450 gdb_assert (this_cu->is_debug_types);
7451 gdb_assert (abbrev_table == NULL);
7452 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7453 die_reader_func, data);
7454 return;
7455 }
7456
7457 /* This is cheap if the section is already read in. */
7458 dwarf2_read_section (objfile, section);
7459
7460 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7461
7462 abbrev_section = get_abbrev_section_for_cu (this_cu);
7463
7464 std::unique_ptr<dwarf2_cu> new_cu;
7465 if (use_existing_cu && this_cu->cu != NULL)
7466 {
7467 cu = this_cu->cu;
7468 /* If this CU is from a DWO file we need to start over, we need to
7469 refetch the attributes from the skeleton CU.
7470 This could be optimized by retrieving those attributes from when we
7471 were here the first time: the previous comp_unit_die was stored in
7472 comp_unit_obstack. But there's no data yet that we need this
7473 optimization. */
7474 if (cu->dwo_unit != NULL)
7475 rereading_dwo_cu = 1;
7476 }
7477 else
7478 {
7479 /* If !use_existing_cu, this_cu->cu must be NULL. */
7480 gdb_assert (this_cu->cu == NULL);
7481 new_cu.reset (new dwarf2_cu (this_cu));
7482 cu = new_cu.get ();
7483 }
7484
7485 /* Get the header. */
7486 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7487 {
7488 /* We already have the header, there's no need to read it in again. */
7489 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7490 }
7491 else
7492 {
7493 if (this_cu->is_debug_types)
7494 {
7495 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7496 &cu->header, section,
7497 abbrev_section, info_ptr,
7498 rcuh_kind::TYPE);
7499
7500 /* Since per_cu is the first member of struct signatured_type,
7501 we can go from a pointer to one to a pointer to the other. */
7502 sig_type = (struct signatured_type *) this_cu;
7503 gdb_assert (sig_type->signature == cu->header.signature);
7504 gdb_assert (sig_type->type_offset_in_tu
7505 == cu->header.type_cu_offset_in_tu);
7506 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7507
7508 /* LENGTH has not been set yet for type units if we're
7509 using .gdb_index. */
7510 this_cu->length = get_cu_length (&cu->header);
7511
7512 /* Establish the type offset that can be used to lookup the type. */
7513 sig_type->type_offset_in_section =
7514 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7515
7516 this_cu->dwarf_version = cu->header.version;
7517 }
7518 else
7519 {
7520 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7521 &cu->header, section,
7522 abbrev_section,
7523 info_ptr,
7524 rcuh_kind::COMPILE);
7525
7526 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7527 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7528 this_cu->dwarf_version = cu->header.version;
7529 }
7530 }
7531
7532 /* Skip dummy compilation units. */
7533 if (info_ptr >= begin_info_ptr + this_cu->length
7534 || peek_abbrev_code (abfd, info_ptr) == 0)
7535 return;
7536
7537 /* If we don't have them yet, read the abbrevs for this compilation unit.
7538 And if we need to read them now, make sure they're freed when we're
7539 done (own the table through ABBREV_TABLE_HOLDER). */
7540 abbrev_table_up abbrev_table_holder;
7541 if (abbrev_table != NULL)
7542 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7543 else
7544 {
7545 abbrev_table_holder
7546 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7547 cu->header.abbrev_sect_off);
7548 abbrev_table = abbrev_table_holder.get ();
7549 }
7550
7551 /* Read the top level CU/TU die. */
7552 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7553 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7554
7555 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7556 return;
7557
7558 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7559 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7560 table from the DWO file and pass the ownership over to us. It will be
7561 referenced from READER, so we must make sure to free it after we're done
7562 with READER.
7563
7564 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7565 DWO CU, that this test will fail (the attribute will not be present). */
7566 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7567 abbrev_table_up dwo_abbrev_table;
7568 if (attr)
7569 {
7570 struct dwo_unit *dwo_unit;
7571 struct die_info *dwo_comp_unit_die;
7572
7573 if (has_children)
7574 {
7575 complaint (&symfile_complaints,
7576 _("compilation unit with DW_AT_GNU_dwo_name"
7577 " has children (offset %s) [in module %s]"),
7578 sect_offset_str (this_cu->sect_off),
7579 bfd_get_filename (abfd));
7580 }
7581 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7582 if (dwo_unit != NULL)
7583 {
7584 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7585 comp_unit_die, NULL,
7586 &reader, &info_ptr,
7587 &dwo_comp_unit_die, &has_children,
7588 &dwo_abbrev_table) == 0)
7589 {
7590 /* Dummy die. */
7591 return;
7592 }
7593 comp_unit_die = dwo_comp_unit_die;
7594 }
7595 else
7596 {
7597 /* Yikes, we couldn't find the rest of the DIE, we only have
7598 the stub. A complaint has already been logged. There's
7599 not much more we can do except pass on the stub DIE to
7600 die_reader_func. We don't want to throw an error on bad
7601 debug info. */
7602 }
7603 }
7604
7605 /* All of the above is setup for this call. Yikes. */
7606 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7607
7608 /* Done, clean up. */
7609 if (new_cu != NULL && keep)
7610 {
7611 /* Link this CU into read_in_chain. */
7612 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7613 dwarf2_per_objfile->read_in_chain = this_cu;
7614 /* The chain owns it now. */
7615 new_cu.release ();
7616 }
7617 }
7618
7619 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7620 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7621 to have already done the lookup to find the DWO file).
7622
7623 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7624 THIS_CU->is_debug_types, but nothing else.
7625
7626 We fill in THIS_CU->length.
7627
7628 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7629 linker) then DIE_READER_FUNC will not get called.
7630
7631 THIS_CU->cu is always freed when done.
7632 This is done in order to not leave THIS_CU->cu in a state where we have
7633 to care whether it refers to the "main" CU or the DWO CU. */
7634
7635 static void
7636 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7637 struct dwo_file *dwo_file,
7638 die_reader_func_ftype *die_reader_func,
7639 void *data)
7640 {
7641 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7642 struct objfile *objfile = dwarf2_per_objfile->objfile;
7643 struct dwarf2_section_info *section = this_cu->section;
7644 bfd *abfd = get_section_bfd_owner (section);
7645 struct dwarf2_section_info *abbrev_section;
7646 const gdb_byte *begin_info_ptr, *info_ptr;
7647 struct die_reader_specs reader;
7648 struct die_info *comp_unit_die;
7649 int has_children;
7650
7651 if (dwarf_die_debug)
7652 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7653 this_cu->is_debug_types ? "type" : "comp",
7654 sect_offset_str (this_cu->sect_off));
7655
7656 gdb_assert (this_cu->cu == NULL);
7657
7658 abbrev_section = (dwo_file != NULL
7659 ? &dwo_file->sections.abbrev
7660 : get_abbrev_section_for_cu (this_cu));
7661
7662 /* This is cheap if the section is already read in. */
7663 dwarf2_read_section (objfile, section);
7664
7665 struct dwarf2_cu cu (this_cu);
7666
7667 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7668 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7669 &cu.header, section,
7670 abbrev_section, info_ptr,
7671 (this_cu->is_debug_types
7672 ? rcuh_kind::TYPE
7673 : rcuh_kind::COMPILE));
7674
7675 this_cu->length = get_cu_length (&cu.header);
7676
7677 /* Skip dummy compilation units. */
7678 if (info_ptr >= begin_info_ptr + this_cu->length
7679 || peek_abbrev_code (abfd, info_ptr) == 0)
7680 return;
7681
7682 abbrev_table_up abbrev_table
7683 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7684 cu.header.abbrev_sect_off);
7685
7686 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7687 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7688
7689 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7690 }
7691
7692 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7693 does not lookup the specified DWO file.
7694 This cannot be used to read DWO files.
7695
7696 THIS_CU->cu is always freed when done.
7697 This is done in order to not leave THIS_CU->cu in a state where we have
7698 to care whether it refers to the "main" CU or the DWO CU.
7699 We can revisit this if the data shows there's a performance issue. */
7700
7701 static void
7702 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7703 die_reader_func_ftype *die_reader_func,
7704 void *data)
7705 {
7706 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7707 }
7708 \f
7709 /* Type Unit Groups.
7710
7711 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7712 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7713 so that all types coming from the same compilation (.o file) are grouped
7714 together. A future step could be to put the types in the same symtab as
7715 the CU the types ultimately came from. */
7716
7717 static hashval_t
7718 hash_type_unit_group (const void *item)
7719 {
7720 const struct type_unit_group *tu_group
7721 = (const struct type_unit_group *) item;
7722
7723 return hash_stmt_list_entry (&tu_group->hash);
7724 }
7725
7726 static int
7727 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7728 {
7729 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7730 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7731
7732 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7733 }
7734
7735 /* Allocate a hash table for type unit groups. */
7736
7737 static htab_t
7738 allocate_type_unit_groups_table (struct objfile *objfile)
7739 {
7740 return htab_create_alloc_ex (3,
7741 hash_type_unit_group,
7742 eq_type_unit_group,
7743 NULL,
7744 &objfile->objfile_obstack,
7745 hashtab_obstack_allocate,
7746 dummy_obstack_deallocate);
7747 }
7748
7749 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7750 partial symtabs. We combine several TUs per psymtab to not let the size
7751 of any one psymtab grow too big. */
7752 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7753 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7754
7755 /* Helper routine for get_type_unit_group.
7756 Create the type_unit_group object used to hold one or more TUs. */
7757
7758 static struct type_unit_group *
7759 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7760 {
7761 struct dwarf2_per_objfile *dwarf2_per_objfile
7762 = cu->per_cu->dwarf2_per_objfile;
7763 struct objfile *objfile = dwarf2_per_objfile->objfile;
7764 struct dwarf2_per_cu_data *per_cu;
7765 struct type_unit_group *tu_group;
7766
7767 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7768 struct type_unit_group);
7769 per_cu = &tu_group->per_cu;
7770 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7771
7772 if (dwarf2_per_objfile->using_index)
7773 {
7774 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7775 struct dwarf2_per_cu_quick_data);
7776 }
7777 else
7778 {
7779 unsigned int line_offset = to_underlying (line_offset_struct);
7780 struct partial_symtab *pst;
7781 char *name;
7782
7783 /* Give the symtab a useful name for debug purposes. */
7784 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7785 name = xstrprintf ("<type_units_%d>",
7786 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7787 else
7788 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7789
7790 pst = create_partial_symtab (per_cu, name);
7791 pst->anonymous = 1;
7792
7793 xfree (name);
7794 }
7795
7796 tu_group->hash.dwo_unit = cu->dwo_unit;
7797 tu_group->hash.line_sect_off = line_offset_struct;
7798
7799 return tu_group;
7800 }
7801
7802 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7803 STMT_LIST is a DW_AT_stmt_list attribute. */
7804
7805 static struct type_unit_group *
7806 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7807 {
7808 struct dwarf2_per_objfile *dwarf2_per_objfile
7809 = cu->per_cu->dwarf2_per_objfile;
7810 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7811 struct type_unit_group *tu_group;
7812 void **slot;
7813 unsigned int line_offset;
7814 struct type_unit_group type_unit_group_for_lookup;
7815
7816 if (dwarf2_per_objfile->type_unit_groups == NULL)
7817 {
7818 dwarf2_per_objfile->type_unit_groups =
7819 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7820 }
7821
7822 /* Do we need to create a new group, or can we use an existing one? */
7823
7824 if (stmt_list)
7825 {
7826 line_offset = DW_UNSND (stmt_list);
7827 ++tu_stats->nr_symtab_sharers;
7828 }
7829 else
7830 {
7831 /* Ugh, no stmt_list. Rare, but we have to handle it.
7832 We can do various things here like create one group per TU or
7833 spread them over multiple groups to split up the expansion work.
7834 To avoid worst case scenarios (too many groups or too large groups)
7835 we, umm, group them in bunches. */
7836 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7837 | (tu_stats->nr_stmt_less_type_units
7838 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7839 ++tu_stats->nr_stmt_less_type_units;
7840 }
7841
7842 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7843 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7844 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7845 &type_unit_group_for_lookup, INSERT);
7846 if (*slot != NULL)
7847 {
7848 tu_group = (struct type_unit_group *) *slot;
7849 gdb_assert (tu_group != NULL);
7850 }
7851 else
7852 {
7853 sect_offset line_offset_struct = (sect_offset) line_offset;
7854 tu_group = create_type_unit_group (cu, line_offset_struct);
7855 *slot = tu_group;
7856 ++tu_stats->nr_symtabs;
7857 }
7858
7859 return tu_group;
7860 }
7861 \f
7862 /* Partial symbol tables. */
7863
7864 /* Create a psymtab named NAME and assign it to PER_CU.
7865
7866 The caller must fill in the following details:
7867 dirname, textlow, texthigh. */
7868
7869 static struct partial_symtab *
7870 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7871 {
7872 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7873 struct partial_symtab *pst;
7874
7875 pst = start_psymtab_common (objfile, name, 0,
7876 objfile->global_psymbols,
7877 objfile->static_psymbols);
7878
7879 pst->psymtabs_addrmap_supported = 1;
7880
7881 /* This is the glue that links PST into GDB's symbol API. */
7882 pst->read_symtab_private = per_cu;
7883 pst->read_symtab = dwarf2_read_symtab;
7884 per_cu->v.psymtab = pst;
7885
7886 return pst;
7887 }
7888
7889 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7890 type. */
7891
7892 struct process_psymtab_comp_unit_data
7893 {
7894 /* True if we are reading a DW_TAG_partial_unit. */
7895
7896 int want_partial_unit;
7897
7898 /* The "pretend" language that is used if the CU doesn't declare a
7899 language. */
7900
7901 enum language pretend_language;
7902 };
7903
7904 /* die_reader_func for process_psymtab_comp_unit. */
7905
7906 static void
7907 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7908 const gdb_byte *info_ptr,
7909 struct die_info *comp_unit_die,
7910 int has_children,
7911 void *data)
7912 {
7913 struct dwarf2_cu *cu = reader->cu;
7914 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7915 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7916 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7917 CORE_ADDR baseaddr;
7918 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7919 struct partial_symtab *pst;
7920 enum pc_bounds_kind cu_bounds_kind;
7921 const char *filename;
7922 struct process_psymtab_comp_unit_data *info
7923 = (struct process_psymtab_comp_unit_data *) data;
7924
7925 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7926 return;
7927
7928 gdb_assert (! per_cu->is_debug_types);
7929
7930 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7931
7932 cu->list_in_scope = &file_symbols;
7933
7934 /* Allocate a new partial symbol table structure. */
7935 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7936 if (filename == NULL)
7937 filename = "";
7938
7939 pst = create_partial_symtab (per_cu, filename);
7940
7941 /* This must be done before calling dwarf2_build_include_psymtabs. */
7942 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7943
7944 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7945
7946 dwarf2_find_base_address (comp_unit_die, cu);
7947
7948 /* Possibly set the default values of LOWPC and HIGHPC from
7949 `DW_AT_ranges'. */
7950 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7951 &best_highpc, cu, pst);
7952 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7953 /* Store the contiguous range if it is not empty; it can be empty for
7954 CUs with no code. */
7955 addrmap_set_empty (objfile->psymtabs_addrmap,
7956 gdbarch_adjust_dwarf2_addr (gdbarch,
7957 best_lowpc + baseaddr),
7958 gdbarch_adjust_dwarf2_addr (gdbarch,
7959 best_highpc + baseaddr) - 1,
7960 pst);
7961
7962 /* Check if comp unit has_children.
7963 If so, read the rest of the partial symbols from this comp unit.
7964 If not, there's no more debug_info for this comp unit. */
7965 if (has_children)
7966 {
7967 struct partial_die_info *first_die;
7968 CORE_ADDR lowpc, highpc;
7969
7970 lowpc = ((CORE_ADDR) -1);
7971 highpc = ((CORE_ADDR) 0);
7972
7973 first_die = load_partial_dies (reader, info_ptr, 1);
7974
7975 scan_partial_symbols (first_die, &lowpc, &highpc,
7976 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7977
7978 /* If we didn't find a lowpc, set it to highpc to avoid
7979 complaints from `maint check'. */
7980 if (lowpc == ((CORE_ADDR) -1))
7981 lowpc = highpc;
7982
7983 /* If the compilation unit didn't have an explicit address range,
7984 then use the information extracted from its child dies. */
7985 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7986 {
7987 best_lowpc = lowpc;
7988 best_highpc = highpc;
7989 }
7990 }
7991 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7992 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
7993
7994 end_psymtab_common (objfile, pst);
7995
7996 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7997 {
7998 int i;
7999 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8000 struct dwarf2_per_cu_data *iter;
8001
8002 /* Fill in 'dependencies' here; we fill in 'users' in a
8003 post-pass. */
8004 pst->number_of_dependencies = len;
8005 pst->dependencies =
8006 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8007 for (i = 0;
8008 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8009 i, iter);
8010 ++i)
8011 pst->dependencies[i] = iter->v.psymtab;
8012
8013 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8014 }
8015
8016 /* Get the list of files included in the current compilation unit,
8017 and build a psymtab for each of them. */
8018 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8019
8020 if (dwarf_read_debug)
8021 {
8022 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8023
8024 fprintf_unfiltered (gdb_stdlog,
8025 "Psymtab for %s unit @%s: %s - %s"
8026 ", %d global, %d static syms\n",
8027 per_cu->is_debug_types ? "type" : "comp",
8028 sect_offset_str (per_cu->sect_off),
8029 paddress (gdbarch, pst->textlow),
8030 paddress (gdbarch, pst->texthigh),
8031 pst->n_global_syms, pst->n_static_syms);
8032 }
8033 }
8034
8035 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8036 Process compilation unit THIS_CU for a psymtab. */
8037
8038 static void
8039 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8040 int want_partial_unit,
8041 enum language pretend_language)
8042 {
8043 /* If this compilation unit was already read in, free the
8044 cached copy in order to read it in again. This is
8045 necessary because we skipped some symbols when we first
8046 read in the compilation unit (see load_partial_dies).
8047 This problem could be avoided, but the benefit is unclear. */
8048 if (this_cu->cu != NULL)
8049 free_one_cached_comp_unit (this_cu);
8050
8051 if (this_cu->is_debug_types)
8052 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8053 build_type_psymtabs_reader, NULL);
8054 else
8055 {
8056 process_psymtab_comp_unit_data info;
8057 info.want_partial_unit = want_partial_unit;
8058 info.pretend_language = pretend_language;
8059 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8060 process_psymtab_comp_unit_reader, &info);
8061 }
8062
8063 /* Age out any secondary CUs. */
8064 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8065 }
8066
8067 /* Reader function for build_type_psymtabs. */
8068
8069 static void
8070 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8071 const gdb_byte *info_ptr,
8072 struct die_info *type_unit_die,
8073 int has_children,
8074 void *data)
8075 {
8076 struct dwarf2_per_objfile *dwarf2_per_objfile
8077 = reader->cu->per_cu->dwarf2_per_objfile;
8078 struct objfile *objfile = dwarf2_per_objfile->objfile;
8079 struct dwarf2_cu *cu = reader->cu;
8080 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8081 struct signatured_type *sig_type;
8082 struct type_unit_group *tu_group;
8083 struct attribute *attr;
8084 struct partial_die_info *first_die;
8085 CORE_ADDR lowpc, highpc;
8086 struct partial_symtab *pst;
8087
8088 gdb_assert (data == NULL);
8089 gdb_assert (per_cu->is_debug_types);
8090 sig_type = (struct signatured_type *) per_cu;
8091
8092 if (! has_children)
8093 return;
8094
8095 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8096 tu_group = get_type_unit_group (cu, attr);
8097
8098 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8099
8100 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8101 cu->list_in_scope = &file_symbols;
8102 pst = create_partial_symtab (per_cu, "");
8103 pst->anonymous = 1;
8104
8105 first_die = load_partial_dies (reader, info_ptr, 1);
8106
8107 lowpc = (CORE_ADDR) -1;
8108 highpc = (CORE_ADDR) 0;
8109 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8110
8111 end_psymtab_common (objfile, pst);
8112 }
8113
8114 /* Struct used to sort TUs by their abbreviation table offset. */
8115
8116 struct tu_abbrev_offset
8117 {
8118 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8119 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8120 {}
8121
8122 signatured_type *sig_type;
8123 sect_offset abbrev_offset;
8124 };
8125
8126 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8127
8128 static bool
8129 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8130 const struct tu_abbrev_offset &b)
8131 {
8132 return a.abbrev_offset < b.abbrev_offset;
8133 }
8134
8135 /* Efficiently read all the type units.
8136 This does the bulk of the work for build_type_psymtabs.
8137
8138 The efficiency is because we sort TUs by the abbrev table they use and
8139 only read each abbrev table once. In one program there are 200K TUs
8140 sharing 8K abbrev tables.
8141
8142 The main purpose of this function is to support building the
8143 dwarf2_per_objfile->type_unit_groups table.
8144 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8145 can collapse the search space by grouping them by stmt_list.
8146 The savings can be significant, in the same program from above the 200K TUs
8147 share 8K stmt_list tables.
8148
8149 FUNC is expected to call get_type_unit_group, which will create the
8150 struct type_unit_group if necessary and add it to
8151 dwarf2_per_objfile->type_unit_groups. */
8152
8153 static void
8154 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8155 {
8156 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8157 abbrev_table_up abbrev_table;
8158 sect_offset abbrev_offset;
8159
8160 /* It's up to the caller to not call us multiple times. */
8161 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8162
8163 if (dwarf2_per_objfile->all_type_units.empty ())
8164 return;
8165
8166 /* TUs typically share abbrev tables, and there can be way more TUs than
8167 abbrev tables. Sort by abbrev table to reduce the number of times we
8168 read each abbrev table in.
8169 Alternatives are to punt or to maintain a cache of abbrev tables.
8170 This is simpler and efficient enough for now.
8171
8172 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8173 symtab to use). Typically TUs with the same abbrev offset have the same
8174 stmt_list value too so in practice this should work well.
8175
8176 The basic algorithm here is:
8177
8178 sort TUs by abbrev table
8179 for each TU with same abbrev table:
8180 read abbrev table if first user
8181 read TU top level DIE
8182 [IWBN if DWO skeletons had DW_AT_stmt_list]
8183 call FUNC */
8184
8185 if (dwarf_read_debug)
8186 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8187
8188 /* Sort in a separate table to maintain the order of all_type_units
8189 for .gdb_index: TU indices directly index all_type_units. */
8190 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8191 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8192
8193 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8194 sorted_by_abbrev.emplace_back
8195 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8196 sig_type->per_cu.section,
8197 sig_type->per_cu.sect_off));
8198
8199 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8200 sort_tu_by_abbrev_offset);
8201
8202 abbrev_offset = (sect_offset) ~(unsigned) 0;
8203
8204 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8205 {
8206 /* Switch to the next abbrev table if necessary. */
8207 if (abbrev_table == NULL
8208 || tu.abbrev_offset != abbrev_offset)
8209 {
8210 abbrev_offset = tu.abbrev_offset;
8211 abbrev_table =
8212 abbrev_table_read_table (dwarf2_per_objfile,
8213 &dwarf2_per_objfile->abbrev,
8214 abbrev_offset);
8215 ++tu_stats->nr_uniq_abbrev_tables;
8216 }
8217
8218 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8219 0, 0, false, build_type_psymtabs_reader, NULL);
8220 }
8221 }
8222
8223 /* Print collected type unit statistics. */
8224
8225 static void
8226 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8227 {
8228 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8229
8230 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8231 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8232 dwarf2_per_objfile->all_type_units.size ());
8233 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8234 tu_stats->nr_uniq_abbrev_tables);
8235 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8236 tu_stats->nr_symtabs);
8237 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8238 tu_stats->nr_symtab_sharers);
8239 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8240 tu_stats->nr_stmt_less_type_units);
8241 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8242 tu_stats->nr_all_type_units_reallocs);
8243 }
8244
8245 /* Traversal function for build_type_psymtabs. */
8246
8247 static int
8248 build_type_psymtab_dependencies (void **slot, void *info)
8249 {
8250 struct dwarf2_per_objfile *dwarf2_per_objfile
8251 = (struct dwarf2_per_objfile *) info;
8252 struct objfile *objfile = dwarf2_per_objfile->objfile;
8253 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8254 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8255 struct partial_symtab *pst = per_cu->v.psymtab;
8256 int len = VEC_length (sig_type_ptr, tu_group->tus);
8257 struct signatured_type *iter;
8258 int i;
8259
8260 gdb_assert (len > 0);
8261 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8262
8263 pst->number_of_dependencies = len;
8264 pst->dependencies =
8265 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8266 for (i = 0;
8267 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8268 ++i)
8269 {
8270 gdb_assert (iter->per_cu.is_debug_types);
8271 pst->dependencies[i] = iter->per_cu.v.psymtab;
8272 iter->type_unit_group = tu_group;
8273 }
8274
8275 VEC_free (sig_type_ptr, tu_group->tus);
8276
8277 return 1;
8278 }
8279
8280 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8281 Build partial symbol tables for the .debug_types comp-units. */
8282
8283 static void
8284 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8285 {
8286 if (! create_all_type_units (dwarf2_per_objfile))
8287 return;
8288
8289 build_type_psymtabs_1 (dwarf2_per_objfile);
8290 }
8291
8292 /* Traversal function for process_skeletonless_type_unit.
8293 Read a TU in a DWO file and build partial symbols for it. */
8294
8295 static int
8296 process_skeletonless_type_unit (void **slot, void *info)
8297 {
8298 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8299 struct dwarf2_per_objfile *dwarf2_per_objfile
8300 = (struct dwarf2_per_objfile *) info;
8301 struct signatured_type find_entry, *entry;
8302
8303 /* If this TU doesn't exist in the global table, add it and read it in. */
8304
8305 if (dwarf2_per_objfile->signatured_types == NULL)
8306 {
8307 dwarf2_per_objfile->signatured_types
8308 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8309 }
8310
8311 find_entry.signature = dwo_unit->signature;
8312 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8313 INSERT);
8314 /* If we've already seen this type there's nothing to do. What's happening
8315 is we're doing our own version of comdat-folding here. */
8316 if (*slot != NULL)
8317 return 1;
8318
8319 /* This does the job that create_all_type_units would have done for
8320 this TU. */
8321 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8322 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8323 *slot = entry;
8324
8325 /* This does the job that build_type_psymtabs_1 would have done. */
8326 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8327 build_type_psymtabs_reader, NULL);
8328
8329 return 1;
8330 }
8331
8332 /* Traversal function for process_skeletonless_type_units. */
8333
8334 static int
8335 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8336 {
8337 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8338
8339 if (dwo_file->tus != NULL)
8340 {
8341 htab_traverse_noresize (dwo_file->tus,
8342 process_skeletonless_type_unit, info);
8343 }
8344
8345 return 1;
8346 }
8347
8348 /* Scan all TUs of DWO files, verifying we've processed them.
8349 This is needed in case a TU was emitted without its skeleton.
8350 Note: This can't be done until we know what all the DWO files are. */
8351
8352 static void
8353 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8354 {
8355 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8356 if (get_dwp_file (dwarf2_per_objfile) == NULL
8357 && dwarf2_per_objfile->dwo_files != NULL)
8358 {
8359 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8360 process_dwo_file_for_skeletonless_type_units,
8361 dwarf2_per_objfile);
8362 }
8363 }
8364
8365 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8366
8367 static void
8368 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8369 {
8370 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8371 {
8372 struct partial_symtab *pst = per_cu->v.psymtab;
8373
8374 if (pst == NULL)
8375 continue;
8376
8377 for (int j = 0; j < pst->number_of_dependencies; ++j)
8378 {
8379 /* Set the 'user' field only if it is not already set. */
8380 if (pst->dependencies[j]->user == NULL)
8381 pst->dependencies[j]->user = pst;
8382 }
8383 }
8384 }
8385
8386 /* Build the partial symbol table by doing a quick pass through the
8387 .debug_info and .debug_abbrev sections. */
8388
8389 static void
8390 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8391 {
8392 struct objfile *objfile = dwarf2_per_objfile->objfile;
8393
8394 if (dwarf_read_debug)
8395 {
8396 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8397 objfile_name (objfile));
8398 }
8399
8400 dwarf2_per_objfile->reading_partial_symbols = 1;
8401
8402 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8403
8404 /* Any cached compilation units will be linked by the per-objfile
8405 read_in_chain. Make sure to free them when we're done. */
8406 free_cached_comp_units freer (dwarf2_per_objfile);
8407
8408 build_type_psymtabs (dwarf2_per_objfile);
8409
8410 create_all_comp_units (dwarf2_per_objfile);
8411
8412 /* Create a temporary address map on a temporary obstack. We later
8413 copy this to the final obstack. */
8414 auto_obstack temp_obstack;
8415
8416 scoped_restore save_psymtabs_addrmap
8417 = make_scoped_restore (&objfile->psymtabs_addrmap,
8418 addrmap_create_mutable (&temp_obstack));
8419
8420 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8421 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8422
8423 /* This has to wait until we read the CUs, we need the list of DWOs. */
8424 process_skeletonless_type_units (dwarf2_per_objfile);
8425
8426 /* Now that all TUs have been processed we can fill in the dependencies. */
8427 if (dwarf2_per_objfile->type_unit_groups != NULL)
8428 {
8429 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8430 build_type_psymtab_dependencies, dwarf2_per_objfile);
8431 }
8432
8433 if (dwarf_read_debug)
8434 print_tu_stats (dwarf2_per_objfile);
8435
8436 set_partial_user (dwarf2_per_objfile);
8437
8438 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8439 &objfile->objfile_obstack);
8440 /* At this point we want to keep the address map. */
8441 save_psymtabs_addrmap.release ();
8442
8443 if (dwarf_read_debug)
8444 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8445 objfile_name (objfile));
8446 }
8447
8448 /* die_reader_func for load_partial_comp_unit. */
8449
8450 static void
8451 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8452 const gdb_byte *info_ptr,
8453 struct die_info *comp_unit_die,
8454 int has_children,
8455 void *data)
8456 {
8457 struct dwarf2_cu *cu = reader->cu;
8458
8459 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8460
8461 /* Check if comp unit has_children.
8462 If so, read the rest of the partial symbols from this comp unit.
8463 If not, there's no more debug_info for this comp unit. */
8464 if (has_children)
8465 load_partial_dies (reader, info_ptr, 0);
8466 }
8467
8468 /* Load the partial DIEs for a secondary CU into memory.
8469 This is also used when rereading a primary CU with load_all_dies. */
8470
8471 static void
8472 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8473 {
8474 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8475 load_partial_comp_unit_reader, NULL);
8476 }
8477
8478 static void
8479 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8480 struct dwarf2_section_info *section,
8481 struct dwarf2_section_info *abbrev_section,
8482 unsigned int is_dwz)
8483 {
8484 const gdb_byte *info_ptr;
8485 struct objfile *objfile = dwarf2_per_objfile->objfile;
8486
8487 if (dwarf_read_debug)
8488 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8489 get_section_name (section),
8490 get_section_file_name (section));
8491
8492 dwarf2_read_section (objfile, section);
8493
8494 info_ptr = section->buffer;
8495
8496 while (info_ptr < section->buffer + section->size)
8497 {
8498 struct dwarf2_per_cu_data *this_cu;
8499
8500 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8501
8502 comp_unit_head cu_header;
8503 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8504 abbrev_section, info_ptr,
8505 rcuh_kind::COMPILE);
8506
8507 /* Save the compilation unit for later lookup. */
8508 if (cu_header.unit_type != DW_UT_type)
8509 {
8510 this_cu = XOBNEW (&objfile->objfile_obstack,
8511 struct dwarf2_per_cu_data);
8512 memset (this_cu, 0, sizeof (*this_cu));
8513 }
8514 else
8515 {
8516 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8517 struct signatured_type);
8518 memset (sig_type, 0, sizeof (*sig_type));
8519 sig_type->signature = cu_header.signature;
8520 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8521 this_cu = &sig_type->per_cu;
8522 }
8523 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8524 this_cu->sect_off = sect_off;
8525 this_cu->length = cu_header.length + cu_header.initial_length_size;
8526 this_cu->is_dwz = is_dwz;
8527 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8528 this_cu->section = section;
8529
8530 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8531
8532 info_ptr = info_ptr + this_cu->length;
8533 }
8534 }
8535
8536 /* Create a list of all compilation units in OBJFILE.
8537 This is only done for -readnow and building partial symtabs. */
8538
8539 static void
8540 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8541 {
8542 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8543 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8544 &dwarf2_per_objfile->abbrev, 0);
8545
8546 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8547 if (dwz != NULL)
8548 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8549 1);
8550 }
8551
8552 /* Process all loaded DIEs for compilation unit CU, starting at
8553 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8554 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8555 DW_AT_ranges). See the comments of add_partial_subprogram on how
8556 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8557
8558 static void
8559 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8560 CORE_ADDR *highpc, int set_addrmap,
8561 struct dwarf2_cu *cu)
8562 {
8563 struct partial_die_info *pdi;
8564
8565 /* Now, march along the PDI's, descending into ones which have
8566 interesting children but skipping the children of the other ones,
8567 until we reach the end of the compilation unit. */
8568
8569 pdi = first_die;
8570
8571 while (pdi != NULL)
8572 {
8573 pdi->fixup (cu);
8574
8575 /* Anonymous namespaces or modules have no name but have interesting
8576 children, so we need to look at them. Ditto for anonymous
8577 enums. */
8578
8579 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8580 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8581 || pdi->tag == DW_TAG_imported_unit
8582 || pdi->tag == DW_TAG_inlined_subroutine)
8583 {
8584 switch (pdi->tag)
8585 {
8586 case DW_TAG_subprogram:
8587 case DW_TAG_inlined_subroutine:
8588 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8589 break;
8590 case DW_TAG_constant:
8591 case DW_TAG_variable:
8592 case DW_TAG_typedef:
8593 case DW_TAG_union_type:
8594 if (!pdi->is_declaration)
8595 {
8596 add_partial_symbol (pdi, cu);
8597 }
8598 break;
8599 case DW_TAG_class_type:
8600 case DW_TAG_interface_type:
8601 case DW_TAG_structure_type:
8602 if (!pdi->is_declaration)
8603 {
8604 add_partial_symbol (pdi, cu);
8605 }
8606 if ((cu->language == language_rust
8607 || cu->language == language_cplus) && pdi->has_children)
8608 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8609 set_addrmap, cu);
8610 break;
8611 case DW_TAG_enumeration_type:
8612 if (!pdi->is_declaration)
8613 add_partial_enumeration (pdi, cu);
8614 break;
8615 case DW_TAG_base_type:
8616 case DW_TAG_subrange_type:
8617 /* File scope base type definitions are added to the partial
8618 symbol table. */
8619 add_partial_symbol (pdi, cu);
8620 break;
8621 case DW_TAG_namespace:
8622 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8623 break;
8624 case DW_TAG_module:
8625 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8626 break;
8627 case DW_TAG_imported_unit:
8628 {
8629 struct dwarf2_per_cu_data *per_cu;
8630
8631 /* For now we don't handle imported units in type units. */
8632 if (cu->per_cu->is_debug_types)
8633 {
8634 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8635 " supported in type units [in module %s]"),
8636 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8637 }
8638
8639 per_cu = dwarf2_find_containing_comp_unit
8640 (pdi->d.sect_off, pdi->is_dwz,
8641 cu->per_cu->dwarf2_per_objfile);
8642
8643 /* Go read the partial unit, if needed. */
8644 if (per_cu->v.psymtab == NULL)
8645 process_psymtab_comp_unit (per_cu, 1, cu->language);
8646
8647 VEC_safe_push (dwarf2_per_cu_ptr,
8648 cu->per_cu->imported_symtabs, per_cu);
8649 }
8650 break;
8651 case DW_TAG_imported_declaration:
8652 add_partial_symbol (pdi, cu);
8653 break;
8654 default:
8655 break;
8656 }
8657 }
8658
8659 /* If the die has a sibling, skip to the sibling. */
8660
8661 pdi = pdi->die_sibling;
8662 }
8663 }
8664
8665 /* Functions used to compute the fully scoped name of a partial DIE.
8666
8667 Normally, this is simple. For C++, the parent DIE's fully scoped
8668 name is concatenated with "::" and the partial DIE's name.
8669 Enumerators are an exception; they use the scope of their parent
8670 enumeration type, i.e. the name of the enumeration type is not
8671 prepended to the enumerator.
8672
8673 There are two complexities. One is DW_AT_specification; in this
8674 case "parent" means the parent of the target of the specification,
8675 instead of the direct parent of the DIE. The other is compilers
8676 which do not emit DW_TAG_namespace; in this case we try to guess
8677 the fully qualified name of structure types from their members'
8678 linkage names. This must be done using the DIE's children rather
8679 than the children of any DW_AT_specification target. We only need
8680 to do this for structures at the top level, i.e. if the target of
8681 any DW_AT_specification (if any; otherwise the DIE itself) does not
8682 have a parent. */
8683
8684 /* Compute the scope prefix associated with PDI's parent, in
8685 compilation unit CU. The result will be allocated on CU's
8686 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8687 field. NULL is returned if no prefix is necessary. */
8688 static const char *
8689 partial_die_parent_scope (struct partial_die_info *pdi,
8690 struct dwarf2_cu *cu)
8691 {
8692 const char *grandparent_scope;
8693 struct partial_die_info *parent, *real_pdi;
8694
8695 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8696 then this means the parent of the specification DIE. */
8697
8698 real_pdi = pdi;
8699 while (real_pdi->has_specification)
8700 real_pdi = find_partial_die (real_pdi->spec_offset,
8701 real_pdi->spec_is_dwz, cu);
8702
8703 parent = real_pdi->die_parent;
8704 if (parent == NULL)
8705 return NULL;
8706
8707 if (parent->scope_set)
8708 return parent->scope;
8709
8710 parent->fixup (cu);
8711
8712 grandparent_scope = partial_die_parent_scope (parent, cu);
8713
8714 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8715 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8716 Work around this problem here. */
8717 if (cu->language == language_cplus
8718 && parent->tag == DW_TAG_namespace
8719 && strcmp (parent->name, "::") == 0
8720 && grandparent_scope == NULL)
8721 {
8722 parent->scope = NULL;
8723 parent->scope_set = 1;
8724 return NULL;
8725 }
8726
8727 if (pdi->tag == DW_TAG_enumerator)
8728 /* Enumerators should not get the name of the enumeration as a prefix. */
8729 parent->scope = grandparent_scope;
8730 else if (parent->tag == DW_TAG_namespace
8731 || parent->tag == DW_TAG_module
8732 || parent->tag == DW_TAG_structure_type
8733 || parent->tag == DW_TAG_class_type
8734 || parent->tag == DW_TAG_interface_type
8735 || parent->tag == DW_TAG_union_type
8736 || parent->tag == DW_TAG_enumeration_type)
8737 {
8738 if (grandparent_scope == NULL)
8739 parent->scope = parent->name;
8740 else
8741 parent->scope = typename_concat (&cu->comp_unit_obstack,
8742 grandparent_scope,
8743 parent->name, 0, cu);
8744 }
8745 else
8746 {
8747 /* FIXME drow/2004-04-01: What should we be doing with
8748 function-local names? For partial symbols, we should probably be
8749 ignoring them. */
8750 complaint (&symfile_complaints,
8751 _("unhandled containing DIE tag %d for DIE at %s"),
8752 parent->tag, sect_offset_str (pdi->sect_off));
8753 parent->scope = grandparent_scope;
8754 }
8755
8756 parent->scope_set = 1;
8757 return parent->scope;
8758 }
8759
8760 /* Return the fully scoped name associated with PDI, from compilation unit
8761 CU. The result will be allocated with malloc. */
8762
8763 static char *
8764 partial_die_full_name (struct partial_die_info *pdi,
8765 struct dwarf2_cu *cu)
8766 {
8767 const char *parent_scope;
8768
8769 /* If this is a template instantiation, we can not work out the
8770 template arguments from partial DIEs. So, unfortunately, we have
8771 to go through the full DIEs. At least any work we do building
8772 types here will be reused if full symbols are loaded later. */
8773 if (pdi->has_template_arguments)
8774 {
8775 pdi->fixup (cu);
8776
8777 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8778 {
8779 struct die_info *die;
8780 struct attribute attr;
8781 struct dwarf2_cu *ref_cu = cu;
8782
8783 /* DW_FORM_ref_addr is using section offset. */
8784 attr.name = (enum dwarf_attribute) 0;
8785 attr.form = DW_FORM_ref_addr;
8786 attr.u.unsnd = to_underlying (pdi->sect_off);
8787 die = follow_die_ref (NULL, &attr, &ref_cu);
8788
8789 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8790 }
8791 }
8792
8793 parent_scope = partial_die_parent_scope (pdi, cu);
8794 if (parent_scope == NULL)
8795 return NULL;
8796 else
8797 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8798 }
8799
8800 static void
8801 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8802 {
8803 struct dwarf2_per_objfile *dwarf2_per_objfile
8804 = cu->per_cu->dwarf2_per_objfile;
8805 struct objfile *objfile = dwarf2_per_objfile->objfile;
8806 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8807 CORE_ADDR addr = 0;
8808 const char *actual_name = NULL;
8809 CORE_ADDR baseaddr;
8810 char *built_actual_name;
8811
8812 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8813
8814 built_actual_name = partial_die_full_name (pdi, cu);
8815 if (built_actual_name != NULL)
8816 actual_name = built_actual_name;
8817
8818 if (actual_name == NULL)
8819 actual_name = pdi->name;
8820
8821 switch (pdi->tag)
8822 {
8823 case DW_TAG_inlined_subroutine:
8824 case DW_TAG_subprogram:
8825 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
8826 if (pdi->is_external || cu->language == language_ada)
8827 {
8828 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8829 of the global scope. But in Ada, we want to be able to access
8830 nested procedures globally. So all Ada subprograms are stored
8831 in the global scope. */
8832 add_psymbol_to_list (actual_name, strlen (actual_name),
8833 built_actual_name != NULL,
8834 VAR_DOMAIN, LOC_BLOCK,
8835 &objfile->global_psymbols,
8836 addr, cu->language, objfile);
8837 }
8838 else
8839 {
8840 add_psymbol_to_list (actual_name, strlen (actual_name),
8841 built_actual_name != NULL,
8842 VAR_DOMAIN, LOC_BLOCK,
8843 &objfile->static_psymbols,
8844 addr, cu->language, objfile);
8845 }
8846
8847 if (pdi->main_subprogram && actual_name != NULL)
8848 set_objfile_main_name (objfile, actual_name, cu->language);
8849 break;
8850 case DW_TAG_constant:
8851 {
8852 std::vector<partial_symbol *> *list;
8853
8854 if (pdi->is_external)
8855 list = &objfile->global_psymbols;
8856 else
8857 list = &objfile->static_psymbols;
8858 add_psymbol_to_list (actual_name, strlen (actual_name),
8859 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8860 list, 0, cu->language, objfile);
8861 }
8862 break;
8863 case DW_TAG_variable:
8864 if (pdi->d.locdesc)
8865 addr = decode_locdesc (pdi->d.locdesc, cu);
8866
8867 if (pdi->d.locdesc
8868 && addr == 0
8869 && !dwarf2_per_objfile->has_section_at_zero)
8870 {
8871 /* A global or static variable may also have been stripped
8872 out by the linker if unused, in which case its address
8873 will be nullified; do not add such variables into partial
8874 symbol table then. */
8875 }
8876 else if (pdi->is_external)
8877 {
8878 /* Global Variable.
8879 Don't enter into the minimal symbol tables as there is
8880 a minimal symbol table entry from the ELF symbols already.
8881 Enter into partial symbol table if it has a location
8882 descriptor or a type.
8883 If the location descriptor is missing, new_symbol will create
8884 a LOC_UNRESOLVED symbol, the address of the variable will then
8885 be determined from the minimal symbol table whenever the variable
8886 is referenced.
8887 The address for the partial symbol table entry is not
8888 used by GDB, but it comes in handy for debugging partial symbol
8889 table building. */
8890
8891 if (pdi->d.locdesc || pdi->has_type)
8892 add_psymbol_to_list (actual_name, strlen (actual_name),
8893 built_actual_name != NULL,
8894 VAR_DOMAIN, LOC_STATIC,
8895 &objfile->global_psymbols,
8896 addr + baseaddr,
8897 cu->language, objfile);
8898 }
8899 else
8900 {
8901 int has_loc = pdi->d.locdesc != NULL;
8902
8903 /* Static Variable. Skip symbols whose value we cannot know (those
8904 without location descriptors or constant values). */
8905 if (!has_loc && !pdi->has_const_value)
8906 {
8907 xfree (built_actual_name);
8908 return;
8909 }
8910
8911 add_psymbol_to_list (actual_name, strlen (actual_name),
8912 built_actual_name != NULL,
8913 VAR_DOMAIN, LOC_STATIC,
8914 &objfile->static_psymbols,
8915 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
8916 cu->language, objfile);
8917 }
8918 break;
8919 case DW_TAG_typedef:
8920 case DW_TAG_base_type:
8921 case DW_TAG_subrange_type:
8922 add_psymbol_to_list (actual_name, strlen (actual_name),
8923 built_actual_name != NULL,
8924 VAR_DOMAIN, LOC_TYPEDEF,
8925 &objfile->static_psymbols,
8926 0, cu->language, objfile);
8927 break;
8928 case DW_TAG_imported_declaration:
8929 case DW_TAG_namespace:
8930 add_psymbol_to_list (actual_name, strlen (actual_name),
8931 built_actual_name != NULL,
8932 VAR_DOMAIN, LOC_TYPEDEF,
8933 &objfile->global_psymbols,
8934 0, cu->language, objfile);
8935 break;
8936 case DW_TAG_module:
8937 add_psymbol_to_list (actual_name, strlen (actual_name),
8938 built_actual_name != NULL,
8939 MODULE_DOMAIN, LOC_TYPEDEF,
8940 &objfile->global_psymbols,
8941 0, cu->language, objfile);
8942 break;
8943 case DW_TAG_class_type:
8944 case DW_TAG_interface_type:
8945 case DW_TAG_structure_type:
8946 case DW_TAG_union_type:
8947 case DW_TAG_enumeration_type:
8948 /* Skip external references. The DWARF standard says in the section
8949 about "Structure, Union, and Class Type Entries": "An incomplete
8950 structure, union or class type is represented by a structure,
8951 union or class entry that does not have a byte size attribute
8952 and that has a DW_AT_declaration attribute." */
8953 if (!pdi->has_byte_size && pdi->is_declaration)
8954 {
8955 xfree (built_actual_name);
8956 return;
8957 }
8958
8959 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8960 static vs. global. */
8961 add_psymbol_to_list (actual_name, strlen (actual_name),
8962 built_actual_name != NULL,
8963 STRUCT_DOMAIN, LOC_TYPEDEF,
8964 cu->language == language_cplus
8965 ? &objfile->global_psymbols
8966 : &objfile->static_psymbols,
8967 0, cu->language, objfile);
8968
8969 break;
8970 case DW_TAG_enumerator:
8971 add_psymbol_to_list (actual_name, strlen (actual_name),
8972 built_actual_name != NULL,
8973 VAR_DOMAIN, LOC_CONST,
8974 cu->language == language_cplus
8975 ? &objfile->global_psymbols
8976 : &objfile->static_psymbols,
8977 0, cu->language, objfile);
8978 break;
8979 default:
8980 break;
8981 }
8982
8983 xfree (built_actual_name);
8984 }
8985
8986 /* Read a partial die corresponding to a namespace; also, add a symbol
8987 corresponding to that namespace to the symbol table. NAMESPACE is
8988 the name of the enclosing namespace. */
8989
8990 static void
8991 add_partial_namespace (struct partial_die_info *pdi,
8992 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8993 int set_addrmap, struct dwarf2_cu *cu)
8994 {
8995 /* Add a symbol for the namespace. */
8996
8997 add_partial_symbol (pdi, cu);
8998
8999 /* Now scan partial symbols in that namespace. */
9000
9001 if (pdi->has_children)
9002 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9003 }
9004
9005 /* Read a partial die corresponding to a Fortran module. */
9006
9007 static void
9008 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9009 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9010 {
9011 /* Add a symbol for the namespace. */
9012
9013 add_partial_symbol (pdi, cu);
9014
9015 /* Now scan partial symbols in that module. */
9016
9017 if (pdi->has_children)
9018 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9019 }
9020
9021 /* Read a partial die corresponding to a subprogram or an inlined
9022 subprogram and create a partial symbol for that subprogram.
9023 When the CU language allows it, this routine also defines a partial
9024 symbol for each nested subprogram that this subprogram contains.
9025 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9026 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9027
9028 PDI may also be a lexical block, in which case we simply search
9029 recursively for subprograms defined inside that lexical block.
9030 Again, this is only performed when the CU language allows this
9031 type of definitions. */
9032
9033 static void
9034 add_partial_subprogram (struct partial_die_info *pdi,
9035 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9036 int set_addrmap, struct dwarf2_cu *cu)
9037 {
9038 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9039 {
9040 if (pdi->has_pc_info)
9041 {
9042 if (pdi->lowpc < *lowpc)
9043 *lowpc = pdi->lowpc;
9044 if (pdi->highpc > *highpc)
9045 *highpc = pdi->highpc;
9046 if (set_addrmap)
9047 {
9048 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9049 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9050 CORE_ADDR baseaddr;
9051 CORE_ADDR highpc;
9052 CORE_ADDR lowpc;
9053
9054 baseaddr = ANOFFSET (objfile->section_offsets,
9055 SECT_OFF_TEXT (objfile));
9056 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9057 pdi->lowpc + baseaddr);
9058 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9059 pdi->highpc + baseaddr);
9060 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9061 cu->per_cu->v.psymtab);
9062 }
9063 }
9064
9065 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9066 {
9067 if (!pdi->is_declaration)
9068 /* Ignore subprogram DIEs that do not have a name, they are
9069 illegal. Do not emit a complaint at this point, we will
9070 do so when we convert this psymtab into a symtab. */
9071 if (pdi->name)
9072 add_partial_symbol (pdi, cu);
9073 }
9074 }
9075
9076 if (! pdi->has_children)
9077 return;
9078
9079 if (cu->language == language_ada)
9080 {
9081 pdi = pdi->die_child;
9082 while (pdi != NULL)
9083 {
9084 pdi->fixup (cu);
9085 if (pdi->tag == DW_TAG_subprogram
9086 || pdi->tag == DW_TAG_inlined_subroutine
9087 || pdi->tag == DW_TAG_lexical_block)
9088 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9089 pdi = pdi->die_sibling;
9090 }
9091 }
9092 }
9093
9094 /* Read a partial die corresponding to an enumeration type. */
9095
9096 static void
9097 add_partial_enumeration (struct partial_die_info *enum_pdi,
9098 struct dwarf2_cu *cu)
9099 {
9100 struct partial_die_info *pdi;
9101
9102 if (enum_pdi->name != NULL)
9103 add_partial_symbol (enum_pdi, cu);
9104
9105 pdi = enum_pdi->die_child;
9106 while (pdi)
9107 {
9108 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9109 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9110 else
9111 add_partial_symbol (pdi, cu);
9112 pdi = pdi->die_sibling;
9113 }
9114 }
9115
9116 /* Return the initial uleb128 in the die at INFO_PTR. */
9117
9118 static unsigned int
9119 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9120 {
9121 unsigned int bytes_read;
9122
9123 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9124 }
9125
9126 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9127 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9128
9129 Return the corresponding abbrev, or NULL if the number is zero (indicating
9130 an empty DIE). In either case *BYTES_READ will be set to the length of
9131 the initial number. */
9132
9133 static struct abbrev_info *
9134 peek_die_abbrev (const die_reader_specs &reader,
9135 const gdb_byte *info_ptr, unsigned int *bytes_read)
9136 {
9137 dwarf2_cu *cu = reader.cu;
9138 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9139 unsigned int abbrev_number
9140 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9141
9142 if (abbrev_number == 0)
9143 return NULL;
9144
9145 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9146 if (!abbrev)
9147 {
9148 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9149 " at offset %s [in module %s]"),
9150 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9151 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9152 }
9153
9154 return abbrev;
9155 }
9156
9157 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9158 Returns a pointer to the end of a series of DIEs, terminated by an empty
9159 DIE. Any children of the skipped DIEs will also be skipped. */
9160
9161 static const gdb_byte *
9162 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9163 {
9164 while (1)
9165 {
9166 unsigned int bytes_read;
9167 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9168
9169 if (abbrev == NULL)
9170 return info_ptr + bytes_read;
9171 else
9172 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9173 }
9174 }
9175
9176 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9177 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9178 abbrev corresponding to that skipped uleb128 should be passed in
9179 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9180 children. */
9181
9182 static const gdb_byte *
9183 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9184 struct abbrev_info *abbrev)
9185 {
9186 unsigned int bytes_read;
9187 struct attribute attr;
9188 bfd *abfd = reader->abfd;
9189 struct dwarf2_cu *cu = reader->cu;
9190 const gdb_byte *buffer = reader->buffer;
9191 const gdb_byte *buffer_end = reader->buffer_end;
9192 unsigned int form, i;
9193
9194 for (i = 0; i < abbrev->num_attrs; i++)
9195 {
9196 /* The only abbrev we care about is DW_AT_sibling. */
9197 if (abbrev->attrs[i].name == DW_AT_sibling)
9198 {
9199 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9200 if (attr.form == DW_FORM_ref_addr)
9201 complaint (&symfile_complaints,
9202 _("ignoring absolute DW_AT_sibling"));
9203 else
9204 {
9205 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9206 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9207
9208 if (sibling_ptr < info_ptr)
9209 complaint (&symfile_complaints,
9210 _("DW_AT_sibling points backwards"));
9211 else if (sibling_ptr > reader->buffer_end)
9212 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9213 else
9214 return sibling_ptr;
9215 }
9216 }
9217
9218 /* If it isn't DW_AT_sibling, skip this attribute. */
9219 form = abbrev->attrs[i].form;
9220 skip_attribute:
9221 switch (form)
9222 {
9223 case DW_FORM_ref_addr:
9224 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9225 and later it is offset sized. */
9226 if (cu->header.version == 2)
9227 info_ptr += cu->header.addr_size;
9228 else
9229 info_ptr += cu->header.offset_size;
9230 break;
9231 case DW_FORM_GNU_ref_alt:
9232 info_ptr += cu->header.offset_size;
9233 break;
9234 case DW_FORM_addr:
9235 info_ptr += cu->header.addr_size;
9236 break;
9237 case DW_FORM_data1:
9238 case DW_FORM_ref1:
9239 case DW_FORM_flag:
9240 info_ptr += 1;
9241 break;
9242 case DW_FORM_flag_present:
9243 case DW_FORM_implicit_const:
9244 break;
9245 case DW_FORM_data2:
9246 case DW_FORM_ref2:
9247 info_ptr += 2;
9248 break;
9249 case DW_FORM_data4:
9250 case DW_FORM_ref4:
9251 info_ptr += 4;
9252 break;
9253 case DW_FORM_data8:
9254 case DW_FORM_ref8:
9255 case DW_FORM_ref_sig8:
9256 info_ptr += 8;
9257 break;
9258 case DW_FORM_data16:
9259 info_ptr += 16;
9260 break;
9261 case DW_FORM_string:
9262 read_direct_string (abfd, info_ptr, &bytes_read);
9263 info_ptr += bytes_read;
9264 break;
9265 case DW_FORM_sec_offset:
9266 case DW_FORM_strp:
9267 case DW_FORM_GNU_strp_alt:
9268 info_ptr += cu->header.offset_size;
9269 break;
9270 case DW_FORM_exprloc:
9271 case DW_FORM_block:
9272 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9273 info_ptr += bytes_read;
9274 break;
9275 case DW_FORM_block1:
9276 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9277 break;
9278 case DW_FORM_block2:
9279 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9280 break;
9281 case DW_FORM_block4:
9282 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9283 break;
9284 case DW_FORM_sdata:
9285 case DW_FORM_udata:
9286 case DW_FORM_ref_udata:
9287 case DW_FORM_GNU_addr_index:
9288 case DW_FORM_GNU_str_index:
9289 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9290 break;
9291 case DW_FORM_indirect:
9292 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9293 info_ptr += bytes_read;
9294 /* We need to continue parsing from here, so just go back to
9295 the top. */
9296 goto skip_attribute;
9297
9298 default:
9299 error (_("Dwarf Error: Cannot handle %s "
9300 "in DWARF reader [in module %s]"),
9301 dwarf_form_name (form),
9302 bfd_get_filename (abfd));
9303 }
9304 }
9305
9306 if (abbrev->has_children)
9307 return skip_children (reader, info_ptr);
9308 else
9309 return info_ptr;
9310 }
9311
9312 /* Locate ORIG_PDI's sibling.
9313 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9314
9315 static const gdb_byte *
9316 locate_pdi_sibling (const struct die_reader_specs *reader,
9317 struct partial_die_info *orig_pdi,
9318 const gdb_byte *info_ptr)
9319 {
9320 /* Do we know the sibling already? */
9321
9322 if (orig_pdi->sibling)
9323 return orig_pdi->sibling;
9324
9325 /* Are there any children to deal with? */
9326
9327 if (!orig_pdi->has_children)
9328 return info_ptr;
9329
9330 /* Skip the children the long way. */
9331
9332 return skip_children (reader, info_ptr);
9333 }
9334
9335 /* Expand this partial symbol table into a full symbol table. SELF is
9336 not NULL. */
9337
9338 static void
9339 dwarf2_read_symtab (struct partial_symtab *self,
9340 struct objfile *objfile)
9341 {
9342 struct dwarf2_per_objfile *dwarf2_per_objfile
9343 = get_dwarf2_per_objfile (objfile);
9344
9345 if (self->readin)
9346 {
9347 warning (_("bug: psymtab for %s is already read in."),
9348 self->filename);
9349 }
9350 else
9351 {
9352 if (info_verbose)
9353 {
9354 printf_filtered (_("Reading in symbols for %s..."),
9355 self->filename);
9356 gdb_flush (gdb_stdout);
9357 }
9358
9359 /* If this psymtab is constructed from a debug-only objfile, the
9360 has_section_at_zero flag will not necessarily be correct. We
9361 can get the correct value for this flag by looking at the data
9362 associated with the (presumably stripped) associated objfile. */
9363 if (objfile->separate_debug_objfile_backlink)
9364 {
9365 struct dwarf2_per_objfile *dpo_backlink
9366 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9367
9368 dwarf2_per_objfile->has_section_at_zero
9369 = dpo_backlink->has_section_at_zero;
9370 }
9371
9372 dwarf2_per_objfile->reading_partial_symbols = 0;
9373
9374 psymtab_to_symtab_1 (self);
9375
9376 /* Finish up the debug error message. */
9377 if (info_verbose)
9378 printf_filtered (_("done.\n"));
9379 }
9380
9381 process_cu_includes (dwarf2_per_objfile);
9382 }
9383 \f
9384 /* Reading in full CUs. */
9385
9386 /* Add PER_CU to the queue. */
9387
9388 static void
9389 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9390 enum language pretend_language)
9391 {
9392 struct dwarf2_queue_item *item;
9393
9394 per_cu->queued = 1;
9395 item = XNEW (struct dwarf2_queue_item);
9396 item->per_cu = per_cu;
9397 item->pretend_language = pretend_language;
9398 item->next = NULL;
9399
9400 if (dwarf2_queue == NULL)
9401 dwarf2_queue = item;
9402 else
9403 dwarf2_queue_tail->next = item;
9404
9405 dwarf2_queue_tail = item;
9406 }
9407
9408 /* If PER_CU is not yet queued, add it to the queue.
9409 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9410 dependency.
9411 The result is non-zero if PER_CU was queued, otherwise the result is zero
9412 meaning either PER_CU is already queued or it is already loaded.
9413
9414 N.B. There is an invariant here that if a CU is queued then it is loaded.
9415 The caller is required to load PER_CU if we return non-zero. */
9416
9417 static int
9418 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9419 struct dwarf2_per_cu_data *per_cu,
9420 enum language pretend_language)
9421 {
9422 /* We may arrive here during partial symbol reading, if we need full
9423 DIEs to process an unusual case (e.g. template arguments). Do
9424 not queue PER_CU, just tell our caller to load its DIEs. */
9425 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9426 {
9427 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9428 return 1;
9429 return 0;
9430 }
9431
9432 /* Mark the dependence relation so that we don't flush PER_CU
9433 too early. */
9434 if (dependent_cu != NULL)
9435 dwarf2_add_dependence (dependent_cu, per_cu);
9436
9437 /* If it's already on the queue, we have nothing to do. */
9438 if (per_cu->queued)
9439 return 0;
9440
9441 /* If the compilation unit is already loaded, just mark it as
9442 used. */
9443 if (per_cu->cu != NULL)
9444 {
9445 per_cu->cu->last_used = 0;
9446 return 0;
9447 }
9448
9449 /* Add it to the queue. */
9450 queue_comp_unit (per_cu, pretend_language);
9451
9452 return 1;
9453 }
9454
9455 /* Process the queue. */
9456
9457 static void
9458 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9459 {
9460 struct dwarf2_queue_item *item, *next_item;
9461
9462 if (dwarf_read_debug)
9463 {
9464 fprintf_unfiltered (gdb_stdlog,
9465 "Expanding one or more symtabs of objfile %s ...\n",
9466 objfile_name (dwarf2_per_objfile->objfile));
9467 }
9468
9469 /* The queue starts out with one item, but following a DIE reference
9470 may load a new CU, adding it to the end of the queue. */
9471 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9472 {
9473 if ((dwarf2_per_objfile->using_index
9474 ? !item->per_cu->v.quick->compunit_symtab
9475 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9476 /* Skip dummy CUs. */
9477 && item->per_cu->cu != NULL)
9478 {
9479 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9480 unsigned int debug_print_threshold;
9481 char buf[100];
9482
9483 if (per_cu->is_debug_types)
9484 {
9485 struct signatured_type *sig_type =
9486 (struct signatured_type *) per_cu;
9487
9488 sprintf (buf, "TU %s at offset %s",
9489 hex_string (sig_type->signature),
9490 sect_offset_str (per_cu->sect_off));
9491 /* There can be 100s of TUs.
9492 Only print them in verbose mode. */
9493 debug_print_threshold = 2;
9494 }
9495 else
9496 {
9497 sprintf (buf, "CU at offset %s",
9498 sect_offset_str (per_cu->sect_off));
9499 debug_print_threshold = 1;
9500 }
9501
9502 if (dwarf_read_debug >= debug_print_threshold)
9503 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9504
9505 if (per_cu->is_debug_types)
9506 process_full_type_unit (per_cu, item->pretend_language);
9507 else
9508 process_full_comp_unit (per_cu, item->pretend_language);
9509
9510 if (dwarf_read_debug >= debug_print_threshold)
9511 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9512 }
9513
9514 item->per_cu->queued = 0;
9515 next_item = item->next;
9516 xfree (item);
9517 }
9518
9519 dwarf2_queue_tail = NULL;
9520
9521 if (dwarf_read_debug)
9522 {
9523 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9524 objfile_name (dwarf2_per_objfile->objfile));
9525 }
9526 }
9527
9528 /* Read in full symbols for PST, and anything it depends on. */
9529
9530 static void
9531 psymtab_to_symtab_1 (struct partial_symtab *pst)
9532 {
9533 struct dwarf2_per_cu_data *per_cu;
9534 int i;
9535
9536 if (pst->readin)
9537 return;
9538
9539 for (i = 0; i < pst->number_of_dependencies; i++)
9540 if (!pst->dependencies[i]->readin
9541 && pst->dependencies[i]->user == NULL)
9542 {
9543 /* Inform about additional files that need to be read in. */
9544 if (info_verbose)
9545 {
9546 /* FIXME: i18n: Need to make this a single string. */
9547 fputs_filtered (" ", gdb_stdout);
9548 wrap_here ("");
9549 fputs_filtered ("and ", gdb_stdout);
9550 wrap_here ("");
9551 printf_filtered ("%s...", pst->dependencies[i]->filename);
9552 wrap_here (""); /* Flush output. */
9553 gdb_flush (gdb_stdout);
9554 }
9555 psymtab_to_symtab_1 (pst->dependencies[i]);
9556 }
9557
9558 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9559
9560 if (per_cu == NULL)
9561 {
9562 /* It's an include file, no symbols to read for it.
9563 Everything is in the parent symtab. */
9564 pst->readin = 1;
9565 return;
9566 }
9567
9568 dw2_do_instantiate_symtab (per_cu, false);
9569 }
9570
9571 /* Trivial hash function for die_info: the hash value of a DIE
9572 is its offset in .debug_info for this objfile. */
9573
9574 static hashval_t
9575 die_hash (const void *item)
9576 {
9577 const struct die_info *die = (const struct die_info *) item;
9578
9579 return to_underlying (die->sect_off);
9580 }
9581
9582 /* Trivial comparison function for die_info structures: two DIEs
9583 are equal if they have the same offset. */
9584
9585 static int
9586 die_eq (const void *item_lhs, const void *item_rhs)
9587 {
9588 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9589 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9590
9591 return die_lhs->sect_off == die_rhs->sect_off;
9592 }
9593
9594 /* die_reader_func for load_full_comp_unit.
9595 This is identical to read_signatured_type_reader,
9596 but is kept separate for now. */
9597
9598 static void
9599 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9600 const gdb_byte *info_ptr,
9601 struct die_info *comp_unit_die,
9602 int has_children,
9603 void *data)
9604 {
9605 struct dwarf2_cu *cu = reader->cu;
9606 enum language *language_ptr = (enum language *) data;
9607
9608 gdb_assert (cu->die_hash == NULL);
9609 cu->die_hash =
9610 htab_create_alloc_ex (cu->header.length / 12,
9611 die_hash,
9612 die_eq,
9613 NULL,
9614 &cu->comp_unit_obstack,
9615 hashtab_obstack_allocate,
9616 dummy_obstack_deallocate);
9617
9618 if (has_children)
9619 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9620 &info_ptr, comp_unit_die);
9621 cu->dies = comp_unit_die;
9622 /* comp_unit_die is not stored in die_hash, no need. */
9623
9624 /* We try not to read any attributes in this function, because not
9625 all CUs needed for references have been loaded yet, and symbol
9626 table processing isn't initialized. But we have to set the CU language,
9627 or we won't be able to build types correctly.
9628 Similarly, if we do not read the producer, we can not apply
9629 producer-specific interpretation. */
9630 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9631 }
9632
9633 /* Load the DIEs associated with PER_CU into memory. */
9634
9635 static void
9636 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9637 bool skip_partial,
9638 enum language pretend_language)
9639 {
9640 gdb_assert (! this_cu->is_debug_types);
9641
9642 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9643 load_full_comp_unit_reader, &pretend_language);
9644 }
9645
9646 /* Add a DIE to the delayed physname list. */
9647
9648 static void
9649 add_to_method_list (struct type *type, int fnfield_index, int index,
9650 const char *name, struct die_info *die,
9651 struct dwarf2_cu *cu)
9652 {
9653 struct delayed_method_info mi;
9654 mi.type = type;
9655 mi.fnfield_index = fnfield_index;
9656 mi.index = index;
9657 mi.name = name;
9658 mi.die = die;
9659 cu->method_list.push_back (mi);
9660 }
9661
9662 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9663 "const" / "volatile". If so, decrements LEN by the length of the
9664 modifier and return true. Otherwise return false. */
9665
9666 template<size_t N>
9667 static bool
9668 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9669 {
9670 size_t mod_len = sizeof (mod) - 1;
9671 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9672 {
9673 len -= mod_len;
9674 return true;
9675 }
9676 return false;
9677 }
9678
9679 /* Compute the physnames of any methods on the CU's method list.
9680
9681 The computation of method physnames is delayed in order to avoid the
9682 (bad) condition that one of the method's formal parameters is of an as yet
9683 incomplete type. */
9684
9685 static void
9686 compute_delayed_physnames (struct dwarf2_cu *cu)
9687 {
9688 /* Only C++ delays computing physnames. */
9689 if (cu->method_list.empty ())
9690 return;
9691 gdb_assert (cu->language == language_cplus);
9692
9693 for (struct delayed_method_info &mi : cu->method_list)
9694 {
9695 const char *physname;
9696 struct fn_fieldlist *fn_flp
9697 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9698 physname = dwarf2_physname (mi.name, mi.die, cu);
9699 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9700 = physname ? physname : "";
9701
9702 /* Since there's no tag to indicate whether a method is a
9703 const/volatile overload, extract that information out of the
9704 demangled name. */
9705 if (physname != NULL)
9706 {
9707 size_t len = strlen (physname);
9708
9709 while (1)
9710 {
9711 if (physname[len] == ')') /* shortcut */
9712 break;
9713 else if (check_modifier (physname, len, " const"))
9714 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9715 else if (check_modifier (physname, len, " volatile"))
9716 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9717 else
9718 break;
9719 }
9720 }
9721 }
9722
9723 /* The list is no longer needed. */
9724 cu->method_list.clear ();
9725 }
9726
9727 /* Go objects should be embedded in a DW_TAG_module DIE,
9728 and it's not clear if/how imported objects will appear.
9729 To keep Go support simple until that's worked out,
9730 go back through what we've read and create something usable.
9731 We could do this while processing each DIE, and feels kinda cleaner,
9732 but that way is more invasive.
9733 This is to, for example, allow the user to type "p var" or "b main"
9734 without having to specify the package name, and allow lookups
9735 of module.object to work in contexts that use the expression
9736 parser. */
9737
9738 static void
9739 fixup_go_packaging (struct dwarf2_cu *cu)
9740 {
9741 char *package_name = NULL;
9742 struct pending *list;
9743 int i;
9744
9745 for (list = global_symbols; list != NULL; list = list->next)
9746 {
9747 for (i = 0; i < list->nsyms; ++i)
9748 {
9749 struct symbol *sym = list->symbol[i];
9750
9751 if (SYMBOL_LANGUAGE (sym) == language_go
9752 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9753 {
9754 char *this_package_name = go_symbol_package_name (sym);
9755
9756 if (this_package_name == NULL)
9757 continue;
9758 if (package_name == NULL)
9759 package_name = this_package_name;
9760 else
9761 {
9762 struct objfile *objfile
9763 = cu->per_cu->dwarf2_per_objfile->objfile;
9764 if (strcmp (package_name, this_package_name) != 0)
9765 complaint (&symfile_complaints,
9766 _("Symtab %s has objects from two different Go packages: %s and %s"),
9767 (symbol_symtab (sym) != NULL
9768 ? symtab_to_filename_for_display
9769 (symbol_symtab (sym))
9770 : objfile_name (objfile)),
9771 this_package_name, package_name);
9772 xfree (this_package_name);
9773 }
9774 }
9775 }
9776 }
9777
9778 if (package_name != NULL)
9779 {
9780 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9781 const char *saved_package_name
9782 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9783 package_name,
9784 strlen (package_name));
9785 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9786 saved_package_name);
9787 struct symbol *sym;
9788
9789 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9790
9791 sym = allocate_symbol (objfile);
9792 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9793 SYMBOL_SET_NAMES (sym, saved_package_name,
9794 strlen (saved_package_name), 0, objfile);
9795 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9796 e.g., "main" finds the "main" module and not C's main(). */
9797 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9798 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9799 SYMBOL_TYPE (sym) = type;
9800
9801 add_symbol_to_list (sym, &global_symbols);
9802
9803 xfree (package_name);
9804 }
9805 }
9806
9807 /* Allocate a fully-qualified name consisting of the two parts on the
9808 obstack. */
9809
9810 static const char *
9811 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9812 {
9813 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9814 }
9815
9816 /* A helper that allocates a struct discriminant_info to attach to a
9817 union type. */
9818
9819 static struct discriminant_info *
9820 alloc_discriminant_info (struct type *type, int discriminant_index,
9821 int default_index)
9822 {
9823 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9824 gdb_assert (discriminant_index == -1
9825 || (discriminant_index >= 0
9826 && discriminant_index < TYPE_NFIELDS (type)));
9827 gdb_assert (default_index == -1
9828 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9829
9830 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9831
9832 struct discriminant_info *disc
9833 = ((struct discriminant_info *)
9834 TYPE_ZALLOC (type,
9835 offsetof (struct discriminant_info, discriminants)
9836 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9837 disc->default_index = default_index;
9838 disc->discriminant_index = discriminant_index;
9839
9840 struct dynamic_prop prop;
9841 prop.kind = PROP_UNDEFINED;
9842 prop.data.baton = disc;
9843
9844 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9845
9846 return disc;
9847 }
9848
9849 /* Some versions of rustc emitted enums in an unusual way.
9850
9851 Ordinary enums were emitted as unions. The first element of each
9852 structure in the union was named "RUST$ENUM$DISR". This element
9853 held the discriminant.
9854
9855 These versions of Rust also implemented the "non-zero"
9856 optimization. When the enum had two values, and one is empty and
9857 the other holds a pointer that cannot be zero, the pointer is used
9858 as the discriminant, with a zero value meaning the empty variant.
9859 Here, the union's first member is of the form
9860 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9861 where the fieldnos are the indices of the fields that should be
9862 traversed in order to find the field (which may be several fields deep)
9863 and the variantname is the name of the variant of the case when the
9864 field is zero.
9865
9866 This function recognizes whether TYPE is of one of these forms,
9867 and, if so, smashes it to be a variant type. */
9868
9869 static void
9870 quirk_rust_enum (struct type *type, struct objfile *objfile)
9871 {
9872 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9873
9874 /* We don't need to deal with empty enums. */
9875 if (TYPE_NFIELDS (type) == 0)
9876 return;
9877
9878 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9879 if (TYPE_NFIELDS (type) == 1
9880 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9881 {
9882 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9883
9884 /* Decode the field name to find the offset of the
9885 discriminant. */
9886 ULONGEST bit_offset = 0;
9887 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9888 while (name[0] >= '0' && name[0] <= '9')
9889 {
9890 char *tail;
9891 unsigned long index = strtoul (name, &tail, 10);
9892 name = tail;
9893 if (*name != '$'
9894 || index >= TYPE_NFIELDS (field_type)
9895 || (TYPE_FIELD_LOC_KIND (field_type, index)
9896 != FIELD_LOC_KIND_BITPOS))
9897 {
9898 complaint (&symfile_complaints,
9899 _("Could not parse Rust enum encoding string \"%s\""
9900 "[in module %s]"),
9901 TYPE_FIELD_NAME (type, 0),
9902 objfile_name (objfile));
9903 return;
9904 }
9905 ++name;
9906
9907 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9908 field_type = TYPE_FIELD_TYPE (field_type, index);
9909 }
9910
9911 /* Make a union to hold the variants. */
9912 struct type *union_type = alloc_type (objfile);
9913 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9914 TYPE_NFIELDS (union_type) = 3;
9915 TYPE_FIELDS (union_type)
9916 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9917 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9918 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9919
9920 /* Put the discriminant must at index 0. */
9921 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9922 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9923 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9924 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9925
9926 /* The order of fields doesn't really matter, so put the real
9927 field at index 1 and the data-less field at index 2. */
9928 struct discriminant_info *disc
9929 = alloc_discriminant_info (union_type, 0, 1);
9930 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9931 TYPE_FIELD_NAME (union_type, 1)
9932 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9933 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9934 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9935 TYPE_FIELD_NAME (union_type, 1));
9936
9937 const char *dataless_name
9938 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9939 name);
9940 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9941 dataless_name);
9942 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9943 /* NAME points into the original discriminant name, which
9944 already has the correct lifetime. */
9945 TYPE_FIELD_NAME (union_type, 2) = name;
9946 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9947 disc->discriminants[2] = 0;
9948
9949 /* Smash this type to be a structure type. We have to do this
9950 because the type has already been recorded. */
9951 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9952 TYPE_NFIELDS (type) = 1;
9953 TYPE_FIELDS (type)
9954 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9955
9956 /* Install the variant part. */
9957 TYPE_FIELD_TYPE (type, 0) = union_type;
9958 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9959 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9960 }
9961 else if (TYPE_NFIELDS (type) == 1)
9962 {
9963 /* We assume that a union with a single field is a univariant
9964 enum. */
9965 /* Smash this type to be a structure type. We have to do this
9966 because the type has already been recorded. */
9967 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9968
9969 /* Make a union to hold the variants. */
9970 struct type *union_type = alloc_type (objfile);
9971 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9972 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9973 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9974 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9975 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9976
9977 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9978 const char *variant_name
9979 = rust_last_path_segment (TYPE_NAME (field_type));
9980 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9981 TYPE_NAME (field_type)
9982 = rust_fully_qualify (&objfile->objfile_obstack,
9983 TYPE_NAME (type), variant_name);
9984
9985 /* Install the union in the outer struct type. */
9986 TYPE_NFIELDS (type) = 1;
9987 TYPE_FIELDS (type)
9988 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9989 TYPE_FIELD_TYPE (type, 0) = union_type;
9990 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9991 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9992
9993 alloc_discriminant_info (union_type, -1, 0);
9994 }
9995 else
9996 {
9997 struct type *disr_type = nullptr;
9998 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9999 {
10000 disr_type = TYPE_FIELD_TYPE (type, i);
10001
10002 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10003 {
10004 /* All fields of a true enum will be structs. */
10005 return;
10006 }
10007 else if (TYPE_NFIELDS (disr_type) == 0)
10008 {
10009 /* Could be data-less variant, so keep going. */
10010 disr_type = nullptr;
10011 }
10012 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10013 "RUST$ENUM$DISR") != 0)
10014 {
10015 /* Not a Rust enum. */
10016 return;
10017 }
10018 else
10019 {
10020 /* Found one. */
10021 break;
10022 }
10023 }
10024
10025 /* If we got here without a discriminant, then it's probably
10026 just a union. */
10027 if (disr_type == nullptr)
10028 return;
10029
10030 /* Smash this type to be a structure type. We have to do this
10031 because the type has already been recorded. */
10032 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10033
10034 /* Make a union to hold the variants. */
10035 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10036 struct type *union_type = alloc_type (objfile);
10037 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10038 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10039 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10040 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10041 TYPE_FIELDS (union_type)
10042 = (struct field *) TYPE_ZALLOC (union_type,
10043 (TYPE_NFIELDS (union_type)
10044 * sizeof (struct field)));
10045
10046 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10047 TYPE_NFIELDS (type) * sizeof (struct field));
10048
10049 /* Install the discriminant at index 0 in the union. */
10050 TYPE_FIELD (union_type, 0) = *disr_field;
10051 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10052 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10053
10054 /* Install the union in the outer struct type. */
10055 TYPE_FIELD_TYPE (type, 0) = union_type;
10056 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10057 TYPE_NFIELDS (type) = 1;
10058
10059 /* Set the size and offset of the union type. */
10060 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10061
10062 /* We need a way to find the correct discriminant given a
10063 variant name. For convenience we build a map here. */
10064 struct type *enum_type = FIELD_TYPE (*disr_field);
10065 std::unordered_map<std::string, ULONGEST> discriminant_map;
10066 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10067 {
10068 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10069 {
10070 const char *name
10071 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10072 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10073 }
10074 }
10075
10076 int n_fields = TYPE_NFIELDS (union_type);
10077 struct discriminant_info *disc
10078 = alloc_discriminant_info (union_type, 0, -1);
10079 /* Skip the discriminant here. */
10080 for (int i = 1; i < n_fields; ++i)
10081 {
10082 /* Find the final word in the name of this variant's type.
10083 That name can be used to look up the correct
10084 discriminant. */
10085 const char *variant_name
10086 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10087 i)));
10088
10089 auto iter = discriminant_map.find (variant_name);
10090 if (iter != discriminant_map.end ())
10091 disc->discriminants[i] = iter->second;
10092
10093 /* Remove the discriminant field, if it exists. */
10094 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10095 if (TYPE_NFIELDS (sub_type) > 0)
10096 {
10097 --TYPE_NFIELDS (sub_type);
10098 ++TYPE_FIELDS (sub_type);
10099 }
10100 TYPE_FIELD_NAME (union_type, i) = variant_name;
10101 TYPE_NAME (sub_type)
10102 = rust_fully_qualify (&objfile->objfile_obstack,
10103 TYPE_NAME (type), variant_name);
10104 }
10105 }
10106 }
10107
10108 /* Rewrite some Rust unions to be structures with variants parts. */
10109
10110 static void
10111 rust_union_quirks (struct dwarf2_cu *cu)
10112 {
10113 gdb_assert (cu->language == language_rust);
10114 for (struct type *type : cu->rust_unions)
10115 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10116 /* We don't need this any more. */
10117 cu->rust_unions.clear ();
10118 }
10119
10120 /* Return the symtab for PER_CU. This works properly regardless of
10121 whether we're using the index or psymtabs. */
10122
10123 static struct compunit_symtab *
10124 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10125 {
10126 return (per_cu->dwarf2_per_objfile->using_index
10127 ? per_cu->v.quick->compunit_symtab
10128 : per_cu->v.psymtab->compunit_symtab);
10129 }
10130
10131 /* A helper function for computing the list of all symbol tables
10132 included by PER_CU. */
10133
10134 static void
10135 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10136 htab_t all_children, htab_t all_type_symtabs,
10137 struct dwarf2_per_cu_data *per_cu,
10138 struct compunit_symtab *immediate_parent)
10139 {
10140 void **slot;
10141 int ix;
10142 struct compunit_symtab *cust;
10143 struct dwarf2_per_cu_data *iter;
10144
10145 slot = htab_find_slot (all_children, per_cu, INSERT);
10146 if (*slot != NULL)
10147 {
10148 /* This inclusion and its children have been processed. */
10149 return;
10150 }
10151
10152 *slot = per_cu;
10153 /* Only add a CU if it has a symbol table. */
10154 cust = get_compunit_symtab (per_cu);
10155 if (cust != NULL)
10156 {
10157 /* If this is a type unit only add its symbol table if we haven't
10158 seen it yet (type unit per_cu's can share symtabs). */
10159 if (per_cu->is_debug_types)
10160 {
10161 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10162 if (*slot == NULL)
10163 {
10164 *slot = cust;
10165 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10166 if (cust->user == NULL)
10167 cust->user = immediate_parent;
10168 }
10169 }
10170 else
10171 {
10172 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10173 if (cust->user == NULL)
10174 cust->user = immediate_parent;
10175 }
10176 }
10177
10178 for (ix = 0;
10179 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10180 ++ix)
10181 {
10182 recursively_compute_inclusions (result, all_children,
10183 all_type_symtabs, iter, cust);
10184 }
10185 }
10186
10187 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10188 PER_CU. */
10189
10190 static void
10191 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10192 {
10193 gdb_assert (! per_cu->is_debug_types);
10194
10195 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10196 {
10197 int ix, len;
10198 struct dwarf2_per_cu_data *per_cu_iter;
10199 struct compunit_symtab *compunit_symtab_iter;
10200 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10201 htab_t all_children, all_type_symtabs;
10202 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10203
10204 /* If we don't have a symtab, we can just skip this case. */
10205 if (cust == NULL)
10206 return;
10207
10208 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10209 NULL, xcalloc, xfree);
10210 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10211 NULL, xcalloc, xfree);
10212
10213 for (ix = 0;
10214 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10215 ix, per_cu_iter);
10216 ++ix)
10217 {
10218 recursively_compute_inclusions (&result_symtabs, all_children,
10219 all_type_symtabs, per_cu_iter,
10220 cust);
10221 }
10222
10223 /* Now we have a transitive closure of all the included symtabs. */
10224 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10225 cust->includes
10226 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10227 struct compunit_symtab *, len + 1);
10228 for (ix = 0;
10229 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10230 compunit_symtab_iter);
10231 ++ix)
10232 cust->includes[ix] = compunit_symtab_iter;
10233 cust->includes[len] = NULL;
10234
10235 VEC_free (compunit_symtab_ptr, result_symtabs);
10236 htab_delete (all_children);
10237 htab_delete (all_type_symtabs);
10238 }
10239 }
10240
10241 /* Compute the 'includes' field for the symtabs of all the CUs we just
10242 read. */
10243
10244 static void
10245 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10246 {
10247 int ix;
10248 struct dwarf2_per_cu_data *iter;
10249
10250 for (ix = 0;
10251 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10252 ix, iter);
10253 ++ix)
10254 {
10255 if (! iter->is_debug_types)
10256 compute_compunit_symtab_includes (iter);
10257 }
10258
10259 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10260 }
10261
10262 /* Generate full symbol information for PER_CU, whose DIEs have
10263 already been loaded into memory. */
10264
10265 static void
10266 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10267 enum language pretend_language)
10268 {
10269 struct dwarf2_cu *cu = per_cu->cu;
10270 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10271 struct objfile *objfile = dwarf2_per_objfile->objfile;
10272 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10273 CORE_ADDR lowpc, highpc;
10274 struct compunit_symtab *cust;
10275 CORE_ADDR baseaddr;
10276 struct block *static_block;
10277 CORE_ADDR addr;
10278
10279 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10280
10281 buildsym_init ();
10282 scoped_free_pendings free_pending;
10283
10284 /* Clear the list here in case something was left over. */
10285 cu->method_list.clear ();
10286
10287 cu->list_in_scope = &file_symbols;
10288
10289 cu->language = pretend_language;
10290 cu->language_defn = language_def (cu->language);
10291
10292 /* Do line number decoding in read_file_scope () */
10293 process_die (cu->dies, cu);
10294
10295 /* For now fudge the Go package. */
10296 if (cu->language == language_go)
10297 fixup_go_packaging (cu);
10298
10299 /* Now that we have processed all the DIEs in the CU, all the types
10300 should be complete, and it should now be safe to compute all of the
10301 physnames. */
10302 compute_delayed_physnames (cu);
10303
10304 if (cu->language == language_rust)
10305 rust_union_quirks (cu);
10306
10307 /* Some compilers don't define a DW_AT_high_pc attribute for the
10308 compilation unit. If the DW_AT_high_pc is missing, synthesize
10309 it, by scanning the DIE's below the compilation unit. */
10310 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10311
10312 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10313 static_block = end_symtab_get_static_block (addr, 0, 1);
10314
10315 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10316 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10317 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10318 addrmap to help ensure it has an accurate map of pc values belonging to
10319 this comp unit. */
10320 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10321
10322 cust = end_symtab_from_static_block (static_block,
10323 SECT_OFF_TEXT (objfile), 0);
10324
10325 if (cust != NULL)
10326 {
10327 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10328
10329 /* Set symtab language to language from DW_AT_language. If the
10330 compilation is from a C file generated by language preprocessors, do
10331 not set the language if it was already deduced by start_subfile. */
10332 if (!(cu->language == language_c
10333 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10334 COMPUNIT_FILETABS (cust)->language = cu->language;
10335
10336 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10337 produce DW_AT_location with location lists but it can be possibly
10338 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10339 there were bugs in prologue debug info, fixed later in GCC-4.5
10340 by "unwind info for epilogues" patch (which is not directly related).
10341
10342 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10343 needed, it would be wrong due to missing DW_AT_producer there.
10344
10345 Still one can confuse GDB by using non-standard GCC compilation
10346 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10347 */
10348 if (cu->has_loclist && gcc_4_minor >= 5)
10349 cust->locations_valid = 1;
10350
10351 if (gcc_4_minor >= 5)
10352 cust->epilogue_unwind_valid = 1;
10353
10354 cust->call_site_htab = cu->call_site_htab;
10355 }
10356
10357 if (dwarf2_per_objfile->using_index)
10358 per_cu->v.quick->compunit_symtab = cust;
10359 else
10360 {
10361 struct partial_symtab *pst = per_cu->v.psymtab;
10362 pst->compunit_symtab = cust;
10363 pst->readin = 1;
10364 }
10365
10366 /* Push it for inclusion processing later. */
10367 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10368 }
10369
10370 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10371 already been loaded into memory. */
10372
10373 static void
10374 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10375 enum language pretend_language)
10376 {
10377 struct dwarf2_cu *cu = per_cu->cu;
10378 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10379 struct objfile *objfile = dwarf2_per_objfile->objfile;
10380 struct compunit_symtab *cust;
10381 struct signatured_type *sig_type;
10382
10383 gdb_assert (per_cu->is_debug_types);
10384 sig_type = (struct signatured_type *) per_cu;
10385
10386 buildsym_init ();
10387 scoped_free_pendings free_pending;
10388
10389 /* Clear the list here in case something was left over. */
10390 cu->method_list.clear ();
10391
10392 cu->list_in_scope = &file_symbols;
10393
10394 cu->language = pretend_language;
10395 cu->language_defn = language_def (cu->language);
10396
10397 /* The symbol tables are set up in read_type_unit_scope. */
10398 process_die (cu->dies, cu);
10399
10400 /* For now fudge the Go package. */
10401 if (cu->language == language_go)
10402 fixup_go_packaging (cu);
10403
10404 /* Now that we have processed all the DIEs in the CU, all the types
10405 should be complete, and it should now be safe to compute all of the
10406 physnames. */
10407 compute_delayed_physnames (cu);
10408
10409 if (cu->language == language_rust)
10410 rust_union_quirks (cu);
10411
10412 /* TUs share symbol tables.
10413 If this is the first TU to use this symtab, complete the construction
10414 of it with end_expandable_symtab. Otherwise, complete the addition of
10415 this TU's symbols to the existing symtab. */
10416 if (sig_type->type_unit_group->compunit_symtab == NULL)
10417 {
10418 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10419 sig_type->type_unit_group->compunit_symtab = cust;
10420
10421 if (cust != NULL)
10422 {
10423 /* Set symtab language to language from DW_AT_language. If the
10424 compilation is from a C file generated by language preprocessors,
10425 do not set the language if it was already deduced by
10426 start_subfile. */
10427 if (!(cu->language == language_c
10428 && COMPUNIT_FILETABS (cust)->language != language_c))
10429 COMPUNIT_FILETABS (cust)->language = cu->language;
10430 }
10431 }
10432 else
10433 {
10434 augment_type_symtab ();
10435 cust = sig_type->type_unit_group->compunit_symtab;
10436 }
10437
10438 if (dwarf2_per_objfile->using_index)
10439 per_cu->v.quick->compunit_symtab = cust;
10440 else
10441 {
10442 struct partial_symtab *pst = per_cu->v.psymtab;
10443 pst->compunit_symtab = cust;
10444 pst->readin = 1;
10445 }
10446 }
10447
10448 /* Process an imported unit DIE. */
10449
10450 static void
10451 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10452 {
10453 struct attribute *attr;
10454
10455 /* For now we don't handle imported units in type units. */
10456 if (cu->per_cu->is_debug_types)
10457 {
10458 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10459 " supported in type units [in module %s]"),
10460 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10461 }
10462
10463 attr = dwarf2_attr (die, DW_AT_import, cu);
10464 if (attr != NULL)
10465 {
10466 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10467 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10468 dwarf2_per_cu_data *per_cu
10469 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10470 cu->per_cu->dwarf2_per_objfile);
10471
10472 /* If necessary, add it to the queue and load its DIEs. */
10473 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10474 load_full_comp_unit (per_cu, false, cu->language);
10475
10476 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10477 per_cu);
10478 }
10479 }
10480
10481 /* RAII object that represents a process_die scope: i.e.,
10482 starts/finishes processing a DIE. */
10483 class process_die_scope
10484 {
10485 public:
10486 process_die_scope (die_info *die, dwarf2_cu *cu)
10487 : m_die (die), m_cu (cu)
10488 {
10489 /* We should only be processing DIEs not already in process. */
10490 gdb_assert (!m_die->in_process);
10491 m_die->in_process = true;
10492 }
10493
10494 ~process_die_scope ()
10495 {
10496 m_die->in_process = false;
10497
10498 /* If we're done processing the DIE for the CU that owns the line
10499 header, we don't need the line header anymore. */
10500 if (m_cu->line_header_die_owner == m_die)
10501 {
10502 delete m_cu->line_header;
10503 m_cu->line_header = NULL;
10504 m_cu->line_header_die_owner = NULL;
10505 }
10506 }
10507
10508 private:
10509 die_info *m_die;
10510 dwarf2_cu *m_cu;
10511 };
10512
10513 /* Process a die and its children. */
10514
10515 static void
10516 process_die (struct die_info *die, struct dwarf2_cu *cu)
10517 {
10518 process_die_scope scope (die, cu);
10519
10520 switch (die->tag)
10521 {
10522 case DW_TAG_padding:
10523 break;
10524 case DW_TAG_compile_unit:
10525 case DW_TAG_partial_unit:
10526 read_file_scope (die, cu);
10527 break;
10528 case DW_TAG_type_unit:
10529 read_type_unit_scope (die, cu);
10530 break;
10531 case DW_TAG_subprogram:
10532 case DW_TAG_inlined_subroutine:
10533 read_func_scope (die, cu);
10534 break;
10535 case DW_TAG_lexical_block:
10536 case DW_TAG_try_block:
10537 case DW_TAG_catch_block:
10538 read_lexical_block_scope (die, cu);
10539 break;
10540 case DW_TAG_call_site:
10541 case DW_TAG_GNU_call_site:
10542 read_call_site_scope (die, cu);
10543 break;
10544 case DW_TAG_class_type:
10545 case DW_TAG_interface_type:
10546 case DW_TAG_structure_type:
10547 case DW_TAG_union_type:
10548 process_structure_scope (die, cu);
10549 break;
10550 case DW_TAG_enumeration_type:
10551 process_enumeration_scope (die, cu);
10552 break;
10553
10554 /* These dies have a type, but processing them does not create
10555 a symbol or recurse to process the children. Therefore we can
10556 read them on-demand through read_type_die. */
10557 case DW_TAG_subroutine_type:
10558 case DW_TAG_set_type:
10559 case DW_TAG_array_type:
10560 case DW_TAG_pointer_type:
10561 case DW_TAG_ptr_to_member_type:
10562 case DW_TAG_reference_type:
10563 case DW_TAG_rvalue_reference_type:
10564 case DW_TAG_string_type:
10565 break;
10566
10567 case DW_TAG_base_type:
10568 case DW_TAG_subrange_type:
10569 case DW_TAG_typedef:
10570 /* Add a typedef symbol for the type definition, if it has a
10571 DW_AT_name. */
10572 new_symbol (die, read_type_die (die, cu), cu);
10573 break;
10574 case DW_TAG_common_block:
10575 read_common_block (die, cu);
10576 break;
10577 case DW_TAG_common_inclusion:
10578 break;
10579 case DW_TAG_namespace:
10580 cu->processing_has_namespace_info = 1;
10581 read_namespace (die, cu);
10582 break;
10583 case DW_TAG_module:
10584 cu->processing_has_namespace_info = 1;
10585 read_module (die, cu);
10586 break;
10587 case DW_TAG_imported_declaration:
10588 cu->processing_has_namespace_info = 1;
10589 if (read_namespace_alias (die, cu))
10590 break;
10591 /* The declaration is not a global namespace alias. */
10592 /* Fall through. */
10593 case DW_TAG_imported_module:
10594 cu->processing_has_namespace_info = 1;
10595 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10596 || cu->language != language_fortran))
10597 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10598 dwarf_tag_name (die->tag));
10599 read_import_statement (die, cu);
10600 break;
10601
10602 case DW_TAG_imported_unit:
10603 process_imported_unit_die (die, cu);
10604 break;
10605
10606 case DW_TAG_variable:
10607 read_variable (die, cu);
10608 break;
10609
10610 default:
10611 new_symbol (die, NULL, cu);
10612 break;
10613 }
10614 }
10615 \f
10616 /* DWARF name computation. */
10617
10618 /* A helper function for dwarf2_compute_name which determines whether DIE
10619 needs to have the name of the scope prepended to the name listed in the
10620 die. */
10621
10622 static int
10623 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10624 {
10625 struct attribute *attr;
10626
10627 switch (die->tag)
10628 {
10629 case DW_TAG_namespace:
10630 case DW_TAG_typedef:
10631 case DW_TAG_class_type:
10632 case DW_TAG_interface_type:
10633 case DW_TAG_structure_type:
10634 case DW_TAG_union_type:
10635 case DW_TAG_enumeration_type:
10636 case DW_TAG_enumerator:
10637 case DW_TAG_subprogram:
10638 case DW_TAG_inlined_subroutine:
10639 case DW_TAG_member:
10640 case DW_TAG_imported_declaration:
10641 return 1;
10642
10643 case DW_TAG_variable:
10644 case DW_TAG_constant:
10645 /* We only need to prefix "globally" visible variables. These include
10646 any variable marked with DW_AT_external or any variable that
10647 lives in a namespace. [Variables in anonymous namespaces
10648 require prefixing, but they are not DW_AT_external.] */
10649
10650 if (dwarf2_attr (die, DW_AT_specification, cu))
10651 {
10652 struct dwarf2_cu *spec_cu = cu;
10653
10654 return die_needs_namespace (die_specification (die, &spec_cu),
10655 spec_cu);
10656 }
10657
10658 attr = dwarf2_attr (die, DW_AT_external, cu);
10659 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10660 && die->parent->tag != DW_TAG_module)
10661 return 0;
10662 /* A variable in a lexical block of some kind does not need a
10663 namespace, even though in C++ such variables may be external
10664 and have a mangled name. */
10665 if (die->parent->tag == DW_TAG_lexical_block
10666 || die->parent->tag == DW_TAG_try_block
10667 || die->parent->tag == DW_TAG_catch_block
10668 || die->parent->tag == DW_TAG_subprogram)
10669 return 0;
10670 return 1;
10671
10672 default:
10673 return 0;
10674 }
10675 }
10676
10677 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10678 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10679 defined for the given DIE. */
10680
10681 static struct attribute *
10682 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10683 {
10684 struct attribute *attr;
10685
10686 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10687 if (attr == NULL)
10688 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10689
10690 return attr;
10691 }
10692
10693 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10694 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10695 defined for the given DIE. */
10696
10697 static const char *
10698 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10699 {
10700 const char *linkage_name;
10701
10702 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10703 if (linkage_name == NULL)
10704 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10705
10706 return linkage_name;
10707 }
10708
10709 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10710 compute the physname for the object, which include a method's:
10711 - formal parameters (C++),
10712 - receiver type (Go),
10713
10714 The term "physname" is a bit confusing.
10715 For C++, for example, it is the demangled name.
10716 For Go, for example, it's the mangled name.
10717
10718 For Ada, return the DIE's linkage name rather than the fully qualified
10719 name. PHYSNAME is ignored..
10720
10721 The result is allocated on the objfile_obstack and canonicalized. */
10722
10723 static const char *
10724 dwarf2_compute_name (const char *name,
10725 struct die_info *die, struct dwarf2_cu *cu,
10726 int physname)
10727 {
10728 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10729
10730 if (name == NULL)
10731 name = dwarf2_name (die, cu);
10732
10733 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10734 but otherwise compute it by typename_concat inside GDB.
10735 FIXME: Actually this is not really true, or at least not always true.
10736 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10737 Fortran names because there is no mangling standard. So new_symbol
10738 will set the demangled name to the result of dwarf2_full_name, and it is
10739 the demangled name that GDB uses if it exists. */
10740 if (cu->language == language_ada
10741 || (cu->language == language_fortran && physname))
10742 {
10743 /* For Ada unit, we prefer the linkage name over the name, as
10744 the former contains the exported name, which the user expects
10745 to be able to reference. Ideally, we want the user to be able
10746 to reference this entity using either natural or linkage name,
10747 but we haven't started looking at this enhancement yet. */
10748 const char *linkage_name = dw2_linkage_name (die, cu);
10749
10750 if (linkage_name != NULL)
10751 return linkage_name;
10752 }
10753
10754 /* These are the only languages we know how to qualify names in. */
10755 if (name != NULL
10756 && (cu->language == language_cplus
10757 || cu->language == language_fortran || cu->language == language_d
10758 || cu->language == language_rust))
10759 {
10760 if (die_needs_namespace (die, cu))
10761 {
10762 const char *prefix;
10763 const char *canonical_name = NULL;
10764
10765 string_file buf;
10766
10767 prefix = determine_prefix (die, cu);
10768 if (*prefix != '\0')
10769 {
10770 char *prefixed_name = typename_concat (NULL, prefix, name,
10771 physname, cu);
10772
10773 buf.puts (prefixed_name);
10774 xfree (prefixed_name);
10775 }
10776 else
10777 buf.puts (name);
10778
10779 /* Template parameters may be specified in the DIE's DW_AT_name, or
10780 as children with DW_TAG_template_type_param or
10781 DW_TAG_value_type_param. If the latter, add them to the name
10782 here. If the name already has template parameters, then
10783 skip this step; some versions of GCC emit both, and
10784 it is more efficient to use the pre-computed name.
10785
10786 Something to keep in mind about this process: it is very
10787 unlikely, or in some cases downright impossible, to produce
10788 something that will match the mangled name of a function.
10789 If the definition of the function has the same debug info,
10790 we should be able to match up with it anyway. But fallbacks
10791 using the minimal symbol, for instance to find a method
10792 implemented in a stripped copy of libstdc++, will not work.
10793 If we do not have debug info for the definition, we will have to
10794 match them up some other way.
10795
10796 When we do name matching there is a related problem with function
10797 templates; two instantiated function templates are allowed to
10798 differ only by their return types, which we do not add here. */
10799
10800 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10801 {
10802 struct attribute *attr;
10803 struct die_info *child;
10804 int first = 1;
10805
10806 die->building_fullname = 1;
10807
10808 for (child = die->child; child != NULL; child = child->sibling)
10809 {
10810 struct type *type;
10811 LONGEST value;
10812 const gdb_byte *bytes;
10813 struct dwarf2_locexpr_baton *baton;
10814 struct value *v;
10815
10816 if (child->tag != DW_TAG_template_type_param
10817 && child->tag != DW_TAG_template_value_param)
10818 continue;
10819
10820 if (first)
10821 {
10822 buf.puts ("<");
10823 first = 0;
10824 }
10825 else
10826 buf.puts (", ");
10827
10828 attr = dwarf2_attr (child, DW_AT_type, cu);
10829 if (attr == NULL)
10830 {
10831 complaint (&symfile_complaints,
10832 _("template parameter missing DW_AT_type"));
10833 buf.puts ("UNKNOWN_TYPE");
10834 continue;
10835 }
10836 type = die_type (child, cu);
10837
10838 if (child->tag == DW_TAG_template_type_param)
10839 {
10840 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10841 continue;
10842 }
10843
10844 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10845 if (attr == NULL)
10846 {
10847 complaint (&symfile_complaints,
10848 _("template parameter missing "
10849 "DW_AT_const_value"));
10850 buf.puts ("UNKNOWN_VALUE");
10851 continue;
10852 }
10853
10854 dwarf2_const_value_attr (attr, type, name,
10855 &cu->comp_unit_obstack, cu,
10856 &value, &bytes, &baton);
10857
10858 if (TYPE_NOSIGN (type))
10859 /* GDB prints characters as NUMBER 'CHAR'. If that's
10860 changed, this can use value_print instead. */
10861 c_printchar (value, type, &buf);
10862 else
10863 {
10864 struct value_print_options opts;
10865
10866 if (baton != NULL)
10867 v = dwarf2_evaluate_loc_desc (type, NULL,
10868 baton->data,
10869 baton->size,
10870 baton->per_cu);
10871 else if (bytes != NULL)
10872 {
10873 v = allocate_value (type);
10874 memcpy (value_contents_writeable (v), bytes,
10875 TYPE_LENGTH (type));
10876 }
10877 else
10878 v = value_from_longest (type, value);
10879
10880 /* Specify decimal so that we do not depend on
10881 the radix. */
10882 get_formatted_print_options (&opts, 'd');
10883 opts.raw = 1;
10884 value_print (v, &buf, &opts);
10885 release_value (v);
10886 }
10887 }
10888
10889 die->building_fullname = 0;
10890
10891 if (!first)
10892 {
10893 /* Close the argument list, with a space if necessary
10894 (nested templates). */
10895 if (!buf.empty () && buf.string ().back () == '>')
10896 buf.puts (" >");
10897 else
10898 buf.puts (">");
10899 }
10900 }
10901
10902 /* For C++ methods, append formal parameter type
10903 information, if PHYSNAME. */
10904
10905 if (physname && die->tag == DW_TAG_subprogram
10906 && cu->language == language_cplus)
10907 {
10908 struct type *type = read_type_die (die, cu);
10909
10910 c_type_print_args (type, &buf, 1, cu->language,
10911 &type_print_raw_options);
10912
10913 if (cu->language == language_cplus)
10914 {
10915 /* Assume that an artificial first parameter is
10916 "this", but do not crash if it is not. RealView
10917 marks unnamed (and thus unused) parameters as
10918 artificial; there is no way to differentiate
10919 the two cases. */
10920 if (TYPE_NFIELDS (type) > 0
10921 && TYPE_FIELD_ARTIFICIAL (type, 0)
10922 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10923 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10924 0))))
10925 buf.puts (" const");
10926 }
10927 }
10928
10929 const std::string &intermediate_name = buf.string ();
10930
10931 if (cu->language == language_cplus)
10932 canonical_name
10933 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10934 &objfile->per_bfd->storage_obstack);
10935
10936 /* If we only computed INTERMEDIATE_NAME, or if
10937 INTERMEDIATE_NAME is already canonical, then we need to
10938 copy it to the appropriate obstack. */
10939 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10940 name = ((const char *)
10941 obstack_copy0 (&objfile->per_bfd->storage_obstack,
10942 intermediate_name.c_str (),
10943 intermediate_name.length ()));
10944 else
10945 name = canonical_name;
10946 }
10947 }
10948
10949 return name;
10950 }
10951
10952 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10953 If scope qualifiers are appropriate they will be added. The result
10954 will be allocated on the storage_obstack, or NULL if the DIE does
10955 not have a name. NAME may either be from a previous call to
10956 dwarf2_name or NULL.
10957
10958 The output string will be canonicalized (if C++). */
10959
10960 static const char *
10961 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10962 {
10963 return dwarf2_compute_name (name, die, cu, 0);
10964 }
10965
10966 /* Construct a physname for the given DIE in CU. NAME may either be
10967 from a previous call to dwarf2_name or NULL. The result will be
10968 allocated on the objfile_objstack or NULL if the DIE does not have a
10969 name.
10970
10971 The output string will be canonicalized (if C++). */
10972
10973 static const char *
10974 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10975 {
10976 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10977 const char *retval, *mangled = NULL, *canon = NULL;
10978 int need_copy = 1;
10979
10980 /* In this case dwarf2_compute_name is just a shortcut not building anything
10981 on its own. */
10982 if (!die_needs_namespace (die, cu))
10983 return dwarf2_compute_name (name, die, cu, 1);
10984
10985 mangled = dw2_linkage_name (die, cu);
10986
10987 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10988 See https://github.com/rust-lang/rust/issues/32925. */
10989 if (cu->language == language_rust && mangled != NULL
10990 && strchr (mangled, '{') != NULL)
10991 mangled = NULL;
10992
10993 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10994 has computed. */
10995 gdb::unique_xmalloc_ptr<char> demangled;
10996 if (mangled != NULL)
10997 {
10998
10999 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11000 {
11001 /* Do nothing (do not demangle the symbol name). */
11002 }
11003 else if (cu->language == language_go)
11004 {
11005 /* This is a lie, but we already lie to the caller new_symbol.
11006 new_symbol assumes we return the mangled name.
11007 This just undoes that lie until things are cleaned up. */
11008 }
11009 else
11010 {
11011 /* Use DMGL_RET_DROP for C++ template functions to suppress
11012 their return type. It is easier for GDB users to search
11013 for such functions as `name(params)' than `long name(params)'.
11014 In such case the minimal symbol names do not match the full
11015 symbol names but for template functions there is never a need
11016 to look up their definition from their declaration so
11017 the only disadvantage remains the minimal symbol variant
11018 `long name(params)' does not have the proper inferior type. */
11019 demangled.reset (gdb_demangle (mangled,
11020 (DMGL_PARAMS | DMGL_ANSI
11021 | DMGL_RET_DROP)));
11022 }
11023 if (demangled)
11024 canon = demangled.get ();
11025 else
11026 {
11027 canon = mangled;
11028 need_copy = 0;
11029 }
11030 }
11031
11032 if (canon == NULL || check_physname)
11033 {
11034 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11035
11036 if (canon != NULL && strcmp (physname, canon) != 0)
11037 {
11038 /* It may not mean a bug in GDB. The compiler could also
11039 compute DW_AT_linkage_name incorrectly. But in such case
11040 GDB would need to be bug-to-bug compatible. */
11041
11042 complaint (&symfile_complaints,
11043 _("Computed physname <%s> does not match demangled <%s> "
11044 "(from linkage <%s>) - DIE at %s [in module %s]"),
11045 physname, canon, mangled, sect_offset_str (die->sect_off),
11046 objfile_name (objfile));
11047
11048 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11049 is available here - over computed PHYSNAME. It is safer
11050 against both buggy GDB and buggy compilers. */
11051
11052 retval = canon;
11053 }
11054 else
11055 {
11056 retval = physname;
11057 need_copy = 0;
11058 }
11059 }
11060 else
11061 retval = canon;
11062
11063 if (need_copy)
11064 retval = ((const char *)
11065 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11066 retval, strlen (retval)));
11067
11068 return retval;
11069 }
11070
11071 /* Inspect DIE in CU for a namespace alias. If one exists, record
11072 a new symbol for it.
11073
11074 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11075
11076 static int
11077 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11078 {
11079 struct attribute *attr;
11080
11081 /* If the die does not have a name, this is not a namespace
11082 alias. */
11083 attr = dwarf2_attr (die, DW_AT_name, cu);
11084 if (attr != NULL)
11085 {
11086 int num;
11087 struct die_info *d = die;
11088 struct dwarf2_cu *imported_cu = cu;
11089
11090 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11091 keep inspecting DIEs until we hit the underlying import. */
11092 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11093 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11094 {
11095 attr = dwarf2_attr (d, DW_AT_import, cu);
11096 if (attr == NULL)
11097 break;
11098
11099 d = follow_die_ref (d, attr, &imported_cu);
11100 if (d->tag != DW_TAG_imported_declaration)
11101 break;
11102 }
11103
11104 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11105 {
11106 complaint (&symfile_complaints,
11107 _("DIE at %s has too many recursively imported "
11108 "declarations"), sect_offset_str (d->sect_off));
11109 return 0;
11110 }
11111
11112 if (attr != NULL)
11113 {
11114 struct type *type;
11115 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11116
11117 type = get_die_type_at_offset (sect_off, cu->per_cu);
11118 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11119 {
11120 /* This declaration is a global namespace alias. Add
11121 a symbol for it whose type is the aliased namespace. */
11122 new_symbol (die, type, cu);
11123 return 1;
11124 }
11125 }
11126 }
11127
11128 return 0;
11129 }
11130
11131 /* Return the using directives repository (global or local?) to use in the
11132 current context for LANGUAGE.
11133
11134 For Ada, imported declarations can materialize renamings, which *may* be
11135 global. However it is impossible (for now?) in DWARF to distinguish
11136 "external" imported declarations and "static" ones. As all imported
11137 declarations seem to be static in all other languages, make them all CU-wide
11138 global only in Ada. */
11139
11140 static struct using_direct **
11141 using_directives (enum language language)
11142 {
11143 if (language == language_ada && context_stack_depth == 0)
11144 return &global_using_directives;
11145 else
11146 return &local_using_directives;
11147 }
11148
11149 /* Read the import statement specified by the given die and record it. */
11150
11151 static void
11152 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11153 {
11154 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11155 struct attribute *import_attr;
11156 struct die_info *imported_die, *child_die;
11157 struct dwarf2_cu *imported_cu;
11158 const char *imported_name;
11159 const char *imported_name_prefix;
11160 const char *canonical_name;
11161 const char *import_alias;
11162 const char *imported_declaration = NULL;
11163 const char *import_prefix;
11164 std::vector<const char *> excludes;
11165
11166 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11167 if (import_attr == NULL)
11168 {
11169 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11170 dwarf_tag_name (die->tag));
11171 return;
11172 }
11173
11174 imported_cu = cu;
11175 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11176 imported_name = dwarf2_name (imported_die, imported_cu);
11177 if (imported_name == NULL)
11178 {
11179 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11180
11181 The import in the following code:
11182 namespace A
11183 {
11184 typedef int B;
11185 }
11186
11187 int main ()
11188 {
11189 using A::B;
11190 B b;
11191 return b;
11192 }
11193
11194 ...
11195 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11196 <52> DW_AT_decl_file : 1
11197 <53> DW_AT_decl_line : 6
11198 <54> DW_AT_import : <0x75>
11199 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11200 <59> DW_AT_name : B
11201 <5b> DW_AT_decl_file : 1
11202 <5c> DW_AT_decl_line : 2
11203 <5d> DW_AT_type : <0x6e>
11204 ...
11205 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11206 <76> DW_AT_byte_size : 4
11207 <77> DW_AT_encoding : 5 (signed)
11208
11209 imports the wrong die ( 0x75 instead of 0x58 ).
11210 This case will be ignored until the gcc bug is fixed. */
11211 return;
11212 }
11213
11214 /* Figure out the local name after import. */
11215 import_alias = dwarf2_name (die, cu);
11216
11217 /* Figure out where the statement is being imported to. */
11218 import_prefix = determine_prefix (die, cu);
11219
11220 /* Figure out what the scope of the imported die is and prepend it
11221 to the name of the imported die. */
11222 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11223
11224 if (imported_die->tag != DW_TAG_namespace
11225 && imported_die->tag != DW_TAG_module)
11226 {
11227 imported_declaration = imported_name;
11228 canonical_name = imported_name_prefix;
11229 }
11230 else if (strlen (imported_name_prefix) > 0)
11231 canonical_name = obconcat (&objfile->objfile_obstack,
11232 imported_name_prefix,
11233 (cu->language == language_d ? "." : "::"),
11234 imported_name, (char *) NULL);
11235 else
11236 canonical_name = imported_name;
11237
11238 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11239 for (child_die = die->child; child_die && child_die->tag;
11240 child_die = sibling_die (child_die))
11241 {
11242 /* DWARF-4: A Fortran use statement with a “rename list” may be
11243 represented by an imported module entry with an import attribute
11244 referring to the module and owned entries corresponding to those
11245 entities that are renamed as part of being imported. */
11246
11247 if (child_die->tag != DW_TAG_imported_declaration)
11248 {
11249 complaint (&symfile_complaints,
11250 _("child DW_TAG_imported_declaration expected "
11251 "- DIE at %s [in module %s]"),
11252 sect_offset_str (child_die->sect_off),
11253 objfile_name (objfile));
11254 continue;
11255 }
11256
11257 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11258 if (import_attr == NULL)
11259 {
11260 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11261 dwarf_tag_name (child_die->tag));
11262 continue;
11263 }
11264
11265 imported_cu = cu;
11266 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11267 &imported_cu);
11268 imported_name = dwarf2_name (imported_die, imported_cu);
11269 if (imported_name == NULL)
11270 {
11271 complaint (&symfile_complaints,
11272 _("child DW_TAG_imported_declaration has unknown "
11273 "imported name - DIE at %s [in module %s]"),
11274 sect_offset_str (child_die->sect_off),
11275 objfile_name (objfile));
11276 continue;
11277 }
11278
11279 excludes.push_back (imported_name);
11280
11281 process_die (child_die, cu);
11282 }
11283
11284 add_using_directive (using_directives (cu->language),
11285 import_prefix,
11286 canonical_name,
11287 import_alias,
11288 imported_declaration,
11289 excludes,
11290 0,
11291 &objfile->objfile_obstack);
11292 }
11293
11294 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11295 types, but gives them a size of zero. Starting with version 14,
11296 ICC is compatible with GCC. */
11297
11298 static int
11299 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11300 {
11301 if (!cu->checked_producer)
11302 check_producer (cu);
11303
11304 return cu->producer_is_icc_lt_14;
11305 }
11306
11307 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11308 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11309 this, it was first present in GCC release 4.3.0. */
11310
11311 static int
11312 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11313 {
11314 if (!cu->checked_producer)
11315 check_producer (cu);
11316
11317 return cu->producer_is_gcc_lt_4_3;
11318 }
11319
11320 static file_and_directory
11321 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11322 {
11323 file_and_directory res;
11324
11325 /* Find the filename. Do not use dwarf2_name here, since the filename
11326 is not a source language identifier. */
11327 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11328 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11329
11330 if (res.comp_dir == NULL
11331 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11332 && IS_ABSOLUTE_PATH (res.name))
11333 {
11334 res.comp_dir_storage = ldirname (res.name);
11335 if (!res.comp_dir_storage.empty ())
11336 res.comp_dir = res.comp_dir_storage.c_str ();
11337 }
11338 if (res.comp_dir != NULL)
11339 {
11340 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11341 directory, get rid of it. */
11342 const char *cp = strchr (res.comp_dir, ':');
11343
11344 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11345 res.comp_dir = cp + 1;
11346 }
11347
11348 if (res.name == NULL)
11349 res.name = "<unknown>";
11350
11351 return res;
11352 }
11353
11354 /* Handle DW_AT_stmt_list for a compilation unit.
11355 DIE is the DW_TAG_compile_unit die for CU.
11356 COMP_DIR is the compilation directory. LOWPC is passed to
11357 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11358
11359 static void
11360 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11361 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11362 {
11363 struct dwarf2_per_objfile *dwarf2_per_objfile
11364 = cu->per_cu->dwarf2_per_objfile;
11365 struct objfile *objfile = dwarf2_per_objfile->objfile;
11366 struct attribute *attr;
11367 struct line_header line_header_local;
11368 hashval_t line_header_local_hash;
11369 void **slot;
11370 int decode_mapping;
11371
11372 gdb_assert (! cu->per_cu->is_debug_types);
11373
11374 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11375 if (attr == NULL)
11376 return;
11377
11378 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11379
11380 /* The line header hash table is only created if needed (it exists to
11381 prevent redundant reading of the line table for partial_units).
11382 If we're given a partial_unit, we'll need it. If we're given a
11383 compile_unit, then use the line header hash table if it's already
11384 created, but don't create one just yet. */
11385
11386 if (dwarf2_per_objfile->line_header_hash == NULL
11387 && die->tag == DW_TAG_partial_unit)
11388 {
11389 dwarf2_per_objfile->line_header_hash
11390 = htab_create_alloc_ex (127, line_header_hash_voidp,
11391 line_header_eq_voidp,
11392 free_line_header_voidp,
11393 &objfile->objfile_obstack,
11394 hashtab_obstack_allocate,
11395 dummy_obstack_deallocate);
11396 }
11397
11398 line_header_local.sect_off = line_offset;
11399 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11400 line_header_local_hash = line_header_hash (&line_header_local);
11401 if (dwarf2_per_objfile->line_header_hash != NULL)
11402 {
11403 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11404 &line_header_local,
11405 line_header_local_hash, NO_INSERT);
11406
11407 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11408 is not present in *SLOT (since if there is something in *SLOT then
11409 it will be for a partial_unit). */
11410 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11411 {
11412 gdb_assert (*slot != NULL);
11413 cu->line_header = (struct line_header *) *slot;
11414 return;
11415 }
11416 }
11417
11418 /* dwarf_decode_line_header does not yet provide sufficient information.
11419 We always have to call also dwarf_decode_lines for it. */
11420 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11421 if (lh == NULL)
11422 return;
11423
11424 cu->line_header = lh.release ();
11425 cu->line_header_die_owner = die;
11426
11427 if (dwarf2_per_objfile->line_header_hash == NULL)
11428 slot = NULL;
11429 else
11430 {
11431 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11432 &line_header_local,
11433 line_header_local_hash, INSERT);
11434 gdb_assert (slot != NULL);
11435 }
11436 if (slot != NULL && *slot == NULL)
11437 {
11438 /* This newly decoded line number information unit will be owned
11439 by line_header_hash hash table. */
11440 *slot = cu->line_header;
11441 cu->line_header_die_owner = NULL;
11442 }
11443 else
11444 {
11445 /* We cannot free any current entry in (*slot) as that struct line_header
11446 may be already used by multiple CUs. Create only temporary decoded
11447 line_header for this CU - it may happen at most once for each line
11448 number information unit. And if we're not using line_header_hash
11449 then this is what we want as well. */
11450 gdb_assert (die->tag != DW_TAG_partial_unit);
11451 }
11452 decode_mapping = (die->tag != DW_TAG_partial_unit);
11453 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11454 decode_mapping);
11455
11456 }
11457
11458 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11459
11460 static void
11461 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11462 {
11463 struct dwarf2_per_objfile *dwarf2_per_objfile
11464 = cu->per_cu->dwarf2_per_objfile;
11465 struct objfile *objfile = dwarf2_per_objfile->objfile;
11466 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11467 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11468 CORE_ADDR highpc = ((CORE_ADDR) 0);
11469 struct attribute *attr;
11470 struct die_info *child_die;
11471 CORE_ADDR baseaddr;
11472
11473 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11474
11475 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11476
11477 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11478 from finish_block. */
11479 if (lowpc == ((CORE_ADDR) -1))
11480 lowpc = highpc;
11481 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11482
11483 file_and_directory fnd = find_file_and_directory (die, cu);
11484
11485 prepare_one_comp_unit (cu, die, cu->language);
11486
11487 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11488 standardised yet. As a workaround for the language detection we fall
11489 back to the DW_AT_producer string. */
11490 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11491 cu->language = language_opencl;
11492
11493 /* Similar hack for Go. */
11494 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11495 set_cu_language (DW_LANG_Go, cu);
11496
11497 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11498
11499 /* Decode line number information if present. We do this before
11500 processing child DIEs, so that the line header table is available
11501 for DW_AT_decl_file. */
11502 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11503
11504 /* Process all dies in compilation unit. */
11505 if (die->child != NULL)
11506 {
11507 child_die = die->child;
11508 while (child_die && child_die->tag)
11509 {
11510 process_die (child_die, cu);
11511 child_die = sibling_die (child_die);
11512 }
11513 }
11514
11515 /* Decode macro information, if present. Dwarf 2 macro information
11516 refers to information in the line number info statement program
11517 header, so we can only read it if we've read the header
11518 successfully. */
11519 attr = dwarf2_attr (die, DW_AT_macros, cu);
11520 if (attr == NULL)
11521 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11522 if (attr && cu->line_header)
11523 {
11524 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11525 complaint (&symfile_complaints,
11526 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11527
11528 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11529 }
11530 else
11531 {
11532 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11533 if (attr && cu->line_header)
11534 {
11535 unsigned int macro_offset = DW_UNSND (attr);
11536
11537 dwarf_decode_macros (cu, macro_offset, 0);
11538 }
11539 }
11540 }
11541
11542 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11543 Create the set of symtabs used by this TU, or if this TU is sharing
11544 symtabs with another TU and the symtabs have already been created
11545 then restore those symtabs in the line header.
11546 We don't need the pc/line-number mapping for type units. */
11547
11548 static void
11549 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11550 {
11551 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11552 struct type_unit_group *tu_group;
11553 int first_time;
11554 struct attribute *attr;
11555 unsigned int i;
11556 struct signatured_type *sig_type;
11557
11558 gdb_assert (per_cu->is_debug_types);
11559 sig_type = (struct signatured_type *) per_cu;
11560
11561 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11562
11563 /* If we're using .gdb_index (includes -readnow) then
11564 per_cu->type_unit_group may not have been set up yet. */
11565 if (sig_type->type_unit_group == NULL)
11566 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11567 tu_group = sig_type->type_unit_group;
11568
11569 /* If we've already processed this stmt_list there's no real need to
11570 do it again, we could fake it and just recreate the part we need
11571 (file name,index -> symtab mapping). If data shows this optimization
11572 is useful we can do it then. */
11573 first_time = tu_group->compunit_symtab == NULL;
11574
11575 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11576 debug info. */
11577 line_header_up lh;
11578 if (attr != NULL)
11579 {
11580 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11581 lh = dwarf_decode_line_header (line_offset, cu);
11582 }
11583 if (lh == NULL)
11584 {
11585 if (first_time)
11586 dwarf2_start_symtab (cu, "", NULL, 0);
11587 else
11588 {
11589 gdb_assert (tu_group->symtabs == NULL);
11590 restart_symtab (tu_group->compunit_symtab, "", 0);
11591 }
11592 return;
11593 }
11594
11595 cu->line_header = lh.release ();
11596 cu->line_header_die_owner = die;
11597
11598 if (first_time)
11599 {
11600 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11601
11602 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11603 still initializing it, and our caller (a few levels up)
11604 process_full_type_unit still needs to know if this is the first
11605 time. */
11606
11607 tu_group->num_symtabs = cu->line_header->file_names.size ();
11608 tu_group->symtabs = XNEWVEC (struct symtab *,
11609 cu->line_header->file_names.size ());
11610
11611 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11612 {
11613 file_entry &fe = cu->line_header->file_names[i];
11614
11615 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11616
11617 if (current_subfile->symtab == NULL)
11618 {
11619 /* NOTE: start_subfile will recognize when it's been
11620 passed a file it has already seen. So we can't
11621 assume there's a simple mapping from
11622 cu->line_header->file_names to subfiles, plus
11623 cu->line_header->file_names may contain dups. */
11624 current_subfile->symtab
11625 = allocate_symtab (cust, current_subfile->name);
11626 }
11627
11628 fe.symtab = current_subfile->symtab;
11629 tu_group->symtabs[i] = fe.symtab;
11630 }
11631 }
11632 else
11633 {
11634 restart_symtab (tu_group->compunit_symtab, "", 0);
11635
11636 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11637 {
11638 file_entry &fe = cu->line_header->file_names[i];
11639
11640 fe.symtab = tu_group->symtabs[i];
11641 }
11642 }
11643
11644 /* The main symtab is allocated last. Type units don't have DW_AT_name
11645 so they don't have a "real" (so to speak) symtab anyway.
11646 There is later code that will assign the main symtab to all symbols
11647 that don't have one. We need to handle the case of a symbol with a
11648 missing symtab (DW_AT_decl_file) anyway. */
11649 }
11650
11651 /* Process DW_TAG_type_unit.
11652 For TUs we want to skip the first top level sibling if it's not the
11653 actual type being defined by this TU. In this case the first top
11654 level sibling is there to provide context only. */
11655
11656 static void
11657 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11658 {
11659 struct die_info *child_die;
11660
11661 prepare_one_comp_unit (cu, die, language_minimal);
11662
11663 /* Initialize (or reinitialize) the machinery for building symtabs.
11664 We do this before processing child DIEs, so that the line header table
11665 is available for DW_AT_decl_file. */
11666 setup_type_unit_groups (die, cu);
11667
11668 if (die->child != NULL)
11669 {
11670 child_die = die->child;
11671 while (child_die && child_die->tag)
11672 {
11673 process_die (child_die, cu);
11674 child_die = sibling_die (child_die);
11675 }
11676 }
11677 }
11678 \f
11679 /* DWO/DWP files.
11680
11681 http://gcc.gnu.org/wiki/DebugFission
11682 http://gcc.gnu.org/wiki/DebugFissionDWP
11683
11684 To simplify handling of both DWO files ("object" files with the DWARF info)
11685 and DWP files (a file with the DWOs packaged up into one file), we treat
11686 DWP files as having a collection of virtual DWO files. */
11687
11688 static hashval_t
11689 hash_dwo_file (const void *item)
11690 {
11691 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11692 hashval_t hash;
11693
11694 hash = htab_hash_string (dwo_file->dwo_name);
11695 if (dwo_file->comp_dir != NULL)
11696 hash += htab_hash_string (dwo_file->comp_dir);
11697 return hash;
11698 }
11699
11700 static int
11701 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11702 {
11703 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11704 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11705
11706 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11707 return 0;
11708 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11709 return lhs->comp_dir == rhs->comp_dir;
11710 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11711 }
11712
11713 /* Allocate a hash table for DWO files. */
11714
11715 static htab_t
11716 allocate_dwo_file_hash_table (struct objfile *objfile)
11717 {
11718 return htab_create_alloc_ex (41,
11719 hash_dwo_file,
11720 eq_dwo_file,
11721 NULL,
11722 &objfile->objfile_obstack,
11723 hashtab_obstack_allocate,
11724 dummy_obstack_deallocate);
11725 }
11726
11727 /* Lookup DWO file DWO_NAME. */
11728
11729 static void **
11730 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11731 const char *dwo_name,
11732 const char *comp_dir)
11733 {
11734 struct dwo_file find_entry;
11735 void **slot;
11736
11737 if (dwarf2_per_objfile->dwo_files == NULL)
11738 dwarf2_per_objfile->dwo_files
11739 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11740
11741 memset (&find_entry, 0, sizeof (find_entry));
11742 find_entry.dwo_name = dwo_name;
11743 find_entry.comp_dir = comp_dir;
11744 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11745
11746 return slot;
11747 }
11748
11749 static hashval_t
11750 hash_dwo_unit (const void *item)
11751 {
11752 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11753
11754 /* This drops the top 32 bits of the id, but is ok for a hash. */
11755 return dwo_unit->signature;
11756 }
11757
11758 static int
11759 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11760 {
11761 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11762 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11763
11764 /* The signature is assumed to be unique within the DWO file.
11765 So while object file CU dwo_id's always have the value zero,
11766 that's OK, assuming each object file DWO file has only one CU,
11767 and that's the rule for now. */
11768 return lhs->signature == rhs->signature;
11769 }
11770
11771 /* Allocate a hash table for DWO CUs,TUs.
11772 There is one of these tables for each of CUs,TUs for each DWO file. */
11773
11774 static htab_t
11775 allocate_dwo_unit_table (struct objfile *objfile)
11776 {
11777 /* Start out with a pretty small number.
11778 Generally DWO files contain only one CU and maybe some TUs. */
11779 return htab_create_alloc_ex (3,
11780 hash_dwo_unit,
11781 eq_dwo_unit,
11782 NULL,
11783 &objfile->objfile_obstack,
11784 hashtab_obstack_allocate,
11785 dummy_obstack_deallocate);
11786 }
11787
11788 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11789
11790 struct create_dwo_cu_data
11791 {
11792 struct dwo_file *dwo_file;
11793 struct dwo_unit dwo_unit;
11794 };
11795
11796 /* die_reader_func for create_dwo_cu. */
11797
11798 static void
11799 create_dwo_cu_reader (const struct die_reader_specs *reader,
11800 const gdb_byte *info_ptr,
11801 struct die_info *comp_unit_die,
11802 int has_children,
11803 void *datap)
11804 {
11805 struct dwarf2_cu *cu = reader->cu;
11806 sect_offset sect_off = cu->per_cu->sect_off;
11807 struct dwarf2_section_info *section = cu->per_cu->section;
11808 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11809 struct dwo_file *dwo_file = data->dwo_file;
11810 struct dwo_unit *dwo_unit = &data->dwo_unit;
11811 struct attribute *attr;
11812
11813 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11814 if (attr == NULL)
11815 {
11816 complaint (&symfile_complaints,
11817 _("Dwarf Error: debug entry at offset %s is missing"
11818 " its dwo_id [in module %s]"),
11819 sect_offset_str (sect_off), dwo_file->dwo_name);
11820 return;
11821 }
11822
11823 dwo_unit->dwo_file = dwo_file;
11824 dwo_unit->signature = DW_UNSND (attr);
11825 dwo_unit->section = section;
11826 dwo_unit->sect_off = sect_off;
11827 dwo_unit->length = cu->per_cu->length;
11828
11829 if (dwarf_read_debug)
11830 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11831 sect_offset_str (sect_off),
11832 hex_string (dwo_unit->signature));
11833 }
11834
11835 /* Create the dwo_units for the CUs in a DWO_FILE.
11836 Note: This function processes DWO files only, not DWP files. */
11837
11838 static void
11839 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11840 struct dwo_file &dwo_file, dwarf2_section_info &section,
11841 htab_t &cus_htab)
11842 {
11843 struct objfile *objfile = dwarf2_per_objfile->objfile;
11844 const gdb_byte *info_ptr, *end_ptr;
11845
11846 dwarf2_read_section (objfile, &section);
11847 info_ptr = section.buffer;
11848
11849 if (info_ptr == NULL)
11850 return;
11851
11852 if (dwarf_read_debug)
11853 {
11854 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11855 get_section_name (&section),
11856 get_section_file_name (&section));
11857 }
11858
11859 end_ptr = info_ptr + section.size;
11860 while (info_ptr < end_ptr)
11861 {
11862 struct dwarf2_per_cu_data per_cu;
11863 struct create_dwo_cu_data create_dwo_cu_data;
11864 struct dwo_unit *dwo_unit;
11865 void **slot;
11866 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11867
11868 memset (&create_dwo_cu_data.dwo_unit, 0,
11869 sizeof (create_dwo_cu_data.dwo_unit));
11870 memset (&per_cu, 0, sizeof (per_cu));
11871 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11872 per_cu.is_debug_types = 0;
11873 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11874 per_cu.section = &section;
11875 create_dwo_cu_data.dwo_file = &dwo_file;
11876
11877 init_cutu_and_read_dies_no_follow (
11878 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11879 info_ptr += per_cu.length;
11880
11881 // If the unit could not be parsed, skip it.
11882 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11883 continue;
11884
11885 if (cus_htab == NULL)
11886 cus_htab = allocate_dwo_unit_table (objfile);
11887
11888 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11889 *dwo_unit = create_dwo_cu_data.dwo_unit;
11890 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11891 gdb_assert (slot != NULL);
11892 if (*slot != NULL)
11893 {
11894 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11895 sect_offset dup_sect_off = dup_cu->sect_off;
11896
11897 complaint (&symfile_complaints,
11898 _("debug cu entry at offset %s is duplicate to"
11899 " the entry at offset %s, signature %s"),
11900 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11901 hex_string (dwo_unit->signature));
11902 }
11903 *slot = (void *)dwo_unit;
11904 }
11905 }
11906
11907 /* DWP file .debug_{cu,tu}_index section format:
11908 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11909
11910 DWP Version 1:
11911
11912 Both index sections have the same format, and serve to map a 64-bit
11913 signature to a set of section numbers. Each section begins with a header,
11914 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11915 indexes, and a pool of 32-bit section numbers. The index sections will be
11916 aligned at 8-byte boundaries in the file.
11917
11918 The index section header consists of:
11919
11920 V, 32 bit version number
11921 -, 32 bits unused
11922 N, 32 bit number of compilation units or type units in the index
11923 M, 32 bit number of slots in the hash table
11924
11925 Numbers are recorded using the byte order of the application binary.
11926
11927 The hash table begins at offset 16 in the section, and consists of an array
11928 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11929 order of the application binary). Unused slots in the hash table are 0.
11930 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11931
11932 The parallel table begins immediately after the hash table
11933 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11934 array of 32-bit indexes (using the byte order of the application binary),
11935 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11936 table contains a 32-bit index into the pool of section numbers. For unused
11937 hash table slots, the corresponding entry in the parallel table will be 0.
11938
11939 The pool of section numbers begins immediately following the hash table
11940 (at offset 16 + 12 * M from the beginning of the section). The pool of
11941 section numbers consists of an array of 32-bit words (using the byte order
11942 of the application binary). Each item in the array is indexed starting
11943 from 0. The hash table entry provides the index of the first section
11944 number in the set. Additional section numbers in the set follow, and the
11945 set is terminated by a 0 entry (section number 0 is not used in ELF).
11946
11947 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11948 section must be the first entry in the set, and the .debug_abbrev.dwo must
11949 be the second entry. Other members of the set may follow in any order.
11950
11951 ---
11952
11953 DWP Version 2:
11954
11955 DWP Version 2 combines all the .debug_info, etc. sections into one,
11956 and the entries in the index tables are now offsets into these sections.
11957 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11958 section.
11959
11960 Index Section Contents:
11961 Header
11962 Hash Table of Signatures dwp_hash_table.hash_table
11963 Parallel Table of Indices dwp_hash_table.unit_table
11964 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11965 Table of Section Sizes dwp_hash_table.v2.sizes
11966
11967 The index section header consists of:
11968
11969 V, 32 bit version number
11970 L, 32 bit number of columns in the table of section offsets
11971 N, 32 bit number of compilation units or type units in the index
11972 M, 32 bit number of slots in the hash table
11973
11974 Numbers are recorded using the byte order of the application binary.
11975
11976 The hash table has the same format as version 1.
11977 The parallel table of indices has the same format as version 1,
11978 except that the entries are origin-1 indices into the table of sections
11979 offsets and the table of section sizes.
11980
11981 The table of offsets begins immediately following the parallel table
11982 (at offset 16 + 12 * M from the beginning of the section). The table is
11983 a two-dimensional array of 32-bit words (using the byte order of the
11984 application binary), with L columns and N+1 rows, in row-major order.
11985 Each row in the array is indexed starting from 0. The first row provides
11986 a key to the remaining rows: each column in this row provides an identifier
11987 for a debug section, and the offsets in the same column of subsequent rows
11988 refer to that section. The section identifiers are:
11989
11990 DW_SECT_INFO 1 .debug_info.dwo
11991 DW_SECT_TYPES 2 .debug_types.dwo
11992 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11993 DW_SECT_LINE 4 .debug_line.dwo
11994 DW_SECT_LOC 5 .debug_loc.dwo
11995 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11996 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11997 DW_SECT_MACRO 8 .debug_macro.dwo
11998
11999 The offsets provided by the CU and TU index sections are the base offsets
12000 for the contributions made by each CU or TU to the corresponding section
12001 in the package file. Each CU and TU header contains an abbrev_offset
12002 field, used to find the abbreviations table for that CU or TU within the
12003 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12004 be interpreted as relative to the base offset given in the index section.
12005 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12006 should be interpreted as relative to the base offset for .debug_line.dwo,
12007 and offsets into other debug sections obtained from DWARF attributes should
12008 also be interpreted as relative to the corresponding base offset.
12009
12010 The table of sizes begins immediately following the table of offsets.
12011 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12012 with L columns and N rows, in row-major order. Each row in the array is
12013 indexed starting from 1 (row 0 is shared by the two tables).
12014
12015 ---
12016
12017 Hash table lookup is handled the same in version 1 and 2:
12018
12019 We assume that N and M will not exceed 2^32 - 1.
12020 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12021
12022 Given a 64-bit compilation unit signature or a type signature S, an entry
12023 in the hash table is located as follows:
12024
12025 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12026 the low-order k bits all set to 1.
12027
12028 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12029
12030 3) If the hash table entry at index H matches the signature, use that
12031 entry. If the hash table entry at index H is unused (all zeroes),
12032 terminate the search: the signature is not present in the table.
12033
12034 4) Let H = (H + H') modulo M. Repeat at Step 3.
12035
12036 Because M > N and H' and M are relatively prime, the search is guaranteed
12037 to stop at an unused slot or find the match. */
12038
12039 /* Create a hash table to map DWO IDs to their CU/TU entry in
12040 .debug_{info,types}.dwo in DWP_FILE.
12041 Returns NULL if there isn't one.
12042 Note: This function processes DWP files only, not DWO files. */
12043
12044 static struct dwp_hash_table *
12045 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12046 struct dwp_file *dwp_file, int is_debug_types)
12047 {
12048 struct objfile *objfile = dwarf2_per_objfile->objfile;
12049 bfd *dbfd = dwp_file->dbfd.get ();
12050 const gdb_byte *index_ptr, *index_end;
12051 struct dwarf2_section_info *index;
12052 uint32_t version, nr_columns, nr_units, nr_slots;
12053 struct dwp_hash_table *htab;
12054
12055 if (is_debug_types)
12056 index = &dwp_file->sections.tu_index;
12057 else
12058 index = &dwp_file->sections.cu_index;
12059
12060 if (dwarf2_section_empty_p (index))
12061 return NULL;
12062 dwarf2_read_section (objfile, index);
12063
12064 index_ptr = index->buffer;
12065 index_end = index_ptr + index->size;
12066
12067 version = read_4_bytes (dbfd, index_ptr);
12068 index_ptr += 4;
12069 if (version == 2)
12070 nr_columns = read_4_bytes (dbfd, index_ptr);
12071 else
12072 nr_columns = 0;
12073 index_ptr += 4;
12074 nr_units = read_4_bytes (dbfd, index_ptr);
12075 index_ptr += 4;
12076 nr_slots = read_4_bytes (dbfd, index_ptr);
12077 index_ptr += 4;
12078
12079 if (version != 1 && version != 2)
12080 {
12081 error (_("Dwarf Error: unsupported DWP file version (%s)"
12082 " [in module %s]"),
12083 pulongest (version), dwp_file->name);
12084 }
12085 if (nr_slots != (nr_slots & -nr_slots))
12086 {
12087 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12088 " is not power of 2 [in module %s]"),
12089 pulongest (nr_slots), dwp_file->name);
12090 }
12091
12092 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12093 htab->version = version;
12094 htab->nr_columns = nr_columns;
12095 htab->nr_units = nr_units;
12096 htab->nr_slots = nr_slots;
12097 htab->hash_table = index_ptr;
12098 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12099
12100 /* Exit early if the table is empty. */
12101 if (nr_slots == 0 || nr_units == 0
12102 || (version == 2 && nr_columns == 0))
12103 {
12104 /* All must be zero. */
12105 if (nr_slots != 0 || nr_units != 0
12106 || (version == 2 && nr_columns != 0))
12107 {
12108 complaint (&symfile_complaints,
12109 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12110 " all zero [in modules %s]"),
12111 dwp_file->name);
12112 }
12113 return htab;
12114 }
12115
12116 if (version == 1)
12117 {
12118 htab->section_pool.v1.indices =
12119 htab->unit_table + sizeof (uint32_t) * nr_slots;
12120 /* It's harder to decide whether the section is too small in v1.
12121 V1 is deprecated anyway so we punt. */
12122 }
12123 else
12124 {
12125 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12126 int *ids = htab->section_pool.v2.section_ids;
12127 /* Reverse map for error checking. */
12128 int ids_seen[DW_SECT_MAX + 1];
12129 int i;
12130
12131 if (nr_columns < 2)
12132 {
12133 error (_("Dwarf Error: bad DWP hash table, too few columns"
12134 " in section table [in module %s]"),
12135 dwp_file->name);
12136 }
12137 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12138 {
12139 error (_("Dwarf Error: bad DWP hash table, too many columns"
12140 " in section table [in module %s]"),
12141 dwp_file->name);
12142 }
12143 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12144 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12145 for (i = 0; i < nr_columns; ++i)
12146 {
12147 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12148
12149 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12150 {
12151 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12152 " in section table [in module %s]"),
12153 id, dwp_file->name);
12154 }
12155 if (ids_seen[id] != -1)
12156 {
12157 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12158 " id %d in section table [in module %s]"),
12159 id, dwp_file->name);
12160 }
12161 ids_seen[id] = i;
12162 ids[i] = id;
12163 }
12164 /* Must have exactly one info or types section. */
12165 if (((ids_seen[DW_SECT_INFO] != -1)
12166 + (ids_seen[DW_SECT_TYPES] != -1))
12167 != 1)
12168 {
12169 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12170 " DWO info/types section [in module %s]"),
12171 dwp_file->name);
12172 }
12173 /* Must have an abbrev section. */
12174 if (ids_seen[DW_SECT_ABBREV] == -1)
12175 {
12176 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12177 " section [in module %s]"),
12178 dwp_file->name);
12179 }
12180 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12181 htab->section_pool.v2.sizes =
12182 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12183 * nr_units * nr_columns);
12184 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12185 * nr_units * nr_columns))
12186 > index_end)
12187 {
12188 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12189 " [in module %s]"),
12190 dwp_file->name);
12191 }
12192 }
12193
12194 return htab;
12195 }
12196
12197 /* Update SECTIONS with the data from SECTP.
12198
12199 This function is like the other "locate" section routines that are
12200 passed to bfd_map_over_sections, but in this context the sections to
12201 read comes from the DWP V1 hash table, not the full ELF section table.
12202
12203 The result is non-zero for success, or zero if an error was found. */
12204
12205 static int
12206 locate_v1_virtual_dwo_sections (asection *sectp,
12207 struct virtual_v1_dwo_sections *sections)
12208 {
12209 const struct dwop_section_names *names = &dwop_section_names;
12210
12211 if (section_is_p (sectp->name, &names->abbrev_dwo))
12212 {
12213 /* There can be only one. */
12214 if (sections->abbrev.s.section != NULL)
12215 return 0;
12216 sections->abbrev.s.section = sectp;
12217 sections->abbrev.size = bfd_get_section_size (sectp);
12218 }
12219 else if (section_is_p (sectp->name, &names->info_dwo)
12220 || section_is_p (sectp->name, &names->types_dwo))
12221 {
12222 /* There can be only one. */
12223 if (sections->info_or_types.s.section != NULL)
12224 return 0;
12225 sections->info_or_types.s.section = sectp;
12226 sections->info_or_types.size = bfd_get_section_size (sectp);
12227 }
12228 else if (section_is_p (sectp->name, &names->line_dwo))
12229 {
12230 /* There can be only one. */
12231 if (sections->line.s.section != NULL)
12232 return 0;
12233 sections->line.s.section = sectp;
12234 sections->line.size = bfd_get_section_size (sectp);
12235 }
12236 else if (section_is_p (sectp->name, &names->loc_dwo))
12237 {
12238 /* There can be only one. */
12239 if (sections->loc.s.section != NULL)
12240 return 0;
12241 sections->loc.s.section = sectp;
12242 sections->loc.size = bfd_get_section_size (sectp);
12243 }
12244 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12245 {
12246 /* There can be only one. */
12247 if (sections->macinfo.s.section != NULL)
12248 return 0;
12249 sections->macinfo.s.section = sectp;
12250 sections->macinfo.size = bfd_get_section_size (sectp);
12251 }
12252 else if (section_is_p (sectp->name, &names->macro_dwo))
12253 {
12254 /* There can be only one. */
12255 if (sections->macro.s.section != NULL)
12256 return 0;
12257 sections->macro.s.section = sectp;
12258 sections->macro.size = bfd_get_section_size (sectp);
12259 }
12260 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12261 {
12262 /* There can be only one. */
12263 if (sections->str_offsets.s.section != NULL)
12264 return 0;
12265 sections->str_offsets.s.section = sectp;
12266 sections->str_offsets.size = bfd_get_section_size (sectp);
12267 }
12268 else
12269 {
12270 /* No other kind of section is valid. */
12271 return 0;
12272 }
12273
12274 return 1;
12275 }
12276
12277 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12278 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12279 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12280 This is for DWP version 1 files. */
12281
12282 static struct dwo_unit *
12283 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12284 struct dwp_file *dwp_file,
12285 uint32_t unit_index,
12286 const char *comp_dir,
12287 ULONGEST signature, int is_debug_types)
12288 {
12289 struct objfile *objfile = dwarf2_per_objfile->objfile;
12290 const struct dwp_hash_table *dwp_htab =
12291 is_debug_types ? dwp_file->tus : dwp_file->cus;
12292 bfd *dbfd = dwp_file->dbfd.get ();
12293 const char *kind = is_debug_types ? "TU" : "CU";
12294 struct dwo_file *dwo_file;
12295 struct dwo_unit *dwo_unit;
12296 struct virtual_v1_dwo_sections sections;
12297 void **dwo_file_slot;
12298 int i;
12299
12300 gdb_assert (dwp_file->version == 1);
12301
12302 if (dwarf_read_debug)
12303 {
12304 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12305 kind,
12306 pulongest (unit_index), hex_string (signature),
12307 dwp_file->name);
12308 }
12309
12310 /* Fetch the sections of this DWO unit.
12311 Put a limit on the number of sections we look for so that bad data
12312 doesn't cause us to loop forever. */
12313
12314 #define MAX_NR_V1_DWO_SECTIONS \
12315 (1 /* .debug_info or .debug_types */ \
12316 + 1 /* .debug_abbrev */ \
12317 + 1 /* .debug_line */ \
12318 + 1 /* .debug_loc */ \
12319 + 1 /* .debug_str_offsets */ \
12320 + 1 /* .debug_macro or .debug_macinfo */ \
12321 + 1 /* trailing zero */)
12322
12323 memset (&sections, 0, sizeof (sections));
12324
12325 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12326 {
12327 asection *sectp;
12328 uint32_t section_nr =
12329 read_4_bytes (dbfd,
12330 dwp_htab->section_pool.v1.indices
12331 + (unit_index + i) * sizeof (uint32_t));
12332
12333 if (section_nr == 0)
12334 break;
12335 if (section_nr >= dwp_file->num_sections)
12336 {
12337 error (_("Dwarf Error: bad DWP hash table, section number too large"
12338 " [in module %s]"),
12339 dwp_file->name);
12340 }
12341
12342 sectp = dwp_file->elf_sections[section_nr];
12343 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12344 {
12345 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12346 " [in module %s]"),
12347 dwp_file->name);
12348 }
12349 }
12350
12351 if (i < 2
12352 || dwarf2_section_empty_p (&sections.info_or_types)
12353 || dwarf2_section_empty_p (&sections.abbrev))
12354 {
12355 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12356 " [in module %s]"),
12357 dwp_file->name);
12358 }
12359 if (i == MAX_NR_V1_DWO_SECTIONS)
12360 {
12361 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12362 " [in module %s]"),
12363 dwp_file->name);
12364 }
12365
12366 /* It's easier for the rest of the code if we fake a struct dwo_file and
12367 have dwo_unit "live" in that. At least for now.
12368
12369 The DWP file can be made up of a random collection of CUs and TUs.
12370 However, for each CU + set of TUs that came from the same original DWO
12371 file, we can combine them back into a virtual DWO file to save space
12372 (fewer struct dwo_file objects to allocate). Remember that for really
12373 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12374
12375 std::string virtual_dwo_name =
12376 string_printf ("virtual-dwo/%d-%d-%d-%d",
12377 get_section_id (&sections.abbrev),
12378 get_section_id (&sections.line),
12379 get_section_id (&sections.loc),
12380 get_section_id (&sections.str_offsets));
12381 /* Can we use an existing virtual DWO file? */
12382 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12383 virtual_dwo_name.c_str (),
12384 comp_dir);
12385 /* Create one if necessary. */
12386 if (*dwo_file_slot == NULL)
12387 {
12388 if (dwarf_read_debug)
12389 {
12390 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12391 virtual_dwo_name.c_str ());
12392 }
12393 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12394 dwo_file->dwo_name
12395 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12396 virtual_dwo_name.c_str (),
12397 virtual_dwo_name.size ());
12398 dwo_file->comp_dir = comp_dir;
12399 dwo_file->sections.abbrev = sections.abbrev;
12400 dwo_file->sections.line = sections.line;
12401 dwo_file->sections.loc = sections.loc;
12402 dwo_file->sections.macinfo = sections.macinfo;
12403 dwo_file->sections.macro = sections.macro;
12404 dwo_file->sections.str_offsets = sections.str_offsets;
12405 /* The "str" section is global to the entire DWP file. */
12406 dwo_file->sections.str = dwp_file->sections.str;
12407 /* The info or types section is assigned below to dwo_unit,
12408 there's no need to record it in dwo_file.
12409 Also, we can't simply record type sections in dwo_file because
12410 we record a pointer into the vector in dwo_unit. As we collect more
12411 types we'll grow the vector and eventually have to reallocate space
12412 for it, invalidating all copies of pointers into the previous
12413 contents. */
12414 *dwo_file_slot = dwo_file;
12415 }
12416 else
12417 {
12418 if (dwarf_read_debug)
12419 {
12420 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12421 virtual_dwo_name.c_str ());
12422 }
12423 dwo_file = (struct dwo_file *) *dwo_file_slot;
12424 }
12425
12426 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12427 dwo_unit->dwo_file = dwo_file;
12428 dwo_unit->signature = signature;
12429 dwo_unit->section =
12430 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12431 *dwo_unit->section = sections.info_or_types;
12432 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12433
12434 return dwo_unit;
12435 }
12436
12437 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12438 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12439 piece within that section used by a TU/CU, return a virtual section
12440 of just that piece. */
12441
12442 static struct dwarf2_section_info
12443 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12444 struct dwarf2_section_info *section,
12445 bfd_size_type offset, bfd_size_type size)
12446 {
12447 struct dwarf2_section_info result;
12448 asection *sectp;
12449
12450 gdb_assert (section != NULL);
12451 gdb_assert (!section->is_virtual);
12452
12453 memset (&result, 0, sizeof (result));
12454 result.s.containing_section = section;
12455 result.is_virtual = 1;
12456
12457 if (size == 0)
12458 return result;
12459
12460 sectp = get_section_bfd_section (section);
12461
12462 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12463 bounds of the real section. This is a pretty-rare event, so just
12464 flag an error (easier) instead of a warning and trying to cope. */
12465 if (sectp == NULL
12466 || offset + size > bfd_get_section_size (sectp))
12467 {
12468 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12469 " in section %s [in module %s]"),
12470 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12471 objfile_name (dwarf2_per_objfile->objfile));
12472 }
12473
12474 result.virtual_offset = offset;
12475 result.size = size;
12476 return result;
12477 }
12478
12479 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12480 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12481 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12482 This is for DWP version 2 files. */
12483
12484 static struct dwo_unit *
12485 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12486 struct dwp_file *dwp_file,
12487 uint32_t unit_index,
12488 const char *comp_dir,
12489 ULONGEST signature, int is_debug_types)
12490 {
12491 struct objfile *objfile = dwarf2_per_objfile->objfile;
12492 const struct dwp_hash_table *dwp_htab =
12493 is_debug_types ? dwp_file->tus : dwp_file->cus;
12494 bfd *dbfd = dwp_file->dbfd.get ();
12495 const char *kind = is_debug_types ? "TU" : "CU";
12496 struct dwo_file *dwo_file;
12497 struct dwo_unit *dwo_unit;
12498 struct virtual_v2_dwo_sections sections;
12499 void **dwo_file_slot;
12500 int i;
12501
12502 gdb_assert (dwp_file->version == 2);
12503
12504 if (dwarf_read_debug)
12505 {
12506 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12507 kind,
12508 pulongest (unit_index), hex_string (signature),
12509 dwp_file->name);
12510 }
12511
12512 /* Fetch the section offsets of this DWO unit. */
12513
12514 memset (&sections, 0, sizeof (sections));
12515
12516 for (i = 0; i < dwp_htab->nr_columns; ++i)
12517 {
12518 uint32_t offset = read_4_bytes (dbfd,
12519 dwp_htab->section_pool.v2.offsets
12520 + (((unit_index - 1) * dwp_htab->nr_columns
12521 + i)
12522 * sizeof (uint32_t)));
12523 uint32_t size = read_4_bytes (dbfd,
12524 dwp_htab->section_pool.v2.sizes
12525 + (((unit_index - 1) * dwp_htab->nr_columns
12526 + i)
12527 * sizeof (uint32_t)));
12528
12529 switch (dwp_htab->section_pool.v2.section_ids[i])
12530 {
12531 case DW_SECT_INFO:
12532 case DW_SECT_TYPES:
12533 sections.info_or_types_offset = offset;
12534 sections.info_or_types_size = size;
12535 break;
12536 case DW_SECT_ABBREV:
12537 sections.abbrev_offset = offset;
12538 sections.abbrev_size = size;
12539 break;
12540 case DW_SECT_LINE:
12541 sections.line_offset = offset;
12542 sections.line_size = size;
12543 break;
12544 case DW_SECT_LOC:
12545 sections.loc_offset = offset;
12546 sections.loc_size = size;
12547 break;
12548 case DW_SECT_STR_OFFSETS:
12549 sections.str_offsets_offset = offset;
12550 sections.str_offsets_size = size;
12551 break;
12552 case DW_SECT_MACINFO:
12553 sections.macinfo_offset = offset;
12554 sections.macinfo_size = size;
12555 break;
12556 case DW_SECT_MACRO:
12557 sections.macro_offset = offset;
12558 sections.macro_size = size;
12559 break;
12560 }
12561 }
12562
12563 /* It's easier for the rest of the code if we fake a struct dwo_file and
12564 have dwo_unit "live" in that. At least for now.
12565
12566 The DWP file can be made up of a random collection of CUs and TUs.
12567 However, for each CU + set of TUs that came from the same original DWO
12568 file, we can combine them back into a virtual DWO file to save space
12569 (fewer struct dwo_file objects to allocate). Remember that for really
12570 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12571
12572 std::string virtual_dwo_name =
12573 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12574 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12575 (long) (sections.line_size ? sections.line_offset : 0),
12576 (long) (sections.loc_size ? sections.loc_offset : 0),
12577 (long) (sections.str_offsets_size
12578 ? sections.str_offsets_offset : 0));
12579 /* Can we use an existing virtual DWO file? */
12580 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12581 virtual_dwo_name.c_str (),
12582 comp_dir);
12583 /* Create one if necessary. */
12584 if (*dwo_file_slot == NULL)
12585 {
12586 if (dwarf_read_debug)
12587 {
12588 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12589 virtual_dwo_name.c_str ());
12590 }
12591 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12592 dwo_file->dwo_name
12593 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12594 virtual_dwo_name.c_str (),
12595 virtual_dwo_name.size ());
12596 dwo_file->comp_dir = comp_dir;
12597 dwo_file->sections.abbrev =
12598 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12599 sections.abbrev_offset, sections.abbrev_size);
12600 dwo_file->sections.line =
12601 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12602 sections.line_offset, sections.line_size);
12603 dwo_file->sections.loc =
12604 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12605 sections.loc_offset, sections.loc_size);
12606 dwo_file->sections.macinfo =
12607 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12608 sections.macinfo_offset, sections.macinfo_size);
12609 dwo_file->sections.macro =
12610 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12611 sections.macro_offset, sections.macro_size);
12612 dwo_file->sections.str_offsets =
12613 create_dwp_v2_section (dwarf2_per_objfile,
12614 &dwp_file->sections.str_offsets,
12615 sections.str_offsets_offset,
12616 sections.str_offsets_size);
12617 /* The "str" section is global to the entire DWP file. */
12618 dwo_file->sections.str = dwp_file->sections.str;
12619 /* The info or types section is assigned below to dwo_unit,
12620 there's no need to record it in dwo_file.
12621 Also, we can't simply record type sections in dwo_file because
12622 we record a pointer into the vector in dwo_unit. As we collect more
12623 types we'll grow the vector and eventually have to reallocate space
12624 for it, invalidating all copies of pointers into the previous
12625 contents. */
12626 *dwo_file_slot = dwo_file;
12627 }
12628 else
12629 {
12630 if (dwarf_read_debug)
12631 {
12632 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12633 virtual_dwo_name.c_str ());
12634 }
12635 dwo_file = (struct dwo_file *) *dwo_file_slot;
12636 }
12637
12638 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12639 dwo_unit->dwo_file = dwo_file;
12640 dwo_unit->signature = signature;
12641 dwo_unit->section =
12642 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12643 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12644 is_debug_types
12645 ? &dwp_file->sections.types
12646 : &dwp_file->sections.info,
12647 sections.info_or_types_offset,
12648 sections.info_or_types_size);
12649 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12650
12651 return dwo_unit;
12652 }
12653
12654 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12655 Returns NULL if the signature isn't found. */
12656
12657 static struct dwo_unit *
12658 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12659 struct dwp_file *dwp_file, const char *comp_dir,
12660 ULONGEST signature, int is_debug_types)
12661 {
12662 const struct dwp_hash_table *dwp_htab =
12663 is_debug_types ? dwp_file->tus : dwp_file->cus;
12664 bfd *dbfd = dwp_file->dbfd.get ();
12665 uint32_t mask = dwp_htab->nr_slots - 1;
12666 uint32_t hash = signature & mask;
12667 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12668 unsigned int i;
12669 void **slot;
12670 struct dwo_unit find_dwo_cu;
12671
12672 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12673 find_dwo_cu.signature = signature;
12674 slot = htab_find_slot (is_debug_types
12675 ? dwp_file->loaded_tus
12676 : dwp_file->loaded_cus,
12677 &find_dwo_cu, INSERT);
12678
12679 if (*slot != NULL)
12680 return (struct dwo_unit *) *slot;
12681
12682 /* Use a for loop so that we don't loop forever on bad debug info. */
12683 for (i = 0; i < dwp_htab->nr_slots; ++i)
12684 {
12685 ULONGEST signature_in_table;
12686
12687 signature_in_table =
12688 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12689 if (signature_in_table == signature)
12690 {
12691 uint32_t unit_index =
12692 read_4_bytes (dbfd,
12693 dwp_htab->unit_table + hash * sizeof (uint32_t));
12694
12695 if (dwp_file->version == 1)
12696 {
12697 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12698 dwp_file, unit_index,
12699 comp_dir, signature,
12700 is_debug_types);
12701 }
12702 else
12703 {
12704 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12705 dwp_file, unit_index,
12706 comp_dir, signature,
12707 is_debug_types);
12708 }
12709 return (struct dwo_unit *) *slot;
12710 }
12711 if (signature_in_table == 0)
12712 return NULL;
12713 hash = (hash + hash2) & mask;
12714 }
12715
12716 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12717 " [in module %s]"),
12718 dwp_file->name);
12719 }
12720
12721 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12722 Open the file specified by FILE_NAME and hand it off to BFD for
12723 preliminary analysis. Return a newly initialized bfd *, which
12724 includes a canonicalized copy of FILE_NAME.
12725 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12726 SEARCH_CWD is true if the current directory is to be searched.
12727 It will be searched before debug-file-directory.
12728 If successful, the file is added to the bfd include table of the
12729 objfile's bfd (see gdb_bfd_record_inclusion).
12730 If unable to find/open the file, return NULL.
12731 NOTE: This function is derived from symfile_bfd_open. */
12732
12733 static gdb_bfd_ref_ptr
12734 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12735 const char *file_name, int is_dwp, int search_cwd)
12736 {
12737 int desc;
12738 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12739 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12740 to debug_file_directory. */
12741 const char *search_path;
12742 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12743
12744 gdb::unique_xmalloc_ptr<char> search_path_holder;
12745 if (search_cwd)
12746 {
12747 if (*debug_file_directory != '\0')
12748 {
12749 search_path_holder.reset (concat (".", dirname_separator_string,
12750 debug_file_directory,
12751 (char *) NULL));
12752 search_path = search_path_holder.get ();
12753 }
12754 else
12755 search_path = ".";
12756 }
12757 else
12758 search_path = debug_file_directory;
12759
12760 openp_flags flags = OPF_RETURN_REALPATH;
12761 if (is_dwp)
12762 flags |= OPF_SEARCH_IN_PATH;
12763
12764 gdb::unique_xmalloc_ptr<char> absolute_name;
12765 desc = openp (search_path, flags, file_name,
12766 O_RDONLY | O_BINARY, &absolute_name);
12767 if (desc < 0)
12768 return NULL;
12769
12770 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12771 gnutarget, desc));
12772 if (sym_bfd == NULL)
12773 return NULL;
12774 bfd_set_cacheable (sym_bfd.get (), 1);
12775
12776 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12777 return NULL;
12778
12779 /* Success. Record the bfd as having been included by the objfile's bfd.
12780 This is important because things like demangled_names_hash lives in the
12781 objfile's per_bfd space and may have references to things like symbol
12782 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12783 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12784
12785 return sym_bfd;
12786 }
12787
12788 /* Try to open DWO file FILE_NAME.
12789 COMP_DIR is the DW_AT_comp_dir attribute.
12790 The result is the bfd handle of the file.
12791 If there is a problem finding or opening the file, return NULL.
12792 Upon success, the canonicalized path of the file is stored in the bfd,
12793 same as symfile_bfd_open. */
12794
12795 static gdb_bfd_ref_ptr
12796 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12797 const char *file_name, const char *comp_dir)
12798 {
12799 if (IS_ABSOLUTE_PATH (file_name))
12800 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12801 0 /*is_dwp*/, 0 /*search_cwd*/);
12802
12803 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12804
12805 if (comp_dir != NULL)
12806 {
12807 char *path_to_try = concat (comp_dir, SLASH_STRING,
12808 file_name, (char *) NULL);
12809
12810 /* NOTE: If comp_dir is a relative path, this will also try the
12811 search path, which seems useful. */
12812 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12813 path_to_try,
12814 0 /*is_dwp*/,
12815 1 /*search_cwd*/));
12816 xfree (path_to_try);
12817 if (abfd != NULL)
12818 return abfd;
12819 }
12820
12821 /* That didn't work, try debug-file-directory, which, despite its name,
12822 is a list of paths. */
12823
12824 if (*debug_file_directory == '\0')
12825 return NULL;
12826
12827 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12828 0 /*is_dwp*/, 1 /*search_cwd*/);
12829 }
12830
12831 /* This function is mapped across the sections and remembers the offset and
12832 size of each of the DWO debugging sections we are interested in. */
12833
12834 static void
12835 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12836 {
12837 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12838 const struct dwop_section_names *names = &dwop_section_names;
12839
12840 if (section_is_p (sectp->name, &names->abbrev_dwo))
12841 {
12842 dwo_sections->abbrev.s.section = sectp;
12843 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12844 }
12845 else if (section_is_p (sectp->name, &names->info_dwo))
12846 {
12847 dwo_sections->info.s.section = sectp;
12848 dwo_sections->info.size = bfd_get_section_size (sectp);
12849 }
12850 else if (section_is_p (sectp->name, &names->line_dwo))
12851 {
12852 dwo_sections->line.s.section = sectp;
12853 dwo_sections->line.size = bfd_get_section_size (sectp);
12854 }
12855 else if (section_is_p (sectp->name, &names->loc_dwo))
12856 {
12857 dwo_sections->loc.s.section = sectp;
12858 dwo_sections->loc.size = bfd_get_section_size (sectp);
12859 }
12860 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12861 {
12862 dwo_sections->macinfo.s.section = sectp;
12863 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12864 }
12865 else if (section_is_p (sectp->name, &names->macro_dwo))
12866 {
12867 dwo_sections->macro.s.section = sectp;
12868 dwo_sections->macro.size = bfd_get_section_size (sectp);
12869 }
12870 else if (section_is_p (sectp->name, &names->str_dwo))
12871 {
12872 dwo_sections->str.s.section = sectp;
12873 dwo_sections->str.size = bfd_get_section_size (sectp);
12874 }
12875 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12876 {
12877 dwo_sections->str_offsets.s.section = sectp;
12878 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12879 }
12880 else if (section_is_p (sectp->name, &names->types_dwo))
12881 {
12882 struct dwarf2_section_info type_section;
12883
12884 memset (&type_section, 0, sizeof (type_section));
12885 type_section.s.section = sectp;
12886 type_section.size = bfd_get_section_size (sectp);
12887 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12888 &type_section);
12889 }
12890 }
12891
12892 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12893 by PER_CU. This is for the non-DWP case.
12894 The result is NULL if DWO_NAME can't be found. */
12895
12896 static struct dwo_file *
12897 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12898 const char *dwo_name, const char *comp_dir)
12899 {
12900 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12901 struct objfile *objfile = dwarf2_per_objfile->objfile;
12902
12903 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
12904 if (dbfd == NULL)
12905 {
12906 if (dwarf_read_debug)
12907 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12908 return NULL;
12909 }
12910
12911 /* We use a unique pointer here, despite the obstack allocation,
12912 because a dwo_file needs some cleanup if it is abandoned. */
12913 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12914 struct dwo_file));
12915 dwo_file->dwo_name = dwo_name;
12916 dwo_file->comp_dir = comp_dir;
12917 dwo_file->dbfd = dbfd.release ();
12918
12919 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12920 &dwo_file->sections);
12921
12922 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12923 dwo_file->cus);
12924
12925 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12926 dwo_file->sections.types, dwo_file->tus);
12927
12928 if (dwarf_read_debug)
12929 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12930
12931 return dwo_file.release ();
12932 }
12933
12934 /* This function is mapped across the sections and remembers the offset and
12935 size of each of the DWP debugging sections common to version 1 and 2 that
12936 we are interested in. */
12937
12938 static void
12939 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12940 void *dwp_file_ptr)
12941 {
12942 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12943 const struct dwop_section_names *names = &dwop_section_names;
12944 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12945
12946 /* Record the ELF section number for later lookup: this is what the
12947 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12948 gdb_assert (elf_section_nr < dwp_file->num_sections);
12949 dwp_file->elf_sections[elf_section_nr] = sectp;
12950
12951 /* Look for specific sections that we need. */
12952 if (section_is_p (sectp->name, &names->str_dwo))
12953 {
12954 dwp_file->sections.str.s.section = sectp;
12955 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12956 }
12957 else if (section_is_p (sectp->name, &names->cu_index))
12958 {
12959 dwp_file->sections.cu_index.s.section = sectp;
12960 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12961 }
12962 else if (section_is_p (sectp->name, &names->tu_index))
12963 {
12964 dwp_file->sections.tu_index.s.section = sectp;
12965 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12966 }
12967 }
12968
12969 /* This function is mapped across the sections and remembers the offset and
12970 size of each of the DWP version 2 debugging sections that we are interested
12971 in. This is split into a separate function because we don't know if we
12972 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12973
12974 static void
12975 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12976 {
12977 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12978 const struct dwop_section_names *names = &dwop_section_names;
12979 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12980
12981 /* Record the ELF section number for later lookup: this is what the
12982 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12983 gdb_assert (elf_section_nr < dwp_file->num_sections);
12984 dwp_file->elf_sections[elf_section_nr] = sectp;
12985
12986 /* Look for specific sections that we need. */
12987 if (section_is_p (sectp->name, &names->abbrev_dwo))
12988 {
12989 dwp_file->sections.abbrev.s.section = sectp;
12990 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
12991 }
12992 else if (section_is_p (sectp->name, &names->info_dwo))
12993 {
12994 dwp_file->sections.info.s.section = sectp;
12995 dwp_file->sections.info.size = bfd_get_section_size (sectp);
12996 }
12997 else if (section_is_p (sectp->name, &names->line_dwo))
12998 {
12999 dwp_file->sections.line.s.section = sectp;
13000 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13001 }
13002 else if (section_is_p (sectp->name, &names->loc_dwo))
13003 {
13004 dwp_file->sections.loc.s.section = sectp;
13005 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13006 }
13007 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13008 {
13009 dwp_file->sections.macinfo.s.section = sectp;
13010 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13011 }
13012 else if (section_is_p (sectp->name, &names->macro_dwo))
13013 {
13014 dwp_file->sections.macro.s.section = sectp;
13015 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13016 }
13017 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13018 {
13019 dwp_file->sections.str_offsets.s.section = sectp;
13020 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13021 }
13022 else if (section_is_p (sectp->name, &names->types_dwo))
13023 {
13024 dwp_file->sections.types.s.section = sectp;
13025 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13026 }
13027 }
13028
13029 /* Hash function for dwp_file loaded CUs/TUs. */
13030
13031 static hashval_t
13032 hash_dwp_loaded_cutus (const void *item)
13033 {
13034 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13035
13036 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13037 return dwo_unit->signature;
13038 }
13039
13040 /* Equality function for dwp_file loaded CUs/TUs. */
13041
13042 static int
13043 eq_dwp_loaded_cutus (const void *a, const void *b)
13044 {
13045 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13046 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13047
13048 return dua->signature == dub->signature;
13049 }
13050
13051 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13052
13053 static htab_t
13054 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13055 {
13056 return htab_create_alloc_ex (3,
13057 hash_dwp_loaded_cutus,
13058 eq_dwp_loaded_cutus,
13059 NULL,
13060 &objfile->objfile_obstack,
13061 hashtab_obstack_allocate,
13062 dummy_obstack_deallocate);
13063 }
13064
13065 /* Try to open DWP file FILE_NAME.
13066 The result is the bfd handle of the file.
13067 If there is a problem finding or opening the file, return NULL.
13068 Upon success, the canonicalized path of the file is stored in the bfd,
13069 same as symfile_bfd_open. */
13070
13071 static gdb_bfd_ref_ptr
13072 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13073 const char *file_name)
13074 {
13075 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13076 1 /*is_dwp*/,
13077 1 /*search_cwd*/));
13078 if (abfd != NULL)
13079 return abfd;
13080
13081 /* Work around upstream bug 15652.
13082 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13083 [Whether that's a "bug" is debatable, but it is getting in our way.]
13084 We have no real idea where the dwp file is, because gdb's realpath-ing
13085 of the executable's path may have discarded the needed info.
13086 [IWBN if the dwp file name was recorded in the executable, akin to
13087 .gnu_debuglink, but that doesn't exist yet.]
13088 Strip the directory from FILE_NAME and search again. */
13089 if (*debug_file_directory != '\0')
13090 {
13091 /* Don't implicitly search the current directory here.
13092 If the user wants to search "." to handle this case,
13093 it must be added to debug-file-directory. */
13094 return try_open_dwop_file (dwarf2_per_objfile,
13095 lbasename (file_name), 1 /*is_dwp*/,
13096 0 /*search_cwd*/);
13097 }
13098
13099 return NULL;
13100 }
13101
13102 /* Initialize the use of the DWP file for the current objfile.
13103 By convention the name of the DWP file is ${objfile}.dwp.
13104 The result is NULL if it can't be found. */
13105
13106 static std::unique_ptr<struct dwp_file>
13107 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13108 {
13109 struct objfile *objfile = dwarf2_per_objfile->objfile;
13110
13111 /* Try to find first .dwp for the binary file before any symbolic links
13112 resolving. */
13113
13114 /* If the objfile is a debug file, find the name of the real binary
13115 file and get the name of dwp file from there. */
13116 std::string dwp_name;
13117 if (objfile->separate_debug_objfile_backlink != NULL)
13118 {
13119 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13120 const char *backlink_basename = lbasename (backlink->original_name);
13121
13122 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13123 }
13124 else
13125 dwp_name = objfile->original_name;
13126
13127 dwp_name += ".dwp";
13128
13129 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13130 if (dbfd == NULL
13131 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13132 {
13133 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13134 dwp_name = objfile_name (objfile);
13135 dwp_name += ".dwp";
13136 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13137 }
13138
13139 if (dbfd == NULL)
13140 {
13141 if (dwarf_read_debug)
13142 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13143 return std::unique_ptr<dwp_file> ();
13144 }
13145
13146 const char *name = bfd_get_filename (dbfd.get ());
13147 std::unique_ptr<struct dwp_file> dwp_file
13148 (new struct dwp_file (name, std::move (dbfd)));
13149
13150 /* +1: section 0 is unused */
13151 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13152 dwp_file->elf_sections =
13153 OBSTACK_CALLOC (&objfile->objfile_obstack,
13154 dwp_file->num_sections, asection *);
13155
13156 bfd_map_over_sections (dwp_file->dbfd.get (),
13157 dwarf2_locate_common_dwp_sections,
13158 dwp_file.get ());
13159
13160 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13161 0);
13162
13163 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13164 1);
13165
13166 /* The DWP file version is stored in the hash table. Oh well. */
13167 if (dwp_file->cus && dwp_file->tus
13168 && dwp_file->cus->version != dwp_file->tus->version)
13169 {
13170 /* Technically speaking, we should try to limp along, but this is
13171 pretty bizarre. We use pulongest here because that's the established
13172 portability solution (e.g, we cannot use %u for uint32_t). */
13173 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13174 " TU version %s [in DWP file %s]"),
13175 pulongest (dwp_file->cus->version),
13176 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13177 }
13178
13179 if (dwp_file->cus)
13180 dwp_file->version = dwp_file->cus->version;
13181 else if (dwp_file->tus)
13182 dwp_file->version = dwp_file->tus->version;
13183 else
13184 dwp_file->version = 2;
13185
13186 if (dwp_file->version == 2)
13187 bfd_map_over_sections (dwp_file->dbfd.get (),
13188 dwarf2_locate_v2_dwp_sections,
13189 dwp_file.get ());
13190
13191 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13192 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13193
13194 if (dwarf_read_debug)
13195 {
13196 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13197 fprintf_unfiltered (gdb_stdlog,
13198 " %s CUs, %s TUs\n",
13199 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13200 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13201 }
13202
13203 return dwp_file;
13204 }
13205
13206 /* Wrapper around open_and_init_dwp_file, only open it once. */
13207
13208 static struct dwp_file *
13209 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13210 {
13211 if (! dwarf2_per_objfile->dwp_checked)
13212 {
13213 dwarf2_per_objfile->dwp_file
13214 = open_and_init_dwp_file (dwarf2_per_objfile);
13215 dwarf2_per_objfile->dwp_checked = 1;
13216 }
13217 return dwarf2_per_objfile->dwp_file.get ();
13218 }
13219
13220 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13221 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13222 or in the DWP file for the objfile, referenced by THIS_UNIT.
13223 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13224 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13225
13226 This is called, for example, when wanting to read a variable with a
13227 complex location. Therefore we don't want to do file i/o for every call.
13228 Therefore we don't want to look for a DWO file on every call.
13229 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13230 then we check if we've already seen DWO_NAME, and only THEN do we check
13231 for a DWO file.
13232
13233 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13234 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13235
13236 static struct dwo_unit *
13237 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13238 const char *dwo_name, const char *comp_dir,
13239 ULONGEST signature, int is_debug_types)
13240 {
13241 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13242 struct objfile *objfile = dwarf2_per_objfile->objfile;
13243 const char *kind = is_debug_types ? "TU" : "CU";
13244 void **dwo_file_slot;
13245 struct dwo_file *dwo_file;
13246 struct dwp_file *dwp_file;
13247
13248 /* First see if there's a DWP file.
13249 If we have a DWP file but didn't find the DWO inside it, don't
13250 look for the original DWO file. It makes gdb behave differently
13251 depending on whether one is debugging in the build tree. */
13252
13253 dwp_file = get_dwp_file (dwarf2_per_objfile);
13254 if (dwp_file != NULL)
13255 {
13256 const struct dwp_hash_table *dwp_htab =
13257 is_debug_types ? dwp_file->tus : dwp_file->cus;
13258
13259 if (dwp_htab != NULL)
13260 {
13261 struct dwo_unit *dwo_cutu =
13262 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13263 signature, is_debug_types);
13264
13265 if (dwo_cutu != NULL)
13266 {
13267 if (dwarf_read_debug)
13268 {
13269 fprintf_unfiltered (gdb_stdlog,
13270 "Virtual DWO %s %s found: @%s\n",
13271 kind, hex_string (signature),
13272 host_address_to_string (dwo_cutu));
13273 }
13274 return dwo_cutu;
13275 }
13276 }
13277 }
13278 else
13279 {
13280 /* No DWP file, look for the DWO file. */
13281
13282 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13283 dwo_name, comp_dir);
13284 if (*dwo_file_slot == NULL)
13285 {
13286 /* Read in the file and build a table of the CUs/TUs it contains. */
13287 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13288 }
13289 /* NOTE: This will be NULL if unable to open the file. */
13290 dwo_file = (struct dwo_file *) *dwo_file_slot;
13291
13292 if (dwo_file != NULL)
13293 {
13294 struct dwo_unit *dwo_cutu = NULL;
13295
13296 if (is_debug_types && dwo_file->tus)
13297 {
13298 struct dwo_unit find_dwo_cutu;
13299
13300 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13301 find_dwo_cutu.signature = signature;
13302 dwo_cutu
13303 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13304 }
13305 else if (!is_debug_types && dwo_file->cus)
13306 {
13307 struct dwo_unit find_dwo_cutu;
13308
13309 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13310 find_dwo_cutu.signature = signature;
13311 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13312 &find_dwo_cutu);
13313 }
13314
13315 if (dwo_cutu != NULL)
13316 {
13317 if (dwarf_read_debug)
13318 {
13319 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13320 kind, dwo_name, hex_string (signature),
13321 host_address_to_string (dwo_cutu));
13322 }
13323 return dwo_cutu;
13324 }
13325 }
13326 }
13327
13328 /* We didn't find it. This could mean a dwo_id mismatch, or
13329 someone deleted the DWO/DWP file, or the search path isn't set up
13330 correctly to find the file. */
13331
13332 if (dwarf_read_debug)
13333 {
13334 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13335 kind, dwo_name, hex_string (signature));
13336 }
13337
13338 /* This is a warning and not a complaint because it can be caused by
13339 pilot error (e.g., user accidentally deleting the DWO). */
13340 {
13341 /* Print the name of the DWP file if we looked there, helps the user
13342 better diagnose the problem. */
13343 std::string dwp_text;
13344
13345 if (dwp_file != NULL)
13346 dwp_text = string_printf (" [in DWP file %s]",
13347 lbasename (dwp_file->name));
13348
13349 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13350 " [in module %s]"),
13351 kind, dwo_name, hex_string (signature),
13352 dwp_text.c_str (),
13353 this_unit->is_debug_types ? "TU" : "CU",
13354 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13355 }
13356 return NULL;
13357 }
13358
13359 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13360 See lookup_dwo_cutu_unit for details. */
13361
13362 static struct dwo_unit *
13363 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13364 const char *dwo_name, const char *comp_dir,
13365 ULONGEST signature)
13366 {
13367 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13368 }
13369
13370 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13371 See lookup_dwo_cutu_unit for details. */
13372
13373 static struct dwo_unit *
13374 lookup_dwo_type_unit (struct signatured_type *this_tu,
13375 const char *dwo_name, const char *comp_dir)
13376 {
13377 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13378 }
13379
13380 /* Traversal function for queue_and_load_all_dwo_tus. */
13381
13382 static int
13383 queue_and_load_dwo_tu (void **slot, void *info)
13384 {
13385 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13386 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13387 ULONGEST signature = dwo_unit->signature;
13388 struct signatured_type *sig_type =
13389 lookup_dwo_signatured_type (per_cu->cu, signature);
13390
13391 if (sig_type != NULL)
13392 {
13393 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13394
13395 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13396 a real dependency of PER_CU on SIG_TYPE. That is detected later
13397 while processing PER_CU. */
13398 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13399 load_full_type_unit (sig_cu);
13400 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13401 }
13402
13403 return 1;
13404 }
13405
13406 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13407 The DWO may have the only definition of the type, though it may not be
13408 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13409 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13410
13411 static void
13412 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13413 {
13414 struct dwo_unit *dwo_unit;
13415 struct dwo_file *dwo_file;
13416
13417 gdb_assert (!per_cu->is_debug_types);
13418 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13419 gdb_assert (per_cu->cu != NULL);
13420
13421 dwo_unit = per_cu->cu->dwo_unit;
13422 gdb_assert (dwo_unit != NULL);
13423
13424 dwo_file = dwo_unit->dwo_file;
13425 if (dwo_file->tus != NULL)
13426 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13427 }
13428
13429 /* Free all resources associated with DWO_FILE.
13430 Close the DWO file and munmap the sections. */
13431
13432 static void
13433 free_dwo_file (struct dwo_file *dwo_file)
13434 {
13435 /* Note: dbfd is NULL for virtual DWO files. */
13436 gdb_bfd_unref (dwo_file->dbfd);
13437
13438 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13439 }
13440
13441 /* Traversal function for free_dwo_files. */
13442
13443 static int
13444 free_dwo_file_from_slot (void **slot, void *info)
13445 {
13446 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13447
13448 free_dwo_file (dwo_file);
13449
13450 return 1;
13451 }
13452
13453 /* Free all resources associated with DWO_FILES. */
13454
13455 static void
13456 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13457 {
13458 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13459 }
13460 \f
13461 /* Read in various DIEs. */
13462
13463 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13464 Inherit only the children of the DW_AT_abstract_origin DIE not being
13465 already referenced by DW_AT_abstract_origin from the children of the
13466 current DIE. */
13467
13468 static void
13469 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13470 {
13471 struct die_info *child_die;
13472 sect_offset *offsetp;
13473 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13474 struct die_info *origin_die;
13475 /* Iterator of the ORIGIN_DIE children. */
13476 struct die_info *origin_child_die;
13477 struct attribute *attr;
13478 struct dwarf2_cu *origin_cu;
13479 struct pending **origin_previous_list_in_scope;
13480
13481 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13482 if (!attr)
13483 return;
13484
13485 /* Note that following die references may follow to a die in a
13486 different cu. */
13487
13488 origin_cu = cu;
13489 origin_die = follow_die_ref (die, attr, &origin_cu);
13490
13491 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13492 symbols in. */
13493 origin_previous_list_in_scope = origin_cu->list_in_scope;
13494 origin_cu->list_in_scope = cu->list_in_scope;
13495
13496 if (die->tag != origin_die->tag
13497 && !(die->tag == DW_TAG_inlined_subroutine
13498 && origin_die->tag == DW_TAG_subprogram))
13499 complaint (&symfile_complaints,
13500 _("DIE %s and its abstract origin %s have different tags"),
13501 sect_offset_str (die->sect_off),
13502 sect_offset_str (origin_die->sect_off));
13503
13504 std::vector<sect_offset> offsets;
13505
13506 for (child_die = die->child;
13507 child_die && child_die->tag;
13508 child_die = sibling_die (child_die))
13509 {
13510 struct die_info *child_origin_die;
13511 struct dwarf2_cu *child_origin_cu;
13512
13513 /* We are trying to process concrete instance entries:
13514 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13515 it's not relevant to our analysis here. i.e. detecting DIEs that are
13516 present in the abstract instance but not referenced in the concrete
13517 one. */
13518 if (child_die->tag == DW_TAG_call_site
13519 || child_die->tag == DW_TAG_GNU_call_site)
13520 continue;
13521
13522 /* For each CHILD_DIE, find the corresponding child of
13523 ORIGIN_DIE. If there is more than one layer of
13524 DW_AT_abstract_origin, follow them all; there shouldn't be,
13525 but GCC versions at least through 4.4 generate this (GCC PR
13526 40573). */
13527 child_origin_die = child_die;
13528 child_origin_cu = cu;
13529 while (1)
13530 {
13531 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13532 child_origin_cu);
13533 if (attr == NULL)
13534 break;
13535 child_origin_die = follow_die_ref (child_origin_die, attr,
13536 &child_origin_cu);
13537 }
13538
13539 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13540 counterpart may exist. */
13541 if (child_origin_die != child_die)
13542 {
13543 if (child_die->tag != child_origin_die->tag
13544 && !(child_die->tag == DW_TAG_inlined_subroutine
13545 && child_origin_die->tag == DW_TAG_subprogram))
13546 complaint (&symfile_complaints,
13547 _("Child DIE %s and its abstract origin %s have "
13548 "different tags"),
13549 sect_offset_str (child_die->sect_off),
13550 sect_offset_str (child_origin_die->sect_off));
13551 if (child_origin_die->parent != origin_die)
13552 complaint (&symfile_complaints,
13553 _("Child DIE %s and its abstract origin %s have "
13554 "different parents"),
13555 sect_offset_str (child_die->sect_off),
13556 sect_offset_str (child_origin_die->sect_off));
13557 else
13558 offsets.push_back (child_origin_die->sect_off);
13559 }
13560 }
13561 std::sort (offsets.begin (), offsets.end ());
13562 sect_offset *offsets_end = offsets.data () + offsets.size ();
13563 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13564 if (offsetp[-1] == *offsetp)
13565 complaint (&symfile_complaints,
13566 _("Multiple children of DIE %s refer "
13567 "to DIE %s as their abstract origin"),
13568 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13569
13570 offsetp = offsets.data ();
13571 origin_child_die = origin_die->child;
13572 while (origin_child_die && origin_child_die->tag)
13573 {
13574 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13575 while (offsetp < offsets_end
13576 && *offsetp < origin_child_die->sect_off)
13577 offsetp++;
13578 if (offsetp >= offsets_end
13579 || *offsetp > origin_child_die->sect_off)
13580 {
13581 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13582 Check whether we're already processing ORIGIN_CHILD_DIE.
13583 This can happen with mutually referenced abstract_origins.
13584 PR 16581. */
13585 if (!origin_child_die->in_process)
13586 process_die (origin_child_die, origin_cu);
13587 }
13588 origin_child_die = sibling_die (origin_child_die);
13589 }
13590 origin_cu->list_in_scope = origin_previous_list_in_scope;
13591 }
13592
13593 static void
13594 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13595 {
13596 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13597 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13598 struct context_stack *newobj;
13599 CORE_ADDR lowpc;
13600 CORE_ADDR highpc;
13601 struct die_info *child_die;
13602 struct attribute *attr, *call_line, *call_file;
13603 const char *name;
13604 CORE_ADDR baseaddr;
13605 struct block *block;
13606 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13607 std::vector<struct symbol *> template_args;
13608 struct template_symbol *templ_func = NULL;
13609
13610 if (inlined_func)
13611 {
13612 /* If we do not have call site information, we can't show the
13613 caller of this inlined function. That's too confusing, so
13614 only use the scope for local variables. */
13615 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13616 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13617 if (call_line == NULL || call_file == NULL)
13618 {
13619 read_lexical_block_scope (die, cu);
13620 return;
13621 }
13622 }
13623
13624 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13625
13626 name = dwarf2_name (die, cu);
13627
13628 /* Ignore functions with missing or empty names. These are actually
13629 illegal according to the DWARF standard. */
13630 if (name == NULL)
13631 {
13632 complaint (&symfile_complaints,
13633 _("missing name for subprogram DIE at %s"),
13634 sect_offset_str (die->sect_off));
13635 return;
13636 }
13637
13638 /* Ignore functions with missing or invalid low and high pc attributes. */
13639 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13640 <= PC_BOUNDS_INVALID)
13641 {
13642 attr = dwarf2_attr (die, DW_AT_external, cu);
13643 if (!attr || !DW_UNSND (attr))
13644 complaint (&symfile_complaints,
13645 _("cannot get low and high bounds "
13646 "for subprogram DIE at %s"),
13647 sect_offset_str (die->sect_off));
13648 return;
13649 }
13650
13651 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13652 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13653
13654 /* If we have any template arguments, then we must allocate a
13655 different sort of symbol. */
13656 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13657 {
13658 if (child_die->tag == DW_TAG_template_type_param
13659 || child_die->tag == DW_TAG_template_value_param)
13660 {
13661 templ_func = allocate_template_symbol (objfile);
13662 templ_func->subclass = SYMBOL_TEMPLATE;
13663 break;
13664 }
13665 }
13666
13667 newobj = push_context (0, lowpc);
13668 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13669 (struct symbol *) templ_func);
13670
13671 /* If there is a location expression for DW_AT_frame_base, record
13672 it. */
13673 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13674 if (attr)
13675 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13676
13677 /* If there is a location for the static link, record it. */
13678 newobj->static_link = NULL;
13679 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13680 if (attr)
13681 {
13682 newobj->static_link
13683 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13684 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13685 }
13686
13687 cu->list_in_scope = &local_symbols;
13688
13689 if (die->child != NULL)
13690 {
13691 child_die = die->child;
13692 while (child_die && child_die->tag)
13693 {
13694 if (child_die->tag == DW_TAG_template_type_param
13695 || child_die->tag == DW_TAG_template_value_param)
13696 {
13697 struct symbol *arg = new_symbol (child_die, NULL, cu);
13698
13699 if (arg != NULL)
13700 template_args.push_back (arg);
13701 }
13702 else
13703 process_die (child_die, cu);
13704 child_die = sibling_die (child_die);
13705 }
13706 }
13707
13708 inherit_abstract_dies (die, cu);
13709
13710 /* If we have a DW_AT_specification, we might need to import using
13711 directives from the context of the specification DIE. See the
13712 comment in determine_prefix. */
13713 if (cu->language == language_cplus
13714 && dwarf2_attr (die, DW_AT_specification, cu))
13715 {
13716 struct dwarf2_cu *spec_cu = cu;
13717 struct die_info *spec_die = die_specification (die, &spec_cu);
13718
13719 while (spec_die)
13720 {
13721 child_die = spec_die->child;
13722 while (child_die && child_die->tag)
13723 {
13724 if (child_die->tag == DW_TAG_imported_module)
13725 process_die (child_die, spec_cu);
13726 child_die = sibling_die (child_die);
13727 }
13728
13729 /* In some cases, GCC generates specification DIEs that
13730 themselves contain DW_AT_specification attributes. */
13731 spec_die = die_specification (spec_die, &spec_cu);
13732 }
13733 }
13734
13735 newobj = pop_context ();
13736 /* Make a block for the local symbols within. */
13737 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13738 newobj->static_link, lowpc, highpc);
13739
13740 /* For C++, set the block's scope. */
13741 if ((cu->language == language_cplus
13742 || cu->language == language_fortran
13743 || cu->language == language_d
13744 || cu->language == language_rust)
13745 && cu->processing_has_namespace_info)
13746 block_set_scope (block, determine_prefix (die, cu),
13747 &objfile->objfile_obstack);
13748
13749 /* If we have address ranges, record them. */
13750 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13751
13752 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13753
13754 /* Attach template arguments to function. */
13755 if (!template_args.empty ())
13756 {
13757 gdb_assert (templ_func != NULL);
13758
13759 templ_func->n_template_arguments = template_args.size ();
13760 templ_func->template_arguments
13761 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13762 templ_func->n_template_arguments);
13763 memcpy (templ_func->template_arguments,
13764 template_args.data (),
13765 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13766 }
13767
13768 /* In C++, we can have functions nested inside functions (e.g., when
13769 a function declares a class that has methods). This means that
13770 when we finish processing a function scope, we may need to go
13771 back to building a containing block's symbol lists. */
13772 local_symbols = newobj->locals;
13773 local_using_directives = newobj->local_using_directives;
13774
13775 /* If we've finished processing a top-level function, subsequent
13776 symbols go in the file symbol list. */
13777 if (outermost_context_p ())
13778 cu->list_in_scope = &file_symbols;
13779 }
13780
13781 /* Process all the DIES contained within a lexical block scope. Start
13782 a new scope, process the dies, and then close the scope. */
13783
13784 static void
13785 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13786 {
13787 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13788 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13789 struct context_stack *newobj;
13790 CORE_ADDR lowpc, highpc;
13791 struct die_info *child_die;
13792 CORE_ADDR baseaddr;
13793
13794 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13795
13796 /* Ignore blocks with missing or invalid low and high pc attributes. */
13797 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13798 as multiple lexical blocks? Handling children in a sane way would
13799 be nasty. Might be easier to properly extend generic blocks to
13800 describe ranges. */
13801 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13802 {
13803 case PC_BOUNDS_NOT_PRESENT:
13804 /* DW_TAG_lexical_block has no attributes, process its children as if
13805 there was no wrapping by that DW_TAG_lexical_block.
13806 GCC does no longer produces such DWARF since GCC r224161. */
13807 for (child_die = die->child;
13808 child_die != NULL && child_die->tag;
13809 child_die = sibling_die (child_die))
13810 process_die (child_die, cu);
13811 return;
13812 case PC_BOUNDS_INVALID:
13813 return;
13814 }
13815 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13816 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13817
13818 push_context (0, lowpc);
13819 if (die->child != NULL)
13820 {
13821 child_die = die->child;
13822 while (child_die && child_die->tag)
13823 {
13824 process_die (child_die, cu);
13825 child_die = sibling_die (child_die);
13826 }
13827 }
13828 inherit_abstract_dies (die, cu);
13829 newobj = pop_context ();
13830
13831 if (local_symbols != NULL || local_using_directives != NULL)
13832 {
13833 struct block *block
13834 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13835 newobj->start_addr, highpc);
13836
13837 /* Note that recording ranges after traversing children, as we
13838 do here, means that recording a parent's ranges entails
13839 walking across all its children's ranges as they appear in
13840 the address map, which is quadratic behavior.
13841
13842 It would be nicer to record the parent's ranges before
13843 traversing its children, simply overriding whatever you find
13844 there. But since we don't even decide whether to create a
13845 block until after we've traversed its children, that's hard
13846 to do. */
13847 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13848 }
13849 local_symbols = newobj->locals;
13850 local_using_directives = newobj->local_using_directives;
13851 }
13852
13853 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13854
13855 static void
13856 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13857 {
13858 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13859 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13860 CORE_ADDR pc, baseaddr;
13861 struct attribute *attr;
13862 struct call_site *call_site, call_site_local;
13863 void **slot;
13864 int nparams;
13865 struct die_info *child_die;
13866
13867 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13868
13869 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13870 if (attr == NULL)
13871 {
13872 /* This was a pre-DWARF-5 GNU extension alias
13873 for DW_AT_call_return_pc. */
13874 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13875 }
13876 if (!attr)
13877 {
13878 complaint (&symfile_complaints,
13879 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
13880 "DIE %s [in module %s]"),
13881 sect_offset_str (die->sect_off), objfile_name (objfile));
13882 return;
13883 }
13884 pc = attr_value_as_address (attr) + baseaddr;
13885 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13886
13887 if (cu->call_site_htab == NULL)
13888 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13889 NULL, &objfile->objfile_obstack,
13890 hashtab_obstack_allocate, NULL);
13891 call_site_local.pc = pc;
13892 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13893 if (*slot != NULL)
13894 {
13895 complaint (&symfile_complaints,
13896 _("Duplicate PC %s for DW_TAG_call_site "
13897 "DIE %s [in module %s]"),
13898 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13899 objfile_name (objfile));
13900 return;
13901 }
13902
13903 /* Count parameters at the caller. */
13904
13905 nparams = 0;
13906 for (child_die = die->child; child_die && child_die->tag;
13907 child_die = sibling_die (child_die))
13908 {
13909 if (child_die->tag != DW_TAG_call_site_parameter
13910 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13911 {
13912 complaint (&symfile_complaints,
13913 _("Tag %d is not DW_TAG_call_site_parameter in "
13914 "DW_TAG_call_site child DIE %s [in module %s]"),
13915 child_die->tag, sect_offset_str (child_die->sect_off),
13916 objfile_name (objfile));
13917 continue;
13918 }
13919
13920 nparams++;
13921 }
13922
13923 call_site
13924 = ((struct call_site *)
13925 obstack_alloc (&objfile->objfile_obstack,
13926 sizeof (*call_site)
13927 + (sizeof (*call_site->parameter) * (nparams - 1))));
13928 *slot = call_site;
13929 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13930 call_site->pc = pc;
13931
13932 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13933 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13934 {
13935 struct die_info *func_die;
13936
13937 /* Skip also over DW_TAG_inlined_subroutine. */
13938 for (func_die = die->parent;
13939 func_die && func_die->tag != DW_TAG_subprogram
13940 && func_die->tag != DW_TAG_subroutine_type;
13941 func_die = func_die->parent);
13942
13943 /* DW_AT_call_all_calls is a superset
13944 of DW_AT_call_all_tail_calls. */
13945 if (func_die
13946 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13947 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13948 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13949 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13950 {
13951 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13952 not complete. But keep CALL_SITE for look ups via call_site_htab,
13953 both the initial caller containing the real return address PC and
13954 the final callee containing the current PC of a chain of tail
13955 calls do not need to have the tail call list complete. But any
13956 function candidate for a virtual tail call frame searched via
13957 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13958 determined unambiguously. */
13959 }
13960 else
13961 {
13962 struct type *func_type = NULL;
13963
13964 if (func_die)
13965 func_type = get_die_type (func_die, cu);
13966 if (func_type != NULL)
13967 {
13968 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13969
13970 /* Enlist this call site to the function. */
13971 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13972 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13973 }
13974 else
13975 complaint (&symfile_complaints,
13976 _("Cannot find function owning DW_TAG_call_site "
13977 "DIE %s [in module %s]"),
13978 sect_offset_str (die->sect_off), objfile_name (objfile));
13979 }
13980 }
13981
13982 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13983 if (attr == NULL)
13984 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13985 if (attr == NULL)
13986 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13987 if (attr == NULL)
13988 {
13989 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13990 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13991 }
13992 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13993 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
13994 /* Keep NULL DWARF_BLOCK. */;
13995 else if (attr_form_is_block (attr))
13996 {
13997 struct dwarf2_locexpr_baton *dlbaton;
13998
13999 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14000 dlbaton->data = DW_BLOCK (attr)->data;
14001 dlbaton->size = DW_BLOCK (attr)->size;
14002 dlbaton->per_cu = cu->per_cu;
14003
14004 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14005 }
14006 else if (attr_form_is_ref (attr))
14007 {
14008 struct dwarf2_cu *target_cu = cu;
14009 struct die_info *target_die;
14010
14011 target_die = follow_die_ref (die, attr, &target_cu);
14012 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14013 if (die_is_declaration (target_die, target_cu))
14014 {
14015 const char *target_physname;
14016
14017 /* Prefer the mangled name; otherwise compute the demangled one. */
14018 target_physname = dw2_linkage_name (target_die, target_cu);
14019 if (target_physname == NULL)
14020 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14021 if (target_physname == NULL)
14022 complaint (&symfile_complaints,
14023 _("DW_AT_call_target target DIE has invalid "
14024 "physname, for referencing DIE %s [in module %s]"),
14025 sect_offset_str (die->sect_off), objfile_name (objfile));
14026 else
14027 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14028 }
14029 else
14030 {
14031 CORE_ADDR lowpc;
14032
14033 /* DW_AT_entry_pc should be preferred. */
14034 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14035 <= PC_BOUNDS_INVALID)
14036 complaint (&symfile_complaints,
14037 _("DW_AT_call_target target DIE has invalid "
14038 "low pc, for referencing DIE %s [in module %s]"),
14039 sect_offset_str (die->sect_off), objfile_name (objfile));
14040 else
14041 {
14042 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14043 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14044 }
14045 }
14046 }
14047 else
14048 complaint (&symfile_complaints,
14049 _("DW_TAG_call_site DW_AT_call_target is neither "
14050 "block nor reference, for DIE %s [in module %s]"),
14051 sect_offset_str (die->sect_off), objfile_name (objfile));
14052
14053 call_site->per_cu = cu->per_cu;
14054
14055 for (child_die = die->child;
14056 child_die && child_die->tag;
14057 child_die = sibling_die (child_die))
14058 {
14059 struct call_site_parameter *parameter;
14060 struct attribute *loc, *origin;
14061
14062 if (child_die->tag != DW_TAG_call_site_parameter
14063 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14064 {
14065 /* Already printed the complaint above. */
14066 continue;
14067 }
14068
14069 gdb_assert (call_site->parameter_count < nparams);
14070 parameter = &call_site->parameter[call_site->parameter_count];
14071
14072 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14073 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14074 register is contained in DW_AT_call_value. */
14075
14076 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14077 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14078 if (origin == NULL)
14079 {
14080 /* This was a pre-DWARF-5 GNU extension alias
14081 for DW_AT_call_parameter. */
14082 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14083 }
14084 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14085 {
14086 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14087
14088 sect_offset sect_off
14089 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14090 if (!offset_in_cu_p (&cu->header, sect_off))
14091 {
14092 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14093 binding can be done only inside one CU. Such referenced DIE
14094 therefore cannot be even moved to DW_TAG_partial_unit. */
14095 complaint (&symfile_complaints,
14096 _("DW_AT_call_parameter offset is not in CU for "
14097 "DW_TAG_call_site child DIE %s [in module %s]"),
14098 sect_offset_str (child_die->sect_off),
14099 objfile_name (objfile));
14100 continue;
14101 }
14102 parameter->u.param_cu_off
14103 = (cu_offset) (sect_off - cu->header.sect_off);
14104 }
14105 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14106 {
14107 complaint (&symfile_complaints,
14108 _("No DW_FORM_block* DW_AT_location for "
14109 "DW_TAG_call_site child DIE %s [in module %s]"),
14110 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14111 continue;
14112 }
14113 else
14114 {
14115 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14116 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14117 if (parameter->u.dwarf_reg != -1)
14118 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14119 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14120 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14121 &parameter->u.fb_offset))
14122 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14123 else
14124 {
14125 complaint (&symfile_complaints,
14126 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14127 "for DW_FORM_block* DW_AT_location is supported for "
14128 "DW_TAG_call_site child DIE %s "
14129 "[in module %s]"),
14130 sect_offset_str (child_die->sect_off),
14131 objfile_name (objfile));
14132 continue;
14133 }
14134 }
14135
14136 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14137 if (attr == NULL)
14138 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14139 if (!attr_form_is_block (attr))
14140 {
14141 complaint (&symfile_complaints,
14142 _("No DW_FORM_block* DW_AT_call_value for "
14143 "DW_TAG_call_site child DIE %s [in module %s]"),
14144 sect_offset_str (child_die->sect_off),
14145 objfile_name (objfile));
14146 continue;
14147 }
14148 parameter->value = DW_BLOCK (attr)->data;
14149 parameter->value_size = DW_BLOCK (attr)->size;
14150
14151 /* Parameters are not pre-cleared by memset above. */
14152 parameter->data_value = NULL;
14153 parameter->data_value_size = 0;
14154 call_site->parameter_count++;
14155
14156 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14157 if (attr == NULL)
14158 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14159 if (attr)
14160 {
14161 if (!attr_form_is_block (attr))
14162 complaint (&symfile_complaints,
14163 _("No DW_FORM_block* DW_AT_call_data_value for "
14164 "DW_TAG_call_site child DIE %s [in module %s]"),
14165 sect_offset_str (child_die->sect_off),
14166 objfile_name (objfile));
14167 else
14168 {
14169 parameter->data_value = DW_BLOCK (attr)->data;
14170 parameter->data_value_size = DW_BLOCK (attr)->size;
14171 }
14172 }
14173 }
14174 }
14175
14176 /* Helper function for read_variable. If DIE represents a virtual
14177 table, then return the type of the concrete object that is
14178 associated with the virtual table. Otherwise, return NULL. */
14179
14180 static struct type *
14181 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14182 {
14183 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14184 if (attr == NULL)
14185 return NULL;
14186
14187 /* Find the type DIE. */
14188 struct die_info *type_die = NULL;
14189 struct dwarf2_cu *type_cu = cu;
14190
14191 if (attr_form_is_ref (attr))
14192 type_die = follow_die_ref (die, attr, &type_cu);
14193 if (type_die == NULL)
14194 return NULL;
14195
14196 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14197 return NULL;
14198 return die_containing_type (type_die, type_cu);
14199 }
14200
14201 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14202
14203 static void
14204 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14205 {
14206 struct rust_vtable_symbol *storage = NULL;
14207
14208 if (cu->language == language_rust)
14209 {
14210 struct type *containing_type = rust_containing_type (die, cu);
14211
14212 if (containing_type != NULL)
14213 {
14214 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14215
14216 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14217 struct rust_vtable_symbol);
14218 initialize_objfile_symbol (storage);
14219 storage->concrete_type = containing_type;
14220 storage->subclass = SYMBOL_RUST_VTABLE;
14221 }
14222 }
14223
14224 new_symbol (die, NULL, cu, storage);
14225 }
14226
14227 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14228 reading .debug_rnglists.
14229 Callback's type should be:
14230 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14231 Return true if the attributes are present and valid, otherwise,
14232 return false. */
14233
14234 template <typename Callback>
14235 static bool
14236 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14237 Callback &&callback)
14238 {
14239 struct dwarf2_per_objfile *dwarf2_per_objfile
14240 = cu->per_cu->dwarf2_per_objfile;
14241 struct objfile *objfile = dwarf2_per_objfile->objfile;
14242 bfd *obfd = objfile->obfd;
14243 /* Base address selection entry. */
14244 CORE_ADDR base;
14245 int found_base;
14246 const gdb_byte *buffer;
14247 CORE_ADDR baseaddr;
14248 bool overflow = false;
14249
14250 found_base = cu->base_known;
14251 base = cu->base_address;
14252
14253 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14254 if (offset >= dwarf2_per_objfile->rnglists.size)
14255 {
14256 complaint (&symfile_complaints,
14257 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14258 offset);
14259 return false;
14260 }
14261 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14262
14263 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14264
14265 while (1)
14266 {
14267 /* Initialize it due to a false compiler warning. */
14268 CORE_ADDR range_beginning = 0, range_end = 0;
14269 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14270 + dwarf2_per_objfile->rnglists.size);
14271 unsigned int bytes_read;
14272
14273 if (buffer == buf_end)
14274 {
14275 overflow = true;
14276 break;
14277 }
14278 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14279 switch (rlet)
14280 {
14281 case DW_RLE_end_of_list:
14282 break;
14283 case DW_RLE_base_address:
14284 if (buffer + cu->header.addr_size > buf_end)
14285 {
14286 overflow = true;
14287 break;
14288 }
14289 base = read_address (obfd, buffer, cu, &bytes_read);
14290 found_base = 1;
14291 buffer += bytes_read;
14292 break;
14293 case DW_RLE_start_length:
14294 if (buffer + cu->header.addr_size > buf_end)
14295 {
14296 overflow = true;
14297 break;
14298 }
14299 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14300 buffer += bytes_read;
14301 range_end = (range_beginning
14302 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14303 buffer += bytes_read;
14304 if (buffer > buf_end)
14305 {
14306 overflow = true;
14307 break;
14308 }
14309 break;
14310 case DW_RLE_offset_pair:
14311 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14312 buffer += bytes_read;
14313 if (buffer > buf_end)
14314 {
14315 overflow = true;
14316 break;
14317 }
14318 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14319 buffer += bytes_read;
14320 if (buffer > buf_end)
14321 {
14322 overflow = true;
14323 break;
14324 }
14325 break;
14326 case DW_RLE_start_end:
14327 if (buffer + 2 * cu->header.addr_size > buf_end)
14328 {
14329 overflow = true;
14330 break;
14331 }
14332 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14333 buffer += bytes_read;
14334 range_end = read_address (obfd, buffer, cu, &bytes_read);
14335 buffer += bytes_read;
14336 break;
14337 default:
14338 complaint (&symfile_complaints,
14339 _("Invalid .debug_rnglists data (no base address)"));
14340 return false;
14341 }
14342 if (rlet == DW_RLE_end_of_list || overflow)
14343 break;
14344 if (rlet == DW_RLE_base_address)
14345 continue;
14346
14347 if (!found_base)
14348 {
14349 /* We have no valid base address for the ranges
14350 data. */
14351 complaint (&symfile_complaints,
14352 _("Invalid .debug_rnglists data (no base address)"));
14353 return false;
14354 }
14355
14356 if (range_beginning > range_end)
14357 {
14358 /* Inverted range entries are invalid. */
14359 complaint (&symfile_complaints,
14360 _("Invalid .debug_rnglists data (inverted range)"));
14361 return false;
14362 }
14363
14364 /* Empty range entries have no effect. */
14365 if (range_beginning == range_end)
14366 continue;
14367
14368 range_beginning += base;
14369 range_end += base;
14370
14371 /* A not-uncommon case of bad debug info.
14372 Don't pollute the addrmap with bad data. */
14373 if (range_beginning + baseaddr == 0
14374 && !dwarf2_per_objfile->has_section_at_zero)
14375 {
14376 complaint (&symfile_complaints,
14377 _(".debug_rnglists entry has start address of zero"
14378 " [in module %s]"), objfile_name (objfile));
14379 continue;
14380 }
14381
14382 callback (range_beginning, range_end);
14383 }
14384
14385 if (overflow)
14386 {
14387 complaint (&symfile_complaints,
14388 _("Offset %d is not terminated "
14389 "for DW_AT_ranges attribute"),
14390 offset);
14391 return false;
14392 }
14393
14394 return true;
14395 }
14396
14397 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14398 Callback's type should be:
14399 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14400 Return 1 if the attributes are present and valid, otherwise, return 0. */
14401
14402 template <typename Callback>
14403 static int
14404 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14405 Callback &&callback)
14406 {
14407 struct dwarf2_per_objfile *dwarf2_per_objfile
14408 = cu->per_cu->dwarf2_per_objfile;
14409 struct objfile *objfile = dwarf2_per_objfile->objfile;
14410 struct comp_unit_head *cu_header = &cu->header;
14411 bfd *obfd = objfile->obfd;
14412 unsigned int addr_size = cu_header->addr_size;
14413 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14414 /* Base address selection entry. */
14415 CORE_ADDR base;
14416 int found_base;
14417 unsigned int dummy;
14418 const gdb_byte *buffer;
14419 CORE_ADDR baseaddr;
14420
14421 if (cu_header->version >= 5)
14422 return dwarf2_rnglists_process (offset, cu, callback);
14423
14424 found_base = cu->base_known;
14425 base = cu->base_address;
14426
14427 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14428 if (offset >= dwarf2_per_objfile->ranges.size)
14429 {
14430 complaint (&symfile_complaints,
14431 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14432 offset);
14433 return 0;
14434 }
14435 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14436
14437 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14438
14439 while (1)
14440 {
14441 CORE_ADDR range_beginning, range_end;
14442
14443 range_beginning = read_address (obfd, buffer, cu, &dummy);
14444 buffer += addr_size;
14445 range_end = read_address (obfd, buffer, cu, &dummy);
14446 buffer += addr_size;
14447 offset += 2 * addr_size;
14448
14449 /* An end of list marker is a pair of zero addresses. */
14450 if (range_beginning == 0 && range_end == 0)
14451 /* Found the end of list entry. */
14452 break;
14453
14454 /* Each base address selection entry is a pair of 2 values.
14455 The first is the largest possible address, the second is
14456 the base address. Check for a base address here. */
14457 if ((range_beginning & mask) == mask)
14458 {
14459 /* If we found the largest possible address, then we already
14460 have the base address in range_end. */
14461 base = range_end;
14462 found_base = 1;
14463 continue;
14464 }
14465
14466 if (!found_base)
14467 {
14468 /* We have no valid base address for the ranges
14469 data. */
14470 complaint (&symfile_complaints,
14471 _("Invalid .debug_ranges data (no base address)"));
14472 return 0;
14473 }
14474
14475 if (range_beginning > range_end)
14476 {
14477 /* Inverted range entries are invalid. */
14478 complaint (&symfile_complaints,
14479 _("Invalid .debug_ranges data (inverted range)"));
14480 return 0;
14481 }
14482
14483 /* Empty range entries have no effect. */
14484 if (range_beginning == range_end)
14485 continue;
14486
14487 range_beginning += base;
14488 range_end += base;
14489
14490 /* A not-uncommon case of bad debug info.
14491 Don't pollute the addrmap with bad data. */
14492 if (range_beginning + baseaddr == 0
14493 && !dwarf2_per_objfile->has_section_at_zero)
14494 {
14495 complaint (&symfile_complaints,
14496 _(".debug_ranges entry has start address of zero"
14497 " [in module %s]"), objfile_name (objfile));
14498 continue;
14499 }
14500
14501 callback (range_beginning, range_end);
14502 }
14503
14504 return 1;
14505 }
14506
14507 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14508 Return 1 if the attributes are present and valid, otherwise, return 0.
14509 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14510
14511 static int
14512 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14513 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14514 struct partial_symtab *ranges_pst)
14515 {
14516 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14517 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14518 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14519 SECT_OFF_TEXT (objfile));
14520 int low_set = 0;
14521 CORE_ADDR low = 0;
14522 CORE_ADDR high = 0;
14523 int retval;
14524
14525 retval = dwarf2_ranges_process (offset, cu,
14526 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14527 {
14528 if (ranges_pst != NULL)
14529 {
14530 CORE_ADDR lowpc;
14531 CORE_ADDR highpc;
14532
14533 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14534 range_beginning + baseaddr);
14535 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14536 range_end + baseaddr);
14537 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14538 ranges_pst);
14539 }
14540
14541 /* FIXME: This is recording everything as a low-high
14542 segment of consecutive addresses. We should have a
14543 data structure for discontiguous block ranges
14544 instead. */
14545 if (! low_set)
14546 {
14547 low = range_beginning;
14548 high = range_end;
14549 low_set = 1;
14550 }
14551 else
14552 {
14553 if (range_beginning < low)
14554 low = range_beginning;
14555 if (range_end > high)
14556 high = range_end;
14557 }
14558 });
14559 if (!retval)
14560 return 0;
14561
14562 if (! low_set)
14563 /* If the first entry is an end-of-list marker, the range
14564 describes an empty scope, i.e. no instructions. */
14565 return 0;
14566
14567 if (low_return)
14568 *low_return = low;
14569 if (high_return)
14570 *high_return = high;
14571 return 1;
14572 }
14573
14574 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14575 definition for the return value. *LOWPC and *HIGHPC are set iff
14576 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14577
14578 static enum pc_bounds_kind
14579 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14580 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14581 struct partial_symtab *pst)
14582 {
14583 struct dwarf2_per_objfile *dwarf2_per_objfile
14584 = cu->per_cu->dwarf2_per_objfile;
14585 struct attribute *attr;
14586 struct attribute *attr_high;
14587 CORE_ADDR low = 0;
14588 CORE_ADDR high = 0;
14589 enum pc_bounds_kind ret;
14590
14591 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14592 if (attr_high)
14593 {
14594 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14595 if (attr)
14596 {
14597 low = attr_value_as_address (attr);
14598 high = attr_value_as_address (attr_high);
14599 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14600 high += low;
14601 }
14602 else
14603 /* Found high w/o low attribute. */
14604 return PC_BOUNDS_INVALID;
14605
14606 /* Found consecutive range of addresses. */
14607 ret = PC_BOUNDS_HIGH_LOW;
14608 }
14609 else
14610 {
14611 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14612 if (attr != NULL)
14613 {
14614 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14615 We take advantage of the fact that DW_AT_ranges does not appear
14616 in DW_TAG_compile_unit of DWO files. */
14617 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14618 unsigned int ranges_offset = (DW_UNSND (attr)
14619 + (need_ranges_base
14620 ? cu->ranges_base
14621 : 0));
14622
14623 /* Value of the DW_AT_ranges attribute is the offset in the
14624 .debug_ranges section. */
14625 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14626 return PC_BOUNDS_INVALID;
14627 /* Found discontinuous range of addresses. */
14628 ret = PC_BOUNDS_RANGES;
14629 }
14630 else
14631 return PC_BOUNDS_NOT_PRESENT;
14632 }
14633
14634 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14635 if (high <= low)
14636 return PC_BOUNDS_INVALID;
14637
14638 /* When using the GNU linker, .gnu.linkonce. sections are used to
14639 eliminate duplicate copies of functions and vtables and such.
14640 The linker will arbitrarily choose one and discard the others.
14641 The AT_*_pc values for such functions refer to local labels in
14642 these sections. If the section from that file was discarded, the
14643 labels are not in the output, so the relocs get a value of 0.
14644 If this is a discarded function, mark the pc bounds as invalid,
14645 so that GDB will ignore it. */
14646 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14647 return PC_BOUNDS_INVALID;
14648
14649 *lowpc = low;
14650 if (highpc)
14651 *highpc = high;
14652 return ret;
14653 }
14654
14655 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14656 its low and high PC addresses. Do nothing if these addresses could not
14657 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14658 and HIGHPC to the high address if greater than HIGHPC. */
14659
14660 static void
14661 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14662 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14663 struct dwarf2_cu *cu)
14664 {
14665 CORE_ADDR low, high;
14666 struct die_info *child = die->child;
14667
14668 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14669 {
14670 *lowpc = std::min (*lowpc, low);
14671 *highpc = std::max (*highpc, high);
14672 }
14673
14674 /* If the language does not allow nested subprograms (either inside
14675 subprograms or lexical blocks), we're done. */
14676 if (cu->language != language_ada)
14677 return;
14678
14679 /* Check all the children of the given DIE. If it contains nested
14680 subprograms, then check their pc bounds. Likewise, we need to
14681 check lexical blocks as well, as they may also contain subprogram
14682 definitions. */
14683 while (child && child->tag)
14684 {
14685 if (child->tag == DW_TAG_subprogram
14686 || child->tag == DW_TAG_lexical_block)
14687 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14688 child = sibling_die (child);
14689 }
14690 }
14691
14692 /* Get the low and high pc's represented by the scope DIE, and store
14693 them in *LOWPC and *HIGHPC. If the correct values can't be
14694 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14695
14696 static void
14697 get_scope_pc_bounds (struct die_info *die,
14698 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14699 struct dwarf2_cu *cu)
14700 {
14701 CORE_ADDR best_low = (CORE_ADDR) -1;
14702 CORE_ADDR best_high = (CORE_ADDR) 0;
14703 CORE_ADDR current_low, current_high;
14704
14705 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14706 >= PC_BOUNDS_RANGES)
14707 {
14708 best_low = current_low;
14709 best_high = current_high;
14710 }
14711 else
14712 {
14713 struct die_info *child = die->child;
14714
14715 while (child && child->tag)
14716 {
14717 switch (child->tag) {
14718 case DW_TAG_subprogram:
14719 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14720 break;
14721 case DW_TAG_namespace:
14722 case DW_TAG_module:
14723 /* FIXME: carlton/2004-01-16: Should we do this for
14724 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14725 that current GCC's always emit the DIEs corresponding
14726 to definitions of methods of classes as children of a
14727 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14728 the DIEs giving the declarations, which could be
14729 anywhere). But I don't see any reason why the
14730 standards says that they have to be there. */
14731 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14732
14733 if (current_low != ((CORE_ADDR) -1))
14734 {
14735 best_low = std::min (best_low, current_low);
14736 best_high = std::max (best_high, current_high);
14737 }
14738 break;
14739 default:
14740 /* Ignore. */
14741 break;
14742 }
14743
14744 child = sibling_die (child);
14745 }
14746 }
14747
14748 *lowpc = best_low;
14749 *highpc = best_high;
14750 }
14751
14752 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14753 in DIE. */
14754
14755 static void
14756 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14757 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14758 {
14759 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14760 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14761 struct attribute *attr;
14762 struct attribute *attr_high;
14763
14764 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14765 if (attr_high)
14766 {
14767 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14768 if (attr)
14769 {
14770 CORE_ADDR low = attr_value_as_address (attr);
14771 CORE_ADDR high = attr_value_as_address (attr_high);
14772
14773 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14774 high += low;
14775
14776 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14777 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14778 record_block_range (block, low, high - 1);
14779 }
14780 }
14781
14782 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14783 if (attr)
14784 {
14785 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14786 We take advantage of the fact that DW_AT_ranges does not appear
14787 in DW_TAG_compile_unit of DWO files. */
14788 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14789
14790 /* The value of the DW_AT_ranges attribute is the offset of the
14791 address range list in the .debug_ranges section. */
14792 unsigned long offset = (DW_UNSND (attr)
14793 + (need_ranges_base ? cu->ranges_base : 0));
14794
14795 dwarf2_ranges_process (offset, cu,
14796 [&] (CORE_ADDR start, CORE_ADDR end)
14797 {
14798 start += baseaddr;
14799 end += baseaddr;
14800 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14801 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14802 record_block_range (block, start, end - 1);
14803 });
14804 }
14805 }
14806
14807 /* Check whether the producer field indicates either of GCC < 4.6, or the
14808 Intel C/C++ compiler, and cache the result in CU. */
14809
14810 static void
14811 check_producer (struct dwarf2_cu *cu)
14812 {
14813 int major, minor;
14814
14815 if (cu->producer == NULL)
14816 {
14817 /* For unknown compilers expect their behavior is DWARF version
14818 compliant.
14819
14820 GCC started to support .debug_types sections by -gdwarf-4 since
14821 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14822 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14823 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14824 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14825 }
14826 else if (producer_is_gcc (cu->producer, &major, &minor))
14827 {
14828 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14829 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14830 }
14831 else if (producer_is_icc (cu->producer, &major, &minor))
14832 cu->producer_is_icc_lt_14 = major < 14;
14833 else
14834 {
14835 /* For other non-GCC compilers, expect their behavior is DWARF version
14836 compliant. */
14837 }
14838
14839 cu->checked_producer = 1;
14840 }
14841
14842 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14843 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14844 during 4.6.0 experimental. */
14845
14846 static int
14847 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14848 {
14849 if (!cu->checked_producer)
14850 check_producer (cu);
14851
14852 return cu->producer_is_gxx_lt_4_6;
14853 }
14854
14855 /* Return the default accessibility type if it is not overriden by
14856 DW_AT_accessibility. */
14857
14858 static enum dwarf_access_attribute
14859 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14860 {
14861 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14862 {
14863 /* The default DWARF 2 accessibility for members is public, the default
14864 accessibility for inheritance is private. */
14865
14866 if (die->tag != DW_TAG_inheritance)
14867 return DW_ACCESS_public;
14868 else
14869 return DW_ACCESS_private;
14870 }
14871 else
14872 {
14873 /* DWARF 3+ defines the default accessibility a different way. The same
14874 rules apply now for DW_TAG_inheritance as for the members and it only
14875 depends on the container kind. */
14876
14877 if (die->parent->tag == DW_TAG_class_type)
14878 return DW_ACCESS_private;
14879 else
14880 return DW_ACCESS_public;
14881 }
14882 }
14883
14884 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14885 offset. If the attribute was not found return 0, otherwise return
14886 1. If it was found but could not properly be handled, set *OFFSET
14887 to 0. */
14888
14889 static int
14890 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14891 LONGEST *offset)
14892 {
14893 struct attribute *attr;
14894
14895 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14896 if (attr != NULL)
14897 {
14898 *offset = 0;
14899
14900 /* Note that we do not check for a section offset first here.
14901 This is because DW_AT_data_member_location is new in DWARF 4,
14902 so if we see it, we can assume that a constant form is really
14903 a constant and not a section offset. */
14904 if (attr_form_is_constant (attr))
14905 *offset = dwarf2_get_attr_constant_value (attr, 0);
14906 else if (attr_form_is_section_offset (attr))
14907 dwarf2_complex_location_expr_complaint ();
14908 else if (attr_form_is_block (attr))
14909 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14910 else
14911 dwarf2_complex_location_expr_complaint ();
14912
14913 return 1;
14914 }
14915
14916 return 0;
14917 }
14918
14919 /* Add an aggregate field to the field list. */
14920
14921 static void
14922 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14923 struct dwarf2_cu *cu)
14924 {
14925 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14926 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14927 struct nextfield *new_field;
14928 struct attribute *attr;
14929 struct field *fp;
14930 const char *fieldname = "";
14931
14932 if (die->tag == DW_TAG_inheritance)
14933 {
14934 fip->baseclasses.emplace_back ();
14935 new_field = &fip->baseclasses.back ();
14936 }
14937 else
14938 {
14939 fip->fields.emplace_back ();
14940 new_field = &fip->fields.back ();
14941 }
14942
14943 fip->nfields++;
14944
14945 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14946 if (attr)
14947 new_field->accessibility = DW_UNSND (attr);
14948 else
14949 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14950 if (new_field->accessibility != DW_ACCESS_public)
14951 fip->non_public_fields = 1;
14952
14953 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14954 if (attr)
14955 new_field->virtuality = DW_UNSND (attr);
14956 else
14957 new_field->virtuality = DW_VIRTUALITY_none;
14958
14959 fp = &new_field->field;
14960
14961 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14962 {
14963 LONGEST offset;
14964
14965 /* Data member other than a C++ static data member. */
14966
14967 /* Get type of field. */
14968 fp->type = die_type (die, cu);
14969
14970 SET_FIELD_BITPOS (*fp, 0);
14971
14972 /* Get bit size of field (zero if none). */
14973 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14974 if (attr)
14975 {
14976 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14977 }
14978 else
14979 {
14980 FIELD_BITSIZE (*fp) = 0;
14981 }
14982
14983 /* Get bit offset of field. */
14984 if (handle_data_member_location (die, cu, &offset))
14985 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14986 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14987 if (attr)
14988 {
14989 if (gdbarch_bits_big_endian (gdbarch))
14990 {
14991 /* For big endian bits, the DW_AT_bit_offset gives the
14992 additional bit offset from the MSB of the containing
14993 anonymous object to the MSB of the field. We don't
14994 have to do anything special since we don't need to
14995 know the size of the anonymous object. */
14996 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14997 }
14998 else
14999 {
15000 /* For little endian bits, compute the bit offset to the
15001 MSB of the anonymous object, subtract off the number of
15002 bits from the MSB of the field to the MSB of the
15003 object, and then subtract off the number of bits of
15004 the field itself. The result is the bit offset of
15005 the LSB of the field. */
15006 int anonymous_size;
15007 int bit_offset = DW_UNSND (attr);
15008
15009 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15010 if (attr)
15011 {
15012 /* The size of the anonymous object containing
15013 the bit field is explicit, so use the
15014 indicated size (in bytes). */
15015 anonymous_size = DW_UNSND (attr);
15016 }
15017 else
15018 {
15019 /* The size of the anonymous object containing
15020 the bit field must be inferred from the type
15021 attribute of the data member containing the
15022 bit field. */
15023 anonymous_size = TYPE_LENGTH (fp->type);
15024 }
15025 SET_FIELD_BITPOS (*fp,
15026 (FIELD_BITPOS (*fp)
15027 + anonymous_size * bits_per_byte
15028 - bit_offset - FIELD_BITSIZE (*fp)));
15029 }
15030 }
15031 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15032 if (attr != NULL)
15033 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15034 + dwarf2_get_attr_constant_value (attr, 0)));
15035
15036 /* Get name of field. */
15037 fieldname = dwarf2_name (die, cu);
15038 if (fieldname == NULL)
15039 fieldname = "";
15040
15041 /* The name is already allocated along with this objfile, so we don't
15042 need to duplicate it for the type. */
15043 fp->name = fieldname;
15044
15045 /* Change accessibility for artificial fields (e.g. virtual table
15046 pointer or virtual base class pointer) to private. */
15047 if (dwarf2_attr (die, DW_AT_artificial, cu))
15048 {
15049 FIELD_ARTIFICIAL (*fp) = 1;
15050 new_field->accessibility = DW_ACCESS_private;
15051 fip->non_public_fields = 1;
15052 }
15053 }
15054 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15055 {
15056 /* C++ static member. */
15057
15058 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15059 is a declaration, but all versions of G++ as of this writing
15060 (so through at least 3.2.1) incorrectly generate
15061 DW_TAG_variable tags. */
15062
15063 const char *physname;
15064
15065 /* Get name of field. */
15066 fieldname = dwarf2_name (die, cu);
15067 if (fieldname == NULL)
15068 return;
15069
15070 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15071 if (attr
15072 /* Only create a symbol if this is an external value.
15073 new_symbol checks this and puts the value in the global symbol
15074 table, which we want. If it is not external, new_symbol
15075 will try to put the value in cu->list_in_scope which is wrong. */
15076 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15077 {
15078 /* A static const member, not much different than an enum as far as
15079 we're concerned, except that we can support more types. */
15080 new_symbol (die, NULL, cu);
15081 }
15082
15083 /* Get physical name. */
15084 physname = dwarf2_physname (fieldname, die, cu);
15085
15086 /* The name is already allocated along with this objfile, so we don't
15087 need to duplicate it for the type. */
15088 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15089 FIELD_TYPE (*fp) = die_type (die, cu);
15090 FIELD_NAME (*fp) = fieldname;
15091 }
15092 else if (die->tag == DW_TAG_inheritance)
15093 {
15094 LONGEST offset;
15095
15096 /* C++ base class field. */
15097 if (handle_data_member_location (die, cu, &offset))
15098 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15099 FIELD_BITSIZE (*fp) = 0;
15100 FIELD_TYPE (*fp) = die_type (die, cu);
15101 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15102 }
15103 else if (die->tag == DW_TAG_variant_part)
15104 {
15105 /* process_structure_scope will treat this DIE as a union. */
15106 process_structure_scope (die, cu);
15107
15108 /* The variant part is relative to the start of the enclosing
15109 structure. */
15110 SET_FIELD_BITPOS (*fp, 0);
15111 fp->type = get_die_type (die, cu);
15112 fp->artificial = 1;
15113 fp->name = "<<variant>>";
15114 }
15115 else
15116 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15117 }
15118
15119 /* Can the type given by DIE define another type? */
15120
15121 static bool
15122 type_can_define_types (const struct die_info *die)
15123 {
15124 switch (die->tag)
15125 {
15126 case DW_TAG_typedef:
15127 case DW_TAG_class_type:
15128 case DW_TAG_structure_type:
15129 case DW_TAG_union_type:
15130 case DW_TAG_enumeration_type:
15131 return true;
15132
15133 default:
15134 return false;
15135 }
15136 }
15137
15138 /* Add a type definition defined in the scope of the FIP's class. */
15139
15140 static void
15141 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15142 struct dwarf2_cu *cu)
15143 {
15144 struct decl_field fp;
15145 memset (&fp, 0, sizeof (fp));
15146
15147 gdb_assert (type_can_define_types (die));
15148
15149 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15150 fp.name = dwarf2_name (die, cu);
15151 fp.type = read_type_die (die, cu);
15152
15153 /* Save accessibility. */
15154 enum dwarf_access_attribute accessibility;
15155 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15156 if (attr != NULL)
15157 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15158 else
15159 accessibility = dwarf2_default_access_attribute (die, cu);
15160 switch (accessibility)
15161 {
15162 case DW_ACCESS_public:
15163 /* The assumed value if neither private nor protected. */
15164 break;
15165 case DW_ACCESS_private:
15166 fp.is_private = 1;
15167 break;
15168 case DW_ACCESS_protected:
15169 fp.is_protected = 1;
15170 break;
15171 default:
15172 complaint (&symfile_complaints,
15173 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15174 }
15175
15176 if (die->tag == DW_TAG_typedef)
15177 fip->typedef_field_list.push_back (fp);
15178 else
15179 fip->nested_types_list.push_back (fp);
15180 }
15181
15182 /* Create the vector of fields, and attach it to the type. */
15183
15184 static void
15185 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15186 struct dwarf2_cu *cu)
15187 {
15188 int nfields = fip->nfields;
15189
15190 /* Record the field count, allocate space for the array of fields,
15191 and create blank accessibility bitfields if necessary. */
15192 TYPE_NFIELDS (type) = nfields;
15193 TYPE_FIELDS (type) = (struct field *)
15194 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15195
15196 if (fip->non_public_fields && cu->language != language_ada)
15197 {
15198 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15199
15200 TYPE_FIELD_PRIVATE_BITS (type) =
15201 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15202 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15203
15204 TYPE_FIELD_PROTECTED_BITS (type) =
15205 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15206 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15207
15208 TYPE_FIELD_IGNORE_BITS (type) =
15209 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15210 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15211 }
15212
15213 /* If the type has baseclasses, allocate and clear a bit vector for
15214 TYPE_FIELD_VIRTUAL_BITS. */
15215 if (!fip->baseclasses.empty () && cu->language != language_ada)
15216 {
15217 int num_bytes = B_BYTES (fip->baseclasses.size ());
15218 unsigned char *pointer;
15219
15220 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15221 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15222 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15223 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15224 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15225 }
15226
15227 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15228 {
15229 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15230
15231 for (int index = 0; index < nfields; ++index)
15232 {
15233 struct nextfield &field = fip->fields[index];
15234
15235 if (field.variant.is_discriminant)
15236 di->discriminant_index = index;
15237 else if (field.variant.default_branch)
15238 di->default_index = index;
15239 else
15240 di->discriminants[index] = field.variant.discriminant_value;
15241 }
15242 }
15243
15244 /* Copy the saved-up fields into the field vector. */
15245 for (int i = 0; i < nfields; ++i)
15246 {
15247 struct nextfield &field
15248 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15249 : fip->fields[i - fip->baseclasses.size ()]);
15250
15251 TYPE_FIELD (type, i) = field.field;
15252 switch (field.accessibility)
15253 {
15254 case DW_ACCESS_private:
15255 if (cu->language != language_ada)
15256 SET_TYPE_FIELD_PRIVATE (type, i);
15257 break;
15258
15259 case DW_ACCESS_protected:
15260 if (cu->language != language_ada)
15261 SET_TYPE_FIELD_PROTECTED (type, i);
15262 break;
15263
15264 case DW_ACCESS_public:
15265 break;
15266
15267 default:
15268 /* Unknown accessibility. Complain and treat it as public. */
15269 {
15270 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15271 field.accessibility);
15272 }
15273 break;
15274 }
15275 if (i < fip->baseclasses.size ())
15276 {
15277 switch (field.virtuality)
15278 {
15279 case DW_VIRTUALITY_virtual:
15280 case DW_VIRTUALITY_pure_virtual:
15281 if (cu->language == language_ada)
15282 error (_("unexpected virtuality in component of Ada type"));
15283 SET_TYPE_FIELD_VIRTUAL (type, i);
15284 break;
15285 }
15286 }
15287 }
15288 }
15289
15290 /* Return true if this member function is a constructor, false
15291 otherwise. */
15292
15293 static int
15294 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15295 {
15296 const char *fieldname;
15297 const char *type_name;
15298 int len;
15299
15300 if (die->parent == NULL)
15301 return 0;
15302
15303 if (die->parent->tag != DW_TAG_structure_type
15304 && die->parent->tag != DW_TAG_union_type
15305 && die->parent->tag != DW_TAG_class_type)
15306 return 0;
15307
15308 fieldname = dwarf2_name (die, cu);
15309 type_name = dwarf2_name (die->parent, cu);
15310 if (fieldname == NULL || type_name == NULL)
15311 return 0;
15312
15313 len = strlen (fieldname);
15314 return (strncmp (fieldname, type_name, len) == 0
15315 && (type_name[len] == '\0' || type_name[len] == '<'));
15316 }
15317
15318 /* Add a member function to the proper fieldlist. */
15319
15320 static void
15321 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15322 struct type *type, struct dwarf2_cu *cu)
15323 {
15324 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15325 struct attribute *attr;
15326 int i;
15327 struct fnfieldlist *flp = nullptr;
15328 struct fn_field *fnp;
15329 const char *fieldname;
15330 struct type *this_type;
15331 enum dwarf_access_attribute accessibility;
15332
15333 if (cu->language == language_ada)
15334 error (_("unexpected member function in Ada type"));
15335
15336 /* Get name of member function. */
15337 fieldname = dwarf2_name (die, cu);
15338 if (fieldname == NULL)
15339 return;
15340
15341 /* Look up member function name in fieldlist. */
15342 for (i = 0; i < fip->fnfieldlists.size (); i++)
15343 {
15344 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15345 {
15346 flp = &fip->fnfieldlists[i];
15347 break;
15348 }
15349 }
15350
15351 /* Create a new fnfieldlist if necessary. */
15352 if (flp == nullptr)
15353 {
15354 fip->fnfieldlists.emplace_back ();
15355 flp = &fip->fnfieldlists.back ();
15356 flp->name = fieldname;
15357 i = fip->fnfieldlists.size () - 1;
15358 }
15359
15360 /* Create a new member function field and add it to the vector of
15361 fnfieldlists. */
15362 flp->fnfields.emplace_back ();
15363 fnp = &flp->fnfields.back ();
15364
15365 /* Delay processing of the physname until later. */
15366 if (cu->language == language_cplus)
15367 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15368 die, cu);
15369 else
15370 {
15371 const char *physname = dwarf2_physname (fieldname, die, cu);
15372 fnp->physname = physname ? physname : "";
15373 }
15374
15375 fnp->type = alloc_type (objfile);
15376 this_type = read_type_die (die, cu);
15377 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15378 {
15379 int nparams = TYPE_NFIELDS (this_type);
15380
15381 /* TYPE is the domain of this method, and THIS_TYPE is the type
15382 of the method itself (TYPE_CODE_METHOD). */
15383 smash_to_method_type (fnp->type, type,
15384 TYPE_TARGET_TYPE (this_type),
15385 TYPE_FIELDS (this_type),
15386 TYPE_NFIELDS (this_type),
15387 TYPE_VARARGS (this_type));
15388
15389 /* Handle static member functions.
15390 Dwarf2 has no clean way to discern C++ static and non-static
15391 member functions. G++ helps GDB by marking the first
15392 parameter for non-static member functions (which is the this
15393 pointer) as artificial. We obtain this information from
15394 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15395 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15396 fnp->voffset = VOFFSET_STATIC;
15397 }
15398 else
15399 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15400 dwarf2_full_name (fieldname, die, cu));
15401
15402 /* Get fcontext from DW_AT_containing_type if present. */
15403 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15404 fnp->fcontext = die_containing_type (die, cu);
15405
15406 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15407 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15408
15409 /* Get accessibility. */
15410 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15411 if (attr)
15412 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15413 else
15414 accessibility = dwarf2_default_access_attribute (die, cu);
15415 switch (accessibility)
15416 {
15417 case DW_ACCESS_private:
15418 fnp->is_private = 1;
15419 break;
15420 case DW_ACCESS_protected:
15421 fnp->is_protected = 1;
15422 break;
15423 }
15424
15425 /* Check for artificial methods. */
15426 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15427 if (attr && DW_UNSND (attr) != 0)
15428 fnp->is_artificial = 1;
15429
15430 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15431
15432 /* Get index in virtual function table if it is a virtual member
15433 function. For older versions of GCC, this is an offset in the
15434 appropriate virtual table, as specified by DW_AT_containing_type.
15435 For everyone else, it is an expression to be evaluated relative
15436 to the object address. */
15437
15438 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15439 if (attr)
15440 {
15441 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15442 {
15443 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15444 {
15445 /* Old-style GCC. */
15446 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15447 }
15448 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15449 || (DW_BLOCK (attr)->size > 1
15450 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15451 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15452 {
15453 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15454 if ((fnp->voffset % cu->header.addr_size) != 0)
15455 dwarf2_complex_location_expr_complaint ();
15456 else
15457 fnp->voffset /= cu->header.addr_size;
15458 fnp->voffset += 2;
15459 }
15460 else
15461 dwarf2_complex_location_expr_complaint ();
15462
15463 if (!fnp->fcontext)
15464 {
15465 /* If there is no `this' field and no DW_AT_containing_type,
15466 we cannot actually find a base class context for the
15467 vtable! */
15468 if (TYPE_NFIELDS (this_type) == 0
15469 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15470 {
15471 complaint (&symfile_complaints,
15472 _("cannot determine context for virtual member "
15473 "function \"%s\" (offset %s)"),
15474 fieldname, sect_offset_str (die->sect_off));
15475 }
15476 else
15477 {
15478 fnp->fcontext
15479 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15480 }
15481 }
15482 }
15483 else if (attr_form_is_section_offset (attr))
15484 {
15485 dwarf2_complex_location_expr_complaint ();
15486 }
15487 else
15488 {
15489 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15490 fieldname);
15491 }
15492 }
15493 else
15494 {
15495 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15496 if (attr && DW_UNSND (attr))
15497 {
15498 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15499 complaint (&symfile_complaints,
15500 _("Member function \"%s\" (offset %s) is virtual "
15501 "but the vtable offset is not specified"),
15502 fieldname, sect_offset_str (die->sect_off));
15503 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15504 TYPE_CPLUS_DYNAMIC (type) = 1;
15505 }
15506 }
15507 }
15508
15509 /* Create the vector of member function fields, and attach it to the type. */
15510
15511 static void
15512 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15513 struct dwarf2_cu *cu)
15514 {
15515 if (cu->language == language_ada)
15516 error (_("unexpected member functions in Ada type"));
15517
15518 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15519 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15520 TYPE_ALLOC (type,
15521 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15522
15523 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15524 {
15525 struct fnfieldlist &nf = fip->fnfieldlists[i];
15526 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15527
15528 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15529 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15530 fn_flp->fn_fields = (struct fn_field *)
15531 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15532
15533 for (int k = 0; k < nf.fnfields.size (); ++k)
15534 fn_flp->fn_fields[k] = nf.fnfields[k];
15535 }
15536
15537 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15538 }
15539
15540 /* Returns non-zero if NAME is the name of a vtable member in CU's
15541 language, zero otherwise. */
15542 static int
15543 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15544 {
15545 static const char vptr[] = "_vptr";
15546
15547 /* Look for the C++ form of the vtable. */
15548 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15549 return 1;
15550
15551 return 0;
15552 }
15553
15554 /* GCC outputs unnamed structures that are really pointers to member
15555 functions, with the ABI-specified layout. If TYPE describes
15556 such a structure, smash it into a member function type.
15557
15558 GCC shouldn't do this; it should just output pointer to member DIEs.
15559 This is GCC PR debug/28767. */
15560
15561 static void
15562 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15563 {
15564 struct type *pfn_type, *self_type, *new_type;
15565
15566 /* Check for a structure with no name and two children. */
15567 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15568 return;
15569
15570 /* Check for __pfn and __delta members. */
15571 if (TYPE_FIELD_NAME (type, 0) == NULL
15572 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15573 || TYPE_FIELD_NAME (type, 1) == NULL
15574 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15575 return;
15576
15577 /* Find the type of the method. */
15578 pfn_type = TYPE_FIELD_TYPE (type, 0);
15579 if (pfn_type == NULL
15580 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15581 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15582 return;
15583
15584 /* Look for the "this" argument. */
15585 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15586 if (TYPE_NFIELDS (pfn_type) == 0
15587 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15588 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15589 return;
15590
15591 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15592 new_type = alloc_type (objfile);
15593 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15594 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15595 TYPE_VARARGS (pfn_type));
15596 smash_to_methodptr_type (type, new_type);
15597 }
15598
15599 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15600 appropriate error checking and issuing complaints if there is a
15601 problem. */
15602
15603 static ULONGEST
15604 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15605 {
15606 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15607
15608 if (attr == nullptr)
15609 return 0;
15610
15611 if (!attr_form_is_constant (attr))
15612 {
15613 complaint (&symfile_complaints,
15614 _("DW_AT_alignment must have constant form"
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 ULONGEST align;
15622 if (attr->form == DW_FORM_sdata)
15623 {
15624 LONGEST val = DW_SND (attr);
15625 if (val < 0)
15626 {
15627 complaint (&symfile_complaints,
15628 _("DW_AT_alignment value must not be negative"
15629 " - DIE at %s [in module %s]"),
15630 sect_offset_str (die->sect_off),
15631 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15632 return 0;
15633 }
15634 align = val;
15635 }
15636 else
15637 align = DW_UNSND (attr);
15638
15639 if (align == 0)
15640 {
15641 complaint (&symfile_complaints,
15642 _("DW_AT_alignment value must not be zero"
15643 " - DIE at %s [in module %s]"),
15644 sect_offset_str (die->sect_off),
15645 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15646 return 0;
15647 }
15648 if ((align & (align - 1)) != 0)
15649 {
15650 complaint (&symfile_complaints,
15651 _("DW_AT_alignment value must be a power of 2"
15652 " - DIE at %s [in module %s]"),
15653 sect_offset_str (die->sect_off),
15654 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15655 return 0;
15656 }
15657
15658 return align;
15659 }
15660
15661 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15662 the alignment for TYPE. */
15663
15664 static void
15665 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15666 struct type *type)
15667 {
15668 if (!set_type_align (type, get_alignment (cu, die)))
15669 complaint (&symfile_complaints,
15670 _("DW_AT_alignment value too large"
15671 " - DIE at %s [in module %s]"),
15672 sect_offset_str (die->sect_off),
15673 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15674 }
15675
15676 /* Called when we find the DIE that starts a structure or union scope
15677 (definition) to create a type for the structure or union. Fill in
15678 the type's name and general properties; the members will not be
15679 processed until process_structure_scope. A symbol table entry for
15680 the type will also not be done until process_structure_scope (assuming
15681 the type has a name).
15682
15683 NOTE: we need to call these functions regardless of whether or not the
15684 DIE has a DW_AT_name attribute, since it might be an anonymous
15685 structure or union. This gets the type entered into our set of
15686 user defined types. */
15687
15688 static struct type *
15689 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15690 {
15691 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15692 struct type *type;
15693 struct attribute *attr;
15694 const char *name;
15695
15696 /* If the definition of this type lives in .debug_types, read that type.
15697 Don't follow DW_AT_specification though, that will take us back up
15698 the chain and we want to go down. */
15699 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15700 if (attr)
15701 {
15702 type = get_DW_AT_signature_type (die, attr, cu);
15703
15704 /* The type's CU may not be the same as CU.
15705 Ensure TYPE is recorded with CU in die_type_hash. */
15706 return set_die_type (die, type, cu);
15707 }
15708
15709 type = alloc_type (objfile);
15710 INIT_CPLUS_SPECIFIC (type);
15711
15712 name = dwarf2_name (die, cu);
15713 if (name != NULL)
15714 {
15715 if (cu->language == language_cplus
15716 || cu->language == language_d
15717 || cu->language == language_rust)
15718 {
15719 const char *full_name = dwarf2_full_name (name, die, cu);
15720
15721 /* dwarf2_full_name might have already finished building the DIE's
15722 type. If so, there is no need to continue. */
15723 if (get_die_type (die, cu) != NULL)
15724 return get_die_type (die, cu);
15725
15726 TYPE_TAG_NAME (type) = full_name;
15727 if (die->tag == DW_TAG_structure_type
15728 || die->tag == DW_TAG_class_type)
15729 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15730 }
15731 else
15732 {
15733 /* The name is already allocated along with this objfile, so
15734 we don't need to duplicate it for the type. */
15735 TYPE_TAG_NAME (type) = name;
15736 if (die->tag == DW_TAG_class_type)
15737 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15738 }
15739 }
15740
15741 if (die->tag == DW_TAG_structure_type)
15742 {
15743 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15744 }
15745 else if (die->tag == DW_TAG_union_type)
15746 {
15747 TYPE_CODE (type) = TYPE_CODE_UNION;
15748 }
15749 else if (die->tag == DW_TAG_variant_part)
15750 {
15751 TYPE_CODE (type) = TYPE_CODE_UNION;
15752 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15753 }
15754 else
15755 {
15756 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15757 }
15758
15759 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15760 TYPE_DECLARED_CLASS (type) = 1;
15761
15762 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15763 if (attr)
15764 {
15765 if (attr_form_is_constant (attr))
15766 TYPE_LENGTH (type) = DW_UNSND (attr);
15767 else
15768 {
15769 /* For the moment, dynamic type sizes are not supported
15770 by GDB's struct type. The actual size is determined
15771 on-demand when resolving the type of a given object,
15772 so set the type's length to zero for now. Otherwise,
15773 we record an expression as the length, and that expression
15774 could lead to a very large value, which could eventually
15775 lead to us trying to allocate that much memory when creating
15776 a value of that type. */
15777 TYPE_LENGTH (type) = 0;
15778 }
15779 }
15780 else
15781 {
15782 TYPE_LENGTH (type) = 0;
15783 }
15784
15785 maybe_set_alignment (cu, die, type);
15786
15787 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15788 {
15789 /* ICC<14 does not output the required DW_AT_declaration on
15790 incomplete types, but gives them a size of zero. */
15791 TYPE_STUB (type) = 1;
15792 }
15793 else
15794 TYPE_STUB_SUPPORTED (type) = 1;
15795
15796 if (die_is_declaration (die, cu))
15797 TYPE_STUB (type) = 1;
15798 else if (attr == NULL && die->child == NULL
15799 && producer_is_realview (cu->producer))
15800 /* RealView does not output the required DW_AT_declaration
15801 on incomplete types. */
15802 TYPE_STUB (type) = 1;
15803
15804 /* We need to add the type field to the die immediately so we don't
15805 infinitely recurse when dealing with pointers to the structure
15806 type within the structure itself. */
15807 set_die_type (die, type, cu);
15808
15809 /* set_die_type should be already done. */
15810 set_descriptive_type (type, die, cu);
15811
15812 return type;
15813 }
15814
15815 /* A helper for process_structure_scope that handles a single member
15816 DIE. */
15817
15818 static void
15819 handle_struct_member_die (struct die_info *child_die, struct type *type,
15820 struct field_info *fi,
15821 std::vector<struct symbol *> *template_args,
15822 struct dwarf2_cu *cu)
15823 {
15824 if (child_die->tag == DW_TAG_member
15825 || child_die->tag == DW_TAG_variable
15826 || child_die->tag == DW_TAG_variant_part)
15827 {
15828 /* NOTE: carlton/2002-11-05: A C++ static data member
15829 should be a DW_TAG_member that is a declaration, but
15830 all versions of G++ as of this writing (so through at
15831 least 3.2.1) incorrectly generate DW_TAG_variable
15832 tags for them instead. */
15833 dwarf2_add_field (fi, child_die, cu);
15834 }
15835 else if (child_die->tag == DW_TAG_subprogram)
15836 {
15837 /* Rust doesn't have member functions in the C++ sense.
15838 However, it does emit ordinary functions as children
15839 of a struct DIE. */
15840 if (cu->language == language_rust)
15841 read_func_scope (child_die, cu);
15842 else
15843 {
15844 /* C++ member function. */
15845 dwarf2_add_member_fn (fi, child_die, type, cu);
15846 }
15847 }
15848 else if (child_die->tag == DW_TAG_inheritance)
15849 {
15850 /* C++ base class field. */
15851 dwarf2_add_field (fi, child_die, cu);
15852 }
15853 else if (type_can_define_types (child_die))
15854 dwarf2_add_type_defn (fi, child_die, cu);
15855 else if (child_die->tag == DW_TAG_template_type_param
15856 || child_die->tag == DW_TAG_template_value_param)
15857 {
15858 struct symbol *arg = new_symbol (child_die, NULL, cu);
15859
15860 if (arg != NULL)
15861 template_args->push_back (arg);
15862 }
15863 else if (child_die->tag == DW_TAG_variant)
15864 {
15865 /* In a variant we want to get the discriminant and also add a
15866 field for our sole member child. */
15867 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15868
15869 for (struct die_info *variant_child = child_die->child;
15870 variant_child != NULL;
15871 variant_child = sibling_die (variant_child))
15872 {
15873 if (variant_child->tag == DW_TAG_member)
15874 {
15875 handle_struct_member_die (variant_child, type, fi,
15876 template_args, cu);
15877 /* Only handle the one. */
15878 break;
15879 }
15880 }
15881
15882 /* We don't handle this but we might as well report it if we see
15883 it. */
15884 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15885 complaint (&symfile_complaints,
15886 _("DW_AT_discr_list is not supported yet"
15887 " - DIE at %s [in module %s]"),
15888 sect_offset_str (child_die->sect_off),
15889 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15890
15891 /* The first field was just added, so we can stash the
15892 discriminant there. */
15893 gdb_assert (!fi->fields.empty ());
15894 if (discr == NULL)
15895 fi->fields.back ().variant.default_branch = true;
15896 else
15897 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15898 }
15899 }
15900
15901 /* Finish creating a structure or union type, including filling in
15902 its members and creating a symbol for it. */
15903
15904 static void
15905 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15906 {
15907 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15908 struct die_info *child_die;
15909 struct type *type;
15910
15911 type = get_die_type (die, cu);
15912 if (type == NULL)
15913 type = read_structure_type (die, cu);
15914
15915 /* When reading a DW_TAG_variant_part, we need to notice when we
15916 read the discriminant member, so we can record it later in the
15917 discriminant_info. */
15918 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15919 sect_offset discr_offset;
15920
15921 if (is_variant_part)
15922 {
15923 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15924 if (discr == NULL)
15925 {
15926 /* Maybe it's a univariant form, an extension we support.
15927 In this case arrange not to check the offset. */
15928 is_variant_part = false;
15929 }
15930 else if (attr_form_is_ref (discr))
15931 {
15932 struct dwarf2_cu *target_cu = cu;
15933 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15934
15935 discr_offset = target_die->sect_off;
15936 }
15937 else
15938 {
15939 complaint (&symfile_complaints,
15940 _("DW_AT_discr does not have DIE reference form"
15941 " - DIE at %s [in module %s]"),
15942 sect_offset_str (die->sect_off),
15943 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15944 is_variant_part = false;
15945 }
15946 }
15947
15948 if (die->child != NULL && ! die_is_declaration (die, cu))
15949 {
15950 struct field_info fi;
15951 std::vector<struct symbol *> template_args;
15952
15953 child_die = die->child;
15954
15955 while (child_die && child_die->tag)
15956 {
15957 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15958
15959 if (is_variant_part && discr_offset == child_die->sect_off)
15960 fi.fields.back ().variant.is_discriminant = true;
15961
15962 child_die = sibling_die (child_die);
15963 }
15964
15965 /* Attach template arguments to type. */
15966 if (!template_args.empty ())
15967 {
15968 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15969 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15970 TYPE_TEMPLATE_ARGUMENTS (type)
15971 = XOBNEWVEC (&objfile->objfile_obstack,
15972 struct symbol *,
15973 TYPE_N_TEMPLATE_ARGUMENTS (type));
15974 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15975 template_args.data (),
15976 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15977 * sizeof (struct symbol *)));
15978 }
15979
15980 /* Attach fields and member functions to the type. */
15981 if (fi.nfields)
15982 dwarf2_attach_fields_to_type (&fi, type, cu);
15983 if (!fi.fnfieldlists.empty ())
15984 {
15985 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15986
15987 /* Get the type which refers to the base class (possibly this
15988 class itself) which contains the vtable pointer for the current
15989 class from the DW_AT_containing_type attribute. This use of
15990 DW_AT_containing_type is a GNU extension. */
15991
15992 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15993 {
15994 struct type *t = die_containing_type (die, cu);
15995
15996 set_type_vptr_basetype (type, t);
15997 if (type == t)
15998 {
15999 int i;
16000
16001 /* Our own class provides vtbl ptr. */
16002 for (i = TYPE_NFIELDS (t) - 1;
16003 i >= TYPE_N_BASECLASSES (t);
16004 --i)
16005 {
16006 const char *fieldname = TYPE_FIELD_NAME (t, i);
16007
16008 if (is_vtable_name (fieldname, cu))
16009 {
16010 set_type_vptr_fieldno (type, i);
16011 break;
16012 }
16013 }
16014
16015 /* Complain if virtual function table field not found. */
16016 if (i < TYPE_N_BASECLASSES (t))
16017 complaint (&symfile_complaints,
16018 _("virtual function table pointer "
16019 "not found when defining class '%s'"),
16020 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16021 "");
16022 }
16023 else
16024 {
16025 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16026 }
16027 }
16028 else if (cu->producer
16029 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16030 {
16031 /* The IBM XLC compiler does not provide direct indication
16032 of the containing type, but the vtable pointer is
16033 always named __vfp. */
16034
16035 int i;
16036
16037 for (i = TYPE_NFIELDS (type) - 1;
16038 i >= TYPE_N_BASECLASSES (type);
16039 --i)
16040 {
16041 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16042 {
16043 set_type_vptr_fieldno (type, i);
16044 set_type_vptr_basetype (type, type);
16045 break;
16046 }
16047 }
16048 }
16049 }
16050
16051 /* Copy fi.typedef_field_list linked list elements content into the
16052 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16053 if (!fi.typedef_field_list.empty ())
16054 {
16055 int count = fi.typedef_field_list.size ();
16056
16057 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16058 TYPE_TYPEDEF_FIELD_ARRAY (type)
16059 = ((struct decl_field *)
16060 TYPE_ALLOC (type,
16061 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16062 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16063
16064 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16065 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16066 }
16067
16068 /* Copy fi.nested_types_list linked list elements content into the
16069 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16070 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16071 {
16072 int count = fi.nested_types_list.size ();
16073
16074 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16075 TYPE_NESTED_TYPES_ARRAY (type)
16076 = ((struct decl_field *)
16077 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16078 TYPE_NESTED_TYPES_COUNT (type) = count;
16079
16080 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16081 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16082 }
16083 }
16084
16085 quirk_gcc_member_function_pointer (type, objfile);
16086 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16087 cu->rust_unions.push_back (type);
16088
16089 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16090 snapshots) has been known to create a die giving a declaration
16091 for a class that has, as a child, a die giving a definition for a
16092 nested class. So we have to process our children even if the
16093 current die is a declaration. Normally, of course, a declaration
16094 won't have any children at all. */
16095
16096 child_die = die->child;
16097
16098 while (child_die != NULL && child_die->tag)
16099 {
16100 if (child_die->tag == DW_TAG_member
16101 || child_die->tag == DW_TAG_variable
16102 || child_die->tag == DW_TAG_inheritance
16103 || child_die->tag == DW_TAG_template_value_param
16104 || child_die->tag == DW_TAG_template_type_param)
16105 {
16106 /* Do nothing. */
16107 }
16108 else
16109 process_die (child_die, cu);
16110
16111 child_die = sibling_die (child_die);
16112 }
16113
16114 /* Do not consider external references. According to the DWARF standard,
16115 these DIEs are identified by the fact that they have no byte_size
16116 attribute, and a declaration attribute. */
16117 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16118 || !die_is_declaration (die, cu))
16119 new_symbol (die, type, cu);
16120 }
16121
16122 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16123 update TYPE using some information only available in DIE's children. */
16124
16125 static void
16126 update_enumeration_type_from_children (struct die_info *die,
16127 struct type *type,
16128 struct dwarf2_cu *cu)
16129 {
16130 struct die_info *child_die;
16131 int unsigned_enum = 1;
16132 int flag_enum = 1;
16133 ULONGEST mask = 0;
16134
16135 auto_obstack obstack;
16136
16137 for (child_die = die->child;
16138 child_die != NULL && child_die->tag;
16139 child_die = sibling_die (child_die))
16140 {
16141 struct attribute *attr;
16142 LONGEST value;
16143 const gdb_byte *bytes;
16144 struct dwarf2_locexpr_baton *baton;
16145 const char *name;
16146
16147 if (child_die->tag != DW_TAG_enumerator)
16148 continue;
16149
16150 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16151 if (attr == NULL)
16152 continue;
16153
16154 name = dwarf2_name (child_die, cu);
16155 if (name == NULL)
16156 name = "<anonymous enumerator>";
16157
16158 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16159 &value, &bytes, &baton);
16160 if (value < 0)
16161 {
16162 unsigned_enum = 0;
16163 flag_enum = 0;
16164 }
16165 else if ((mask & value) != 0)
16166 flag_enum = 0;
16167 else
16168 mask |= value;
16169
16170 /* If we already know that the enum type is neither unsigned, nor
16171 a flag type, no need to look at the rest of the enumerates. */
16172 if (!unsigned_enum && !flag_enum)
16173 break;
16174 }
16175
16176 if (unsigned_enum)
16177 TYPE_UNSIGNED (type) = 1;
16178 if (flag_enum)
16179 TYPE_FLAG_ENUM (type) = 1;
16180 }
16181
16182 /* Given a DW_AT_enumeration_type die, set its type. We do not
16183 complete the type's fields yet, or create any symbols. */
16184
16185 static struct type *
16186 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16187 {
16188 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16189 struct type *type;
16190 struct attribute *attr;
16191 const char *name;
16192
16193 /* If the definition of this type lives in .debug_types, read that type.
16194 Don't follow DW_AT_specification though, that will take us back up
16195 the chain and we want to go down. */
16196 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16197 if (attr)
16198 {
16199 type = get_DW_AT_signature_type (die, attr, cu);
16200
16201 /* The type's CU may not be the same as CU.
16202 Ensure TYPE is recorded with CU in die_type_hash. */
16203 return set_die_type (die, type, cu);
16204 }
16205
16206 type = alloc_type (objfile);
16207
16208 TYPE_CODE (type) = TYPE_CODE_ENUM;
16209 name = dwarf2_full_name (NULL, die, cu);
16210 if (name != NULL)
16211 TYPE_TAG_NAME (type) = name;
16212
16213 attr = dwarf2_attr (die, DW_AT_type, cu);
16214 if (attr != NULL)
16215 {
16216 struct type *underlying_type = die_type (die, cu);
16217
16218 TYPE_TARGET_TYPE (type) = underlying_type;
16219 }
16220
16221 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16222 if (attr)
16223 {
16224 TYPE_LENGTH (type) = DW_UNSND (attr);
16225 }
16226 else
16227 {
16228 TYPE_LENGTH (type) = 0;
16229 }
16230
16231 maybe_set_alignment (cu, die, type);
16232
16233 /* The enumeration DIE can be incomplete. In Ada, any type can be
16234 declared as private in the package spec, and then defined only
16235 inside the package body. Such types are known as Taft Amendment
16236 Types. When another package uses such a type, an incomplete DIE
16237 may be generated by the compiler. */
16238 if (die_is_declaration (die, cu))
16239 TYPE_STUB (type) = 1;
16240
16241 /* Finish the creation of this type by using the enum's children.
16242 We must call this even when the underlying type has been provided
16243 so that we can determine if we're looking at a "flag" enum. */
16244 update_enumeration_type_from_children (die, type, cu);
16245
16246 /* If this type has an underlying type that is not a stub, then we
16247 may use its attributes. We always use the "unsigned" attribute
16248 in this situation, because ordinarily we guess whether the type
16249 is unsigned -- but the guess can be wrong and the underlying type
16250 can tell us the reality. However, we defer to a local size
16251 attribute if one exists, because this lets the compiler override
16252 the underlying type if needed. */
16253 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16254 {
16255 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16256 if (TYPE_LENGTH (type) == 0)
16257 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16258 if (TYPE_RAW_ALIGN (type) == 0
16259 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16260 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16261 }
16262
16263 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16264
16265 return set_die_type (die, type, cu);
16266 }
16267
16268 /* Given a pointer to a die which begins an enumeration, process all
16269 the dies that define the members of the enumeration, and create the
16270 symbol for the enumeration type.
16271
16272 NOTE: We reverse the order of the element list. */
16273
16274 static void
16275 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16276 {
16277 struct type *this_type;
16278
16279 this_type = get_die_type (die, cu);
16280 if (this_type == NULL)
16281 this_type = read_enumeration_type (die, cu);
16282
16283 if (die->child != NULL)
16284 {
16285 struct die_info *child_die;
16286 struct symbol *sym;
16287 struct field *fields = NULL;
16288 int num_fields = 0;
16289 const char *name;
16290
16291 child_die = die->child;
16292 while (child_die && child_die->tag)
16293 {
16294 if (child_die->tag != DW_TAG_enumerator)
16295 {
16296 process_die (child_die, cu);
16297 }
16298 else
16299 {
16300 name = dwarf2_name (child_die, cu);
16301 if (name)
16302 {
16303 sym = new_symbol (child_die, this_type, cu);
16304
16305 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16306 {
16307 fields = (struct field *)
16308 xrealloc (fields,
16309 (num_fields + DW_FIELD_ALLOC_CHUNK)
16310 * sizeof (struct field));
16311 }
16312
16313 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16314 FIELD_TYPE (fields[num_fields]) = NULL;
16315 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16316 FIELD_BITSIZE (fields[num_fields]) = 0;
16317
16318 num_fields++;
16319 }
16320 }
16321
16322 child_die = sibling_die (child_die);
16323 }
16324
16325 if (num_fields)
16326 {
16327 TYPE_NFIELDS (this_type) = num_fields;
16328 TYPE_FIELDS (this_type) = (struct field *)
16329 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16330 memcpy (TYPE_FIELDS (this_type), fields,
16331 sizeof (struct field) * num_fields);
16332 xfree (fields);
16333 }
16334 }
16335
16336 /* If we are reading an enum from a .debug_types unit, and the enum
16337 is a declaration, and the enum is not the signatured type in the
16338 unit, then we do not want to add a symbol for it. Adding a
16339 symbol would in some cases obscure the true definition of the
16340 enum, giving users an incomplete type when the definition is
16341 actually available. Note that we do not want to do this for all
16342 enums which are just declarations, because C++0x allows forward
16343 enum declarations. */
16344 if (cu->per_cu->is_debug_types
16345 && die_is_declaration (die, cu))
16346 {
16347 struct signatured_type *sig_type;
16348
16349 sig_type = (struct signatured_type *) cu->per_cu;
16350 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16351 if (sig_type->type_offset_in_section != die->sect_off)
16352 return;
16353 }
16354
16355 new_symbol (die, this_type, cu);
16356 }
16357
16358 /* Extract all information from a DW_TAG_array_type DIE and put it in
16359 the DIE's type field. For now, this only handles one dimensional
16360 arrays. */
16361
16362 static struct type *
16363 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16364 {
16365 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16366 struct die_info *child_die;
16367 struct type *type;
16368 struct type *element_type, *range_type, *index_type;
16369 struct attribute *attr;
16370 const char *name;
16371 struct dynamic_prop *byte_stride_prop = NULL;
16372 unsigned int bit_stride = 0;
16373
16374 element_type = die_type (die, cu);
16375
16376 /* The die_type call above may have already set the type for this DIE. */
16377 type = get_die_type (die, cu);
16378 if (type)
16379 return type;
16380
16381 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16382 if (attr != NULL)
16383 {
16384 int stride_ok;
16385
16386 byte_stride_prop
16387 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16388 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16389 if (!stride_ok)
16390 {
16391 complaint (&symfile_complaints,
16392 _("unable to read array DW_AT_byte_stride "
16393 " - DIE at %s [in module %s]"),
16394 sect_offset_str (die->sect_off),
16395 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16396 /* Ignore this attribute. We will likely not be able to print
16397 arrays of this type correctly, but there is little we can do
16398 to help if we cannot read the attribute's value. */
16399 byte_stride_prop = NULL;
16400 }
16401 }
16402
16403 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16404 if (attr != NULL)
16405 bit_stride = DW_UNSND (attr);
16406
16407 /* Irix 6.2 native cc creates array types without children for
16408 arrays with unspecified length. */
16409 if (die->child == NULL)
16410 {
16411 index_type = objfile_type (objfile)->builtin_int;
16412 range_type = create_static_range_type (NULL, index_type, 0, -1);
16413 type = create_array_type_with_stride (NULL, element_type, range_type,
16414 byte_stride_prop, bit_stride);
16415 return set_die_type (die, type, cu);
16416 }
16417
16418 std::vector<struct type *> range_types;
16419 child_die = die->child;
16420 while (child_die && child_die->tag)
16421 {
16422 if (child_die->tag == DW_TAG_subrange_type)
16423 {
16424 struct type *child_type = read_type_die (child_die, cu);
16425
16426 if (child_type != NULL)
16427 {
16428 /* The range type was succesfully read. Save it for the
16429 array type creation. */
16430 range_types.push_back (child_type);
16431 }
16432 }
16433 child_die = sibling_die (child_die);
16434 }
16435
16436 /* Dwarf2 dimensions are output from left to right, create the
16437 necessary array types in backwards order. */
16438
16439 type = element_type;
16440
16441 if (read_array_order (die, cu) == DW_ORD_col_major)
16442 {
16443 int i = 0;
16444
16445 while (i < range_types.size ())
16446 type = create_array_type_with_stride (NULL, type, range_types[i++],
16447 byte_stride_prop, bit_stride);
16448 }
16449 else
16450 {
16451 size_t ndim = range_types.size ();
16452 while (ndim-- > 0)
16453 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16454 byte_stride_prop, bit_stride);
16455 }
16456
16457 /* Understand Dwarf2 support for vector types (like they occur on
16458 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16459 array type. This is not part of the Dwarf2/3 standard yet, but a
16460 custom vendor extension. The main difference between a regular
16461 array and the vector variant is that vectors are passed by value
16462 to functions. */
16463 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16464 if (attr)
16465 make_vector_type (type);
16466
16467 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16468 implementation may choose to implement triple vectors using this
16469 attribute. */
16470 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16471 if (attr)
16472 {
16473 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16474 TYPE_LENGTH (type) = DW_UNSND (attr);
16475 else
16476 complaint (&symfile_complaints,
16477 _("DW_AT_byte_size for array type smaller "
16478 "than the total size of elements"));
16479 }
16480
16481 name = dwarf2_name (die, cu);
16482 if (name)
16483 TYPE_NAME (type) = name;
16484
16485 maybe_set_alignment (cu, die, type);
16486
16487 /* Install the type in the die. */
16488 set_die_type (die, type, cu);
16489
16490 /* set_die_type should be already done. */
16491 set_descriptive_type (type, die, cu);
16492
16493 return type;
16494 }
16495
16496 static enum dwarf_array_dim_ordering
16497 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16498 {
16499 struct attribute *attr;
16500
16501 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16502
16503 if (attr)
16504 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16505
16506 /* GNU F77 is a special case, as at 08/2004 array type info is the
16507 opposite order to the dwarf2 specification, but data is still
16508 laid out as per normal fortran.
16509
16510 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16511 version checking. */
16512
16513 if (cu->language == language_fortran
16514 && cu->producer && strstr (cu->producer, "GNU F77"))
16515 {
16516 return DW_ORD_row_major;
16517 }
16518
16519 switch (cu->language_defn->la_array_ordering)
16520 {
16521 case array_column_major:
16522 return DW_ORD_col_major;
16523 case array_row_major:
16524 default:
16525 return DW_ORD_row_major;
16526 };
16527 }
16528
16529 /* Extract all information from a DW_TAG_set_type DIE and put it in
16530 the DIE's type field. */
16531
16532 static struct type *
16533 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16534 {
16535 struct type *domain_type, *set_type;
16536 struct attribute *attr;
16537
16538 domain_type = die_type (die, cu);
16539
16540 /* The die_type call above may have already set the type for this DIE. */
16541 set_type = get_die_type (die, cu);
16542 if (set_type)
16543 return set_type;
16544
16545 set_type = create_set_type (NULL, domain_type);
16546
16547 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16548 if (attr)
16549 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16550
16551 maybe_set_alignment (cu, die, set_type);
16552
16553 return set_die_type (die, set_type, cu);
16554 }
16555
16556 /* A helper for read_common_block that creates a locexpr baton.
16557 SYM is the symbol which we are marking as computed.
16558 COMMON_DIE is the DIE for the common block.
16559 COMMON_LOC is the location expression attribute for the common
16560 block itself.
16561 MEMBER_LOC is the location expression attribute for the particular
16562 member of the common block that we are processing.
16563 CU is the CU from which the above come. */
16564
16565 static void
16566 mark_common_block_symbol_computed (struct symbol *sym,
16567 struct die_info *common_die,
16568 struct attribute *common_loc,
16569 struct attribute *member_loc,
16570 struct dwarf2_cu *cu)
16571 {
16572 struct dwarf2_per_objfile *dwarf2_per_objfile
16573 = cu->per_cu->dwarf2_per_objfile;
16574 struct objfile *objfile = dwarf2_per_objfile->objfile;
16575 struct dwarf2_locexpr_baton *baton;
16576 gdb_byte *ptr;
16577 unsigned int cu_off;
16578 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16579 LONGEST offset = 0;
16580
16581 gdb_assert (common_loc && member_loc);
16582 gdb_assert (attr_form_is_block (common_loc));
16583 gdb_assert (attr_form_is_block (member_loc)
16584 || attr_form_is_constant (member_loc));
16585
16586 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16587 baton->per_cu = cu->per_cu;
16588 gdb_assert (baton->per_cu);
16589
16590 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16591
16592 if (attr_form_is_constant (member_loc))
16593 {
16594 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16595 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16596 }
16597 else
16598 baton->size += DW_BLOCK (member_loc)->size;
16599
16600 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16601 baton->data = ptr;
16602
16603 *ptr++ = DW_OP_call4;
16604 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16605 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16606 ptr += 4;
16607
16608 if (attr_form_is_constant (member_loc))
16609 {
16610 *ptr++ = DW_OP_addr;
16611 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16612 ptr += cu->header.addr_size;
16613 }
16614 else
16615 {
16616 /* We have to copy the data here, because DW_OP_call4 will only
16617 use a DW_AT_location attribute. */
16618 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16619 ptr += DW_BLOCK (member_loc)->size;
16620 }
16621
16622 *ptr++ = DW_OP_plus;
16623 gdb_assert (ptr - baton->data == baton->size);
16624
16625 SYMBOL_LOCATION_BATON (sym) = baton;
16626 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16627 }
16628
16629 /* Create appropriate locally-scoped variables for all the
16630 DW_TAG_common_block entries. Also create a struct common_block
16631 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16632 is used to sepate the common blocks name namespace from regular
16633 variable names. */
16634
16635 static void
16636 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16637 {
16638 struct attribute *attr;
16639
16640 attr = dwarf2_attr (die, DW_AT_location, cu);
16641 if (attr)
16642 {
16643 /* Support the .debug_loc offsets. */
16644 if (attr_form_is_block (attr))
16645 {
16646 /* Ok. */
16647 }
16648 else if (attr_form_is_section_offset (attr))
16649 {
16650 dwarf2_complex_location_expr_complaint ();
16651 attr = NULL;
16652 }
16653 else
16654 {
16655 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16656 "common block member");
16657 attr = NULL;
16658 }
16659 }
16660
16661 if (die->child != NULL)
16662 {
16663 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16664 struct die_info *child_die;
16665 size_t n_entries = 0, size;
16666 struct common_block *common_block;
16667 struct symbol *sym;
16668
16669 for (child_die = die->child;
16670 child_die && child_die->tag;
16671 child_die = sibling_die (child_die))
16672 ++n_entries;
16673
16674 size = (sizeof (struct common_block)
16675 + (n_entries - 1) * sizeof (struct symbol *));
16676 common_block
16677 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16678 size);
16679 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16680 common_block->n_entries = 0;
16681
16682 for (child_die = die->child;
16683 child_die && child_die->tag;
16684 child_die = sibling_die (child_die))
16685 {
16686 /* Create the symbol in the DW_TAG_common_block block in the current
16687 symbol scope. */
16688 sym = new_symbol (child_die, NULL, cu);
16689 if (sym != NULL)
16690 {
16691 struct attribute *member_loc;
16692
16693 common_block->contents[common_block->n_entries++] = sym;
16694
16695 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16696 cu);
16697 if (member_loc)
16698 {
16699 /* GDB has handled this for a long time, but it is
16700 not specified by DWARF. It seems to have been
16701 emitted by gfortran at least as recently as:
16702 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16703 complaint (&symfile_complaints,
16704 _("Variable in common block has "
16705 "DW_AT_data_member_location "
16706 "- DIE at %s [in module %s]"),
16707 sect_offset_str (child_die->sect_off),
16708 objfile_name (objfile));
16709
16710 if (attr_form_is_section_offset (member_loc))
16711 dwarf2_complex_location_expr_complaint ();
16712 else if (attr_form_is_constant (member_loc)
16713 || attr_form_is_block (member_loc))
16714 {
16715 if (attr)
16716 mark_common_block_symbol_computed (sym, die, attr,
16717 member_loc, cu);
16718 }
16719 else
16720 dwarf2_complex_location_expr_complaint ();
16721 }
16722 }
16723 }
16724
16725 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16726 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16727 }
16728 }
16729
16730 /* Create a type for a C++ namespace. */
16731
16732 static struct type *
16733 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16734 {
16735 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16736 const char *previous_prefix, *name;
16737 int is_anonymous;
16738 struct type *type;
16739
16740 /* For extensions, reuse the type of the original namespace. */
16741 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16742 {
16743 struct die_info *ext_die;
16744 struct dwarf2_cu *ext_cu = cu;
16745
16746 ext_die = dwarf2_extension (die, &ext_cu);
16747 type = read_type_die (ext_die, ext_cu);
16748
16749 /* EXT_CU may not be the same as CU.
16750 Ensure TYPE is recorded with CU in die_type_hash. */
16751 return set_die_type (die, type, cu);
16752 }
16753
16754 name = namespace_name (die, &is_anonymous, cu);
16755
16756 /* Now build the name of the current namespace. */
16757
16758 previous_prefix = determine_prefix (die, cu);
16759 if (previous_prefix[0] != '\0')
16760 name = typename_concat (&objfile->objfile_obstack,
16761 previous_prefix, name, 0, cu);
16762
16763 /* Create the type. */
16764 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16765 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16766
16767 return set_die_type (die, type, cu);
16768 }
16769
16770 /* Read a namespace scope. */
16771
16772 static void
16773 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16774 {
16775 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16776 int is_anonymous;
16777
16778 /* Add a symbol associated to this if we haven't seen the namespace
16779 before. Also, add a using directive if it's an anonymous
16780 namespace. */
16781
16782 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16783 {
16784 struct type *type;
16785
16786 type = read_type_die (die, cu);
16787 new_symbol (die, type, cu);
16788
16789 namespace_name (die, &is_anonymous, cu);
16790 if (is_anonymous)
16791 {
16792 const char *previous_prefix = determine_prefix (die, cu);
16793
16794 std::vector<const char *> excludes;
16795 add_using_directive (using_directives (cu->language),
16796 previous_prefix, TYPE_NAME (type), NULL,
16797 NULL, excludes, 0, &objfile->objfile_obstack);
16798 }
16799 }
16800
16801 if (die->child != NULL)
16802 {
16803 struct die_info *child_die = die->child;
16804
16805 while (child_die && child_die->tag)
16806 {
16807 process_die (child_die, cu);
16808 child_die = sibling_die (child_die);
16809 }
16810 }
16811 }
16812
16813 /* Read a Fortran module as type. This DIE can be only a declaration used for
16814 imported module. Still we need that type as local Fortran "use ... only"
16815 declaration imports depend on the created type in determine_prefix. */
16816
16817 static struct type *
16818 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16819 {
16820 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16821 const char *module_name;
16822 struct type *type;
16823
16824 module_name = dwarf2_name (die, cu);
16825 if (!module_name)
16826 complaint (&symfile_complaints,
16827 _("DW_TAG_module has no name, offset %s"),
16828 sect_offset_str (die->sect_off));
16829 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16830
16831 /* determine_prefix uses TYPE_TAG_NAME. */
16832 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16833
16834 return set_die_type (die, type, cu);
16835 }
16836
16837 /* Read a Fortran module. */
16838
16839 static void
16840 read_module (struct die_info *die, struct dwarf2_cu *cu)
16841 {
16842 struct die_info *child_die = die->child;
16843 struct type *type;
16844
16845 type = read_type_die (die, cu);
16846 new_symbol (die, type, cu);
16847
16848 while (child_die && child_die->tag)
16849 {
16850 process_die (child_die, cu);
16851 child_die = sibling_die (child_die);
16852 }
16853 }
16854
16855 /* Return the name of the namespace represented by DIE. Set
16856 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16857 namespace. */
16858
16859 static const char *
16860 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16861 {
16862 struct die_info *current_die;
16863 const char *name = NULL;
16864
16865 /* Loop through the extensions until we find a name. */
16866
16867 for (current_die = die;
16868 current_die != NULL;
16869 current_die = dwarf2_extension (die, &cu))
16870 {
16871 /* We don't use dwarf2_name here so that we can detect the absence
16872 of a name -> anonymous namespace. */
16873 name = dwarf2_string_attr (die, DW_AT_name, cu);
16874
16875 if (name != NULL)
16876 break;
16877 }
16878
16879 /* Is it an anonymous namespace? */
16880
16881 *is_anonymous = (name == NULL);
16882 if (*is_anonymous)
16883 name = CP_ANONYMOUS_NAMESPACE_STR;
16884
16885 return name;
16886 }
16887
16888 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16889 the user defined type vector. */
16890
16891 static struct type *
16892 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16893 {
16894 struct gdbarch *gdbarch
16895 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16896 struct comp_unit_head *cu_header = &cu->header;
16897 struct type *type;
16898 struct attribute *attr_byte_size;
16899 struct attribute *attr_address_class;
16900 int byte_size, addr_class;
16901 struct type *target_type;
16902
16903 target_type = die_type (die, cu);
16904
16905 /* The die_type call above may have already set the type for this DIE. */
16906 type = get_die_type (die, cu);
16907 if (type)
16908 return type;
16909
16910 type = lookup_pointer_type (target_type);
16911
16912 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16913 if (attr_byte_size)
16914 byte_size = DW_UNSND (attr_byte_size);
16915 else
16916 byte_size = cu_header->addr_size;
16917
16918 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16919 if (attr_address_class)
16920 addr_class = DW_UNSND (attr_address_class);
16921 else
16922 addr_class = DW_ADDR_none;
16923
16924 ULONGEST alignment = get_alignment (cu, die);
16925
16926 /* If the pointer size, alignment, or address class is different
16927 than the default, create a type variant marked as such and set
16928 the length accordingly. */
16929 if (TYPE_LENGTH (type) != byte_size
16930 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16931 && alignment != TYPE_RAW_ALIGN (type))
16932 || addr_class != DW_ADDR_none)
16933 {
16934 if (gdbarch_address_class_type_flags_p (gdbarch))
16935 {
16936 int type_flags;
16937
16938 type_flags = gdbarch_address_class_type_flags
16939 (gdbarch, byte_size, addr_class);
16940 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16941 == 0);
16942 type = make_type_with_address_space (type, type_flags);
16943 }
16944 else if (TYPE_LENGTH (type) != byte_size)
16945 {
16946 complaint (&symfile_complaints,
16947 _("invalid pointer size %d"), byte_size);
16948 }
16949 else if (TYPE_RAW_ALIGN (type) != alignment)
16950 {
16951 complaint (&symfile_complaints,
16952 _("Invalid DW_AT_alignment"
16953 " - DIE at %s [in module %s]"),
16954 sect_offset_str (die->sect_off),
16955 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16956 }
16957 else
16958 {
16959 /* Should we also complain about unhandled address classes? */
16960 }
16961 }
16962
16963 TYPE_LENGTH (type) = byte_size;
16964 set_type_align (type, alignment);
16965 return set_die_type (die, type, cu);
16966 }
16967
16968 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16969 the user defined type vector. */
16970
16971 static struct type *
16972 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16973 {
16974 struct type *type;
16975 struct type *to_type;
16976 struct type *domain;
16977
16978 to_type = die_type (die, cu);
16979 domain = die_containing_type (die, cu);
16980
16981 /* The calls above may have already set the type for this DIE. */
16982 type = get_die_type (die, cu);
16983 if (type)
16984 return type;
16985
16986 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16987 type = lookup_methodptr_type (to_type);
16988 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16989 {
16990 struct type *new_type
16991 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16992
16993 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16994 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16995 TYPE_VARARGS (to_type));
16996 type = lookup_methodptr_type (new_type);
16997 }
16998 else
16999 type = lookup_memberptr_type (to_type, domain);
17000
17001 return set_die_type (die, type, cu);
17002 }
17003
17004 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17005 the user defined type vector. */
17006
17007 static struct type *
17008 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17009 enum type_code refcode)
17010 {
17011 struct comp_unit_head *cu_header = &cu->header;
17012 struct type *type, *target_type;
17013 struct attribute *attr;
17014
17015 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17016
17017 target_type = die_type (die, cu);
17018
17019 /* The die_type call above may have already set the type for this DIE. */
17020 type = get_die_type (die, cu);
17021 if (type)
17022 return type;
17023
17024 type = lookup_reference_type (target_type, refcode);
17025 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17026 if (attr)
17027 {
17028 TYPE_LENGTH (type) = DW_UNSND (attr);
17029 }
17030 else
17031 {
17032 TYPE_LENGTH (type) = cu_header->addr_size;
17033 }
17034 maybe_set_alignment (cu, die, type);
17035 return set_die_type (die, type, cu);
17036 }
17037
17038 /* Add the given cv-qualifiers to the element type of the array. GCC
17039 outputs DWARF type qualifiers that apply to an array, not the
17040 element type. But GDB relies on the array element type to carry
17041 the cv-qualifiers. This mimics section 6.7.3 of the C99
17042 specification. */
17043
17044 static struct type *
17045 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17046 struct type *base_type, int cnst, int voltl)
17047 {
17048 struct type *el_type, *inner_array;
17049
17050 base_type = copy_type (base_type);
17051 inner_array = base_type;
17052
17053 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17054 {
17055 TYPE_TARGET_TYPE (inner_array) =
17056 copy_type (TYPE_TARGET_TYPE (inner_array));
17057 inner_array = TYPE_TARGET_TYPE (inner_array);
17058 }
17059
17060 el_type = TYPE_TARGET_TYPE (inner_array);
17061 cnst |= TYPE_CONST (el_type);
17062 voltl |= TYPE_VOLATILE (el_type);
17063 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17064
17065 return set_die_type (die, base_type, cu);
17066 }
17067
17068 static struct type *
17069 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17070 {
17071 struct type *base_type, *cv_type;
17072
17073 base_type = die_type (die, cu);
17074
17075 /* The die_type call above may have already set the type for this DIE. */
17076 cv_type = get_die_type (die, cu);
17077 if (cv_type)
17078 return cv_type;
17079
17080 /* In case the const qualifier is applied to an array type, the element type
17081 is so qualified, not the array type (section 6.7.3 of C99). */
17082 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17083 return add_array_cv_type (die, cu, base_type, 1, 0);
17084
17085 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17086 return set_die_type (die, cv_type, cu);
17087 }
17088
17089 static struct type *
17090 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17091 {
17092 struct type *base_type, *cv_type;
17093
17094 base_type = die_type (die, cu);
17095
17096 /* The die_type call above may have already set the type for this DIE. */
17097 cv_type = get_die_type (die, cu);
17098 if (cv_type)
17099 return cv_type;
17100
17101 /* In case the volatile qualifier is applied to an array type, the
17102 element type is so qualified, not the array type (section 6.7.3
17103 of C99). */
17104 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17105 return add_array_cv_type (die, cu, base_type, 0, 1);
17106
17107 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17108 return set_die_type (die, cv_type, cu);
17109 }
17110
17111 /* Handle DW_TAG_restrict_type. */
17112
17113 static struct type *
17114 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17115 {
17116 struct type *base_type, *cv_type;
17117
17118 base_type = die_type (die, cu);
17119
17120 /* The die_type call above may have already set the type for this DIE. */
17121 cv_type = get_die_type (die, cu);
17122 if (cv_type)
17123 return cv_type;
17124
17125 cv_type = make_restrict_type (base_type);
17126 return set_die_type (die, cv_type, cu);
17127 }
17128
17129 /* Handle DW_TAG_atomic_type. */
17130
17131 static struct type *
17132 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17133 {
17134 struct type *base_type, *cv_type;
17135
17136 base_type = die_type (die, cu);
17137
17138 /* The die_type call above may have already set the type for this DIE. */
17139 cv_type = get_die_type (die, cu);
17140 if (cv_type)
17141 return cv_type;
17142
17143 cv_type = make_atomic_type (base_type);
17144 return set_die_type (die, cv_type, cu);
17145 }
17146
17147 /* Extract all information from a DW_TAG_string_type DIE and add to
17148 the user defined type vector. It isn't really a user defined type,
17149 but it behaves like one, with other DIE's using an AT_user_def_type
17150 attribute to reference it. */
17151
17152 static struct type *
17153 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17154 {
17155 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17156 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17157 struct type *type, *range_type, *index_type, *char_type;
17158 struct attribute *attr;
17159 unsigned int length;
17160
17161 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17162 if (attr)
17163 {
17164 length = DW_UNSND (attr);
17165 }
17166 else
17167 {
17168 /* Check for the DW_AT_byte_size attribute. */
17169 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17170 if (attr)
17171 {
17172 length = DW_UNSND (attr);
17173 }
17174 else
17175 {
17176 length = 1;
17177 }
17178 }
17179
17180 index_type = objfile_type (objfile)->builtin_int;
17181 range_type = create_static_range_type (NULL, index_type, 1, length);
17182 char_type = language_string_char_type (cu->language_defn, gdbarch);
17183 type = create_string_type (NULL, char_type, range_type);
17184
17185 return set_die_type (die, type, cu);
17186 }
17187
17188 /* Assuming that DIE corresponds to a function, returns nonzero
17189 if the function is prototyped. */
17190
17191 static int
17192 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17193 {
17194 struct attribute *attr;
17195
17196 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17197 if (attr && (DW_UNSND (attr) != 0))
17198 return 1;
17199
17200 /* The DWARF standard implies that the DW_AT_prototyped attribute
17201 is only meaninful for C, but the concept also extends to other
17202 languages that allow unprototyped functions (Eg: Objective C).
17203 For all other languages, assume that functions are always
17204 prototyped. */
17205 if (cu->language != language_c
17206 && cu->language != language_objc
17207 && cu->language != language_opencl)
17208 return 1;
17209
17210 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17211 prototyped and unprototyped functions; default to prototyped,
17212 since that is more common in modern code (and RealView warns
17213 about unprototyped functions). */
17214 if (producer_is_realview (cu->producer))
17215 return 1;
17216
17217 return 0;
17218 }
17219
17220 /* Handle DIES due to C code like:
17221
17222 struct foo
17223 {
17224 int (*funcp)(int a, long l);
17225 int b;
17226 };
17227
17228 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17229
17230 static struct type *
17231 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17232 {
17233 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17234 struct type *type; /* Type that this function returns. */
17235 struct type *ftype; /* Function that returns above type. */
17236 struct attribute *attr;
17237
17238 type = die_type (die, cu);
17239
17240 /* The die_type call above may have already set the type for this DIE. */
17241 ftype = get_die_type (die, cu);
17242 if (ftype)
17243 return ftype;
17244
17245 ftype = lookup_function_type (type);
17246
17247 if (prototyped_function_p (die, cu))
17248 TYPE_PROTOTYPED (ftype) = 1;
17249
17250 /* Store the calling convention in the type if it's available in
17251 the subroutine die. Otherwise set the calling convention to
17252 the default value DW_CC_normal. */
17253 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17254 if (attr)
17255 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17256 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17257 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17258 else
17259 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17260
17261 /* Record whether the function returns normally to its caller or not
17262 if the DWARF producer set that information. */
17263 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17264 if (attr && (DW_UNSND (attr) != 0))
17265 TYPE_NO_RETURN (ftype) = 1;
17266
17267 /* We need to add the subroutine type to the die immediately so
17268 we don't infinitely recurse when dealing with parameters
17269 declared as the same subroutine type. */
17270 set_die_type (die, ftype, cu);
17271
17272 if (die->child != NULL)
17273 {
17274 struct type *void_type = objfile_type (objfile)->builtin_void;
17275 struct die_info *child_die;
17276 int nparams, iparams;
17277
17278 /* Count the number of parameters.
17279 FIXME: GDB currently ignores vararg functions, but knows about
17280 vararg member functions. */
17281 nparams = 0;
17282 child_die = die->child;
17283 while (child_die && child_die->tag)
17284 {
17285 if (child_die->tag == DW_TAG_formal_parameter)
17286 nparams++;
17287 else if (child_die->tag == DW_TAG_unspecified_parameters)
17288 TYPE_VARARGS (ftype) = 1;
17289 child_die = sibling_die (child_die);
17290 }
17291
17292 /* Allocate storage for parameters and fill them in. */
17293 TYPE_NFIELDS (ftype) = nparams;
17294 TYPE_FIELDS (ftype) = (struct field *)
17295 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17296
17297 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17298 even if we error out during the parameters reading below. */
17299 for (iparams = 0; iparams < nparams; iparams++)
17300 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17301
17302 iparams = 0;
17303 child_die = die->child;
17304 while (child_die && child_die->tag)
17305 {
17306 if (child_die->tag == DW_TAG_formal_parameter)
17307 {
17308 struct type *arg_type;
17309
17310 /* DWARF version 2 has no clean way to discern C++
17311 static and non-static member functions. G++ helps
17312 GDB by marking the first parameter for non-static
17313 member functions (which is the this pointer) as
17314 artificial. We pass this information to
17315 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17316
17317 DWARF version 3 added DW_AT_object_pointer, which GCC
17318 4.5 does not yet generate. */
17319 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17320 if (attr)
17321 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17322 else
17323 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17324 arg_type = die_type (child_die, cu);
17325
17326 /* RealView does not mark THIS as const, which the testsuite
17327 expects. GCC marks THIS as const in method definitions,
17328 but not in the class specifications (GCC PR 43053). */
17329 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17330 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17331 {
17332 int is_this = 0;
17333 struct dwarf2_cu *arg_cu = cu;
17334 const char *name = dwarf2_name (child_die, cu);
17335
17336 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17337 if (attr)
17338 {
17339 /* If the compiler emits this, use it. */
17340 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17341 is_this = 1;
17342 }
17343 else if (name && strcmp (name, "this") == 0)
17344 /* Function definitions will have the argument names. */
17345 is_this = 1;
17346 else if (name == NULL && iparams == 0)
17347 /* Declarations may not have the names, so like
17348 elsewhere in GDB, assume an artificial first
17349 argument is "this". */
17350 is_this = 1;
17351
17352 if (is_this)
17353 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17354 arg_type, 0);
17355 }
17356
17357 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17358 iparams++;
17359 }
17360 child_die = sibling_die (child_die);
17361 }
17362 }
17363
17364 return ftype;
17365 }
17366
17367 static struct type *
17368 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17369 {
17370 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17371 const char *name = NULL;
17372 struct type *this_type, *target_type;
17373
17374 name = dwarf2_full_name (NULL, die, cu);
17375 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17376 TYPE_TARGET_STUB (this_type) = 1;
17377 set_die_type (die, this_type, cu);
17378 target_type = die_type (die, cu);
17379 if (target_type != this_type)
17380 TYPE_TARGET_TYPE (this_type) = target_type;
17381 else
17382 {
17383 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17384 spec and cause infinite loops in GDB. */
17385 complaint (&symfile_complaints,
17386 _("Self-referential DW_TAG_typedef "
17387 "- DIE at %s [in module %s]"),
17388 sect_offset_str (die->sect_off), objfile_name (objfile));
17389 TYPE_TARGET_TYPE (this_type) = NULL;
17390 }
17391 return this_type;
17392 }
17393
17394 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17395 (which may be different from NAME) to the architecture back-end to allow
17396 it to guess the correct format if necessary. */
17397
17398 static struct type *
17399 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17400 const char *name_hint)
17401 {
17402 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17403 const struct floatformat **format;
17404 struct type *type;
17405
17406 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17407 if (format)
17408 type = init_float_type (objfile, bits, name, format);
17409 else
17410 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17411
17412 return type;
17413 }
17414
17415 /* Find a representation of a given base type and install
17416 it in the TYPE field of the die. */
17417
17418 static struct type *
17419 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17420 {
17421 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17422 struct type *type;
17423 struct attribute *attr;
17424 int encoding = 0, bits = 0;
17425 const char *name;
17426
17427 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17428 if (attr)
17429 {
17430 encoding = DW_UNSND (attr);
17431 }
17432 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17433 if (attr)
17434 {
17435 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17436 }
17437 name = dwarf2_name (die, cu);
17438 if (!name)
17439 {
17440 complaint (&symfile_complaints,
17441 _("DW_AT_name missing from DW_TAG_base_type"));
17442 }
17443
17444 switch (encoding)
17445 {
17446 case DW_ATE_address:
17447 /* Turn DW_ATE_address into a void * pointer. */
17448 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17449 type = init_pointer_type (objfile, bits, name, type);
17450 break;
17451 case DW_ATE_boolean:
17452 type = init_boolean_type (objfile, bits, 1, name);
17453 break;
17454 case DW_ATE_complex_float:
17455 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17456 type = init_complex_type (objfile, name, type);
17457 break;
17458 case DW_ATE_decimal_float:
17459 type = init_decfloat_type (objfile, bits, name);
17460 break;
17461 case DW_ATE_float:
17462 type = dwarf2_init_float_type (objfile, bits, name, name);
17463 break;
17464 case DW_ATE_signed:
17465 type = init_integer_type (objfile, bits, 0, name);
17466 break;
17467 case DW_ATE_unsigned:
17468 if (cu->language == language_fortran
17469 && name
17470 && startswith (name, "character("))
17471 type = init_character_type (objfile, bits, 1, name);
17472 else
17473 type = init_integer_type (objfile, bits, 1, name);
17474 break;
17475 case DW_ATE_signed_char:
17476 if (cu->language == language_ada || cu->language == language_m2
17477 || cu->language == language_pascal
17478 || cu->language == language_fortran)
17479 type = init_character_type (objfile, bits, 0, name);
17480 else
17481 type = init_integer_type (objfile, bits, 0, name);
17482 break;
17483 case DW_ATE_unsigned_char:
17484 if (cu->language == language_ada || cu->language == language_m2
17485 || cu->language == language_pascal
17486 || cu->language == language_fortran
17487 || cu->language == language_rust)
17488 type = init_character_type (objfile, bits, 1, name);
17489 else
17490 type = init_integer_type (objfile, bits, 1, name);
17491 break;
17492 case DW_ATE_UTF:
17493 {
17494 gdbarch *arch = get_objfile_arch (objfile);
17495
17496 if (bits == 16)
17497 type = builtin_type (arch)->builtin_char16;
17498 else if (bits == 32)
17499 type = builtin_type (arch)->builtin_char32;
17500 else
17501 {
17502 complaint (&symfile_complaints,
17503 _("unsupported DW_ATE_UTF bit size: '%d'"),
17504 bits);
17505 type = init_integer_type (objfile, bits, 1, name);
17506 }
17507 return set_die_type (die, type, cu);
17508 }
17509 break;
17510
17511 default:
17512 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17513 dwarf_type_encoding_name (encoding));
17514 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17515 break;
17516 }
17517
17518 if (name && strcmp (name, "char") == 0)
17519 TYPE_NOSIGN (type) = 1;
17520
17521 maybe_set_alignment (cu, die, type);
17522
17523 return set_die_type (die, type, cu);
17524 }
17525
17526 /* Parse dwarf attribute if it's a block, reference or constant and put the
17527 resulting value of the attribute into struct bound_prop.
17528 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17529
17530 static int
17531 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17532 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17533 {
17534 struct dwarf2_property_baton *baton;
17535 struct obstack *obstack
17536 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17537
17538 if (attr == NULL || prop == NULL)
17539 return 0;
17540
17541 if (attr_form_is_block (attr))
17542 {
17543 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17544 baton->referenced_type = NULL;
17545 baton->locexpr.per_cu = cu->per_cu;
17546 baton->locexpr.size = DW_BLOCK (attr)->size;
17547 baton->locexpr.data = DW_BLOCK (attr)->data;
17548 prop->data.baton = baton;
17549 prop->kind = PROP_LOCEXPR;
17550 gdb_assert (prop->data.baton != NULL);
17551 }
17552 else if (attr_form_is_ref (attr))
17553 {
17554 struct dwarf2_cu *target_cu = cu;
17555 struct die_info *target_die;
17556 struct attribute *target_attr;
17557
17558 target_die = follow_die_ref (die, attr, &target_cu);
17559 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17560 if (target_attr == NULL)
17561 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17562 target_cu);
17563 if (target_attr == NULL)
17564 return 0;
17565
17566 switch (target_attr->name)
17567 {
17568 case DW_AT_location:
17569 if (attr_form_is_section_offset (target_attr))
17570 {
17571 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17572 baton->referenced_type = die_type (target_die, target_cu);
17573 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17574 prop->data.baton = baton;
17575 prop->kind = PROP_LOCLIST;
17576 gdb_assert (prop->data.baton != NULL);
17577 }
17578 else if (attr_form_is_block (target_attr))
17579 {
17580 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17581 baton->referenced_type = die_type (target_die, target_cu);
17582 baton->locexpr.per_cu = cu->per_cu;
17583 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17584 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17585 prop->data.baton = baton;
17586 prop->kind = PROP_LOCEXPR;
17587 gdb_assert (prop->data.baton != NULL);
17588 }
17589 else
17590 {
17591 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17592 "dynamic property");
17593 return 0;
17594 }
17595 break;
17596 case DW_AT_data_member_location:
17597 {
17598 LONGEST offset;
17599
17600 if (!handle_data_member_location (target_die, target_cu,
17601 &offset))
17602 return 0;
17603
17604 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17605 baton->referenced_type = read_type_die (target_die->parent,
17606 target_cu);
17607 baton->offset_info.offset = offset;
17608 baton->offset_info.type = die_type (target_die, target_cu);
17609 prop->data.baton = baton;
17610 prop->kind = PROP_ADDR_OFFSET;
17611 break;
17612 }
17613 }
17614 }
17615 else if (attr_form_is_constant (attr))
17616 {
17617 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17618 prop->kind = PROP_CONST;
17619 }
17620 else
17621 {
17622 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17623 dwarf2_name (die, cu));
17624 return 0;
17625 }
17626
17627 return 1;
17628 }
17629
17630 /* Read the given DW_AT_subrange DIE. */
17631
17632 static struct type *
17633 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17634 {
17635 struct type *base_type, *orig_base_type;
17636 struct type *range_type;
17637 struct attribute *attr;
17638 struct dynamic_prop low, high;
17639 int low_default_is_valid;
17640 int high_bound_is_count = 0;
17641 const char *name;
17642 LONGEST negative_mask;
17643
17644 orig_base_type = die_type (die, cu);
17645 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17646 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17647 creating the range type, but we use the result of check_typedef
17648 when examining properties of the type. */
17649 base_type = check_typedef (orig_base_type);
17650
17651 /* The die_type call above may have already set the type for this DIE. */
17652 range_type = get_die_type (die, cu);
17653 if (range_type)
17654 return range_type;
17655
17656 low.kind = PROP_CONST;
17657 high.kind = PROP_CONST;
17658 high.data.const_val = 0;
17659
17660 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17661 omitting DW_AT_lower_bound. */
17662 switch (cu->language)
17663 {
17664 case language_c:
17665 case language_cplus:
17666 low.data.const_val = 0;
17667 low_default_is_valid = 1;
17668 break;
17669 case language_fortran:
17670 low.data.const_val = 1;
17671 low_default_is_valid = 1;
17672 break;
17673 case language_d:
17674 case language_objc:
17675 case language_rust:
17676 low.data.const_val = 0;
17677 low_default_is_valid = (cu->header.version >= 4);
17678 break;
17679 case language_ada:
17680 case language_m2:
17681 case language_pascal:
17682 low.data.const_val = 1;
17683 low_default_is_valid = (cu->header.version >= 4);
17684 break;
17685 default:
17686 low.data.const_val = 0;
17687 low_default_is_valid = 0;
17688 break;
17689 }
17690
17691 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17692 if (attr)
17693 attr_to_dynamic_prop (attr, die, cu, &low);
17694 else if (!low_default_is_valid)
17695 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17696 "- DIE at %s [in module %s]"),
17697 sect_offset_str (die->sect_off),
17698 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17699
17700 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17701 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17702 {
17703 attr = dwarf2_attr (die, DW_AT_count, cu);
17704 if (attr_to_dynamic_prop (attr, die, cu, &high))
17705 {
17706 /* If bounds are constant do the final calculation here. */
17707 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17708 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17709 else
17710 high_bound_is_count = 1;
17711 }
17712 }
17713
17714 /* Dwarf-2 specifications explicitly allows to create subrange types
17715 without specifying a base type.
17716 In that case, the base type must be set to the type of
17717 the lower bound, upper bound or count, in that order, if any of these
17718 three attributes references an object that has a type.
17719 If no base type is found, the Dwarf-2 specifications say that
17720 a signed integer type of size equal to the size of an address should
17721 be used.
17722 For the following C code: `extern char gdb_int [];'
17723 GCC produces an empty range DIE.
17724 FIXME: muller/2010-05-28: Possible references to object for low bound,
17725 high bound or count are not yet handled by this code. */
17726 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17727 {
17728 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17729 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17730 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17731 struct type *int_type = objfile_type (objfile)->builtin_int;
17732
17733 /* Test "int", "long int", and "long long int" objfile types,
17734 and select the first one having a size above or equal to the
17735 architecture address size. */
17736 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17737 base_type = int_type;
17738 else
17739 {
17740 int_type = objfile_type (objfile)->builtin_long;
17741 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17742 base_type = int_type;
17743 else
17744 {
17745 int_type = objfile_type (objfile)->builtin_long_long;
17746 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17747 base_type = int_type;
17748 }
17749 }
17750 }
17751
17752 /* Normally, the DWARF producers are expected to use a signed
17753 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17754 But this is unfortunately not always the case, as witnessed
17755 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17756 is used instead. To work around that ambiguity, we treat
17757 the bounds as signed, and thus sign-extend their values, when
17758 the base type is signed. */
17759 negative_mask =
17760 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17761 if (low.kind == PROP_CONST
17762 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17763 low.data.const_val |= negative_mask;
17764 if (high.kind == PROP_CONST
17765 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17766 high.data.const_val |= negative_mask;
17767
17768 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17769
17770 if (high_bound_is_count)
17771 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17772
17773 /* Ada expects an empty array on no boundary attributes. */
17774 if (attr == NULL && cu->language != language_ada)
17775 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17776
17777 name = dwarf2_name (die, cu);
17778 if (name)
17779 TYPE_NAME (range_type) = name;
17780
17781 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17782 if (attr)
17783 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17784
17785 maybe_set_alignment (cu, die, range_type);
17786
17787 set_die_type (die, range_type, cu);
17788
17789 /* set_die_type should be already done. */
17790 set_descriptive_type (range_type, die, cu);
17791
17792 return range_type;
17793 }
17794
17795 static struct type *
17796 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17797 {
17798 struct type *type;
17799
17800 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17801 NULL);
17802 TYPE_NAME (type) = dwarf2_name (die, cu);
17803
17804 /* In Ada, an unspecified type is typically used when the description
17805 of the type is defered to a different unit. When encountering
17806 such a type, we treat it as a stub, and try to resolve it later on,
17807 when needed. */
17808 if (cu->language == language_ada)
17809 TYPE_STUB (type) = 1;
17810
17811 return set_die_type (die, type, cu);
17812 }
17813
17814 /* Read a single die and all its descendents. Set the die's sibling
17815 field to NULL; set other fields in the die correctly, and set all
17816 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17817 location of the info_ptr after reading all of those dies. PARENT
17818 is the parent of the die in question. */
17819
17820 static struct die_info *
17821 read_die_and_children (const struct die_reader_specs *reader,
17822 const gdb_byte *info_ptr,
17823 const gdb_byte **new_info_ptr,
17824 struct die_info *parent)
17825 {
17826 struct die_info *die;
17827 const gdb_byte *cur_ptr;
17828 int has_children;
17829
17830 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17831 if (die == NULL)
17832 {
17833 *new_info_ptr = cur_ptr;
17834 return NULL;
17835 }
17836 store_in_ref_table (die, reader->cu);
17837
17838 if (has_children)
17839 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17840 else
17841 {
17842 die->child = NULL;
17843 *new_info_ptr = cur_ptr;
17844 }
17845
17846 die->sibling = NULL;
17847 die->parent = parent;
17848 return die;
17849 }
17850
17851 /* Read a die, all of its descendents, and all of its siblings; set
17852 all of the fields of all of the dies correctly. Arguments are as
17853 in read_die_and_children. */
17854
17855 static struct die_info *
17856 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17857 const gdb_byte *info_ptr,
17858 const gdb_byte **new_info_ptr,
17859 struct die_info *parent)
17860 {
17861 struct die_info *first_die, *last_sibling;
17862 const gdb_byte *cur_ptr;
17863
17864 cur_ptr = info_ptr;
17865 first_die = last_sibling = NULL;
17866
17867 while (1)
17868 {
17869 struct die_info *die
17870 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17871
17872 if (die == NULL)
17873 {
17874 *new_info_ptr = cur_ptr;
17875 return first_die;
17876 }
17877
17878 if (!first_die)
17879 first_die = die;
17880 else
17881 last_sibling->sibling = die;
17882
17883 last_sibling = die;
17884 }
17885 }
17886
17887 /* Read a die, all of its descendents, and all of its siblings; set
17888 all of the fields of all of the dies correctly. Arguments are as
17889 in read_die_and_children.
17890 This the main entry point for reading a DIE and all its children. */
17891
17892 static struct die_info *
17893 read_die_and_siblings (const struct die_reader_specs *reader,
17894 const gdb_byte *info_ptr,
17895 const gdb_byte **new_info_ptr,
17896 struct die_info *parent)
17897 {
17898 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17899 new_info_ptr, parent);
17900
17901 if (dwarf_die_debug)
17902 {
17903 fprintf_unfiltered (gdb_stdlog,
17904 "Read die from %s@0x%x of %s:\n",
17905 get_section_name (reader->die_section),
17906 (unsigned) (info_ptr - reader->die_section->buffer),
17907 bfd_get_filename (reader->abfd));
17908 dump_die (die, dwarf_die_debug);
17909 }
17910
17911 return die;
17912 }
17913
17914 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17915 attributes.
17916 The caller is responsible for filling in the extra attributes
17917 and updating (*DIEP)->num_attrs.
17918 Set DIEP to point to a newly allocated die with its information,
17919 except for its child, sibling, and parent fields.
17920 Set HAS_CHILDREN to tell whether the die has children or not. */
17921
17922 static const gdb_byte *
17923 read_full_die_1 (const struct die_reader_specs *reader,
17924 struct die_info **diep, const gdb_byte *info_ptr,
17925 int *has_children, int num_extra_attrs)
17926 {
17927 unsigned int abbrev_number, bytes_read, i;
17928 struct abbrev_info *abbrev;
17929 struct die_info *die;
17930 struct dwarf2_cu *cu = reader->cu;
17931 bfd *abfd = reader->abfd;
17932
17933 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17934 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17935 info_ptr += bytes_read;
17936 if (!abbrev_number)
17937 {
17938 *diep = NULL;
17939 *has_children = 0;
17940 return info_ptr;
17941 }
17942
17943 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17944 if (!abbrev)
17945 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17946 abbrev_number,
17947 bfd_get_filename (abfd));
17948
17949 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17950 die->sect_off = sect_off;
17951 die->tag = abbrev->tag;
17952 die->abbrev = abbrev_number;
17953
17954 /* Make the result usable.
17955 The caller needs to update num_attrs after adding the extra
17956 attributes. */
17957 die->num_attrs = abbrev->num_attrs;
17958
17959 for (i = 0; i < abbrev->num_attrs; ++i)
17960 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17961 info_ptr);
17962
17963 *diep = die;
17964 *has_children = abbrev->has_children;
17965 return info_ptr;
17966 }
17967
17968 /* Read a die and all its attributes.
17969 Set DIEP to point to a newly allocated die with its information,
17970 except for its child, sibling, and parent fields.
17971 Set HAS_CHILDREN to tell whether the die has children or not. */
17972
17973 static const gdb_byte *
17974 read_full_die (const struct die_reader_specs *reader,
17975 struct die_info **diep, const gdb_byte *info_ptr,
17976 int *has_children)
17977 {
17978 const gdb_byte *result;
17979
17980 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17981
17982 if (dwarf_die_debug)
17983 {
17984 fprintf_unfiltered (gdb_stdlog,
17985 "Read die from %s@0x%x of %s:\n",
17986 get_section_name (reader->die_section),
17987 (unsigned) (info_ptr - reader->die_section->buffer),
17988 bfd_get_filename (reader->abfd));
17989 dump_die (*diep, dwarf_die_debug);
17990 }
17991
17992 return result;
17993 }
17994 \f
17995 /* Abbreviation tables.
17996
17997 In DWARF version 2, the description of the debugging information is
17998 stored in a separate .debug_abbrev section. Before we read any
17999 dies from a section we read in all abbreviations and install them
18000 in a hash table. */
18001
18002 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18003
18004 struct abbrev_info *
18005 abbrev_table::alloc_abbrev ()
18006 {
18007 struct abbrev_info *abbrev;
18008
18009 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18010 memset (abbrev, 0, sizeof (struct abbrev_info));
18011
18012 return abbrev;
18013 }
18014
18015 /* Add an abbreviation to the table. */
18016
18017 void
18018 abbrev_table::add_abbrev (unsigned int abbrev_number,
18019 struct abbrev_info *abbrev)
18020 {
18021 unsigned int hash_number;
18022
18023 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18024 abbrev->next = m_abbrevs[hash_number];
18025 m_abbrevs[hash_number] = abbrev;
18026 }
18027
18028 /* Look up an abbrev in the table.
18029 Returns NULL if the abbrev is not found. */
18030
18031 struct abbrev_info *
18032 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18033 {
18034 unsigned int hash_number;
18035 struct abbrev_info *abbrev;
18036
18037 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18038 abbrev = m_abbrevs[hash_number];
18039
18040 while (abbrev)
18041 {
18042 if (abbrev->number == abbrev_number)
18043 return abbrev;
18044 abbrev = abbrev->next;
18045 }
18046 return NULL;
18047 }
18048
18049 /* Read in an abbrev table. */
18050
18051 static abbrev_table_up
18052 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18053 struct dwarf2_section_info *section,
18054 sect_offset sect_off)
18055 {
18056 struct objfile *objfile = dwarf2_per_objfile->objfile;
18057 bfd *abfd = get_section_bfd_owner (section);
18058 const gdb_byte *abbrev_ptr;
18059 struct abbrev_info *cur_abbrev;
18060 unsigned int abbrev_number, bytes_read, abbrev_name;
18061 unsigned int abbrev_form;
18062 struct attr_abbrev *cur_attrs;
18063 unsigned int allocated_attrs;
18064
18065 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18066
18067 dwarf2_read_section (objfile, section);
18068 abbrev_ptr = section->buffer + to_underlying (sect_off);
18069 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18070 abbrev_ptr += bytes_read;
18071
18072 allocated_attrs = ATTR_ALLOC_CHUNK;
18073 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18074
18075 /* Loop until we reach an abbrev number of 0. */
18076 while (abbrev_number)
18077 {
18078 cur_abbrev = abbrev_table->alloc_abbrev ();
18079
18080 /* read in abbrev header */
18081 cur_abbrev->number = abbrev_number;
18082 cur_abbrev->tag
18083 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18084 abbrev_ptr += bytes_read;
18085 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18086 abbrev_ptr += 1;
18087
18088 /* now read in declarations */
18089 for (;;)
18090 {
18091 LONGEST implicit_const;
18092
18093 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18094 abbrev_ptr += bytes_read;
18095 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18096 abbrev_ptr += bytes_read;
18097 if (abbrev_form == DW_FORM_implicit_const)
18098 {
18099 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18100 &bytes_read);
18101 abbrev_ptr += bytes_read;
18102 }
18103 else
18104 {
18105 /* Initialize it due to a false compiler warning. */
18106 implicit_const = -1;
18107 }
18108
18109 if (abbrev_name == 0)
18110 break;
18111
18112 if (cur_abbrev->num_attrs == allocated_attrs)
18113 {
18114 allocated_attrs += ATTR_ALLOC_CHUNK;
18115 cur_attrs
18116 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18117 }
18118
18119 cur_attrs[cur_abbrev->num_attrs].name
18120 = (enum dwarf_attribute) abbrev_name;
18121 cur_attrs[cur_abbrev->num_attrs].form
18122 = (enum dwarf_form) abbrev_form;
18123 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18124 ++cur_abbrev->num_attrs;
18125 }
18126
18127 cur_abbrev->attrs =
18128 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18129 cur_abbrev->num_attrs);
18130 memcpy (cur_abbrev->attrs, cur_attrs,
18131 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18132
18133 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18134
18135 /* Get next abbreviation.
18136 Under Irix6 the abbreviations for a compilation unit are not
18137 always properly terminated with an abbrev number of 0.
18138 Exit loop if we encounter an abbreviation which we have
18139 already read (which means we are about to read the abbreviations
18140 for the next compile unit) or if the end of the abbreviation
18141 table is reached. */
18142 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18143 break;
18144 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18145 abbrev_ptr += bytes_read;
18146 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18147 break;
18148 }
18149
18150 xfree (cur_attrs);
18151 return abbrev_table;
18152 }
18153
18154 /* Returns nonzero if TAG represents a type that we might generate a partial
18155 symbol for. */
18156
18157 static int
18158 is_type_tag_for_partial (int tag)
18159 {
18160 switch (tag)
18161 {
18162 #if 0
18163 /* Some types that would be reasonable to generate partial symbols for,
18164 that we don't at present. */
18165 case DW_TAG_array_type:
18166 case DW_TAG_file_type:
18167 case DW_TAG_ptr_to_member_type:
18168 case DW_TAG_set_type:
18169 case DW_TAG_string_type:
18170 case DW_TAG_subroutine_type:
18171 #endif
18172 case DW_TAG_base_type:
18173 case DW_TAG_class_type:
18174 case DW_TAG_interface_type:
18175 case DW_TAG_enumeration_type:
18176 case DW_TAG_structure_type:
18177 case DW_TAG_subrange_type:
18178 case DW_TAG_typedef:
18179 case DW_TAG_union_type:
18180 return 1;
18181 default:
18182 return 0;
18183 }
18184 }
18185
18186 /* Load all DIEs that are interesting for partial symbols into memory. */
18187
18188 static struct partial_die_info *
18189 load_partial_dies (const struct die_reader_specs *reader,
18190 const gdb_byte *info_ptr, int building_psymtab)
18191 {
18192 struct dwarf2_cu *cu = reader->cu;
18193 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18194 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18195 unsigned int bytes_read;
18196 unsigned int load_all = 0;
18197 int nesting_level = 1;
18198
18199 parent_die = NULL;
18200 last_die = NULL;
18201
18202 gdb_assert (cu->per_cu != NULL);
18203 if (cu->per_cu->load_all_dies)
18204 load_all = 1;
18205
18206 cu->partial_dies
18207 = htab_create_alloc_ex (cu->header.length / 12,
18208 partial_die_hash,
18209 partial_die_eq,
18210 NULL,
18211 &cu->comp_unit_obstack,
18212 hashtab_obstack_allocate,
18213 dummy_obstack_deallocate);
18214
18215 while (1)
18216 {
18217 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18218
18219 /* A NULL abbrev means the end of a series of children. */
18220 if (abbrev == NULL)
18221 {
18222 if (--nesting_level == 0)
18223 return first_die;
18224
18225 info_ptr += bytes_read;
18226 last_die = parent_die;
18227 parent_die = parent_die->die_parent;
18228 continue;
18229 }
18230
18231 /* Check for template arguments. We never save these; if
18232 they're seen, we just mark the parent, and go on our way. */
18233 if (parent_die != NULL
18234 && cu->language == language_cplus
18235 && (abbrev->tag == DW_TAG_template_type_param
18236 || abbrev->tag == DW_TAG_template_value_param))
18237 {
18238 parent_die->has_template_arguments = 1;
18239
18240 if (!load_all)
18241 {
18242 /* We don't need a partial DIE for the template argument. */
18243 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18244 continue;
18245 }
18246 }
18247
18248 /* We only recurse into c++ subprograms looking for template arguments.
18249 Skip their other children. */
18250 if (!load_all
18251 && cu->language == language_cplus
18252 && parent_die != NULL
18253 && parent_die->tag == DW_TAG_subprogram)
18254 {
18255 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18256 continue;
18257 }
18258
18259 /* Check whether this DIE is interesting enough to save. Normally
18260 we would not be interested in members here, but there may be
18261 later variables referencing them via DW_AT_specification (for
18262 static members). */
18263 if (!load_all
18264 && !is_type_tag_for_partial (abbrev->tag)
18265 && abbrev->tag != DW_TAG_constant
18266 && abbrev->tag != DW_TAG_enumerator
18267 && abbrev->tag != DW_TAG_subprogram
18268 && abbrev->tag != DW_TAG_inlined_subroutine
18269 && abbrev->tag != DW_TAG_lexical_block
18270 && abbrev->tag != DW_TAG_variable
18271 && abbrev->tag != DW_TAG_namespace
18272 && abbrev->tag != DW_TAG_module
18273 && abbrev->tag != DW_TAG_member
18274 && abbrev->tag != DW_TAG_imported_unit
18275 && abbrev->tag != DW_TAG_imported_declaration)
18276 {
18277 /* Otherwise we skip to the next sibling, if any. */
18278 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18279 continue;
18280 }
18281
18282 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18283 abbrev);
18284
18285 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18286
18287 /* This two-pass algorithm for processing partial symbols has a
18288 high cost in cache pressure. Thus, handle some simple cases
18289 here which cover the majority of C partial symbols. DIEs
18290 which neither have specification tags in them, nor could have
18291 specification tags elsewhere pointing at them, can simply be
18292 processed and discarded.
18293
18294 This segment is also optional; scan_partial_symbols and
18295 add_partial_symbol will handle these DIEs if we chain
18296 them in normally. When compilers which do not emit large
18297 quantities of duplicate debug information are more common,
18298 this code can probably be removed. */
18299
18300 /* Any complete simple types at the top level (pretty much all
18301 of them, for a language without namespaces), can be processed
18302 directly. */
18303 if (parent_die == NULL
18304 && pdi.has_specification == 0
18305 && pdi.is_declaration == 0
18306 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18307 || pdi.tag == DW_TAG_base_type
18308 || pdi.tag == DW_TAG_subrange_type))
18309 {
18310 if (building_psymtab && pdi.name != NULL)
18311 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18312 VAR_DOMAIN, LOC_TYPEDEF,
18313 &objfile->static_psymbols,
18314 0, cu->language, objfile);
18315 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18316 continue;
18317 }
18318
18319 /* The exception for DW_TAG_typedef with has_children above is
18320 a workaround of GCC PR debug/47510. In the case of this complaint
18321 type_name_no_tag_or_error will error on such types later.
18322
18323 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18324 it could not find the child DIEs referenced later, this is checked
18325 above. In correct DWARF DW_TAG_typedef should have no children. */
18326
18327 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18328 complaint (&symfile_complaints,
18329 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18330 "- DIE at %s [in module %s]"),
18331 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18332
18333 /* If we're at the second level, and we're an enumerator, and
18334 our parent has no specification (meaning possibly lives in a
18335 namespace elsewhere), then we can add the partial symbol now
18336 instead of queueing it. */
18337 if (pdi.tag == DW_TAG_enumerator
18338 && parent_die != NULL
18339 && parent_die->die_parent == NULL
18340 && parent_die->tag == DW_TAG_enumeration_type
18341 && parent_die->has_specification == 0)
18342 {
18343 if (pdi.name == NULL)
18344 complaint (&symfile_complaints,
18345 _("malformed enumerator DIE ignored"));
18346 else if (building_psymtab)
18347 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18348 VAR_DOMAIN, LOC_CONST,
18349 cu->language == language_cplus
18350 ? &objfile->global_psymbols
18351 : &objfile->static_psymbols,
18352 0, cu->language, objfile);
18353
18354 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18355 continue;
18356 }
18357
18358 struct partial_die_info *part_die
18359 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18360
18361 /* We'll save this DIE so link it in. */
18362 part_die->die_parent = parent_die;
18363 part_die->die_sibling = NULL;
18364 part_die->die_child = NULL;
18365
18366 if (last_die && last_die == parent_die)
18367 last_die->die_child = part_die;
18368 else if (last_die)
18369 last_die->die_sibling = part_die;
18370
18371 last_die = part_die;
18372
18373 if (first_die == NULL)
18374 first_die = part_die;
18375
18376 /* Maybe add the DIE to the hash table. Not all DIEs that we
18377 find interesting need to be in the hash table, because we
18378 also have the parent/sibling/child chains; only those that we
18379 might refer to by offset later during partial symbol reading.
18380
18381 For now this means things that might have be the target of a
18382 DW_AT_specification, DW_AT_abstract_origin, or
18383 DW_AT_extension. DW_AT_extension will refer only to
18384 namespaces; DW_AT_abstract_origin refers to functions (and
18385 many things under the function DIE, but we do not recurse
18386 into function DIEs during partial symbol reading) and
18387 possibly variables as well; DW_AT_specification refers to
18388 declarations. Declarations ought to have the DW_AT_declaration
18389 flag. It happens that GCC forgets to put it in sometimes, but
18390 only for functions, not for types.
18391
18392 Adding more things than necessary to the hash table is harmless
18393 except for the performance cost. Adding too few will result in
18394 wasted time in find_partial_die, when we reread the compilation
18395 unit with load_all_dies set. */
18396
18397 if (load_all
18398 || abbrev->tag == DW_TAG_constant
18399 || abbrev->tag == DW_TAG_subprogram
18400 || abbrev->tag == DW_TAG_variable
18401 || abbrev->tag == DW_TAG_namespace
18402 || part_die->is_declaration)
18403 {
18404 void **slot;
18405
18406 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18407 to_underlying (part_die->sect_off),
18408 INSERT);
18409 *slot = part_die;
18410 }
18411
18412 /* For some DIEs we want to follow their children (if any). For C
18413 we have no reason to follow the children of structures; for other
18414 languages we have to, so that we can get at method physnames
18415 to infer fully qualified class names, for DW_AT_specification,
18416 and for C++ template arguments. For C++, we also look one level
18417 inside functions to find template arguments (if the name of the
18418 function does not already contain the template arguments).
18419
18420 For Ada, we need to scan the children of subprograms and lexical
18421 blocks as well because Ada allows the definition of nested
18422 entities that could be interesting for the debugger, such as
18423 nested subprograms for instance. */
18424 if (last_die->has_children
18425 && (load_all
18426 || last_die->tag == DW_TAG_namespace
18427 || last_die->tag == DW_TAG_module
18428 || last_die->tag == DW_TAG_enumeration_type
18429 || (cu->language == language_cplus
18430 && last_die->tag == DW_TAG_subprogram
18431 && (last_die->name == NULL
18432 || strchr (last_die->name, '<') == NULL))
18433 || (cu->language != language_c
18434 && (last_die->tag == DW_TAG_class_type
18435 || last_die->tag == DW_TAG_interface_type
18436 || last_die->tag == DW_TAG_structure_type
18437 || last_die->tag == DW_TAG_union_type))
18438 || (cu->language == language_ada
18439 && (last_die->tag == DW_TAG_subprogram
18440 || last_die->tag == DW_TAG_lexical_block))))
18441 {
18442 nesting_level++;
18443 parent_die = last_die;
18444 continue;
18445 }
18446
18447 /* Otherwise we skip to the next sibling, if any. */
18448 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18449
18450 /* Back to the top, do it again. */
18451 }
18452 }
18453
18454 partial_die_info::partial_die_info (sect_offset sect_off_,
18455 struct abbrev_info *abbrev)
18456 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18457 {
18458 }
18459
18460 /* Read a minimal amount of information into the minimal die structure.
18461 INFO_PTR should point just after the initial uleb128 of a DIE. */
18462
18463 const gdb_byte *
18464 partial_die_info::read (const struct die_reader_specs *reader,
18465 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18466 {
18467 struct dwarf2_cu *cu = reader->cu;
18468 struct dwarf2_per_objfile *dwarf2_per_objfile
18469 = cu->per_cu->dwarf2_per_objfile;
18470 unsigned int i;
18471 int has_low_pc_attr = 0;
18472 int has_high_pc_attr = 0;
18473 int high_pc_relative = 0;
18474
18475 for (i = 0; i < abbrev.num_attrs; ++i)
18476 {
18477 struct attribute attr;
18478
18479 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18480
18481 /* Store the data if it is of an attribute we want to keep in a
18482 partial symbol table. */
18483 switch (attr.name)
18484 {
18485 case DW_AT_name:
18486 switch (tag)
18487 {
18488 case DW_TAG_compile_unit:
18489 case DW_TAG_partial_unit:
18490 case DW_TAG_type_unit:
18491 /* Compilation units have a DW_AT_name that is a filename, not
18492 a source language identifier. */
18493 case DW_TAG_enumeration_type:
18494 case DW_TAG_enumerator:
18495 /* These tags always have simple identifiers already; no need
18496 to canonicalize them. */
18497 name = DW_STRING (&attr);
18498 break;
18499 default:
18500 {
18501 struct objfile *objfile = dwarf2_per_objfile->objfile;
18502
18503 name
18504 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18505 &objfile->per_bfd->storage_obstack);
18506 }
18507 break;
18508 }
18509 break;
18510 case DW_AT_linkage_name:
18511 case DW_AT_MIPS_linkage_name:
18512 /* Note that both forms of linkage name might appear. We
18513 assume they will be the same, and we only store the last
18514 one we see. */
18515 if (cu->language == language_ada)
18516 name = DW_STRING (&attr);
18517 linkage_name = DW_STRING (&attr);
18518 break;
18519 case DW_AT_low_pc:
18520 has_low_pc_attr = 1;
18521 lowpc = attr_value_as_address (&attr);
18522 break;
18523 case DW_AT_high_pc:
18524 has_high_pc_attr = 1;
18525 highpc = attr_value_as_address (&attr);
18526 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18527 high_pc_relative = 1;
18528 break;
18529 case DW_AT_location:
18530 /* Support the .debug_loc offsets. */
18531 if (attr_form_is_block (&attr))
18532 {
18533 d.locdesc = DW_BLOCK (&attr);
18534 }
18535 else if (attr_form_is_section_offset (&attr))
18536 {
18537 dwarf2_complex_location_expr_complaint ();
18538 }
18539 else
18540 {
18541 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18542 "partial symbol information");
18543 }
18544 break;
18545 case DW_AT_external:
18546 is_external = DW_UNSND (&attr);
18547 break;
18548 case DW_AT_declaration:
18549 is_declaration = DW_UNSND (&attr);
18550 break;
18551 case DW_AT_type:
18552 has_type = 1;
18553 break;
18554 case DW_AT_abstract_origin:
18555 case DW_AT_specification:
18556 case DW_AT_extension:
18557 has_specification = 1;
18558 spec_offset = dwarf2_get_ref_die_offset (&attr);
18559 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18560 || cu->per_cu->is_dwz);
18561 break;
18562 case DW_AT_sibling:
18563 /* Ignore absolute siblings, they might point outside of
18564 the current compile unit. */
18565 if (attr.form == DW_FORM_ref_addr)
18566 complaint (&symfile_complaints,
18567 _("ignoring absolute DW_AT_sibling"));
18568 else
18569 {
18570 const gdb_byte *buffer = reader->buffer;
18571 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18572 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18573
18574 if (sibling_ptr < info_ptr)
18575 complaint (&symfile_complaints,
18576 _("DW_AT_sibling points backwards"));
18577 else if (sibling_ptr > reader->buffer_end)
18578 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18579 else
18580 sibling = sibling_ptr;
18581 }
18582 break;
18583 case DW_AT_byte_size:
18584 has_byte_size = 1;
18585 break;
18586 case DW_AT_const_value:
18587 has_const_value = 1;
18588 break;
18589 case DW_AT_calling_convention:
18590 /* DWARF doesn't provide a way to identify a program's source-level
18591 entry point. DW_AT_calling_convention attributes are only meant
18592 to describe functions' calling conventions.
18593
18594 However, because it's a necessary piece of information in
18595 Fortran, and before DWARF 4 DW_CC_program was the only
18596 piece of debugging information whose definition refers to
18597 a 'main program' at all, several compilers marked Fortran
18598 main programs with DW_CC_program --- even when those
18599 functions use the standard calling conventions.
18600
18601 Although DWARF now specifies a way to provide this
18602 information, we support this practice for backward
18603 compatibility. */
18604 if (DW_UNSND (&attr) == DW_CC_program
18605 && cu->language == language_fortran)
18606 main_subprogram = 1;
18607 break;
18608 case DW_AT_inline:
18609 if (DW_UNSND (&attr) == DW_INL_inlined
18610 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18611 may_be_inlined = 1;
18612 break;
18613
18614 case DW_AT_import:
18615 if (tag == DW_TAG_imported_unit)
18616 {
18617 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18618 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18619 || cu->per_cu->is_dwz);
18620 }
18621 break;
18622
18623 case DW_AT_main_subprogram:
18624 main_subprogram = DW_UNSND (&attr);
18625 break;
18626
18627 default:
18628 break;
18629 }
18630 }
18631
18632 if (high_pc_relative)
18633 highpc += lowpc;
18634
18635 if (has_low_pc_attr && has_high_pc_attr)
18636 {
18637 /* When using the GNU linker, .gnu.linkonce. sections are used to
18638 eliminate duplicate copies of functions and vtables and such.
18639 The linker will arbitrarily choose one and discard the others.
18640 The AT_*_pc values for such functions refer to local labels in
18641 these sections. If the section from that file was discarded, the
18642 labels are not in the output, so the relocs get a value of 0.
18643 If this is a discarded function, mark the pc bounds as invalid,
18644 so that GDB will ignore it. */
18645 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18646 {
18647 struct objfile *objfile = dwarf2_per_objfile->objfile;
18648 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18649
18650 complaint (&symfile_complaints,
18651 _("DW_AT_low_pc %s is zero "
18652 "for DIE at %s [in module %s]"),
18653 paddress (gdbarch, lowpc),
18654 sect_offset_str (sect_off),
18655 objfile_name (objfile));
18656 }
18657 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18658 else if (lowpc >= highpc)
18659 {
18660 struct objfile *objfile = dwarf2_per_objfile->objfile;
18661 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18662
18663 complaint (&symfile_complaints,
18664 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18665 "for DIE at %s [in module %s]"),
18666 paddress (gdbarch, lowpc),
18667 paddress (gdbarch, highpc),
18668 sect_offset_str (sect_off),
18669 objfile_name (objfile));
18670 }
18671 else
18672 has_pc_info = 1;
18673 }
18674
18675 return info_ptr;
18676 }
18677
18678 /* Find a cached partial DIE at OFFSET in CU. */
18679
18680 struct partial_die_info *
18681 dwarf2_cu::find_partial_die (sect_offset sect_off)
18682 {
18683 struct partial_die_info *lookup_die = NULL;
18684 struct partial_die_info part_die (sect_off);
18685
18686 lookup_die = ((struct partial_die_info *)
18687 htab_find_with_hash (partial_dies, &part_die,
18688 to_underlying (sect_off)));
18689
18690 return lookup_die;
18691 }
18692
18693 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18694 except in the case of .debug_types DIEs which do not reference
18695 outside their CU (they do however referencing other types via
18696 DW_FORM_ref_sig8). */
18697
18698 static struct partial_die_info *
18699 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18700 {
18701 struct dwarf2_per_objfile *dwarf2_per_objfile
18702 = cu->per_cu->dwarf2_per_objfile;
18703 struct objfile *objfile = dwarf2_per_objfile->objfile;
18704 struct dwarf2_per_cu_data *per_cu = NULL;
18705 struct partial_die_info *pd = NULL;
18706
18707 if (offset_in_dwz == cu->per_cu->is_dwz
18708 && offset_in_cu_p (&cu->header, sect_off))
18709 {
18710 pd = cu->find_partial_die (sect_off);
18711 if (pd != NULL)
18712 return pd;
18713 /* We missed recording what we needed.
18714 Load all dies and try again. */
18715 per_cu = cu->per_cu;
18716 }
18717 else
18718 {
18719 /* TUs don't reference other CUs/TUs (except via type signatures). */
18720 if (cu->per_cu->is_debug_types)
18721 {
18722 error (_("Dwarf Error: Type Unit at offset %s contains"
18723 " external reference to offset %s [in module %s].\n"),
18724 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18725 bfd_get_filename (objfile->obfd));
18726 }
18727 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18728 dwarf2_per_objfile);
18729
18730 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18731 load_partial_comp_unit (per_cu);
18732
18733 per_cu->cu->last_used = 0;
18734 pd = per_cu->cu->find_partial_die (sect_off);
18735 }
18736
18737 /* If we didn't find it, and not all dies have been loaded,
18738 load them all and try again. */
18739
18740 if (pd == NULL && per_cu->load_all_dies == 0)
18741 {
18742 per_cu->load_all_dies = 1;
18743
18744 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18745 THIS_CU->cu may already be in use. So we can't just free it and
18746 replace its DIEs with the ones we read in. Instead, we leave those
18747 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18748 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18749 set. */
18750 load_partial_comp_unit (per_cu);
18751
18752 pd = per_cu->cu->find_partial_die (sect_off);
18753 }
18754
18755 if (pd == NULL)
18756 internal_error (__FILE__, __LINE__,
18757 _("could not find partial DIE %s "
18758 "in cache [from module %s]\n"),
18759 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18760 return pd;
18761 }
18762
18763 /* See if we can figure out if the class lives in a namespace. We do
18764 this by looking for a member function; its demangled name will
18765 contain namespace info, if there is any. */
18766
18767 static void
18768 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18769 struct dwarf2_cu *cu)
18770 {
18771 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18772 what template types look like, because the demangler
18773 frequently doesn't give the same name as the debug info. We
18774 could fix this by only using the demangled name to get the
18775 prefix (but see comment in read_structure_type). */
18776
18777 struct partial_die_info *real_pdi;
18778 struct partial_die_info *child_pdi;
18779
18780 /* If this DIE (this DIE's specification, if any) has a parent, then
18781 we should not do this. We'll prepend the parent's fully qualified
18782 name when we create the partial symbol. */
18783
18784 real_pdi = struct_pdi;
18785 while (real_pdi->has_specification)
18786 real_pdi = find_partial_die (real_pdi->spec_offset,
18787 real_pdi->spec_is_dwz, cu);
18788
18789 if (real_pdi->die_parent != NULL)
18790 return;
18791
18792 for (child_pdi = struct_pdi->die_child;
18793 child_pdi != NULL;
18794 child_pdi = child_pdi->die_sibling)
18795 {
18796 if (child_pdi->tag == DW_TAG_subprogram
18797 && child_pdi->linkage_name != NULL)
18798 {
18799 char *actual_class_name
18800 = language_class_name_from_physname (cu->language_defn,
18801 child_pdi->linkage_name);
18802 if (actual_class_name != NULL)
18803 {
18804 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18805 struct_pdi->name
18806 = ((const char *)
18807 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18808 actual_class_name,
18809 strlen (actual_class_name)));
18810 xfree (actual_class_name);
18811 }
18812 break;
18813 }
18814 }
18815 }
18816
18817 void
18818 partial_die_info::fixup (struct dwarf2_cu *cu)
18819 {
18820 /* Once we've fixed up a die, there's no point in doing so again.
18821 This also avoids a memory leak if we were to call
18822 guess_partial_die_structure_name multiple times. */
18823 if (fixup_called)
18824 return;
18825
18826 /* If we found a reference attribute and the DIE has no name, try
18827 to find a name in the referred to DIE. */
18828
18829 if (name == NULL && has_specification)
18830 {
18831 struct partial_die_info *spec_die;
18832
18833 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
18834
18835 spec_die->fixup (cu);
18836
18837 if (spec_die->name)
18838 {
18839 name = spec_die->name;
18840
18841 /* Copy DW_AT_external attribute if it is set. */
18842 if (spec_die->is_external)
18843 is_external = spec_die->is_external;
18844 }
18845 }
18846
18847 /* Set default names for some unnamed DIEs. */
18848
18849 if (name == NULL && tag == DW_TAG_namespace)
18850 name = CP_ANONYMOUS_NAMESPACE_STR;
18851
18852 /* If there is no parent die to provide a namespace, and there are
18853 children, see if we can determine the namespace from their linkage
18854 name. */
18855 if (cu->language == language_cplus
18856 && !VEC_empty (dwarf2_section_info_def,
18857 cu->per_cu->dwarf2_per_objfile->types)
18858 && die_parent == NULL
18859 && has_children
18860 && (tag == DW_TAG_class_type
18861 || tag == DW_TAG_structure_type
18862 || tag == DW_TAG_union_type))
18863 guess_partial_die_structure_name (this, cu);
18864
18865 /* GCC might emit a nameless struct or union that has a linkage
18866 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18867 if (name == NULL
18868 && (tag == DW_TAG_class_type
18869 || tag == DW_TAG_interface_type
18870 || tag == DW_TAG_structure_type
18871 || tag == DW_TAG_union_type)
18872 && linkage_name != NULL)
18873 {
18874 char *demangled;
18875
18876 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
18877 if (demangled)
18878 {
18879 const char *base;
18880
18881 /* Strip any leading namespaces/classes, keep only the base name.
18882 DW_AT_name for named DIEs does not contain the prefixes. */
18883 base = strrchr (demangled, ':');
18884 if (base && base > demangled && base[-1] == ':')
18885 base++;
18886 else
18887 base = demangled;
18888
18889 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18890 name
18891 = ((const char *)
18892 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18893 base, strlen (base)));
18894 xfree (demangled);
18895 }
18896 }
18897
18898 fixup_called = 1;
18899 }
18900
18901 /* Read an attribute value described by an attribute form. */
18902
18903 static const gdb_byte *
18904 read_attribute_value (const struct die_reader_specs *reader,
18905 struct attribute *attr, unsigned form,
18906 LONGEST implicit_const, const gdb_byte *info_ptr)
18907 {
18908 struct dwarf2_cu *cu = reader->cu;
18909 struct dwarf2_per_objfile *dwarf2_per_objfile
18910 = cu->per_cu->dwarf2_per_objfile;
18911 struct objfile *objfile = dwarf2_per_objfile->objfile;
18912 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18913 bfd *abfd = reader->abfd;
18914 struct comp_unit_head *cu_header = &cu->header;
18915 unsigned int bytes_read;
18916 struct dwarf_block *blk;
18917
18918 attr->form = (enum dwarf_form) form;
18919 switch (form)
18920 {
18921 case DW_FORM_ref_addr:
18922 if (cu->header.version == 2)
18923 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18924 else
18925 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18926 &cu->header, &bytes_read);
18927 info_ptr += bytes_read;
18928 break;
18929 case DW_FORM_GNU_ref_alt:
18930 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18931 info_ptr += bytes_read;
18932 break;
18933 case DW_FORM_addr:
18934 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18935 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18936 info_ptr += bytes_read;
18937 break;
18938 case DW_FORM_block2:
18939 blk = dwarf_alloc_block (cu);
18940 blk->size = read_2_bytes (abfd, info_ptr);
18941 info_ptr += 2;
18942 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18943 info_ptr += blk->size;
18944 DW_BLOCK (attr) = blk;
18945 break;
18946 case DW_FORM_block4:
18947 blk = dwarf_alloc_block (cu);
18948 blk->size = read_4_bytes (abfd, info_ptr);
18949 info_ptr += 4;
18950 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18951 info_ptr += blk->size;
18952 DW_BLOCK (attr) = blk;
18953 break;
18954 case DW_FORM_data2:
18955 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18956 info_ptr += 2;
18957 break;
18958 case DW_FORM_data4:
18959 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18960 info_ptr += 4;
18961 break;
18962 case DW_FORM_data8:
18963 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18964 info_ptr += 8;
18965 break;
18966 case DW_FORM_data16:
18967 blk = dwarf_alloc_block (cu);
18968 blk->size = 16;
18969 blk->data = read_n_bytes (abfd, info_ptr, 16);
18970 info_ptr += 16;
18971 DW_BLOCK (attr) = blk;
18972 break;
18973 case DW_FORM_sec_offset:
18974 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18975 info_ptr += bytes_read;
18976 break;
18977 case DW_FORM_string:
18978 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18979 DW_STRING_IS_CANONICAL (attr) = 0;
18980 info_ptr += bytes_read;
18981 break;
18982 case DW_FORM_strp:
18983 if (!cu->per_cu->is_dwz)
18984 {
18985 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18986 abfd, info_ptr, cu_header,
18987 &bytes_read);
18988 DW_STRING_IS_CANONICAL (attr) = 0;
18989 info_ptr += bytes_read;
18990 break;
18991 }
18992 /* FALLTHROUGH */
18993 case DW_FORM_line_strp:
18994 if (!cu->per_cu->is_dwz)
18995 {
18996 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18997 abfd, info_ptr,
18998 cu_header, &bytes_read);
18999 DW_STRING_IS_CANONICAL (attr) = 0;
19000 info_ptr += bytes_read;
19001 break;
19002 }
19003 /* FALLTHROUGH */
19004 case DW_FORM_GNU_strp_alt:
19005 {
19006 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19007 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19008 &bytes_read);
19009
19010 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19011 dwz, str_offset);
19012 DW_STRING_IS_CANONICAL (attr) = 0;
19013 info_ptr += bytes_read;
19014 }
19015 break;
19016 case DW_FORM_exprloc:
19017 case DW_FORM_block:
19018 blk = dwarf_alloc_block (cu);
19019 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19020 info_ptr += bytes_read;
19021 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19022 info_ptr += blk->size;
19023 DW_BLOCK (attr) = blk;
19024 break;
19025 case DW_FORM_block1:
19026 blk = dwarf_alloc_block (cu);
19027 blk->size = read_1_byte (abfd, info_ptr);
19028 info_ptr += 1;
19029 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19030 info_ptr += blk->size;
19031 DW_BLOCK (attr) = blk;
19032 break;
19033 case DW_FORM_data1:
19034 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19035 info_ptr += 1;
19036 break;
19037 case DW_FORM_flag:
19038 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19039 info_ptr += 1;
19040 break;
19041 case DW_FORM_flag_present:
19042 DW_UNSND (attr) = 1;
19043 break;
19044 case DW_FORM_sdata:
19045 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19046 info_ptr += bytes_read;
19047 break;
19048 case DW_FORM_udata:
19049 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19050 info_ptr += bytes_read;
19051 break;
19052 case DW_FORM_ref1:
19053 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19054 + read_1_byte (abfd, info_ptr));
19055 info_ptr += 1;
19056 break;
19057 case DW_FORM_ref2:
19058 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19059 + read_2_bytes (abfd, info_ptr));
19060 info_ptr += 2;
19061 break;
19062 case DW_FORM_ref4:
19063 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19064 + read_4_bytes (abfd, info_ptr));
19065 info_ptr += 4;
19066 break;
19067 case DW_FORM_ref8:
19068 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19069 + read_8_bytes (abfd, info_ptr));
19070 info_ptr += 8;
19071 break;
19072 case DW_FORM_ref_sig8:
19073 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19074 info_ptr += 8;
19075 break;
19076 case DW_FORM_ref_udata:
19077 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19078 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19079 info_ptr += bytes_read;
19080 break;
19081 case DW_FORM_indirect:
19082 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19083 info_ptr += bytes_read;
19084 if (form == DW_FORM_implicit_const)
19085 {
19086 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19087 info_ptr += bytes_read;
19088 }
19089 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19090 info_ptr);
19091 break;
19092 case DW_FORM_implicit_const:
19093 DW_SND (attr) = implicit_const;
19094 break;
19095 case DW_FORM_GNU_addr_index:
19096 if (reader->dwo_file == NULL)
19097 {
19098 /* For now flag a hard error.
19099 Later we can turn this into a complaint. */
19100 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19101 dwarf_form_name (form),
19102 bfd_get_filename (abfd));
19103 }
19104 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19105 info_ptr += bytes_read;
19106 break;
19107 case DW_FORM_GNU_str_index:
19108 if (reader->dwo_file == NULL)
19109 {
19110 /* For now flag a hard error.
19111 Later we can turn this into a complaint if warranted. */
19112 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19113 dwarf_form_name (form),
19114 bfd_get_filename (abfd));
19115 }
19116 {
19117 ULONGEST str_index =
19118 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19119
19120 DW_STRING (attr) = read_str_index (reader, str_index);
19121 DW_STRING_IS_CANONICAL (attr) = 0;
19122 info_ptr += bytes_read;
19123 }
19124 break;
19125 default:
19126 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19127 dwarf_form_name (form),
19128 bfd_get_filename (abfd));
19129 }
19130
19131 /* Super hack. */
19132 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19133 attr->form = DW_FORM_GNU_ref_alt;
19134
19135 /* We have seen instances where the compiler tried to emit a byte
19136 size attribute of -1 which ended up being encoded as an unsigned
19137 0xffffffff. Although 0xffffffff is technically a valid size value,
19138 an object of this size seems pretty unlikely so we can relatively
19139 safely treat these cases as if the size attribute was invalid and
19140 treat them as zero by default. */
19141 if (attr->name == DW_AT_byte_size
19142 && form == DW_FORM_data4
19143 && DW_UNSND (attr) >= 0xffffffff)
19144 {
19145 complaint
19146 (&symfile_complaints,
19147 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19148 hex_string (DW_UNSND (attr)));
19149 DW_UNSND (attr) = 0;
19150 }
19151
19152 return info_ptr;
19153 }
19154
19155 /* Read an attribute described by an abbreviated attribute. */
19156
19157 static const gdb_byte *
19158 read_attribute (const struct die_reader_specs *reader,
19159 struct attribute *attr, struct attr_abbrev *abbrev,
19160 const gdb_byte *info_ptr)
19161 {
19162 attr->name = abbrev->name;
19163 return read_attribute_value (reader, attr, abbrev->form,
19164 abbrev->implicit_const, info_ptr);
19165 }
19166
19167 /* Read dwarf information from a buffer. */
19168
19169 static unsigned int
19170 read_1_byte (bfd *abfd, const gdb_byte *buf)
19171 {
19172 return bfd_get_8 (abfd, buf);
19173 }
19174
19175 static int
19176 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19177 {
19178 return bfd_get_signed_8 (abfd, buf);
19179 }
19180
19181 static unsigned int
19182 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19183 {
19184 return bfd_get_16 (abfd, buf);
19185 }
19186
19187 static int
19188 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19189 {
19190 return bfd_get_signed_16 (abfd, buf);
19191 }
19192
19193 static unsigned int
19194 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19195 {
19196 return bfd_get_32 (abfd, buf);
19197 }
19198
19199 static int
19200 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19201 {
19202 return bfd_get_signed_32 (abfd, buf);
19203 }
19204
19205 static ULONGEST
19206 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19207 {
19208 return bfd_get_64 (abfd, buf);
19209 }
19210
19211 static CORE_ADDR
19212 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19213 unsigned int *bytes_read)
19214 {
19215 struct comp_unit_head *cu_header = &cu->header;
19216 CORE_ADDR retval = 0;
19217
19218 if (cu_header->signed_addr_p)
19219 {
19220 switch (cu_header->addr_size)
19221 {
19222 case 2:
19223 retval = bfd_get_signed_16 (abfd, buf);
19224 break;
19225 case 4:
19226 retval = bfd_get_signed_32 (abfd, buf);
19227 break;
19228 case 8:
19229 retval = bfd_get_signed_64 (abfd, buf);
19230 break;
19231 default:
19232 internal_error (__FILE__, __LINE__,
19233 _("read_address: bad switch, signed [in module %s]"),
19234 bfd_get_filename (abfd));
19235 }
19236 }
19237 else
19238 {
19239 switch (cu_header->addr_size)
19240 {
19241 case 2:
19242 retval = bfd_get_16 (abfd, buf);
19243 break;
19244 case 4:
19245 retval = bfd_get_32 (abfd, buf);
19246 break;
19247 case 8:
19248 retval = bfd_get_64 (abfd, buf);
19249 break;
19250 default:
19251 internal_error (__FILE__, __LINE__,
19252 _("read_address: bad switch, "
19253 "unsigned [in module %s]"),
19254 bfd_get_filename (abfd));
19255 }
19256 }
19257
19258 *bytes_read = cu_header->addr_size;
19259 return retval;
19260 }
19261
19262 /* Read the initial length from a section. The (draft) DWARF 3
19263 specification allows the initial length to take up either 4 bytes
19264 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19265 bytes describe the length and all offsets will be 8 bytes in length
19266 instead of 4.
19267
19268 An older, non-standard 64-bit format is also handled by this
19269 function. The older format in question stores the initial length
19270 as an 8-byte quantity without an escape value. Lengths greater
19271 than 2^32 aren't very common which means that the initial 4 bytes
19272 is almost always zero. Since a length value of zero doesn't make
19273 sense for the 32-bit format, this initial zero can be considered to
19274 be an escape value which indicates the presence of the older 64-bit
19275 format. As written, the code can't detect (old format) lengths
19276 greater than 4GB. If it becomes necessary to handle lengths
19277 somewhat larger than 4GB, we could allow other small values (such
19278 as the non-sensical values of 1, 2, and 3) to also be used as
19279 escape values indicating the presence of the old format.
19280
19281 The value returned via bytes_read should be used to increment the
19282 relevant pointer after calling read_initial_length().
19283
19284 [ Note: read_initial_length() and read_offset() are based on the
19285 document entitled "DWARF Debugging Information Format", revision
19286 3, draft 8, dated November 19, 2001. This document was obtained
19287 from:
19288
19289 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19290
19291 This document is only a draft and is subject to change. (So beware.)
19292
19293 Details regarding the older, non-standard 64-bit format were
19294 determined empirically by examining 64-bit ELF files produced by
19295 the SGI toolchain on an IRIX 6.5 machine.
19296
19297 - Kevin, July 16, 2002
19298 ] */
19299
19300 static LONGEST
19301 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19302 {
19303 LONGEST length = bfd_get_32 (abfd, buf);
19304
19305 if (length == 0xffffffff)
19306 {
19307 length = bfd_get_64 (abfd, buf + 4);
19308 *bytes_read = 12;
19309 }
19310 else if (length == 0)
19311 {
19312 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19313 length = bfd_get_64 (abfd, buf);
19314 *bytes_read = 8;
19315 }
19316 else
19317 {
19318 *bytes_read = 4;
19319 }
19320
19321 return length;
19322 }
19323
19324 /* Cover function for read_initial_length.
19325 Returns the length of the object at BUF, and stores the size of the
19326 initial length in *BYTES_READ and stores the size that offsets will be in
19327 *OFFSET_SIZE.
19328 If the initial length size is not equivalent to that specified in
19329 CU_HEADER then issue a complaint.
19330 This is useful when reading non-comp-unit headers. */
19331
19332 static LONGEST
19333 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19334 const struct comp_unit_head *cu_header,
19335 unsigned int *bytes_read,
19336 unsigned int *offset_size)
19337 {
19338 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19339
19340 gdb_assert (cu_header->initial_length_size == 4
19341 || cu_header->initial_length_size == 8
19342 || cu_header->initial_length_size == 12);
19343
19344 if (cu_header->initial_length_size != *bytes_read)
19345 complaint (&symfile_complaints,
19346 _("intermixed 32-bit and 64-bit DWARF sections"));
19347
19348 *offset_size = (*bytes_read == 4) ? 4 : 8;
19349 return length;
19350 }
19351
19352 /* Read an offset from the data stream. The size of the offset is
19353 given by cu_header->offset_size. */
19354
19355 static LONGEST
19356 read_offset (bfd *abfd, const gdb_byte *buf,
19357 const struct comp_unit_head *cu_header,
19358 unsigned int *bytes_read)
19359 {
19360 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19361
19362 *bytes_read = cu_header->offset_size;
19363 return offset;
19364 }
19365
19366 /* Read an offset from the data stream. */
19367
19368 static LONGEST
19369 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19370 {
19371 LONGEST retval = 0;
19372
19373 switch (offset_size)
19374 {
19375 case 4:
19376 retval = bfd_get_32 (abfd, buf);
19377 break;
19378 case 8:
19379 retval = bfd_get_64 (abfd, buf);
19380 break;
19381 default:
19382 internal_error (__FILE__, __LINE__,
19383 _("read_offset_1: bad switch [in module %s]"),
19384 bfd_get_filename (abfd));
19385 }
19386
19387 return retval;
19388 }
19389
19390 static const gdb_byte *
19391 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19392 {
19393 /* If the size of a host char is 8 bits, we can return a pointer
19394 to the buffer, otherwise we have to copy the data to a buffer
19395 allocated on the temporary obstack. */
19396 gdb_assert (HOST_CHAR_BIT == 8);
19397 return buf;
19398 }
19399
19400 static const char *
19401 read_direct_string (bfd *abfd, const gdb_byte *buf,
19402 unsigned int *bytes_read_ptr)
19403 {
19404 /* If the size of a host char is 8 bits, we can return a pointer
19405 to the string, otherwise we have to copy the string to a buffer
19406 allocated on the temporary obstack. */
19407 gdb_assert (HOST_CHAR_BIT == 8);
19408 if (*buf == '\0')
19409 {
19410 *bytes_read_ptr = 1;
19411 return NULL;
19412 }
19413 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19414 return (const char *) buf;
19415 }
19416
19417 /* Return pointer to string at section SECT offset STR_OFFSET with error
19418 reporting strings FORM_NAME and SECT_NAME. */
19419
19420 static const char *
19421 read_indirect_string_at_offset_from (struct objfile *objfile,
19422 bfd *abfd, LONGEST str_offset,
19423 struct dwarf2_section_info *sect,
19424 const char *form_name,
19425 const char *sect_name)
19426 {
19427 dwarf2_read_section (objfile, sect);
19428 if (sect->buffer == NULL)
19429 error (_("%s used without %s section [in module %s]"),
19430 form_name, sect_name, bfd_get_filename (abfd));
19431 if (str_offset >= sect->size)
19432 error (_("%s pointing outside of %s section [in module %s]"),
19433 form_name, sect_name, bfd_get_filename (abfd));
19434 gdb_assert (HOST_CHAR_BIT == 8);
19435 if (sect->buffer[str_offset] == '\0')
19436 return NULL;
19437 return (const char *) (sect->buffer + str_offset);
19438 }
19439
19440 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19441
19442 static const char *
19443 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19444 bfd *abfd, LONGEST str_offset)
19445 {
19446 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19447 abfd, str_offset,
19448 &dwarf2_per_objfile->str,
19449 "DW_FORM_strp", ".debug_str");
19450 }
19451
19452 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19453
19454 static const char *
19455 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19456 bfd *abfd, LONGEST str_offset)
19457 {
19458 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19459 abfd, str_offset,
19460 &dwarf2_per_objfile->line_str,
19461 "DW_FORM_line_strp",
19462 ".debug_line_str");
19463 }
19464
19465 /* Read a string at offset STR_OFFSET in the .debug_str section from
19466 the .dwz file DWZ. Throw an error if the offset is too large. If
19467 the string consists of a single NUL byte, return NULL; otherwise
19468 return a pointer to the string. */
19469
19470 static const char *
19471 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19472 LONGEST str_offset)
19473 {
19474 dwarf2_read_section (objfile, &dwz->str);
19475
19476 if (dwz->str.buffer == NULL)
19477 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19478 "section [in module %s]"),
19479 bfd_get_filename (dwz->dwz_bfd));
19480 if (str_offset >= dwz->str.size)
19481 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19482 ".debug_str section [in module %s]"),
19483 bfd_get_filename (dwz->dwz_bfd));
19484 gdb_assert (HOST_CHAR_BIT == 8);
19485 if (dwz->str.buffer[str_offset] == '\0')
19486 return NULL;
19487 return (const char *) (dwz->str.buffer + str_offset);
19488 }
19489
19490 /* Return pointer to string at .debug_str offset as read from BUF.
19491 BUF is assumed to be in a compilation unit described by CU_HEADER.
19492 Return *BYTES_READ_PTR count of bytes read from BUF. */
19493
19494 static const char *
19495 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19496 const gdb_byte *buf,
19497 const struct comp_unit_head *cu_header,
19498 unsigned int *bytes_read_ptr)
19499 {
19500 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19501
19502 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19503 }
19504
19505 /* Return pointer to string at .debug_line_str offset as read from BUF.
19506 BUF is assumed to be in a compilation unit described by CU_HEADER.
19507 Return *BYTES_READ_PTR count of bytes read from BUF. */
19508
19509 static const char *
19510 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19511 bfd *abfd, const gdb_byte *buf,
19512 const struct comp_unit_head *cu_header,
19513 unsigned int *bytes_read_ptr)
19514 {
19515 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19516
19517 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19518 str_offset);
19519 }
19520
19521 ULONGEST
19522 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19523 unsigned int *bytes_read_ptr)
19524 {
19525 ULONGEST result;
19526 unsigned int num_read;
19527 int shift;
19528 unsigned char byte;
19529
19530 result = 0;
19531 shift = 0;
19532 num_read = 0;
19533 while (1)
19534 {
19535 byte = bfd_get_8 (abfd, buf);
19536 buf++;
19537 num_read++;
19538 result |= ((ULONGEST) (byte & 127) << shift);
19539 if ((byte & 128) == 0)
19540 {
19541 break;
19542 }
19543 shift += 7;
19544 }
19545 *bytes_read_ptr = num_read;
19546 return result;
19547 }
19548
19549 static LONGEST
19550 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19551 unsigned int *bytes_read_ptr)
19552 {
19553 LONGEST result;
19554 int shift, num_read;
19555 unsigned char byte;
19556
19557 result = 0;
19558 shift = 0;
19559 num_read = 0;
19560 while (1)
19561 {
19562 byte = bfd_get_8 (abfd, buf);
19563 buf++;
19564 num_read++;
19565 result |= ((LONGEST) (byte & 127) << shift);
19566 shift += 7;
19567 if ((byte & 128) == 0)
19568 {
19569 break;
19570 }
19571 }
19572 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19573 result |= -(((LONGEST) 1) << shift);
19574 *bytes_read_ptr = num_read;
19575 return result;
19576 }
19577
19578 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19579 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19580 ADDR_SIZE is the size of addresses from the CU header. */
19581
19582 static CORE_ADDR
19583 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19584 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19585 {
19586 struct objfile *objfile = dwarf2_per_objfile->objfile;
19587 bfd *abfd = objfile->obfd;
19588 const gdb_byte *info_ptr;
19589
19590 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19591 if (dwarf2_per_objfile->addr.buffer == NULL)
19592 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19593 objfile_name (objfile));
19594 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19595 error (_("DW_FORM_addr_index pointing outside of "
19596 ".debug_addr section [in module %s]"),
19597 objfile_name (objfile));
19598 info_ptr = (dwarf2_per_objfile->addr.buffer
19599 + addr_base + addr_index * addr_size);
19600 if (addr_size == 4)
19601 return bfd_get_32 (abfd, info_ptr);
19602 else
19603 return bfd_get_64 (abfd, info_ptr);
19604 }
19605
19606 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19607
19608 static CORE_ADDR
19609 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19610 {
19611 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19612 cu->addr_base, cu->header.addr_size);
19613 }
19614
19615 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19616
19617 static CORE_ADDR
19618 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19619 unsigned int *bytes_read)
19620 {
19621 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19622 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19623
19624 return read_addr_index (cu, addr_index);
19625 }
19626
19627 /* Data structure to pass results from dwarf2_read_addr_index_reader
19628 back to dwarf2_read_addr_index. */
19629
19630 struct dwarf2_read_addr_index_data
19631 {
19632 ULONGEST addr_base;
19633 int addr_size;
19634 };
19635
19636 /* die_reader_func for dwarf2_read_addr_index. */
19637
19638 static void
19639 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19640 const gdb_byte *info_ptr,
19641 struct die_info *comp_unit_die,
19642 int has_children,
19643 void *data)
19644 {
19645 struct dwarf2_cu *cu = reader->cu;
19646 struct dwarf2_read_addr_index_data *aidata =
19647 (struct dwarf2_read_addr_index_data *) data;
19648
19649 aidata->addr_base = cu->addr_base;
19650 aidata->addr_size = cu->header.addr_size;
19651 }
19652
19653 /* Given an index in .debug_addr, fetch the value.
19654 NOTE: This can be called during dwarf expression evaluation,
19655 long after the debug information has been read, and thus per_cu->cu
19656 may no longer exist. */
19657
19658 CORE_ADDR
19659 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19660 unsigned int addr_index)
19661 {
19662 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19663 struct dwarf2_cu *cu = per_cu->cu;
19664 ULONGEST addr_base;
19665 int addr_size;
19666
19667 /* We need addr_base and addr_size.
19668 If we don't have PER_CU->cu, we have to get it.
19669 Nasty, but the alternative is storing the needed info in PER_CU,
19670 which at this point doesn't seem justified: it's not clear how frequently
19671 it would get used and it would increase the size of every PER_CU.
19672 Entry points like dwarf2_per_cu_addr_size do a similar thing
19673 so we're not in uncharted territory here.
19674 Alas we need to be a bit more complicated as addr_base is contained
19675 in the DIE.
19676
19677 We don't need to read the entire CU(/TU).
19678 We just need the header and top level die.
19679
19680 IWBN to use the aging mechanism to let us lazily later discard the CU.
19681 For now we skip this optimization. */
19682
19683 if (cu != NULL)
19684 {
19685 addr_base = cu->addr_base;
19686 addr_size = cu->header.addr_size;
19687 }
19688 else
19689 {
19690 struct dwarf2_read_addr_index_data aidata;
19691
19692 /* Note: We can't use init_cutu_and_read_dies_simple here,
19693 we need addr_base. */
19694 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
19695 dwarf2_read_addr_index_reader, &aidata);
19696 addr_base = aidata.addr_base;
19697 addr_size = aidata.addr_size;
19698 }
19699
19700 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19701 addr_size);
19702 }
19703
19704 /* Given a DW_FORM_GNU_str_index, fetch the string.
19705 This is only used by the Fission support. */
19706
19707 static const char *
19708 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19709 {
19710 struct dwarf2_cu *cu = reader->cu;
19711 struct dwarf2_per_objfile *dwarf2_per_objfile
19712 = cu->per_cu->dwarf2_per_objfile;
19713 struct objfile *objfile = dwarf2_per_objfile->objfile;
19714 const char *objf_name = objfile_name (objfile);
19715 bfd *abfd = objfile->obfd;
19716 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19717 struct dwarf2_section_info *str_offsets_section =
19718 &reader->dwo_file->sections.str_offsets;
19719 const gdb_byte *info_ptr;
19720 ULONGEST str_offset;
19721 static const char form_name[] = "DW_FORM_GNU_str_index";
19722
19723 dwarf2_read_section (objfile, str_section);
19724 dwarf2_read_section (objfile, str_offsets_section);
19725 if (str_section->buffer == NULL)
19726 error (_("%s used without .debug_str.dwo section"
19727 " in CU at offset %s [in module %s]"),
19728 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19729 if (str_offsets_section->buffer == NULL)
19730 error (_("%s used without .debug_str_offsets.dwo section"
19731 " in CU at offset %s [in module %s]"),
19732 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19733 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19734 error (_("%s pointing outside of .debug_str_offsets.dwo"
19735 " section in CU at offset %s [in module %s]"),
19736 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19737 info_ptr = (str_offsets_section->buffer
19738 + str_index * cu->header.offset_size);
19739 if (cu->header.offset_size == 4)
19740 str_offset = bfd_get_32 (abfd, info_ptr);
19741 else
19742 str_offset = bfd_get_64 (abfd, info_ptr);
19743 if (str_offset >= str_section->size)
19744 error (_("Offset from %s pointing outside of"
19745 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19746 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19747 return (const char *) (str_section->buffer + str_offset);
19748 }
19749
19750 /* Return the length of an LEB128 number in BUF. */
19751
19752 static int
19753 leb128_size (const gdb_byte *buf)
19754 {
19755 const gdb_byte *begin = buf;
19756 gdb_byte byte;
19757
19758 while (1)
19759 {
19760 byte = *buf++;
19761 if ((byte & 128) == 0)
19762 return buf - begin;
19763 }
19764 }
19765
19766 static void
19767 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19768 {
19769 switch (lang)
19770 {
19771 case DW_LANG_C89:
19772 case DW_LANG_C99:
19773 case DW_LANG_C11:
19774 case DW_LANG_C:
19775 case DW_LANG_UPC:
19776 cu->language = language_c;
19777 break;
19778 case DW_LANG_Java:
19779 case DW_LANG_C_plus_plus:
19780 case DW_LANG_C_plus_plus_11:
19781 case DW_LANG_C_plus_plus_14:
19782 cu->language = language_cplus;
19783 break;
19784 case DW_LANG_D:
19785 cu->language = language_d;
19786 break;
19787 case DW_LANG_Fortran77:
19788 case DW_LANG_Fortran90:
19789 case DW_LANG_Fortran95:
19790 case DW_LANG_Fortran03:
19791 case DW_LANG_Fortran08:
19792 cu->language = language_fortran;
19793 break;
19794 case DW_LANG_Go:
19795 cu->language = language_go;
19796 break;
19797 case DW_LANG_Mips_Assembler:
19798 cu->language = language_asm;
19799 break;
19800 case DW_LANG_Ada83:
19801 case DW_LANG_Ada95:
19802 cu->language = language_ada;
19803 break;
19804 case DW_LANG_Modula2:
19805 cu->language = language_m2;
19806 break;
19807 case DW_LANG_Pascal83:
19808 cu->language = language_pascal;
19809 break;
19810 case DW_LANG_ObjC:
19811 cu->language = language_objc;
19812 break;
19813 case DW_LANG_Rust:
19814 case DW_LANG_Rust_old:
19815 cu->language = language_rust;
19816 break;
19817 case DW_LANG_Cobol74:
19818 case DW_LANG_Cobol85:
19819 default:
19820 cu->language = language_minimal;
19821 break;
19822 }
19823 cu->language_defn = language_def (cu->language);
19824 }
19825
19826 /* Return the named attribute or NULL if not there. */
19827
19828 static struct attribute *
19829 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19830 {
19831 for (;;)
19832 {
19833 unsigned int i;
19834 struct attribute *spec = NULL;
19835
19836 for (i = 0; i < die->num_attrs; ++i)
19837 {
19838 if (die->attrs[i].name == name)
19839 return &die->attrs[i];
19840 if (die->attrs[i].name == DW_AT_specification
19841 || die->attrs[i].name == DW_AT_abstract_origin)
19842 spec = &die->attrs[i];
19843 }
19844
19845 if (!spec)
19846 break;
19847
19848 die = follow_die_ref (die, spec, &cu);
19849 }
19850
19851 return NULL;
19852 }
19853
19854 /* Return the named attribute or NULL if not there,
19855 but do not follow DW_AT_specification, etc.
19856 This is for use in contexts where we're reading .debug_types dies.
19857 Following DW_AT_specification, DW_AT_abstract_origin will take us
19858 back up the chain, and we want to go down. */
19859
19860 static struct attribute *
19861 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19862 {
19863 unsigned int i;
19864
19865 for (i = 0; i < die->num_attrs; ++i)
19866 if (die->attrs[i].name == name)
19867 return &die->attrs[i];
19868
19869 return NULL;
19870 }
19871
19872 /* Return the string associated with a string-typed attribute, or NULL if it
19873 is either not found or is of an incorrect type. */
19874
19875 static const char *
19876 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19877 {
19878 struct attribute *attr;
19879 const char *str = NULL;
19880
19881 attr = dwarf2_attr (die, name, cu);
19882
19883 if (attr != NULL)
19884 {
19885 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19886 || attr->form == DW_FORM_string
19887 || attr->form == DW_FORM_GNU_str_index
19888 || attr->form == DW_FORM_GNU_strp_alt)
19889 str = DW_STRING (attr);
19890 else
19891 complaint (&symfile_complaints,
19892 _("string type expected for attribute %s for "
19893 "DIE at %s in module %s"),
19894 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19895 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19896 }
19897
19898 return str;
19899 }
19900
19901 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19902 and holds a non-zero value. This function should only be used for
19903 DW_FORM_flag or DW_FORM_flag_present attributes. */
19904
19905 static int
19906 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19907 {
19908 struct attribute *attr = dwarf2_attr (die, name, cu);
19909
19910 return (attr && DW_UNSND (attr));
19911 }
19912
19913 static int
19914 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19915 {
19916 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19917 which value is non-zero. However, we have to be careful with
19918 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19919 (via dwarf2_flag_true_p) follows this attribute. So we may
19920 end up accidently finding a declaration attribute that belongs
19921 to a different DIE referenced by the specification attribute,
19922 even though the given DIE does not have a declaration attribute. */
19923 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19924 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19925 }
19926
19927 /* Return the die giving the specification for DIE, if there is
19928 one. *SPEC_CU is the CU containing DIE on input, and the CU
19929 containing the return value on output. If there is no
19930 specification, but there is an abstract origin, that is
19931 returned. */
19932
19933 static struct die_info *
19934 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19935 {
19936 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19937 *spec_cu);
19938
19939 if (spec_attr == NULL)
19940 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19941
19942 if (spec_attr == NULL)
19943 return NULL;
19944 else
19945 return follow_die_ref (die, spec_attr, spec_cu);
19946 }
19947
19948 /* Stub for free_line_header to match void * callback types. */
19949
19950 static void
19951 free_line_header_voidp (void *arg)
19952 {
19953 struct line_header *lh = (struct line_header *) arg;
19954
19955 delete lh;
19956 }
19957
19958 void
19959 line_header::add_include_dir (const char *include_dir)
19960 {
19961 if (dwarf_line_debug >= 2)
19962 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19963 include_dirs.size () + 1, include_dir);
19964
19965 include_dirs.push_back (include_dir);
19966 }
19967
19968 void
19969 line_header::add_file_name (const char *name,
19970 dir_index d_index,
19971 unsigned int mod_time,
19972 unsigned int length)
19973 {
19974 if (dwarf_line_debug >= 2)
19975 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19976 (unsigned) file_names.size () + 1, name);
19977
19978 file_names.emplace_back (name, d_index, mod_time, length);
19979 }
19980
19981 /* A convenience function to find the proper .debug_line section for a CU. */
19982
19983 static struct dwarf2_section_info *
19984 get_debug_line_section (struct dwarf2_cu *cu)
19985 {
19986 struct dwarf2_section_info *section;
19987 struct dwarf2_per_objfile *dwarf2_per_objfile
19988 = cu->per_cu->dwarf2_per_objfile;
19989
19990 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19991 DWO file. */
19992 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19993 section = &cu->dwo_unit->dwo_file->sections.line;
19994 else if (cu->per_cu->is_dwz)
19995 {
19996 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19997
19998 section = &dwz->line;
19999 }
20000 else
20001 section = &dwarf2_per_objfile->line;
20002
20003 return section;
20004 }
20005
20006 /* Read directory or file name entry format, starting with byte of
20007 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20008 entries count and the entries themselves in the described entry
20009 format. */
20010
20011 static void
20012 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20013 bfd *abfd, const gdb_byte **bufp,
20014 struct line_header *lh,
20015 const struct comp_unit_head *cu_header,
20016 void (*callback) (struct line_header *lh,
20017 const char *name,
20018 dir_index d_index,
20019 unsigned int mod_time,
20020 unsigned int length))
20021 {
20022 gdb_byte format_count, formati;
20023 ULONGEST data_count, datai;
20024 const gdb_byte *buf = *bufp;
20025 const gdb_byte *format_header_data;
20026 unsigned int bytes_read;
20027
20028 format_count = read_1_byte (abfd, buf);
20029 buf += 1;
20030 format_header_data = buf;
20031 for (formati = 0; formati < format_count; formati++)
20032 {
20033 read_unsigned_leb128 (abfd, buf, &bytes_read);
20034 buf += bytes_read;
20035 read_unsigned_leb128 (abfd, buf, &bytes_read);
20036 buf += bytes_read;
20037 }
20038
20039 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20040 buf += bytes_read;
20041 for (datai = 0; datai < data_count; datai++)
20042 {
20043 const gdb_byte *format = format_header_data;
20044 struct file_entry fe;
20045
20046 for (formati = 0; formati < format_count; formati++)
20047 {
20048 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20049 format += bytes_read;
20050
20051 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20052 format += bytes_read;
20053
20054 gdb::optional<const char *> string;
20055 gdb::optional<unsigned int> uint;
20056
20057 switch (form)
20058 {
20059 case DW_FORM_string:
20060 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20061 buf += bytes_read;
20062 break;
20063
20064 case DW_FORM_line_strp:
20065 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20066 abfd, buf,
20067 cu_header,
20068 &bytes_read));
20069 buf += bytes_read;
20070 break;
20071
20072 case DW_FORM_data1:
20073 uint.emplace (read_1_byte (abfd, buf));
20074 buf += 1;
20075 break;
20076
20077 case DW_FORM_data2:
20078 uint.emplace (read_2_bytes (abfd, buf));
20079 buf += 2;
20080 break;
20081
20082 case DW_FORM_data4:
20083 uint.emplace (read_4_bytes (abfd, buf));
20084 buf += 4;
20085 break;
20086
20087 case DW_FORM_data8:
20088 uint.emplace (read_8_bytes (abfd, buf));
20089 buf += 8;
20090 break;
20091
20092 case DW_FORM_udata:
20093 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20094 buf += bytes_read;
20095 break;
20096
20097 case DW_FORM_block:
20098 /* It is valid only for DW_LNCT_timestamp which is ignored by
20099 current GDB. */
20100 break;
20101 }
20102
20103 switch (content_type)
20104 {
20105 case DW_LNCT_path:
20106 if (string.has_value ())
20107 fe.name = *string;
20108 break;
20109 case DW_LNCT_directory_index:
20110 if (uint.has_value ())
20111 fe.d_index = (dir_index) *uint;
20112 break;
20113 case DW_LNCT_timestamp:
20114 if (uint.has_value ())
20115 fe.mod_time = *uint;
20116 break;
20117 case DW_LNCT_size:
20118 if (uint.has_value ())
20119 fe.length = *uint;
20120 break;
20121 case DW_LNCT_MD5:
20122 break;
20123 default:
20124 complaint (&symfile_complaints,
20125 _("Unknown format content type %s"),
20126 pulongest (content_type));
20127 }
20128 }
20129
20130 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20131 }
20132
20133 *bufp = buf;
20134 }
20135
20136 /* Read the statement program header starting at OFFSET in
20137 .debug_line, or .debug_line.dwo. Return a pointer
20138 to a struct line_header, allocated using xmalloc.
20139 Returns NULL if there is a problem reading the header, e.g., if it
20140 has a version we don't understand.
20141
20142 NOTE: the strings in the include directory and file name tables of
20143 the returned object point into the dwarf line section buffer,
20144 and must not be freed. */
20145
20146 static line_header_up
20147 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20148 {
20149 const gdb_byte *line_ptr;
20150 unsigned int bytes_read, offset_size;
20151 int i;
20152 const char *cur_dir, *cur_file;
20153 struct dwarf2_section_info *section;
20154 bfd *abfd;
20155 struct dwarf2_per_objfile *dwarf2_per_objfile
20156 = cu->per_cu->dwarf2_per_objfile;
20157
20158 section = get_debug_line_section (cu);
20159 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20160 if (section->buffer == NULL)
20161 {
20162 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20163 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20164 else
20165 complaint (&symfile_complaints, _("missing .debug_line section"));
20166 return 0;
20167 }
20168
20169 /* We can't do this until we know the section is non-empty.
20170 Only then do we know we have such a section. */
20171 abfd = get_section_bfd_owner (section);
20172
20173 /* Make sure that at least there's room for the total_length field.
20174 That could be 12 bytes long, but we're just going to fudge that. */
20175 if (to_underlying (sect_off) + 4 >= section->size)
20176 {
20177 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20178 return 0;
20179 }
20180
20181 line_header_up lh (new line_header ());
20182
20183 lh->sect_off = sect_off;
20184 lh->offset_in_dwz = cu->per_cu->is_dwz;
20185
20186 line_ptr = section->buffer + to_underlying (sect_off);
20187
20188 /* Read in the header. */
20189 lh->total_length =
20190 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20191 &bytes_read, &offset_size);
20192 line_ptr += bytes_read;
20193 if (line_ptr + lh->total_length > (section->buffer + section->size))
20194 {
20195 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20196 return 0;
20197 }
20198 lh->statement_program_end = line_ptr + lh->total_length;
20199 lh->version = read_2_bytes (abfd, line_ptr);
20200 line_ptr += 2;
20201 if (lh->version > 5)
20202 {
20203 /* This is a version we don't understand. The format could have
20204 changed in ways we don't handle properly so just punt. */
20205 complaint (&symfile_complaints,
20206 _("unsupported version in .debug_line section"));
20207 return NULL;
20208 }
20209 if (lh->version >= 5)
20210 {
20211 gdb_byte segment_selector_size;
20212
20213 /* Skip address size. */
20214 read_1_byte (abfd, line_ptr);
20215 line_ptr += 1;
20216
20217 segment_selector_size = read_1_byte (abfd, line_ptr);
20218 line_ptr += 1;
20219 if (segment_selector_size != 0)
20220 {
20221 complaint (&symfile_complaints,
20222 _("unsupported segment selector size %u "
20223 "in .debug_line section"),
20224 segment_selector_size);
20225 return NULL;
20226 }
20227 }
20228 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20229 line_ptr += offset_size;
20230 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20231 line_ptr += 1;
20232 if (lh->version >= 4)
20233 {
20234 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20235 line_ptr += 1;
20236 }
20237 else
20238 lh->maximum_ops_per_instruction = 1;
20239
20240 if (lh->maximum_ops_per_instruction == 0)
20241 {
20242 lh->maximum_ops_per_instruction = 1;
20243 complaint (&symfile_complaints,
20244 _("invalid maximum_ops_per_instruction "
20245 "in `.debug_line' section"));
20246 }
20247
20248 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20249 line_ptr += 1;
20250 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20251 line_ptr += 1;
20252 lh->line_range = read_1_byte (abfd, line_ptr);
20253 line_ptr += 1;
20254 lh->opcode_base = read_1_byte (abfd, line_ptr);
20255 line_ptr += 1;
20256 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20257
20258 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20259 for (i = 1; i < lh->opcode_base; ++i)
20260 {
20261 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20262 line_ptr += 1;
20263 }
20264
20265 if (lh->version >= 5)
20266 {
20267 /* Read directory table. */
20268 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20269 &cu->header,
20270 [] (struct line_header *lh, const char *name,
20271 dir_index d_index, unsigned int mod_time,
20272 unsigned int length)
20273 {
20274 lh->add_include_dir (name);
20275 });
20276
20277 /* Read file name table. */
20278 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20279 &cu->header,
20280 [] (struct line_header *lh, const char *name,
20281 dir_index d_index, unsigned int mod_time,
20282 unsigned int length)
20283 {
20284 lh->add_file_name (name, d_index, mod_time, length);
20285 });
20286 }
20287 else
20288 {
20289 /* Read directory table. */
20290 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20291 {
20292 line_ptr += bytes_read;
20293 lh->add_include_dir (cur_dir);
20294 }
20295 line_ptr += bytes_read;
20296
20297 /* Read file name table. */
20298 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20299 {
20300 unsigned int mod_time, length;
20301 dir_index d_index;
20302
20303 line_ptr += bytes_read;
20304 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20305 line_ptr += bytes_read;
20306 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20307 line_ptr += bytes_read;
20308 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20309 line_ptr += bytes_read;
20310
20311 lh->add_file_name (cur_file, d_index, mod_time, length);
20312 }
20313 line_ptr += bytes_read;
20314 }
20315 lh->statement_program_start = line_ptr;
20316
20317 if (line_ptr > (section->buffer + section->size))
20318 complaint (&symfile_complaints,
20319 _("line number info header doesn't "
20320 "fit in `.debug_line' section"));
20321
20322 return lh;
20323 }
20324
20325 /* Subroutine of dwarf_decode_lines to simplify it.
20326 Return the file name of the psymtab for included file FILE_INDEX
20327 in line header LH of PST.
20328 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20329 If space for the result is malloc'd, *NAME_HOLDER will be set.
20330 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20331
20332 static const char *
20333 psymtab_include_file_name (const struct line_header *lh, int file_index,
20334 const struct partial_symtab *pst,
20335 const char *comp_dir,
20336 gdb::unique_xmalloc_ptr<char> *name_holder)
20337 {
20338 const file_entry &fe = lh->file_names[file_index];
20339 const char *include_name = fe.name;
20340 const char *include_name_to_compare = include_name;
20341 const char *pst_filename;
20342 int file_is_pst;
20343
20344 const char *dir_name = fe.include_dir (lh);
20345
20346 gdb::unique_xmalloc_ptr<char> hold_compare;
20347 if (!IS_ABSOLUTE_PATH (include_name)
20348 && (dir_name != NULL || comp_dir != NULL))
20349 {
20350 /* Avoid creating a duplicate psymtab for PST.
20351 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20352 Before we do the comparison, however, we need to account
20353 for DIR_NAME and COMP_DIR.
20354 First prepend dir_name (if non-NULL). If we still don't
20355 have an absolute path prepend comp_dir (if non-NULL).
20356 However, the directory we record in the include-file's
20357 psymtab does not contain COMP_DIR (to match the
20358 corresponding symtab(s)).
20359
20360 Example:
20361
20362 bash$ cd /tmp
20363 bash$ gcc -g ./hello.c
20364 include_name = "hello.c"
20365 dir_name = "."
20366 DW_AT_comp_dir = comp_dir = "/tmp"
20367 DW_AT_name = "./hello.c"
20368
20369 */
20370
20371 if (dir_name != NULL)
20372 {
20373 name_holder->reset (concat (dir_name, SLASH_STRING,
20374 include_name, (char *) NULL));
20375 include_name = name_holder->get ();
20376 include_name_to_compare = include_name;
20377 }
20378 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20379 {
20380 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20381 include_name, (char *) NULL));
20382 include_name_to_compare = hold_compare.get ();
20383 }
20384 }
20385
20386 pst_filename = pst->filename;
20387 gdb::unique_xmalloc_ptr<char> copied_name;
20388 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20389 {
20390 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20391 pst_filename, (char *) NULL));
20392 pst_filename = copied_name.get ();
20393 }
20394
20395 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20396
20397 if (file_is_pst)
20398 return NULL;
20399 return include_name;
20400 }
20401
20402 /* State machine to track the state of the line number program. */
20403
20404 class lnp_state_machine
20405 {
20406 public:
20407 /* Initialize a machine state for the start of a line number
20408 program. */
20409 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20410
20411 file_entry *current_file ()
20412 {
20413 /* lh->file_names is 0-based, but the file name numbers in the
20414 statement program are 1-based. */
20415 return m_line_header->file_name_at (m_file);
20416 }
20417
20418 /* Record the line in the state machine. END_SEQUENCE is true if
20419 we're processing the end of a sequence. */
20420 void record_line (bool end_sequence);
20421
20422 /* Check address and if invalid nop-out the rest of the lines in this
20423 sequence. */
20424 void check_line_address (struct dwarf2_cu *cu,
20425 const gdb_byte *line_ptr,
20426 CORE_ADDR lowpc, CORE_ADDR address);
20427
20428 void handle_set_discriminator (unsigned int discriminator)
20429 {
20430 m_discriminator = discriminator;
20431 m_line_has_non_zero_discriminator |= discriminator != 0;
20432 }
20433
20434 /* Handle DW_LNE_set_address. */
20435 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20436 {
20437 m_op_index = 0;
20438 address += baseaddr;
20439 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20440 }
20441
20442 /* Handle DW_LNS_advance_pc. */
20443 void handle_advance_pc (CORE_ADDR adjust);
20444
20445 /* Handle a special opcode. */
20446 void handle_special_opcode (unsigned char op_code);
20447
20448 /* Handle DW_LNS_advance_line. */
20449 void handle_advance_line (int line_delta)
20450 {
20451 advance_line (line_delta);
20452 }
20453
20454 /* Handle DW_LNS_set_file. */
20455 void handle_set_file (file_name_index file);
20456
20457 /* Handle DW_LNS_negate_stmt. */
20458 void handle_negate_stmt ()
20459 {
20460 m_is_stmt = !m_is_stmt;
20461 }
20462
20463 /* Handle DW_LNS_const_add_pc. */
20464 void handle_const_add_pc ();
20465
20466 /* Handle DW_LNS_fixed_advance_pc. */
20467 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20468 {
20469 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20470 m_op_index = 0;
20471 }
20472
20473 /* Handle DW_LNS_copy. */
20474 void handle_copy ()
20475 {
20476 record_line (false);
20477 m_discriminator = 0;
20478 }
20479
20480 /* Handle DW_LNE_end_sequence. */
20481 void handle_end_sequence ()
20482 {
20483 m_record_line_callback = ::record_line;
20484 }
20485
20486 private:
20487 /* Advance the line by LINE_DELTA. */
20488 void advance_line (int line_delta)
20489 {
20490 m_line += line_delta;
20491
20492 if (line_delta != 0)
20493 m_line_has_non_zero_discriminator = m_discriminator != 0;
20494 }
20495
20496 gdbarch *m_gdbarch;
20497
20498 /* True if we're recording lines.
20499 Otherwise we're building partial symtabs and are just interested in
20500 finding include files mentioned by the line number program. */
20501 bool m_record_lines_p;
20502
20503 /* The line number header. */
20504 line_header *m_line_header;
20505
20506 /* These are part of the standard DWARF line number state machine,
20507 and initialized according to the DWARF spec. */
20508
20509 unsigned char m_op_index = 0;
20510 /* The line table index (1-based) of the current file. */
20511 file_name_index m_file = (file_name_index) 1;
20512 unsigned int m_line = 1;
20513
20514 /* These are initialized in the constructor. */
20515
20516 CORE_ADDR m_address;
20517 bool m_is_stmt;
20518 unsigned int m_discriminator;
20519
20520 /* Additional bits of state we need to track. */
20521
20522 /* The last file that we called dwarf2_start_subfile for.
20523 This is only used for TLLs. */
20524 unsigned int m_last_file = 0;
20525 /* The last file a line number was recorded for. */
20526 struct subfile *m_last_subfile = NULL;
20527
20528 /* The function to call to record a line. */
20529 record_line_ftype *m_record_line_callback = NULL;
20530
20531 /* The last line number that was recorded, used to coalesce
20532 consecutive entries for the same line. This can happen, for
20533 example, when discriminators are present. PR 17276. */
20534 unsigned int m_last_line = 0;
20535 bool m_line_has_non_zero_discriminator = false;
20536 };
20537
20538 void
20539 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20540 {
20541 CORE_ADDR addr_adj = (((m_op_index + adjust)
20542 / m_line_header->maximum_ops_per_instruction)
20543 * m_line_header->minimum_instruction_length);
20544 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20545 m_op_index = ((m_op_index + adjust)
20546 % m_line_header->maximum_ops_per_instruction);
20547 }
20548
20549 void
20550 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20551 {
20552 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20553 CORE_ADDR addr_adj = (((m_op_index
20554 + (adj_opcode / m_line_header->line_range))
20555 / m_line_header->maximum_ops_per_instruction)
20556 * m_line_header->minimum_instruction_length);
20557 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20558 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20559 % m_line_header->maximum_ops_per_instruction);
20560
20561 int line_delta = (m_line_header->line_base
20562 + (adj_opcode % m_line_header->line_range));
20563 advance_line (line_delta);
20564 record_line (false);
20565 m_discriminator = 0;
20566 }
20567
20568 void
20569 lnp_state_machine::handle_set_file (file_name_index file)
20570 {
20571 m_file = file;
20572
20573 const file_entry *fe = current_file ();
20574 if (fe == NULL)
20575 dwarf2_debug_line_missing_file_complaint ();
20576 else if (m_record_lines_p)
20577 {
20578 const char *dir = fe->include_dir (m_line_header);
20579
20580 m_last_subfile = current_subfile;
20581 m_line_has_non_zero_discriminator = m_discriminator != 0;
20582 dwarf2_start_subfile (fe->name, dir);
20583 }
20584 }
20585
20586 void
20587 lnp_state_machine::handle_const_add_pc ()
20588 {
20589 CORE_ADDR adjust
20590 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20591
20592 CORE_ADDR addr_adj
20593 = (((m_op_index + adjust)
20594 / m_line_header->maximum_ops_per_instruction)
20595 * m_line_header->minimum_instruction_length);
20596
20597 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20598 m_op_index = ((m_op_index + adjust)
20599 % m_line_header->maximum_ops_per_instruction);
20600 }
20601
20602 /* Ignore this record_line request. */
20603
20604 static void
20605 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20606 {
20607 return;
20608 }
20609
20610 /* Return non-zero if we should add LINE to the line number table.
20611 LINE is the line to add, LAST_LINE is the last line that was added,
20612 LAST_SUBFILE is the subfile for LAST_LINE.
20613 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20614 had a non-zero discriminator.
20615
20616 We have to be careful in the presence of discriminators.
20617 E.g., for this line:
20618
20619 for (i = 0; i < 100000; i++);
20620
20621 clang can emit four line number entries for that one line,
20622 each with a different discriminator.
20623 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20624
20625 However, we want gdb to coalesce all four entries into one.
20626 Otherwise the user could stepi into the middle of the line and
20627 gdb would get confused about whether the pc really was in the
20628 middle of the line.
20629
20630 Things are further complicated by the fact that two consecutive
20631 line number entries for the same line is a heuristic used by gcc
20632 to denote the end of the prologue. So we can't just discard duplicate
20633 entries, we have to be selective about it. The heuristic we use is
20634 that we only collapse consecutive entries for the same line if at least
20635 one of those entries has a non-zero discriminator. PR 17276.
20636
20637 Note: Addresses in the line number state machine can never go backwards
20638 within one sequence, thus this coalescing is ok. */
20639
20640 static int
20641 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20642 int line_has_non_zero_discriminator,
20643 struct subfile *last_subfile)
20644 {
20645 if (current_subfile != last_subfile)
20646 return 1;
20647 if (line != last_line)
20648 return 1;
20649 /* Same line for the same file that we've seen already.
20650 As a last check, for pr 17276, only record the line if the line
20651 has never had a non-zero discriminator. */
20652 if (!line_has_non_zero_discriminator)
20653 return 1;
20654 return 0;
20655 }
20656
20657 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20658 in the line table of subfile SUBFILE. */
20659
20660 static void
20661 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20662 unsigned int line, CORE_ADDR address,
20663 record_line_ftype p_record_line)
20664 {
20665 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20666
20667 if (dwarf_line_debug)
20668 {
20669 fprintf_unfiltered (gdb_stdlog,
20670 "Recording line %u, file %s, address %s\n",
20671 line, lbasename (subfile->name),
20672 paddress (gdbarch, address));
20673 }
20674
20675 (*p_record_line) (subfile, line, addr);
20676 }
20677
20678 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20679 Mark the end of a set of line number records.
20680 The arguments are the same as for dwarf_record_line_1.
20681 If SUBFILE is NULL the request is ignored. */
20682
20683 static void
20684 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20685 CORE_ADDR address, record_line_ftype p_record_line)
20686 {
20687 if (subfile == NULL)
20688 return;
20689
20690 if (dwarf_line_debug)
20691 {
20692 fprintf_unfiltered (gdb_stdlog,
20693 "Finishing current line, file %s, address %s\n",
20694 lbasename (subfile->name),
20695 paddress (gdbarch, address));
20696 }
20697
20698 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20699 }
20700
20701 void
20702 lnp_state_machine::record_line (bool end_sequence)
20703 {
20704 if (dwarf_line_debug)
20705 {
20706 fprintf_unfiltered (gdb_stdlog,
20707 "Processing actual line %u: file %u,"
20708 " address %s, is_stmt %u, discrim %u\n",
20709 m_line, to_underlying (m_file),
20710 paddress (m_gdbarch, m_address),
20711 m_is_stmt, m_discriminator);
20712 }
20713
20714 file_entry *fe = current_file ();
20715
20716 if (fe == NULL)
20717 dwarf2_debug_line_missing_file_complaint ();
20718 /* For now we ignore lines not starting on an instruction boundary.
20719 But not when processing end_sequence for compatibility with the
20720 previous version of the code. */
20721 else if (m_op_index == 0 || end_sequence)
20722 {
20723 fe->included_p = 1;
20724 if (m_record_lines_p && m_is_stmt)
20725 {
20726 if (m_last_subfile != current_subfile || end_sequence)
20727 {
20728 dwarf_finish_line (m_gdbarch, m_last_subfile,
20729 m_address, m_record_line_callback);
20730 }
20731
20732 if (!end_sequence)
20733 {
20734 if (dwarf_record_line_p (m_line, m_last_line,
20735 m_line_has_non_zero_discriminator,
20736 m_last_subfile))
20737 {
20738 dwarf_record_line_1 (m_gdbarch, current_subfile,
20739 m_line, m_address,
20740 m_record_line_callback);
20741 }
20742 m_last_subfile = current_subfile;
20743 m_last_line = m_line;
20744 }
20745 }
20746 }
20747 }
20748
20749 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20750 bool record_lines_p)
20751 {
20752 m_gdbarch = arch;
20753 m_record_lines_p = record_lines_p;
20754 m_line_header = lh;
20755
20756 m_record_line_callback = ::record_line;
20757
20758 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20759 was a line entry for it so that the backend has a chance to adjust it
20760 and also record it in case it needs it. This is currently used by MIPS
20761 code, cf. `mips_adjust_dwarf2_line'. */
20762 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20763 m_is_stmt = lh->default_is_stmt;
20764 m_discriminator = 0;
20765 }
20766
20767 void
20768 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20769 const gdb_byte *line_ptr,
20770 CORE_ADDR lowpc, CORE_ADDR address)
20771 {
20772 /* If address < lowpc then it's not a usable value, it's outside the
20773 pc range of the CU. However, we restrict the test to only address
20774 values of zero to preserve GDB's previous behaviour which is to
20775 handle the specific case of a function being GC'd by the linker. */
20776
20777 if (address == 0 && address < lowpc)
20778 {
20779 /* This line table is for a function which has been
20780 GCd by the linker. Ignore it. PR gdb/12528 */
20781
20782 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20783 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20784
20785 complaint (&symfile_complaints,
20786 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20787 line_offset, objfile_name (objfile));
20788 m_record_line_callback = noop_record_line;
20789 /* Note: record_line_callback is left as noop_record_line until
20790 we see DW_LNE_end_sequence. */
20791 }
20792 }
20793
20794 /* Subroutine of dwarf_decode_lines to simplify it.
20795 Process the line number information in LH.
20796 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20797 program in order to set included_p for every referenced header. */
20798
20799 static void
20800 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20801 const int decode_for_pst_p, CORE_ADDR lowpc)
20802 {
20803 const gdb_byte *line_ptr, *extended_end;
20804 const gdb_byte *line_end;
20805 unsigned int bytes_read, extended_len;
20806 unsigned char op_code, extended_op;
20807 CORE_ADDR baseaddr;
20808 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20809 bfd *abfd = objfile->obfd;
20810 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20811 /* True if we're recording line info (as opposed to building partial
20812 symtabs and just interested in finding include files mentioned by
20813 the line number program). */
20814 bool record_lines_p = !decode_for_pst_p;
20815
20816 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20817
20818 line_ptr = lh->statement_program_start;
20819 line_end = lh->statement_program_end;
20820
20821 /* Read the statement sequences until there's nothing left. */
20822 while (line_ptr < line_end)
20823 {
20824 /* The DWARF line number program state machine. Reset the state
20825 machine at the start of each sequence. */
20826 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20827 bool end_sequence = false;
20828
20829 if (record_lines_p)
20830 {
20831 /* Start a subfile for the current file of the state
20832 machine. */
20833 const file_entry *fe = state_machine.current_file ();
20834
20835 if (fe != NULL)
20836 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20837 }
20838
20839 /* Decode the table. */
20840 while (line_ptr < line_end && !end_sequence)
20841 {
20842 op_code = read_1_byte (abfd, line_ptr);
20843 line_ptr += 1;
20844
20845 if (op_code >= lh->opcode_base)
20846 {
20847 /* Special opcode. */
20848 state_machine.handle_special_opcode (op_code);
20849 }
20850 else switch (op_code)
20851 {
20852 case DW_LNS_extended_op:
20853 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20854 &bytes_read);
20855 line_ptr += bytes_read;
20856 extended_end = line_ptr + extended_len;
20857 extended_op = read_1_byte (abfd, line_ptr);
20858 line_ptr += 1;
20859 switch (extended_op)
20860 {
20861 case DW_LNE_end_sequence:
20862 state_machine.handle_end_sequence ();
20863 end_sequence = true;
20864 break;
20865 case DW_LNE_set_address:
20866 {
20867 CORE_ADDR address
20868 = read_address (abfd, line_ptr, cu, &bytes_read);
20869 line_ptr += bytes_read;
20870
20871 state_machine.check_line_address (cu, line_ptr,
20872 lowpc, address);
20873 state_machine.handle_set_address (baseaddr, address);
20874 }
20875 break;
20876 case DW_LNE_define_file:
20877 {
20878 const char *cur_file;
20879 unsigned int mod_time, length;
20880 dir_index dindex;
20881
20882 cur_file = read_direct_string (abfd, line_ptr,
20883 &bytes_read);
20884 line_ptr += bytes_read;
20885 dindex = (dir_index)
20886 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20887 line_ptr += bytes_read;
20888 mod_time =
20889 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20890 line_ptr += bytes_read;
20891 length =
20892 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20893 line_ptr += bytes_read;
20894 lh->add_file_name (cur_file, dindex, mod_time, length);
20895 }
20896 break;
20897 case DW_LNE_set_discriminator:
20898 {
20899 /* The discriminator is not interesting to the
20900 debugger; just ignore it. We still need to
20901 check its value though:
20902 if there are consecutive entries for the same
20903 (non-prologue) line we want to coalesce them.
20904 PR 17276. */
20905 unsigned int discr
20906 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20907 line_ptr += bytes_read;
20908
20909 state_machine.handle_set_discriminator (discr);
20910 }
20911 break;
20912 default:
20913 complaint (&symfile_complaints,
20914 _("mangled .debug_line section"));
20915 return;
20916 }
20917 /* Make sure that we parsed the extended op correctly. If e.g.
20918 we expected a different address size than the producer used,
20919 we may have read the wrong number of bytes. */
20920 if (line_ptr != extended_end)
20921 {
20922 complaint (&symfile_complaints,
20923 _("mangled .debug_line section"));
20924 return;
20925 }
20926 break;
20927 case DW_LNS_copy:
20928 state_machine.handle_copy ();
20929 break;
20930 case DW_LNS_advance_pc:
20931 {
20932 CORE_ADDR adjust
20933 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20934 line_ptr += bytes_read;
20935
20936 state_machine.handle_advance_pc (adjust);
20937 }
20938 break;
20939 case DW_LNS_advance_line:
20940 {
20941 int line_delta
20942 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20943 line_ptr += bytes_read;
20944
20945 state_machine.handle_advance_line (line_delta);
20946 }
20947 break;
20948 case DW_LNS_set_file:
20949 {
20950 file_name_index file
20951 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20952 &bytes_read);
20953 line_ptr += bytes_read;
20954
20955 state_machine.handle_set_file (file);
20956 }
20957 break;
20958 case DW_LNS_set_column:
20959 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20960 line_ptr += bytes_read;
20961 break;
20962 case DW_LNS_negate_stmt:
20963 state_machine.handle_negate_stmt ();
20964 break;
20965 case DW_LNS_set_basic_block:
20966 break;
20967 /* Add to the address register of the state machine the
20968 address increment value corresponding to special opcode
20969 255. I.e., this value is scaled by the minimum
20970 instruction length since special opcode 255 would have
20971 scaled the increment. */
20972 case DW_LNS_const_add_pc:
20973 state_machine.handle_const_add_pc ();
20974 break;
20975 case DW_LNS_fixed_advance_pc:
20976 {
20977 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20978 line_ptr += 2;
20979
20980 state_machine.handle_fixed_advance_pc (addr_adj);
20981 }
20982 break;
20983 default:
20984 {
20985 /* Unknown standard opcode, ignore it. */
20986 int i;
20987
20988 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20989 {
20990 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20991 line_ptr += bytes_read;
20992 }
20993 }
20994 }
20995 }
20996
20997 if (!end_sequence)
20998 dwarf2_debug_line_missing_end_sequence_complaint ();
20999
21000 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21001 in which case we still finish recording the last line). */
21002 state_machine.record_line (true);
21003 }
21004 }
21005
21006 /* Decode the Line Number Program (LNP) for the given line_header
21007 structure and CU. The actual information extracted and the type
21008 of structures created from the LNP depends on the value of PST.
21009
21010 1. If PST is NULL, then this procedure uses the data from the program
21011 to create all necessary symbol tables, and their linetables.
21012
21013 2. If PST is not NULL, this procedure reads the program to determine
21014 the list of files included by the unit represented by PST, and
21015 builds all the associated partial symbol tables.
21016
21017 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21018 It is used for relative paths in the line table.
21019 NOTE: When processing partial symtabs (pst != NULL),
21020 comp_dir == pst->dirname.
21021
21022 NOTE: It is important that psymtabs have the same file name (via strcmp)
21023 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21024 symtab we don't use it in the name of the psymtabs we create.
21025 E.g. expand_line_sal requires this when finding psymtabs to expand.
21026 A good testcase for this is mb-inline.exp.
21027
21028 LOWPC is the lowest address in CU (or 0 if not known).
21029
21030 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21031 for its PC<->lines mapping information. Otherwise only the filename
21032 table is read in. */
21033
21034 static void
21035 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21036 struct dwarf2_cu *cu, struct partial_symtab *pst,
21037 CORE_ADDR lowpc, int decode_mapping)
21038 {
21039 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21040 const int decode_for_pst_p = (pst != NULL);
21041
21042 if (decode_mapping)
21043 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21044
21045 if (decode_for_pst_p)
21046 {
21047 int file_index;
21048
21049 /* Now that we're done scanning the Line Header Program, we can
21050 create the psymtab of each included file. */
21051 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21052 if (lh->file_names[file_index].included_p == 1)
21053 {
21054 gdb::unique_xmalloc_ptr<char> name_holder;
21055 const char *include_name =
21056 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21057 &name_holder);
21058 if (include_name != NULL)
21059 dwarf2_create_include_psymtab (include_name, pst, objfile);
21060 }
21061 }
21062 else
21063 {
21064 /* Make sure a symtab is created for every file, even files
21065 which contain only variables (i.e. no code with associated
21066 line numbers). */
21067 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21068 int i;
21069
21070 for (i = 0; i < lh->file_names.size (); i++)
21071 {
21072 file_entry &fe = lh->file_names[i];
21073
21074 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21075
21076 if (current_subfile->symtab == NULL)
21077 {
21078 current_subfile->symtab
21079 = allocate_symtab (cust, current_subfile->name);
21080 }
21081 fe.symtab = current_subfile->symtab;
21082 }
21083 }
21084 }
21085
21086 /* Start a subfile for DWARF. FILENAME is the name of the file and
21087 DIRNAME the name of the source directory which contains FILENAME
21088 or NULL if not known.
21089 This routine tries to keep line numbers from identical absolute and
21090 relative file names in a common subfile.
21091
21092 Using the `list' example from the GDB testsuite, which resides in
21093 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21094 of /srcdir/list0.c yields the following debugging information for list0.c:
21095
21096 DW_AT_name: /srcdir/list0.c
21097 DW_AT_comp_dir: /compdir
21098 files.files[0].name: list0.h
21099 files.files[0].dir: /srcdir
21100 files.files[1].name: list0.c
21101 files.files[1].dir: /srcdir
21102
21103 The line number information for list0.c has to end up in a single
21104 subfile, so that `break /srcdir/list0.c:1' works as expected.
21105 start_subfile will ensure that this happens provided that we pass the
21106 concatenation of files.files[1].dir and files.files[1].name as the
21107 subfile's name. */
21108
21109 static void
21110 dwarf2_start_subfile (const char *filename, const char *dirname)
21111 {
21112 char *copy = NULL;
21113
21114 /* In order not to lose the line information directory,
21115 we concatenate it to the filename when it makes sense.
21116 Note that the Dwarf3 standard says (speaking of filenames in line
21117 information): ``The directory index is ignored for file names
21118 that represent full path names''. Thus ignoring dirname in the
21119 `else' branch below isn't an issue. */
21120
21121 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21122 {
21123 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21124 filename = copy;
21125 }
21126
21127 start_subfile (filename);
21128
21129 if (copy != NULL)
21130 xfree (copy);
21131 }
21132
21133 /* Start a symtab for DWARF.
21134 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21135
21136 static struct compunit_symtab *
21137 dwarf2_start_symtab (struct dwarf2_cu *cu,
21138 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21139 {
21140 struct compunit_symtab *cust
21141 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21142 low_pc, cu->language);
21143
21144 record_debugformat ("DWARF 2");
21145 record_producer (cu->producer);
21146
21147 /* We assume that we're processing GCC output. */
21148 processing_gcc_compilation = 2;
21149
21150 cu->processing_has_namespace_info = 0;
21151
21152 return cust;
21153 }
21154
21155 static void
21156 var_decode_location (struct attribute *attr, struct symbol *sym,
21157 struct dwarf2_cu *cu)
21158 {
21159 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21160 struct comp_unit_head *cu_header = &cu->header;
21161
21162 /* NOTE drow/2003-01-30: There used to be a comment and some special
21163 code here to turn a symbol with DW_AT_external and a
21164 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21165 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21166 with some versions of binutils) where shared libraries could have
21167 relocations against symbols in their debug information - the
21168 minimal symbol would have the right address, but the debug info
21169 would not. It's no longer necessary, because we will explicitly
21170 apply relocations when we read in the debug information now. */
21171
21172 /* A DW_AT_location attribute with no contents indicates that a
21173 variable has been optimized away. */
21174 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21175 {
21176 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21177 return;
21178 }
21179
21180 /* Handle one degenerate form of location expression specially, to
21181 preserve GDB's previous behavior when section offsets are
21182 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21183 then mark this symbol as LOC_STATIC. */
21184
21185 if (attr_form_is_block (attr)
21186 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21187 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21188 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21189 && (DW_BLOCK (attr)->size
21190 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21191 {
21192 unsigned int dummy;
21193
21194 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21195 SYMBOL_VALUE_ADDRESS (sym) =
21196 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21197 else
21198 SYMBOL_VALUE_ADDRESS (sym) =
21199 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21200 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21201 fixup_symbol_section (sym, objfile);
21202 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21203 SYMBOL_SECTION (sym));
21204 return;
21205 }
21206
21207 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21208 expression evaluator, and use LOC_COMPUTED only when necessary
21209 (i.e. when the value of a register or memory location is
21210 referenced, or a thread-local block, etc.). Then again, it might
21211 not be worthwhile. I'm assuming that it isn't unless performance
21212 or memory numbers show me otherwise. */
21213
21214 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21215
21216 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21217 cu->has_loclist = 1;
21218 }
21219
21220 /* Given a pointer to a DWARF information entry, figure out if we need
21221 to make a symbol table entry for it, and if so, create a new entry
21222 and return a pointer to it.
21223 If TYPE is NULL, determine symbol type from the die, otherwise
21224 used the passed type.
21225 If SPACE is not NULL, use it to hold the new symbol. If it is
21226 NULL, allocate a new symbol on the objfile's obstack. */
21227
21228 static struct symbol *
21229 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21230 struct symbol *space)
21231 {
21232 struct dwarf2_per_objfile *dwarf2_per_objfile
21233 = cu->per_cu->dwarf2_per_objfile;
21234 struct objfile *objfile = dwarf2_per_objfile->objfile;
21235 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21236 struct symbol *sym = NULL;
21237 const char *name;
21238 struct attribute *attr = NULL;
21239 struct attribute *attr2 = NULL;
21240 CORE_ADDR baseaddr;
21241 struct pending **list_to_add = NULL;
21242
21243 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21244
21245 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21246
21247 name = dwarf2_name (die, cu);
21248 if (name)
21249 {
21250 const char *linkagename;
21251 int suppress_add = 0;
21252
21253 if (space)
21254 sym = space;
21255 else
21256 sym = allocate_symbol (objfile);
21257 OBJSTAT (objfile, n_syms++);
21258
21259 /* Cache this symbol's name and the name's demangled form (if any). */
21260 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21261 linkagename = dwarf2_physname (name, die, cu);
21262 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21263
21264 /* Fortran does not have mangling standard and the mangling does differ
21265 between gfortran, iFort etc. */
21266 if (cu->language == language_fortran
21267 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21268 symbol_set_demangled_name (&(sym->ginfo),
21269 dwarf2_full_name (name, die, cu),
21270 NULL);
21271
21272 /* Default assumptions.
21273 Use the passed type or decode it from the die. */
21274 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21275 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21276 if (type != NULL)
21277 SYMBOL_TYPE (sym) = type;
21278 else
21279 SYMBOL_TYPE (sym) = die_type (die, cu);
21280 attr = dwarf2_attr (die,
21281 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21282 cu);
21283 if (attr)
21284 {
21285 SYMBOL_LINE (sym) = DW_UNSND (attr);
21286 }
21287
21288 attr = dwarf2_attr (die,
21289 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21290 cu);
21291 if (attr)
21292 {
21293 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21294 struct file_entry *fe;
21295
21296 if (cu->line_header != NULL)
21297 fe = cu->line_header->file_name_at (file_index);
21298 else
21299 fe = NULL;
21300
21301 if (fe == NULL)
21302 complaint (&symfile_complaints,
21303 _("file index out of range"));
21304 else
21305 symbol_set_symtab (sym, fe->symtab);
21306 }
21307
21308 switch (die->tag)
21309 {
21310 case DW_TAG_label:
21311 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21312 if (attr)
21313 {
21314 CORE_ADDR addr;
21315
21316 addr = attr_value_as_address (attr);
21317 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21318 SYMBOL_VALUE_ADDRESS (sym) = addr;
21319 }
21320 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21321 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21322 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21323 add_symbol_to_list (sym, cu->list_in_scope);
21324 break;
21325 case DW_TAG_subprogram:
21326 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21327 finish_block. */
21328 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21329 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21330 if ((attr2 && (DW_UNSND (attr2) != 0))
21331 || cu->language == language_ada)
21332 {
21333 /* Subprograms marked external are stored as a global symbol.
21334 Ada subprograms, whether marked external or not, are always
21335 stored as a global symbol, because we want to be able to
21336 access them globally. For instance, we want to be able
21337 to break on a nested subprogram without having to
21338 specify the context. */
21339 list_to_add = &global_symbols;
21340 }
21341 else
21342 {
21343 list_to_add = cu->list_in_scope;
21344 }
21345 break;
21346 case DW_TAG_inlined_subroutine:
21347 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21348 finish_block. */
21349 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21350 SYMBOL_INLINED (sym) = 1;
21351 list_to_add = cu->list_in_scope;
21352 break;
21353 case DW_TAG_template_value_param:
21354 suppress_add = 1;
21355 /* Fall through. */
21356 case DW_TAG_constant:
21357 case DW_TAG_variable:
21358 case DW_TAG_member:
21359 /* Compilation with minimal debug info may result in
21360 variables with missing type entries. Change the
21361 misleading `void' type to something sensible. */
21362 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21363 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21364
21365 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21366 /* In the case of DW_TAG_member, we should only be called for
21367 static const members. */
21368 if (die->tag == DW_TAG_member)
21369 {
21370 /* dwarf2_add_field uses die_is_declaration,
21371 so we do the same. */
21372 gdb_assert (die_is_declaration (die, cu));
21373 gdb_assert (attr);
21374 }
21375 if (attr)
21376 {
21377 dwarf2_const_value (attr, sym, cu);
21378 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21379 if (!suppress_add)
21380 {
21381 if (attr2 && (DW_UNSND (attr2) != 0))
21382 list_to_add = &global_symbols;
21383 else
21384 list_to_add = cu->list_in_scope;
21385 }
21386 break;
21387 }
21388 attr = dwarf2_attr (die, DW_AT_location, cu);
21389 if (attr)
21390 {
21391 var_decode_location (attr, sym, cu);
21392 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21393
21394 /* Fortran explicitly imports any global symbols to the local
21395 scope by DW_TAG_common_block. */
21396 if (cu->language == language_fortran && die->parent
21397 && die->parent->tag == DW_TAG_common_block)
21398 attr2 = NULL;
21399
21400 if (SYMBOL_CLASS (sym) == LOC_STATIC
21401 && SYMBOL_VALUE_ADDRESS (sym) == 0
21402 && !dwarf2_per_objfile->has_section_at_zero)
21403 {
21404 /* When a static variable is eliminated by the linker,
21405 the corresponding debug information is not stripped
21406 out, but the variable address is set to null;
21407 do not add such variables into symbol table. */
21408 }
21409 else if (attr2 && (DW_UNSND (attr2) != 0))
21410 {
21411 /* Workaround gfortran PR debug/40040 - it uses
21412 DW_AT_location for variables in -fPIC libraries which may
21413 get overriden by other libraries/executable and get
21414 a different address. Resolve it by the minimal symbol
21415 which may come from inferior's executable using copy
21416 relocation. Make this workaround only for gfortran as for
21417 other compilers GDB cannot guess the minimal symbol
21418 Fortran mangling kind. */
21419 if (cu->language == language_fortran && die->parent
21420 && die->parent->tag == DW_TAG_module
21421 && cu->producer
21422 && startswith (cu->producer, "GNU Fortran"))
21423 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21424
21425 /* A variable with DW_AT_external is never static,
21426 but it may be block-scoped. */
21427 list_to_add = (cu->list_in_scope == &file_symbols
21428 ? &global_symbols : cu->list_in_scope);
21429 }
21430 else
21431 list_to_add = cu->list_in_scope;
21432 }
21433 else
21434 {
21435 /* We do not know the address of this symbol.
21436 If it is an external symbol and we have type information
21437 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21438 The address of the variable will then be determined from
21439 the minimal symbol table whenever the variable is
21440 referenced. */
21441 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21442
21443 /* Fortran explicitly imports any global symbols to the local
21444 scope by DW_TAG_common_block. */
21445 if (cu->language == language_fortran && die->parent
21446 && die->parent->tag == DW_TAG_common_block)
21447 {
21448 /* SYMBOL_CLASS doesn't matter here because
21449 read_common_block is going to reset it. */
21450 if (!suppress_add)
21451 list_to_add = cu->list_in_scope;
21452 }
21453 else if (attr2 && (DW_UNSND (attr2) != 0)
21454 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21455 {
21456 /* A variable with DW_AT_external is never static, but it
21457 may be block-scoped. */
21458 list_to_add = (cu->list_in_scope == &file_symbols
21459 ? &global_symbols : cu->list_in_scope);
21460
21461 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21462 }
21463 else if (!die_is_declaration (die, cu))
21464 {
21465 /* Use the default LOC_OPTIMIZED_OUT class. */
21466 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21467 if (!suppress_add)
21468 list_to_add = cu->list_in_scope;
21469 }
21470 }
21471 break;
21472 case DW_TAG_formal_parameter:
21473 /* If we are inside a function, mark this as an argument. If
21474 not, we might be looking at an argument to an inlined function
21475 when we do not have enough information to show inlined frames;
21476 pretend it's a local variable in that case so that the user can
21477 still see it. */
21478 if (context_stack_depth > 0
21479 && context_stack[context_stack_depth - 1].name != NULL)
21480 SYMBOL_IS_ARGUMENT (sym) = 1;
21481 attr = dwarf2_attr (die, DW_AT_location, cu);
21482 if (attr)
21483 {
21484 var_decode_location (attr, sym, cu);
21485 }
21486 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21487 if (attr)
21488 {
21489 dwarf2_const_value (attr, sym, cu);
21490 }
21491
21492 list_to_add = cu->list_in_scope;
21493 break;
21494 case DW_TAG_unspecified_parameters:
21495 /* From varargs functions; gdb doesn't seem to have any
21496 interest in this information, so just ignore it for now.
21497 (FIXME?) */
21498 break;
21499 case DW_TAG_template_type_param:
21500 suppress_add = 1;
21501 /* Fall through. */
21502 case DW_TAG_class_type:
21503 case DW_TAG_interface_type:
21504 case DW_TAG_structure_type:
21505 case DW_TAG_union_type:
21506 case DW_TAG_set_type:
21507 case DW_TAG_enumeration_type:
21508 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21509 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21510
21511 {
21512 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21513 really ever be static objects: otherwise, if you try
21514 to, say, break of a class's method and you're in a file
21515 which doesn't mention that class, it won't work unless
21516 the check for all static symbols in lookup_symbol_aux
21517 saves you. See the OtherFileClass tests in
21518 gdb.c++/namespace.exp. */
21519
21520 if (!suppress_add)
21521 {
21522 list_to_add = (cu->list_in_scope == &file_symbols
21523 && cu->language == language_cplus
21524 ? &global_symbols : cu->list_in_scope);
21525
21526 /* The semantics of C++ state that "struct foo {
21527 ... }" also defines a typedef for "foo". */
21528 if (cu->language == language_cplus
21529 || cu->language == language_ada
21530 || cu->language == language_d
21531 || cu->language == language_rust)
21532 {
21533 /* The symbol's name is already allocated along
21534 with this objfile, so we don't need to
21535 duplicate it for the type. */
21536 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21537 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21538 }
21539 }
21540 }
21541 break;
21542 case DW_TAG_typedef:
21543 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21544 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21545 list_to_add = cu->list_in_scope;
21546 break;
21547 case DW_TAG_base_type:
21548 case DW_TAG_subrange_type:
21549 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21550 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21551 list_to_add = cu->list_in_scope;
21552 break;
21553 case DW_TAG_enumerator:
21554 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21555 if (attr)
21556 {
21557 dwarf2_const_value (attr, sym, cu);
21558 }
21559 {
21560 /* NOTE: carlton/2003-11-10: See comment above in the
21561 DW_TAG_class_type, etc. block. */
21562
21563 list_to_add = (cu->list_in_scope == &file_symbols
21564 && cu->language == language_cplus
21565 ? &global_symbols : cu->list_in_scope);
21566 }
21567 break;
21568 case DW_TAG_imported_declaration:
21569 case DW_TAG_namespace:
21570 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21571 list_to_add = &global_symbols;
21572 break;
21573 case DW_TAG_module:
21574 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21575 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21576 list_to_add = &global_symbols;
21577 break;
21578 case DW_TAG_common_block:
21579 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21580 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21581 add_symbol_to_list (sym, cu->list_in_scope);
21582 break;
21583 default:
21584 /* Not a tag we recognize. Hopefully we aren't processing
21585 trash data, but since we must specifically ignore things
21586 we don't recognize, there is nothing else we should do at
21587 this point. */
21588 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21589 dwarf_tag_name (die->tag));
21590 break;
21591 }
21592
21593 if (suppress_add)
21594 {
21595 sym->hash_next = objfile->template_symbols;
21596 objfile->template_symbols = sym;
21597 list_to_add = NULL;
21598 }
21599
21600 if (list_to_add != NULL)
21601 add_symbol_to_list (sym, list_to_add);
21602
21603 /* For the benefit of old versions of GCC, check for anonymous
21604 namespaces based on the demangled name. */
21605 if (!cu->processing_has_namespace_info
21606 && cu->language == language_cplus)
21607 cp_scan_for_anonymous_namespaces (sym, objfile);
21608 }
21609 return (sym);
21610 }
21611
21612 /* Given an attr with a DW_FORM_dataN value in host byte order,
21613 zero-extend it as appropriate for the symbol's type. The DWARF
21614 standard (v4) is not entirely clear about the meaning of using
21615 DW_FORM_dataN for a constant with a signed type, where the type is
21616 wider than the data. The conclusion of a discussion on the DWARF
21617 list was that this is unspecified. We choose to always zero-extend
21618 because that is the interpretation long in use by GCC. */
21619
21620 static gdb_byte *
21621 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21622 struct dwarf2_cu *cu, LONGEST *value, int bits)
21623 {
21624 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21625 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21626 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21627 LONGEST l = DW_UNSND (attr);
21628
21629 if (bits < sizeof (*value) * 8)
21630 {
21631 l &= ((LONGEST) 1 << bits) - 1;
21632 *value = l;
21633 }
21634 else if (bits == sizeof (*value) * 8)
21635 *value = l;
21636 else
21637 {
21638 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21639 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21640 return bytes;
21641 }
21642
21643 return NULL;
21644 }
21645
21646 /* Read a constant value from an attribute. Either set *VALUE, or if
21647 the value does not fit in *VALUE, set *BYTES - either already
21648 allocated on the objfile obstack, or newly allocated on OBSTACK,
21649 or, set *BATON, if we translated the constant to a location
21650 expression. */
21651
21652 static void
21653 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21654 const char *name, struct obstack *obstack,
21655 struct dwarf2_cu *cu,
21656 LONGEST *value, const gdb_byte **bytes,
21657 struct dwarf2_locexpr_baton **baton)
21658 {
21659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21660 struct comp_unit_head *cu_header = &cu->header;
21661 struct dwarf_block *blk;
21662 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21663 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21664
21665 *value = 0;
21666 *bytes = NULL;
21667 *baton = NULL;
21668
21669 switch (attr->form)
21670 {
21671 case DW_FORM_addr:
21672 case DW_FORM_GNU_addr_index:
21673 {
21674 gdb_byte *data;
21675
21676 if (TYPE_LENGTH (type) != cu_header->addr_size)
21677 dwarf2_const_value_length_mismatch_complaint (name,
21678 cu_header->addr_size,
21679 TYPE_LENGTH (type));
21680 /* Symbols of this form are reasonably rare, so we just
21681 piggyback on the existing location code rather than writing
21682 a new implementation of symbol_computed_ops. */
21683 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21684 (*baton)->per_cu = cu->per_cu;
21685 gdb_assert ((*baton)->per_cu);
21686
21687 (*baton)->size = 2 + cu_header->addr_size;
21688 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21689 (*baton)->data = data;
21690
21691 data[0] = DW_OP_addr;
21692 store_unsigned_integer (&data[1], cu_header->addr_size,
21693 byte_order, DW_ADDR (attr));
21694 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21695 }
21696 break;
21697 case DW_FORM_string:
21698 case DW_FORM_strp:
21699 case DW_FORM_GNU_str_index:
21700 case DW_FORM_GNU_strp_alt:
21701 /* DW_STRING is already allocated on the objfile obstack, point
21702 directly to it. */
21703 *bytes = (const gdb_byte *) DW_STRING (attr);
21704 break;
21705 case DW_FORM_block1:
21706 case DW_FORM_block2:
21707 case DW_FORM_block4:
21708 case DW_FORM_block:
21709 case DW_FORM_exprloc:
21710 case DW_FORM_data16:
21711 blk = DW_BLOCK (attr);
21712 if (TYPE_LENGTH (type) != blk->size)
21713 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21714 TYPE_LENGTH (type));
21715 *bytes = blk->data;
21716 break;
21717
21718 /* The DW_AT_const_value attributes are supposed to carry the
21719 symbol's value "represented as it would be on the target
21720 architecture." By the time we get here, it's already been
21721 converted to host endianness, so we just need to sign- or
21722 zero-extend it as appropriate. */
21723 case DW_FORM_data1:
21724 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21725 break;
21726 case DW_FORM_data2:
21727 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21728 break;
21729 case DW_FORM_data4:
21730 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21731 break;
21732 case DW_FORM_data8:
21733 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21734 break;
21735
21736 case DW_FORM_sdata:
21737 case DW_FORM_implicit_const:
21738 *value = DW_SND (attr);
21739 break;
21740
21741 case DW_FORM_udata:
21742 *value = DW_UNSND (attr);
21743 break;
21744
21745 default:
21746 complaint (&symfile_complaints,
21747 _("unsupported const value attribute form: '%s'"),
21748 dwarf_form_name (attr->form));
21749 *value = 0;
21750 break;
21751 }
21752 }
21753
21754
21755 /* Copy constant value from an attribute to a symbol. */
21756
21757 static void
21758 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21759 struct dwarf2_cu *cu)
21760 {
21761 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21762 LONGEST value;
21763 const gdb_byte *bytes;
21764 struct dwarf2_locexpr_baton *baton;
21765
21766 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21767 SYMBOL_PRINT_NAME (sym),
21768 &objfile->objfile_obstack, cu,
21769 &value, &bytes, &baton);
21770
21771 if (baton != NULL)
21772 {
21773 SYMBOL_LOCATION_BATON (sym) = baton;
21774 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21775 }
21776 else if (bytes != NULL)
21777 {
21778 SYMBOL_VALUE_BYTES (sym) = bytes;
21779 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21780 }
21781 else
21782 {
21783 SYMBOL_VALUE (sym) = value;
21784 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21785 }
21786 }
21787
21788 /* Return the type of the die in question using its DW_AT_type attribute. */
21789
21790 static struct type *
21791 die_type (struct die_info *die, struct dwarf2_cu *cu)
21792 {
21793 struct attribute *type_attr;
21794
21795 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21796 if (!type_attr)
21797 {
21798 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21799 /* A missing DW_AT_type represents a void type. */
21800 return objfile_type (objfile)->builtin_void;
21801 }
21802
21803 return lookup_die_type (die, type_attr, cu);
21804 }
21805
21806 /* True iff CU's producer generates GNAT Ada auxiliary information
21807 that allows to find parallel types through that information instead
21808 of having to do expensive parallel lookups by type name. */
21809
21810 static int
21811 need_gnat_info (struct dwarf2_cu *cu)
21812 {
21813 /* Assume that the Ada compiler was GNAT, which always produces
21814 the auxiliary information. */
21815 return (cu->language == language_ada);
21816 }
21817
21818 /* Return the auxiliary type of the die in question using its
21819 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21820 attribute is not present. */
21821
21822 static struct type *
21823 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21824 {
21825 struct attribute *type_attr;
21826
21827 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21828 if (!type_attr)
21829 return NULL;
21830
21831 return lookup_die_type (die, type_attr, cu);
21832 }
21833
21834 /* If DIE has a descriptive_type attribute, then set the TYPE's
21835 descriptive type accordingly. */
21836
21837 static void
21838 set_descriptive_type (struct type *type, struct die_info *die,
21839 struct dwarf2_cu *cu)
21840 {
21841 struct type *descriptive_type = die_descriptive_type (die, cu);
21842
21843 if (descriptive_type)
21844 {
21845 ALLOCATE_GNAT_AUX_TYPE (type);
21846 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21847 }
21848 }
21849
21850 /* Return the containing type of the die in question using its
21851 DW_AT_containing_type attribute. */
21852
21853 static struct type *
21854 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21855 {
21856 struct attribute *type_attr;
21857 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21858
21859 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21860 if (!type_attr)
21861 error (_("Dwarf Error: Problem turning containing type into gdb type "
21862 "[in module %s]"), objfile_name (objfile));
21863
21864 return lookup_die_type (die, type_attr, cu);
21865 }
21866
21867 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21868
21869 static struct type *
21870 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21871 {
21872 struct dwarf2_per_objfile *dwarf2_per_objfile
21873 = cu->per_cu->dwarf2_per_objfile;
21874 struct objfile *objfile = dwarf2_per_objfile->objfile;
21875 char *message, *saved;
21876
21877 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
21878 objfile_name (objfile),
21879 sect_offset_str (cu->header.sect_off),
21880 sect_offset_str (die->sect_off));
21881 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21882 message, strlen (message));
21883 xfree (message);
21884
21885 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21886 }
21887
21888 /* Look up the type of DIE in CU using its type attribute ATTR.
21889 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21890 DW_AT_containing_type.
21891 If there is no type substitute an error marker. */
21892
21893 static struct type *
21894 lookup_die_type (struct die_info *die, const struct attribute *attr,
21895 struct dwarf2_cu *cu)
21896 {
21897 struct dwarf2_per_objfile *dwarf2_per_objfile
21898 = cu->per_cu->dwarf2_per_objfile;
21899 struct objfile *objfile = dwarf2_per_objfile->objfile;
21900 struct type *this_type;
21901
21902 gdb_assert (attr->name == DW_AT_type
21903 || attr->name == DW_AT_GNAT_descriptive_type
21904 || attr->name == DW_AT_containing_type);
21905
21906 /* First see if we have it cached. */
21907
21908 if (attr->form == DW_FORM_GNU_ref_alt)
21909 {
21910 struct dwarf2_per_cu_data *per_cu;
21911 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21912
21913 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21914 dwarf2_per_objfile);
21915 this_type = get_die_type_at_offset (sect_off, per_cu);
21916 }
21917 else if (attr_form_is_ref (attr))
21918 {
21919 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21920
21921 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21922 }
21923 else if (attr->form == DW_FORM_ref_sig8)
21924 {
21925 ULONGEST signature = DW_SIGNATURE (attr);
21926
21927 return get_signatured_type (die, signature, cu);
21928 }
21929 else
21930 {
21931 complaint (&symfile_complaints,
21932 _("Dwarf Error: Bad type attribute %s in DIE"
21933 " at %s [in module %s]"),
21934 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21935 objfile_name (objfile));
21936 return build_error_marker_type (cu, die);
21937 }
21938
21939 /* If not cached we need to read it in. */
21940
21941 if (this_type == NULL)
21942 {
21943 struct die_info *type_die = NULL;
21944 struct dwarf2_cu *type_cu = cu;
21945
21946 if (attr_form_is_ref (attr))
21947 type_die = follow_die_ref (die, attr, &type_cu);
21948 if (type_die == NULL)
21949 return build_error_marker_type (cu, die);
21950 /* If we find the type now, it's probably because the type came
21951 from an inter-CU reference and the type's CU got expanded before
21952 ours. */
21953 this_type = read_type_die (type_die, type_cu);
21954 }
21955
21956 /* If we still don't have a type use an error marker. */
21957
21958 if (this_type == NULL)
21959 return build_error_marker_type (cu, die);
21960
21961 return this_type;
21962 }
21963
21964 /* Return the type in DIE, CU.
21965 Returns NULL for invalid types.
21966
21967 This first does a lookup in die_type_hash,
21968 and only reads the die in if necessary.
21969
21970 NOTE: This can be called when reading in partial or full symbols. */
21971
21972 static struct type *
21973 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21974 {
21975 struct type *this_type;
21976
21977 this_type = get_die_type (die, cu);
21978 if (this_type)
21979 return this_type;
21980
21981 return read_type_die_1 (die, cu);
21982 }
21983
21984 /* Read the type in DIE, CU.
21985 Returns NULL for invalid types. */
21986
21987 static struct type *
21988 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21989 {
21990 struct type *this_type = NULL;
21991
21992 switch (die->tag)
21993 {
21994 case DW_TAG_class_type:
21995 case DW_TAG_interface_type:
21996 case DW_TAG_structure_type:
21997 case DW_TAG_union_type:
21998 this_type = read_structure_type (die, cu);
21999 break;
22000 case DW_TAG_enumeration_type:
22001 this_type = read_enumeration_type (die, cu);
22002 break;
22003 case DW_TAG_subprogram:
22004 case DW_TAG_subroutine_type:
22005 case DW_TAG_inlined_subroutine:
22006 this_type = read_subroutine_type (die, cu);
22007 break;
22008 case DW_TAG_array_type:
22009 this_type = read_array_type (die, cu);
22010 break;
22011 case DW_TAG_set_type:
22012 this_type = read_set_type (die, cu);
22013 break;
22014 case DW_TAG_pointer_type:
22015 this_type = read_tag_pointer_type (die, cu);
22016 break;
22017 case DW_TAG_ptr_to_member_type:
22018 this_type = read_tag_ptr_to_member_type (die, cu);
22019 break;
22020 case DW_TAG_reference_type:
22021 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22022 break;
22023 case DW_TAG_rvalue_reference_type:
22024 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22025 break;
22026 case DW_TAG_const_type:
22027 this_type = read_tag_const_type (die, cu);
22028 break;
22029 case DW_TAG_volatile_type:
22030 this_type = read_tag_volatile_type (die, cu);
22031 break;
22032 case DW_TAG_restrict_type:
22033 this_type = read_tag_restrict_type (die, cu);
22034 break;
22035 case DW_TAG_string_type:
22036 this_type = read_tag_string_type (die, cu);
22037 break;
22038 case DW_TAG_typedef:
22039 this_type = read_typedef (die, cu);
22040 break;
22041 case DW_TAG_subrange_type:
22042 this_type = read_subrange_type (die, cu);
22043 break;
22044 case DW_TAG_base_type:
22045 this_type = read_base_type (die, cu);
22046 break;
22047 case DW_TAG_unspecified_type:
22048 this_type = read_unspecified_type (die, cu);
22049 break;
22050 case DW_TAG_namespace:
22051 this_type = read_namespace_type (die, cu);
22052 break;
22053 case DW_TAG_module:
22054 this_type = read_module_type (die, cu);
22055 break;
22056 case DW_TAG_atomic_type:
22057 this_type = read_tag_atomic_type (die, cu);
22058 break;
22059 default:
22060 complaint (&symfile_complaints,
22061 _("unexpected tag in read_type_die: '%s'"),
22062 dwarf_tag_name (die->tag));
22063 break;
22064 }
22065
22066 return this_type;
22067 }
22068
22069 /* See if we can figure out if the class lives in a namespace. We do
22070 this by looking for a member function; its demangled name will
22071 contain namespace info, if there is any.
22072 Return the computed name or NULL.
22073 Space for the result is allocated on the objfile's obstack.
22074 This is the full-die version of guess_partial_die_structure_name.
22075 In this case we know DIE has no useful parent. */
22076
22077 static char *
22078 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22079 {
22080 struct die_info *spec_die;
22081 struct dwarf2_cu *spec_cu;
22082 struct die_info *child;
22083 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22084
22085 spec_cu = cu;
22086 spec_die = die_specification (die, &spec_cu);
22087 if (spec_die != NULL)
22088 {
22089 die = spec_die;
22090 cu = spec_cu;
22091 }
22092
22093 for (child = die->child;
22094 child != NULL;
22095 child = child->sibling)
22096 {
22097 if (child->tag == DW_TAG_subprogram)
22098 {
22099 const char *linkage_name = dw2_linkage_name (child, cu);
22100
22101 if (linkage_name != NULL)
22102 {
22103 char *actual_name
22104 = language_class_name_from_physname (cu->language_defn,
22105 linkage_name);
22106 char *name = NULL;
22107
22108 if (actual_name != NULL)
22109 {
22110 const char *die_name = dwarf2_name (die, cu);
22111
22112 if (die_name != NULL
22113 && strcmp (die_name, actual_name) != 0)
22114 {
22115 /* Strip off the class name from the full name.
22116 We want the prefix. */
22117 int die_name_len = strlen (die_name);
22118 int actual_name_len = strlen (actual_name);
22119
22120 /* Test for '::' as a sanity check. */
22121 if (actual_name_len > die_name_len + 2
22122 && actual_name[actual_name_len
22123 - die_name_len - 1] == ':')
22124 name = (char *) obstack_copy0 (
22125 &objfile->per_bfd->storage_obstack,
22126 actual_name, actual_name_len - die_name_len - 2);
22127 }
22128 }
22129 xfree (actual_name);
22130 return name;
22131 }
22132 }
22133 }
22134
22135 return NULL;
22136 }
22137
22138 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22139 prefix part in such case. See
22140 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22141
22142 static const char *
22143 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22144 {
22145 struct attribute *attr;
22146 const char *base;
22147
22148 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22149 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22150 return NULL;
22151
22152 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22153 return NULL;
22154
22155 attr = dw2_linkage_name_attr (die, cu);
22156 if (attr == NULL || DW_STRING (attr) == NULL)
22157 return NULL;
22158
22159 /* dwarf2_name had to be already called. */
22160 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22161
22162 /* Strip the base name, keep any leading namespaces/classes. */
22163 base = strrchr (DW_STRING (attr), ':');
22164 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22165 return "";
22166
22167 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22168 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22169 DW_STRING (attr),
22170 &base[-1] - DW_STRING (attr));
22171 }
22172
22173 /* Return the name of the namespace/class that DIE is defined within,
22174 or "" if we can't tell. The caller should not xfree the result.
22175
22176 For example, if we're within the method foo() in the following
22177 code:
22178
22179 namespace N {
22180 class C {
22181 void foo () {
22182 }
22183 };
22184 }
22185
22186 then determine_prefix on foo's die will return "N::C". */
22187
22188 static const char *
22189 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22190 {
22191 struct dwarf2_per_objfile *dwarf2_per_objfile
22192 = cu->per_cu->dwarf2_per_objfile;
22193 struct die_info *parent, *spec_die;
22194 struct dwarf2_cu *spec_cu;
22195 struct type *parent_type;
22196 const char *retval;
22197
22198 if (cu->language != language_cplus
22199 && cu->language != language_fortran && cu->language != language_d
22200 && cu->language != language_rust)
22201 return "";
22202
22203 retval = anonymous_struct_prefix (die, cu);
22204 if (retval)
22205 return retval;
22206
22207 /* We have to be careful in the presence of DW_AT_specification.
22208 For example, with GCC 3.4, given the code
22209
22210 namespace N {
22211 void foo() {
22212 // Definition of N::foo.
22213 }
22214 }
22215
22216 then we'll have a tree of DIEs like this:
22217
22218 1: DW_TAG_compile_unit
22219 2: DW_TAG_namespace // N
22220 3: DW_TAG_subprogram // declaration of N::foo
22221 4: DW_TAG_subprogram // definition of N::foo
22222 DW_AT_specification // refers to die #3
22223
22224 Thus, when processing die #4, we have to pretend that we're in
22225 the context of its DW_AT_specification, namely the contex of die
22226 #3. */
22227 spec_cu = cu;
22228 spec_die = die_specification (die, &spec_cu);
22229 if (spec_die == NULL)
22230 parent = die->parent;
22231 else
22232 {
22233 parent = spec_die->parent;
22234 cu = spec_cu;
22235 }
22236
22237 if (parent == NULL)
22238 return "";
22239 else if (parent->building_fullname)
22240 {
22241 const char *name;
22242 const char *parent_name;
22243
22244 /* It has been seen on RealView 2.2 built binaries,
22245 DW_TAG_template_type_param types actually _defined_ as
22246 children of the parent class:
22247
22248 enum E {};
22249 template class <class Enum> Class{};
22250 Class<enum E> class_e;
22251
22252 1: DW_TAG_class_type (Class)
22253 2: DW_TAG_enumeration_type (E)
22254 3: DW_TAG_enumerator (enum1:0)
22255 3: DW_TAG_enumerator (enum2:1)
22256 ...
22257 2: DW_TAG_template_type_param
22258 DW_AT_type DW_FORM_ref_udata (E)
22259
22260 Besides being broken debug info, it can put GDB into an
22261 infinite loop. Consider:
22262
22263 When we're building the full name for Class<E>, we'll start
22264 at Class, and go look over its template type parameters,
22265 finding E. We'll then try to build the full name of E, and
22266 reach here. We're now trying to build the full name of E,
22267 and look over the parent DIE for containing scope. In the
22268 broken case, if we followed the parent DIE of E, we'd again
22269 find Class, and once again go look at its template type
22270 arguments, etc., etc. Simply don't consider such parent die
22271 as source-level parent of this die (it can't be, the language
22272 doesn't allow it), and break the loop here. */
22273 name = dwarf2_name (die, cu);
22274 parent_name = dwarf2_name (parent, cu);
22275 complaint (&symfile_complaints,
22276 _("template param type '%s' defined within parent '%s'"),
22277 name ? name : "<unknown>",
22278 parent_name ? parent_name : "<unknown>");
22279 return "";
22280 }
22281 else
22282 switch (parent->tag)
22283 {
22284 case DW_TAG_namespace:
22285 parent_type = read_type_die (parent, cu);
22286 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22287 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22288 Work around this problem here. */
22289 if (cu->language == language_cplus
22290 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22291 return "";
22292 /* We give a name to even anonymous namespaces. */
22293 return TYPE_TAG_NAME (parent_type);
22294 case DW_TAG_class_type:
22295 case DW_TAG_interface_type:
22296 case DW_TAG_structure_type:
22297 case DW_TAG_union_type:
22298 case DW_TAG_module:
22299 parent_type = read_type_die (parent, cu);
22300 if (TYPE_TAG_NAME (parent_type) != NULL)
22301 return TYPE_TAG_NAME (parent_type);
22302 else
22303 /* An anonymous structure is only allowed non-static data
22304 members; no typedefs, no member functions, et cetera.
22305 So it does not need a prefix. */
22306 return "";
22307 case DW_TAG_compile_unit:
22308 case DW_TAG_partial_unit:
22309 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22310 if (cu->language == language_cplus
22311 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22312 && die->child != NULL
22313 && (die->tag == DW_TAG_class_type
22314 || die->tag == DW_TAG_structure_type
22315 || die->tag == DW_TAG_union_type))
22316 {
22317 char *name = guess_full_die_structure_name (die, cu);
22318 if (name != NULL)
22319 return name;
22320 }
22321 return "";
22322 case DW_TAG_enumeration_type:
22323 parent_type = read_type_die (parent, cu);
22324 if (TYPE_DECLARED_CLASS (parent_type))
22325 {
22326 if (TYPE_TAG_NAME (parent_type) != NULL)
22327 return TYPE_TAG_NAME (parent_type);
22328 return "";
22329 }
22330 /* Fall through. */
22331 default:
22332 return determine_prefix (parent, cu);
22333 }
22334 }
22335
22336 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22337 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22338 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22339 an obconcat, otherwise allocate storage for the result. The CU argument is
22340 used to determine the language and hence, the appropriate separator. */
22341
22342 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22343
22344 static char *
22345 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22346 int physname, struct dwarf2_cu *cu)
22347 {
22348 const char *lead = "";
22349 const char *sep;
22350
22351 if (suffix == NULL || suffix[0] == '\0'
22352 || prefix == NULL || prefix[0] == '\0')
22353 sep = "";
22354 else if (cu->language == language_d)
22355 {
22356 /* For D, the 'main' function could be defined in any module, but it
22357 should never be prefixed. */
22358 if (strcmp (suffix, "D main") == 0)
22359 {
22360 prefix = "";
22361 sep = "";
22362 }
22363 else
22364 sep = ".";
22365 }
22366 else if (cu->language == language_fortran && physname)
22367 {
22368 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22369 DW_AT_MIPS_linkage_name is preferred and used instead. */
22370
22371 lead = "__";
22372 sep = "_MOD_";
22373 }
22374 else
22375 sep = "::";
22376
22377 if (prefix == NULL)
22378 prefix = "";
22379 if (suffix == NULL)
22380 suffix = "";
22381
22382 if (obs == NULL)
22383 {
22384 char *retval
22385 = ((char *)
22386 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22387
22388 strcpy (retval, lead);
22389 strcat (retval, prefix);
22390 strcat (retval, sep);
22391 strcat (retval, suffix);
22392 return retval;
22393 }
22394 else
22395 {
22396 /* We have an obstack. */
22397 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22398 }
22399 }
22400
22401 /* Return sibling of die, NULL if no sibling. */
22402
22403 static struct die_info *
22404 sibling_die (struct die_info *die)
22405 {
22406 return die->sibling;
22407 }
22408
22409 /* Get name of a die, return NULL if not found. */
22410
22411 static const char *
22412 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22413 struct obstack *obstack)
22414 {
22415 if (name && cu->language == language_cplus)
22416 {
22417 std::string canon_name = cp_canonicalize_string (name);
22418
22419 if (!canon_name.empty ())
22420 {
22421 if (canon_name != name)
22422 name = (const char *) obstack_copy0 (obstack,
22423 canon_name.c_str (),
22424 canon_name.length ());
22425 }
22426 }
22427
22428 return name;
22429 }
22430
22431 /* Get name of a die, return NULL if not found.
22432 Anonymous namespaces are converted to their magic string. */
22433
22434 static const char *
22435 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22436 {
22437 struct attribute *attr;
22438 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22439
22440 attr = dwarf2_attr (die, DW_AT_name, cu);
22441 if ((!attr || !DW_STRING (attr))
22442 && die->tag != DW_TAG_namespace
22443 && die->tag != DW_TAG_class_type
22444 && die->tag != DW_TAG_interface_type
22445 && die->tag != DW_TAG_structure_type
22446 && die->tag != DW_TAG_union_type)
22447 return NULL;
22448
22449 switch (die->tag)
22450 {
22451 case DW_TAG_compile_unit:
22452 case DW_TAG_partial_unit:
22453 /* Compilation units have a DW_AT_name that is a filename, not
22454 a source language identifier. */
22455 case DW_TAG_enumeration_type:
22456 case DW_TAG_enumerator:
22457 /* These tags always have simple identifiers already; no need
22458 to canonicalize them. */
22459 return DW_STRING (attr);
22460
22461 case DW_TAG_namespace:
22462 if (attr != NULL && DW_STRING (attr) != NULL)
22463 return DW_STRING (attr);
22464 return CP_ANONYMOUS_NAMESPACE_STR;
22465
22466 case DW_TAG_class_type:
22467 case DW_TAG_interface_type:
22468 case DW_TAG_structure_type:
22469 case DW_TAG_union_type:
22470 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22471 structures or unions. These were of the form "._%d" in GCC 4.1,
22472 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22473 and GCC 4.4. We work around this problem by ignoring these. */
22474 if (attr && DW_STRING (attr)
22475 && (startswith (DW_STRING (attr), "._")
22476 || startswith (DW_STRING (attr), "<anonymous")))
22477 return NULL;
22478
22479 /* GCC might emit a nameless typedef that has a linkage name. See
22480 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22481 if (!attr || DW_STRING (attr) == NULL)
22482 {
22483 char *demangled = NULL;
22484
22485 attr = dw2_linkage_name_attr (die, cu);
22486 if (attr == NULL || DW_STRING (attr) == NULL)
22487 return NULL;
22488
22489 /* Avoid demangling DW_STRING (attr) the second time on a second
22490 call for the same DIE. */
22491 if (!DW_STRING_IS_CANONICAL (attr))
22492 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22493
22494 if (demangled)
22495 {
22496 const char *base;
22497
22498 /* FIXME: we already did this for the partial symbol... */
22499 DW_STRING (attr)
22500 = ((const char *)
22501 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22502 demangled, strlen (demangled)));
22503 DW_STRING_IS_CANONICAL (attr) = 1;
22504 xfree (demangled);
22505
22506 /* Strip any leading namespaces/classes, keep only the base name.
22507 DW_AT_name for named DIEs does not contain the prefixes. */
22508 base = strrchr (DW_STRING (attr), ':');
22509 if (base && base > DW_STRING (attr) && base[-1] == ':')
22510 return &base[1];
22511 else
22512 return DW_STRING (attr);
22513 }
22514 }
22515 break;
22516
22517 default:
22518 break;
22519 }
22520
22521 if (!DW_STRING_IS_CANONICAL (attr))
22522 {
22523 DW_STRING (attr)
22524 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22525 &objfile->per_bfd->storage_obstack);
22526 DW_STRING_IS_CANONICAL (attr) = 1;
22527 }
22528 return DW_STRING (attr);
22529 }
22530
22531 /* Return the die that this die in an extension of, or NULL if there
22532 is none. *EXT_CU is the CU containing DIE on input, and the CU
22533 containing the return value on output. */
22534
22535 static struct die_info *
22536 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22537 {
22538 struct attribute *attr;
22539
22540 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22541 if (attr == NULL)
22542 return NULL;
22543
22544 return follow_die_ref (die, attr, ext_cu);
22545 }
22546
22547 /* Convert a DIE tag into its string name. */
22548
22549 static const char *
22550 dwarf_tag_name (unsigned tag)
22551 {
22552 const char *name = get_DW_TAG_name (tag);
22553
22554 if (name == NULL)
22555 return "DW_TAG_<unknown>";
22556
22557 return name;
22558 }
22559
22560 /* Convert a DWARF attribute code into its string name. */
22561
22562 static const char *
22563 dwarf_attr_name (unsigned attr)
22564 {
22565 const char *name;
22566
22567 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22568 if (attr == DW_AT_MIPS_fde)
22569 return "DW_AT_MIPS_fde";
22570 #else
22571 if (attr == DW_AT_HP_block_index)
22572 return "DW_AT_HP_block_index";
22573 #endif
22574
22575 name = get_DW_AT_name (attr);
22576
22577 if (name == NULL)
22578 return "DW_AT_<unknown>";
22579
22580 return name;
22581 }
22582
22583 /* Convert a DWARF value form code into its string name. */
22584
22585 static const char *
22586 dwarf_form_name (unsigned form)
22587 {
22588 const char *name = get_DW_FORM_name (form);
22589
22590 if (name == NULL)
22591 return "DW_FORM_<unknown>";
22592
22593 return name;
22594 }
22595
22596 static const char *
22597 dwarf_bool_name (unsigned mybool)
22598 {
22599 if (mybool)
22600 return "TRUE";
22601 else
22602 return "FALSE";
22603 }
22604
22605 /* Convert a DWARF type code into its string name. */
22606
22607 static const char *
22608 dwarf_type_encoding_name (unsigned enc)
22609 {
22610 const char *name = get_DW_ATE_name (enc);
22611
22612 if (name == NULL)
22613 return "DW_ATE_<unknown>";
22614
22615 return name;
22616 }
22617
22618 static void
22619 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22620 {
22621 unsigned int i;
22622
22623 print_spaces (indent, f);
22624 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22625 dwarf_tag_name (die->tag), die->abbrev,
22626 sect_offset_str (die->sect_off));
22627
22628 if (die->parent != NULL)
22629 {
22630 print_spaces (indent, f);
22631 fprintf_unfiltered (f, " parent at offset: %s\n",
22632 sect_offset_str (die->parent->sect_off));
22633 }
22634
22635 print_spaces (indent, f);
22636 fprintf_unfiltered (f, " has children: %s\n",
22637 dwarf_bool_name (die->child != NULL));
22638
22639 print_spaces (indent, f);
22640 fprintf_unfiltered (f, " attributes:\n");
22641
22642 for (i = 0; i < die->num_attrs; ++i)
22643 {
22644 print_spaces (indent, f);
22645 fprintf_unfiltered (f, " %s (%s) ",
22646 dwarf_attr_name (die->attrs[i].name),
22647 dwarf_form_name (die->attrs[i].form));
22648
22649 switch (die->attrs[i].form)
22650 {
22651 case DW_FORM_addr:
22652 case DW_FORM_GNU_addr_index:
22653 fprintf_unfiltered (f, "address: ");
22654 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22655 break;
22656 case DW_FORM_block2:
22657 case DW_FORM_block4:
22658 case DW_FORM_block:
22659 case DW_FORM_block1:
22660 fprintf_unfiltered (f, "block: size %s",
22661 pulongest (DW_BLOCK (&die->attrs[i])->size));
22662 break;
22663 case DW_FORM_exprloc:
22664 fprintf_unfiltered (f, "expression: size %s",
22665 pulongest (DW_BLOCK (&die->attrs[i])->size));
22666 break;
22667 case DW_FORM_data16:
22668 fprintf_unfiltered (f, "constant of 16 bytes");
22669 break;
22670 case DW_FORM_ref_addr:
22671 fprintf_unfiltered (f, "ref address: ");
22672 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22673 break;
22674 case DW_FORM_GNU_ref_alt:
22675 fprintf_unfiltered (f, "alt ref address: ");
22676 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22677 break;
22678 case DW_FORM_ref1:
22679 case DW_FORM_ref2:
22680 case DW_FORM_ref4:
22681 case DW_FORM_ref8:
22682 case DW_FORM_ref_udata:
22683 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22684 (long) (DW_UNSND (&die->attrs[i])));
22685 break;
22686 case DW_FORM_data1:
22687 case DW_FORM_data2:
22688 case DW_FORM_data4:
22689 case DW_FORM_data8:
22690 case DW_FORM_udata:
22691 case DW_FORM_sdata:
22692 fprintf_unfiltered (f, "constant: %s",
22693 pulongest (DW_UNSND (&die->attrs[i])));
22694 break;
22695 case DW_FORM_sec_offset:
22696 fprintf_unfiltered (f, "section offset: %s",
22697 pulongest (DW_UNSND (&die->attrs[i])));
22698 break;
22699 case DW_FORM_ref_sig8:
22700 fprintf_unfiltered (f, "signature: %s",
22701 hex_string (DW_SIGNATURE (&die->attrs[i])));
22702 break;
22703 case DW_FORM_string:
22704 case DW_FORM_strp:
22705 case DW_FORM_line_strp:
22706 case DW_FORM_GNU_str_index:
22707 case DW_FORM_GNU_strp_alt:
22708 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22709 DW_STRING (&die->attrs[i])
22710 ? DW_STRING (&die->attrs[i]) : "",
22711 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22712 break;
22713 case DW_FORM_flag:
22714 if (DW_UNSND (&die->attrs[i]))
22715 fprintf_unfiltered (f, "flag: TRUE");
22716 else
22717 fprintf_unfiltered (f, "flag: FALSE");
22718 break;
22719 case DW_FORM_flag_present:
22720 fprintf_unfiltered (f, "flag: TRUE");
22721 break;
22722 case DW_FORM_indirect:
22723 /* The reader will have reduced the indirect form to
22724 the "base form" so this form should not occur. */
22725 fprintf_unfiltered (f,
22726 "unexpected attribute form: DW_FORM_indirect");
22727 break;
22728 case DW_FORM_implicit_const:
22729 fprintf_unfiltered (f, "constant: %s",
22730 plongest (DW_SND (&die->attrs[i])));
22731 break;
22732 default:
22733 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22734 die->attrs[i].form);
22735 break;
22736 }
22737 fprintf_unfiltered (f, "\n");
22738 }
22739 }
22740
22741 static void
22742 dump_die_for_error (struct die_info *die)
22743 {
22744 dump_die_shallow (gdb_stderr, 0, die);
22745 }
22746
22747 static void
22748 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22749 {
22750 int indent = level * 4;
22751
22752 gdb_assert (die != NULL);
22753
22754 if (level >= max_level)
22755 return;
22756
22757 dump_die_shallow (f, indent, die);
22758
22759 if (die->child != NULL)
22760 {
22761 print_spaces (indent, f);
22762 fprintf_unfiltered (f, " Children:");
22763 if (level + 1 < max_level)
22764 {
22765 fprintf_unfiltered (f, "\n");
22766 dump_die_1 (f, level + 1, max_level, die->child);
22767 }
22768 else
22769 {
22770 fprintf_unfiltered (f,
22771 " [not printed, max nesting level reached]\n");
22772 }
22773 }
22774
22775 if (die->sibling != NULL && level > 0)
22776 {
22777 dump_die_1 (f, level, max_level, die->sibling);
22778 }
22779 }
22780
22781 /* This is called from the pdie macro in gdbinit.in.
22782 It's not static so gcc will keep a copy callable from gdb. */
22783
22784 void
22785 dump_die (struct die_info *die, int max_level)
22786 {
22787 dump_die_1 (gdb_stdlog, 0, max_level, die);
22788 }
22789
22790 static void
22791 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22792 {
22793 void **slot;
22794
22795 slot = htab_find_slot_with_hash (cu->die_hash, die,
22796 to_underlying (die->sect_off),
22797 INSERT);
22798
22799 *slot = die;
22800 }
22801
22802 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22803 required kind. */
22804
22805 static sect_offset
22806 dwarf2_get_ref_die_offset (const struct attribute *attr)
22807 {
22808 if (attr_form_is_ref (attr))
22809 return (sect_offset) DW_UNSND (attr);
22810
22811 complaint (&symfile_complaints,
22812 _("unsupported die ref attribute form: '%s'"),
22813 dwarf_form_name (attr->form));
22814 return {};
22815 }
22816
22817 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22818 * the value held by the attribute is not constant. */
22819
22820 static LONGEST
22821 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22822 {
22823 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22824 return DW_SND (attr);
22825 else if (attr->form == DW_FORM_udata
22826 || attr->form == DW_FORM_data1
22827 || attr->form == DW_FORM_data2
22828 || attr->form == DW_FORM_data4
22829 || attr->form == DW_FORM_data8)
22830 return DW_UNSND (attr);
22831 else
22832 {
22833 /* For DW_FORM_data16 see attr_form_is_constant. */
22834 complaint (&symfile_complaints,
22835 _("Attribute value is not a constant (%s)"),
22836 dwarf_form_name (attr->form));
22837 return default_value;
22838 }
22839 }
22840
22841 /* Follow reference or signature attribute ATTR of SRC_DIE.
22842 On entry *REF_CU is the CU of SRC_DIE.
22843 On exit *REF_CU is the CU of the result. */
22844
22845 static struct die_info *
22846 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22847 struct dwarf2_cu **ref_cu)
22848 {
22849 struct die_info *die;
22850
22851 if (attr_form_is_ref (attr))
22852 die = follow_die_ref (src_die, attr, ref_cu);
22853 else if (attr->form == DW_FORM_ref_sig8)
22854 die = follow_die_sig (src_die, attr, ref_cu);
22855 else
22856 {
22857 dump_die_for_error (src_die);
22858 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22859 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22860 }
22861
22862 return die;
22863 }
22864
22865 /* Follow reference OFFSET.
22866 On entry *REF_CU is the CU of the source die referencing OFFSET.
22867 On exit *REF_CU is the CU of the result.
22868 Returns NULL if OFFSET is invalid. */
22869
22870 static struct die_info *
22871 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22872 struct dwarf2_cu **ref_cu)
22873 {
22874 struct die_info temp_die;
22875 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22876 struct dwarf2_per_objfile *dwarf2_per_objfile
22877 = cu->per_cu->dwarf2_per_objfile;
22878
22879 gdb_assert (cu->per_cu != NULL);
22880
22881 target_cu = cu;
22882
22883 if (cu->per_cu->is_debug_types)
22884 {
22885 /* .debug_types CUs cannot reference anything outside their CU.
22886 If they need to, they have to reference a signatured type via
22887 DW_FORM_ref_sig8. */
22888 if (!offset_in_cu_p (&cu->header, sect_off))
22889 return NULL;
22890 }
22891 else if (offset_in_dwz != cu->per_cu->is_dwz
22892 || !offset_in_cu_p (&cu->header, sect_off))
22893 {
22894 struct dwarf2_per_cu_data *per_cu;
22895
22896 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22897 dwarf2_per_objfile);
22898
22899 /* If necessary, add it to the queue and load its DIEs. */
22900 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22901 load_full_comp_unit (per_cu, false, cu->language);
22902
22903 target_cu = per_cu->cu;
22904 }
22905 else if (cu->dies == NULL)
22906 {
22907 /* We're loading full DIEs during partial symbol reading. */
22908 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22909 load_full_comp_unit (cu->per_cu, false, language_minimal);
22910 }
22911
22912 *ref_cu = target_cu;
22913 temp_die.sect_off = sect_off;
22914 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22915 &temp_die,
22916 to_underlying (sect_off));
22917 }
22918
22919 /* Follow reference attribute ATTR of SRC_DIE.
22920 On entry *REF_CU is the CU of SRC_DIE.
22921 On exit *REF_CU is the CU of the result. */
22922
22923 static struct die_info *
22924 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22925 struct dwarf2_cu **ref_cu)
22926 {
22927 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22928 struct dwarf2_cu *cu = *ref_cu;
22929 struct die_info *die;
22930
22931 die = follow_die_offset (sect_off,
22932 (attr->form == DW_FORM_GNU_ref_alt
22933 || cu->per_cu->is_dwz),
22934 ref_cu);
22935 if (!die)
22936 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22937 "at %s [in module %s]"),
22938 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22939 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22940
22941 return die;
22942 }
22943
22944 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22945 Returned value is intended for DW_OP_call*. Returned
22946 dwarf2_locexpr_baton->data has lifetime of
22947 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22948
22949 struct dwarf2_locexpr_baton
22950 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22951 struct dwarf2_per_cu_data *per_cu,
22952 CORE_ADDR (*get_frame_pc) (void *baton),
22953 void *baton)
22954 {
22955 struct dwarf2_cu *cu;
22956 struct die_info *die;
22957 struct attribute *attr;
22958 struct dwarf2_locexpr_baton retval;
22959 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22960 struct objfile *objfile = dwarf2_per_objfile->objfile;
22961
22962 if (per_cu->cu == NULL)
22963 load_cu (per_cu, false);
22964 cu = per_cu->cu;
22965 if (cu == NULL)
22966 {
22967 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22968 Instead just throw an error, not much else we can do. */
22969 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22970 sect_offset_str (sect_off), objfile_name (objfile));
22971 }
22972
22973 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22974 if (!die)
22975 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22976 sect_offset_str (sect_off), objfile_name (objfile));
22977
22978 attr = dwarf2_attr (die, DW_AT_location, cu);
22979 if (!attr)
22980 {
22981 /* DWARF: "If there is no such attribute, then there is no effect.".
22982 DATA is ignored if SIZE is 0. */
22983
22984 retval.data = NULL;
22985 retval.size = 0;
22986 }
22987 else if (attr_form_is_section_offset (attr))
22988 {
22989 struct dwarf2_loclist_baton loclist_baton;
22990 CORE_ADDR pc = (*get_frame_pc) (baton);
22991 size_t size;
22992
22993 fill_in_loclist_baton (cu, &loclist_baton, attr);
22994
22995 retval.data = dwarf2_find_location_expression (&loclist_baton,
22996 &size, pc);
22997 retval.size = size;
22998 }
22999 else
23000 {
23001 if (!attr_form_is_block (attr))
23002 error (_("Dwarf Error: DIE at %s referenced in module %s "
23003 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23004 sect_offset_str (sect_off), objfile_name (objfile));
23005
23006 retval.data = DW_BLOCK (attr)->data;
23007 retval.size = DW_BLOCK (attr)->size;
23008 }
23009 retval.per_cu = cu->per_cu;
23010
23011 age_cached_comp_units (dwarf2_per_objfile);
23012
23013 return retval;
23014 }
23015
23016 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23017 offset. */
23018
23019 struct dwarf2_locexpr_baton
23020 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23021 struct dwarf2_per_cu_data *per_cu,
23022 CORE_ADDR (*get_frame_pc) (void *baton),
23023 void *baton)
23024 {
23025 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23026
23027 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23028 }
23029
23030 /* Write a constant of a given type as target-ordered bytes into
23031 OBSTACK. */
23032
23033 static const gdb_byte *
23034 write_constant_as_bytes (struct obstack *obstack,
23035 enum bfd_endian byte_order,
23036 struct type *type,
23037 ULONGEST value,
23038 LONGEST *len)
23039 {
23040 gdb_byte *result;
23041
23042 *len = TYPE_LENGTH (type);
23043 result = (gdb_byte *) obstack_alloc (obstack, *len);
23044 store_unsigned_integer (result, *len, byte_order, value);
23045
23046 return result;
23047 }
23048
23049 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23050 pointer to the constant bytes and set LEN to the length of the
23051 data. If memory is needed, allocate it on OBSTACK. If the DIE
23052 does not have a DW_AT_const_value, return NULL. */
23053
23054 const gdb_byte *
23055 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23056 struct dwarf2_per_cu_data *per_cu,
23057 struct obstack *obstack,
23058 LONGEST *len)
23059 {
23060 struct dwarf2_cu *cu;
23061 struct die_info *die;
23062 struct attribute *attr;
23063 const gdb_byte *result = NULL;
23064 struct type *type;
23065 LONGEST value;
23066 enum bfd_endian byte_order;
23067 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23068
23069 if (per_cu->cu == NULL)
23070 load_cu (per_cu, false);
23071 cu = per_cu->cu;
23072 if (cu == NULL)
23073 {
23074 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23075 Instead just throw an error, not much else we can do. */
23076 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23077 sect_offset_str (sect_off), objfile_name (objfile));
23078 }
23079
23080 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23081 if (!die)
23082 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23083 sect_offset_str (sect_off), objfile_name (objfile));
23084
23085 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23086 if (attr == NULL)
23087 return NULL;
23088
23089 byte_order = (bfd_big_endian (objfile->obfd)
23090 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23091
23092 switch (attr->form)
23093 {
23094 case DW_FORM_addr:
23095 case DW_FORM_GNU_addr_index:
23096 {
23097 gdb_byte *tem;
23098
23099 *len = cu->header.addr_size;
23100 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23101 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23102 result = tem;
23103 }
23104 break;
23105 case DW_FORM_string:
23106 case DW_FORM_strp:
23107 case DW_FORM_GNU_str_index:
23108 case DW_FORM_GNU_strp_alt:
23109 /* DW_STRING is already allocated on the objfile obstack, point
23110 directly to it. */
23111 result = (const gdb_byte *) DW_STRING (attr);
23112 *len = strlen (DW_STRING (attr));
23113 break;
23114 case DW_FORM_block1:
23115 case DW_FORM_block2:
23116 case DW_FORM_block4:
23117 case DW_FORM_block:
23118 case DW_FORM_exprloc:
23119 case DW_FORM_data16:
23120 result = DW_BLOCK (attr)->data;
23121 *len = DW_BLOCK (attr)->size;
23122 break;
23123
23124 /* The DW_AT_const_value attributes are supposed to carry the
23125 symbol's value "represented as it would be on the target
23126 architecture." By the time we get here, it's already been
23127 converted to host endianness, so we just need to sign- or
23128 zero-extend it as appropriate. */
23129 case DW_FORM_data1:
23130 type = die_type (die, cu);
23131 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23132 if (result == NULL)
23133 result = write_constant_as_bytes (obstack, byte_order,
23134 type, value, len);
23135 break;
23136 case DW_FORM_data2:
23137 type = die_type (die, cu);
23138 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23139 if (result == NULL)
23140 result = write_constant_as_bytes (obstack, byte_order,
23141 type, value, len);
23142 break;
23143 case DW_FORM_data4:
23144 type = die_type (die, cu);
23145 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23146 if (result == NULL)
23147 result = write_constant_as_bytes (obstack, byte_order,
23148 type, value, len);
23149 break;
23150 case DW_FORM_data8:
23151 type = die_type (die, cu);
23152 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23153 if (result == NULL)
23154 result = write_constant_as_bytes (obstack, byte_order,
23155 type, value, len);
23156 break;
23157
23158 case DW_FORM_sdata:
23159 case DW_FORM_implicit_const:
23160 type = die_type (die, cu);
23161 result = write_constant_as_bytes (obstack, byte_order,
23162 type, DW_SND (attr), len);
23163 break;
23164
23165 case DW_FORM_udata:
23166 type = die_type (die, cu);
23167 result = write_constant_as_bytes (obstack, byte_order,
23168 type, DW_UNSND (attr), len);
23169 break;
23170
23171 default:
23172 complaint (&symfile_complaints,
23173 _("unsupported const value attribute form: '%s'"),
23174 dwarf_form_name (attr->form));
23175 break;
23176 }
23177
23178 return result;
23179 }
23180
23181 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23182 valid type for this die is found. */
23183
23184 struct type *
23185 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23186 struct dwarf2_per_cu_data *per_cu)
23187 {
23188 struct dwarf2_cu *cu;
23189 struct die_info *die;
23190
23191 if (per_cu->cu == NULL)
23192 load_cu (per_cu, false);
23193 cu = per_cu->cu;
23194 if (!cu)
23195 return NULL;
23196
23197 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23198 if (!die)
23199 return NULL;
23200
23201 return die_type (die, cu);
23202 }
23203
23204 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23205 PER_CU. */
23206
23207 struct type *
23208 dwarf2_get_die_type (cu_offset die_offset,
23209 struct dwarf2_per_cu_data *per_cu)
23210 {
23211 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23212 return get_die_type_at_offset (die_offset_sect, per_cu);
23213 }
23214
23215 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23216 On entry *REF_CU is the CU of SRC_DIE.
23217 On exit *REF_CU is the CU of the result.
23218 Returns NULL if the referenced DIE isn't found. */
23219
23220 static struct die_info *
23221 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23222 struct dwarf2_cu **ref_cu)
23223 {
23224 struct die_info temp_die;
23225 struct dwarf2_cu *sig_cu;
23226 struct die_info *die;
23227
23228 /* While it might be nice to assert sig_type->type == NULL here,
23229 we can get here for DW_AT_imported_declaration where we need
23230 the DIE not the type. */
23231
23232 /* If necessary, add it to the queue and load its DIEs. */
23233
23234 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23235 read_signatured_type (sig_type);
23236
23237 sig_cu = sig_type->per_cu.cu;
23238 gdb_assert (sig_cu != NULL);
23239 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23240 temp_die.sect_off = sig_type->type_offset_in_section;
23241 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23242 to_underlying (temp_die.sect_off));
23243 if (die)
23244 {
23245 struct dwarf2_per_objfile *dwarf2_per_objfile
23246 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23247
23248 /* For .gdb_index version 7 keep track of included TUs.
23249 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23250 if (dwarf2_per_objfile->index_table != NULL
23251 && dwarf2_per_objfile->index_table->version <= 7)
23252 {
23253 VEC_safe_push (dwarf2_per_cu_ptr,
23254 (*ref_cu)->per_cu->imported_symtabs,
23255 sig_cu->per_cu);
23256 }
23257
23258 *ref_cu = sig_cu;
23259 return die;
23260 }
23261
23262 return NULL;
23263 }
23264
23265 /* Follow signatured type referenced by ATTR in SRC_DIE.
23266 On entry *REF_CU is the CU of SRC_DIE.
23267 On exit *REF_CU is the CU of the result.
23268 The result is the DIE of the type.
23269 If the referenced type cannot be found an error is thrown. */
23270
23271 static struct die_info *
23272 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23273 struct dwarf2_cu **ref_cu)
23274 {
23275 ULONGEST signature = DW_SIGNATURE (attr);
23276 struct signatured_type *sig_type;
23277 struct die_info *die;
23278
23279 gdb_assert (attr->form == DW_FORM_ref_sig8);
23280
23281 sig_type = lookup_signatured_type (*ref_cu, signature);
23282 /* sig_type will be NULL if the signatured type is missing from
23283 the debug info. */
23284 if (sig_type == NULL)
23285 {
23286 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23287 " from DIE at %s [in module %s]"),
23288 hex_string (signature), sect_offset_str (src_die->sect_off),
23289 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23290 }
23291
23292 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23293 if (die == NULL)
23294 {
23295 dump_die_for_error (src_die);
23296 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23297 " from DIE at %s [in module %s]"),
23298 hex_string (signature), sect_offset_str (src_die->sect_off),
23299 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23300 }
23301
23302 return die;
23303 }
23304
23305 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23306 reading in and processing the type unit if necessary. */
23307
23308 static struct type *
23309 get_signatured_type (struct die_info *die, ULONGEST signature,
23310 struct dwarf2_cu *cu)
23311 {
23312 struct dwarf2_per_objfile *dwarf2_per_objfile
23313 = cu->per_cu->dwarf2_per_objfile;
23314 struct signatured_type *sig_type;
23315 struct dwarf2_cu *type_cu;
23316 struct die_info *type_die;
23317 struct type *type;
23318
23319 sig_type = lookup_signatured_type (cu, signature);
23320 /* sig_type will be NULL if the signatured type is missing from
23321 the debug info. */
23322 if (sig_type == NULL)
23323 {
23324 complaint (&symfile_complaints,
23325 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23326 " from DIE at %s [in module %s]"),
23327 hex_string (signature), sect_offset_str (die->sect_off),
23328 objfile_name (dwarf2_per_objfile->objfile));
23329 return build_error_marker_type (cu, die);
23330 }
23331
23332 /* If we already know the type we're done. */
23333 if (sig_type->type != NULL)
23334 return sig_type->type;
23335
23336 type_cu = cu;
23337 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23338 if (type_die != NULL)
23339 {
23340 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23341 is created. This is important, for example, because for c++ classes
23342 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23343 type = read_type_die (type_die, type_cu);
23344 if (type == NULL)
23345 {
23346 complaint (&symfile_complaints,
23347 _("Dwarf Error: Cannot build signatured type %s"
23348 " referenced from DIE at %s [in module %s]"),
23349 hex_string (signature), sect_offset_str (die->sect_off),
23350 objfile_name (dwarf2_per_objfile->objfile));
23351 type = build_error_marker_type (cu, die);
23352 }
23353 }
23354 else
23355 {
23356 complaint (&symfile_complaints,
23357 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23358 " from DIE at %s [in module %s]"),
23359 hex_string (signature), sect_offset_str (die->sect_off),
23360 objfile_name (dwarf2_per_objfile->objfile));
23361 type = build_error_marker_type (cu, die);
23362 }
23363 sig_type->type = type;
23364
23365 return type;
23366 }
23367
23368 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23369 reading in and processing the type unit if necessary. */
23370
23371 static struct type *
23372 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23373 struct dwarf2_cu *cu) /* ARI: editCase function */
23374 {
23375 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23376 if (attr_form_is_ref (attr))
23377 {
23378 struct dwarf2_cu *type_cu = cu;
23379 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23380
23381 return read_type_die (type_die, type_cu);
23382 }
23383 else if (attr->form == DW_FORM_ref_sig8)
23384 {
23385 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23386 }
23387 else
23388 {
23389 struct dwarf2_per_objfile *dwarf2_per_objfile
23390 = cu->per_cu->dwarf2_per_objfile;
23391
23392 complaint (&symfile_complaints,
23393 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23394 " at %s [in module %s]"),
23395 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23396 objfile_name (dwarf2_per_objfile->objfile));
23397 return build_error_marker_type (cu, die);
23398 }
23399 }
23400
23401 /* Load the DIEs associated with type unit PER_CU into memory. */
23402
23403 static void
23404 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23405 {
23406 struct signatured_type *sig_type;
23407
23408 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23409 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23410
23411 /* We have the per_cu, but we need the signatured_type.
23412 Fortunately this is an easy translation. */
23413 gdb_assert (per_cu->is_debug_types);
23414 sig_type = (struct signatured_type *) per_cu;
23415
23416 gdb_assert (per_cu->cu == NULL);
23417
23418 read_signatured_type (sig_type);
23419
23420 gdb_assert (per_cu->cu != NULL);
23421 }
23422
23423 /* die_reader_func for read_signatured_type.
23424 This is identical to load_full_comp_unit_reader,
23425 but is kept separate for now. */
23426
23427 static void
23428 read_signatured_type_reader (const struct die_reader_specs *reader,
23429 const gdb_byte *info_ptr,
23430 struct die_info *comp_unit_die,
23431 int has_children,
23432 void *data)
23433 {
23434 struct dwarf2_cu *cu = reader->cu;
23435
23436 gdb_assert (cu->die_hash == NULL);
23437 cu->die_hash =
23438 htab_create_alloc_ex (cu->header.length / 12,
23439 die_hash,
23440 die_eq,
23441 NULL,
23442 &cu->comp_unit_obstack,
23443 hashtab_obstack_allocate,
23444 dummy_obstack_deallocate);
23445
23446 if (has_children)
23447 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23448 &info_ptr, comp_unit_die);
23449 cu->dies = comp_unit_die;
23450 /* comp_unit_die is not stored in die_hash, no need. */
23451
23452 /* We try not to read any attributes in this function, because not
23453 all CUs needed for references have been loaded yet, and symbol
23454 table processing isn't initialized. But we have to set the CU language,
23455 or we won't be able to build types correctly.
23456 Similarly, if we do not read the producer, we can not apply
23457 producer-specific interpretation. */
23458 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23459 }
23460
23461 /* Read in a signatured type and build its CU and DIEs.
23462 If the type is a stub for the real type in a DWO file,
23463 read in the real type from the DWO file as well. */
23464
23465 static void
23466 read_signatured_type (struct signatured_type *sig_type)
23467 {
23468 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23469
23470 gdb_assert (per_cu->is_debug_types);
23471 gdb_assert (per_cu->cu == NULL);
23472
23473 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
23474 read_signatured_type_reader, NULL);
23475 sig_type->per_cu.tu_read = 1;
23476 }
23477
23478 /* Decode simple location descriptions.
23479 Given a pointer to a dwarf block that defines a location, compute
23480 the location and return the value.
23481
23482 NOTE drow/2003-11-18: This function is called in two situations
23483 now: for the address of static or global variables (partial symbols
23484 only) and for offsets into structures which are expected to be
23485 (more or less) constant. The partial symbol case should go away,
23486 and only the constant case should remain. That will let this
23487 function complain more accurately. A few special modes are allowed
23488 without complaint for global variables (for instance, global
23489 register values and thread-local values).
23490
23491 A location description containing no operations indicates that the
23492 object is optimized out. The return value is 0 for that case.
23493 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23494 callers will only want a very basic result and this can become a
23495 complaint.
23496
23497 Note that stack[0] is unused except as a default error return. */
23498
23499 static CORE_ADDR
23500 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23501 {
23502 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23503 size_t i;
23504 size_t size = blk->size;
23505 const gdb_byte *data = blk->data;
23506 CORE_ADDR stack[64];
23507 int stacki;
23508 unsigned int bytes_read, unsnd;
23509 gdb_byte op;
23510
23511 i = 0;
23512 stacki = 0;
23513 stack[stacki] = 0;
23514 stack[++stacki] = 0;
23515
23516 while (i < size)
23517 {
23518 op = data[i++];
23519 switch (op)
23520 {
23521 case DW_OP_lit0:
23522 case DW_OP_lit1:
23523 case DW_OP_lit2:
23524 case DW_OP_lit3:
23525 case DW_OP_lit4:
23526 case DW_OP_lit5:
23527 case DW_OP_lit6:
23528 case DW_OP_lit7:
23529 case DW_OP_lit8:
23530 case DW_OP_lit9:
23531 case DW_OP_lit10:
23532 case DW_OP_lit11:
23533 case DW_OP_lit12:
23534 case DW_OP_lit13:
23535 case DW_OP_lit14:
23536 case DW_OP_lit15:
23537 case DW_OP_lit16:
23538 case DW_OP_lit17:
23539 case DW_OP_lit18:
23540 case DW_OP_lit19:
23541 case DW_OP_lit20:
23542 case DW_OP_lit21:
23543 case DW_OP_lit22:
23544 case DW_OP_lit23:
23545 case DW_OP_lit24:
23546 case DW_OP_lit25:
23547 case DW_OP_lit26:
23548 case DW_OP_lit27:
23549 case DW_OP_lit28:
23550 case DW_OP_lit29:
23551 case DW_OP_lit30:
23552 case DW_OP_lit31:
23553 stack[++stacki] = op - DW_OP_lit0;
23554 break;
23555
23556 case DW_OP_reg0:
23557 case DW_OP_reg1:
23558 case DW_OP_reg2:
23559 case DW_OP_reg3:
23560 case DW_OP_reg4:
23561 case DW_OP_reg5:
23562 case DW_OP_reg6:
23563 case DW_OP_reg7:
23564 case DW_OP_reg8:
23565 case DW_OP_reg9:
23566 case DW_OP_reg10:
23567 case DW_OP_reg11:
23568 case DW_OP_reg12:
23569 case DW_OP_reg13:
23570 case DW_OP_reg14:
23571 case DW_OP_reg15:
23572 case DW_OP_reg16:
23573 case DW_OP_reg17:
23574 case DW_OP_reg18:
23575 case DW_OP_reg19:
23576 case DW_OP_reg20:
23577 case DW_OP_reg21:
23578 case DW_OP_reg22:
23579 case DW_OP_reg23:
23580 case DW_OP_reg24:
23581 case DW_OP_reg25:
23582 case DW_OP_reg26:
23583 case DW_OP_reg27:
23584 case DW_OP_reg28:
23585 case DW_OP_reg29:
23586 case DW_OP_reg30:
23587 case DW_OP_reg31:
23588 stack[++stacki] = op - DW_OP_reg0;
23589 if (i < size)
23590 dwarf2_complex_location_expr_complaint ();
23591 break;
23592
23593 case DW_OP_regx:
23594 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23595 i += bytes_read;
23596 stack[++stacki] = unsnd;
23597 if (i < size)
23598 dwarf2_complex_location_expr_complaint ();
23599 break;
23600
23601 case DW_OP_addr:
23602 stack[++stacki] = read_address (objfile->obfd, &data[i],
23603 cu, &bytes_read);
23604 i += bytes_read;
23605 break;
23606
23607 case DW_OP_const1u:
23608 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23609 i += 1;
23610 break;
23611
23612 case DW_OP_const1s:
23613 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23614 i += 1;
23615 break;
23616
23617 case DW_OP_const2u:
23618 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23619 i += 2;
23620 break;
23621
23622 case DW_OP_const2s:
23623 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23624 i += 2;
23625 break;
23626
23627 case DW_OP_const4u:
23628 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23629 i += 4;
23630 break;
23631
23632 case DW_OP_const4s:
23633 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23634 i += 4;
23635 break;
23636
23637 case DW_OP_const8u:
23638 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23639 i += 8;
23640 break;
23641
23642 case DW_OP_constu:
23643 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23644 &bytes_read);
23645 i += bytes_read;
23646 break;
23647
23648 case DW_OP_consts:
23649 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23650 i += bytes_read;
23651 break;
23652
23653 case DW_OP_dup:
23654 stack[stacki + 1] = stack[stacki];
23655 stacki++;
23656 break;
23657
23658 case DW_OP_plus:
23659 stack[stacki - 1] += stack[stacki];
23660 stacki--;
23661 break;
23662
23663 case DW_OP_plus_uconst:
23664 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23665 &bytes_read);
23666 i += bytes_read;
23667 break;
23668
23669 case DW_OP_minus:
23670 stack[stacki - 1] -= stack[stacki];
23671 stacki--;
23672 break;
23673
23674 case DW_OP_deref:
23675 /* If we're not the last op, then we definitely can't encode
23676 this using GDB's address_class enum. This is valid for partial
23677 global symbols, although the variable's address will be bogus
23678 in the psymtab. */
23679 if (i < size)
23680 dwarf2_complex_location_expr_complaint ();
23681 break;
23682
23683 case DW_OP_GNU_push_tls_address:
23684 case DW_OP_form_tls_address:
23685 /* The top of the stack has the offset from the beginning
23686 of the thread control block at which the variable is located. */
23687 /* Nothing should follow this operator, so the top of stack would
23688 be returned. */
23689 /* This is valid for partial global symbols, but the variable's
23690 address will be bogus in the psymtab. Make it always at least
23691 non-zero to not look as a variable garbage collected by linker
23692 which have DW_OP_addr 0. */
23693 if (i < size)
23694 dwarf2_complex_location_expr_complaint ();
23695 stack[stacki]++;
23696 break;
23697
23698 case DW_OP_GNU_uninit:
23699 break;
23700
23701 case DW_OP_GNU_addr_index:
23702 case DW_OP_GNU_const_index:
23703 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23704 &bytes_read);
23705 i += bytes_read;
23706 break;
23707
23708 default:
23709 {
23710 const char *name = get_DW_OP_name (op);
23711
23712 if (name)
23713 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23714 name);
23715 else
23716 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23717 op);
23718 }
23719
23720 return (stack[stacki]);
23721 }
23722
23723 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23724 outside of the allocated space. Also enforce minimum>0. */
23725 if (stacki >= ARRAY_SIZE (stack) - 1)
23726 {
23727 complaint (&symfile_complaints,
23728 _("location description stack overflow"));
23729 return 0;
23730 }
23731
23732 if (stacki <= 0)
23733 {
23734 complaint (&symfile_complaints,
23735 _("location description stack underflow"));
23736 return 0;
23737 }
23738 }
23739 return (stack[stacki]);
23740 }
23741
23742 /* memory allocation interface */
23743
23744 static struct dwarf_block *
23745 dwarf_alloc_block (struct dwarf2_cu *cu)
23746 {
23747 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23748 }
23749
23750 static struct die_info *
23751 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23752 {
23753 struct die_info *die;
23754 size_t size = sizeof (struct die_info);
23755
23756 if (num_attrs > 1)
23757 size += (num_attrs - 1) * sizeof (struct attribute);
23758
23759 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23760 memset (die, 0, sizeof (struct die_info));
23761 return (die);
23762 }
23763
23764 \f
23765 /* Macro support. */
23766
23767 /* Return file name relative to the compilation directory of file number I in
23768 *LH's file name table. The result is allocated using xmalloc; the caller is
23769 responsible for freeing it. */
23770
23771 static char *
23772 file_file_name (int file, struct line_header *lh)
23773 {
23774 /* Is the file number a valid index into the line header's file name
23775 table? Remember that file numbers start with one, not zero. */
23776 if (1 <= file && file <= lh->file_names.size ())
23777 {
23778 const file_entry &fe = lh->file_names[file - 1];
23779
23780 if (!IS_ABSOLUTE_PATH (fe.name))
23781 {
23782 const char *dir = fe.include_dir (lh);
23783 if (dir != NULL)
23784 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23785 }
23786 return xstrdup (fe.name);
23787 }
23788 else
23789 {
23790 /* The compiler produced a bogus file number. We can at least
23791 record the macro definitions made in the file, even if we
23792 won't be able to find the file by name. */
23793 char fake_name[80];
23794
23795 xsnprintf (fake_name, sizeof (fake_name),
23796 "<bad macro file number %d>", file);
23797
23798 complaint (&symfile_complaints,
23799 _("bad file number in macro information (%d)"),
23800 file);
23801
23802 return xstrdup (fake_name);
23803 }
23804 }
23805
23806 /* Return the full name of file number I in *LH's file name table.
23807 Use COMP_DIR as the name of the current directory of the
23808 compilation. The result is allocated using xmalloc; the caller is
23809 responsible for freeing it. */
23810 static char *
23811 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23812 {
23813 /* Is the file number a valid index into the line header's file name
23814 table? Remember that file numbers start with one, not zero. */
23815 if (1 <= file && file <= lh->file_names.size ())
23816 {
23817 char *relative = file_file_name (file, lh);
23818
23819 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23820 return relative;
23821 return reconcat (relative, comp_dir, SLASH_STRING,
23822 relative, (char *) NULL);
23823 }
23824 else
23825 return file_file_name (file, lh);
23826 }
23827
23828
23829 static struct macro_source_file *
23830 macro_start_file (int file, int line,
23831 struct macro_source_file *current_file,
23832 struct line_header *lh)
23833 {
23834 /* File name relative to the compilation directory of this source file. */
23835 char *file_name = file_file_name (file, lh);
23836
23837 if (! current_file)
23838 {
23839 /* Note: We don't create a macro table for this compilation unit
23840 at all until we actually get a filename. */
23841 struct macro_table *macro_table = get_macro_table ();
23842
23843 /* If we have no current file, then this must be the start_file
23844 directive for the compilation unit's main source file. */
23845 current_file = macro_set_main (macro_table, file_name);
23846 macro_define_special (macro_table);
23847 }
23848 else
23849 current_file = macro_include (current_file, line, file_name);
23850
23851 xfree (file_name);
23852
23853 return current_file;
23854 }
23855
23856 static const char *
23857 consume_improper_spaces (const char *p, const char *body)
23858 {
23859 if (*p == ' ')
23860 {
23861 complaint (&symfile_complaints,
23862 _("macro definition contains spaces "
23863 "in formal argument list:\n`%s'"),
23864 body);
23865
23866 while (*p == ' ')
23867 p++;
23868 }
23869
23870 return p;
23871 }
23872
23873
23874 static void
23875 parse_macro_definition (struct macro_source_file *file, int line,
23876 const char *body)
23877 {
23878 const char *p;
23879
23880 /* The body string takes one of two forms. For object-like macro
23881 definitions, it should be:
23882
23883 <macro name> " " <definition>
23884
23885 For function-like macro definitions, it should be:
23886
23887 <macro name> "() " <definition>
23888 or
23889 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23890
23891 Spaces may appear only where explicitly indicated, and in the
23892 <definition>.
23893
23894 The Dwarf 2 spec says that an object-like macro's name is always
23895 followed by a space, but versions of GCC around March 2002 omit
23896 the space when the macro's definition is the empty string.
23897
23898 The Dwarf 2 spec says that there should be no spaces between the
23899 formal arguments in a function-like macro's formal argument list,
23900 but versions of GCC around March 2002 include spaces after the
23901 commas. */
23902
23903
23904 /* Find the extent of the macro name. The macro name is terminated
23905 by either a space or null character (for an object-like macro) or
23906 an opening paren (for a function-like macro). */
23907 for (p = body; *p; p++)
23908 if (*p == ' ' || *p == '(')
23909 break;
23910
23911 if (*p == ' ' || *p == '\0')
23912 {
23913 /* It's an object-like macro. */
23914 int name_len = p - body;
23915 char *name = savestring (body, name_len);
23916 const char *replacement;
23917
23918 if (*p == ' ')
23919 replacement = body + name_len + 1;
23920 else
23921 {
23922 dwarf2_macro_malformed_definition_complaint (body);
23923 replacement = body + name_len;
23924 }
23925
23926 macro_define_object (file, line, name, replacement);
23927
23928 xfree (name);
23929 }
23930 else if (*p == '(')
23931 {
23932 /* It's a function-like macro. */
23933 char *name = savestring (body, p - body);
23934 int argc = 0;
23935 int argv_size = 1;
23936 char **argv = XNEWVEC (char *, argv_size);
23937
23938 p++;
23939
23940 p = consume_improper_spaces (p, body);
23941
23942 /* Parse the formal argument list. */
23943 while (*p && *p != ')')
23944 {
23945 /* Find the extent of the current argument name. */
23946 const char *arg_start = p;
23947
23948 while (*p && *p != ',' && *p != ')' && *p != ' ')
23949 p++;
23950
23951 if (! *p || p == arg_start)
23952 dwarf2_macro_malformed_definition_complaint (body);
23953 else
23954 {
23955 /* Make sure argv has room for the new argument. */
23956 if (argc >= argv_size)
23957 {
23958 argv_size *= 2;
23959 argv = XRESIZEVEC (char *, argv, argv_size);
23960 }
23961
23962 argv[argc++] = savestring (arg_start, p - arg_start);
23963 }
23964
23965 p = consume_improper_spaces (p, body);
23966
23967 /* Consume the comma, if present. */
23968 if (*p == ',')
23969 {
23970 p++;
23971
23972 p = consume_improper_spaces (p, body);
23973 }
23974 }
23975
23976 if (*p == ')')
23977 {
23978 p++;
23979
23980 if (*p == ' ')
23981 /* Perfectly formed definition, no complaints. */
23982 macro_define_function (file, line, name,
23983 argc, (const char **) argv,
23984 p + 1);
23985 else if (*p == '\0')
23986 {
23987 /* Complain, but do define it. */
23988 dwarf2_macro_malformed_definition_complaint (body);
23989 macro_define_function (file, line, name,
23990 argc, (const char **) argv,
23991 p);
23992 }
23993 else
23994 /* Just complain. */
23995 dwarf2_macro_malformed_definition_complaint (body);
23996 }
23997 else
23998 /* Just complain. */
23999 dwarf2_macro_malformed_definition_complaint (body);
24000
24001 xfree (name);
24002 {
24003 int i;
24004
24005 for (i = 0; i < argc; i++)
24006 xfree (argv[i]);
24007 }
24008 xfree (argv);
24009 }
24010 else
24011 dwarf2_macro_malformed_definition_complaint (body);
24012 }
24013
24014 /* Skip some bytes from BYTES according to the form given in FORM.
24015 Returns the new pointer. */
24016
24017 static const gdb_byte *
24018 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24019 enum dwarf_form form,
24020 unsigned int offset_size,
24021 struct dwarf2_section_info *section)
24022 {
24023 unsigned int bytes_read;
24024
24025 switch (form)
24026 {
24027 case DW_FORM_data1:
24028 case DW_FORM_flag:
24029 ++bytes;
24030 break;
24031
24032 case DW_FORM_data2:
24033 bytes += 2;
24034 break;
24035
24036 case DW_FORM_data4:
24037 bytes += 4;
24038 break;
24039
24040 case DW_FORM_data8:
24041 bytes += 8;
24042 break;
24043
24044 case DW_FORM_data16:
24045 bytes += 16;
24046 break;
24047
24048 case DW_FORM_string:
24049 read_direct_string (abfd, bytes, &bytes_read);
24050 bytes += bytes_read;
24051 break;
24052
24053 case DW_FORM_sec_offset:
24054 case DW_FORM_strp:
24055 case DW_FORM_GNU_strp_alt:
24056 bytes += offset_size;
24057 break;
24058
24059 case DW_FORM_block:
24060 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24061 bytes += bytes_read;
24062 break;
24063
24064 case DW_FORM_block1:
24065 bytes += 1 + read_1_byte (abfd, bytes);
24066 break;
24067 case DW_FORM_block2:
24068 bytes += 2 + read_2_bytes (abfd, bytes);
24069 break;
24070 case DW_FORM_block4:
24071 bytes += 4 + read_4_bytes (abfd, bytes);
24072 break;
24073
24074 case DW_FORM_sdata:
24075 case DW_FORM_udata:
24076 case DW_FORM_GNU_addr_index:
24077 case DW_FORM_GNU_str_index:
24078 bytes = gdb_skip_leb128 (bytes, buffer_end);
24079 if (bytes == NULL)
24080 {
24081 dwarf2_section_buffer_overflow_complaint (section);
24082 return NULL;
24083 }
24084 break;
24085
24086 case DW_FORM_implicit_const:
24087 break;
24088
24089 default:
24090 {
24091 complaint (&symfile_complaints,
24092 _("invalid form 0x%x in `%s'"),
24093 form, get_section_name (section));
24094 return NULL;
24095 }
24096 }
24097
24098 return bytes;
24099 }
24100
24101 /* A helper for dwarf_decode_macros that handles skipping an unknown
24102 opcode. Returns an updated pointer to the macro data buffer; or,
24103 on error, issues a complaint and returns NULL. */
24104
24105 static const gdb_byte *
24106 skip_unknown_opcode (unsigned int opcode,
24107 const gdb_byte **opcode_definitions,
24108 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24109 bfd *abfd,
24110 unsigned int offset_size,
24111 struct dwarf2_section_info *section)
24112 {
24113 unsigned int bytes_read, i;
24114 unsigned long arg;
24115 const gdb_byte *defn;
24116
24117 if (opcode_definitions[opcode] == NULL)
24118 {
24119 complaint (&symfile_complaints,
24120 _("unrecognized DW_MACFINO opcode 0x%x"),
24121 opcode);
24122 return NULL;
24123 }
24124
24125 defn = opcode_definitions[opcode];
24126 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24127 defn += bytes_read;
24128
24129 for (i = 0; i < arg; ++i)
24130 {
24131 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24132 (enum dwarf_form) defn[i], offset_size,
24133 section);
24134 if (mac_ptr == NULL)
24135 {
24136 /* skip_form_bytes already issued the complaint. */
24137 return NULL;
24138 }
24139 }
24140
24141 return mac_ptr;
24142 }
24143
24144 /* A helper function which parses the header of a macro section.
24145 If the macro section is the extended (for now called "GNU") type,
24146 then this updates *OFFSET_SIZE. Returns a pointer to just after
24147 the header, or issues a complaint and returns NULL on error. */
24148
24149 static const gdb_byte *
24150 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24151 bfd *abfd,
24152 const gdb_byte *mac_ptr,
24153 unsigned int *offset_size,
24154 int section_is_gnu)
24155 {
24156 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24157
24158 if (section_is_gnu)
24159 {
24160 unsigned int version, flags;
24161
24162 version = read_2_bytes (abfd, mac_ptr);
24163 if (version != 4 && version != 5)
24164 {
24165 complaint (&symfile_complaints,
24166 _("unrecognized version `%d' in .debug_macro section"),
24167 version);
24168 return NULL;
24169 }
24170 mac_ptr += 2;
24171
24172 flags = read_1_byte (abfd, mac_ptr);
24173 ++mac_ptr;
24174 *offset_size = (flags & 1) ? 8 : 4;
24175
24176 if ((flags & 2) != 0)
24177 /* We don't need the line table offset. */
24178 mac_ptr += *offset_size;
24179
24180 /* Vendor opcode descriptions. */
24181 if ((flags & 4) != 0)
24182 {
24183 unsigned int i, count;
24184
24185 count = read_1_byte (abfd, mac_ptr);
24186 ++mac_ptr;
24187 for (i = 0; i < count; ++i)
24188 {
24189 unsigned int opcode, bytes_read;
24190 unsigned long arg;
24191
24192 opcode = read_1_byte (abfd, mac_ptr);
24193 ++mac_ptr;
24194 opcode_definitions[opcode] = mac_ptr;
24195 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24196 mac_ptr += bytes_read;
24197 mac_ptr += arg;
24198 }
24199 }
24200 }
24201
24202 return mac_ptr;
24203 }
24204
24205 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24206 including DW_MACRO_import. */
24207
24208 static void
24209 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24210 bfd *abfd,
24211 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24212 struct macro_source_file *current_file,
24213 struct line_header *lh,
24214 struct dwarf2_section_info *section,
24215 int section_is_gnu, int section_is_dwz,
24216 unsigned int offset_size,
24217 htab_t include_hash)
24218 {
24219 struct objfile *objfile = dwarf2_per_objfile->objfile;
24220 enum dwarf_macro_record_type macinfo_type;
24221 int at_commandline;
24222 const gdb_byte *opcode_definitions[256];
24223
24224 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24225 &offset_size, section_is_gnu);
24226 if (mac_ptr == NULL)
24227 {
24228 /* We already issued a complaint. */
24229 return;
24230 }
24231
24232 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24233 GDB is still reading the definitions from command line. First
24234 DW_MACINFO_start_file will need to be ignored as it was already executed
24235 to create CURRENT_FILE for the main source holding also the command line
24236 definitions. On first met DW_MACINFO_start_file this flag is reset to
24237 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24238
24239 at_commandline = 1;
24240
24241 do
24242 {
24243 /* Do we at least have room for a macinfo type byte? */
24244 if (mac_ptr >= mac_end)
24245 {
24246 dwarf2_section_buffer_overflow_complaint (section);
24247 break;
24248 }
24249
24250 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24251 mac_ptr++;
24252
24253 /* Note that we rely on the fact that the corresponding GNU and
24254 DWARF constants are the same. */
24255 DIAGNOSTIC_PUSH
24256 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24257 switch (macinfo_type)
24258 {
24259 /* A zero macinfo type indicates the end of the macro
24260 information. */
24261 case 0:
24262 break;
24263
24264 case DW_MACRO_define:
24265 case DW_MACRO_undef:
24266 case DW_MACRO_define_strp:
24267 case DW_MACRO_undef_strp:
24268 case DW_MACRO_define_sup:
24269 case DW_MACRO_undef_sup:
24270 {
24271 unsigned int bytes_read;
24272 int line;
24273 const char *body;
24274 int is_define;
24275
24276 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24277 mac_ptr += bytes_read;
24278
24279 if (macinfo_type == DW_MACRO_define
24280 || macinfo_type == DW_MACRO_undef)
24281 {
24282 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24283 mac_ptr += bytes_read;
24284 }
24285 else
24286 {
24287 LONGEST str_offset;
24288
24289 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24290 mac_ptr += offset_size;
24291
24292 if (macinfo_type == DW_MACRO_define_sup
24293 || macinfo_type == DW_MACRO_undef_sup
24294 || section_is_dwz)
24295 {
24296 struct dwz_file *dwz
24297 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24298
24299 body = read_indirect_string_from_dwz (objfile,
24300 dwz, str_offset);
24301 }
24302 else
24303 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24304 abfd, str_offset);
24305 }
24306
24307 is_define = (macinfo_type == DW_MACRO_define
24308 || macinfo_type == DW_MACRO_define_strp
24309 || macinfo_type == DW_MACRO_define_sup);
24310 if (! current_file)
24311 {
24312 /* DWARF violation as no main source is present. */
24313 complaint (&symfile_complaints,
24314 _("debug info with no main source gives macro %s "
24315 "on line %d: %s"),
24316 is_define ? _("definition") : _("undefinition"),
24317 line, body);
24318 break;
24319 }
24320 if ((line == 0 && !at_commandline)
24321 || (line != 0 && at_commandline))
24322 complaint (&symfile_complaints,
24323 _("debug info gives %s macro %s with %s line %d: %s"),
24324 at_commandline ? _("command-line") : _("in-file"),
24325 is_define ? _("definition") : _("undefinition"),
24326 line == 0 ? _("zero") : _("non-zero"), line, body);
24327
24328 if (is_define)
24329 parse_macro_definition (current_file, line, body);
24330 else
24331 {
24332 gdb_assert (macinfo_type == DW_MACRO_undef
24333 || macinfo_type == DW_MACRO_undef_strp
24334 || macinfo_type == DW_MACRO_undef_sup);
24335 macro_undef (current_file, line, body);
24336 }
24337 }
24338 break;
24339
24340 case DW_MACRO_start_file:
24341 {
24342 unsigned int bytes_read;
24343 int line, file;
24344
24345 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24346 mac_ptr += bytes_read;
24347 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24348 mac_ptr += bytes_read;
24349
24350 if ((line == 0 && !at_commandline)
24351 || (line != 0 && at_commandline))
24352 complaint (&symfile_complaints,
24353 _("debug info gives source %d included "
24354 "from %s at %s line %d"),
24355 file, at_commandline ? _("command-line") : _("file"),
24356 line == 0 ? _("zero") : _("non-zero"), line);
24357
24358 if (at_commandline)
24359 {
24360 /* This DW_MACRO_start_file was executed in the
24361 pass one. */
24362 at_commandline = 0;
24363 }
24364 else
24365 current_file = macro_start_file (file, line, current_file, lh);
24366 }
24367 break;
24368
24369 case DW_MACRO_end_file:
24370 if (! current_file)
24371 complaint (&symfile_complaints,
24372 _("macro debug info has an unmatched "
24373 "`close_file' directive"));
24374 else
24375 {
24376 current_file = current_file->included_by;
24377 if (! current_file)
24378 {
24379 enum dwarf_macro_record_type next_type;
24380
24381 /* GCC circa March 2002 doesn't produce the zero
24382 type byte marking the end of the compilation
24383 unit. Complain if it's not there, but exit no
24384 matter what. */
24385
24386 /* Do we at least have room for a macinfo type byte? */
24387 if (mac_ptr >= mac_end)
24388 {
24389 dwarf2_section_buffer_overflow_complaint (section);
24390 return;
24391 }
24392
24393 /* We don't increment mac_ptr here, so this is just
24394 a look-ahead. */
24395 next_type
24396 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24397 mac_ptr);
24398 if (next_type != 0)
24399 complaint (&symfile_complaints,
24400 _("no terminating 0-type entry for "
24401 "macros in `.debug_macinfo' section"));
24402
24403 return;
24404 }
24405 }
24406 break;
24407
24408 case DW_MACRO_import:
24409 case DW_MACRO_import_sup:
24410 {
24411 LONGEST offset;
24412 void **slot;
24413 bfd *include_bfd = abfd;
24414 struct dwarf2_section_info *include_section = section;
24415 const gdb_byte *include_mac_end = mac_end;
24416 int is_dwz = section_is_dwz;
24417 const gdb_byte *new_mac_ptr;
24418
24419 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24420 mac_ptr += offset_size;
24421
24422 if (macinfo_type == DW_MACRO_import_sup)
24423 {
24424 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24425
24426 dwarf2_read_section (objfile, &dwz->macro);
24427
24428 include_section = &dwz->macro;
24429 include_bfd = get_section_bfd_owner (include_section);
24430 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24431 is_dwz = 1;
24432 }
24433
24434 new_mac_ptr = include_section->buffer + offset;
24435 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24436
24437 if (*slot != NULL)
24438 {
24439 /* This has actually happened; see
24440 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24441 complaint (&symfile_complaints,
24442 _("recursive DW_MACRO_import in "
24443 ".debug_macro section"));
24444 }
24445 else
24446 {
24447 *slot = (void *) new_mac_ptr;
24448
24449 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24450 include_bfd, new_mac_ptr,
24451 include_mac_end, current_file, lh,
24452 section, section_is_gnu, is_dwz,
24453 offset_size, include_hash);
24454
24455 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24456 }
24457 }
24458 break;
24459
24460 case DW_MACINFO_vendor_ext:
24461 if (!section_is_gnu)
24462 {
24463 unsigned int bytes_read;
24464
24465 /* This reads the constant, but since we don't recognize
24466 any vendor extensions, we ignore it. */
24467 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24468 mac_ptr += bytes_read;
24469 read_direct_string (abfd, mac_ptr, &bytes_read);
24470 mac_ptr += bytes_read;
24471
24472 /* We don't recognize any vendor extensions. */
24473 break;
24474 }
24475 /* FALLTHROUGH */
24476
24477 default:
24478 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24479 mac_ptr, mac_end, abfd, offset_size,
24480 section);
24481 if (mac_ptr == NULL)
24482 return;
24483 break;
24484 }
24485 DIAGNOSTIC_POP
24486 } while (macinfo_type != 0);
24487 }
24488
24489 static void
24490 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24491 int section_is_gnu)
24492 {
24493 struct dwarf2_per_objfile *dwarf2_per_objfile
24494 = cu->per_cu->dwarf2_per_objfile;
24495 struct objfile *objfile = dwarf2_per_objfile->objfile;
24496 struct line_header *lh = cu->line_header;
24497 bfd *abfd;
24498 const gdb_byte *mac_ptr, *mac_end;
24499 struct macro_source_file *current_file = 0;
24500 enum dwarf_macro_record_type macinfo_type;
24501 unsigned int offset_size = cu->header.offset_size;
24502 const gdb_byte *opcode_definitions[256];
24503 void **slot;
24504 struct dwarf2_section_info *section;
24505 const char *section_name;
24506
24507 if (cu->dwo_unit != NULL)
24508 {
24509 if (section_is_gnu)
24510 {
24511 section = &cu->dwo_unit->dwo_file->sections.macro;
24512 section_name = ".debug_macro.dwo";
24513 }
24514 else
24515 {
24516 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24517 section_name = ".debug_macinfo.dwo";
24518 }
24519 }
24520 else
24521 {
24522 if (section_is_gnu)
24523 {
24524 section = &dwarf2_per_objfile->macro;
24525 section_name = ".debug_macro";
24526 }
24527 else
24528 {
24529 section = &dwarf2_per_objfile->macinfo;
24530 section_name = ".debug_macinfo";
24531 }
24532 }
24533
24534 dwarf2_read_section (objfile, section);
24535 if (section->buffer == NULL)
24536 {
24537 complaint (&symfile_complaints, _("missing %s section"), section_name);
24538 return;
24539 }
24540 abfd = get_section_bfd_owner (section);
24541
24542 /* First pass: Find the name of the base filename.
24543 This filename is needed in order to process all macros whose definition
24544 (or undefinition) comes from the command line. These macros are defined
24545 before the first DW_MACINFO_start_file entry, and yet still need to be
24546 associated to the base file.
24547
24548 To determine the base file name, we scan the macro definitions until we
24549 reach the first DW_MACINFO_start_file entry. We then initialize
24550 CURRENT_FILE accordingly so that any macro definition found before the
24551 first DW_MACINFO_start_file can still be associated to the base file. */
24552
24553 mac_ptr = section->buffer + offset;
24554 mac_end = section->buffer + section->size;
24555
24556 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24557 &offset_size, section_is_gnu);
24558 if (mac_ptr == NULL)
24559 {
24560 /* We already issued a complaint. */
24561 return;
24562 }
24563
24564 do
24565 {
24566 /* Do we at least have room for a macinfo type byte? */
24567 if (mac_ptr >= mac_end)
24568 {
24569 /* Complaint is printed during the second pass as GDB will probably
24570 stop the first pass earlier upon finding
24571 DW_MACINFO_start_file. */
24572 break;
24573 }
24574
24575 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24576 mac_ptr++;
24577
24578 /* Note that we rely on the fact that the corresponding GNU and
24579 DWARF constants are the same. */
24580 DIAGNOSTIC_PUSH
24581 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24582 switch (macinfo_type)
24583 {
24584 /* A zero macinfo type indicates the end of the macro
24585 information. */
24586 case 0:
24587 break;
24588
24589 case DW_MACRO_define:
24590 case DW_MACRO_undef:
24591 /* Only skip the data by MAC_PTR. */
24592 {
24593 unsigned int bytes_read;
24594
24595 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24596 mac_ptr += bytes_read;
24597 read_direct_string (abfd, mac_ptr, &bytes_read);
24598 mac_ptr += bytes_read;
24599 }
24600 break;
24601
24602 case DW_MACRO_start_file:
24603 {
24604 unsigned int bytes_read;
24605 int line, file;
24606
24607 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24608 mac_ptr += bytes_read;
24609 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24610 mac_ptr += bytes_read;
24611
24612 current_file = macro_start_file (file, line, current_file, lh);
24613 }
24614 break;
24615
24616 case DW_MACRO_end_file:
24617 /* No data to skip by MAC_PTR. */
24618 break;
24619
24620 case DW_MACRO_define_strp:
24621 case DW_MACRO_undef_strp:
24622 case DW_MACRO_define_sup:
24623 case DW_MACRO_undef_sup:
24624 {
24625 unsigned int bytes_read;
24626
24627 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24628 mac_ptr += bytes_read;
24629 mac_ptr += offset_size;
24630 }
24631 break;
24632
24633 case DW_MACRO_import:
24634 case DW_MACRO_import_sup:
24635 /* Note that, according to the spec, a transparent include
24636 chain cannot call DW_MACRO_start_file. So, we can just
24637 skip this opcode. */
24638 mac_ptr += offset_size;
24639 break;
24640
24641 case DW_MACINFO_vendor_ext:
24642 /* Only skip the data by MAC_PTR. */
24643 if (!section_is_gnu)
24644 {
24645 unsigned int bytes_read;
24646
24647 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24648 mac_ptr += bytes_read;
24649 read_direct_string (abfd, mac_ptr, &bytes_read);
24650 mac_ptr += bytes_read;
24651 }
24652 /* FALLTHROUGH */
24653
24654 default:
24655 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24656 mac_ptr, mac_end, abfd, offset_size,
24657 section);
24658 if (mac_ptr == NULL)
24659 return;
24660 break;
24661 }
24662 DIAGNOSTIC_POP
24663 } while (macinfo_type != 0 && current_file == NULL);
24664
24665 /* Second pass: Process all entries.
24666
24667 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24668 command-line macro definitions/undefinitions. This flag is unset when we
24669 reach the first DW_MACINFO_start_file entry. */
24670
24671 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24672 htab_eq_pointer,
24673 NULL, xcalloc, xfree));
24674 mac_ptr = section->buffer + offset;
24675 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24676 *slot = (void *) mac_ptr;
24677 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24678 abfd, mac_ptr, mac_end,
24679 current_file, lh, section,
24680 section_is_gnu, 0, offset_size,
24681 include_hash.get ());
24682 }
24683
24684 /* Check if the attribute's form is a DW_FORM_block*
24685 if so return true else false. */
24686
24687 static int
24688 attr_form_is_block (const struct attribute *attr)
24689 {
24690 return (attr == NULL ? 0 :
24691 attr->form == DW_FORM_block1
24692 || attr->form == DW_FORM_block2
24693 || attr->form == DW_FORM_block4
24694 || attr->form == DW_FORM_block
24695 || attr->form == DW_FORM_exprloc);
24696 }
24697
24698 /* Return non-zero if ATTR's value is a section offset --- classes
24699 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24700 You may use DW_UNSND (attr) to retrieve such offsets.
24701
24702 Section 7.5.4, "Attribute Encodings", explains that no attribute
24703 may have a value that belongs to more than one of these classes; it
24704 would be ambiguous if we did, because we use the same forms for all
24705 of them. */
24706
24707 static int
24708 attr_form_is_section_offset (const struct attribute *attr)
24709 {
24710 return (attr->form == DW_FORM_data4
24711 || attr->form == DW_FORM_data8
24712 || attr->form == DW_FORM_sec_offset);
24713 }
24714
24715 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24716 zero otherwise. When this function returns true, you can apply
24717 dwarf2_get_attr_constant_value to it.
24718
24719 However, note that for some attributes you must check
24720 attr_form_is_section_offset before using this test. DW_FORM_data4
24721 and DW_FORM_data8 are members of both the constant class, and of
24722 the classes that contain offsets into other debug sections
24723 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24724 that, if an attribute's can be either a constant or one of the
24725 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24726 taken as section offsets, not constants.
24727
24728 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24729 cannot handle that. */
24730
24731 static int
24732 attr_form_is_constant (const struct attribute *attr)
24733 {
24734 switch (attr->form)
24735 {
24736 case DW_FORM_sdata:
24737 case DW_FORM_udata:
24738 case DW_FORM_data1:
24739 case DW_FORM_data2:
24740 case DW_FORM_data4:
24741 case DW_FORM_data8:
24742 case DW_FORM_implicit_const:
24743 return 1;
24744 default:
24745 return 0;
24746 }
24747 }
24748
24749
24750 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24751 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24752
24753 static int
24754 attr_form_is_ref (const struct attribute *attr)
24755 {
24756 switch (attr->form)
24757 {
24758 case DW_FORM_ref_addr:
24759 case DW_FORM_ref1:
24760 case DW_FORM_ref2:
24761 case DW_FORM_ref4:
24762 case DW_FORM_ref8:
24763 case DW_FORM_ref_udata:
24764 case DW_FORM_GNU_ref_alt:
24765 return 1;
24766 default:
24767 return 0;
24768 }
24769 }
24770
24771 /* Return the .debug_loc section to use for CU.
24772 For DWO files use .debug_loc.dwo. */
24773
24774 static struct dwarf2_section_info *
24775 cu_debug_loc_section (struct dwarf2_cu *cu)
24776 {
24777 struct dwarf2_per_objfile *dwarf2_per_objfile
24778 = cu->per_cu->dwarf2_per_objfile;
24779
24780 if (cu->dwo_unit)
24781 {
24782 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24783
24784 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24785 }
24786 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24787 : &dwarf2_per_objfile->loc);
24788 }
24789
24790 /* A helper function that fills in a dwarf2_loclist_baton. */
24791
24792 static void
24793 fill_in_loclist_baton (struct dwarf2_cu *cu,
24794 struct dwarf2_loclist_baton *baton,
24795 const struct attribute *attr)
24796 {
24797 struct dwarf2_per_objfile *dwarf2_per_objfile
24798 = cu->per_cu->dwarf2_per_objfile;
24799 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24800
24801 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24802
24803 baton->per_cu = cu->per_cu;
24804 gdb_assert (baton->per_cu);
24805 /* We don't know how long the location list is, but make sure we
24806 don't run off the edge of the section. */
24807 baton->size = section->size - DW_UNSND (attr);
24808 baton->data = section->buffer + DW_UNSND (attr);
24809 baton->base_address = cu->base_address;
24810 baton->from_dwo = cu->dwo_unit != NULL;
24811 }
24812
24813 static void
24814 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24815 struct dwarf2_cu *cu, int is_block)
24816 {
24817 struct dwarf2_per_objfile *dwarf2_per_objfile
24818 = cu->per_cu->dwarf2_per_objfile;
24819 struct objfile *objfile = dwarf2_per_objfile->objfile;
24820 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24821
24822 if (attr_form_is_section_offset (attr)
24823 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24824 the section. If so, fall through to the complaint in the
24825 other branch. */
24826 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24827 {
24828 struct dwarf2_loclist_baton *baton;
24829
24830 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24831
24832 fill_in_loclist_baton (cu, baton, attr);
24833
24834 if (cu->base_known == 0)
24835 complaint (&symfile_complaints,
24836 _("Location list used without "
24837 "specifying the CU base address."));
24838
24839 SYMBOL_ACLASS_INDEX (sym) = (is_block
24840 ? dwarf2_loclist_block_index
24841 : dwarf2_loclist_index);
24842 SYMBOL_LOCATION_BATON (sym) = baton;
24843 }
24844 else
24845 {
24846 struct dwarf2_locexpr_baton *baton;
24847
24848 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24849 baton->per_cu = cu->per_cu;
24850 gdb_assert (baton->per_cu);
24851
24852 if (attr_form_is_block (attr))
24853 {
24854 /* Note that we're just copying the block's data pointer
24855 here, not the actual data. We're still pointing into the
24856 info_buffer for SYM's objfile; right now we never release
24857 that buffer, but when we do clean up properly this may
24858 need to change. */
24859 baton->size = DW_BLOCK (attr)->size;
24860 baton->data = DW_BLOCK (attr)->data;
24861 }
24862 else
24863 {
24864 dwarf2_invalid_attrib_class_complaint ("location description",
24865 SYMBOL_NATURAL_NAME (sym));
24866 baton->size = 0;
24867 }
24868
24869 SYMBOL_ACLASS_INDEX (sym) = (is_block
24870 ? dwarf2_locexpr_block_index
24871 : dwarf2_locexpr_index);
24872 SYMBOL_LOCATION_BATON (sym) = baton;
24873 }
24874 }
24875
24876 /* Return the OBJFILE associated with the compilation unit CU. If CU
24877 came from a separate debuginfo file, then the master objfile is
24878 returned. */
24879
24880 struct objfile *
24881 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24882 {
24883 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24884
24885 /* Return the master objfile, so that we can report and look up the
24886 correct file containing this variable. */
24887 if (objfile->separate_debug_objfile_backlink)
24888 objfile = objfile->separate_debug_objfile_backlink;
24889
24890 return objfile;
24891 }
24892
24893 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24894 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24895 CU_HEADERP first. */
24896
24897 static const struct comp_unit_head *
24898 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24899 struct dwarf2_per_cu_data *per_cu)
24900 {
24901 const gdb_byte *info_ptr;
24902
24903 if (per_cu->cu)
24904 return &per_cu->cu->header;
24905
24906 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24907
24908 memset (cu_headerp, 0, sizeof (*cu_headerp));
24909 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24910 rcuh_kind::COMPILE);
24911
24912 return cu_headerp;
24913 }
24914
24915 /* Return the address size given in the compilation unit header for CU. */
24916
24917 int
24918 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24919 {
24920 struct comp_unit_head cu_header_local;
24921 const struct comp_unit_head *cu_headerp;
24922
24923 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24924
24925 return cu_headerp->addr_size;
24926 }
24927
24928 /* Return the offset size given in the compilation unit header for CU. */
24929
24930 int
24931 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24932 {
24933 struct comp_unit_head cu_header_local;
24934 const struct comp_unit_head *cu_headerp;
24935
24936 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24937
24938 return cu_headerp->offset_size;
24939 }
24940
24941 /* See its dwarf2loc.h declaration. */
24942
24943 int
24944 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24945 {
24946 struct comp_unit_head cu_header_local;
24947 const struct comp_unit_head *cu_headerp;
24948
24949 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24950
24951 if (cu_headerp->version == 2)
24952 return cu_headerp->addr_size;
24953 else
24954 return cu_headerp->offset_size;
24955 }
24956
24957 /* Return the text offset of the CU. The returned offset comes from
24958 this CU's objfile. If this objfile came from a separate debuginfo
24959 file, then the offset may be different from the corresponding
24960 offset in the parent objfile. */
24961
24962 CORE_ADDR
24963 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24964 {
24965 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24966
24967 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24968 }
24969
24970 /* Return DWARF version number of PER_CU. */
24971
24972 short
24973 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24974 {
24975 return per_cu->dwarf_version;
24976 }
24977
24978 /* Locate the .debug_info compilation unit from CU's objfile which contains
24979 the DIE at OFFSET. Raises an error on failure. */
24980
24981 static struct dwarf2_per_cu_data *
24982 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24983 unsigned int offset_in_dwz,
24984 struct dwarf2_per_objfile *dwarf2_per_objfile)
24985 {
24986 struct dwarf2_per_cu_data *this_cu;
24987 int low, high;
24988 const sect_offset *cu_off;
24989
24990 low = 0;
24991 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24992 while (high > low)
24993 {
24994 struct dwarf2_per_cu_data *mid_cu;
24995 int mid = low + (high - low) / 2;
24996
24997 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24998 cu_off = &mid_cu->sect_off;
24999 if (mid_cu->is_dwz > offset_in_dwz
25000 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25001 high = mid;
25002 else
25003 low = mid + 1;
25004 }
25005 gdb_assert (low == high);
25006 this_cu = dwarf2_per_objfile->all_comp_units[low];
25007 cu_off = &this_cu->sect_off;
25008 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25009 {
25010 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25011 error (_("Dwarf Error: could not find partial DIE containing "
25012 "offset %s [in module %s]"),
25013 sect_offset_str (sect_off),
25014 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25015
25016 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25017 <= sect_off);
25018 return dwarf2_per_objfile->all_comp_units[low-1];
25019 }
25020 else
25021 {
25022 this_cu = dwarf2_per_objfile->all_comp_units[low];
25023 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25024 && sect_off >= this_cu->sect_off + this_cu->length)
25025 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25026 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25027 return this_cu;
25028 }
25029 }
25030
25031 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25032
25033 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25034 : per_cu (per_cu_),
25035 mark (0),
25036 has_loclist (0),
25037 checked_producer (0),
25038 producer_is_gxx_lt_4_6 (0),
25039 producer_is_gcc_lt_4_3 (0),
25040 producer_is_icc_lt_14 (0),
25041 processing_has_namespace_info (0)
25042 {
25043 per_cu->cu = this;
25044 }
25045
25046 /* Destroy a dwarf2_cu. */
25047
25048 dwarf2_cu::~dwarf2_cu ()
25049 {
25050 per_cu->cu = NULL;
25051 }
25052
25053 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25054
25055 static void
25056 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25057 enum language pretend_language)
25058 {
25059 struct attribute *attr;
25060
25061 /* Set the language we're debugging. */
25062 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25063 if (attr)
25064 set_cu_language (DW_UNSND (attr), cu);
25065 else
25066 {
25067 cu->language = pretend_language;
25068 cu->language_defn = language_def (cu->language);
25069 }
25070
25071 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25072 }
25073
25074 /* Increase the age counter on each cached compilation unit, and free
25075 any that are too old. */
25076
25077 static void
25078 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25079 {
25080 struct dwarf2_per_cu_data *per_cu, **last_chain;
25081
25082 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25083 per_cu = dwarf2_per_objfile->read_in_chain;
25084 while (per_cu != NULL)
25085 {
25086 per_cu->cu->last_used ++;
25087 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25088 dwarf2_mark (per_cu->cu);
25089 per_cu = per_cu->cu->read_in_chain;
25090 }
25091
25092 per_cu = dwarf2_per_objfile->read_in_chain;
25093 last_chain = &dwarf2_per_objfile->read_in_chain;
25094 while (per_cu != NULL)
25095 {
25096 struct dwarf2_per_cu_data *next_cu;
25097
25098 next_cu = per_cu->cu->read_in_chain;
25099
25100 if (!per_cu->cu->mark)
25101 {
25102 delete per_cu->cu;
25103 *last_chain = next_cu;
25104 }
25105 else
25106 last_chain = &per_cu->cu->read_in_chain;
25107
25108 per_cu = next_cu;
25109 }
25110 }
25111
25112 /* Remove a single compilation unit from the cache. */
25113
25114 static void
25115 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25116 {
25117 struct dwarf2_per_cu_data *per_cu, **last_chain;
25118 struct dwarf2_per_objfile *dwarf2_per_objfile
25119 = target_per_cu->dwarf2_per_objfile;
25120
25121 per_cu = dwarf2_per_objfile->read_in_chain;
25122 last_chain = &dwarf2_per_objfile->read_in_chain;
25123 while (per_cu != NULL)
25124 {
25125 struct dwarf2_per_cu_data *next_cu;
25126
25127 next_cu = per_cu->cu->read_in_chain;
25128
25129 if (per_cu == target_per_cu)
25130 {
25131 delete per_cu->cu;
25132 per_cu->cu = NULL;
25133 *last_chain = next_cu;
25134 break;
25135 }
25136 else
25137 last_chain = &per_cu->cu->read_in_chain;
25138
25139 per_cu = next_cu;
25140 }
25141 }
25142
25143 /* Release all extra memory associated with OBJFILE. */
25144
25145 void
25146 dwarf2_free_objfile (struct objfile *objfile)
25147 {
25148 struct dwarf2_per_objfile *dwarf2_per_objfile
25149 = get_dwarf2_per_objfile (objfile);
25150
25151 delete dwarf2_per_objfile;
25152 }
25153
25154 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25155 We store these in a hash table separate from the DIEs, and preserve them
25156 when the DIEs are flushed out of cache.
25157
25158 The CU "per_cu" pointer is needed because offset alone is not enough to
25159 uniquely identify the type. A file may have multiple .debug_types sections,
25160 or the type may come from a DWO file. Furthermore, while it's more logical
25161 to use per_cu->section+offset, with Fission the section with the data is in
25162 the DWO file but we don't know that section at the point we need it.
25163 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25164 because we can enter the lookup routine, get_die_type_at_offset, from
25165 outside this file, and thus won't necessarily have PER_CU->cu.
25166 Fortunately, PER_CU is stable for the life of the objfile. */
25167
25168 struct dwarf2_per_cu_offset_and_type
25169 {
25170 const struct dwarf2_per_cu_data *per_cu;
25171 sect_offset sect_off;
25172 struct type *type;
25173 };
25174
25175 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25176
25177 static hashval_t
25178 per_cu_offset_and_type_hash (const void *item)
25179 {
25180 const struct dwarf2_per_cu_offset_and_type *ofs
25181 = (const struct dwarf2_per_cu_offset_and_type *) item;
25182
25183 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25184 }
25185
25186 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25187
25188 static int
25189 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25190 {
25191 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25192 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25193 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25194 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25195
25196 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25197 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25198 }
25199
25200 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25201 table if necessary. For convenience, return TYPE.
25202
25203 The DIEs reading must have careful ordering to:
25204 * Not cause infite loops trying to read in DIEs as a prerequisite for
25205 reading current DIE.
25206 * Not trying to dereference contents of still incompletely read in types
25207 while reading in other DIEs.
25208 * Enable referencing still incompletely read in types just by a pointer to
25209 the type without accessing its fields.
25210
25211 Therefore caller should follow these rules:
25212 * Try to fetch any prerequisite types we may need to build this DIE type
25213 before building the type and calling set_die_type.
25214 * After building type call set_die_type for current DIE as soon as
25215 possible before fetching more types to complete the current type.
25216 * Make the type as complete as possible before fetching more types. */
25217
25218 static struct type *
25219 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25220 {
25221 struct dwarf2_per_objfile *dwarf2_per_objfile
25222 = cu->per_cu->dwarf2_per_objfile;
25223 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25224 struct objfile *objfile = dwarf2_per_objfile->objfile;
25225 struct attribute *attr;
25226 struct dynamic_prop prop;
25227
25228 /* For Ada types, make sure that the gnat-specific data is always
25229 initialized (if not already set). There are a few types where
25230 we should not be doing so, because the type-specific area is
25231 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25232 where the type-specific area is used to store the floatformat).
25233 But this is not a problem, because the gnat-specific information
25234 is actually not needed for these types. */
25235 if (need_gnat_info (cu)
25236 && TYPE_CODE (type) != TYPE_CODE_FUNC
25237 && TYPE_CODE (type) != TYPE_CODE_FLT
25238 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25239 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25240 && TYPE_CODE (type) != TYPE_CODE_METHOD
25241 && !HAVE_GNAT_AUX_INFO (type))
25242 INIT_GNAT_SPECIFIC (type);
25243
25244 /* Read DW_AT_allocated and set in type. */
25245 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25246 if (attr_form_is_block (attr))
25247 {
25248 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25249 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25250 }
25251 else if (attr != NULL)
25252 {
25253 complaint (&symfile_complaints,
25254 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25255 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25256 sect_offset_str (die->sect_off));
25257 }
25258
25259 /* Read DW_AT_associated and set in type. */
25260 attr = dwarf2_attr (die, DW_AT_associated, cu);
25261 if (attr_form_is_block (attr))
25262 {
25263 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25264 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25265 }
25266 else if (attr != NULL)
25267 {
25268 complaint (&symfile_complaints,
25269 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25270 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25271 sect_offset_str (die->sect_off));
25272 }
25273
25274 /* Read DW_AT_data_location and set in type. */
25275 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25276 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25277 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25278
25279 if (dwarf2_per_objfile->die_type_hash == NULL)
25280 {
25281 dwarf2_per_objfile->die_type_hash =
25282 htab_create_alloc_ex (127,
25283 per_cu_offset_and_type_hash,
25284 per_cu_offset_and_type_eq,
25285 NULL,
25286 &objfile->objfile_obstack,
25287 hashtab_obstack_allocate,
25288 dummy_obstack_deallocate);
25289 }
25290
25291 ofs.per_cu = cu->per_cu;
25292 ofs.sect_off = die->sect_off;
25293 ofs.type = type;
25294 slot = (struct dwarf2_per_cu_offset_and_type **)
25295 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25296 if (*slot)
25297 complaint (&symfile_complaints,
25298 _("A problem internal to GDB: DIE %s has type already set"),
25299 sect_offset_str (die->sect_off));
25300 *slot = XOBNEW (&objfile->objfile_obstack,
25301 struct dwarf2_per_cu_offset_and_type);
25302 **slot = ofs;
25303 return type;
25304 }
25305
25306 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25307 or return NULL if the die does not have a saved type. */
25308
25309 static struct type *
25310 get_die_type_at_offset (sect_offset sect_off,
25311 struct dwarf2_per_cu_data *per_cu)
25312 {
25313 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25314 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25315
25316 if (dwarf2_per_objfile->die_type_hash == NULL)
25317 return NULL;
25318
25319 ofs.per_cu = per_cu;
25320 ofs.sect_off = sect_off;
25321 slot = ((struct dwarf2_per_cu_offset_and_type *)
25322 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25323 if (slot)
25324 return slot->type;
25325 else
25326 return NULL;
25327 }
25328
25329 /* Look up the type for DIE in CU in die_type_hash,
25330 or return NULL if DIE does not have a saved type. */
25331
25332 static struct type *
25333 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25334 {
25335 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25336 }
25337
25338 /* Add a dependence relationship from CU to REF_PER_CU. */
25339
25340 static void
25341 dwarf2_add_dependence (struct dwarf2_cu *cu,
25342 struct dwarf2_per_cu_data *ref_per_cu)
25343 {
25344 void **slot;
25345
25346 if (cu->dependencies == NULL)
25347 cu->dependencies
25348 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25349 NULL, &cu->comp_unit_obstack,
25350 hashtab_obstack_allocate,
25351 dummy_obstack_deallocate);
25352
25353 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25354 if (*slot == NULL)
25355 *slot = ref_per_cu;
25356 }
25357
25358 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25359 Set the mark field in every compilation unit in the
25360 cache that we must keep because we are keeping CU. */
25361
25362 static int
25363 dwarf2_mark_helper (void **slot, void *data)
25364 {
25365 struct dwarf2_per_cu_data *per_cu;
25366
25367 per_cu = (struct dwarf2_per_cu_data *) *slot;
25368
25369 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25370 reading of the chain. As such dependencies remain valid it is not much
25371 useful to track and undo them during QUIT cleanups. */
25372 if (per_cu->cu == NULL)
25373 return 1;
25374
25375 if (per_cu->cu->mark)
25376 return 1;
25377 per_cu->cu->mark = 1;
25378
25379 if (per_cu->cu->dependencies != NULL)
25380 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25381
25382 return 1;
25383 }
25384
25385 /* Set the mark field in CU and in every other compilation unit in the
25386 cache that we must keep because we are keeping CU. */
25387
25388 static void
25389 dwarf2_mark (struct dwarf2_cu *cu)
25390 {
25391 if (cu->mark)
25392 return;
25393 cu->mark = 1;
25394 if (cu->dependencies != NULL)
25395 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25396 }
25397
25398 static void
25399 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25400 {
25401 while (per_cu)
25402 {
25403 per_cu->cu->mark = 0;
25404 per_cu = per_cu->cu->read_in_chain;
25405 }
25406 }
25407
25408 /* Trivial hash function for partial_die_info: the hash value of a DIE
25409 is its offset in .debug_info for this objfile. */
25410
25411 static hashval_t
25412 partial_die_hash (const void *item)
25413 {
25414 const struct partial_die_info *part_die
25415 = (const struct partial_die_info *) item;
25416
25417 return to_underlying (part_die->sect_off);
25418 }
25419
25420 /* Trivial comparison function for partial_die_info structures: two DIEs
25421 are equal if they have the same offset. */
25422
25423 static int
25424 partial_die_eq (const void *item_lhs, const void *item_rhs)
25425 {
25426 const struct partial_die_info *part_die_lhs
25427 = (const struct partial_die_info *) item_lhs;
25428 const struct partial_die_info *part_die_rhs
25429 = (const struct partial_die_info *) item_rhs;
25430
25431 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25432 }
25433
25434 static struct cmd_list_element *set_dwarf_cmdlist;
25435 static struct cmd_list_element *show_dwarf_cmdlist;
25436
25437 static void
25438 set_dwarf_cmd (const char *args, int from_tty)
25439 {
25440 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25441 gdb_stdout);
25442 }
25443
25444 static void
25445 show_dwarf_cmd (const char *args, int from_tty)
25446 {
25447 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25448 }
25449
25450 int dwarf_always_disassemble;
25451
25452 static void
25453 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25454 struct cmd_list_element *c, const char *value)
25455 {
25456 fprintf_filtered (file,
25457 _("Whether to always disassemble "
25458 "DWARF expressions is %s.\n"),
25459 value);
25460 }
25461
25462 static void
25463 show_check_physname (struct ui_file *file, int from_tty,
25464 struct cmd_list_element *c, const char *value)
25465 {
25466 fprintf_filtered (file,
25467 _("Whether to check \"physname\" is %s.\n"),
25468 value);
25469 }
25470
25471 void
25472 _initialize_dwarf2_read (void)
25473 {
25474
25475 dwarf2_objfile_data_key = register_objfile_data ();
25476
25477 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25478 Set DWARF specific variables.\n\
25479 Configure DWARF variables such as the cache size"),
25480 &set_dwarf_cmdlist, "maintenance set dwarf ",
25481 0/*allow-unknown*/, &maintenance_set_cmdlist);
25482
25483 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25484 Show DWARF specific variables\n\
25485 Show DWARF variables such as the cache size"),
25486 &show_dwarf_cmdlist, "maintenance show dwarf ",
25487 0/*allow-unknown*/, &maintenance_show_cmdlist);
25488
25489 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25490 &dwarf_max_cache_age, _("\
25491 Set the upper bound on the age of cached DWARF compilation units."), _("\
25492 Show the upper bound on the age of cached DWARF compilation units."), _("\
25493 A higher limit means that cached compilation units will be stored\n\
25494 in memory longer, and more total memory will be used. Zero disables\n\
25495 caching, which can slow down startup."),
25496 NULL,
25497 show_dwarf_max_cache_age,
25498 &set_dwarf_cmdlist,
25499 &show_dwarf_cmdlist);
25500
25501 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25502 &dwarf_always_disassemble, _("\
25503 Set whether `info address' always disassembles DWARF expressions."), _("\
25504 Show whether `info address' always disassembles DWARF expressions."), _("\
25505 When enabled, DWARF expressions are always printed in an assembly-like\n\
25506 syntax. When disabled, expressions will be printed in a more\n\
25507 conversational style, when possible."),
25508 NULL,
25509 show_dwarf_always_disassemble,
25510 &set_dwarf_cmdlist,
25511 &show_dwarf_cmdlist);
25512
25513 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25514 Set debugging of the DWARF reader."), _("\
25515 Show debugging of the DWARF reader."), _("\
25516 When enabled (non-zero), debugging messages are printed during DWARF\n\
25517 reading and symtab expansion. A value of 1 (one) provides basic\n\
25518 information. A value greater than 1 provides more verbose information."),
25519 NULL,
25520 NULL,
25521 &setdebuglist, &showdebuglist);
25522
25523 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25524 Set debugging of the DWARF DIE reader."), _("\
25525 Show debugging of the DWARF DIE reader."), _("\
25526 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25527 The value is the maximum depth to print."),
25528 NULL,
25529 NULL,
25530 &setdebuglist, &showdebuglist);
25531
25532 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25533 Set debugging of the dwarf line reader."), _("\
25534 Show debugging of the dwarf line reader."), _("\
25535 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25536 A value of 1 (one) provides basic information.\n\
25537 A value greater than 1 provides more verbose information."),
25538 NULL,
25539 NULL,
25540 &setdebuglist, &showdebuglist);
25541
25542 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25543 Set cross-checking of \"physname\" code against demangler."), _("\
25544 Show cross-checking of \"physname\" code against demangler."), _("\
25545 When enabled, GDB's internal \"physname\" code is checked against\n\
25546 the demangler."),
25547 NULL, show_check_physname,
25548 &setdebuglist, &showdebuglist);
25549
25550 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25551 no_class, &use_deprecated_index_sections, _("\
25552 Set whether to use deprecated gdb_index sections."), _("\
25553 Show whether to use deprecated gdb_index sections."), _("\
25554 When enabled, deprecated .gdb_index sections are used anyway.\n\
25555 Normally they are ignored either because of a missing feature or\n\
25556 performance issue.\n\
25557 Warning: This option must be enabled before gdb reads the file."),
25558 NULL,
25559 NULL,
25560 &setlist, &showlist);
25561
25562 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25563 &dwarf2_locexpr_funcs);
25564 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25565 &dwarf2_loclist_funcs);
25566
25567 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25568 &dwarf2_block_frame_base_locexpr_funcs);
25569 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25570 &dwarf2_block_frame_base_loclist_funcs);
25571
25572 #if GDB_SELF_TEST
25573 selftests::register_test ("dw2_expand_symtabs_matching",
25574 selftests::dw2_expand_symtabs_matching::run_test);
25575 #endif
25576 }
This page took 0.523021 seconds and 5 git commands to generate.