Replace dw2_get_cu/dw2_get_cutu with methods of dwarf2_per_objfile
[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 /* The name_component table (a sorted vector). See name_component's
151 description above. */
152 std::vector<name_component> name_components;
153
154 /* How NAME_COMPONENTS is sorted. */
155 enum case_sensitivity name_components_casing;
156
157 /* Return the number of names in the symbol table. */
158 virtual size_t symbol_name_count () const = 0;
159
160 /* Get the name of the symbol at IDX in the symbol table. */
161 virtual const char *symbol_name_at (offset_type idx) const = 0;
162
163 /* Return whether the name at IDX in the symbol table should be
164 ignored. */
165 virtual bool symbol_name_slot_invalid (offset_type idx) const
166 {
167 return false;
168 }
169
170 /* Build the symbol name component sorted vector, if we haven't
171 yet. */
172 void build_name_components ();
173
174 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
175 possible matches for LN_NO_PARAMS in the name component
176 vector. */
177 std::pair<std::vector<name_component>::const_iterator,
178 std::vector<name_component>::const_iterator>
179 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
180
181 /* Prevent deleting/destroying via a base class pointer. */
182 protected:
183 ~mapped_index_base() = default;
184 };
185
186 /* A description of the mapped index. The file format is described in
187 a comment by the code that writes the index. */
188 struct mapped_index final : public mapped_index_base
189 {
190 /* A slot/bucket in the symbol table hash. */
191 struct symbol_table_slot
192 {
193 const offset_type name;
194 const offset_type vec;
195 };
196
197 /* Index data format version. */
198 int version;
199
200 /* The total length of the buffer. */
201 off_t total_size;
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;
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 /* Name of the file. */
814 const char *name;
815
816 /* File format version. */
817 int version;
818
819 /* The bfd. */
820 bfd *dbfd;
821
822 /* Section info for this file. */
823 struct dwp_sections sections;
824
825 /* Table of CUs in the file. */
826 const struct dwp_hash_table *cus;
827
828 /* Table of TUs in the file. */
829 const struct dwp_hash_table *tus;
830
831 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
832 htab_t loaded_cus;
833 htab_t loaded_tus;
834
835 /* Table to map ELF section numbers to their sections.
836 This is only needed for the DWP V1 file format. */
837 unsigned int num_sections;
838 asection **elf_sections;
839 };
840
841 /* This represents a '.dwz' file. */
842
843 struct dwz_file
844 {
845 /* A dwz file can only contain a few sections. */
846 struct dwarf2_section_info abbrev;
847 struct dwarf2_section_info info;
848 struct dwarf2_section_info str;
849 struct dwarf2_section_info line;
850 struct dwarf2_section_info macro;
851 struct dwarf2_section_info gdb_index;
852 struct dwarf2_section_info debug_names;
853
854 /* The dwz's BFD. */
855 bfd *dwz_bfd;
856 };
857
858 /* Struct used to pass misc. parameters to read_die_and_children, et
859 al. which are used for both .debug_info and .debug_types dies.
860 All parameters here are unchanging for the life of the call. This
861 struct exists to abstract away the constant parameters of die reading. */
862
863 struct die_reader_specs
864 {
865 /* The bfd of die_section. */
866 bfd* abfd;
867
868 /* The CU of the DIE we are parsing. */
869 struct dwarf2_cu *cu;
870
871 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
872 struct dwo_file *dwo_file;
873
874 /* The section the die comes from.
875 This is either .debug_info or .debug_types, or the .dwo variants. */
876 struct dwarf2_section_info *die_section;
877
878 /* die_section->buffer. */
879 const gdb_byte *buffer;
880
881 /* The end of the buffer. */
882 const gdb_byte *buffer_end;
883
884 /* The value of the DW_AT_comp_dir attribute. */
885 const char *comp_dir;
886
887 /* The abbreviation table to use when reading the DIEs. */
888 struct abbrev_table *abbrev_table;
889 };
890
891 /* Type of function passed to init_cutu_and_read_dies, et.al. */
892 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
893 const gdb_byte *info_ptr,
894 struct die_info *comp_unit_die,
895 int has_children,
896 void *data);
897
898 /* A 1-based directory index. This is a strong typedef to prevent
899 accidentally using a directory index as a 0-based index into an
900 array/vector. */
901 enum class dir_index : unsigned int {};
902
903 /* Likewise, a 1-based file name index. */
904 enum class file_name_index : unsigned int {};
905
906 struct file_entry
907 {
908 file_entry () = default;
909
910 file_entry (const char *name_, dir_index d_index_,
911 unsigned int mod_time_, unsigned int length_)
912 : name (name_),
913 d_index (d_index_),
914 mod_time (mod_time_),
915 length (length_)
916 {}
917
918 /* Return the include directory at D_INDEX stored in LH. Returns
919 NULL if D_INDEX is out of bounds. */
920 const char *include_dir (const line_header *lh) const;
921
922 /* The file name. Note this is an observing pointer. The memory is
923 owned by debug_line_buffer. */
924 const char *name {};
925
926 /* The directory index (1-based). */
927 dir_index d_index {};
928
929 unsigned int mod_time {};
930
931 unsigned int length {};
932
933 /* True if referenced by the Line Number Program. */
934 bool included_p {};
935
936 /* The associated symbol table, if any. */
937 struct symtab *symtab {};
938 };
939
940 /* The line number information for a compilation unit (found in the
941 .debug_line section) begins with a "statement program header",
942 which contains the following information. */
943 struct line_header
944 {
945 line_header ()
946 : offset_in_dwz {}
947 {}
948
949 /* Add an entry to the include directory table. */
950 void add_include_dir (const char *include_dir);
951
952 /* Add an entry to the file name table. */
953 void add_file_name (const char *name, dir_index d_index,
954 unsigned int mod_time, unsigned int length);
955
956 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
957 is out of bounds. */
958 const char *include_dir_at (dir_index index) const
959 {
960 /* Convert directory index number (1-based) to vector index
961 (0-based). */
962 size_t vec_index = to_underlying (index) - 1;
963
964 if (vec_index >= include_dirs.size ())
965 return NULL;
966 return include_dirs[vec_index];
967 }
968
969 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
970 is out of bounds. */
971 file_entry *file_name_at (file_name_index index)
972 {
973 /* Convert file name index number (1-based) to vector index
974 (0-based). */
975 size_t vec_index = to_underlying (index) - 1;
976
977 if (vec_index >= file_names.size ())
978 return NULL;
979 return &file_names[vec_index];
980 }
981
982 /* Const version of the above. */
983 const file_entry *file_name_at (unsigned int index) const
984 {
985 if (index >= file_names.size ())
986 return NULL;
987 return &file_names[index];
988 }
989
990 /* Offset of line number information in .debug_line section. */
991 sect_offset sect_off {};
992
993 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
994 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
995
996 unsigned int total_length {};
997 unsigned short version {};
998 unsigned int header_length {};
999 unsigned char minimum_instruction_length {};
1000 unsigned char maximum_ops_per_instruction {};
1001 unsigned char default_is_stmt {};
1002 int line_base {};
1003 unsigned char line_range {};
1004 unsigned char opcode_base {};
1005
1006 /* standard_opcode_lengths[i] is the number of operands for the
1007 standard opcode whose value is i. This means that
1008 standard_opcode_lengths[0] is unused, and the last meaningful
1009 element is standard_opcode_lengths[opcode_base - 1]. */
1010 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1011
1012 /* The include_directories table. Note these are observing
1013 pointers. The memory is owned by debug_line_buffer. */
1014 std::vector<const char *> include_dirs;
1015
1016 /* The file_names table. */
1017 std::vector<file_entry> file_names;
1018
1019 /* The start and end of the statement program following this
1020 header. These point into dwarf2_per_objfile->line_buffer. */
1021 const gdb_byte *statement_program_start {}, *statement_program_end {};
1022 };
1023
1024 typedef std::unique_ptr<line_header> line_header_up;
1025
1026 const char *
1027 file_entry::include_dir (const line_header *lh) const
1028 {
1029 return lh->include_dir_at (d_index);
1030 }
1031
1032 /* When we construct a partial symbol table entry we only
1033 need this much information. */
1034 struct partial_die_info : public allocate_on_obstack
1035 {
1036 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1037
1038 /* Disable assign but still keep copy ctor, which is needed
1039 load_partial_dies. */
1040 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1041
1042 /* Adjust the partial die before generating a symbol for it. This
1043 function may set the is_external flag or change the DIE's
1044 name. */
1045 void fixup (struct dwarf2_cu *cu);
1046
1047 /* Read a minimal amount of information into the minimal die
1048 structure. */
1049 const gdb_byte *read (const struct die_reader_specs *reader,
1050 const struct abbrev_info &abbrev,
1051 const gdb_byte *info_ptr);
1052
1053 /* Offset of this DIE. */
1054 const sect_offset sect_off;
1055
1056 /* DWARF-2 tag for this DIE. */
1057 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1058
1059 /* Assorted flags describing the data found in this DIE. */
1060 const unsigned int has_children : 1;
1061
1062 unsigned int is_external : 1;
1063 unsigned int is_declaration : 1;
1064 unsigned int has_type : 1;
1065 unsigned int has_specification : 1;
1066 unsigned int has_pc_info : 1;
1067 unsigned int may_be_inlined : 1;
1068
1069 /* This DIE has been marked DW_AT_main_subprogram. */
1070 unsigned int main_subprogram : 1;
1071
1072 /* Flag set if the SCOPE field of this structure has been
1073 computed. */
1074 unsigned int scope_set : 1;
1075
1076 /* Flag set if the DIE has a byte_size attribute. */
1077 unsigned int has_byte_size : 1;
1078
1079 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1080 unsigned int has_const_value : 1;
1081
1082 /* Flag set if any of the DIE's children are template arguments. */
1083 unsigned int has_template_arguments : 1;
1084
1085 /* Flag set if fixup has been called on this die. */
1086 unsigned int fixup_called : 1;
1087
1088 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1089 unsigned int is_dwz : 1;
1090
1091 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1092 unsigned int spec_is_dwz : 1;
1093
1094 /* The name of this DIE. Normally the value of DW_AT_name, but
1095 sometimes a default name for unnamed DIEs. */
1096 const char *name = nullptr;
1097
1098 /* The linkage name, if present. */
1099 const char *linkage_name = nullptr;
1100
1101 /* The scope to prepend to our children. This is generally
1102 allocated on the comp_unit_obstack, so will disappear
1103 when this compilation unit leaves the cache. */
1104 const char *scope = nullptr;
1105
1106 /* Some data associated with the partial DIE. The tag determines
1107 which field is live. */
1108 union
1109 {
1110 /* The location description associated with this DIE, if any. */
1111 struct dwarf_block *locdesc;
1112 /* The offset of an import, for DW_TAG_imported_unit. */
1113 sect_offset sect_off;
1114 } d {};
1115
1116 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1117 CORE_ADDR lowpc = 0;
1118 CORE_ADDR highpc = 0;
1119
1120 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1121 DW_AT_sibling, if any. */
1122 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1123 could return DW_AT_sibling values to its caller load_partial_dies. */
1124 const gdb_byte *sibling = nullptr;
1125
1126 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1127 DW_AT_specification (or DW_AT_abstract_origin or
1128 DW_AT_extension). */
1129 sect_offset spec_offset {};
1130
1131 /* Pointers to this DIE's parent, first child, and next sibling,
1132 if any. */
1133 struct partial_die_info *die_parent = nullptr;
1134 struct partial_die_info *die_child = nullptr;
1135 struct partial_die_info *die_sibling = nullptr;
1136
1137 friend struct partial_die_info *
1138 dwarf2_cu::find_partial_die (sect_offset sect_off);
1139
1140 private:
1141 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1142 partial_die_info (sect_offset sect_off)
1143 : partial_die_info (sect_off, DW_TAG_padding, 0)
1144 {
1145 }
1146
1147 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1148 int has_children_)
1149 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1150 {
1151 is_external = 0;
1152 is_declaration = 0;
1153 has_type = 0;
1154 has_specification = 0;
1155 has_pc_info = 0;
1156 may_be_inlined = 0;
1157 main_subprogram = 0;
1158 scope_set = 0;
1159 has_byte_size = 0;
1160 has_const_value = 0;
1161 has_template_arguments = 0;
1162 fixup_called = 0;
1163 is_dwz = 0;
1164 spec_is_dwz = 0;
1165 }
1166 };
1167
1168 /* This data structure holds the information of an abbrev. */
1169 struct abbrev_info
1170 {
1171 unsigned int number; /* number identifying abbrev */
1172 enum dwarf_tag tag; /* dwarf tag */
1173 unsigned short has_children; /* boolean */
1174 unsigned short num_attrs; /* number of attributes */
1175 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1176 struct abbrev_info *next; /* next in chain */
1177 };
1178
1179 struct attr_abbrev
1180 {
1181 ENUM_BITFIELD(dwarf_attribute) name : 16;
1182 ENUM_BITFIELD(dwarf_form) form : 16;
1183
1184 /* It is valid only if FORM is DW_FORM_implicit_const. */
1185 LONGEST implicit_const;
1186 };
1187
1188 /* Size of abbrev_table.abbrev_hash_table. */
1189 #define ABBREV_HASH_SIZE 121
1190
1191 /* Top level data structure to contain an abbreviation table. */
1192
1193 struct abbrev_table
1194 {
1195 explicit abbrev_table (sect_offset off)
1196 : sect_off (off)
1197 {
1198 m_abbrevs =
1199 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1200 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1201 }
1202
1203 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1204
1205 /* Allocate space for a struct abbrev_info object in
1206 ABBREV_TABLE. */
1207 struct abbrev_info *alloc_abbrev ();
1208
1209 /* Add an abbreviation to the table. */
1210 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1211
1212 /* Look up an abbrev in the table.
1213 Returns NULL if the abbrev is not found. */
1214
1215 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1216
1217
1218 /* Where the abbrev table came from.
1219 This is used as a sanity check when the table is used. */
1220 const sect_offset sect_off;
1221
1222 /* Storage for the abbrev table. */
1223 auto_obstack abbrev_obstack;
1224
1225 private:
1226
1227 /* Hash table of abbrevs.
1228 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1229 It could be statically allocated, but the previous code didn't so we
1230 don't either. */
1231 struct abbrev_info **m_abbrevs;
1232 };
1233
1234 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1235
1236 /* Attributes have a name and a value. */
1237 struct attribute
1238 {
1239 ENUM_BITFIELD(dwarf_attribute) name : 16;
1240 ENUM_BITFIELD(dwarf_form) form : 15;
1241
1242 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1243 field should be in u.str (existing only for DW_STRING) but it is kept
1244 here for better struct attribute alignment. */
1245 unsigned int string_is_canonical : 1;
1246
1247 union
1248 {
1249 const char *str;
1250 struct dwarf_block *blk;
1251 ULONGEST unsnd;
1252 LONGEST snd;
1253 CORE_ADDR addr;
1254 ULONGEST signature;
1255 }
1256 u;
1257 };
1258
1259 /* This data structure holds a complete die structure. */
1260 struct die_info
1261 {
1262 /* DWARF-2 tag for this DIE. */
1263 ENUM_BITFIELD(dwarf_tag) tag : 16;
1264
1265 /* Number of attributes */
1266 unsigned char num_attrs;
1267
1268 /* True if we're presently building the full type name for the
1269 type derived from this DIE. */
1270 unsigned char building_fullname : 1;
1271
1272 /* True if this die is in process. PR 16581. */
1273 unsigned char in_process : 1;
1274
1275 /* Abbrev number */
1276 unsigned int abbrev;
1277
1278 /* Offset in .debug_info or .debug_types section. */
1279 sect_offset sect_off;
1280
1281 /* The dies in a compilation unit form an n-ary tree. PARENT
1282 points to this die's parent; CHILD points to the first child of
1283 this node; and all the children of a given node are chained
1284 together via their SIBLING fields. */
1285 struct die_info *child; /* Its first child, if any. */
1286 struct die_info *sibling; /* Its next sibling, if any. */
1287 struct die_info *parent; /* Its parent, if any. */
1288
1289 /* An array of attributes, with NUM_ATTRS elements. There may be
1290 zero, but it's not common and zero-sized arrays are not
1291 sufficiently portable C. */
1292 struct attribute attrs[1];
1293 };
1294
1295 /* Get at parts of an attribute structure. */
1296
1297 #define DW_STRING(attr) ((attr)->u.str)
1298 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1299 #define DW_UNSND(attr) ((attr)->u.unsnd)
1300 #define DW_BLOCK(attr) ((attr)->u.blk)
1301 #define DW_SND(attr) ((attr)->u.snd)
1302 #define DW_ADDR(attr) ((attr)->u.addr)
1303 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1304
1305 /* Blocks are a bunch of untyped bytes. */
1306 struct dwarf_block
1307 {
1308 size_t size;
1309
1310 /* Valid only if SIZE is not zero. */
1311 const gdb_byte *data;
1312 };
1313
1314 #ifndef ATTR_ALLOC_CHUNK
1315 #define ATTR_ALLOC_CHUNK 4
1316 #endif
1317
1318 /* Allocate fields for structs, unions and enums in this size. */
1319 #ifndef DW_FIELD_ALLOC_CHUNK
1320 #define DW_FIELD_ALLOC_CHUNK 4
1321 #endif
1322
1323 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1324 but this would require a corresponding change in unpack_field_as_long
1325 and friends. */
1326 static int bits_per_byte = 8;
1327
1328 /* When reading a variant or variant part, we track a bit more
1329 information about the field, and store it in an object of this
1330 type. */
1331
1332 struct variant_field
1333 {
1334 /* If we see a DW_TAG_variant, then this will be the discriminant
1335 value. */
1336 ULONGEST discriminant_value;
1337 /* If we see a DW_TAG_variant, then this will be set if this is the
1338 default branch. */
1339 bool default_branch;
1340 /* While reading a DW_TAG_variant_part, this will be set if this
1341 field is the discriminant. */
1342 bool is_discriminant;
1343 };
1344
1345 struct nextfield
1346 {
1347 int accessibility = 0;
1348 int virtuality = 0;
1349 /* Extra information to describe a variant or variant part. */
1350 struct variant_field variant {};
1351 struct field field {};
1352 };
1353
1354 struct fnfieldlist
1355 {
1356 const char *name = nullptr;
1357 std::vector<struct fn_field> fnfields;
1358 };
1359
1360 /* The routines that read and process dies for a C struct or C++ class
1361 pass lists of data member fields and lists of member function fields
1362 in an instance of a field_info structure, as defined below. */
1363 struct field_info
1364 {
1365 /* List of data member and baseclasses fields. */
1366 std::vector<struct nextfield> fields;
1367 std::vector<struct nextfield> baseclasses;
1368
1369 /* Number of fields (including baseclasses). */
1370 int nfields = 0;
1371
1372 /* Set if the accesibility of one of the fields is not public. */
1373 int non_public_fields = 0;
1374
1375 /* Member function fieldlist array, contains name of possibly overloaded
1376 member function, number of overloaded member functions and a pointer
1377 to the head of the member function field chain. */
1378 std::vector<struct fnfieldlist> fnfieldlists;
1379
1380 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1381 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1382 std::vector<struct decl_field> typedef_field_list;
1383
1384 /* Nested types defined by this class and the number of elements in this
1385 list. */
1386 std::vector<struct decl_field> nested_types_list;
1387 };
1388
1389 /* One item on the queue of compilation units to read in full symbols
1390 for. */
1391 struct dwarf2_queue_item
1392 {
1393 struct dwarf2_per_cu_data *per_cu;
1394 enum language pretend_language;
1395 struct dwarf2_queue_item *next;
1396 };
1397
1398 /* The current queue. */
1399 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1400
1401 /* Loaded secondary compilation units are kept in memory until they
1402 have not been referenced for the processing of this many
1403 compilation units. Set this to zero to disable caching. Cache
1404 sizes of up to at least twenty will improve startup time for
1405 typical inter-CU-reference binaries, at an obvious memory cost. */
1406 static int dwarf_max_cache_age = 5;
1407 static void
1408 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1409 struct cmd_list_element *c, const char *value)
1410 {
1411 fprintf_filtered (file, _("The upper bound on the age of cached "
1412 "DWARF compilation units is %s.\n"),
1413 value);
1414 }
1415 \f
1416 /* local function prototypes */
1417
1418 static const char *get_section_name (const struct dwarf2_section_info *);
1419
1420 static const char *get_section_file_name (const struct dwarf2_section_info *);
1421
1422 static void dwarf2_find_base_address (struct die_info *die,
1423 struct dwarf2_cu *cu);
1424
1425 static struct partial_symtab *create_partial_symtab
1426 (struct dwarf2_per_cu_data *per_cu, const char *name);
1427
1428 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1429 const gdb_byte *info_ptr,
1430 struct die_info *type_unit_die,
1431 int has_children, void *data);
1432
1433 static void dwarf2_build_psymtabs_hard
1434 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1435
1436 static void scan_partial_symbols (struct partial_die_info *,
1437 CORE_ADDR *, CORE_ADDR *,
1438 int, struct dwarf2_cu *);
1439
1440 static void add_partial_symbol (struct partial_die_info *,
1441 struct dwarf2_cu *);
1442
1443 static void add_partial_namespace (struct partial_die_info *pdi,
1444 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1445 int set_addrmap, struct dwarf2_cu *cu);
1446
1447 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1448 CORE_ADDR *highpc, int set_addrmap,
1449 struct dwarf2_cu *cu);
1450
1451 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1452 struct dwarf2_cu *cu);
1453
1454 static void add_partial_subprogram (struct partial_die_info *pdi,
1455 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1456 int need_pc, struct dwarf2_cu *cu);
1457
1458 static void dwarf2_read_symtab (struct partial_symtab *,
1459 struct objfile *);
1460
1461 static void psymtab_to_symtab_1 (struct partial_symtab *);
1462
1463 static abbrev_table_up abbrev_table_read_table
1464 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1465 sect_offset);
1466
1467 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1468
1469 static struct partial_die_info *load_partial_dies
1470 (const struct die_reader_specs *, const gdb_byte *, int);
1471
1472 static struct partial_die_info *find_partial_die (sect_offset, int,
1473 struct dwarf2_cu *);
1474
1475 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1476 struct attribute *, struct attr_abbrev *,
1477 const gdb_byte *);
1478
1479 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1480
1481 static int read_1_signed_byte (bfd *, const gdb_byte *);
1482
1483 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1484
1485 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1486
1487 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1488
1489 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1490 unsigned int *);
1491
1492 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1493
1494 static LONGEST read_checked_initial_length_and_offset
1495 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1496 unsigned int *, unsigned int *);
1497
1498 static LONGEST read_offset (bfd *, const gdb_byte *,
1499 const struct comp_unit_head *,
1500 unsigned int *);
1501
1502 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1503
1504 static sect_offset read_abbrev_offset
1505 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1506 struct dwarf2_section_info *, sect_offset);
1507
1508 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1509
1510 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1511
1512 static const char *read_indirect_string
1513 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1514 const struct comp_unit_head *, unsigned int *);
1515
1516 static const char *read_indirect_line_string
1517 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1518 const struct comp_unit_head *, unsigned int *);
1519
1520 static const char *read_indirect_string_at_offset
1521 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1522 LONGEST str_offset);
1523
1524 static const char *read_indirect_string_from_dwz
1525 (struct objfile *objfile, struct dwz_file *, LONGEST);
1526
1527 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1528
1529 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1530 const gdb_byte *,
1531 unsigned int *);
1532
1533 static const char *read_str_index (const struct die_reader_specs *reader,
1534 ULONGEST str_index);
1535
1536 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1537
1538 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1539 struct dwarf2_cu *);
1540
1541 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1542 unsigned int);
1543
1544 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1545 struct dwarf2_cu *cu);
1546
1547 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1548 struct dwarf2_cu *cu);
1549
1550 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1551
1552 static struct die_info *die_specification (struct die_info *die,
1553 struct dwarf2_cu **);
1554
1555 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1556 struct dwarf2_cu *cu);
1557
1558 static void dwarf_decode_lines (struct line_header *, const char *,
1559 struct dwarf2_cu *, struct partial_symtab *,
1560 CORE_ADDR, int decode_mapping);
1561
1562 static void dwarf2_start_subfile (const char *, const char *);
1563
1564 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1565 const char *, const char *,
1566 CORE_ADDR);
1567
1568 static struct symbol *new_symbol (struct die_info *, struct type *,
1569 struct dwarf2_cu *, struct symbol * = NULL);
1570
1571 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1572 struct dwarf2_cu *);
1573
1574 static void dwarf2_const_value_attr (const struct attribute *attr,
1575 struct type *type,
1576 const char *name,
1577 struct obstack *obstack,
1578 struct dwarf2_cu *cu, LONGEST *value,
1579 const gdb_byte **bytes,
1580 struct dwarf2_locexpr_baton **baton);
1581
1582 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1583
1584 static int need_gnat_info (struct dwarf2_cu *);
1585
1586 static struct type *die_descriptive_type (struct die_info *,
1587 struct dwarf2_cu *);
1588
1589 static void set_descriptive_type (struct type *, struct die_info *,
1590 struct dwarf2_cu *);
1591
1592 static struct type *die_containing_type (struct die_info *,
1593 struct dwarf2_cu *);
1594
1595 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1596 struct dwarf2_cu *);
1597
1598 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1599
1600 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1601
1602 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1603
1604 static char *typename_concat (struct obstack *obs, const char *prefix,
1605 const char *suffix, int physname,
1606 struct dwarf2_cu *cu);
1607
1608 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1609
1610 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1611
1612 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1613
1614 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1615
1616 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1617
1618 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1619
1620 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1621 struct dwarf2_cu *, struct partial_symtab *);
1622
1623 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1624 values. Keep the items ordered with increasing constraints compliance. */
1625 enum pc_bounds_kind
1626 {
1627 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1628 PC_BOUNDS_NOT_PRESENT,
1629
1630 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1631 were present but they do not form a valid range of PC addresses. */
1632 PC_BOUNDS_INVALID,
1633
1634 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1635 PC_BOUNDS_RANGES,
1636
1637 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1638 PC_BOUNDS_HIGH_LOW,
1639 };
1640
1641 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1642 CORE_ADDR *, CORE_ADDR *,
1643 struct dwarf2_cu *,
1644 struct partial_symtab *);
1645
1646 static void get_scope_pc_bounds (struct die_info *,
1647 CORE_ADDR *, CORE_ADDR *,
1648 struct dwarf2_cu *);
1649
1650 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1651 CORE_ADDR, struct dwarf2_cu *);
1652
1653 static void dwarf2_add_field (struct field_info *, struct die_info *,
1654 struct dwarf2_cu *);
1655
1656 static void dwarf2_attach_fields_to_type (struct field_info *,
1657 struct type *, struct dwarf2_cu *);
1658
1659 static void dwarf2_add_member_fn (struct field_info *,
1660 struct die_info *, struct type *,
1661 struct dwarf2_cu *);
1662
1663 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1664 struct type *,
1665 struct dwarf2_cu *);
1666
1667 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1668
1669 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1670
1671 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1672
1673 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1674
1675 static struct using_direct **using_directives (enum language);
1676
1677 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1678
1679 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1680
1681 static struct type *read_module_type (struct die_info *die,
1682 struct dwarf2_cu *cu);
1683
1684 static const char *namespace_name (struct die_info *die,
1685 int *is_anonymous, struct dwarf2_cu *);
1686
1687 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1688
1689 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1690
1691 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1692 struct dwarf2_cu *);
1693
1694 static struct die_info *read_die_and_siblings_1
1695 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1696 struct die_info *);
1697
1698 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1699 const gdb_byte *info_ptr,
1700 const gdb_byte **new_info_ptr,
1701 struct die_info *parent);
1702
1703 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1704 struct die_info **, const gdb_byte *,
1705 int *, int);
1706
1707 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1708 struct die_info **, const gdb_byte *,
1709 int *);
1710
1711 static void process_die (struct die_info *, struct dwarf2_cu *);
1712
1713 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1714 struct obstack *);
1715
1716 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1717
1718 static const char *dwarf2_full_name (const char *name,
1719 struct die_info *die,
1720 struct dwarf2_cu *cu);
1721
1722 static const char *dwarf2_physname (const char *name, struct die_info *die,
1723 struct dwarf2_cu *cu);
1724
1725 static struct die_info *dwarf2_extension (struct die_info *die,
1726 struct dwarf2_cu **);
1727
1728 static const char *dwarf_tag_name (unsigned int);
1729
1730 static const char *dwarf_attr_name (unsigned int);
1731
1732 static const char *dwarf_form_name (unsigned int);
1733
1734 static const char *dwarf_bool_name (unsigned int);
1735
1736 static const char *dwarf_type_encoding_name (unsigned int);
1737
1738 static struct die_info *sibling_die (struct die_info *);
1739
1740 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1741
1742 static void dump_die_for_error (struct die_info *);
1743
1744 static void dump_die_1 (struct ui_file *, int level, int max_level,
1745 struct die_info *);
1746
1747 /*static*/ void dump_die (struct die_info *, int max_level);
1748
1749 static void store_in_ref_table (struct die_info *,
1750 struct dwarf2_cu *);
1751
1752 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1753
1754 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1755
1756 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1757 const struct attribute *,
1758 struct dwarf2_cu **);
1759
1760 static struct die_info *follow_die_ref (struct die_info *,
1761 const struct attribute *,
1762 struct dwarf2_cu **);
1763
1764 static struct die_info *follow_die_sig (struct die_info *,
1765 const struct attribute *,
1766 struct dwarf2_cu **);
1767
1768 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1769 struct dwarf2_cu *);
1770
1771 static struct type *get_DW_AT_signature_type (struct die_info *,
1772 const struct attribute *,
1773 struct dwarf2_cu *);
1774
1775 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1776
1777 static void read_signatured_type (struct signatured_type *);
1778
1779 static int attr_to_dynamic_prop (const struct attribute *attr,
1780 struct die_info *die, struct dwarf2_cu *cu,
1781 struct dynamic_prop *prop);
1782
1783 /* memory allocation interface */
1784
1785 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1786
1787 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1788
1789 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1790
1791 static int attr_form_is_block (const struct attribute *);
1792
1793 static int attr_form_is_section_offset (const struct attribute *);
1794
1795 static int attr_form_is_constant (const struct attribute *);
1796
1797 static int attr_form_is_ref (const struct attribute *);
1798
1799 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1800 struct dwarf2_loclist_baton *baton,
1801 const struct attribute *attr);
1802
1803 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1804 struct symbol *sym,
1805 struct dwarf2_cu *cu,
1806 int is_block);
1807
1808 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1809 const gdb_byte *info_ptr,
1810 struct abbrev_info *abbrev);
1811
1812 static hashval_t partial_die_hash (const void *item);
1813
1814 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1815
1816 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1817 (sect_offset sect_off, unsigned int offset_in_dwz,
1818 struct dwarf2_per_objfile *dwarf2_per_objfile);
1819
1820 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1821 struct die_info *comp_unit_die,
1822 enum language pretend_language);
1823
1824 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1825
1826 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1827
1828 static struct type *set_die_type (struct die_info *, struct type *,
1829 struct dwarf2_cu *);
1830
1831 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1832
1833 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1834
1835 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1836 enum language);
1837
1838 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1839 enum language);
1840
1841 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1842 enum language);
1843
1844 static void dwarf2_add_dependence (struct dwarf2_cu *,
1845 struct dwarf2_per_cu_data *);
1846
1847 static void dwarf2_mark (struct dwarf2_cu *);
1848
1849 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1850
1851 static struct type *get_die_type_at_offset (sect_offset,
1852 struct dwarf2_per_cu_data *);
1853
1854 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1855
1856 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1857 enum language pretend_language);
1858
1859 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1860
1861 /* Class, the destructor of which frees all allocated queue entries. This
1862 will only have work to do if an error was thrown while processing the
1863 dwarf. If no error was thrown then the queue entries should have all
1864 been processed, and freed, as we went along. */
1865
1866 class dwarf2_queue_guard
1867 {
1868 public:
1869 dwarf2_queue_guard () = default;
1870
1871 /* Free any entries remaining on the queue. There should only be
1872 entries left if we hit an error while processing the dwarf. */
1873 ~dwarf2_queue_guard ()
1874 {
1875 struct dwarf2_queue_item *item, *last;
1876
1877 item = dwarf2_queue;
1878 while (item)
1879 {
1880 /* Anything still marked queued is likely to be in an
1881 inconsistent state, so discard it. */
1882 if (item->per_cu->queued)
1883 {
1884 if (item->per_cu->cu != NULL)
1885 free_one_cached_comp_unit (item->per_cu);
1886 item->per_cu->queued = 0;
1887 }
1888
1889 last = item;
1890 item = item->next;
1891 xfree (last);
1892 }
1893
1894 dwarf2_queue = dwarf2_queue_tail = NULL;
1895 }
1896 };
1897
1898 /* The return type of find_file_and_directory. Note, the enclosed
1899 string pointers are only valid while this object is valid. */
1900
1901 struct file_and_directory
1902 {
1903 /* The filename. This is never NULL. */
1904 const char *name;
1905
1906 /* The compilation directory. NULL if not known. If we needed to
1907 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1908 points directly to the DW_AT_comp_dir string attribute owned by
1909 the obstack that owns the DIE. */
1910 const char *comp_dir;
1911
1912 /* If we needed to build a new string for comp_dir, this is what
1913 owns the storage. */
1914 std::string comp_dir_storage;
1915 };
1916
1917 static file_and_directory find_file_and_directory (struct die_info *die,
1918 struct dwarf2_cu *cu);
1919
1920 static char *file_full_name (int file, struct line_header *lh,
1921 const char *comp_dir);
1922
1923 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1924 enum class rcuh_kind { COMPILE, TYPE };
1925
1926 static const gdb_byte *read_and_check_comp_unit_head
1927 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1928 struct comp_unit_head *header,
1929 struct dwarf2_section_info *section,
1930 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1931 rcuh_kind section_kind);
1932
1933 static void init_cutu_and_read_dies
1934 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1935 int use_existing_cu, int keep,
1936 die_reader_func_ftype *die_reader_func, void *data);
1937
1938 static void init_cutu_and_read_dies_simple
1939 (struct dwarf2_per_cu_data *this_cu,
1940 die_reader_func_ftype *die_reader_func, void *data);
1941
1942 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1943
1944 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1945
1946 static struct dwo_unit *lookup_dwo_unit_in_dwp
1947 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1948 struct dwp_file *dwp_file, const char *comp_dir,
1949 ULONGEST signature, int is_debug_types);
1950
1951 static struct dwp_file *get_dwp_file
1952 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1953
1954 static struct dwo_unit *lookup_dwo_comp_unit
1955 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1956
1957 static struct dwo_unit *lookup_dwo_type_unit
1958 (struct signatured_type *, const char *, const char *);
1959
1960 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1961
1962 static void free_dwo_file (struct dwo_file *);
1963
1964 /* A unique_ptr helper to free a dwo_file. */
1965
1966 struct dwo_file_deleter
1967 {
1968 void operator() (struct dwo_file *df) const
1969 {
1970 free_dwo_file (df);
1971 }
1972 };
1973
1974 /* A unique pointer to a dwo_file. */
1975
1976 typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
1977
1978 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1979
1980 static void check_producer (struct dwarf2_cu *cu);
1981
1982 static void free_line_header_voidp (void *arg);
1983 \f
1984 /* Various complaints about symbol reading that don't abort the process. */
1985
1986 static void
1987 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1988 {
1989 complaint (&symfile_complaints,
1990 _("statement list doesn't fit in .debug_line section"));
1991 }
1992
1993 static void
1994 dwarf2_debug_line_missing_file_complaint (void)
1995 {
1996 complaint (&symfile_complaints,
1997 _(".debug_line section has line data without a file"));
1998 }
1999
2000 static void
2001 dwarf2_debug_line_missing_end_sequence_complaint (void)
2002 {
2003 complaint (&symfile_complaints,
2004 _(".debug_line section has line "
2005 "program sequence without an end"));
2006 }
2007
2008 static void
2009 dwarf2_complex_location_expr_complaint (void)
2010 {
2011 complaint (&symfile_complaints, _("location expression too complex"));
2012 }
2013
2014 static void
2015 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2016 int arg3)
2017 {
2018 complaint (&symfile_complaints,
2019 _("const value length mismatch for '%s', got %d, expected %d"),
2020 arg1, arg2, arg3);
2021 }
2022
2023 static void
2024 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2025 {
2026 complaint (&symfile_complaints,
2027 _("debug info runs off end of %s section"
2028 " [in module %s]"),
2029 get_section_name (section),
2030 get_section_file_name (section));
2031 }
2032
2033 static void
2034 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2035 {
2036 complaint (&symfile_complaints,
2037 _("macro debug info contains a "
2038 "malformed macro definition:\n`%s'"),
2039 arg1);
2040 }
2041
2042 static void
2043 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2044 {
2045 complaint (&symfile_complaints,
2046 _("invalid attribute class or form for '%s' in '%s'"),
2047 arg1, arg2);
2048 }
2049
2050 /* Hash function for line_header_hash. */
2051
2052 static hashval_t
2053 line_header_hash (const struct line_header *ofs)
2054 {
2055 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2056 }
2057
2058 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2059
2060 static hashval_t
2061 line_header_hash_voidp (const void *item)
2062 {
2063 const struct line_header *ofs = (const struct line_header *) item;
2064
2065 return line_header_hash (ofs);
2066 }
2067
2068 /* Equality function for line_header_hash. */
2069
2070 static int
2071 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2072 {
2073 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2074 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2075
2076 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2077 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2078 }
2079
2080 \f
2081
2082 /* Read the given attribute value as an address, taking the attribute's
2083 form into account. */
2084
2085 static CORE_ADDR
2086 attr_value_as_address (struct attribute *attr)
2087 {
2088 CORE_ADDR addr;
2089
2090 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2091 {
2092 /* Aside from a few clearly defined exceptions, attributes that
2093 contain an address must always be in DW_FORM_addr form.
2094 Unfortunately, some compilers happen to be violating this
2095 requirement by encoding addresses using other forms, such
2096 as DW_FORM_data4 for example. For those broken compilers,
2097 we try to do our best, without any guarantee of success,
2098 to interpret the address correctly. It would also be nice
2099 to generate a complaint, but that would require us to maintain
2100 a list of legitimate cases where a non-address form is allowed,
2101 as well as update callers to pass in at least the CU's DWARF
2102 version. This is more overhead than what we're willing to
2103 expand for a pretty rare case. */
2104 addr = DW_UNSND (attr);
2105 }
2106 else
2107 addr = DW_ADDR (attr);
2108
2109 return addr;
2110 }
2111
2112 /* See declaration. */
2113
2114 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2115 const dwarf2_debug_sections *names)
2116 : objfile (objfile_)
2117 {
2118 if (names == NULL)
2119 names = &dwarf2_elf_names;
2120
2121 bfd *obfd = objfile->obfd;
2122
2123 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2124 locate_sections (obfd, sec, *names);
2125 }
2126
2127 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2128
2129 dwarf2_per_objfile::~dwarf2_per_objfile ()
2130 {
2131 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2132 free_cached_comp_units ();
2133
2134 if (quick_file_names_table)
2135 htab_delete (quick_file_names_table);
2136
2137 if (line_header_hash)
2138 htab_delete (line_header_hash);
2139
2140 for (int ix = 0; ix < n_comp_units; ++ix)
2141 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2142
2143 for (int ix = 0; ix < n_type_units; ++ix)
2144 VEC_free (dwarf2_per_cu_ptr,
2145 all_type_units[ix]->per_cu.imported_symtabs);
2146 xfree (all_type_units);
2147
2148 VEC_free (dwarf2_section_info_def, types);
2149
2150 if (dwo_files != NULL)
2151 free_dwo_files (dwo_files, objfile);
2152 if (dwp_file != NULL)
2153 gdb_bfd_unref (dwp_file->dbfd);
2154
2155 if (dwz_file != NULL && dwz_file->dwz_bfd)
2156 gdb_bfd_unref (dwz_file->dwz_bfd);
2157
2158 if (index_table != NULL)
2159 index_table->~mapped_index ();
2160
2161 /* Everything else should be on the objfile obstack. */
2162 }
2163
2164 /* See declaration. */
2165
2166 void
2167 dwarf2_per_objfile::free_cached_comp_units ()
2168 {
2169 dwarf2_per_cu_data *per_cu = read_in_chain;
2170 dwarf2_per_cu_data **last_chain = &read_in_chain;
2171 while (per_cu != NULL)
2172 {
2173 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2174
2175 delete per_cu->cu;
2176 *last_chain = next_cu;
2177 per_cu = next_cu;
2178 }
2179 }
2180
2181 /* A helper class that calls free_cached_comp_units on
2182 destruction. */
2183
2184 class free_cached_comp_units
2185 {
2186 public:
2187
2188 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2189 : m_per_objfile (per_objfile)
2190 {
2191 }
2192
2193 ~free_cached_comp_units ()
2194 {
2195 m_per_objfile->free_cached_comp_units ();
2196 }
2197
2198 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2199
2200 private:
2201
2202 dwarf2_per_objfile *m_per_objfile;
2203 };
2204
2205 /* Try to locate the sections we need for DWARF 2 debugging
2206 information and return true if we have enough to do something.
2207 NAMES points to the dwarf2 section names, or is NULL if the standard
2208 ELF names are used. */
2209
2210 int
2211 dwarf2_has_info (struct objfile *objfile,
2212 const struct dwarf2_debug_sections *names)
2213 {
2214 if (objfile->flags & OBJF_READNEVER)
2215 return 0;
2216
2217 struct dwarf2_per_objfile *dwarf2_per_objfile
2218 = get_dwarf2_per_objfile (objfile);
2219
2220 if (dwarf2_per_objfile == NULL)
2221 {
2222 /* Initialize per-objfile state. */
2223 dwarf2_per_objfile
2224 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2225 names);
2226 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2227 }
2228 return (!dwarf2_per_objfile->info.is_virtual
2229 && dwarf2_per_objfile->info.s.section != NULL
2230 && !dwarf2_per_objfile->abbrev.is_virtual
2231 && dwarf2_per_objfile->abbrev.s.section != NULL);
2232 }
2233
2234 /* Return the containing section of virtual section SECTION. */
2235
2236 static struct dwarf2_section_info *
2237 get_containing_section (const struct dwarf2_section_info *section)
2238 {
2239 gdb_assert (section->is_virtual);
2240 return section->s.containing_section;
2241 }
2242
2243 /* Return the bfd owner of SECTION. */
2244
2245 static struct bfd *
2246 get_section_bfd_owner (const struct dwarf2_section_info *section)
2247 {
2248 if (section->is_virtual)
2249 {
2250 section = get_containing_section (section);
2251 gdb_assert (!section->is_virtual);
2252 }
2253 return section->s.section->owner;
2254 }
2255
2256 /* Return the bfd section of SECTION.
2257 Returns NULL if the section is not present. */
2258
2259 static asection *
2260 get_section_bfd_section (const struct dwarf2_section_info *section)
2261 {
2262 if (section->is_virtual)
2263 {
2264 section = get_containing_section (section);
2265 gdb_assert (!section->is_virtual);
2266 }
2267 return section->s.section;
2268 }
2269
2270 /* Return the name of SECTION. */
2271
2272 static const char *
2273 get_section_name (const struct dwarf2_section_info *section)
2274 {
2275 asection *sectp = get_section_bfd_section (section);
2276
2277 gdb_assert (sectp != NULL);
2278 return bfd_section_name (get_section_bfd_owner (section), sectp);
2279 }
2280
2281 /* Return the name of the file SECTION is in. */
2282
2283 static const char *
2284 get_section_file_name (const struct dwarf2_section_info *section)
2285 {
2286 bfd *abfd = get_section_bfd_owner (section);
2287
2288 return bfd_get_filename (abfd);
2289 }
2290
2291 /* Return the id of SECTION.
2292 Returns 0 if SECTION doesn't exist. */
2293
2294 static int
2295 get_section_id (const struct dwarf2_section_info *section)
2296 {
2297 asection *sectp = get_section_bfd_section (section);
2298
2299 if (sectp == NULL)
2300 return 0;
2301 return sectp->id;
2302 }
2303
2304 /* Return the flags of SECTION.
2305 SECTION (or containing section if this is a virtual section) must exist. */
2306
2307 static int
2308 get_section_flags (const struct dwarf2_section_info *section)
2309 {
2310 asection *sectp = get_section_bfd_section (section);
2311
2312 gdb_assert (sectp != NULL);
2313 return bfd_get_section_flags (sectp->owner, sectp);
2314 }
2315
2316 /* When loading sections, we look either for uncompressed section or for
2317 compressed section names. */
2318
2319 static int
2320 section_is_p (const char *section_name,
2321 const struct dwarf2_section_names *names)
2322 {
2323 if (names->normal != NULL
2324 && strcmp (section_name, names->normal) == 0)
2325 return 1;
2326 if (names->compressed != NULL
2327 && strcmp (section_name, names->compressed) == 0)
2328 return 1;
2329 return 0;
2330 }
2331
2332 /* See declaration. */
2333
2334 void
2335 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2336 const dwarf2_debug_sections &names)
2337 {
2338 flagword aflag = bfd_get_section_flags (abfd, sectp);
2339
2340 if ((aflag & SEC_HAS_CONTENTS) == 0)
2341 {
2342 }
2343 else if (section_is_p (sectp->name, &names.info))
2344 {
2345 this->info.s.section = sectp;
2346 this->info.size = bfd_get_section_size (sectp);
2347 }
2348 else if (section_is_p (sectp->name, &names.abbrev))
2349 {
2350 this->abbrev.s.section = sectp;
2351 this->abbrev.size = bfd_get_section_size (sectp);
2352 }
2353 else if (section_is_p (sectp->name, &names.line))
2354 {
2355 this->line.s.section = sectp;
2356 this->line.size = bfd_get_section_size (sectp);
2357 }
2358 else if (section_is_p (sectp->name, &names.loc))
2359 {
2360 this->loc.s.section = sectp;
2361 this->loc.size = bfd_get_section_size (sectp);
2362 }
2363 else if (section_is_p (sectp->name, &names.loclists))
2364 {
2365 this->loclists.s.section = sectp;
2366 this->loclists.size = bfd_get_section_size (sectp);
2367 }
2368 else if (section_is_p (sectp->name, &names.macinfo))
2369 {
2370 this->macinfo.s.section = sectp;
2371 this->macinfo.size = bfd_get_section_size (sectp);
2372 }
2373 else if (section_is_p (sectp->name, &names.macro))
2374 {
2375 this->macro.s.section = sectp;
2376 this->macro.size = bfd_get_section_size (sectp);
2377 }
2378 else if (section_is_p (sectp->name, &names.str))
2379 {
2380 this->str.s.section = sectp;
2381 this->str.size = bfd_get_section_size (sectp);
2382 }
2383 else if (section_is_p (sectp->name, &names.line_str))
2384 {
2385 this->line_str.s.section = sectp;
2386 this->line_str.size = bfd_get_section_size (sectp);
2387 }
2388 else if (section_is_p (sectp->name, &names.addr))
2389 {
2390 this->addr.s.section = sectp;
2391 this->addr.size = bfd_get_section_size (sectp);
2392 }
2393 else if (section_is_p (sectp->name, &names.frame))
2394 {
2395 this->frame.s.section = sectp;
2396 this->frame.size = bfd_get_section_size (sectp);
2397 }
2398 else if (section_is_p (sectp->name, &names.eh_frame))
2399 {
2400 this->eh_frame.s.section = sectp;
2401 this->eh_frame.size = bfd_get_section_size (sectp);
2402 }
2403 else if (section_is_p (sectp->name, &names.ranges))
2404 {
2405 this->ranges.s.section = sectp;
2406 this->ranges.size = bfd_get_section_size (sectp);
2407 }
2408 else if (section_is_p (sectp->name, &names.rnglists))
2409 {
2410 this->rnglists.s.section = sectp;
2411 this->rnglists.size = bfd_get_section_size (sectp);
2412 }
2413 else if (section_is_p (sectp->name, &names.types))
2414 {
2415 struct dwarf2_section_info type_section;
2416
2417 memset (&type_section, 0, sizeof (type_section));
2418 type_section.s.section = sectp;
2419 type_section.size = bfd_get_section_size (sectp);
2420
2421 VEC_safe_push (dwarf2_section_info_def, this->types,
2422 &type_section);
2423 }
2424 else if (section_is_p (sectp->name, &names.gdb_index))
2425 {
2426 this->gdb_index.s.section = sectp;
2427 this->gdb_index.size = bfd_get_section_size (sectp);
2428 }
2429 else if (section_is_p (sectp->name, &names.debug_names))
2430 {
2431 this->debug_names.s.section = sectp;
2432 this->debug_names.size = bfd_get_section_size (sectp);
2433 }
2434 else if (section_is_p (sectp->name, &names.debug_aranges))
2435 {
2436 this->debug_aranges.s.section = sectp;
2437 this->debug_aranges.size = bfd_get_section_size (sectp);
2438 }
2439
2440 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2441 && bfd_section_vma (abfd, sectp) == 0)
2442 this->has_section_at_zero = true;
2443 }
2444
2445 /* A helper function that decides whether a section is empty,
2446 or not present. */
2447
2448 static int
2449 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2450 {
2451 if (section->is_virtual)
2452 return section->size == 0;
2453 return section->s.section == NULL || section->size == 0;
2454 }
2455
2456 /* See dwarf2read.h. */
2457
2458 void
2459 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2460 {
2461 asection *sectp;
2462 bfd *abfd;
2463 gdb_byte *buf, *retbuf;
2464
2465 if (info->readin)
2466 return;
2467 info->buffer = NULL;
2468 info->readin = 1;
2469
2470 if (dwarf2_section_empty_p (info))
2471 return;
2472
2473 sectp = get_section_bfd_section (info);
2474
2475 /* If this is a virtual section we need to read in the real one first. */
2476 if (info->is_virtual)
2477 {
2478 struct dwarf2_section_info *containing_section =
2479 get_containing_section (info);
2480
2481 gdb_assert (sectp != NULL);
2482 if ((sectp->flags & SEC_RELOC) != 0)
2483 {
2484 error (_("Dwarf Error: DWP format V2 with relocations is not"
2485 " supported in section %s [in module %s]"),
2486 get_section_name (info), get_section_file_name (info));
2487 }
2488 dwarf2_read_section (objfile, containing_section);
2489 /* Other code should have already caught virtual sections that don't
2490 fit. */
2491 gdb_assert (info->virtual_offset + info->size
2492 <= containing_section->size);
2493 /* If the real section is empty or there was a problem reading the
2494 section we shouldn't get here. */
2495 gdb_assert (containing_section->buffer != NULL);
2496 info->buffer = containing_section->buffer + info->virtual_offset;
2497 return;
2498 }
2499
2500 /* If the section has relocations, we must read it ourselves.
2501 Otherwise we attach it to the BFD. */
2502 if ((sectp->flags & SEC_RELOC) == 0)
2503 {
2504 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2505 return;
2506 }
2507
2508 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2509 info->buffer = buf;
2510
2511 /* When debugging .o files, we may need to apply relocations; see
2512 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2513 We never compress sections in .o files, so we only need to
2514 try this when the section is not compressed. */
2515 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2516 if (retbuf != NULL)
2517 {
2518 info->buffer = retbuf;
2519 return;
2520 }
2521
2522 abfd = get_section_bfd_owner (info);
2523 gdb_assert (abfd != NULL);
2524
2525 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2526 || bfd_bread (buf, info->size, abfd) != info->size)
2527 {
2528 error (_("Dwarf Error: Can't read DWARF data"
2529 " in section %s [in module %s]"),
2530 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2531 }
2532 }
2533
2534 /* A helper function that returns the size of a section in a safe way.
2535 If you are positive that the section has been read before using the
2536 size, then it is safe to refer to the dwarf2_section_info object's
2537 "size" field directly. In other cases, you must call this
2538 function, because for compressed sections the size field is not set
2539 correctly until the section has been read. */
2540
2541 static bfd_size_type
2542 dwarf2_section_size (struct objfile *objfile,
2543 struct dwarf2_section_info *info)
2544 {
2545 if (!info->readin)
2546 dwarf2_read_section (objfile, info);
2547 return info->size;
2548 }
2549
2550 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2551 SECTION_NAME. */
2552
2553 void
2554 dwarf2_get_section_info (struct objfile *objfile,
2555 enum dwarf2_section_enum sect,
2556 asection **sectp, const gdb_byte **bufp,
2557 bfd_size_type *sizep)
2558 {
2559 struct dwarf2_per_objfile *data
2560 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2561 dwarf2_objfile_data_key);
2562 struct dwarf2_section_info *info;
2563
2564 /* We may see an objfile without any DWARF, in which case we just
2565 return nothing. */
2566 if (data == NULL)
2567 {
2568 *sectp = NULL;
2569 *bufp = NULL;
2570 *sizep = 0;
2571 return;
2572 }
2573 switch (sect)
2574 {
2575 case DWARF2_DEBUG_FRAME:
2576 info = &data->frame;
2577 break;
2578 case DWARF2_EH_FRAME:
2579 info = &data->eh_frame;
2580 break;
2581 default:
2582 gdb_assert_not_reached ("unexpected section");
2583 }
2584
2585 dwarf2_read_section (objfile, info);
2586
2587 *sectp = get_section_bfd_section (info);
2588 *bufp = info->buffer;
2589 *sizep = info->size;
2590 }
2591
2592 /* A helper function to find the sections for a .dwz file. */
2593
2594 static void
2595 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2596 {
2597 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2598
2599 /* Note that we only support the standard ELF names, because .dwz
2600 is ELF-only (at the time of writing). */
2601 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2602 {
2603 dwz_file->abbrev.s.section = sectp;
2604 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2605 }
2606 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2607 {
2608 dwz_file->info.s.section = sectp;
2609 dwz_file->info.size = bfd_get_section_size (sectp);
2610 }
2611 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2612 {
2613 dwz_file->str.s.section = sectp;
2614 dwz_file->str.size = bfd_get_section_size (sectp);
2615 }
2616 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2617 {
2618 dwz_file->line.s.section = sectp;
2619 dwz_file->line.size = bfd_get_section_size (sectp);
2620 }
2621 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2622 {
2623 dwz_file->macro.s.section = sectp;
2624 dwz_file->macro.size = bfd_get_section_size (sectp);
2625 }
2626 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2627 {
2628 dwz_file->gdb_index.s.section = sectp;
2629 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2630 }
2631 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2632 {
2633 dwz_file->debug_names.s.section = sectp;
2634 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2635 }
2636 }
2637
2638 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2639 there is no .gnu_debugaltlink section in the file. Error if there
2640 is such a section but the file cannot be found. */
2641
2642 static struct dwz_file *
2643 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2644 {
2645 const char *filename;
2646 struct dwz_file *result;
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;
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 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2699 struct dwz_file);
2700 result->dwz_bfd = dwz_bfd.release ();
2701
2702 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2703
2704 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2705 dwarf2_per_objfile->dwz_file = result;
2706 return result;
2707 }
2708 \f
2709 /* DWARF quick_symbols_functions support. */
2710
2711 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2712 unique line tables, so we maintain a separate table of all .debug_line
2713 derived entries to support the sharing.
2714 All the quick functions need is the list of file names. We discard the
2715 line_header when we're done and don't need to record it here. */
2716 struct quick_file_names
2717 {
2718 /* The data used to construct the hash key. */
2719 struct stmt_list_hash hash;
2720
2721 /* The number of entries in file_names, real_names. */
2722 unsigned int num_file_names;
2723
2724 /* The file names from the line table, after being run through
2725 file_full_name. */
2726 const char **file_names;
2727
2728 /* The file names from the line table after being run through
2729 gdb_realpath. These are computed lazily. */
2730 const char **real_names;
2731 };
2732
2733 /* When using the index (and thus not using psymtabs), each CU has an
2734 object of this type. This is used to hold information needed by
2735 the various "quick" methods. */
2736 struct dwarf2_per_cu_quick_data
2737 {
2738 /* The file table. This can be NULL if there was no file table
2739 or it's currently not read in.
2740 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2741 struct quick_file_names *file_names;
2742
2743 /* The corresponding symbol table. This is NULL if symbols for this
2744 CU have not yet been read. */
2745 struct compunit_symtab *compunit_symtab;
2746
2747 /* A temporary mark bit used when iterating over all CUs in
2748 expand_symtabs_matching. */
2749 unsigned int mark : 1;
2750
2751 /* True if we've tried to read the file table and found there isn't one.
2752 There will be no point in trying to read it again next time. */
2753 unsigned int no_file_data : 1;
2754 };
2755
2756 /* Utility hash function for a stmt_list_hash. */
2757
2758 static hashval_t
2759 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2760 {
2761 hashval_t v = 0;
2762
2763 if (stmt_list_hash->dwo_unit != NULL)
2764 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2765 v += to_underlying (stmt_list_hash->line_sect_off);
2766 return v;
2767 }
2768
2769 /* Utility equality function for a stmt_list_hash. */
2770
2771 static int
2772 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2773 const struct stmt_list_hash *rhs)
2774 {
2775 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2776 return 0;
2777 if (lhs->dwo_unit != NULL
2778 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2779 return 0;
2780
2781 return lhs->line_sect_off == rhs->line_sect_off;
2782 }
2783
2784 /* Hash function for a quick_file_names. */
2785
2786 static hashval_t
2787 hash_file_name_entry (const void *e)
2788 {
2789 const struct quick_file_names *file_data
2790 = (const struct quick_file_names *) e;
2791
2792 return hash_stmt_list_entry (&file_data->hash);
2793 }
2794
2795 /* Equality function for a quick_file_names. */
2796
2797 static int
2798 eq_file_name_entry (const void *a, const void *b)
2799 {
2800 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2801 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2802
2803 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2804 }
2805
2806 /* Delete function for a quick_file_names. */
2807
2808 static void
2809 delete_file_name_entry (void *e)
2810 {
2811 struct quick_file_names *file_data = (struct quick_file_names *) e;
2812 int i;
2813
2814 for (i = 0; i < file_data->num_file_names; ++i)
2815 {
2816 xfree ((void*) file_data->file_names[i]);
2817 if (file_data->real_names)
2818 xfree ((void*) file_data->real_names[i]);
2819 }
2820
2821 /* The space for the struct itself lives on objfile_obstack,
2822 so we don't free it here. */
2823 }
2824
2825 /* Create a quick_file_names hash table. */
2826
2827 static htab_t
2828 create_quick_file_names_table (unsigned int nr_initial_entries)
2829 {
2830 return htab_create_alloc (nr_initial_entries,
2831 hash_file_name_entry, eq_file_name_entry,
2832 delete_file_name_entry, xcalloc, xfree);
2833 }
2834
2835 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2836 have to be created afterwards. You should call age_cached_comp_units after
2837 processing PER_CU->CU. dw2_setup must have been already called. */
2838
2839 static void
2840 load_cu (struct dwarf2_per_cu_data *per_cu)
2841 {
2842 if (per_cu->is_debug_types)
2843 load_full_type_unit (per_cu);
2844 else
2845 load_full_comp_unit (per_cu, language_minimal);
2846
2847 if (per_cu->cu == NULL)
2848 return; /* Dummy CU. */
2849
2850 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2851 }
2852
2853 /* Read in the symbols for PER_CU. */
2854
2855 static void
2856 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2857 {
2858 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2859
2860 /* Skip type_unit_groups, reading the type units they contain
2861 is handled elsewhere. */
2862 if (IS_TYPE_UNIT_GROUP (per_cu))
2863 return;
2864
2865 /* The destructor of dwarf2_queue_guard frees any entries left on
2866 the queue. After this point we're guaranteed to leave this function
2867 with the dwarf queue empty. */
2868 dwarf2_queue_guard q_guard;
2869
2870 if (dwarf2_per_objfile->using_index
2871 ? per_cu->v.quick->compunit_symtab == NULL
2872 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2873 {
2874 queue_comp_unit (per_cu, language_minimal);
2875 load_cu (per_cu);
2876
2877 /* If we just loaded a CU from a DWO, and we're working with an index
2878 that may badly handle TUs, load all the TUs in that DWO as well.
2879 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2880 if (!per_cu->is_debug_types
2881 && per_cu->cu != NULL
2882 && per_cu->cu->dwo_unit != NULL
2883 && dwarf2_per_objfile->index_table != NULL
2884 && dwarf2_per_objfile->index_table->version <= 7
2885 /* DWP files aren't supported yet. */
2886 && get_dwp_file (dwarf2_per_objfile) == NULL)
2887 queue_and_load_all_dwo_tus (per_cu);
2888 }
2889
2890 process_queue (dwarf2_per_objfile);
2891
2892 /* Age the cache, releasing compilation units that have not
2893 been used recently. */
2894 age_cached_comp_units (dwarf2_per_objfile);
2895 }
2896
2897 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2898 the objfile from which this CU came. Returns the resulting symbol
2899 table. */
2900
2901 static struct compunit_symtab *
2902 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2903 {
2904 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2905
2906 gdb_assert (dwarf2_per_objfile->using_index);
2907 if (!per_cu->v.quick->compunit_symtab)
2908 {
2909 free_cached_comp_units freer (dwarf2_per_objfile);
2910 scoped_restore decrementer = increment_reading_symtab ();
2911 dw2_do_instantiate_symtab (per_cu);
2912 process_cu_includes (dwarf2_per_objfile);
2913 }
2914
2915 return per_cu->v.quick->compunit_symtab;
2916 }
2917
2918 /* See declaration. */
2919
2920 dwarf2_per_cu_data *
2921 dwarf2_per_objfile::get_cutu (int index)
2922 {
2923 if (index >= this->n_comp_units)
2924 {
2925 index -= this->n_comp_units;
2926 gdb_assert (index < this->n_type_units);
2927 return &this->all_type_units[index]->per_cu;
2928 }
2929
2930 return this->all_comp_units[index];
2931 }
2932
2933 /* See declaration. */
2934
2935 dwarf2_per_cu_data *
2936 dwarf2_per_objfile::get_cu (int index)
2937 {
2938 gdb_assert (index >= 0 && index < this->n_comp_units);
2939
2940 return this->all_comp_units[index];
2941 }
2942
2943 /* See declaration. */
2944
2945 signatured_type *
2946 dwarf2_per_objfile::get_tu (int index)
2947 {
2948 gdb_assert (index >= 0 && index < this->n_type_units);
2949
2950 return this->all_type_units[index];
2951 }
2952
2953 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2954 objfile_obstack, and constructed with the specified field
2955 values. */
2956
2957 static dwarf2_per_cu_data *
2958 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2959 struct dwarf2_section_info *section,
2960 int is_dwz,
2961 sect_offset sect_off, ULONGEST length)
2962 {
2963 struct objfile *objfile = dwarf2_per_objfile->objfile;
2964 dwarf2_per_cu_data *the_cu
2965 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2966 struct dwarf2_per_cu_data);
2967 the_cu->sect_off = sect_off;
2968 the_cu->length = length;
2969 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2970 the_cu->section = section;
2971 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2972 struct dwarf2_per_cu_quick_data);
2973 the_cu->is_dwz = is_dwz;
2974 return the_cu;
2975 }
2976
2977 /* A helper for create_cus_from_index that handles a given list of
2978 CUs. */
2979
2980 static void
2981 create_cus_from_index_list (struct objfile *objfile,
2982 const gdb_byte *cu_list, offset_type n_elements,
2983 struct dwarf2_section_info *section,
2984 int is_dwz,
2985 int base_offset)
2986 {
2987 offset_type i;
2988 struct dwarf2_per_objfile *dwarf2_per_objfile
2989 = get_dwarf2_per_objfile (objfile);
2990
2991 for (i = 0; i < n_elements; i += 2)
2992 {
2993 gdb_static_assert (sizeof (ULONGEST) >= 8);
2994
2995 sect_offset sect_off
2996 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2997 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2998 cu_list += 2 * 8;
2999
3000 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3001 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3002 sect_off, length);
3003 }
3004 }
3005
3006 /* Read the CU list from the mapped index, and use it to create all
3007 the CU objects for this objfile. */
3008
3009 static void
3010 create_cus_from_index (struct objfile *objfile,
3011 const gdb_byte *cu_list, offset_type cu_list_elements,
3012 const gdb_byte *dwz_list, offset_type dwz_elements)
3013 {
3014 struct dwz_file *dwz;
3015 struct dwarf2_per_objfile *dwarf2_per_objfile
3016 = get_dwarf2_per_objfile (objfile);
3017
3018 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3019 dwarf2_per_objfile->all_comp_units =
3020 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3021 dwarf2_per_objfile->n_comp_units);
3022
3023 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3024 &dwarf2_per_objfile->info, 0, 0);
3025
3026 if (dwz_elements == 0)
3027 return;
3028
3029 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3030 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3031 cu_list_elements / 2);
3032 }
3033
3034 /* Create the signatured type hash table from the index. */
3035
3036 static void
3037 create_signatured_type_table_from_index (struct objfile *objfile,
3038 struct dwarf2_section_info *section,
3039 const gdb_byte *bytes,
3040 offset_type elements)
3041 {
3042 offset_type i;
3043 htab_t sig_types_hash;
3044 struct dwarf2_per_objfile *dwarf2_per_objfile
3045 = get_dwarf2_per_objfile (objfile);
3046
3047 dwarf2_per_objfile->n_type_units
3048 = dwarf2_per_objfile->n_allocated_type_units
3049 = elements / 3;
3050 dwarf2_per_objfile->all_type_units =
3051 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3052
3053 sig_types_hash = allocate_signatured_type_table (objfile);
3054
3055 for (i = 0; i < elements; i += 3)
3056 {
3057 struct signatured_type *sig_type;
3058 ULONGEST signature;
3059 void **slot;
3060 cu_offset type_offset_in_tu;
3061
3062 gdb_static_assert (sizeof (ULONGEST) >= 8);
3063 sect_offset sect_off
3064 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3065 type_offset_in_tu
3066 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3067 BFD_ENDIAN_LITTLE);
3068 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3069 bytes += 3 * 8;
3070
3071 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3072 struct signatured_type);
3073 sig_type->signature = signature;
3074 sig_type->type_offset_in_tu = type_offset_in_tu;
3075 sig_type->per_cu.is_debug_types = 1;
3076 sig_type->per_cu.section = section;
3077 sig_type->per_cu.sect_off = sect_off;
3078 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3079 sig_type->per_cu.v.quick
3080 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3081 struct dwarf2_per_cu_quick_data);
3082
3083 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3084 *slot = sig_type;
3085
3086 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3087 }
3088
3089 dwarf2_per_objfile->signatured_types = sig_types_hash;
3090 }
3091
3092 /* Create the signatured type hash table from .debug_names. */
3093
3094 static void
3095 create_signatured_type_table_from_debug_names
3096 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3097 const mapped_debug_names &map,
3098 struct dwarf2_section_info *section,
3099 struct dwarf2_section_info *abbrev_section)
3100 {
3101 struct objfile *objfile = dwarf2_per_objfile->objfile;
3102
3103 dwarf2_read_section (objfile, section);
3104 dwarf2_read_section (objfile, abbrev_section);
3105
3106 dwarf2_per_objfile->n_type_units
3107 = dwarf2_per_objfile->n_allocated_type_units
3108 = map.tu_count;
3109 dwarf2_per_objfile->all_type_units
3110 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3111
3112 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3113
3114 for (uint32_t i = 0; i < map.tu_count; ++i)
3115 {
3116 struct signatured_type *sig_type;
3117 void **slot;
3118
3119 sect_offset sect_off
3120 = (sect_offset) (extract_unsigned_integer
3121 (map.tu_table_reordered + i * map.offset_size,
3122 map.offset_size,
3123 map.dwarf5_byte_order));
3124
3125 comp_unit_head cu_header;
3126 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3127 abbrev_section,
3128 section->buffer + to_underlying (sect_off),
3129 rcuh_kind::TYPE);
3130
3131 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3132 struct signatured_type);
3133 sig_type->signature = cu_header.signature;
3134 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3135 sig_type->per_cu.is_debug_types = 1;
3136 sig_type->per_cu.section = section;
3137 sig_type->per_cu.sect_off = sect_off;
3138 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3139 sig_type->per_cu.v.quick
3140 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3141 struct dwarf2_per_cu_quick_data);
3142
3143 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3144 *slot = sig_type;
3145
3146 dwarf2_per_objfile->all_type_units[i] = sig_type;
3147 }
3148
3149 dwarf2_per_objfile->signatured_types = sig_types_hash;
3150 }
3151
3152 /* Read the address map data from the mapped index, and use it to
3153 populate the objfile's psymtabs_addrmap. */
3154
3155 static void
3156 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3157 struct mapped_index *index)
3158 {
3159 struct objfile *objfile = dwarf2_per_objfile->objfile;
3160 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3161 const gdb_byte *iter, *end;
3162 struct addrmap *mutable_map;
3163 CORE_ADDR baseaddr;
3164
3165 auto_obstack temp_obstack;
3166
3167 mutable_map = addrmap_create_mutable (&temp_obstack);
3168
3169 iter = index->address_table.data ();
3170 end = iter + index->address_table.size ();
3171
3172 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3173
3174 while (iter < end)
3175 {
3176 ULONGEST hi, lo, cu_index;
3177 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3178 iter += 8;
3179 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3180 iter += 8;
3181 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3182 iter += 4;
3183
3184 if (lo > hi)
3185 {
3186 complaint (&symfile_complaints,
3187 _(".gdb_index address table has invalid range (%s - %s)"),
3188 hex_string (lo), hex_string (hi));
3189 continue;
3190 }
3191
3192 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3193 {
3194 complaint (&symfile_complaints,
3195 _(".gdb_index address table has invalid CU number %u"),
3196 (unsigned) cu_index);
3197 continue;
3198 }
3199
3200 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3201 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3202 addrmap_set_empty (mutable_map, lo, hi - 1,
3203 dwarf2_per_objfile->get_cu (cu_index));
3204 }
3205
3206 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3207 &objfile->objfile_obstack);
3208 }
3209
3210 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3211 populate the objfile's psymtabs_addrmap. */
3212
3213 static void
3214 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3215 struct dwarf2_section_info *section)
3216 {
3217 struct objfile *objfile = dwarf2_per_objfile->objfile;
3218 bfd *abfd = objfile->obfd;
3219 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3220 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3221 SECT_OFF_TEXT (objfile));
3222
3223 auto_obstack temp_obstack;
3224 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3225
3226 std::unordered_map<sect_offset,
3227 dwarf2_per_cu_data *,
3228 gdb::hash_enum<sect_offset>>
3229 debug_info_offset_to_per_cu;
3230 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3231 {
3232 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (cui);
3233 const auto insertpair
3234 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3235 if (!insertpair.second)
3236 {
3237 warning (_("Section .debug_aranges in %s has duplicate "
3238 "debug_info_offset %s, ignoring .debug_aranges."),
3239 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3240 return;
3241 }
3242 }
3243
3244 dwarf2_read_section (objfile, section);
3245
3246 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3247
3248 const gdb_byte *addr = section->buffer;
3249
3250 while (addr < section->buffer + section->size)
3251 {
3252 const gdb_byte *const entry_addr = addr;
3253 unsigned int bytes_read;
3254
3255 const LONGEST entry_length = read_initial_length (abfd, addr,
3256 &bytes_read);
3257 addr += bytes_read;
3258
3259 const gdb_byte *const entry_end = addr + entry_length;
3260 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3261 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3262 if (addr + entry_length > section->buffer + section->size)
3263 {
3264 warning (_("Section .debug_aranges in %s entry at offset %zu "
3265 "length %s exceeds section length %s, "
3266 "ignoring .debug_aranges."),
3267 objfile_name (objfile), entry_addr - section->buffer,
3268 plongest (bytes_read + entry_length),
3269 pulongest (section->size));
3270 return;
3271 }
3272
3273 /* The version number. */
3274 const uint16_t version = read_2_bytes (abfd, addr);
3275 addr += 2;
3276 if (version != 2)
3277 {
3278 warning (_("Section .debug_aranges in %s entry at offset %zu "
3279 "has unsupported version %d, ignoring .debug_aranges."),
3280 objfile_name (objfile), entry_addr - section->buffer,
3281 version);
3282 return;
3283 }
3284
3285 const uint64_t debug_info_offset
3286 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3287 addr += offset_size;
3288 const auto per_cu_it
3289 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3290 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3291 {
3292 warning (_("Section .debug_aranges in %s entry at offset %zu "
3293 "debug_info_offset %s does not exists, "
3294 "ignoring .debug_aranges."),
3295 objfile_name (objfile), entry_addr - section->buffer,
3296 pulongest (debug_info_offset));
3297 return;
3298 }
3299 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3300
3301 const uint8_t address_size = *addr++;
3302 if (address_size < 1 || address_size > 8)
3303 {
3304 warning (_("Section .debug_aranges in %s entry at offset %zu "
3305 "address_size %u is invalid, ignoring .debug_aranges."),
3306 objfile_name (objfile), entry_addr - section->buffer,
3307 address_size);
3308 return;
3309 }
3310
3311 const uint8_t segment_selector_size = *addr++;
3312 if (segment_selector_size != 0)
3313 {
3314 warning (_("Section .debug_aranges in %s entry at offset %zu "
3315 "segment_selector_size %u is not supported, "
3316 "ignoring .debug_aranges."),
3317 objfile_name (objfile), entry_addr - section->buffer,
3318 segment_selector_size);
3319 return;
3320 }
3321
3322 /* Must pad to an alignment boundary that is twice the address
3323 size. It is undocumented by the DWARF standard but GCC does
3324 use it. */
3325 for (size_t padding = ((-(addr - section->buffer))
3326 & (2 * address_size - 1));
3327 padding > 0; padding--)
3328 if (*addr++ != 0)
3329 {
3330 warning (_("Section .debug_aranges in %s entry at offset %zu "
3331 "padding is not zero, ignoring .debug_aranges."),
3332 objfile_name (objfile), entry_addr - section->buffer);
3333 return;
3334 }
3335
3336 for (;;)
3337 {
3338 if (addr + 2 * address_size > entry_end)
3339 {
3340 warning (_("Section .debug_aranges in %s entry at offset %zu "
3341 "address list is not properly terminated, "
3342 "ignoring .debug_aranges."),
3343 objfile_name (objfile), entry_addr - section->buffer);
3344 return;
3345 }
3346 ULONGEST start = extract_unsigned_integer (addr, address_size,
3347 dwarf5_byte_order);
3348 addr += address_size;
3349 ULONGEST length = extract_unsigned_integer (addr, address_size,
3350 dwarf5_byte_order);
3351 addr += address_size;
3352 if (start == 0 && length == 0)
3353 break;
3354 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3355 {
3356 /* Symbol was eliminated due to a COMDAT group. */
3357 continue;
3358 }
3359 ULONGEST end = start + length;
3360 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3361 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3362 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3363 }
3364 }
3365
3366 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3367 &objfile->objfile_obstack);
3368 }
3369
3370 /* Find a slot in the mapped index INDEX for the object named NAME.
3371 If NAME is found, set *VEC_OUT to point to the CU vector in the
3372 constant pool and return true. If NAME cannot be found, return
3373 false. */
3374
3375 static bool
3376 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3377 offset_type **vec_out)
3378 {
3379 offset_type hash;
3380 offset_type slot, step;
3381 int (*cmp) (const char *, const char *);
3382
3383 gdb::unique_xmalloc_ptr<char> without_params;
3384 if (current_language->la_language == language_cplus
3385 || current_language->la_language == language_fortran
3386 || current_language->la_language == language_d)
3387 {
3388 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3389 not contain any. */
3390
3391 if (strchr (name, '(') != NULL)
3392 {
3393 without_params = cp_remove_params (name);
3394
3395 if (without_params != NULL)
3396 name = without_params.get ();
3397 }
3398 }
3399
3400 /* Index version 4 did not support case insensitive searches. But the
3401 indices for case insensitive languages are built in lowercase, therefore
3402 simulate our NAME being searched is also lowercased. */
3403 hash = mapped_index_string_hash ((index->version == 4
3404 && case_sensitivity == case_sensitive_off
3405 ? 5 : index->version),
3406 name);
3407
3408 slot = hash & (index->symbol_table.size () - 1);
3409 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3410 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3411
3412 for (;;)
3413 {
3414 const char *str;
3415
3416 const auto &bucket = index->symbol_table[slot];
3417 if (bucket.name == 0 && bucket.vec == 0)
3418 return false;
3419
3420 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3421 if (!cmp (name, str))
3422 {
3423 *vec_out = (offset_type *) (index->constant_pool
3424 + MAYBE_SWAP (bucket.vec));
3425 return true;
3426 }
3427
3428 slot = (slot + step) & (index->symbol_table.size () - 1);
3429 }
3430 }
3431
3432 /* A helper function that reads the .gdb_index from SECTION and fills
3433 in MAP. FILENAME is the name of the file containing the section;
3434 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3435 ok to use deprecated sections.
3436
3437 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3438 out parameters that are filled in with information about the CU and
3439 TU lists in the section.
3440
3441 Returns 1 if all went well, 0 otherwise. */
3442
3443 static int
3444 read_index_from_section (struct objfile *objfile,
3445 const char *filename,
3446 int deprecated_ok,
3447 struct dwarf2_section_info *section,
3448 struct mapped_index *map,
3449 const gdb_byte **cu_list,
3450 offset_type *cu_list_elements,
3451 const gdb_byte **types_list,
3452 offset_type *types_list_elements)
3453 {
3454 const gdb_byte *addr;
3455 offset_type version;
3456 offset_type *metadata;
3457 int i;
3458
3459 if (dwarf2_section_empty_p (section))
3460 return 0;
3461
3462 /* Older elfutils strip versions could keep the section in the main
3463 executable while splitting it for the separate debug info file. */
3464 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3465 return 0;
3466
3467 dwarf2_read_section (objfile, section);
3468
3469 addr = section->buffer;
3470 /* Version check. */
3471 version = MAYBE_SWAP (*(offset_type *) addr);
3472 /* Versions earlier than 3 emitted every copy of a psymbol. This
3473 causes the index to behave very poorly for certain requests. Version 3
3474 contained incomplete addrmap. So, it seems better to just ignore such
3475 indices. */
3476 if (version < 4)
3477 {
3478 static int warning_printed = 0;
3479 if (!warning_printed)
3480 {
3481 warning (_("Skipping obsolete .gdb_index section in %s."),
3482 filename);
3483 warning_printed = 1;
3484 }
3485 return 0;
3486 }
3487 /* Index version 4 uses a different hash function than index version
3488 5 and later.
3489
3490 Versions earlier than 6 did not emit psymbols for inlined
3491 functions. Using these files will cause GDB not to be able to
3492 set breakpoints on inlined functions by name, so we ignore these
3493 indices unless the user has done
3494 "set use-deprecated-index-sections on". */
3495 if (version < 6 && !deprecated_ok)
3496 {
3497 static int warning_printed = 0;
3498 if (!warning_printed)
3499 {
3500 warning (_("\
3501 Skipping deprecated .gdb_index section in %s.\n\
3502 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3503 to use the section anyway."),
3504 filename);
3505 warning_printed = 1;
3506 }
3507 return 0;
3508 }
3509 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3510 of the TU (for symbols coming from TUs),
3511 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3512 Plus gold-generated indices can have duplicate entries for global symbols,
3513 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3514 These are just performance bugs, and we can't distinguish gdb-generated
3515 indices from gold-generated ones, so issue no warning here. */
3516
3517 /* Indexes with higher version than the one supported by GDB may be no
3518 longer backward compatible. */
3519 if (version > 8)
3520 return 0;
3521
3522 map->version = version;
3523 map->total_size = section->size;
3524
3525 metadata = (offset_type *) (addr + sizeof (offset_type));
3526
3527 i = 0;
3528 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3529 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3530 / 8);
3531 ++i;
3532
3533 *types_list = addr + MAYBE_SWAP (metadata[i]);
3534 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3535 - MAYBE_SWAP (metadata[i]))
3536 / 8);
3537 ++i;
3538
3539 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3540 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3541 map->address_table
3542 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3543 ++i;
3544
3545 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3546 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3547 map->symbol_table
3548 = gdb::array_view<mapped_index::symbol_table_slot>
3549 ((mapped_index::symbol_table_slot *) symbol_table,
3550 (mapped_index::symbol_table_slot *) symbol_table_end);
3551
3552 ++i;
3553 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3554
3555 return 1;
3556 }
3557
3558 /* Read .gdb_index. If everything went ok, initialize the "quick"
3559 elements of all the CUs and return 1. Otherwise, return 0. */
3560
3561 static int
3562 dwarf2_read_index (struct objfile *objfile)
3563 {
3564 struct mapped_index local_map, *map;
3565 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3566 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3567 struct dwz_file *dwz;
3568 struct dwarf2_per_objfile *dwarf2_per_objfile
3569 = get_dwarf2_per_objfile (objfile);
3570
3571 if (!read_index_from_section (objfile, objfile_name (objfile),
3572 use_deprecated_index_sections,
3573 &dwarf2_per_objfile->gdb_index, &local_map,
3574 &cu_list, &cu_list_elements,
3575 &types_list, &types_list_elements))
3576 return 0;
3577
3578 /* Don't use the index if it's empty. */
3579 if (local_map.symbol_table.empty ())
3580 return 0;
3581
3582 /* If there is a .dwz file, read it so we can get its CU list as
3583 well. */
3584 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3585 if (dwz != NULL)
3586 {
3587 struct mapped_index dwz_map;
3588 const gdb_byte *dwz_types_ignore;
3589 offset_type dwz_types_elements_ignore;
3590
3591 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3592 1,
3593 &dwz->gdb_index, &dwz_map,
3594 &dwz_list, &dwz_list_elements,
3595 &dwz_types_ignore,
3596 &dwz_types_elements_ignore))
3597 {
3598 warning (_("could not read '.gdb_index' section from %s; skipping"),
3599 bfd_get_filename (dwz->dwz_bfd));
3600 return 0;
3601 }
3602 }
3603
3604 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3605 dwz_list_elements);
3606
3607 if (types_list_elements)
3608 {
3609 struct dwarf2_section_info *section;
3610
3611 /* We can only handle a single .debug_types when we have an
3612 index. */
3613 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3614 return 0;
3615
3616 section = VEC_index (dwarf2_section_info_def,
3617 dwarf2_per_objfile->types, 0);
3618
3619 create_signatured_type_table_from_index (objfile, section, types_list,
3620 types_list_elements);
3621 }
3622
3623 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
3624
3625 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3626 map = new (map) mapped_index ();
3627 *map = local_map;
3628
3629 dwarf2_per_objfile->index_table = map;
3630 dwarf2_per_objfile->using_index = 1;
3631 dwarf2_per_objfile->quick_file_names_table =
3632 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3633
3634 return 1;
3635 }
3636
3637 /* die_reader_func for dw2_get_file_names. */
3638
3639 static void
3640 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3641 const gdb_byte *info_ptr,
3642 struct die_info *comp_unit_die,
3643 int has_children,
3644 void *data)
3645 {
3646 struct dwarf2_cu *cu = reader->cu;
3647 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3648 struct dwarf2_per_objfile *dwarf2_per_objfile
3649 = cu->per_cu->dwarf2_per_objfile;
3650 struct objfile *objfile = dwarf2_per_objfile->objfile;
3651 struct dwarf2_per_cu_data *lh_cu;
3652 struct attribute *attr;
3653 int i;
3654 void **slot;
3655 struct quick_file_names *qfn;
3656
3657 gdb_assert (! this_cu->is_debug_types);
3658
3659 /* Our callers never want to match partial units -- instead they
3660 will match the enclosing full CU. */
3661 if (comp_unit_die->tag == DW_TAG_partial_unit)
3662 {
3663 this_cu->v.quick->no_file_data = 1;
3664 return;
3665 }
3666
3667 lh_cu = this_cu;
3668 slot = NULL;
3669
3670 line_header_up lh;
3671 sect_offset line_offset {};
3672
3673 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3674 if (attr)
3675 {
3676 struct quick_file_names find_entry;
3677
3678 line_offset = (sect_offset) DW_UNSND (attr);
3679
3680 /* We may have already read in this line header (TU line header sharing).
3681 If we have we're done. */
3682 find_entry.hash.dwo_unit = cu->dwo_unit;
3683 find_entry.hash.line_sect_off = line_offset;
3684 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3685 &find_entry, INSERT);
3686 if (*slot != NULL)
3687 {
3688 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3689 return;
3690 }
3691
3692 lh = dwarf_decode_line_header (line_offset, cu);
3693 }
3694 if (lh == NULL)
3695 {
3696 lh_cu->v.quick->no_file_data = 1;
3697 return;
3698 }
3699
3700 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3701 qfn->hash.dwo_unit = cu->dwo_unit;
3702 qfn->hash.line_sect_off = line_offset;
3703 gdb_assert (slot != NULL);
3704 *slot = qfn;
3705
3706 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3707
3708 qfn->num_file_names = lh->file_names.size ();
3709 qfn->file_names =
3710 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3711 for (i = 0; i < lh->file_names.size (); ++i)
3712 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3713 qfn->real_names = NULL;
3714
3715 lh_cu->v.quick->file_names = qfn;
3716 }
3717
3718 /* A helper for the "quick" functions which attempts to read the line
3719 table for THIS_CU. */
3720
3721 static struct quick_file_names *
3722 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3723 {
3724 /* This should never be called for TUs. */
3725 gdb_assert (! this_cu->is_debug_types);
3726 /* Nor type unit groups. */
3727 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3728
3729 if (this_cu->v.quick->file_names != NULL)
3730 return this_cu->v.quick->file_names;
3731 /* If we know there is no line data, no point in looking again. */
3732 if (this_cu->v.quick->no_file_data)
3733 return NULL;
3734
3735 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3736
3737 if (this_cu->v.quick->no_file_data)
3738 return NULL;
3739 return this_cu->v.quick->file_names;
3740 }
3741
3742 /* A helper for the "quick" functions which computes and caches the
3743 real path for a given file name from the line table. */
3744
3745 static const char *
3746 dw2_get_real_path (struct objfile *objfile,
3747 struct quick_file_names *qfn, int index)
3748 {
3749 if (qfn->real_names == NULL)
3750 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3751 qfn->num_file_names, const char *);
3752
3753 if (qfn->real_names[index] == NULL)
3754 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3755
3756 return qfn->real_names[index];
3757 }
3758
3759 static struct symtab *
3760 dw2_find_last_source_symtab (struct objfile *objfile)
3761 {
3762 struct dwarf2_per_objfile *dwarf2_per_objfile
3763 = get_dwarf2_per_objfile (objfile);
3764 int index = dwarf2_per_objfile->n_comp_units - 1;
3765 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->get_cu (index);
3766 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
3767
3768 if (cust == NULL)
3769 return NULL;
3770
3771 return compunit_primary_filetab (cust);
3772 }
3773
3774 /* Traversal function for dw2_forget_cached_source_info. */
3775
3776 static int
3777 dw2_free_cached_file_names (void **slot, void *info)
3778 {
3779 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3780
3781 if (file_data->real_names)
3782 {
3783 int i;
3784
3785 for (i = 0; i < file_data->num_file_names; ++i)
3786 {
3787 xfree ((void*) file_data->real_names[i]);
3788 file_data->real_names[i] = NULL;
3789 }
3790 }
3791
3792 return 1;
3793 }
3794
3795 static void
3796 dw2_forget_cached_source_info (struct objfile *objfile)
3797 {
3798 struct dwarf2_per_objfile *dwarf2_per_objfile
3799 = get_dwarf2_per_objfile (objfile);
3800
3801 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3802 dw2_free_cached_file_names, NULL);
3803 }
3804
3805 /* Helper function for dw2_map_symtabs_matching_filename that expands
3806 the symtabs and calls the iterator. */
3807
3808 static int
3809 dw2_map_expand_apply (struct objfile *objfile,
3810 struct dwarf2_per_cu_data *per_cu,
3811 const char *name, const char *real_path,
3812 gdb::function_view<bool (symtab *)> callback)
3813 {
3814 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3815
3816 /* Don't visit already-expanded CUs. */
3817 if (per_cu->v.quick->compunit_symtab)
3818 return 0;
3819
3820 /* This may expand more than one symtab, and we want to iterate over
3821 all of them. */
3822 dw2_instantiate_symtab (per_cu);
3823
3824 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3825 last_made, callback);
3826 }
3827
3828 /* Implementation of the map_symtabs_matching_filename method. */
3829
3830 static bool
3831 dw2_map_symtabs_matching_filename
3832 (struct objfile *objfile, const char *name, const char *real_path,
3833 gdb::function_view<bool (symtab *)> callback)
3834 {
3835 const char *name_basename = lbasename (name);
3836 struct dwarf2_per_objfile *dwarf2_per_objfile
3837 = get_dwarf2_per_objfile (objfile);
3838
3839 /* The rule is CUs specify all the files, including those used by
3840 any TU, so there's no need to scan TUs here. */
3841
3842 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3843 {
3844 int j;
3845 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (i);
3846 struct quick_file_names *file_data;
3847
3848 /* We only need to look at symtabs not already expanded. */
3849 if (per_cu->v.quick->compunit_symtab)
3850 continue;
3851
3852 file_data = dw2_get_file_names (per_cu);
3853 if (file_data == NULL)
3854 continue;
3855
3856 for (j = 0; j < file_data->num_file_names; ++j)
3857 {
3858 const char *this_name = file_data->file_names[j];
3859 const char *this_real_name;
3860
3861 if (compare_filenames_for_search (this_name, name))
3862 {
3863 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3864 callback))
3865 return true;
3866 continue;
3867 }
3868
3869 /* Before we invoke realpath, which can get expensive when many
3870 files are involved, do a quick comparison of the basenames. */
3871 if (! basenames_may_differ
3872 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3873 continue;
3874
3875 this_real_name = dw2_get_real_path (objfile, file_data, j);
3876 if (compare_filenames_for_search (this_real_name, name))
3877 {
3878 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3879 callback))
3880 return true;
3881 continue;
3882 }
3883
3884 if (real_path != NULL)
3885 {
3886 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3887 gdb_assert (IS_ABSOLUTE_PATH (name));
3888 if (this_real_name != NULL
3889 && FILENAME_CMP (real_path, this_real_name) == 0)
3890 {
3891 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3892 callback))
3893 return true;
3894 continue;
3895 }
3896 }
3897 }
3898 }
3899
3900 return false;
3901 }
3902
3903 /* Struct used to manage iterating over all CUs looking for a symbol. */
3904
3905 struct dw2_symtab_iterator
3906 {
3907 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3908 struct dwarf2_per_objfile *dwarf2_per_objfile;
3909 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3910 int want_specific_block;
3911 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3912 Unused if !WANT_SPECIFIC_BLOCK. */
3913 int block_index;
3914 /* The kind of symbol we're looking for. */
3915 domain_enum domain;
3916 /* The list of CUs from the index entry of the symbol,
3917 or NULL if not found. */
3918 offset_type *vec;
3919 /* The next element in VEC to look at. */
3920 int next;
3921 /* The number of elements in VEC, or zero if there is no match. */
3922 int length;
3923 /* Have we seen a global version of the symbol?
3924 If so we can ignore all further global instances.
3925 This is to work around gold/15646, inefficient gold-generated
3926 indices. */
3927 int global_seen;
3928 };
3929
3930 /* Initialize the index symtab iterator ITER.
3931 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3932 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3933
3934 static void
3935 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3936 struct dwarf2_per_objfile *dwarf2_per_objfile,
3937 int want_specific_block,
3938 int block_index,
3939 domain_enum domain,
3940 const char *name)
3941 {
3942 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3943 iter->want_specific_block = want_specific_block;
3944 iter->block_index = block_index;
3945 iter->domain = domain;
3946 iter->next = 0;
3947 iter->global_seen = 0;
3948
3949 mapped_index *index = dwarf2_per_objfile->index_table;
3950
3951 /* index is NULL if OBJF_READNOW. */
3952 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3953 iter->length = MAYBE_SWAP (*iter->vec);
3954 else
3955 {
3956 iter->vec = NULL;
3957 iter->length = 0;
3958 }
3959 }
3960
3961 /* Return the next matching CU or NULL if there are no more. */
3962
3963 static struct dwarf2_per_cu_data *
3964 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3965 {
3966 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3967
3968 for ( ; iter->next < iter->length; ++iter->next)
3969 {
3970 offset_type cu_index_and_attrs =
3971 MAYBE_SWAP (iter->vec[iter->next + 1]);
3972 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3973 int want_static = iter->block_index != GLOBAL_BLOCK;
3974 /* This value is only valid for index versions >= 7. */
3975 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3976 gdb_index_symbol_kind symbol_kind =
3977 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3978 /* Only check the symbol attributes if they're present.
3979 Indices prior to version 7 don't record them,
3980 and indices >= 7 may elide them for certain symbols
3981 (gold does this). */
3982 int attrs_valid =
3983 (dwarf2_per_objfile->index_table->version >= 7
3984 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3985
3986 /* Don't crash on bad data. */
3987 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3988 + dwarf2_per_objfile->n_type_units))
3989 {
3990 complaint (&symfile_complaints,
3991 _(".gdb_index entry has bad CU index"
3992 " [in module %s]"),
3993 objfile_name (dwarf2_per_objfile->objfile));
3994 continue;
3995 }
3996
3997 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3998
3999 /* Skip if already read in. */
4000 if (per_cu->v.quick->compunit_symtab)
4001 continue;
4002
4003 /* Check static vs global. */
4004 if (attrs_valid)
4005 {
4006 if (iter->want_specific_block
4007 && want_static != is_static)
4008 continue;
4009 /* Work around gold/15646. */
4010 if (!is_static && iter->global_seen)
4011 continue;
4012 if (!is_static)
4013 iter->global_seen = 1;
4014 }
4015
4016 /* Only check the symbol's kind if it has one. */
4017 if (attrs_valid)
4018 {
4019 switch (iter->domain)
4020 {
4021 case VAR_DOMAIN:
4022 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4023 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4024 /* Some types are also in VAR_DOMAIN. */
4025 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4026 continue;
4027 break;
4028 case STRUCT_DOMAIN:
4029 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4030 continue;
4031 break;
4032 case LABEL_DOMAIN:
4033 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4034 continue;
4035 break;
4036 default:
4037 break;
4038 }
4039 }
4040
4041 ++iter->next;
4042 return per_cu;
4043 }
4044
4045 return NULL;
4046 }
4047
4048 static struct compunit_symtab *
4049 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4050 const char *name, domain_enum domain)
4051 {
4052 struct compunit_symtab *stab_best = NULL;
4053 struct dwarf2_per_objfile *dwarf2_per_objfile
4054 = get_dwarf2_per_objfile (objfile);
4055
4056 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4057
4058 struct dw2_symtab_iterator iter;
4059 struct dwarf2_per_cu_data *per_cu;
4060
4061 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4062
4063 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4064 {
4065 struct symbol *sym, *with_opaque = NULL;
4066 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4067 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4068 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4069
4070 sym = block_find_symbol (block, name, domain,
4071 block_find_non_opaque_type_preferred,
4072 &with_opaque);
4073
4074 /* Some caution must be observed with overloaded functions
4075 and methods, since the index will not contain any overload
4076 information (but NAME might contain it). */
4077
4078 if (sym != NULL
4079 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4080 return stab;
4081 if (with_opaque != NULL
4082 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4083 stab_best = stab;
4084
4085 /* Keep looking through other CUs. */
4086 }
4087
4088 return stab_best;
4089 }
4090
4091 static void
4092 dw2_print_stats (struct objfile *objfile)
4093 {
4094 struct dwarf2_per_objfile *dwarf2_per_objfile
4095 = get_dwarf2_per_objfile (objfile);
4096 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4097 int count = 0;
4098
4099 for (int i = 0; i < total; ++i)
4100 {
4101 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4102
4103 if (!per_cu->v.quick->compunit_symtab)
4104 ++count;
4105 }
4106 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4107 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4108 }
4109
4110 /* This dumps minimal information about the index.
4111 It is called via "mt print objfiles".
4112 One use is to verify .gdb_index has been loaded by the
4113 gdb.dwarf2/gdb-index.exp testcase. */
4114
4115 static void
4116 dw2_dump (struct objfile *objfile)
4117 {
4118 struct dwarf2_per_objfile *dwarf2_per_objfile
4119 = get_dwarf2_per_objfile (objfile);
4120
4121 gdb_assert (dwarf2_per_objfile->using_index);
4122 printf_filtered (".gdb_index:");
4123 if (dwarf2_per_objfile->index_table != NULL)
4124 {
4125 printf_filtered (" version %d\n",
4126 dwarf2_per_objfile->index_table->version);
4127 }
4128 else
4129 printf_filtered (" faked for \"readnow\"\n");
4130 printf_filtered ("\n");
4131 }
4132
4133 static void
4134 dw2_relocate (struct objfile *objfile,
4135 const struct section_offsets *new_offsets,
4136 const struct section_offsets *delta)
4137 {
4138 /* There's nothing to relocate here. */
4139 }
4140
4141 static void
4142 dw2_expand_symtabs_for_function (struct objfile *objfile,
4143 const char *func_name)
4144 {
4145 struct dwarf2_per_objfile *dwarf2_per_objfile
4146 = get_dwarf2_per_objfile (objfile);
4147
4148 struct dw2_symtab_iterator iter;
4149 struct dwarf2_per_cu_data *per_cu;
4150
4151 /* Note: It doesn't matter what we pass for block_index here. */
4152 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4153 func_name);
4154
4155 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4156 dw2_instantiate_symtab (per_cu);
4157
4158 }
4159
4160 static void
4161 dw2_expand_all_symtabs (struct objfile *objfile)
4162 {
4163 struct dwarf2_per_objfile *dwarf2_per_objfile
4164 = get_dwarf2_per_objfile (objfile);
4165 int total_units = (dwarf2_per_objfile->n_comp_units
4166 + dwarf2_per_objfile->n_type_units);
4167
4168 for (int i = 0; i < total_units; ++i)
4169 {
4170 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4171
4172 dw2_instantiate_symtab (per_cu);
4173 }
4174 }
4175
4176 static void
4177 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4178 const char *fullname)
4179 {
4180 struct dwarf2_per_objfile *dwarf2_per_objfile
4181 = get_dwarf2_per_objfile (objfile);
4182
4183 /* We don't need to consider type units here.
4184 This is only called for examining code, e.g. expand_line_sal.
4185 There can be an order of magnitude (or more) more type units
4186 than comp units, and we avoid them if we can. */
4187
4188 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4189 {
4190 int j;
4191 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (i);
4192 struct quick_file_names *file_data;
4193
4194 /* We only need to look at symtabs not already expanded. */
4195 if (per_cu->v.quick->compunit_symtab)
4196 continue;
4197
4198 file_data = dw2_get_file_names (per_cu);
4199 if (file_data == NULL)
4200 continue;
4201
4202 for (j = 0; j < file_data->num_file_names; ++j)
4203 {
4204 const char *this_fullname = file_data->file_names[j];
4205
4206 if (filename_cmp (this_fullname, fullname) == 0)
4207 {
4208 dw2_instantiate_symtab (per_cu);
4209 break;
4210 }
4211 }
4212 }
4213 }
4214
4215 static void
4216 dw2_map_matching_symbols (struct objfile *objfile,
4217 const char * name, domain_enum domain,
4218 int global,
4219 int (*callback) (struct block *,
4220 struct symbol *, void *),
4221 void *data, symbol_name_match_type match,
4222 symbol_compare_ftype *ordered_compare)
4223 {
4224 /* Currently unimplemented; used for Ada. The function can be called if the
4225 current language is Ada for a non-Ada objfile using GNU index. As Ada
4226 does not look for non-Ada symbols this function should just return. */
4227 }
4228
4229 /* Symbol name matcher for .gdb_index names.
4230
4231 Symbol names in .gdb_index have a few particularities:
4232
4233 - There's no indication of which is the language of each symbol.
4234
4235 Since each language has its own symbol name matching algorithm,
4236 and we don't know which language is the right one, we must match
4237 each symbol against all languages. This would be a potential
4238 performance problem if it were not mitigated by the
4239 mapped_index::name_components lookup table, which significantly
4240 reduces the number of times we need to call into this matcher,
4241 making it a non-issue.
4242
4243 - Symbol names in the index have no overload (parameter)
4244 information. I.e., in C++, "foo(int)" and "foo(long)" both
4245 appear as "foo" in the index, for example.
4246
4247 This means that the lookup names passed to the symbol name
4248 matcher functions must have no parameter information either
4249 because (e.g.) symbol search name "foo" does not match
4250 lookup-name "foo(int)" [while swapping search name for lookup
4251 name would match].
4252 */
4253 class gdb_index_symbol_name_matcher
4254 {
4255 public:
4256 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4257 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4258
4259 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4260 Returns true if any matcher matches. */
4261 bool matches (const char *symbol_name);
4262
4263 private:
4264 /* A reference to the lookup name we're matching against. */
4265 const lookup_name_info &m_lookup_name;
4266
4267 /* A vector holding all the different symbol name matchers, for all
4268 languages. */
4269 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4270 };
4271
4272 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4273 (const lookup_name_info &lookup_name)
4274 : m_lookup_name (lookup_name)
4275 {
4276 /* Prepare the vector of comparison functions upfront, to avoid
4277 doing the same work for each symbol. Care is taken to avoid
4278 matching with the same matcher more than once if/when multiple
4279 languages use the same matcher function. */
4280 auto &matchers = m_symbol_name_matcher_funcs;
4281 matchers.reserve (nr_languages);
4282
4283 matchers.push_back (default_symbol_name_matcher);
4284
4285 for (int i = 0; i < nr_languages; i++)
4286 {
4287 const language_defn *lang = language_def ((enum language) i);
4288 symbol_name_matcher_ftype *name_matcher
4289 = get_symbol_name_matcher (lang, m_lookup_name);
4290
4291 /* Don't insert the same comparison routine more than once.
4292 Note that we do this linear walk instead of a seemingly
4293 cheaper sorted insert, or use a std::set or something like
4294 that, because relative order of function addresses is not
4295 stable. This is not a problem in practice because the number
4296 of supported languages is low, and the cost here is tiny
4297 compared to the number of searches we'll do afterwards using
4298 this object. */
4299 if (name_matcher != default_symbol_name_matcher
4300 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4301 == matchers.end ()))
4302 matchers.push_back (name_matcher);
4303 }
4304 }
4305
4306 bool
4307 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4308 {
4309 for (auto matches_name : m_symbol_name_matcher_funcs)
4310 if (matches_name (symbol_name, m_lookup_name, NULL))
4311 return true;
4312
4313 return false;
4314 }
4315
4316 /* Starting from a search name, return the string that finds the upper
4317 bound of all strings that start with SEARCH_NAME in a sorted name
4318 list. Returns the empty string to indicate that the upper bound is
4319 the end of the list. */
4320
4321 static std::string
4322 make_sort_after_prefix_name (const char *search_name)
4323 {
4324 /* When looking to complete "func", we find the upper bound of all
4325 symbols that start with "func" by looking for where we'd insert
4326 the closest string that would follow "func" in lexicographical
4327 order. Usually, that's "func"-with-last-character-incremented,
4328 i.e. "fund". Mind non-ASCII characters, though. Usually those
4329 will be UTF-8 multi-byte sequences, but we can't be certain.
4330 Especially mind the 0xff character, which is a valid character in
4331 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4332 rule out compilers allowing it in identifiers. Note that
4333 conveniently, strcmp/strcasecmp are specified to compare
4334 characters interpreted as unsigned char. So what we do is treat
4335 the whole string as a base 256 number composed of a sequence of
4336 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4337 to 0, and carries 1 to the following more-significant position.
4338 If the very first character in SEARCH_NAME ends up incremented
4339 and carries/overflows, then the upper bound is the end of the
4340 list. The string after the empty string is also the empty
4341 string.
4342
4343 Some examples of this operation:
4344
4345 SEARCH_NAME => "+1" RESULT
4346
4347 "abc" => "abd"
4348 "ab\xff" => "ac"
4349 "\xff" "a" "\xff" => "\xff" "b"
4350 "\xff" => ""
4351 "\xff\xff" => ""
4352 "" => ""
4353
4354 Then, with these symbols for example:
4355
4356 func
4357 func1
4358 fund
4359
4360 completing "func" looks for symbols between "func" and
4361 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4362 which finds "func" and "func1", but not "fund".
4363
4364 And with:
4365
4366 funcÿ (Latin1 'ÿ' [0xff])
4367 funcÿ1
4368 fund
4369
4370 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4371 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4372
4373 And with:
4374
4375 ÿÿ (Latin1 'ÿ' [0xff])
4376 ÿÿ1
4377
4378 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4379 the end of the list.
4380 */
4381 std::string after = search_name;
4382 while (!after.empty () && (unsigned char) after.back () == 0xff)
4383 after.pop_back ();
4384 if (!after.empty ())
4385 after.back () = (unsigned char) after.back () + 1;
4386 return after;
4387 }
4388
4389 /* See declaration. */
4390
4391 std::pair<std::vector<name_component>::const_iterator,
4392 std::vector<name_component>::const_iterator>
4393 mapped_index_base::find_name_components_bounds
4394 (const lookup_name_info &lookup_name_without_params) const
4395 {
4396 auto *name_cmp
4397 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4398
4399 const char *cplus
4400 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4401
4402 /* Comparison function object for lower_bound that matches against a
4403 given symbol name. */
4404 auto lookup_compare_lower = [&] (const name_component &elem,
4405 const char *name)
4406 {
4407 const char *elem_qualified = this->symbol_name_at (elem.idx);
4408 const char *elem_name = elem_qualified + elem.name_offset;
4409 return name_cmp (elem_name, name) < 0;
4410 };
4411
4412 /* Comparison function object for upper_bound that matches against a
4413 given symbol name. */
4414 auto lookup_compare_upper = [&] (const char *name,
4415 const name_component &elem)
4416 {
4417 const char *elem_qualified = this->symbol_name_at (elem.idx);
4418 const char *elem_name = elem_qualified + elem.name_offset;
4419 return name_cmp (name, elem_name) < 0;
4420 };
4421
4422 auto begin = this->name_components.begin ();
4423 auto end = this->name_components.end ();
4424
4425 /* Find the lower bound. */
4426 auto lower = [&] ()
4427 {
4428 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4429 return begin;
4430 else
4431 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4432 } ();
4433
4434 /* Find the upper bound. */
4435 auto upper = [&] ()
4436 {
4437 if (lookup_name_without_params.completion_mode ())
4438 {
4439 /* In completion mode, we want UPPER to point past all
4440 symbols names that have the same prefix. I.e., with
4441 these symbols, and completing "func":
4442
4443 function << lower bound
4444 function1
4445 other_function << upper bound
4446
4447 We find the upper bound by looking for the insertion
4448 point of "func"-with-last-character-incremented,
4449 i.e. "fund". */
4450 std::string after = make_sort_after_prefix_name (cplus);
4451 if (after.empty ())
4452 return end;
4453 return std::lower_bound (lower, end, after.c_str (),
4454 lookup_compare_lower);
4455 }
4456 else
4457 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4458 } ();
4459
4460 return {lower, upper};
4461 }
4462
4463 /* See declaration. */
4464
4465 void
4466 mapped_index_base::build_name_components ()
4467 {
4468 if (!this->name_components.empty ())
4469 return;
4470
4471 this->name_components_casing = case_sensitivity;
4472 auto *name_cmp
4473 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4474
4475 /* The code below only knows how to break apart components of C++
4476 symbol names (and other languages that use '::' as
4477 namespace/module separator). If we add support for wild matching
4478 to some language that uses some other operator (E.g., Ada, Go and
4479 D use '.'), then we'll need to try splitting the symbol name
4480 according to that language too. Note that Ada does support wild
4481 matching, but doesn't currently support .gdb_index. */
4482 auto count = this->symbol_name_count ();
4483 for (offset_type idx = 0; idx < count; idx++)
4484 {
4485 if (this->symbol_name_slot_invalid (idx))
4486 continue;
4487
4488 const char *name = this->symbol_name_at (idx);
4489
4490 /* Add each name component to the name component table. */
4491 unsigned int previous_len = 0;
4492 for (unsigned int current_len = cp_find_first_component (name);
4493 name[current_len] != '\0';
4494 current_len += cp_find_first_component (name + current_len))
4495 {
4496 gdb_assert (name[current_len] == ':');
4497 this->name_components.push_back ({previous_len, idx});
4498 /* Skip the '::'. */
4499 current_len += 2;
4500 previous_len = current_len;
4501 }
4502 this->name_components.push_back ({previous_len, idx});
4503 }
4504
4505 /* Sort name_components elements by name. */
4506 auto name_comp_compare = [&] (const name_component &left,
4507 const name_component &right)
4508 {
4509 const char *left_qualified = this->symbol_name_at (left.idx);
4510 const char *right_qualified = this->symbol_name_at (right.idx);
4511
4512 const char *left_name = left_qualified + left.name_offset;
4513 const char *right_name = right_qualified + right.name_offset;
4514
4515 return name_cmp (left_name, right_name) < 0;
4516 };
4517
4518 std::sort (this->name_components.begin (),
4519 this->name_components.end (),
4520 name_comp_compare);
4521 }
4522
4523 /* Helper for dw2_expand_symtabs_matching that works with a
4524 mapped_index_base instead of the containing objfile. This is split
4525 to a separate function in order to be able to unit test the
4526 name_components matching using a mock mapped_index_base. For each
4527 symbol name that matches, calls MATCH_CALLBACK, passing it the
4528 symbol's index in the mapped_index_base symbol table. */
4529
4530 static void
4531 dw2_expand_symtabs_matching_symbol
4532 (mapped_index_base &index,
4533 const lookup_name_info &lookup_name_in,
4534 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4535 enum search_domain kind,
4536 gdb::function_view<void (offset_type)> match_callback)
4537 {
4538 lookup_name_info lookup_name_without_params
4539 = lookup_name_in.make_ignore_params ();
4540 gdb_index_symbol_name_matcher lookup_name_matcher
4541 (lookup_name_without_params);
4542
4543 /* Build the symbol name component sorted vector, if we haven't
4544 yet. */
4545 index.build_name_components ();
4546
4547 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4548
4549 /* Now for each symbol name in range, check to see if we have a name
4550 match, and if so, call the MATCH_CALLBACK callback. */
4551
4552 /* The same symbol may appear more than once in the range though.
4553 E.g., if we're looking for symbols that complete "w", and we have
4554 a symbol named "w1::w2", we'll find the two name components for
4555 that same symbol in the range. To be sure we only call the
4556 callback once per symbol, we first collect the symbol name
4557 indexes that matched in a temporary vector and ignore
4558 duplicates. */
4559 std::vector<offset_type> matches;
4560 matches.reserve (std::distance (bounds.first, bounds.second));
4561
4562 for (; bounds.first != bounds.second; ++bounds.first)
4563 {
4564 const char *qualified = index.symbol_name_at (bounds.first->idx);
4565
4566 if (!lookup_name_matcher.matches (qualified)
4567 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4568 continue;
4569
4570 matches.push_back (bounds.first->idx);
4571 }
4572
4573 std::sort (matches.begin (), matches.end ());
4574
4575 /* Finally call the callback, once per match. */
4576 ULONGEST prev = -1;
4577 for (offset_type idx : matches)
4578 {
4579 if (prev != idx)
4580 {
4581 match_callback (idx);
4582 prev = idx;
4583 }
4584 }
4585
4586 /* Above we use a type wider than idx's for 'prev', since 0 and
4587 (offset_type)-1 are both possible values. */
4588 static_assert (sizeof (prev) > sizeof (offset_type), "");
4589 }
4590
4591 #if GDB_SELF_TEST
4592
4593 namespace selftests { namespace dw2_expand_symtabs_matching {
4594
4595 /* A mock .gdb_index/.debug_names-like name index table, enough to
4596 exercise dw2_expand_symtabs_matching_symbol, which works with the
4597 mapped_index_base interface. Builds an index from the symbol list
4598 passed as parameter to the constructor. */
4599 class mock_mapped_index : public mapped_index_base
4600 {
4601 public:
4602 mock_mapped_index (gdb::array_view<const char *> symbols)
4603 : m_symbol_table (symbols)
4604 {}
4605
4606 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4607
4608 /* Return the number of names in the symbol table. */
4609 virtual size_t symbol_name_count () const
4610 {
4611 return m_symbol_table.size ();
4612 }
4613
4614 /* Get the name of the symbol at IDX in the symbol table. */
4615 virtual const char *symbol_name_at (offset_type idx) const
4616 {
4617 return m_symbol_table[idx];
4618 }
4619
4620 private:
4621 gdb::array_view<const char *> m_symbol_table;
4622 };
4623
4624 /* Convenience function that converts a NULL pointer to a "<null>"
4625 string, to pass to print routines. */
4626
4627 static const char *
4628 string_or_null (const char *str)
4629 {
4630 return str != NULL ? str : "<null>";
4631 }
4632
4633 /* Check if a lookup_name_info built from
4634 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4635 index. EXPECTED_LIST is the list of expected matches, in expected
4636 matching order. If no match expected, then an empty list is
4637 specified. Returns true on success. On failure prints a warning
4638 indicating the file:line that failed, and returns false. */
4639
4640 static bool
4641 check_match (const char *file, int line,
4642 mock_mapped_index &mock_index,
4643 const char *name, symbol_name_match_type match_type,
4644 bool completion_mode,
4645 std::initializer_list<const char *> expected_list)
4646 {
4647 lookup_name_info lookup_name (name, match_type, completion_mode);
4648
4649 bool matched = true;
4650
4651 auto mismatch = [&] (const char *expected_str,
4652 const char *got)
4653 {
4654 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4655 "expected=\"%s\", got=\"%s\"\n"),
4656 file, line,
4657 (match_type == symbol_name_match_type::FULL
4658 ? "FULL" : "WILD"),
4659 name, string_or_null (expected_str), string_or_null (got));
4660 matched = false;
4661 };
4662
4663 auto expected_it = expected_list.begin ();
4664 auto expected_end = expected_list.end ();
4665
4666 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4667 NULL, ALL_DOMAIN,
4668 [&] (offset_type idx)
4669 {
4670 const char *matched_name = mock_index.symbol_name_at (idx);
4671 const char *expected_str
4672 = expected_it == expected_end ? NULL : *expected_it++;
4673
4674 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4675 mismatch (expected_str, matched_name);
4676 });
4677
4678 const char *expected_str
4679 = expected_it == expected_end ? NULL : *expected_it++;
4680 if (expected_str != NULL)
4681 mismatch (expected_str, NULL);
4682
4683 return matched;
4684 }
4685
4686 /* The symbols added to the mock mapped_index for testing (in
4687 canonical form). */
4688 static const char *test_symbols[] = {
4689 "function",
4690 "std::bar",
4691 "std::zfunction",
4692 "std::zfunction2",
4693 "w1::w2",
4694 "ns::foo<char*>",
4695 "ns::foo<int>",
4696 "ns::foo<long>",
4697 "ns2::tmpl<int>::foo2",
4698 "(anonymous namespace)::A::B::C",
4699
4700 /* These are used to check that the increment-last-char in the
4701 matching algorithm for completion doesn't match "t1_fund" when
4702 completing "t1_func". */
4703 "t1_func",
4704 "t1_func1",
4705 "t1_fund",
4706 "t1_fund1",
4707
4708 /* A UTF-8 name with multi-byte sequences to make sure that
4709 cp-name-parser understands this as a single identifier ("função"
4710 is "function" in PT). */
4711 u8"u8função",
4712
4713 /* \377 (0xff) is Latin1 'ÿ'. */
4714 "yfunc\377",
4715
4716 /* \377 (0xff) is Latin1 'ÿ'. */
4717 "\377",
4718 "\377\377123",
4719
4720 /* A name with all sorts of complications. Starts with "z" to make
4721 it easier for the completion tests below. */
4722 #define Z_SYM_NAME \
4723 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4724 "::tuple<(anonymous namespace)::ui*, " \
4725 "std::default_delete<(anonymous namespace)::ui>, void>"
4726
4727 Z_SYM_NAME
4728 };
4729
4730 /* Returns true if the mapped_index_base::find_name_component_bounds
4731 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4732 in completion mode. */
4733
4734 static bool
4735 check_find_bounds_finds (mapped_index_base &index,
4736 const char *search_name,
4737 gdb::array_view<const char *> expected_syms)
4738 {
4739 lookup_name_info lookup_name (search_name,
4740 symbol_name_match_type::FULL, true);
4741
4742 auto bounds = index.find_name_components_bounds (lookup_name);
4743
4744 size_t distance = std::distance (bounds.first, bounds.second);
4745 if (distance != expected_syms.size ())
4746 return false;
4747
4748 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4749 {
4750 auto nc_elem = bounds.first + exp_elem;
4751 const char *qualified = index.symbol_name_at (nc_elem->idx);
4752 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4753 return false;
4754 }
4755
4756 return true;
4757 }
4758
4759 /* Test the lower-level mapped_index::find_name_component_bounds
4760 method. */
4761
4762 static void
4763 test_mapped_index_find_name_component_bounds ()
4764 {
4765 mock_mapped_index mock_index (test_symbols);
4766
4767 mock_index.build_name_components ();
4768
4769 /* Test the lower-level mapped_index::find_name_component_bounds
4770 method in completion mode. */
4771 {
4772 static const char *expected_syms[] = {
4773 "t1_func",
4774 "t1_func1",
4775 };
4776
4777 SELF_CHECK (check_find_bounds_finds (mock_index,
4778 "t1_func", expected_syms));
4779 }
4780
4781 /* Check that the increment-last-char in the name matching algorithm
4782 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4783 {
4784 static const char *expected_syms1[] = {
4785 "\377",
4786 "\377\377123",
4787 };
4788 SELF_CHECK (check_find_bounds_finds (mock_index,
4789 "\377", expected_syms1));
4790
4791 static const char *expected_syms2[] = {
4792 "\377\377123",
4793 };
4794 SELF_CHECK (check_find_bounds_finds (mock_index,
4795 "\377\377", expected_syms2));
4796 }
4797 }
4798
4799 /* Test dw2_expand_symtabs_matching_symbol. */
4800
4801 static void
4802 test_dw2_expand_symtabs_matching_symbol ()
4803 {
4804 mock_mapped_index mock_index (test_symbols);
4805
4806 /* We let all tests run until the end even if some fails, for debug
4807 convenience. */
4808 bool any_mismatch = false;
4809
4810 /* Create the expected symbols list (an initializer_list). Needed
4811 because lists have commas, and we need to pass them to CHECK,
4812 which is a macro. */
4813 #define EXPECT(...) { __VA_ARGS__ }
4814
4815 /* Wrapper for check_match that passes down the current
4816 __FILE__/__LINE__. */
4817 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4818 any_mismatch |= !check_match (__FILE__, __LINE__, \
4819 mock_index, \
4820 NAME, MATCH_TYPE, COMPLETION_MODE, \
4821 EXPECTED_LIST)
4822
4823 /* Identity checks. */
4824 for (const char *sym : test_symbols)
4825 {
4826 /* Should be able to match all existing symbols. */
4827 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4828 EXPECT (sym));
4829
4830 /* Should be able to match all existing symbols with
4831 parameters. */
4832 std::string with_params = std::string (sym) + "(int)";
4833 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4834 EXPECT (sym));
4835
4836 /* Should be able to match all existing symbols with
4837 parameters and qualifiers. */
4838 with_params = std::string (sym) + " ( int ) const";
4839 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4840 EXPECT (sym));
4841
4842 /* This should really find sym, but cp-name-parser.y doesn't
4843 know about lvalue/rvalue qualifiers yet. */
4844 with_params = std::string (sym) + " ( int ) &&";
4845 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4846 {});
4847 }
4848
4849 /* Check that the name matching algorithm for completion doesn't get
4850 confused with Latin1 'ÿ' / 0xff. */
4851 {
4852 static const char str[] = "\377";
4853 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4854 EXPECT ("\377", "\377\377123"));
4855 }
4856
4857 /* Check that the increment-last-char in the matching algorithm for
4858 completion doesn't match "t1_fund" when completing "t1_func". */
4859 {
4860 static const char str[] = "t1_func";
4861 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4862 EXPECT ("t1_func", "t1_func1"));
4863 }
4864
4865 /* Check that completion mode works at each prefix of the expected
4866 symbol name. */
4867 {
4868 static const char str[] = "function(int)";
4869 size_t len = strlen (str);
4870 std::string lookup;
4871
4872 for (size_t i = 1; i < len; i++)
4873 {
4874 lookup.assign (str, i);
4875 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4876 EXPECT ("function"));
4877 }
4878 }
4879
4880 /* While "w" is a prefix of both components, the match function
4881 should still only be called once. */
4882 {
4883 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4884 EXPECT ("w1::w2"));
4885 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4886 EXPECT ("w1::w2"));
4887 }
4888
4889 /* Same, with a "complicated" symbol. */
4890 {
4891 static const char str[] = Z_SYM_NAME;
4892 size_t len = strlen (str);
4893 std::string lookup;
4894
4895 for (size_t i = 1; i < len; i++)
4896 {
4897 lookup.assign (str, i);
4898 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4899 EXPECT (Z_SYM_NAME));
4900 }
4901 }
4902
4903 /* In FULL mode, an incomplete symbol doesn't match. */
4904 {
4905 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4906 {});
4907 }
4908
4909 /* A complete symbol with parameters matches any overload, since the
4910 index has no overload info. */
4911 {
4912 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4913 EXPECT ("std::zfunction", "std::zfunction2"));
4914 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4915 EXPECT ("std::zfunction", "std::zfunction2"));
4916 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4917 EXPECT ("std::zfunction", "std::zfunction2"));
4918 }
4919
4920 /* Check that whitespace is ignored appropriately. A symbol with a
4921 template argument list. */
4922 {
4923 static const char expected[] = "ns::foo<int>";
4924 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4925 EXPECT (expected));
4926 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4927 EXPECT (expected));
4928 }
4929
4930 /* Check that whitespace is ignored appropriately. A symbol with a
4931 template argument list that includes a pointer. */
4932 {
4933 static const char expected[] = "ns::foo<char*>";
4934 /* Try both completion and non-completion modes. */
4935 static const bool completion_mode[2] = {false, true};
4936 for (size_t i = 0; i < 2; i++)
4937 {
4938 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4939 completion_mode[i], EXPECT (expected));
4940 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4941 completion_mode[i], EXPECT (expected));
4942
4943 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4944 completion_mode[i], EXPECT (expected));
4945 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4946 completion_mode[i], EXPECT (expected));
4947 }
4948 }
4949
4950 {
4951 /* Check method qualifiers are ignored. */
4952 static const char expected[] = "ns::foo<char*>";
4953 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4954 symbol_name_match_type::FULL, true, EXPECT (expected));
4955 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4956 symbol_name_match_type::FULL, true, EXPECT (expected));
4957 CHECK_MATCH ("foo < char * > ( int ) const",
4958 symbol_name_match_type::WILD, true, EXPECT (expected));
4959 CHECK_MATCH ("foo < char * > ( int ) &&",
4960 symbol_name_match_type::WILD, true, EXPECT (expected));
4961 }
4962
4963 /* Test lookup names that don't match anything. */
4964 {
4965 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4966 {});
4967
4968 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4969 {});
4970 }
4971
4972 /* Some wild matching tests, exercising "(anonymous namespace)",
4973 which should not be confused with a parameter list. */
4974 {
4975 static const char *syms[] = {
4976 "A::B::C",
4977 "B::C",
4978 "C",
4979 "A :: B :: C ( int )",
4980 "B :: C ( int )",
4981 "C ( int )",
4982 };
4983
4984 for (const char *s : syms)
4985 {
4986 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4987 EXPECT ("(anonymous namespace)::A::B::C"));
4988 }
4989 }
4990
4991 {
4992 static const char expected[] = "ns2::tmpl<int>::foo2";
4993 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4994 EXPECT (expected));
4995 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4996 EXPECT (expected));
4997 }
4998
4999 SELF_CHECK (!any_mismatch);
5000
5001 #undef EXPECT
5002 #undef CHECK_MATCH
5003 }
5004
5005 static void
5006 run_test ()
5007 {
5008 test_mapped_index_find_name_component_bounds ();
5009 test_dw2_expand_symtabs_matching_symbol ();
5010 }
5011
5012 }} // namespace selftests::dw2_expand_symtabs_matching
5013
5014 #endif /* GDB_SELF_TEST */
5015
5016 /* If FILE_MATCHER is NULL or if PER_CU has
5017 dwarf2_per_cu_quick_data::MARK set (see
5018 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5019 EXPANSION_NOTIFY on it. */
5020
5021 static void
5022 dw2_expand_symtabs_matching_one
5023 (struct dwarf2_per_cu_data *per_cu,
5024 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5025 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5026 {
5027 if (file_matcher == NULL || per_cu->v.quick->mark)
5028 {
5029 bool symtab_was_null
5030 = (per_cu->v.quick->compunit_symtab == NULL);
5031
5032 dw2_instantiate_symtab (per_cu);
5033
5034 if (expansion_notify != NULL
5035 && symtab_was_null
5036 && per_cu->v.quick->compunit_symtab != NULL)
5037 expansion_notify (per_cu->v.quick->compunit_symtab);
5038 }
5039 }
5040
5041 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5042 matched, to expand corresponding CUs that were marked. IDX is the
5043 index of the symbol name that matched. */
5044
5045 static void
5046 dw2_expand_marked_cus
5047 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5048 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5049 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5050 search_domain kind)
5051 {
5052 offset_type *vec, vec_len, vec_idx;
5053 bool global_seen = false;
5054 mapped_index &index = *dwarf2_per_objfile->index_table;
5055
5056 vec = (offset_type *) (index.constant_pool
5057 + MAYBE_SWAP (index.symbol_table[idx].vec));
5058 vec_len = MAYBE_SWAP (vec[0]);
5059 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5060 {
5061 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5062 /* This value is only valid for index versions >= 7. */
5063 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5064 gdb_index_symbol_kind symbol_kind =
5065 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5066 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5067 /* Only check the symbol attributes if they're present.
5068 Indices prior to version 7 don't record them,
5069 and indices >= 7 may elide them for certain symbols
5070 (gold does this). */
5071 int attrs_valid =
5072 (index.version >= 7
5073 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5074
5075 /* Work around gold/15646. */
5076 if (attrs_valid)
5077 {
5078 if (!is_static && global_seen)
5079 continue;
5080 if (!is_static)
5081 global_seen = true;
5082 }
5083
5084 /* Only check the symbol's kind if it has one. */
5085 if (attrs_valid)
5086 {
5087 switch (kind)
5088 {
5089 case VARIABLES_DOMAIN:
5090 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5091 continue;
5092 break;
5093 case FUNCTIONS_DOMAIN:
5094 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5095 continue;
5096 break;
5097 case TYPES_DOMAIN:
5098 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5099 continue;
5100 break;
5101 default:
5102 break;
5103 }
5104 }
5105
5106 /* Don't crash on bad data. */
5107 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5108 + dwarf2_per_objfile->n_type_units))
5109 {
5110 complaint (&symfile_complaints,
5111 _(".gdb_index entry has bad CU index"
5112 " [in module %s]"),
5113 objfile_name (dwarf2_per_objfile->objfile));
5114 continue;
5115 }
5116
5117 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5118 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5119 expansion_notify);
5120 }
5121 }
5122
5123 /* If FILE_MATCHER is non-NULL, set all the
5124 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5125 that match FILE_MATCHER. */
5126
5127 static void
5128 dw_expand_symtabs_matching_file_matcher
5129 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5130 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5131 {
5132 if (file_matcher == NULL)
5133 return;
5134
5135 objfile *const objfile = dwarf2_per_objfile->objfile;
5136
5137 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5138 htab_eq_pointer,
5139 NULL, xcalloc, xfree));
5140 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5141 htab_eq_pointer,
5142 NULL, xcalloc, xfree));
5143
5144 /* The rule is CUs specify all the files, including those used by
5145 any TU, so there's no need to scan TUs here. */
5146
5147 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5148 {
5149 int j;
5150 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (i);
5151 struct quick_file_names *file_data;
5152 void **slot;
5153
5154 QUIT;
5155
5156 per_cu->v.quick->mark = 0;
5157
5158 /* We only need to look at symtabs not already expanded. */
5159 if (per_cu->v.quick->compunit_symtab)
5160 continue;
5161
5162 file_data = dw2_get_file_names (per_cu);
5163 if (file_data == NULL)
5164 continue;
5165
5166 if (htab_find (visited_not_found.get (), file_data) != NULL)
5167 continue;
5168 else if (htab_find (visited_found.get (), file_data) != NULL)
5169 {
5170 per_cu->v.quick->mark = 1;
5171 continue;
5172 }
5173
5174 for (j = 0; j < file_data->num_file_names; ++j)
5175 {
5176 const char *this_real_name;
5177
5178 if (file_matcher (file_data->file_names[j], false))
5179 {
5180 per_cu->v.quick->mark = 1;
5181 break;
5182 }
5183
5184 /* Before we invoke realpath, which can get expensive when many
5185 files are involved, do a quick comparison of the basenames. */
5186 if (!basenames_may_differ
5187 && !file_matcher (lbasename (file_data->file_names[j]),
5188 true))
5189 continue;
5190
5191 this_real_name = dw2_get_real_path (objfile, file_data, j);
5192 if (file_matcher (this_real_name, false))
5193 {
5194 per_cu->v.quick->mark = 1;
5195 break;
5196 }
5197 }
5198
5199 slot = htab_find_slot (per_cu->v.quick->mark
5200 ? visited_found.get ()
5201 : visited_not_found.get (),
5202 file_data, INSERT);
5203 *slot = file_data;
5204 }
5205 }
5206
5207 static void
5208 dw2_expand_symtabs_matching
5209 (struct objfile *objfile,
5210 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5211 const lookup_name_info &lookup_name,
5212 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5213 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5214 enum search_domain kind)
5215 {
5216 struct dwarf2_per_objfile *dwarf2_per_objfile
5217 = get_dwarf2_per_objfile (objfile);
5218
5219 /* index_table is NULL if OBJF_READNOW. */
5220 if (!dwarf2_per_objfile->index_table)
5221 return;
5222
5223 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5224
5225 mapped_index &index = *dwarf2_per_objfile->index_table;
5226
5227 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5228 symbol_matcher,
5229 kind, [&] (offset_type idx)
5230 {
5231 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5232 expansion_notify, kind);
5233 });
5234 }
5235
5236 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5237 symtab. */
5238
5239 static struct compunit_symtab *
5240 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5241 CORE_ADDR pc)
5242 {
5243 int i;
5244
5245 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5246 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5247 return cust;
5248
5249 if (cust->includes == NULL)
5250 return NULL;
5251
5252 for (i = 0; cust->includes[i]; ++i)
5253 {
5254 struct compunit_symtab *s = cust->includes[i];
5255
5256 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5257 if (s != NULL)
5258 return s;
5259 }
5260
5261 return NULL;
5262 }
5263
5264 static struct compunit_symtab *
5265 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5266 struct bound_minimal_symbol msymbol,
5267 CORE_ADDR pc,
5268 struct obj_section *section,
5269 int warn_if_readin)
5270 {
5271 struct dwarf2_per_cu_data *data;
5272 struct compunit_symtab *result;
5273
5274 if (!objfile->psymtabs_addrmap)
5275 return NULL;
5276
5277 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5278 pc);
5279 if (!data)
5280 return NULL;
5281
5282 if (warn_if_readin && data->v.quick->compunit_symtab)
5283 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5284 paddress (get_objfile_arch (objfile), pc));
5285
5286 result
5287 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5288 pc);
5289 gdb_assert (result != NULL);
5290 return result;
5291 }
5292
5293 static void
5294 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5295 void *data, int need_fullname)
5296 {
5297 struct dwarf2_per_objfile *dwarf2_per_objfile
5298 = get_dwarf2_per_objfile (objfile);
5299
5300 if (!dwarf2_per_objfile->filenames_cache)
5301 {
5302 dwarf2_per_objfile->filenames_cache.emplace ();
5303
5304 htab_up visited (htab_create_alloc (10,
5305 htab_hash_pointer, htab_eq_pointer,
5306 NULL, xcalloc, xfree));
5307
5308 /* The rule is CUs specify all the files, including those used
5309 by any TU, so there's no need to scan TUs here. We can
5310 ignore file names coming from already-expanded CUs. */
5311
5312 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5313 {
5314 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (i);
5315
5316 if (per_cu->v.quick->compunit_symtab)
5317 {
5318 void **slot = htab_find_slot (visited.get (),
5319 per_cu->v.quick->file_names,
5320 INSERT);
5321
5322 *slot = per_cu->v.quick->file_names;
5323 }
5324 }
5325
5326 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5327 {
5328 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (i);
5329 struct quick_file_names *file_data;
5330 void **slot;
5331
5332 /* We only need to look at symtabs not already expanded. */
5333 if (per_cu->v.quick->compunit_symtab)
5334 continue;
5335
5336 file_data = dw2_get_file_names (per_cu);
5337 if (file_data == NULL)
5338 continue;
5339
5340 slot = htab_find_slot (visited.get (), file_data, INSERT);
5341 if (*slot)
5342 {
5343 /* Already visited. */
5344 continue;
5345 }
5346 *slot = file_data;
5347
5348 for (int j = 0; j < file_data->num_file_names; ++j)
5349 {
5350 const char *filename = file_data->file_names[j];
5351 dwarf2_per_objfile->filenames_cache->seen (filename);
5352 }
5353 }
5354 }
5355
5356 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5357 {
5358 gdb::unique_xmalloc_ptr<char> this_real_name;
5359
5360 if (need_fullname)
5361 this_real_name = gdb_realpath (filename);
5362 (*fun) (filename, this_real_name.get (), data);
5363 });
5364 }
5365
5366 static int
5367 dw2_has_symbols (struct objfile *objfile)
5368 {
5369 return 1;
5370 }
5371
5372 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5373 {
5374 dw2_has_symbols,
5375 dw2_find_last_source_symtab,
5376 dw2_forget_cached_source_info,
5377 dw2_map_symtabs_matching_filename,
5378 dw2_lookup_symbol,
5379 dw2_print_stats,
5380 dw2_dump,
5381 dw2_relocate,
5382 dw2_expand_symtabs_for_function,
5383 dw2_expand_all_symtabs,
5384 dw2_expand_symtabs_with_fullname,
5385 dw2_map_matching_symbols,
5386 dw2_expand_symtabs_matching,
5387 dw2_find_pc_sect_compunit_symtab,
5388 NULL,
5389 dw2_map_symbol_filenames
5390 };
5391
5392 /* DWARF-5 debug_names reader. */
5393
5394 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5395 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5396
5397 /* A helper function that reads the .debug_names section in SECTION
5398 and fills in MAP. FILENAME is the name of the file containing the
5399 section; it is used for error reporting.
5400
5401 Returns true if all went well, false otherwise. */
5402
5403 static bool
5404 read_debug_names_from_section (struct objfile *objfile,
5405 const char *filename,
5406 struct dwarf2_section_info *section,
5407 mapped_debug_names &map)
5408 {
5409 if (dwarf2_section_empty_p (section))
5410 return false;
5411
5412 /* Older elfutils strip versions could keep the section in the main
5413 executable while splitting it for the separate debug info file. */
5414 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5415 return false;
5416
5417 dwarf2_read_section (objfile, section);
5418
5419 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5420
5421 const gdb_byte *addr = section->buffer;
5422
5423 bfd *const abfd = get_section_bfd_owner (section);
5424
5425 unsigned int bytes_read;
5426 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5427 addr += bytes_read;
5428
5429 map.dwarf5_is_dwarf64 = bytes_read != 4;
5430 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5431 if (bytes_read + length != section->size)
5432 {
5433 /* There may be multiple per-CU indices. */
5434 warning (_("Section .debug_names in %s length %s does not match "
5435 "section length %s, ignoring .debug_names."),
5436 filename, plongest (bytes_read + length),
5437 pulongest (section->size));
5438 return false;
5439 }
5440
5441 /* The version number. */
5442 uint16_t version = read_2_bytes (abfd, addr);
5443 addr += 2;
5444 if (version != 5)
5445 {
5446 warning (_("Section .debug_names in %s has unsupported version %d, "
5447 "ignoring .debug_names."),
5448 filename, version);
5449 return false;
5450 }
5451
5452 /* Padding. */
5453 uint16_t padding = read_2_bytes (abfd, addr);
5454 addr += 2;
5455 if (padding != 0)
5456 {
5457 warning (_("Section .debug_names in %s has unsupported padding %d, "
5458 "ignoring .debug_names."),
5459 filename, padding);
5460 return false;
5461 }
5462
5463 /* comp_unit_count - The number of CUs in the CU list. */
5464 map.cu_count = read_4_bytes (abfd, addr);
5465 addr += 4;
5466
5467 /* local_type_unit_count - The number of TUs in the local TU
5468 list. */
5469 map.tu_count = read_4_bytes (abfd, addr);
5470 addr += 4;
5471
5472 /* foreign_type_unit_count - The number of TUs in the foreign TU
5473 list. */
5474 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5475 addr += 4;
5476 if (foreign_tu_count != 0)
5477 {
5478 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5479 "ignoring .debug_names."),
5480 filename, static_cast<unsigned long> (foreign_tu_count));
5481 return false;
5482 }
5483
5484 /* bucket_count - The number of hash buckets in the hash lookup
5485 table. */
5486 map.bucket_count = read_4_bytes (abfd, addr);
5487 addr += 4;
5488
5489 /* name_count - The number of unique names in the index. */
5490 map.name_count = read_4_bytes (abfd, addr);
5491 addr += 4;
5492
5493 /* abbrev_table_size - The size in bytes of the abbreviations
5494 table. */
5495 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5496 addr += 4;
5497
5498 /* augmentation_string_size - The size in bytes of the augmentation
5499 string. This value is rounded up to a multiple of 4. */
5500 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5501 addr += 4;
5502 map.augmentation_is_gdb = ((augmentation_string_size
5503 == sizeof (dwarf5_augmentation))
5504 && memcmp (addr, dwarf5_augmentation,
5505 sizeof (dwarf5_augmentation)) == 0);
5506 augmentation_string_size += (-augmentation_string_size) & 3;
5507 addr += augmentation_string_size;
5508
5509 /* List of CUs */
5510 map.cu_table_reordered = addr;
5511 addr += map.cu_count * map.offset_size;
5512
5513 /* List of Local TUs */
5514 map.tu_table_reordered = addr;
5515 addr += map.tu_count * map.offset_size;
5516
5517 /* Hash Lookup Table */
5518 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5519 addr += map.bucket_count * 4;
5520 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5521 addr += map.name_count * 4;
5522
5523 /* Name Table */
5524 map.name_table_string_offs_reordered = addr;
5525 addr += map.name_count * map.offset_size;
5526 map.name_table_entry_offs_reordered = addr;
5527 addr += map.name_count * map.offset_size;
5528
5529 const gdb_byte *abbrev_table_start = addr;
5530 for (;;)
5531 {
5532 unsigned int bytes_read;
5533 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5534 addr += bytes_read;
5535 if (index_num == 0)
5536 break;
5537
5538 const auto insertpair
5539 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5540 if (!insertpair.second)
5541 {
5542 warning (_("Section .debug_names in %s has duplicate index %s, "
5543 "ignoring .debug_names."),
5544 filename, pulongest (index_num));
5545 return false;
5546 }
5547 mapped_debug_names::index_val &indexval = insertpair.first->second;
5548 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5549 addr += bytes_read;
5550
5551 for (;;)
5552 {
5553 mapped_debug_names::index_val::attr attr;
5554 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5555 addr += bytes_read;
5556 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5557 addr += bytes_read;
5558 if (attr.form == DW_FORM_implicit_const)
5559 {
5560 attr.implicit_const = read_signed_leb128 (abfd, addr,
5561 &bytes_read);
5562 addr += bytes_read;
5563 }
5564 if (attr.dw_idx == 0 && attr.form == 0)
5565 break;
5566 indexval.attr_vec.push_back (std::move (attr));
5567 }
5568 }
5569 if (addr != abbrev_table_start + abbrev_table_size)
5570 {
5571 warning (_("Section .debug_names in %s has abbreviation_table "
5572 "of size %zu vs. written as %u, ignoring .debug_names."),
5573 filename, addr - abbrev_table_start, abbrev_table_size);
5574 return false;
5575 }
5576 map.entry_pool = addr;
5577
5578 return true;
5579 }
5580
5581 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5582 list. */
5583
5584 static void
5585 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5586 const mapped_debug_names &map,
5587 dwarf2_section_info &section,
5588 bool is_dwz, int base_offset)
5589 {
5590 sect_offset sect_off_prev;
5591 for (uint32_t i = 0; i <= map.cu_count; ++i)
5592 {
5593 sect_offset sect_off_next;
5594 if (i < map.cu_count)
5595 {
5596 sect_off_next
5597 = (sect_offset) (extract_unsigned_integer
5598 (map.cu_table_reordered + i * map.offset_size,
5599 map.offset_size,
5600 map.dwarf5_byte_order));
5601 }
5602 else
5603 sect_off_next = (sect_offset) section.size;
5604 if (i >= 1)
5605 {
5606 const ULONGEST length = sect_off_next - sect_off_prev;
5607 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5608 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5609 sect_off_prev, length);
5610 }
5611 sect_off_prev = sect_off_next;
5612 }
5613 }
5614
5615 /* Read the CU list from the mapped index, and use it to create all
5616 the CU objects for this dwarf2_per_objfile. */
5617
5618 static void
5619 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5620 const mapped_debug_names &map,
5621 const mapped_debug_names &dwz_map)
5622 {
5623 struct objfile *objfile = dwarf2_per_objfile->objfile;
5624
5625 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5626 dwarf2_per_objfile->all_comp_units
5627 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5628 dwarf2_per_objfile->n_comp_units);
5629
5630 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5631 dwarf2_per_objfile->info,
5632 false /* is_dwz */,
5633 0 /* base_offset */);
5634
5635 if (dwz_map.cu_count == 0)
5636 return;
5637
5638 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5639 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5640 true /* is_dwz */,
5641 map.cu_count /* base_offset */);
5642 }
5643
5644 /* Read .debug_names. If everything went ok, initialize the "quick"
5645 elements of all the CUs and return true. Otherwise, return false. */
5646
5647 static bool
5648 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5649 {
5650 mapped_debug_names local_map (dwarf2_per_objfile);
5651 mapped_debug_names dwz_map (dwarf2_per_objfile);
5652 struct objfile *objfile = dwarf2_per_objfile->objfile;
5653
5654 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5655 &dwarf2_per_objfile->debug_names,
5656 local_map))
5657 return false;
5658
5659 /* Don't use the index if it's empty. */
5660 if (local_map.name_count == 0)
5661 return false;
5662
5663 /* If there is a .dwz file, read it so we can get its CU list as
5664 well. */
5665 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5666 if (dwz != NULL)
5667 {
5668 if (!read_debug_names_from_section (objfile,
5669 bfd_get_filename (dwz->dwz_bfd),
5670 &dwz->debug_names, dwz_map))
5671 {
5672 warning (_("could not read '.debug_names' section from %s; skipping"),
5673 bfd_get_filename (dwz->dwz_bfd));
5674 return false;
5675 }
5676 }
5677
5678 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
5679
5680 if (local_map.tu_count != 0)
5681 {
5682 /* We can only handle a single .debug_types when we have an
5683 index. */
5684 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5685 return false;
5686
5687 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5688 dwarf2_per_objfile->types, 0);
5689
5690 create_signatured_type_table_from_debug_names
5691 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5692 }
5693
5694 create_addrmap_from_aranges (dwarf2_per_objfile,
5695 &dwarf2_per_objfile->debug_aranges);
5696
5697 dwarf2_per_objfile->debug_names_table.reset
5698 (new mapped_debug_names (dwarf2_per_objfile));
5699 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5700 dwarf2_per_objfile->using_index = 1;
5701 dwarf2_per_objfile->quick_file_names_table =
5702 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5703
5704 return true;
5705 }
5706
5707 /* Type used to manage iterating over all CUs looking for a symbol for
5708 .debug_names. */
5709
5710 class dw2_debug_names_iterator
5711 {
5712 public:
5713 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5714 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5715 dw2_debug_names_iterator (const mapped_debug_names &map,
5716 bool want_specific_block,
5717 block_enum block_index, domain_enum domain,
5718 const char *name)
5719 : m_map (map), m_want_specific_block (want_specific_block),
5720 m_block_index (block_index), m_domain (domain),
5721 m_addr (find_vec_in_debug_names (map, name))
5722 {}
5723
5724 dw2_debug_names_iterator (const mapped_debug_names &map,
5725 search_domain search, uint32_t namei)
5726 : m_map (map),
5727 m_search (search),
5728 m_addr (find_vec_in_debug_names (map, namei))
5729 {}
5730
5731 /* Return the next matching CU or NULL if there are no more. */
5732 dwarf2_per_cu_data *next ();
5733
5734 private:
5735 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5736 const char *name);
5737 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5738 uint32_t namei);
5739
5740 /* The internalized form of .debug_names. */
5741 const mapped_debug_names &m_map;
5742
5743 /* If true, only look for symbols that match BLOCK_INDEX. */
5744 const bool m_want_specific_block = false;
5745
5746 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5747 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5748 value. */
5749 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5750
5751 /* The kind of symbol we're looking for. */
5752 const domain_enum m_domain = UNDEF_DOMAIN;
5753 const search_domain m_search = ALL_DOMAIN;
5754
5755 /* The list of CUs from the index entry of the symbol, or NULL if
5756 not found. */
5757 const gdb_byte *m_addr;
5758 };
5759
5760 const char *
5761 mapped_debug_names::namei_to_name (uint32_t namei) const
5762 {
5763 const ULONGEST namei_string_offs
5764 = extract_unsigned_integer ((name_table_string_offs_reordered
5765 + namei * offset_size),
5766 offset_size,
5767 dwarf5_byte_order);
5768 return read_indirect_string_at_offset
5769 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5770 }
5771
5772 /* Find a slot in .debug_names for the object named NAME. If NAME is
5773 found, return pointer to its pool data. If NAME cannot be found,
5774 return NULL. */
5775
5776 const gdb_byte *
5777 dw2_debug_names_iterator::find_vec_in_debug_names
5778 (const mapped_debug_names &map, const char *name)
5779 {
5780 int (*cmp) (const char *, const char *);
5781
5782 if (current_language->la_language == language_cplus
5783 || current_language->la_language == language_fortran
5784 || current_language->la_language == language_d)
5785 {
5786 /* NAME is already canonical. Drop any qualifiers as
5787 .debug_names does not contain any. */
5788
5789 if (strchr (name, '(') != NULL)
5790 {
5791 gdb::unique_xmalloc_ptr<char> without_params
5792 = cp_remove_params (name);
5793
5794 if (without_params != NULL)
5795 {
5796 name = without_params.get();
5797 }
5798 }
5799 }
5800
5801 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5802
5803 const uint32_t full_hash = dwarf5_djb_hash (name);
5804 uint32_t namei
5805 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5806 (map.bucket_table_reordered
5807 + (full_hash % map.bucket_count)), 4,
5808 map.dwarf5_byte_order);
5809 if (namei == 0)
5810 return NULL;
5811 --namei;
5812 if (namei >= map.name_count)
5813 {
5814 complaint (&symfile_complaints,
5815 _("Wrong .debug_names with name index %u but name_count=%u "
5816 "[in module %s]"),
5817 namei, map.name_count,
5818 objfile_name (map.dwarf2_per_objfile->objfile));
5819 return NULL;
5820 }
5821
5822 for (;;)
5823 {
5824 const uint32_t namei_full_hash
5825 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5826 (map.hash_table_reordered + namei), 4,
5827 map.dwarf5_byte_order);
5828 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5829 return NULL;
5830
5831 if (full_hash == namei_full_hash)
5832 {
5833 const char *const namei_string = map.namei_to_name (namei);
5834
5835 #if 0 /* An expensive sanity check. */
5836 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5837 {
5838 complaint (&symfile_complaints,
5839 _("Wrong .debug_names hash for string at index %u "
5840 "[in module %s]"),
5841 namei, objfile_name (dwarf2_per_objfile->objfile));
5842 return NULL;
5843 }
5844 #endif
5845
5846 if (cmp (namei_string, name) == 0)
5847 {
5848 const ULONGEST namei_entry_offs
5849 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5850 + namei * map.offset_size),
5851 map.offset_size, map.dwarf5_byte_order);
5852 return map.entry_pool + namei_entry_offs;
5853 }
5854 }
5855
5856 ++namei;
5857 if (namei >= map.name_count)
5858 return NULL;
5859 }
5860 }
5861
5862 const gdb_byte *
5863 dw2_debug_names_iterator::find_vec_in_debug_names
5864 (const mapped_debug_names &map, uint32_t namei)
5865 {
5866 if (namei >= map.name_count)
5867 {
5868 complaint (&symfile_complaints,
5869 _("Wrong .debug_names with name index %u but name_count=%u "
5870 "[in module %s]"),
5871 namei, map.name_count,
5872 objfile_name (map.dwarf2_per_objfile->objfile));
5873 return NULL;
5874 }
5875
5876 const ULONGEST namei_entry_offs
5877 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5878 + namei * map.offset_size),
5879 map.offset_size, map.dwarf5_byte_order);
5880 return map.entry_pool + namei_entry_offs;
5881 }
5882
5883 /* See dw2_debug_names_iterator. */
5884
5885 dwarf2_per_cu_data *
5886 dw2_debug_names_iterator::next ()
5887 {
5888 if (m_addr == NULL)
5889 return NULL;
5890
5891 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5892 struct objfile *objfile = dwarf2_per_objfile->objfile;
5893 bfd *const abfd = objfile->obfd;
5894
5895 again:
5896
5897 unsigned int bytes_read;
5898 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5899 m_addr += bytes_read;
5900 if (abbrev == 0)
5901 return NULL;
5902
5903 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5904 if (indexval_it == m_map.abbrev_map.cend ())
5905 {
5906 complaint (&symfile_complaints,
5907 _("Wrong .debug_names undefined abbrev code %s "
5908 "[in module %s]"),
5909 pulongest (abbrev), objfile_name (objfile));
5910 return NULL;
5911 }
5912 const mapped_debug_names::index_val &indexval = indexval_it->second;
5913 bool have_is_static = false;
5914 bool is_static;
5915 dwarf2_per_cu_data *per_cu = NULL;
5916 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5917 {
5918 ULONGEST ull;
5919 switch (attr.form)
5920 {
5921 case DW_FORM_implicit_const:
5922 ull = attr.implicit_const;
5923 break;
5924 case DW_FORM_flag_present:
5925 ull = 1;
5926 break;
5927 case DW_FORM_udata:
5928 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5929 m_addr += bytes_read;
5930 break;
5931 default:
5932 complaint (&symfile_complaints,
5933 _("Unsupported .debug_names form %s [in module %s]"),
5934 dwarf_form_name (attr.form),
5935 objfile_name (objfile));
5936 return NULL;
5937 }
5938 switch (attr.dw_idx)
5939 {
5940 case DW_IDX_compile_unit:
5941 /* Don't crash on bad data. */
5942 if (ull >= dwarf2_per_objfile->n_comp_units)
5943 {
5944 complaint (&symfile_complaints,
5945 _(".debug_names entry has bad CU index %s"
5946 " [in module %s]"),
5947 pulongest (ull),
5948 objfile_name (dwarf2_per_objfile->objfile));
5949 continue;
5950 }
5951 per_cu = dwarf2_per_objfile->get_cutu (ull);
5952 break;
5953 case DW_IDX_type_unit:
5954 /* Don't crash on bad data. */
5955 if (ull >= dwarf2_per_objfile->n_type_units)
5956 {
5957 complaint (&symfile_complaints,
5958 _(".debug_names entry has bad TU index %s"
5959 " [in module %s]"),
5960 pulongest (ull),
5961 objfile_name (dwarf2_per_objfile->objfile));
5962 continue;
5963 }
5964 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5965 break;
5966 case DW_IDX_GNU_internal:
5967 if (!m_map.augmentation_is_gdb)
5968 break;
5969 have_is_static = true;
5970 is_static = true;
5971 break;
5972 case DW_IDX_GNU_external:
5973 if (!m_map.augmentation_is_gdb)
5974 break;
5975 have_is_static = true;
5976 is_static = false;
5977 break;
5978 }
5979 }
5980
5981 /* Skip if already read in. */
5982 if (per_cu->v.quick->compunit_symtab)
5983 goto again;
5984
5985 /* Check static vs global. */
5986 if (have_is_static)
5987 {
5988 const bool want_static = m_block_index != GLOBAL_BLOCK;
5989 if (m_want_specific_block && want_static != is_static)
5990 goto again;
5991 }
5992
5993 /* Match dw2_symtab_iter_next, symbol_kind
5994 and debug_names::psymbol_tag. */
5995 switch (m_domain)
5996 {
5997 case VAR_DOMAIN:
5998 switch (indexval.dwarf_tag)
5999 {
6000 case DW_TAG_variable:
6001 case DW_TAG_subprogram:
6002 /* Some types are also in VAR_DOMAIN. */
6003 case DW_TAG_typedef:
6004 case DW_TAG_structure_type:
6005 break;
6006 default:
6007 goto again;
6008 }
6009 break;
6010 case STRUCT_DOMAIN:
6011 switch (indexval.dwarf_tag)
6012 {
6013 case DW_TAG_typedef:
6014 case DW_TAG_structure_type:
6015 break;
6016 default:
6017 goto again;
6018 }
6019 break;
6020 case LABEL_DOMAIN:
6021 switch (indexval.dwarf_tag)
6022 {
6023 case 0:
6024 case DW_TAG_variable:
6025 break;
6026 default:
6027 goto again;
6028 }
6029 break;
6030 default:
6031 break;
6032 }
6033
6034 /* Match dw2_expand_symtabs_matching, symbol_kind and
6035 debug_names::psymbol_tag. */
6036 switch (m_search)
6037 {
6038 case VARIABLES_DOMAIN:
6039 switch (indexval.dwarf_tag)
6040 {
6041 case DW_TAG_variable:
6042 break;
6043 default:
6044 goto again;
6045 }
6046 break;
6047 case FUNCTIONS_DOMAIN:
6048 switch (indexval.dwarf_tag)
6049 {
6050 case DW_TAG_subprogram:
6051 break;
6052 default:
6053 goto again;
6054 }
6055 break;
6056 case TYPES_DOMAIN:
6057 switch (indexval.dwarf_tag)
6058 {
6059 case DW_TAG_typedef:
6060 case DW_TAG_structure_type:
6061 break;
6062 default:
6063 goto again;
6064 }
6065 break;
6066 default:
6067 break;
6068 }
6069
6070 return per_cu;
6071 }
6072
6073 static struct compunit_symtab *
6074 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6075 const char *name, domain_enum domain)
6076 {
6077 const block_enum block_index = static_cast<block_enum> (block_index_int);
6078 struct dwarf2_per_objfile *dwarf2_per_objfile
6079 = get_dwarf2_per_objfile (objfile);
6080
6081 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6082 if (!mapp)
6083 {
6084 /* index is NULL if OBJF_READNOW. */
6085 return NULL;
6086 }
6087 const auto &map = *mapp;
6088
6089 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6090 block_index, domain, name);
6091
6092 struct compunit_symtab *stab_best = NULL;
6093 struct dwarf2_per_cu_data *per_cu;
6094 while ((per_cu = iter.next ()) != NULL)
6095 {
6096 struct symbol *sym, *with_opaque = NULL;
6097 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6098 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6099 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6100
6101 sym = block_find_symbol (block, name, domain,
6102 block_find_non_opaque_type_preferred,
6103 &with_opaque);
6104
6105 /* Some caution must be observed with overloaded functions and
6106 methods, since the index will not contain any overload
6107 information (but NAME might contain it). */
6108
6109 if (sym != NULL
6110 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6111 return stab;
6112 if (with_opaque != NULL
6113 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6114 stab_best = stab;
6115
6116 /* Keep looking through other CUs. */
6117 }
6118
6119 return stab_best;
6120 }
6121
6122 /* This dumps minimal information about .debug_names. It is called
6123 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6124 uses this to verify that .debug_names has been loaded. */
6125
6126 static void
6127 dw2_debug_names_dump (struct objfile *objfile)
6128 {
6129 struct dwarf2_per_objfile *dwarf2_per_objfile
6130 = get_dwarf2_per_objfile (objfile);
6131
6132 gdb_assert (dwarf2_per_objfile->using_index);
6133 printf_filtered (".debug_names:");
6134 if (dwarf2_per_objfile->debug_names_table)
6135 printf_filtered (" exists\n");
6136 else
6137 printf_filtered (" faked for \"readnow\"\n");
6138 printf_filtered ("\n");
6139 }
6140
6141 static void
6142 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6143 const char *func_name)
6144 {
6145 struct dwarf2_per_objfile *dwarf2_per_objfile
6146 = get_dwarf2_per_objfile (objfile);
6147
6148 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6149 if (dwarf2_per_objfile->debug_names_table)
6150 {
6151 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6152
6153 /* Note: It doesn't matter what we pass for block_index here. */
6154 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6155 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6156
6157 struct dwarf2_per_cu_data *per_cu;
6158 while ((per_cu = iter.next ()) != NULL)
6159 dw2_instantiate_symtab (per_cu);
6160 }
6161 }
6162
6163 static void
6164 dw2_debug_names_expand_symtabs_matching
6165 (struct objfile *objfile,
6166 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6167 const lookup_name_info &lookup_name,
6168 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6169 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6170 enum search_domain kind)
6171 {
6172 struct dwarf2_per_objfile *dwarf2_per_objfile
6173 = get_dwarf2_per_objfile (objfile);
6174
6175 /* debug_names_table is NULL if OBJF_READNOW. */
6176 if (!dwarf2_per_objfile->debug_names_table)
6177 return;
6178
6179 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6180
6181 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6182
6183 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6184 symbol_matcher,
6185 kind, [&] (offset_type namei)
6186 {
6187 /* The name was matched, now expand corresponding CUs that were
6188 marked. */
6189 dw2_debug_names_iterator iter (map, kind, namei);
6190
6191 struct dwarf2_per_cu_data *per_cu;
6192 while ((per_cu = iter.next ()) != NULL)
6193 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6194 expansion_notify);
6195 });
6196 }
6197
6198 const struct quick_symbol_functions dwarf2_debug_names_functions =
6199 {
6200 dw2_has_symbols,
6201 dw2_find_last_source_symtab,
6202 dw2_forget_cached_source_info,
6203 dw2_map_symtabs_matching_filename,
6204 dw2_debug_names_lookup_symbol,
6205 dw2_print_stats,
6206 dw2_debug_names_dump,
6207 dw2_relocate,
6208 dw2_debug_names_expand_symtabs_for_function,
6209 dw2_expand_all_symtabs,
6210 dw2_expand_symtabs_with_fullname,
6211 dw2_map_matching_symbols,
6212 dw2_debug_names_expand_symtabs_matching,
6213 dw2_find_pc_sect_compunit_symtab,
6214 NULL,
6215 dw2_map_symbol_filenames
6216 };
6217
6218 /* See symfile.h. */
6219
6220 bool
6221 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6222 {
6223 struct dwarf2_per_objfile *dwarf2_per_objfile
6224 = get_dwarf2_per_objfile (objfile);
6225
6226 /* If we're about to read full symbols, don't bother with the
6227 indices. In this case we also don't care if some other debug
6228 format is making psymtabs, because they are all about to be
6229 expanded anyway. */
6230 if ((objfile->flags & OBJF_READNOW))
6231 {
6232 int i;
6233
6234 dwarf2_per_objfile->using_index = 1;
6235 create_all_comp_units (dwarf2_per_objfile);
6236 create_all_type_units (dwarf2_per_objfile);
6237 dwarf2_per_objfile->quick_file_names_table =
6238 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6239
6240 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6241 + dwarf2_per_objfile->n_type_units); ++i)
6242 {
6243 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6244
6245 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6246 struct dwarf2_per_cu_quick_data);
6247 }
6248
6249 /* Return 1 so that gdb sees the "quick" functions. However,
6250 these functions will be no-ops because we will have expanded
6251 all symtabs. */
6252 *index_kind = dw_index_kind::GDB_INDEX;
6253 return true;
6254 }
6255
6256 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6257 {
6258 *index_kind = dw_index_kind::DEBUG_NAMES;
6259 return true;
6260 }
6261
6262 if (dwarf2_read_index (objfile))
6263 {
6264 *index_kind = dw_index_kind::GDB_INDEX;
6265 return true;
6266 }
6267
6268 return false;
6269 }
6270
6271 \f
6272
6273 /* Build a partial symbol table. */
6274
6275 void
6276 dwarf2_build_psymtabs (struct objfile *objfile)
6277 {
6278 struct dwarf2_per_objfile *dwarf2_per_objfile
6279 = get_dwarf2_per_objfile (objfile);
6280
6281 if (objfile->global_psymbols.capacity () == 0
6282 && objfile->static_psymbols.capacity () == 0)
6283 init_psymbol_list (objfile, 1024);
6284
6285 TRY
6286 {
6287 /* This isn't really ideal: all the data we allocate on the
6288 objfile's obstack is still uselessly kept around. However,
6289 freeing it seems unsafe. */
6290 psymtab_discarder psymtabs (objfile);
6291 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6292 psymtabs.keep ();
6293 }
6294 CATCH (except, RETURN_MASK_ERROR)
6295 {
6296 exception_print (gdb_stderr, except);
6297 }
6298 END_CATCH
6299 }
6300
6301 /* Return the total length of the CU described by HEADER. */
6302
6303 static unsigned int
6304 get_cu_length (const struct comp_unit_head *header)
6305 {
6306 return header->initial_length_size + header->length;
6307 }
6308
6309 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6310
6311 static inline bool
6312 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6313 {
6314 sect_offset bottom = cu_header->sect_off;
6315 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6316
6317 return sect_off >= bottom && sect_off < top;
6318 }
6319
6320 /* Find the base address of the compilation unit for range lists and
6321 location lists. It will normally be specified by DW_AT_low_pc.
6322 In DWARF-3 draft 4, the base address could be overridden by
6323 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6324 compilation units with discontinuous ranges. */
6325
6326 static void
6327 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6328 {
6329 struct attribute *attr;
6330
6331 cu->base_known = 0;
6332 cu->base_address = 0;
6333
6334 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6335 if (attr)
6336 {
6337 cu->base_address = attr_value_as_address (attr);
6338 cu->base_known = 1;
6339 }
6340 else
6341 {
6342 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6343 if (attr)
6344 {
6345 cu->base_address = attr_value_as_address (attr);
6346 cu->base_known = 1;
6347 }
6348 }
6349 }
6350
6351 /* Read in the comp unit header information from the debug_info at info_ptr.
6352 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6353 NOTE: This leaves members offset, first_die_offset to be filled in
6354 by the caller. */
6355
6356 static const gdb_byte *
6357 read_comp_unit_head (struct comp_unit_head *cu_header,
6358 const gdb_byte *info_ptr,
6359 struct dwarf2_section_info *section,
6360 rcuh_kind section_kind)
6361 {
6362 int signed_addr;
6363 unsigned int bytes_read;
6364 const char *filename = get_section_file_name (section);
6365 bfd *abfd = get_section_bfd_owner (section);
6366
6367 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6368 cu_header->initial_length_size = bytes_read;
6369 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6370 info_ptr += bytes_read;
6371 cu_header->version = read_2_bytes (abfd, info_ptr);
6372 info_ptr += 2;
6373 if (cu_header->version < 5)
6374 switch (section_kind)
6375 {
6376 case rcuh_kind::COMPILE:
6377 cu_header->unit_type = DW_UT_compile;
6378 break;
6379 case rcuh_kind::TYPE:
6380 cu_header->unit_type = DW_UT_type;
6381 break;
6382 default:
6383 internal_error (__FILE__, __LINE__,
6384 _("read_comp_unit_head: invalid section_kind"));
6385 }
6386 else
6387 {
6388 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6389 (read_1_byte (abfd, info_ptr));
6390 info_ptr += 1;
6391 switch (cu_header->unit_type)
6392 {
6393 case DW_UT_compile:
6394 if (section_kind != rcuh_kind::COMPILE)
6395 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6396 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6397 filename);
6398 break;
6399 case DW_UT_type:
6400 section_kind = rcuh_kind::TYPE;
6401 break;
6402 default:
6403 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6404 "(is %d, should be %d or %d) [in module %s]"),
6405 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6406 }
6407
6408 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6409 info_ptr += 1;
6410 }
6411 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6412 cu_header,
6413 &bytes_read);
6414 info_ptr += bytes_read;
6415 if (cu_header->version < 5)
6416 {
6417 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6418 info_ptr += 1;
6419 }
6420 signed_addr = bfd_get_sign_extend_vma (abfd);
6421 if (signed_addr < 0)
6422 internal_error (__FILE__, __LINE__,
6423 _("read_comp_unit_head: dwarf from non elf file"));
6424 cu_header->signed_addr_p = signed_addr;
6425
6426 if (section_kind == rcuh_kind::TYPE)
6427 {
6428 LONGEST type_offset;
6429
6430 cu_header->signature = read_8_bytes (abfd, info_ptr);
6431 info_ptr += 8;
6432
6433 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6434 info_ptr += bytes_read;
6435 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6436 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6437 error (_("Dwarf Error: Too big type_offset in compilation unit "
6438 "header (is %s) [in module %s]"), plongest (type_offset),
6439 filename);
6440 }
6441
6442 return info_ptr;
6443 }
6444
6445 /* Helper function that returns the proper abbrev section for
6446 THIS_CU. */
6447
6448 static struct dwarf2_section_info *
6449 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6450 {
6451 struct dwarf2_section_info *abbrev;
6452 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6453
6454 if (this_cu->is_dwz)
6455 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6456 else
6457 abbrev = &dwarf2_per_objfile->abbrev;
6458
6459 return abbrev;
6460 }
6461
6462 /* Subroutine of read_and_check_comp_unit_head and
6463 read_and_check_type_unit_head to simplify them.
6464 Perform various error checking on the header. */
6465
6466 static void
6467 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6468 struct comp_unit_head *header,
6469 struct dwarf2_section_info *section,
6470 struct dwarf2_section_info *abbrev_section)
6471 {
6472 const char *filename = get_section_file_name (section);
6473
6474 if (header->version < 2 || header->version > 5)
6475 error (_("Dwarf Error: wrong version in compilation unit header "
6476 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6477 filename);
6478
6479 if (to_underlying (header->abbrev_sect_off)
6480 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6481 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6482 "(offset %s + 6) [in module %s]"),
6483 sect_offset_str (header->abbrev_sect_off),
6484 sect_offset_str (header->sect_off),
6485 filename);
6486
6487 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6488 avoid potential 32-bit overflow. */
6489 if (((ULONGEST) header->sect_off + get_cu_length (header))
6490 > section->size)
6491 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6492 "(offset %s + 0) [in module %s]"),
6493 header->length, sect_offset_str (header->sect_off),
6494 filename);
6495 }
6496
6497 /* Read in a CU/TU header and perform some basic error checking.
6498 The contents of the header are stored in HEADER.
6499 The result is a pointer to the start of the first DIE. */
6500
6501 static const gdb_byte *
6502 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6503 struct comp_unit_head *header,
6504 struct dwarf2_section_info *section,
6505 struct dwarf2_section_info *abbrev_section,
6506 const gdb_byte *info_ptr,
6507 rcuh_kind section_kind)
6508 {
6509 const gdb_byte *beg_of_comp_unit = info_ptr;
6510
6511 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6512
6513 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6514
6515 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6516
6517 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6518 abbrev_section);
6519
6520 return info_ptr;
6521 }
6522
6523 /* Fetch the abbreviation table offset from a comp or type unit header. */
6524
6525 static sect_offset
6526 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6527 struct dwarf2_section_info *section,
6528 sect_offset sect_off)
6529 {
6530 bfd *abfd = get_section_bfd_owner (section);
6531 const gdb_byte *info_ptr;
6532 unsigned int initial_length_size, offset_size;
6533 uint16_t version;
6534
6535 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6536 info_ptr = section->buffer + to_underlying (sect_off);
6537 read_initial_length (abfd, info_ptr, &initial_length_size);
6538 offset_size = initial_length_size == 4 ? 4 : 8;
6539 info_ptr += initial_length_size;
6540
6541 version = read_2_bytes (abfd, info_ptr);
6542 info_ptr += 2;
6543 if (version >= 5)
6544 {
6545 /* Skip unit type and address size. */
6546 info_ptr += 2;
6547 }
6548
6549 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6550 }
6551
6552 /* Allocate a new partial symtab for file named NAME and mark this new
6553 partial symtab as being an include of PST. */
6554
6555 static void
6556 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6557 struct objfile *objfile)
6558 {
6559 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6560
6561 if (!IS_ABSOLUTE_PATH (subpst->filename))
6562 {
6563 /* It shares objfile->objfile_obstack. */
6564 subpst->dirname = pst->dirname;
6565 }
6566
6567 subpst->textlow = 0;
6568 subpst->texthigh = 0;
6569
6570 subpst->dependencies
6571 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6572 subpst->dependencies[0] = pst;
6573 subpst->number_of_dependencies = 1;
6574
6575 subpst->globals_offset = 0;
6576 subpst->n_global_syms = 0;
6577 subpst->statics_offset = 0;
6578 subpst->n_static_syms = 0;
6579 subpst->compunit_symtab = NULL;
6580 subpst->read_symtab = pst->read_symtab;
6581 subpst->readin = 0;
6582
6583 /* No private part is necessary for include psymtabs. This property
6584 can be used to differentiate between such include psymtabs and
6585 the regular ones. */
6586 subpst->read_symtab_private = NULL;
6587 }
6588
6589 /* Read the Line Number Program data and extract the list of files
6590 included by the source file represented by PST. Build an include
6591 partial symtab for each of these included files. */
6592
6593 static void
6594 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6595 struct die_info *die,
6596 struct partial_symtab *pst)
6597 {
6598 line_header_up lh;
6599 struct attribute *attr;
6600
6601 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6602 if (attr)
6603 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6604 if (lh == NULL)
6605 return; /* No linetable, so no includes. */
6606
6607 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6608 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6609 }
6610
6611 static hashval_t
6612 hash_signatured_type (const void *item)
6613 {
6614 const struct signatured_type *sig_type
6615 = (const struct signatured_type *) item;
6616
6617 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6618 return sig_type->signature;
6619 }
6620
6621 static int
6622 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6623 {
6624 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6625 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6626
6627 return lhs->signature == rhs->signature;
6628 }
6629
6630 /* Allocate a hash table for signatured types. */
6631
6632 static htab_t
6633 allocate_signatured_type_table (struct objfile *objfile)
6634 {
6635 return htab_create_alloc_ex (41,
6636 hash_signatured_type,
6637 eq_signatured_type,
6638 NULL,
6639 &objfile->objfile_obstack,
6640 hashtab_obstack_allocate,
6641 dummy_obstack_deallocate);
6642 }
6643
6644 /* A helper function to add a signatured type CU to a table. */
6645
6646 static int
6647 add_signatured_type_cu_to_table (void **slot, void *datum)
6648 {
6649 struct signatured_type *sigt = (struct signatured_type *) *slot;
6650 struct signatured_type ***datap = (struct signatured_type ***) datum;
6651
6652 **datap = sigt;
6653 ++*datap;
6654
6655 return 1;
6656 }
6657
6658 /* A helper for create_debug_types_hash_table. Read types from SECTION
6659 and fill them into TYPES_HTAB. It will process only type units,
6660 therefore DW_UT_type. */
6661
6662 static void
6663 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6664 struct dwo_file *dwo_file,
6665 dwarf2_section_info *section, htab_t &types_htab,
6666 rcuh_kind section_kind)
6667 {
6668 struct objfile *objfile = dwarf2_per_objfile->objfile;
6669 struct dwarf2_section_info *abbrev_section;
6670 bfd *abfd;
6671 const gdb_byte *info_ptr, *end_ptr;
6672
6673 abbrev_section = (dwo_file != NULL
6674 ? &dwo_file->sections.abbrev
6675 : &dwarf2_per_objfile->abbrev);
6676
6677 if (dwarf_read_debug)
6678 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6679 get_section_name (section),
6680 get_section_file_name (abbrev_section));
6681
6682 dwarf2_read_section (objfile, section);
6683 info_ptr = section->buffer;
6684
6685 if (info_ptr == NULL)
6686 return;
6687
6688 /* We can't set abfd until now because the section may be empty or
6689 not present, in which case the bfd is unknown. */
6690 abfd = get_section_bfd_owner (section);
6691
6692 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6693 because we don't need to read any dies: the signature is in the
6694 header. */
6695
6696 end_ptr = info_ptr + section->size;
6697 while (info_ptr < end_ptr)
6698 {
6699 struct signatured_type *sig_type;
6700 struct dwo_unit *dwo_tu;
6701 void **slot;
6702 const gdb_byte *ptr = info_ptr;
6703 struct comp_unit_head header;
6704 unsigned int length;
6705
6706 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6707
6708 /* Initialize it due to a false compiler warning. */
6709 header.signature = -1;
6710 header.type_cu_offset_in_tu = (cu_offset) -1;
6711
6712 /* We need to read the type's signature in order to build the hash
6713 table, but we don't need anything else just yet. */
6714
6715 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6716 abbrev_section, ptr, section_kind);
6717
6718 length = get_cu_length (&header);
6719
6720 /* Skip dummy type units. */
6721 if (ptr >= info_ptr + length
6722 || peek_abbrev_code (abfd, ptr) == 0
6723 || header.unit_type != DW_UT_type)
6724 {
6725 info_ptr += length;
6726 continue;
6727 }
6728
6729 if (types_htab == NULL)
6730 {
6731 if (dwo_file)
6732 types_htab = allocate_dwo_unit_table (objfile);
6733 else
6734 types_htab = allocate_signatured_type_table (objfile);
6735 }
6736
6737 if (dwo_file)
6738 {
6739 sig_type = NULL;
6740 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6741 struct dwo_unit);
6742 dwo_tu->dwo_file = dwo_file;
6743 dwo_tu->signature = header.signature;
6744 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6745 dwo_tu->section = section;
6746 dwo_tu->sect_off = sect_off;
6747 dwo_tu->length = length;
6748 }
6749 else
6750 {
6751 /* N.B.: type_offset is not usable if this type uses a DWO file.
6752 The real type_offset is in the DWO file. */
6753 dwo_tu = NULL;
6754 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6755 struct signatured_type);
6756 sig_type->signature = header.signature;
6757 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6758 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6759 sig_type->per_cu.is_debug_types = 1;
6760 sig_type->per_cu.section = section;
6761 sig_type->per_cu.sect_off = sect_off;
6762 sig_type->per_cu.length = length;
6763 }
6764
6765 slot = htab_find_slot (types_htab,
6766 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6767 INSERT);
6768 gdb_assert (slot != NULL);
6769 if (*slot != NULL)
6770 {
6771 sect_offset dup_sect_off;
6772
6773 if (dwo_file)
6774 {
6775 const struct dwo_unit *dup_tu
6776 = (const struct dwo_unit *) *slot;
6777
6778 dup_sect_off = dup_tu->sect_off;
6779 }
6780 else
6781 {
6782 const struct signatured_type *dup_tu
6783 = (const struct signatured_type *) *slot;
6784
6785 dup_sect_off = dup_tu->per_cu.sect_off;
6786 }
6787
6788 complaint (&symfile_complaints,
6789 _("debug type entry at offset %s is duplicate to"
6790 " the entry at offset %s, signature %s"),
6791 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6792 hex_string (header.signature));
6793 }
6794 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6795
6796 if (dwarf_read_debug > 1)
6797 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6798 sect_offset_str (sect_off),
6799 hex_string (header.signature));
6800
6801 info_ptr += length;
6802 }
6803 }
6804
6805 /* Create the hash table of all entries in the .debug_types
6806 (or .debug_types.dwo) section(s).
6807 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6808 otherwise it is NULL.
6809
6810 The result is a pointer to the hash table or NULL if there are no types.
6811
6812 Note: This function processes DWO files only, not DWP files. */
6813
6814 static void
6815 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6816 struct dwo_file *dwo_file,
6817 VEC (dwarf2_section_info_def) *types,
6818 htab_t &types_htab)
6819 {
6820 int ix;
6821 struct dwarf2_section_info *section;
6822
6823 if (VEC_empty (dwarf2_section_info_def, types))
6824 return;
6825
6826 for (ix = 0;
6827 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6828 ++ix)
6829 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6830 types_htab, rcuh_kind::TYPE);
6831 }
6832
6833 /* Create the hash table of all entries in the .debug_types section,
6834 and initialize all_type_units.
6835 The result is zero if there is an error (e.g. missing .debug_types section),
6836 otherwise non-zero. */
6837
6838 static int
6839 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6840 {
6841 htab_t types_htab = NULL;
6842 struct signatured_type **iter;
6843
6844 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6845 &dwarf2_per_objfile->info, types_htab,
6846 rcuh_kind::COMPILE);
6847 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6848 dwarf2_per_objfile->types, types_htab);
6849 if (types_htab == NULL)
6850 {
6851 dwarf2_per_objfile->signatured_types = NULL;
6852 return 0;
6853 }
6854
6855 dwarf2_per_objfile->signatured_types = types_htab;
6856
6857 dwarf2_per_objfile->n_type_units
6858 = dwarf2_per_objfile->n_allocated_type_units
6859 = htab_elements (types_htab);
6860 dwarf2_per_objfile->all_type_units =
6861 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
6862 iter = &dwarf2_per_objfile->all_type_units[0];
6863 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
6864 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
6865 == dwarf2_per_objfile->n_type_units);
6866
6867 return 1;
6868 }
6869
6870 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6871 If SLOT is non-NULL, it is the entry to use in the hash table.
6872 Otherwise we find one. */
6873
6874 static struct signatured_type *
6875 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6876 void **slot)
6877 {
6878 struct objfile *objfile = dwarf2_per_objfile->objfile;
6879 int n_type_units = dwarf2_per_objfile->n_type_units;
6880 struct signatured_type *sig_type;
6881
6882 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
6883 ++n_type_units;
6884 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
6885 {
6886 if (dwarf2_per_objfile->n_allocated_type_units == 0)
6887 dwarf2_per_objfile->n_allocated_type_units = 1;
6888 dwarf2_per_objfile->n_allocated_type_units *= 2;
6889 dwarf2_per_objfile->all_type_units
6890 = XRESIZEVEC (struct signatured_type *,
6891 dwarf2_per_objfile->all_type_units,
6892 dwarf2_per_objfile->n_allocated_type_units);
6893 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6894 }
6895 dwarf2_per_objfile->n_type_units = n_type_units;
6896
6897 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6898 struct signatured_type);
6899 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
6900 sig_type->signature = sig;
6901 sig_type->per_cu.is_debug_types = 1;
6902 if (dwarf2_per_objfile->using_index)
6903 {
6904 sig_type->per_cu.v.quick =
6905 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6906 struct dwarf2_per_cu_quick_data);
6907 }
6908
6909 if (slot == NULL)
6910 {
6911 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6912 sig_type, INSERT);
6913 }
6914 gdb_assert (*slot == NULL);
6915 *slot = sig_type;
6916 /* The rest of sig_type must be filled in by the caller. */
6917 return sig_type;
6918 }
6919
6920 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6921 Fill in SIG_ENTRY with DWO_ENTRY. */
6922
6923 static void
6924 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6925 struct signatured_type *sig_entry,
6926 struct dwo_unit *dwo_entry)
6927 {
6928 /* Make sure we're not clobbering something we don't expect to. */
6929 gdb_assert (! sig_entry->per_cu.queued);
6930 gdb_assert (sig_entry->per_cu.cu == NULL);
6931 if (dwarf2_per_objfile->using_index)
6932 {
6933 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6934 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6935 }
6936 else
6937 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6938 gdb_assert (sig_entry->signature == dwo_entry->signature);
6939 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6940 gdb_assert (sig_entry->type_unit_group == NULL);
6941 gdb_assert (sig_entry->dwo_unit == NULL);
6942
6943 sig_entry->per_cu.section = dwo_entry->section;
6944 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6945 sig_entry->per_cu.length = dwo_entry->length;
6946 sig_entry->per_cu.reading_dwo_directly = 1;
6947 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6948 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6949 sig_entry->dwo_unit = dwo_entry;
6950 }
6951
6952 /* Subroutine of lookup_signatured_type.
6953 If we haven't read the TU yet, create the signatured_type data structure
6954 for a TU to be read in directly from a DWO file, bypassing the stub.
6955 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6956 using .gdb_index, then when reading a CU we want to stay in the DWO file
6957 containing that CU. Otherwise we could end up reading several other DWO
6958 files (due to comdat folding) to process the transitive closure of all the
6959 mentioned TUs, and that can be slow. The current DWO file will have every
6960 type signature that it needs.
6961 We only do this for .gdb_index because in the psymtab case we already have
6962 to read all the DWOs to build the type unit groups. */
6963
6964 static struct signatured_type *
6965 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6966 {
6967 struct dwarf2_per_objfile *dwarf2_per_objfile
6968 = cu->per_cu->dwarf2_per_objfile;
6969 struct objfile *objfile = dwarf2_per_objfile->objfile;
6970 struct dwo_file *dwo_file;
6971 struct dwo_unit find_dwo_entry, *dwo_entry;
6972 struct signatured_type find_sig_entry, *sig_entry;
6973 void **slot;
6974
6975 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6976
6977 /* If TU skeletons have been removed then we may not have read in any
6978 TUs yet. */
6979 if (dwarf2_per_objfile->signatured_types == NULL)
6980 {
6981 dwarf2_per_objfile->signatured_types
6982 = allocate_signatured_type_table (objfile);
6983 }
6984
6985 /* We only ever need to read in one copy of a signatured type.
6986 Use the global signatured_types array to do our own comdat-folding
6987 of types. If this is the first time we're reading this TU, and
6988 the TU has an entry in .gdb_index, replace the recorded data from
6989 .gdb_index with this TU. */
6990
6991 find_sig_entry.signature = sig;
6992 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6993 &find_sig_entry, INSERT);
6994 sig_entry = (struct signatured_type *) *slot;
6995
6996 /* We can get here with the TU already read, *or* in the process of being
6997 read. Don't reassign the global entry to point to this DWO if that's
6998 the case. Also note that if the TU is already being read, it may not
6999 have come from a DWO, the program may be a mix of Fission-compiled
7000 code and non-Fission-compiled code. */
7001
7002 /* Have we already tried to read this TU?
7003 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7004 needn't exist in the global table yet). */
7005 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7006 return sig_entry;
7007
7008 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7009 dwo_unit of the TU itself. */
7010 dwo_file = cu->dwo_unit->dwo_file;
7011
7012 /* Ok, this is the first time we're reading this TU. */
7013 if (dwo_file->tus == NULL)
7014 return NULL;
7015 find_dwo_entry.signature = sig;
7016 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7017 if (dwo_entry == NULL)
7018 return NULL;
7019
7020 /* If the global table doesn't have an entry for this TU, add one. */
7021 if (sig_entry == NULL)
7022 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7023
7024 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7025 sig_entry->per_cu.tu_read = 1;
7026 return sig_entry;
7027 }
7028
7029 /* Subroutine of lookup_signatured_type.
7030 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7031 then try the DWP file. If the TU stub (skeleton) has been removed then
7032 it won't be in .gdb_index. */
7033
7034 static struct signatured_type *
7035 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7036 {
7037 struct dwarf2_per_objfile *dwarf2_per_objfile
7038 = cu->per_cu->dwarf2_per_objfile;
7039 struct objfile *objfile = dwarf2_per_objfile->objfile;
7040 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7041 struct dwo_unit *dwo_entry;
7042 struct signatured_type find_sig_entry, *sig_entry;
7043 void **slot;
7044
7045 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7046 gdb_assert (dwp_file != NULL);
7047
7048 /* If TU skeletons have been removed then we may not have read in any
7049 TUs yet. */
7050 if (dwarf2_per_objfile->signatured_types == NULL)
7051 {
7052 dwarf2_per_objfile->signatured_types
7053 = allocate_signatured_type_table (objfile);
7054 }
7055
7056 find_sig_entry.signature = sig;
7057 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7058 &find_sig_entry, INSERT);
7059 sig_entry = (struct signatured_type *) *slot;
7060
7061 /* Have we already tried to read this TU?
7062 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7063 needn't exist in the global table yet). */
7064 if (sig_entry != NULL)
7065 return sig_entry;
7066
7067 if (dwp_file->tus == NULL)
7068 return NULL;
7069 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7070 sig, 1 /* is_debug_types */);
7071 if (dwo_entry == NULL)
7072 return NULL;
7073
7074 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7075 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7076
7077 return sig_entry;
7078 }
7079
7080 /* Lookup a signature based type for DW_FORM_ref_sig8.
7081 Returns NULL if signature SIG is not present in the table.
7082 It is up to the caller to complain about this. */
7083
7084 static struct signatured_type *
7085 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7086 {
7087 struct dwarf2_per_objfile *dwarf2_per_objfile
7088 = cu->per_cu->dwarf2_per_objfile;
7089
7090 if (cu->dwo_unit
7091 && dwarf2_per_objfile->using_index)
7092 {
7093 /* We're in a DWO/DWP file, and we're using .gdb_index.
7094 These cases require special processing. */
7095 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7096 return lookup_dwo_signatured_type (cu, sig);
7097 else
7098 return lookup_dwp_signatured_type (cu, sig);
7099 }
7100 else
7101 {
7102 struct signatured_type find_entry, *entry;
7103
7104 if (dwarf2_per_objfile->signatured_types == NULL)
7105 return NULL;
7106 find_entry.signature = sig;
7107 entry = ((struct signatured_type *)
7108 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7109 return entry;
7110 }
7111 }
7112 \f
7113 /* Low level DIE reading support. */
7114
7115 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7116
7117 static void
7118 init_cu_die_reader (struct die_reader_specs *reader,
7119 struct dwarf2_cu *cu,
7120 struct dwarf2_section_info *section,
7121 struct dwo_file *dwo_file,
7122 struct abbrev_table *abbrev_table)
7123 {
7124 gdb_assert (section->readin && section->buffer != NULL);
7125 reader->abfd = get_section_bfd_owner (section);
7126 reader->cu = cu;
7127 reader->dwo_file = dwo_file;
7128 reader->die_section = section;
7129 reader->buffer = section->buffer;
7130 reader->buffer_end = section->buffer + section->size;
7131 reader->comp_dir = NULL;
7132 reader->abbrev_table = abbrev_table;
7133 }
7134
7135 /* Subroutine of init_cutu_and_read_dies to simplify it.
7136 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7137 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7138 already.
7139
7140 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7141 from it to the DIE in the DWO. If NULL we are skipping the stub.
7142 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7143 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7144 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7145 STUB_COMP_DIR may be non-NULL.
7146 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7147 are filled in with the info of the DIE from the DWO file.
7148 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7149 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7150 kept around for at least as long as *RESULT_READER.
7151
7152 The result is non-zero if a valid (non-dummy) DIE was found. */
7153
7154 static int
7155 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7156 struct dwo_unit *dwo_unit,
7157 struct die_info *stub_comp_unit_die,
7158 const char *stub_comp_dir,
7159 struct die_reader_specs *result_reader,
7160 const gdb_byte **result_info_ptr,
7161 struct die_info **result_comp_unit_die,
7162 int *result_has_children,
7163 abbrev_table_up *result_dwo_abbrev_table)
7164 {
7165 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7166 struct objfile *objfile = dwarf2_per_objfile->objfile;
7167 struct dwarf2_cu *cu = this_cu->cu;
7168 bfd *abfd;
7169 const gdb_byte *begin_info_ptr, *info_ptr;
7170 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7171 int i,num_extra_attrs;
7172 struct dwarf2_section_info *dwo_abbrev_section;
7173 struct attribute *attr;
7174 struct die_info *comp_unit_die;
7175
7176 /* At most one of these may be provided. */
7177 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7178
7179 /* These attributes aren't processed until later:
7180 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7181 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7182 referenced later. However, these attributes are found in the stub
7183 which we won't have later. In order to not impose this complication
7184 on the rest of the code, we read them here and copy them to the
7185 DWO CU/TU die. */
7186
7187 stmt_list = NULL;
7188 low_pc = NULL;
7189 high_pc = NULL;
7190 ranges = NULL;
7191 comp_dir = NULL;
7192
7193 if (stub_comp_unit_die != NULL)
7194 {
7195 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7196 DWO file. */
7197 if (! this_cu->is_debug_types)
7198 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7199 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7200 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7201 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7202 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7203
7204 /* There should be a DW_AT_addr_base attribute here (if needed).
7205 We need the value before we can process DW_FORM_GNU_addr_index. */
7206 cu->addr_base = 0;
7207 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7208 if (attr)
7209 cu->addr_base = DW_UNSND (attr);
7210
7211 /* There should be a DW_AT_ranges_base attribute here (if needed).
7212 We need the value before we can process DW_AT_ranges. */
7213 cu->ranges_base = 0;
7214 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7215 if (attr)
7216 cu->ranges_base = DW_UNSND (attr);
7217 }
7218 else if (stub_comp_dir != NULL)
7219 {
7220 /* Reconstruct the comp_dir attribute to simplify the code below. */
7221 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7222 comp_dir->name = DW_AT_comp_dir;
7223 comp_dir->form = DW_FORM_string;
7224 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7225 DW_STRING (comp_dir) = stub_comp_dir;
7226 }
7227
7228 /* Set up for reading the DWO CU/TU. */
7229 cu->dwo_unit = dwo_unit;
7230 dwarf2_section_info *section = dwo_unit->section;
7231 dwarf2_read_section (objfile, section);
7232 abfd = get_section_bfd_owner (section);
7233 begin_info_ptr = info_ptr = (section->buffer
7234 + to_underlying (dwo_unit->sect_off));
7235 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7236
7237 if (this_cu->is_debug_types)
7238 {
7239 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7240
7241 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7242 &cu->header, section,
7243 dwo_abbrev_section,
7244 info_ptr, rcuh_kind::TYPE);
7245 /* This is not an assert because it can be caused by bad debug info. */
7246 if (sig_type->signature != cu->header.signature)
7247 {
7248 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7249 " TU at offset %s [in module %s]"),
7250 hex_string (sig_type->signature),
7251 hex_string (cu->header.signature),
7252 sect_offset_str (dwo_unit->sect_off),
7253 bfd_get_filename (abfd));
7254 }
7255 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7256 /* For DWOs coming from DWP files, we don't know the CU length
7257 nor the type's offset in the TU until now. */
7258 dwo_unit->length = get_cu_length (&cu->header);
7259 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7260
7261 /* Establish the type offset that can be used to lookup the type.
7262 For DWO files, we don't know it until now. */
7263 sig_type->type_offset_in_section
7264 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7265 }
7266 else
7267 {
7268 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7269 &cu->header, section,
7270 dwo_abbrev_section,
7271 info_ptr, rcuh_kind::COMPILE);
7272 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7273 /* For DWOs coming from DWP files, we don't know the CU length
7274 until now. */
7275 dwo_unit->length = get_cu_length (&cu->header);
7276 }
7277
7278 *result_dwo_abbrev_table
7279 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7280 cu->header.abbrev_sect_off);
7281 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7282 result_dwo_abbrev_table->get ());
7283
7284 /* Read in the die, but leave space to copy over the attributes
7285 from the stub. This has the benefit of simplifying the rest of
7286 the code - all the work to maintain the illusion of a single
7287 DW_TAG_{compile,type}_unit DIE is done here. */
7288 num_extra_attrs = ((stmt_list != NULL)
7289 + (low_pc != NULL)
7290 + (high_pc != NULL)
7291 + (ranges != NULL)
7292 + (comp_dir != NULL));
7293 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7294 result_has_children, num_extra_attrs);
7295
7296 /* Copy over the attributes from the stub to the DIE we just read in. */
7297 comp_unit_die = *result_comp_unit_die;
7298 i = comp_unit_die->num_attrs;
7299 if (stmt_list != NULL)
7300 comp_unit_die->attrs[i++] = *stmt_list;
7301 if (low_pc != NULL)
7302 comp_unit_die->attrs[i++] = *low_pc;
7303 if (high_pc != NULL)
7304 comp_unit_die->attrs[i++] = *high_pc;
7305 if (ranges != NULL)
7306 comp_unit_die->attrs[i++] = *ranges;
7307 if (comp_dir != NULL)
7308 comp_unit_die->attrs[i++] = *comp_dir;
7309 comp_unit_die->num_attrs += num_extra_attrs;
7310
7311 if (dwarf_die_debug)
7312 {
7313 fprintf_unfiltered (gdb_stdlog,
7314 "Read die from %s@0x%x of %s:\n",
7315 get_section_name (section),
7316 (unsigned) (begin_info_ptr - section->buffer),
7317 bfd_get_filename (abfd));
7318 dump_die (comp_unit_die, dwarf_die_debug);
7319 }
7320
7321 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7322 TUs by skipping the stub and going directly to the entry in the DWO file.
7323 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7324 to get it via circuitous means. Blech. */
7325 if (comp_dir != NULL)
7326 result_reader->comp_dir = DW_STRING (comp_dir);
7327
7328 /* Skip dummy compilation units. */
7329 if (info_ptr >= begin_info_ptr + dwo_unit->length
7330 || peek_abbrev_code (abfd, info_ptr) == 0)
7331 return 0;
7332
7333 *result_info_ptr = info_ptr;
7334 return 1;
7335 }
7336
7337 /* Subroutine of init_cutu_and_read_dies to simplify it.
7338 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7339 Returns NULL if the specified DWO unit cannot be found. */
7340
7341 static struct dwo_unit *
7342 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7343 struct die_info *comp_unit_die)
7344 {
7345 struct dwarf2_cu *cu = this_cu->cu;
7346 ULONGEST signature;
7347 struct dwo_unit *dwo_unit;
7348 const char *comp_dir, *dwo_name;
7349
7350 gdb_assert (cu != NULL);
7351
7352 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7353 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7354 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7355
7356 if (this_cu->is_debug_types)
7357 {
7358 struct signatured_type *sig_type;
7359
7360 /* Since this_cu is the first member of struct signatured_type,
7361 we can go from a pointer to one to a pointer to the other. */
7362 sig_type = (struct signatured_type *) this_cu;
7363 signature = sig_type->signature;
7364 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7365 }
7366 else
7367 {
7368 struct attribute *attr;
7369
7370 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7371 if (! attr)
7372 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7373 " [in module %s]"),
7374 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7375 signature = DW_UNSND (attr);
7376 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7377 signature);
7378 }
7379
7380 return dwo_unit;
7381 }
7382
7383 /* Subroutine of init_cutu_and_read_dies to simplify it.
7384 See it for a description of the parameters.
7385 Read a TU directly from a DWO file, bypassing the stub. */
7386
7387 static void
7388 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7389 int use_existing_cu, int keep,
7390 die_reader_func_ftype *die_reader_func,
7391 void *data)
7392 {
7393 std::unique_ptr<dwarf2_cu> new_cu;
7394 struct signatured_type *sig_type;
7395 struct die_reader_specs reader;
7396 const gdb_byte *info_ptr;
7397 struct die_info *comp_unit_die;
7398 int has_children;
7399 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7400
7401 /* Verify we can do the following downcast, and that we have the
7402 data we need. */
7403 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7404 sig_type = (struct signatured_type *) this_cu;
7405 gdb_assert (sig_type->dwo_unit != NULL);
7406
7407 if (use_existing_cu && this_cu->cu != NULL)
7408 {
7409 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7410 /* There's no need to do the rereading_dwo_cu handling that
7411 init_cutu_and_read_dies does since we don't read the stub. */
7412 }
7413 else
7414 {
7415 /* If !use_existing_cu, this_cu->cu must be NULL. */
7416 gdb_assert (this_cu->cu == NULL);
7417 new_cu.reset (new dwarf2_cu (this_cu));
7418 }
7419
7420 /* A future optimization, if needed, would be to use an existing
7421 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7422 could share abbrev tables. */
7423
7424 /* The abbreviation table used by READER, this must live at least as long as
7425 READER. */
7426 abbrev_table_up dwo_abbrev_table;
7427
7428 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7429 NULL /* stub_comp_unit_die */,
7430 sig_type->dwo_unit->dwo_file->comp_dir,
7431 &reader, &info_ptr,
7432 &comp_unit_die, &has_children,
7433 &dwo_abbrev_table) == 0)
7434 {
7435 /* Dummy die. */
7436 return;
7437 }
7438
7439 /* All the "real" work is done here. */
7440 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7441
7442 /* This duplicates the code in init_cutu_and_read_dies,
7443 but the alternative is making the latter more complex.
7444 This function is only for the special case of using DWO files directly:
7445 no point in overly complicating the general case just to handle this. */
7446 if (new_cu != NULL && keep)
7447 {
7448 /* Link this CU into read_in_chain. */
7449 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7450 dwarf2_per_objfile->read_in_chain = this_cu;
7451 /* The chain owns it now. */
7452 new_cu.release ();
7453 }
7454 }
7455
7456 /* Initialize a CU (or TU) and read its DIEs.
7457 If the CU defers to a DWO file, read the DWO file as well.
7458
7459 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7460 Otherwise the table specified in the comp unit header is read in and used.
7461 This is an optimization for when we already have the abbrev table.
7462
7463 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7464 Otherwise, a new CU is allocated with xmalloc.
7465
7466 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7467 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7468
7469 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7470 linker) then DIE_READER_FUNC will not get called. */
7471
7472 static void
7473 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7474 struct abbrev_table *abbrev_table,
7475 int use_existing_cu, int keep,
7476 die_reader_func_ftype *die_reader_func,
7477 void *data)
7478 {
7479 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7480 struct objfile *objfile = dwarf2_per_objfile->objfile;
7481 struct dwarf2_section_info *section = this_cu->section;
7482 bfd *abfd = get_section_bfd_owner (section);
7483 struct dwarf2_cu *cu;
7484 const gdb_byte *begin_info_ptr, *info_ptr;
7485 struct die_reader_specs reader;
7486 struct die_info *comp_unit_die;
7487 int has_children;
7488 struct attribute *attr;
7489 struct signatured_type *sig_type = NULL;
7490 struct dwarf2_section_info *abbrev_section;
7491 /* Non-zero if CU currently points to a DWO file and we need to
7492 reread it. When this happens we need to reread the skeleton die
7493 before we can reread the DWO file (this only applies to CUs, not TUs). */
7494 int rereading_dwo_cu = 0;
7495
7496 if (dwarf_die_debug)
7497 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7498 this_cu->is_debug_types ? "type" : "comp",
7499 sect_offset_str (this_cu->sect_off));
7500
7501 if (use_existing_cu)
7502 gdb_assert (keep);
7503
7504 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7505 file (instead of going through the stub), short-circuit all of this. */
7506 if (this_cu->reading_dwo_directly)
7507 {
7508 /* Narrow down the scope of possibilities to have to understand. */
7509 gdb_assert (this_cu->is_debug_types);
7510 gdb_assert (abbrev_table == NULL);
7511 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7512 die_reader_func, data);
7513 return;
7514 }
7515
7516 /* This is cheap if the section is already read in. */
7517 dwarf2_read_section (objfile, section);
7518
7519 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7520
7521 abbrev_section = get_abbrev_section_for_cu (this_cu);
7522
7523 std::unique_ptr<dwarf2_cu> new_cu;
7524 if (use_existing_cu && this_cu->cu != NULL)
7525 {
7526 cu = this_cu->cu;
7527 /* If this CU is from a DWO file we need to start over, we need to
7528 refetch the attributes from the skeleton CU.
7529 This could be optimized by retrieving those attributes from when we
7530 were here the first time: the previous comp_unit_die was stored in
7531 comp_unit_obstack. But there's no data yet that we need this
7532 optimization. */
7533 if (cu->dwo_unit != NULL)
7534 rereading_dwo_cu = 1;
7535 }
7536 else
7537 {
7538 /* If !use_existing_cu, this_cu->cu must be NULL. */
7539 gdb_assert (this_cu->cu == NULL);
7540 new_cu.reset (new dwarf2_cu (this_cu));
7541 cu = new_cu.get ();
7542 }
7543
7544 /* Get the header. */
7545 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7546 {
7547 /* We already have the header, there's no need to read it in again. */
7548 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7549 }
7550 else
7551 {
7552 if (this_cu->is_debug_types)
7553 {
7554 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7555 &cu->header, section,
7556 abbrev_section, info_ptr,
7557 rcuh_kind::TYPE);
7558
7559 /* Since per_cu is the first member of struct signatured_type,
7560 we can go from a pointer to one to a pointer to the other. */
7561 sig_type = (struct signatured_type *) this_cu;
7562 gdb_assert (sig_type->signature == cu->header.signature);
7563 gdb_assert (sig_type->type_offset_in_tu
7564 == cu->header.type_cu_offset_in_tu);
7565 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7566
7567 /* LENGTH has not been set yet for type units if we're
7568 using .gdb_index. */
7569 this_cu->length = get_cu_length (&cu->header);
7570
7571 /* Establish the type offset that can be used to lookup the type. */
7572 sig_type->type_offset_in_section =
7573 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7574
7575 this_cu->dwarf_version = cu->header.version;
7576 }
7577 else
7578 {
7579 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7580 &cu->header, section,
7581 abbrev_section,
7582 info_ptr,
7583 rcuh_kind::COMPILE);
7584
7585 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7586 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7587 this_cu->dwarf_version = cu->header.version;
7588 }
7589 }
7590
7591 /* Skip dummy compilation units. */
7592 if (info_ptr >= begin_info_ptr + this_cu->length
7593 || peek_abbrev_code (abfd, info_ptr) == 0)
7594 return;
7595
7596 /* If we don't have them yet, read the abbrevs for this compilation unit.
7597 And if we need to read them now, make sure they're freed when we're
7598 done (own the table through ABBREV_TABLE_HOLDER). */
7599 abbrev_table_up abbrev_table_holder;
7600 if (abbrev_table != NULL)
7601 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7602 else
7603 {
7604 abbrev_table_holder
7605 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7606 cu->header.abbrev_sect_off);
7607 abbrev_table = abbrev_table_holder.get ();
7608 }
7609
7610 /* Read the top level CU/TU die. */
7611 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7612 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7613
7614 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7615 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7616 table from the DWO file and pass the ownership over to us. It will be
7617 referenced from READER, so we must make sure to free it after we're done
7618 with READER.
7619
7620 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7621 DWO CU, that this test will fail (the attribute will not be present). */
7622 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7623 abbrev_table_up dwo_abbrev_table;
7624 if (attr)
7625 {
7626 struct dwo_unit *dwo_unit;
7627 struct die_info *dwo_comp_unit_die;
7628
7629 if (has_children)
7630 {
7631 complaint (&symfile_complaints,
7632 _("compilation unit with DW_AT_GNU_dwo_name"
7633 " has children (offset %s) [in module %s]"),
7634 sect_offset_str (this_cu->sect_off),
7635 bfd_get_filename (abfd));
7636 }
7637 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7638 if (dwo_unit != NULL)
7639 {
7640 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7641 comp_unit_die, NULL,
7642 &reader, &info_ptr,
7643 &dwo_comp_unit_die, &has_children,
7644 &dwo_abbrev_table) == 0)
7645 {
7646 /* Dummy die. */
7647 return;
7648 }
7649 comp_unit_die = dwo_comp_unit_die;
7650 }
7651 else
7652 {
7653 /* Yikes, we couldn't find the rest of the DIE, we only have
7654 the stub. A complaint has already been logged. There's
7655 not much more we can do except pass on the stub DIE to
7656 die_reader_func. We don't want to throw an error on bad
7657 debug info. */
7658 }
7659 }
7660
7661 /* All of the above is setup for this call. Yikes. */
7662 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7663
7664 /* Done, clean up. */
7665 if (new_cu != NULL && keep)
7666 {
7667 /* Link this CU into read_in_chain. */
7668 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7669 dwarf2_per_objfile->read_in_chain = this_cu;
7670 /* The chain owns it now. */
7671 new_cu.release ();
7672 }
7673 }
7674
7675 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7676 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7677 to have already done the lookup to find the DWO file).
7678
7679 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7680 THIS_CU->is_debug_types, but nothing else.
7681
7682 We fill in THIS_CU->length.
7683
7684 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7685 linker) then DIE_READER_FUNC will not get called.
7686
7687 THIS_CU->cu is always freed when done.
7688 This is done in order to not leave THIS_CU->cu in a state where we have
7689 to care whether it refers to the "main" CU or the DWO CU. */
7690
7691 static void
7692 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7693 struct dwo_file *dwo_file,
7694 die_reader_func_ftype *die_reader_func,
7695 void *data)
7696 {
7697 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7698 struct objfile *objfile = dwarf2_per_objfile->objfile;
7699 struct dwarf2_section_info *section = this_cu->section;
7700 bfd *abfd = get_section_bfd_owner (section);
7701 struct dwarf2_section_info *abbrev_section;
7702 const gdb_byte *begin_info_ptr, *info_ptr;
7703 struct die_reader_specs reader;
7704 struct die_info *comp_unit_die;
7705 int has_children;
7706
7707 if (dwarf_die_debug)
7708 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7709 this_cu->is_debug_types ? "type" : "comp",
7710 sect_offset_str (this_cu->sect_off));
7711
7712 gdb_assert (this_cu->cu == NULL);
7713
7714 abbrev_section = (dwo_file != NULL
7715 ? &dwo_file->sections.abbrev
7716 : get_abbrev_section_for_cu (this_cu));
7717
7718 /* This is cheap if the section is already read in. */
7719 dwarf2_read_section (objfile, section);
7720
7721 struct dwarf2_cu cu (this_cu);
7722
7723 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7724 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7725 &cu.header, section,
7726 abbrev_section, info_ptr,
7727 (this_cu->is_debug_types
7728 ? rcuh_kind::TYPE
7729 : rcuh_kind::COMPILE));
7730
7731 this_cu->length = get_cu_length (&cu.header);
7732
7733 /* Skip dummy compilation units. */
7734 if (info_ptr >= begin_info_ptr + this_cu->length
7735 || peek_abbrev_code (abfd, info_ptr) == 0)
7736 return;
7737
7738 abbrev_table_up abbrev_table
7739 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7740 cu.header.abbrev_sect_off);
7741
7742 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7743 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7744
7745 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7746 }
7747
7748 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7749 does not lookup the specified DWO file.
7750 This cannot be used to read DWO files.
7751
7752 THIS_CU->cu is always freed when done.
7753 This is done in order to not leave THIS_CU->cu in a state where we have
7754 to care whether it refers to the "main" CU or the DWO CU.
7755 We can revisit this if the data shows there's a performance issue. */
7756
7757 static void
7758 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7759 die_reader_func_ftype *die_reader_func,
7760 void *data)
7761 {
7762 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7763 }
7764 \f
7765 /* Type Unit Groups.
7766
7767 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7768 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7769 so that all types coming from the same compilation (.o file) are grouped
7770 together. A future step could be to put the types in the same symtab as
7771 the CU the types ultimately came from. */
7772
7773 static hashval_t
7774 hash_type_unit_group (const void *item)
7775 {
7776 const struct type_unit_group *tu_group
7777 = (const struct type_unit_group *) item;
7778
7779 return hash_stmt_list_entry (&tu_group->hash);
7780 }
7781
7782 static int
7783 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7784 {
7785 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7786 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7787
7788 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7789 }
7790
7791 /* Allocate a hash table for type unit groups. */
7792
7793 static htab_t
7794 allocate_type_unit_groups_table (struct objfile *objfile)
7795 {
7796 return htab_create_alloc_ex (3,
7797 hash_type_unit_group,
7798 eq_type_unit_group,
7799 NULL,
7800 &objfile->objfile_obstack,
7801 hashtab_obstack_allocate,
7802 dummy_obstack_deallocate);
7803 }
7804
7805 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7806 partial symtabs. We combine several TUs per psymtab to not let the size
7807 of any one psymtab grow too big. */
7808 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7809 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7810
7811 /* Helper routine for get_type_unit_group.
7812 Create the type_unit_group object used to hold one or more TUs. */
7813
7814 static struct type_unit_group *
7815 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7816 {
7817 struct dwarf2_per_objfile *dwarf2_per_objfile
7818 = cu->per_cu->dwarf2_per_objfile;
7819 struct objfile *objfile = dwarf2_per_objfile->objfile;
7820 struct dwarf2_per_cu_data *per_cu;
7821 struct type_unit_group *tu_group;
7822
7823 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7824 struct type_unit_group);
7825 per_cu = &tu_group->per_cu;
7826 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7827
7828 if (dwarf2_per_objfile->using_index)
7829 {
7830 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7831 struct dwarf2_per_cu_quick_data);
7832 }
7833 else
7834 {
7835 unsigned int line_offset = to_underlying (line_offset_struct);
7836 struct partial_symtab *pst;
7837 char *name;
7838
7839 /* Give the symtab a useful name for debug purposes. */
7840 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7841 name = xstrprintf ("<type_units_%d>",
7842 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7843 else
7844 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7845
7846 pst = create_partial_symtab (per_cu, name);
7847 pst->anonymous = 1;
7848
7849 xfree (name);
7850 }
7851
7852 tu_group->hash.dwo_unit = cu->dwo_unit;
7853 tu_group->hash.line_sect_off = line_offset_struct;
7854
7855 return tu_group;
7856 }
7857
7858 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7859 STMT_LIST is a DW_AT_stmt_list attribute. */
7860
7861 static struct type_unit_group *
7862 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7863 {
7864 struct dwarf2_per_objfile *dwarf2_per_objfile
7865 = cu->per_cu->dwarf2_per_objfile;
7866 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7867 struct type_unit_group *tu_group;
7868 void **slot;
7869 unsigned int line_offset;
7870 struct type_unit_group type_unit_group_for_lookup;
7871
7872 if (dwarf2_per_objfile->type_unit_groups == NULL)
7873 {
7874 dwarf2_per_objfile->type_unit_groups =
7875 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7876 }
7877
7878 /* Do we need to create a new group, or can we use an existing one? */
7879
7880 if (stmt_list)
7881 {
7882 line_offset = DW_UNSND (stmt_list);
7883 ++tu_stats->nr_symtab_sharers;
7884 }
7885 else
7886 {
7887 /* Ugh, no stmt_list. Rare, but we have to handle it.
7888 We can do various things here like create one group per TU or
7889 spread them over multiple groups to split up the expansion work.
7890 To avoid worst case scenarios (too many groups or too large groups)
7891 we, umm, group them in bunches. */
7892 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7893 | (tu_stats->nr_stmt_less_type_units
7894 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7895 ++tu_stats->nr_stmt_less_type_units;
7896 }
7897
7898 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7899 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7900 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7901 &type_unit_group_for_lookup, INSERT);
7902 if (*slot != NULL)
7903 {
7904 tu_group = (struct type_unit_group *) *slot;
7905 gdb_assert (tu_group != NULL);
7906 }
7907 else
7908 {
7909 sect_offset line_offset_struct = (sect_offset) line_offset;
7910 tu_group = create_type_unit_group (cu, line_offset_struct);
7911 *slot = tu_group;
7912 ++tu_stats->nr_symtabs;
7913 }
7914
7915 return tu_group;
7916 }
7917 \f
7918 /* Partial symbol tables. */
7919
7920 /* Create a psymtab named NAME and assign it to PER_CU.
7921
7922 The caller must fill in the following details:
7923 dirname, textlow, texthigh. */
7924
7925 static struct partial_symtab *
7926 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7927 {
7928 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7929 struct partial_symtab *pst;
7930
7931 pst = start_psymtab_common (objfile, name, 0,
7932 objfile->global_psymbols,
7933 objfile->static_psymbols);
7934
7935 pst->psymtabs_addrmap_supported = 1;
7936
7937 /* This is the glue that links PST into GDB's symbol API. */
7938 pst->read_symtab_private = per_cu;
7939 pst->read_symtab = dwarf2_read_symtab;
7940 per_cu->v.psymtab = pst;
7941
7942 return pst;
7943 }
7944
7945 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7946 type. */
7947
7948 struct process_psymtab_comp_unit_data
7949 {
7950 /* True if we are reading a DW_TAG_partial_unit. */
7951
7952 int want_partial_unit;
7953
7954 /* The "pretend" language that is used if the CU doesn't declare a
7955 language. */
7956
7957 enum language pretend_language;
7958 };
7959
7960 /* die_reader_func for process_psymtab_comp_unit. */
7961
7962 static void
7963 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7964 const gdb_byte *info_ptr,
7965 struct die_info *comp_unit_die,
7966 int has_children,
7967 void *data)
7968 {
7969 struct dwarf2_cu *cu = reader->cu;
7970 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7971 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7972 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7973 CORE_ADDR baseaddr;
7974 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7975 struct partial_symtab *pst;
7976 enum pc_bounds_kind cu_bounds_kind;
7977 const char *filename;
7978 struct process_psymtab_comp_unit_data *info
7979 = (struct process_psymtab_comp_unit_data *) data;
7980
7981 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7982 return;
7983
7984 gdb_assert (! per_cu->is_debug_types);
7985
7986 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7987
7988 cu->list_in_scope = &file_symbols;
7989
7990 /* Allocate a new partial symbol table structure. */
7991 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7992 if (filename == NULL)
7993 filename = "";
7994
7995 pst = create_partial_symtab (per_cu, filename);
7996
7997 /* This must be done before calling dwarf2_build_include_psymtabs. */
7998 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7999
8000 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8001
8002 dwarf2_find_base_address (comp_unit_die, cu);
8003
8004 /* Possibly set the default values of LOWPC and HIGHPC from
8005 `DW_AT_ranges'. */
8006 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8007 &best_highpc, cu, pst);
8008 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8009 /* Store the contiguous range if it is not empty; it can be empty for
8010 CUs with no code. */
8011 addrmap_set_empty (objfile->psymtabs_addrmap,
8012 gdbarch_adjust_dwarf2_addr (gdbarch,
8013 best_lowpc + baseaddr),
8014 gdbarch_adjust_dwarf2_addr (gdbarch,
8015 best_highpc + baseaddr) - 1,
8016 pst);
8017
8018 /* Check if comp unit has_children.
8019 If so, read the rest of the partial symbols from this comp unit.
8020 If not, there's no more debug_info for this comp unit. */
8021 if (has_children)
8022 {
8023 struct partial_die_info *first_die;
8024 CORE_ADDR lowpc, highpc;
8025
8026 lowpc = ((CORE_ADDR) -1);
8027 highpc = ((CORE_ADDR) 0);
8028
8029 first_die = load_partial_dies (reader, info_ptr, 1);
8030
8031 scan_partial_symbols (first_die, &lowpc, &highpc,
8032 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8033
8034 /* If we didn't find a lowpc, set it to highpc to avoid
8035 complaints from `maint check'. */
8036 if (lowpc == ((CORE_ADDR) -1))
8037 lowpc = highpc;
8038
8039 /* If the compilation unit didn't have an explicit address range,
8040 then use the information extracted from its child dies. */
8041 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8042 {
8043 best_lowpc = lowpc;
8044 best_highpc = highpc;
8045 }
8046 }
8047 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8048 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8049
8050 end_psymtab_common (objfile, pst);
8051
8052 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8053 {
8054 int i;
8055 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8056 struct dwarf2_per_cu_data *iter;
8057
8058 /* Fill in 'dependencies' here; we fill in 'users' in a
8059 post-pass. */
8060 pst->number_of_dependencies = len;
8061 pst->dependencies =
8062 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8063 for (i = 0;
8064 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8065 i, iter);
8066 ++i)
8067 pst->dependencies[i] = iter->v.psymtab;
8068
8069 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8070 }
8071
8072 /* Get the list of files included in the current compilation unit,
8073 and build a psymtab for each of them. */
8074 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8075
8076 if (dwarf_read_debug)
8077 {
8078 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8079
8080 fprintf_unfiltered (gdb_stdlog,
8081 "Psymtab for %s unit @%s: %s - %s"
8082 ", %d global, %d static syms\n",
8083 per_cu->is_debug_types ? "type" : "comp",
8084 sect_offset_str (per_cu->sect_off),
8085 paddress (gdbarch, pst->textlow),
8086 paddress (gdbarch, pst->texthigh),
8087 pst->n_global_syms, pst->n_static_syms);
8088 }
8089 }
8090
8091 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8092 Process compilation unit THIS_CU for a psymtab. */
8093
8094 static void
8095 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8096 int want_partial_unit,
8097 enum language pretend_language)
8098 {
8099 /* If this compilation unit was already read in, free the
8100 cached copy in order to read it in again. This is
8101 necessary because we skipped some symbols when we first
8102 read in the compilation unit (see load_partial_dies).
8103 This problem could be avoided, but the benefit is unclear. */
8104 if (this_cu->cu != NULL)
8105 free_one_cached_comp_unit (this_cu);
8106
8107 if (this_cu->is_debug_types)
8108 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8109 NULL);
8110 else
8111 {
8112 process_psymtab_comp_unit_data info;
8113 info.want_partial_unit = want_partial_unit;
8114 info.pretend_language = pretend_language;
8115 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8116 process_psymtab_comp_unit_reader, &info);
8117 }
8118
8119 /* Age out any secondary CUs. */
8120 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8121 }
8122
8123 /* Reader function for build_type_psymtabs. */
8124
8125 static void
8126 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8127 const gdb_byte *info_ptr,
8128 struct die_info *type_unit_die,
8129 int has_children,
8130 void *data)
8131 {
8132 struct dwarf2_per_objfile *dwarf2_per_objfile
8133 = reader->cu->per_cu->dwarf2_per_objfile;
8134 struct objfile *objfile = dwarf2_per_objfile->objfile;
8135 struct dwarf2_cu *cu = reader->cu;
8136 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8137 struct signatured_type *sig_type;
8138 struct type_unit_group *tu_group;
8139 struct attribute *attr;
8140 struct partial_die_info *first_die;
8141 CORE_ADDR lowpc, highpc;
8142 struct partial_symtab *pst;
8143
8144 gdb_assert (data == NULL);
8145 gdb_assert (per_cu->is_debug_types);
8146 sig_type = (struct signatured_type *) per_cu;
8147
8148 if (! has_children)
8149 return;
8150
8151 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8152 tu_group = get_type_unit_group (cu, attr);
8153
8154 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8155
8156 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8157 cu->list_in_scope = &file_symbols;
8158 pst = create_partial_symtab (per_cu, "");
8159 pst->anonymous = 1;
8160
8161 first_die = load_partial_dies (reader, info_ptr, 1);
8162
8163 lowpc = (CORE_ADDR) -1;
8164 highpc = (CORE_ADDR) 0;
8165 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8166
8167 end_psymtab_common (objfile, pst);
8168 }
8169
8170 /* Struct used to sort TUs by their abbreviation table offset. */
8171
8172 struct tu_abbrev_offset
8173 {
8174 struct signatured_type *sig_type;
8175 sect_offset abbrev_offset;
8176 };
8177
8178 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8179
8180 static bool
8181 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8182 const struct tu_abbrev_offset &b)
8183 {
8184 return a.abbrev_offset < b.abbrev_offset;
8185 }
8186
8187 /* Efficiently read all the type units.
8188 This does the bulk of the work for build_type_psymtabs.
8189
8190 The efficiency is because we sort TUs by the abbrev table they use and
8191 only read each abbrev table once. In one program there are 200K TUs
8192 sharing 8K abbrev tables.
8193
8194 The main purpose of this function is to support building the
8195 dwarf2_per_objfile->type_unit_groups table.
8196 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8197 can collapse the search space by grouping them by stmt_list.
8198 The savings can be significant, in the same program from above the 200K TUs
8199 share 8K stmt_list tables.
8200
8201 FUNC is expected to call get_type_unit_group, which will create the
8202 struct type_unit_group if necessary and add it to
8203 dwarf2_per_objfile->type_unit_groups. */
8204
8205 static void
8206 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8207 {
8208 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8209 abbrev_table_up abbrev_table;
8210 sect_offset abbrev_offset;
8211 int i;
8212
8213 /* It's up to the caller to not call us multiple times. */
8214 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8215
8216 if (dwarf2_per_objfile->n_type_units == 0)
8217 return;
8218
8219 /* TUs typically share abbrev tables, and there can be way more TUs than
8220 abbrev tables. Sort by abbrev table to reduce the number of times we
8221 read each abbrev table in.
8222 Alternatives are to punt or to maintain a cache of abbrev tables.
8223 This is simpler and efficient enough for now.
8224
8225 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8226 symtab to use). Typically TUs with the same abbrev offset have the same
8227 stmt_list value too so in practice this should work well.
8228
8229 The basic algorithm here is:
8230
8231 sort TUs by abbrev table
8232 for each TU with same abbrev table:
8233 read abbrev table if first user
8234 read TU top level DIE
8235 [IWBN if DWO skeletons had DW_AT_stmt_list]
8236 call FUNC */
8237
8238 if (dwarf_read_debug)
8239 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8240
8241 /* Sort in a separate table to maintain the order of all_type_units
8242 for .gdb_index: TU indices directly index all_type_units. */
8243 std::vector<struct tu_abbrev_offset> sorted_by_abbrev
8244 (dwarf2_per_objfile->n_type_units);
8245 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8246 {
8247 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8248
8249 sorted_by_abbrev[i].sig_type = sig_type;
8250 sorted_by_abbrev[i].abbrev_offset =
8251 read_abbrev_offset (dwarf2_per_objfile,
8252 sig_type->per_cu.section,
8253 sig_type->per_cu.sect_off);
8254 }
8255 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8256 sort_tu_by_abbrev_offset);
8257
8258 abbrev_offset = (sect_offset) ~(unsigned) 0;
8259
8260 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8261 {
8262 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8263
8264 /* Switch to the next abbrev table if necessary. */
8265 if (abbrev_table == NULL
8266 || tu->abbrev_offset != abbrev_offset)
8267 {
8268 abbrev_offset = tu->abbrev_offset;
8269 abbrev_table =
8270 abbrev_table_read_table (dwarf2_per_objfile,
8271 &dwarf2_per_objfile->abbrev,
8272 abbrev_offset);
8273 ++tu_stats->nr_uniq_abbrev_tables;
8274 }
8275
8276 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8277 0, 0, build_type_psymtabs_reader, NULL);
8278 }
8279 }
8280
8281 /* Print collected type unit statistics. */
8282
8283 static void
8284 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8285 {
8286 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8287
8288 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8289 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8290 dwarf2_per_objfile->n_type_units);
8291 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8292 tu_stats->nr_uniq_abbrev_tables);
8293 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8294 tu_stats->nr_symtabs);
8295 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8296 tu_stats->nr_symtab_sharers);
8297 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8298 tu_stats->nr_stmt_less_type_units);
8299 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8300 tu_stats->nr_all_type_units_reallocs);
8301 }
8302
8303 /* Traversal function for build_type_psymtabs. */
8304
8305 static int
8306 build_type_psymtab_dependencies (void **slot, void *info)
8307 {
8308 struct dwarf2_per_objfile *dwarf2_per_objfile
8309 = (struct dwarf2_per_objfile *) info;
8310 struct objfile *objfile = dwarf2_per_objfile->objfile;
8311 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8312 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8313 struct partial_symtab *pst = per_cu->v.psymtab;
8314 int len = VEC_length (sig_type_ptr, tu_group->tus);
8315 struct signatured_type *iter;
8316 int i;
8317
8318 gdb_assert (len > 0);
8319 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8320
8321 pst->number_of_dependencies = len;
8322 pst->dependencies =
8323 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8324 for (i = 0;
8325 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8326 ++i)
8327 {
8328 gdb_assert (iter->per_cu.is_debug_types);
8329 pst->dependencies[i] = iter->per_cu.v.psymtab;
8330 iter->type_unit_group = tu_group;
8331 }
8332
8333 VEC_free (sig_type_ptr, tu_group->tus);
8334
8335 return 1;
8336 }
8337
8338 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8339 Build partial symbol tables for the .debug_types comp-units. */
8340
8341 static void
8342 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8343 {
8344 if (! create_all_type_units (dwarf2_per_objfile))
8345 return;
8346
8347 build_type_psymtabs_1 (dwarf2_per_objfile);
8348 }
8349
8350 /* Traversal function for process_skeletonless_type_unit.
8351 Read a TU in a DWO file and build partial symbols for it. */
8352
8353 static int
8354 process_skeletonless_type_unit (void **slot, void *info)
8355 {
8356 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8357 struct dwarf2_per_objfile *dwarf2_per_objfile
8358 = (struct dwarf2_per_objfile *) info;
8359 struct signatured_type find_entry, *entry;
8360
8361 /* If this TU doesn't exist in the global table, add it and read it in. */
8362
8363 if (dwarf2_per_objfile->signatured_types == NULL)
8364 {
8365 dwarf2_per_objfile->signatured_types
8366 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8367 }
8368
8369 find_entry.signature = dwo_unit->signature;
8370 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8371 INSERT);
8372 /* If we've already seen this type there's nothing to do. What's happening
8373 is we're doing our own version of comdat-folding here. */
8374 if (*slot != NULL)
8375 return 1;
8376
8377 /* This does the job that create_all_type_units would have done for
8378 this TU. */
8379 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8380 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8381 *slot = entry;
8382
8383 /* This does the job that build_type_psymtabs_1 would have done. */
8384 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8385 build_type_psymtabs_reader, NULL);
8386
8387 return 1;
8388 }
8389
8390 /* Traversal function for process_skeletonless_type_units. */
8391
8392 static int
8393 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8394 {
8395 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8396
8397 if (dwo_file->tus != NULL)
8398 {
8399 htab_traverse_noresize (dwo_file->tus,
8400 process_skeletonless_type_unit, info);
8401 }
8402
8403 return 1;
8404 }
8405
8406 /* Scan all TUs of DWO files, verifying we've processed them.
8407 This is needed in case a TU was emitted without its skeleton.
8408 Note: This can't be done until we know what all the DWO files are. */
8409
8410 static void
8411 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8412 {
8413 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8414 if (get_dwp_file (dwarf2_per_objfile) == NULL
8415 && dwarf2_per_objfile->dwo_files != NULL)
8416 {
8417 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8418 process_dwo_file_for_skeletonless_type_units,
8419 dwarf2_per_objfile);
8420 }
8421 }
8422
8423 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8424
8425 static void
8426 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8427 {
8428 int i;
8429
8430 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8431 {
8432 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (i);
8433 struct partial_symtab *pst = per_cu->v.psymtab;
8434 int j;
8435
8436 if (pst == NULL)
8437 continue;
8438
8439 for (j = 0; j < pst->number_of_dependencies; ++j)
8440 {
8441 /* Set the 'user' field only if it is not already set. */
8442 if (pst->dependencies[j]->user == NULL)
8443 pst->dependencies[j]->user = pst;
8444 }
8445 }
8446 }
8447
8448 /* Build the partial symbol table by doing a quick pass through the
8449 .debug_info and .debug_abbrev sections. */
8450
8451 static void
8452 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8453 {
8454 int i;
8455 struct objfile *objfile = dwarf2_per_objfile->objfile;
8456
8457 if (dwarf_read_debug)
8458 {
8459 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8460 objfile_name (objfile));
8461 }
8462
8463 dwarf2_per_objfile->reading_partial_symbols = 1;
8464
8465 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8466
8467 /* Any cached compilation units will be linked by the per-objfile
8468 read_in_chain. Make sure to free them when we're done. */
8469 free_cached_comp_units freer (dwarf2_per_objfile);
8470
8471 build_type_psymtabs (dwarf2_per_objfile);
8472
8473 create_all_comp_units (dwarf2_per_objfile);
8474
8475 /* Create a temporary address map on a temporary obstack. We later
8476 copy this to the final obstack. */
8477 auto_obstack temp_obstack;
8478
8479 scoped_restore save_psymtabs_addrmap
8480 = make_scoped_restore (&objfile->psymtabs_addrmap,
8481 addrmap_create_mutable (&temp_obstack));
8482
8483 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8484 {
8485 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (i);
8486
8487 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8488 }
8489
8490 /* This has to wait until we read the CUs, we need the list of DWOs. */
8491 process_skeletonless_type_units (dwarf2_per_objfile);
8492
8493 /* Now that all TUs have been processed we can fill in the dependencies. */
8494 if (dwarf2_per_objfile->type_unit_groups != NULL)
8495 {
8496 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8497 build_type_psymtab_dependencies, dwarf2_per_objfile);
8498 }
8499
8500 if (dwarf_read_debug)
8501 print_tu_stats (dwarf2_per_objfile);
8502
8503 set_partial_user (dwarf2_per_objfile);
8504
8505 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8506 &objfile->objfile_obstack);
8507 /* At this point we want to keep the address map. */
8508 save_psymtabs_addrmap.release ();
8509
8510 if (dwarf_read_debug)
8511 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8512 objfile_name (objfile));
8513 }
8514
8515 /* die_reader_func for load_partial_comp_unit. */
8516
8517 static void
8518 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8519 const gdb_byte *info_ptr,
8520 struct die_info *comp_unit_die,
8521 int has_children,
8522 void *data)
8523 {
8524 struct dwarf2_cu *cu = reader->cu;
8525
8526 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8527
8528 /* Check if comp unit has_children.
8529 If so, read the rest of the partial symbols from this comp unit.
8530 If not, there's no more debug_info for this comp unit. */
8531 if (has_children)
8532 load_partial_dies (reader, info_ptr, 0);
8533 }
8534
8535 /* Load the partial DIEs for a secondary CU into memory.
8536 This is also used when rereading a primary CU with load_all_dies. */
8537
8538 static void
8539 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8540 {
8541 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8542 load_partial_comp_unit_reader, NULL);
8543 }
8544
8545 static void
8546 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8547 struct dwarf2_section_info *section,
8548 struct dwarf2_section_info *abbrev_section,
8549 unsigned int is_dwz,
8550 int *n_allocated,
8551 int *n_comp_units,
8552 struct dwarf2_per_cu_data ***all_comp_units)
8553 {
8554 const gdb_byte *info_ptr;
8555 struct objfile *objfile = dwarf2_per_objfile->objfile;
8556
8557 if (dwarf_read_debug)
8558 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8559 get_section_name (section),
8560 get_section_file_name (section));
8561
8562 dwarf2_read_section (objfile, section);
8563
8564 info_ptr = section->buffer;
8565
8566 while (info_ptr < section->buffer + section->size)
8567 {
8568 struct dwarf2_per_cu_data *this_cu;
8569
8570 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8571
8572 comp_unit_head cu_header;
8573 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8574 abbrev_section, info_ptr,
8575 rcuh_kind::COMPILE);
8576
8577 /* Save the compilation unit for later lookup. */
8578 if (cu_header.unit_type != DW_UT_type)
8579 {
8580 this_cu = XOBNEW (&objfile->objfile_obstack,
8581 struct dwarf2_per_cu_data);
8582 memset (this_cu, 0, sizeof (*this_cu));
8583 }
8584 else
8585 {
8586 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8587 struct signatured_type);
8588 memset (sig_type, 0, sizeof (*sig_type));
8589 sig_type->signature = cu_header.signature;
8590 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8591 this_cu = &sig_type->per_cu;
8592 }
8593 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8594 this_cu->sect_off = sect_off;
8595 this_cu->length = cu_header.length + cu_header.initial_length_size;
8596 this_cu->is_dwz = is_dwz;
8597 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8598 this_cu->section = section;
8599
8600 if (*n_comp_units == *n_allocated)
8601 {
8602 *n_allocated *= 2;
8603 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8604 *all_comp_units, *n_allocated);
8605 }
8606 (*all_comp_units)[*n_comp_units] = this_cu;
8607 ++*n_comp_units;
8608
8609 info_ptr = info_ptr + this_cu->length;
8610 }
8611 }
8612
8613 /* Create a list of all compilation units in OBJFILE.
8614 This is only done for -readnow and building partial symtabs. */
8615
8616 static void
8617 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8618 {
8619 int n_allocated;
8620 int n_comp_units;
8621 struct dwarf2_per_cu_data **all_comp_units;
8622 struct dwz_file *dwz;
8623 struct objfile *objfile = dwarf2_per_objfile->objfile;
8624
8625 n_comp_units = 0;
8626 n_allocated = 10;
8627 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
8628
8629 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8630 &dwarf2_per_objfile->abbrev, 0,
8631 &n_allocated, &n_comp_units, &all_comp_units);
8632
8633 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8634 if (dwz != NULL)
8635 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8636 1, &n_allocated, &n_comp_units,
8637 &all_comp_units);
8638
8639 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8640 struct dwarf2_per_cu_data *,
8641 n_comp_units);
8642 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8643 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8644 xfree (all_comp_units);
8645 dwarf2_per_objfile->n_comp_units = n_comp_units;
8646 }
8647
8648 /* Process all loaded DIEs for compilation unit CU, starting at
8649 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8650 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8651 DW_AT_ranges). See the comments of add_partial_subprogram on how
8652 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8653
8654 static void
8655 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8656 CORE_ADDR *highpc, int set_addrmap,
8657 struct dwarf2_cu *cu)
8658 {
8659 struct partial_die_info *pdi;
8660
8661 /* Now, march along the PDI's, descending into ones which have
8662 interesting children but skipping the children of the other ones,
8663 until we reach the end of the compilation unit. */
8664
8665 pdi = first_die;
8666
8667 while (pdi != NULL)
8668 {
8669 pdi->fixup (cu);
8670
8671 /* Anonymous namespaces or modules have no name but have interesting
8672 children, so we need to look at them. Ditto for anonymous
8673 enums. */
8674
8675 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8676 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8677 || pdi->tag == DW_TAG_imported_unit
8678 || pdi->tag == DW_TAG_inlined_subroutine)
8679 {
8680 switch (pdi->tag)
8681 {
8682 case DW_TAG_subprogram:
8683 case DW_TAG_inlined_subroutine:
8684 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8685 break;
8686 case DW_TAG_constant:
8687 case DW_TAG_variable:
8688 case DW_TAG_typedef:
8689 case DW_TAG_union_type:
8690 if (!pdi->is_declaration)
8691 {
8692 add_partial_symbol (pdi, cu);
8693 }
8694 break;
8695 case DW_TAG_class_type:
8696 case DW_TAG_interface_type:
8697 case DW_TAG_structure_type:
8698 if (!pdi->is_declaration)
8699 {
8700 add_partial_symbol (pdi, cu);
8701 }
8702 if ((cu->language == language_rust
8703 || cu->language == language_cplus) && pdi->has_children)
8704 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8705 set_addrmap, cu);
8706 break;
8707 case DW_TAG_enumeration_type:
8708 if (!pdi->is_declaration)
8709 add_partial_enumeration (pdi, cu);
8710 break;
8711 case DW_TAG_base_type:
8712 case DW_TAG_subrange_type:
8713 /* File scope base type definitions are added to the partial
8714 symbol table. */
8715 add_partial_symbol (pdi, cu);
8716 break;
8717 case DW_TAG_namespace:
8718 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8719 break;
8720 case DW_TAG_module:
8721 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8722 break;
8723 case DW_TAG_imported_unit:
8724 {
8725 struct dwarf2_per_cu_data *per_cu;
8726
8727 /* For now we don't handle imported units in type units. */
8728 if (cu->per_cu->is_debug_types)
8729 {
8730 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8731 " supported in type units [in module %s]"),
8732 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8733 }
8734
8735 per_cu = dwarf2_find_containing_comp_unit
8736 (pdi->d.sect_off, pdi->is_dwz,
8737 cu->per_cu->dwarf2_per_objfile);
8738
8739 /* Go read the partial unit, if needed. */
8740 if (per_cu->v.psymtab == NULL)
8741 process_psymtab_comp_unit (per_cu, 1, cu->language);
8742
8743 VEC_safe_push (dwarf2_per_cu_ptr,
8744 cu->per_cu->imported_symtabs, per_cu);
8745 }
8746 break;
8747 case DW_TAG_imported_declaration:
8748 add_partial_symbol (pdi, cu);
8749 break;
8750 default:
8751 break;
8752 }
8753 }
8754
8755 /* If the die has a sibling, skip to the sibling. */
8756
8757 pdi = pdi->die_sibling;
8758 }
8759 }
8760
8761 /* Functions used to compute the fully scoped name of a partial DIE.
8762
8763 Normally, this is simple. For C++, the parent DIE's fully scoped
8764 name is concatenated with "::" and the partial DIE's name.
8765 Enumerators are an exception; they use the scope of their parent
8766 enumeration type, i.e. the name of the enumeration type is not
8767 prepended to the enumerator.
8768
8769 There are two complexities. One is DW_AT_specification; in this
8770 case "parent" means the parent of the target of the specification,
8771 instead of the direct parent of the DIE. The other is compilers
8772 which do not emit DW_TAG_namespace; in this case we try to guess
8773 the fully qualified name of structure types from their members'
8774 linkage names. This must be done using the DIE's children rather
8775 than the children of any DW_AT_specification target. We only need
8776 to do this for structures at the top level, i.e. if the target of
8777 any DW_AT_specification (if any; otherwise the DIE itself) does not
8778 have a parent. */
8779
8780 /* Compute the scope prefix associated with PDI's parent, in
8781 compilation unit CU. The result will be allocated on CU's
8782 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8783 field. NULL is returned if no prefix is necessary. */
8784 static const char *
8785 partial_die_parent_scope (struct partial_die_info *pdi,
8786 struct dwarf2_cu *cu)
8787 {
8788 const char *grandparent_scope;
8789 struct partial_die_info *parent, *real_pdi;
8790
8791 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8792 then this means the parent of the specification DIE. */
8793
8794 real_pdi = pdi;
8795 while (real_pdi->has_specification)
8796 real_pdi = find_partial_die (real_pdi->spec_offset,
8797 real_pdi->spec_is_dwz, cu);
8798
8799 parent = real_pdi->die_parent;
8800 if (parent == NULL)
8801 return NULL;
8802
8803 if (parent->scope_set)
8804 return parent->scope;
8805
8806 parent->fixup (cu);
8807
8808 grandparent_scope = partial_die_parent_scope (parent, cu);
8809
8810 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8811 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8812 Work around this problem here. */
8813 if (cu->language == language_cplus
8814 && parent->tag == DW_TAG_namespace
8815 && strcmp (parent->name, "::") == 0
8816 && grandparent_scope == NULL)
8817 {
8818 parent->scope = NULL;
8819 parent->scope_set = 1;
8820 return NULL;
8821 }
8822
8823 if (pdi->tag == DW_TAG_enumerator)
8824 /* Enumerators should not get the name of the enumeration as a prefix. */
8825 parent->scope = grandparent_scope;
8826 else if (parent->tag == DW_TAG_namespace
8827 || parent->tag == DW_TAG_module
8828 || parent->tag == DW_TAG_structure_type
8829 || parent->tag == DW_TAG_class_type
8830 || parent->tag == DW_TAG_interface_type
8831 || parent->tag == DW_TAG_union_type
8832 || parent->tag == DW_TAG_enumeration_type)
8833 {
8834 if (grandparent_scope == NULL)
8835 parent->scope = parent->name;
8836 else
8837 parent->scope = typename_concat (&cu->comp_unit_obstack,
8838 grandparent_scope,
8839 parent->name, 0, cu);
8840 }
8841 else
8842 {
8843 /* FIXME drow/2004-04-01: What should we be doing with
8844 function-local names? For partial symbols, we should probably be
8845 ignoring them. */
8846 complaint (&symfile_complaints,
8847 _("unhandled containing DIE tag %d for DIE at %s"),
8848 parent->tag, sect_offset_str (pdi->sect_off));
8849 parent->scope = grandparent_scope;
8850 }
8851
8852 parent->scope_set = 1;
8853 return parent->scope;
8854 }
8855
8856 /* Return the fully scoped name associated with PDI, from compilation unit
8857 CU. The result will be allocated with malloc. */
8858
8859 static char *
8860 partial_die_full_name (struct partial_die_info *pdi,
8861 struct dwarf2_cu *cu)
8862 {
8863 const char *parent_scope;
8864
8865 /* If this is a template instantiation, we can not work out the
8866 template arguments from partial DIEs. So, unfortunately, we have
8867 to go through the full DIEs. At least any work we do building
8868 types here will be reused if full symbols are loaded later. */
8869 if (pdi->has_template_arguments)
8870 {
8871 pdi->fixup (cu);
8872
8873 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8874 {
8875 struct die_info *die;
8876 struct attribute attr;
8877 struct dwarf2_cu *ref_cu = cu;
8878
8879 /* DW_FORM_ref_addr is using section offset. */
8880 attr.name = (enum dwarf_attribute) 0;
8881 attr.form = DW_FORM_ref_addr;
8882 attr.u.unsnd = to_underlying (pdi->sect_off);
8883 die = follow_die_ref (NULL, &attr, &ref_cu);
8884
8885 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8886 }
8887 }
8888
8889 parent_scope = partial_die_parent_scope (pdi, cu);
8890 if (parent_scope == NULL)
8891 return NULL;
8892 else
8893 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8894 }
8895
8896 static void
8897 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8898 {
8899 struct dwarf2_per_objfile *dwarf2_per_objfile
8900 = cu->per_cu->dwarf2_per_objfile;
8901 struct objfile *objfile = dwarf2_per_objfile->objfile;
8902 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8903 CORE_ADDR addr = 0;
8904 const char *actual_name = NULL;
8905 CORE_ADDR baseaddr;
8906 char *built_actual_name;
8907
8908 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8909
8910 built_actual_name = partial_die_full_name (pdi, cu);
8911 if (built_actual_name != NULL)
8912 actual_name = built_actual_name;
8913
8914 if (actual_name == NULL)
8915 actual_name = pdi->name;
8916
8917 switch (pdi->tag)
8918 {
8919 case DW_TAG_inlined_subroutine:
8920 case DW_TAG_subprogram:
8921 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
8922 if (pdi->is_external || cu->language == language_ada)
8923 {
8924 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8925 of the global scope. But in Ada, we want to be able to access
8926 nested procedures globally. So all Ada subprograms are stored
8927 in the global scope. */
8928 add_psymbol_to_list (actual_name, strlen (actual_name),
8929 built_actual_name != NULL,
8930 VAR_DOMAIN, LOC_BLOCK,
8931 &objfile->global_psymbols,
8932 addr, cu->language, objfile);
8933 }
8934 else
8935 {
8936 add_psymbol_to_list (actual_name, strlen (actual_name),
8937 built_actual_name != NULL,
8938 VAR_DOMAIN, LOC_BLOCK,
8939 &objfile->static_psymbols,
8940 addr, cu->language, objfile);
8941 }
8942
8943 if (pdi->main_subprogram && actual_name != NULL)
8944 set_objfile_main_name (objfile, actual_name, cu->language);
8945 break;
8946 case DW_TAG_constant:
8947 {
8948 std::vector<partial_symbol *> *list;
8949
8950 if (pdi->is_external)
8951 list = &objfile->global_psymbols;
8952 else
8953 list = &objfile->static_psymbols;
8954 add_psymbol_to_list (actual_name, strlen (actual_name),
8955 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8956 list, 0, cu->language, objfile);
8957 }
8958 break;
8959 case DW_TAG_variable:
8960 if (pdi->d.locdesc)
8961 addr = decode_locdesc (pdi->d.locdesc, cu);
8962
8963 if (pdi->d.locdesc
8964 && addr == 0
8965 && !dwarf2_per_objfile->has_section_at_zero)
8966 {
8967 /* A global or static variable may also have been stripped
8968 out by the linker if unused, in which case its address
8969 will be nullified; do not add such variables into partial
8970 symbol table then. */
8971 }
8972 else if (pdi->is_external)
8973 {
8974 /* Global Variable.
8975 Don't enter into the minimal symbol tables as there is
8976 a minimal symbol table entry from the ELF symbols already.
8977 Enter into partial symbol table if it has a location
8978 descriptor or a type.
8979 If the location descriptor is missing, new_symbol will create
8980 a LOC_UNRESOLVED symbol, the address of the variable will then
8981 be determined from the minimal symbol table whenever the variable
8982 is referenced.
8983 The address for the partial symbol table entry is not
8984 used by GDB, but it comes in handy for debugging partial symbol
8985 table building. */
8986
8987 if (pdi->d.locdesc || pdi->has_type)
8988 add_psymbol_to_list (actual_name, strlen (actual_name),
8989 built_actual_name != NULL,
8990 VAR_DOMAIN, LOC_STATIC,
8991 &objfile->global_psymbols,
8992 addr + baseaddr,
8993 cu->language, objfile);
8994 }
8995 else
8996 {
8997 int has_loc = pdi->d.locdesc != NULL;
8998
8999 /* Static Variable. Skip symbols whose value we cannot know (those
9000 without location descriptors or constant values). */
9001 if (!has_loc && !pdi->has_const_value)
9002 {
9003 xfree (built_actual_name);
9004 return;
9005 }
9006
9007 add_psymbol_to_list (actual_name, strlen (actual_name),
9008 built_actual_name != NULL,
9009 VAR_DOMAIN, LOC_STATIC,
9010 &objfile->static_psymbols,
9011 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9012 cu->language, objfile);
9013 }
9014 break;
9015 case DW_TAG_typedef:
9016 case DW_TAG_base_type:
9017 case DW_TAG_subrange_type:
9018 add_psymbol_to_list (actual_name, strlen (actual_name),
9019 built_actual_name != NULL,
9020 VAR_DOMAIN, LOC_TYPEDEF,
9021 &objfile->static_psymbols,
9022 0, cu->language, objfile);
9023 break;
9024 case DW_TAG_imported_declaration:
9025 case DW_TAG_namespace:
9026 add_psymbol_to_list (actual_name, strlen (actual_name),
9027 built_actual_name != NULL,
9028 VAR_DOMAIN, LOC_TYPEDEF,
9029 &objfile->global_psymbols,
9030 0, cu->language, objfile);
9031 break;
9032 case DW_TAG_module:
9033 add_psymbol_to_list (actual_name, strlen (actual_name),
9034 built_actual_name != NULL,
9035 MODULE_DOMAIN, LOC_TYPEDEF,
9036 &objfile->global_psymbols,
9037 0, cu->language, objfile);
9038 break;
9039 case DW_TAG_class_type:
9040 case DW_TAG_interface_type:
9041 case DW_TAG_structure_type:
9042 case DW_TAG_union_type:
9043 case DW_TAG_enumeration_type:
9044 /* Skip external references. The DWARF standard says in the section
9045 about "Structure, Union, and Class Type Entries": "An incomplete
9046 structure, union or class type is represented by a structure,
9047 union or class entry that does not have a byte size attribute
9048 and that has a DW_AT_declaration attribute." */
9049 if (!pdi->has_byte_size && pdi->is_declaration)
9050 {
9051 xfree (built_actual_name);
9052 return;
9053 }
9054
9055 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9056 static vs. global. */
9057 add_psymbol_to_list (actual_name, strlen (actual_name),
9058 built_actual_name != NULL,
9059 STRUCT_DOMAIN, LOC_TYPEDEF,
9060 cu->language == language_cplus
9061 ? &objfile->global_psymbols
9062 : &objfile->static_psymbols,
9063 0, cu->language, objfile);
9064
9065 break;
9066 case DW_TAG_enumerator:
9067 add_psymbol_to_list (actual_name, strlen (actual_name),
9068 built_actual_name != NULL,
9069 VAR_DOMAIN, LOC_CONST,
9070 cu->language == language_cplus
9071 ? &objfile->global_psymbols
9072 : &objfile->static_psymbols,
9073 0, cu->language, objfile);
9074 break;
9075 default:
9076 break;
9077 }
9078
9079 xfree (built_actual_name);
9080 }
9081
9082 /* Read a partial die corresponding to a namespace; also, add a symbol
9083 corresponding to that namespace to the symbol table. NAMESPACE is
9084 the name of the enclosing namespace. */
9085
9086 static void
9087 add_partial_namespace (struct partial_die_info *pdi,
9088 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9089 int set_addrmap, struct dwarf2_cu *cu)
9090 {
9091 /* Add a symbol for the namespace. */
9092
9093 add_partial_symbol (pdi, cu);
9094
9095 /* Now scan partial symbols in that namespace. */
9096
9097 if (pdi->has_children)
9098 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9099 }
9100
9101 /* Read a partial die corresponding to a Fortran module. */
9102
9103 static void
9104 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9105 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9106 {
9107 /* Add a symbol for the namespace. */
9108
9109 add_partial_symbol (pdi, cu);
9110
9111 /* Now scan partial symbols in that module. */
9112
9113 if (pdi->has_children)
9114 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9115 }
9116
9117 /* Read a partial die corresponding to a subprogram or an inlined
9118 subprogram and create a partial symbol for that subprogram.
9119 When the CU language allows it, this routine also defines a partial
9120 symbol for each nested subprogram that this subprogram contains.
9121 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9122 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9123
9124 PDI may also be a lexical block, in which case we simply search
9125 recursively for subprograms defined inside that lexical block.
9126 Again, this is only performed when the CU language allows this
9127 type of definitions. */
9128
9129 static void
9130 add_partial_subprogram (struct partial_die_info *pdi,
9131 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9132 int set_addrmap, struct dwarf2_cu *cu)
9133 {
9134 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9135 {
9136 if (pdi->has_pc_info)
9137 {
9138 if (pdi->lowpc < *lowpc)
9139 *lowpc = pdi->lowpc;
9140 if (pdi->highpc > *highpc)
9141 *highpc = pdi->highpc;
9142 if (set_addrmap)
9143 {
9144 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9145 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9146 CORE_ADDR baseaddr;
9147 CORE_ADDR highpc;
9148 CORE_ADDR lowpc;
9149
9150 baseaddr = ANOFFSET (objfile->section_offsets,
9151 SECT_OFF_TEXT (objfile));
9152 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9153 pdi->lowpc + baseaddr);
9154 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9155 pdi->highpc + baseaddr);
9156 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9157 cu->per_cu->v.psymtab);
9158 }
9159 }
9160
9161 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9162 {
9163 if (!pdi->is_declaration)
9164 /* Ignore subprogram DIEs that do not have a name, they are
9165 illegal. Do not emit a complaint at this point, we will
9166 do so when we convert this psymtab into a symtab. */
9167 if (pdi->name)
9168 add_partial_symbol (pdi, cu);
9169 }
9170 }
9171
9172 if (! pdi->has_children)
9173 return;
9174
9175 if (cu->language == language_ada)
9176 {
9177 pdi = pdi->die_child;
9178 while (pdi != NULL)
9179 {
9180 pdi->fixup (cu);
9181 if (pdi->tag == DW_TAG_subprogram
9182 || pdi->tag == DW_TAG_inlined_subroutine
9183 || pdi->tag == DW_TAG_lexical_block)
9184 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9185 pdi = pdi->die_sibling;
9186 }
9187 }
9188 }
9189
9190 /* Read a partial die corresponding to an enumeration type. */
9191
9192 static void
9193 add_partial_enumeration (struct partial_die_info *enum_pdi,
9194 struct dwarf2_cu *cu)
9195 {
9196 struct partial_die_info *pdi;
9197
9198 if (enum_pdi->name != NULL)
9199 add_partial_symbol (enum_pdi, cu);
9200
9201 pdi = enum_pdi->die_child;
9202 while (pdi)
9203 {
9204 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9205 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9206 else
9207 add_partial_symbol (pdi, cu);
9208 pdi = pdi->die_sibling;
9209 }
9210 }
9211
9212 /* Return the initial uleb128 in the die at INFO_PTR. */
9213
9214 static unsigned int
9215 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9216 {
9217 unsigned int bytes_read;
9218
9219 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9220 }
9221
9222 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9223 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9224
9225 Return the corresponding abbrev, or NULL if the number is zero (indicating
9226 an empty DIE). In either case *BYTES_READ will be set to the length of
9227 the initial number. */
9228
9229 static struct abbrev_info *
9230 peek_die_abbrev (const die_reader_specs &reader,
9231 const gdb_byte *info_ptr, unsigned int *bytes_read)
9232 {
9233 dwarf2_cu *cu = reader.cu;
9234 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9235 unsigned int abbrev_number
9236 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9237
9238 if (abbrev_number == 0)
9239 return NULL;
9240
9241 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9242 if (!abbrev)
9243 {
9244 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9245 " at offset %s [in module %s]"),
9246 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9247 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9248 }
9249
9250 return abbrev;
9251 }
9252
9253 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9254 Returns a pointer to the end of a series of DIEs, terminated by an empty
9255 DIE. Any children of the skipped DIEs will also be skipped. */
9256
9257 static const gdb_byte *
9258 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9259 {
9260 while (1)
9261 {
9262 unsigned int bytes_read;
9263 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9264
9265 if (abbrev == NULL)
9266 return info_ptr + bytes_read;
9267 else
9268 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9269 }
9270 }
9271
9272 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9273 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9274 abbrev corresponding to that skipped uleb128 should be passed in
9275 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9276 children. */
9277
9278 static const gdb_byte *
9279 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9280 struct abbrev_info *abbrev)
9281 {
9282 unsigned int bytes_read;
9283 struct attribute attr;
9284 bfd *abfd = reader->abfd;
9285 struct dwarf2_cu *cu = reader->cu;
9286 const gdb_byte *buffer = reader->buffer;
9287 const gdb_byte *buffer_end = reader->buffer_end;
9288 unsigned int form, i;
9289
9290 for (i = 0; i < abbrev->num_attrs; i++)
9291 {
9292 /* The only abbrev we care about is DW_AT_sibling. */
9293 if (abbrev->attrs[i].name == DW_AT_sibling)
9294 {
9295 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9296 if (attr.form == DW_FORM_ref_addr)
9297 complaint (&symfile_complaints,
9298 _("ignoring absolute DW_AT_sibling"));
9299 else
9300 {
9301 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9302 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9303
9304 if (sibling_ptr < info_ptr)
9305 complaint (&symfile_complaints,
9306 _("DW_AT_sibling points backwards"));
9307 else if (sibling_ptr > reader->buffer_end)
9308 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9309 else
9310 return sibling_ptr;
9311 }
9312 }
9313
9314 /* If it isn't DW_AT_sibling, skip this attribute. */
9315 form = abbrev->attrs[i].form;
9316 skip_attribute:
9317 switch (form)
9318 {
9319 case DW_FORM_ref_addr:
9320 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9321 and later it is offset sized. */
9322 if (cu->header.version == 2)
9323 info_ptr += cu->header.addr_size;
9324 else
9325 info_ptr += cu->header.offset_size;
9326 break;
9327 case DW_FORM_GNU_ref_alt:
9328 info_ptr += cu->header.offset_size;
9329 break;
9330 case DW_FORM_addr:
9331 info_ptr += cu->header.addr_size;
9332 break;
9333 case DW_FORM_data1:
9334 case DW_FORM_ref1:
9335 case DW_FORM_flag:
9336 info_ptr += 1;
9337 break;
9338 case DW_FORM_flag_present:
9339 case DW_FORM_implicit_const:
9340 break;
9341 case DW_FORM_data2:
9342 case DW_FORM_ref2:
9343 info_ptr += 2;
9344 break;
9345 case DW_FORM_data4:
9346 case DW_FORM_ref4:
9347 info_ptr += 4;
9348 break;
9349 case DW_FORM_data8:
9350 case DW_FORM_ref8:
9351 case DW_FORM_ref_sig8:
9352 info_ptr += 8;
9353 break;
9354 case DW_FORM_data16:
9355 info_ptr += 16;
9356 break;
9357 case DW_FORM_string:
9358 read_direct_string (abfd, info_ptr, &bytes_read);
9359 info_ptr += bytes_read;
9360 break;
9361 case DW_FORM_sec_offset:
9362 case DW_FORM_strp:
9363 case DW_FORM_GNU_strp_alt:
9364 info_ptr += cu->header.offset_size;
9365 break;
9366 case DW_FORM_exprloc:
9367 case DW_FORM_block:
9368 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9369 info_ptr += bytes_read;
9370 break;
9371 case DW_FORM_block1:
9372 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9373 break;
9374 case DW_FORM_block2:
9375 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9376 break;
9377 case DW_FORM_block4:
9378 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9379 break;
9380 case DW_FORM_sdata:
9381 case DW_FORM_udata:
9382 case DW_FORM_ref_udata:
9383 case DW_FORM_GNU_addr_index:
9384 case DW_FORM_GNU_str_index:
9385 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9386 break;
9387 case DW_FORM_indirect:
9388 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9389 info_ptr += bytes_read;
9390 /* We need to continue parsing from here, so just go back to
9391 the top. */
9392 goto skip_attribute;
9393
9394 default:
9395 error (_("Dwarf Error: Cannot handle %s "
9396 "in DWARF reader [in module %s]"),
9397 dwarf_form_name (form),
9398 bfd_get_filename (abfd));
9399 }
9400 }
9401
9402 if (abbrev->has_children)
9403 return skip_children (reader, info_ptr);
9404 else
9405 return info_ptr;
9406 }
9407
9408 /* Locate ORIG_PDI's sibling.
9409 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9410
9411 static const gdb_byte *
9412 locate_pdi_sibling (const struct die_reader_specs *reader,
9413 struct partial_die_info *orig_pdi,
9414 const gdb_byte *info_ptr)
9415 {
9416 /* Do we know the sibling already? */
9417
9418 if (orig_pdi->sibling)
9419 return orig_pdi->sibling;
9420
9421 /* Are there any children to deal with? */
9422
9423 if (!orig_pdi->has_children)
9424 return info_ptr;
9425
9426 /* Skip the children the long way. */
9427
9428 return skip_children (reader, info_ptr);
9429 }
9430
9431 /* Expand this partial symbol table into a full symbol table. SELF is
9432 not NULL. */
9433
9434 static void
9435 dwarf2_read_symtab (struct partial_symtab *self,
9436 struct objfile *objfile)
9437 {
9438 struct dwarf2_per_objfile *dwarf2_per_objfile
9439 = get_dwarf2_per_objfile (objfile);
9440
9441 if (self->readin)
9442 {
9443 warning (_("bug: psymtab for %s is already read in."),
9444 self->filename);
9445 }
9446 else
9447 {
9448 if (info_verbose)
9449 {
9450 printf_filtered (_("Reading in symbols for %s..."),
9451 self->filename);
9452 gdb_flush (gdb_stdout);
9453 }
9454
9455 /* If this psymtab is constructed from a debug-only objfile, the
9456 has_section_at_zero flag will not necessarily be correct. We
9457 can get the correct value for this flag by looking at the data
9458 associated with the (presumably stripped) associated objfile. */
9459 if (objfile->separate_debug_objfile_backlink)
9460 {
9461 struct dwarf2_per_objfile *dpo_backlink
9462 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9463
9464 dwarf2_per_objfile->has_section_at_zero
9465 = dpo_backlink->has_section_at_zero;
9466 }
9467
9468 dwarf2_per_objfile->reading_partial_symbols = 0;
9469
9470 psymtab_to_symtab_1 (self);
9471
9472 /* Finish up the debug error message. */
9473 if (info_verbose)
9474 printf_filtered (_("done.\n"));
9475 }
9476
9477 process_cu_includes (dwarf2_per_objfile);
9478 }
9479 \f
9480 /* Reading in full CUs. */
9481
9482 /* Add PER_CU to the queue. */
9483
9484 static void
9485 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9486 enum language pretend_language)
9487 {
9488 struct dwarf2_queue_item *item;
9489
9490 per_cu->queued = 1;
9491 item = XNEW (struct dwarf2_queue_item);
9492 item->per_cu = per_cu;
9493 item->pretend_language = pretend_language;
9494 item->next = NULL;
9495
9496 if (dwarf2_queue == NULL)
9497 dwarf2_queue = item;
9498 else
9499 dwarf2_queue_tail->next = item;
9500
9501 dwarf2_queue_tail = item;
9502 }
9503
9504 /* If PER_CU is not yet queued, add it to the queue.
9505 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9506 dependency.
9507 The result is non-zero if PER_CU was queued, otherwise the result is zero
9508 meaning either PER_CU is already queued or it is already loaded.
9509
9510 N.B. There is an invariant here that if a CU is queued then it is loaded.
9511 The caller is required to load PER_CU if we return non-zero. */
9512
9513 static int
9514 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9515 struct dwarf2_per_cu_data *per_cu,
9516 enum language pretend_language)
9517 {
9518 /* We may arrive here during partial symbol reading, if we need full
9519 DIEs to process an unusual case (e.g. template arguments). Do
9520 not queue PER_CU, just tell our caller to load its DIEs. */
9521 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9522 {
9523 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9524 return 1;
9525 return 0;
9526 }
9527
9528 /* Mark the dependence relation so that we don't flush PER_CU
9529 too early. */
9530 if (dependent_cu != NULL)
9531 dwarf2_add_dependence (dependent_cu, per_cu);
9532
9533 /* If it's already on the queue, we have nothing to do. */
9534 if (per_cu->queued)
9535 return 0;
9536
9537 /* If the compilation unit is already loaded, just mark it as
9538 used. */
9539 if (per_cu->cu != NULL)
9540 {
9541 per_cu->cu->last_used = 0;
9542 return 0;
9543 }
9544
9545 /* Add it to the queue. */
9546 queue_comp_unit (per_cu, pretend_language);
9547
9548 return 1;
9549 }
9550
9551 /* Process the queue. */
9552
9553 static void
9554 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9555 {
9556 struct dwarf2_queue_item *item, *next_item;
9557
9558 if (dwarf_read_debug)
9559 {
9560 fprintf_unfiltered (gdb_stdlog,
9561 "Expanding one or more symtabs of objfile %s ...\n",
9562 objfile_name (dwarf2_per_objfile->objfile));
9563 }
9564
9565 /* The queue starts out with one item, but following a DIE reference
9566 may load a new CU, adding it to the end of the queue. */
9567 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9568 {
9569 if ((dwarf2_per_objfile->using_index
9570 ? !item->per_cu->v.quick->compunit_symtab
9571 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9572 /* Skip dummy CUs. */
9573 && item->per_cu->cu != NULL)
9574 {
9575 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9576 unsigned int debug_print_threshold;
9577 char buf[100];
9578
9579 if (per_cu->is_debug_types)
9580 {
9581 struct signatured_type *sig_type =
9582 (struct signatured_type *) per_cu;
9583
9584 sprintf (buf, "TU %s at offset %s",
9585 hex_string (sig_type->signature),
9586 sect_offset_str (per_cu->sect_off));
9587 /* There can be 100s of TUs.
9588 Only print them in verbose mode. */
9589 debug_print_threshold = 2;
9590 }
9591 else
9592 {
9593 sprintf (buf, "CU at offset %s",
9594 sect_offset_str (per_cu->sect_off));
9595 debug_print_threshold = 1;
9596 }
9597
9598 if (dwarf_read_debug >= debug_print_threshold)
9599 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9600
9601 if (per_cu->is_debug_types)
9602 process_full_type_unit (per_cu, item->pretend_language);
9603 else
9604 process_full_comp_unit (per_cu, item->pretend_language);
9605
9606 if (dwarf_read_debug >= debug_print_threshold)
9607 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9608 }
9609
9610 item->per_cu->queued = 0;
9611 next_item = item->next;
9612 xfree (item);
9613 }
9614
9615 dwarf2_queue_tail = NULL;
9616
9617 if (dwarf_read_debug)
9618 {
9619 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9620 objfile_name (dwarf2_per_objfile->objfile));
9621 }
9622 }
9623
9624 /* Read in full symbols for PST, and anything it depends on. */
9625
9626 static void
9627 psymtab_to_symtab_1 (struct partial_symtab *pst)
9628 {
9629 struct dwarf2_per_cu_data *per_cu;
9630 int i;
9631
9632 if (pst->readin)
9633 return;
9634
9635 for (i = 0; i < pst->number_of_dependencies; i++)
9636 if (!pst->dependencies[i]->readin
9637 && pst->dependencies[i]->user == NULL)
9638 {
9639 /* Inform about additional files that need to be read in. */
9640 if (info_verbose)
9641 {
9642 /* FIXME: i18n: Need to make this a single string. */
9643 fputs_filtered (" ", gdb_stdout);
9644 wrap_here ("");
9645 fputs_filtered ("and ", gdb_stdout);
9646 wrap_here ("");
9647 printf_filtered ("%s...", pst->dependencies[i]->filename);
9648 wrap_here (""); /* Flush output. */
9649 gdb_flush (gdb_stdout);
9650 }
9651 psymtab_to_symtab_1 (pst->dependencies[i]);
9652 }
9653
9654 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9655
9656 if (per_cu == NULL)
9657 {
9658 /* It's an include file, no symbols to read for it.
9659 Everything is in the parent symtab. */
9660 pst->readin = 1;
9661 return;
9662 }
9663
9664 dw2_do_instantiate_symtab (per_cu);
9665 }
9666
9667 /* Trivial hash function for die_info: the hash value of a DIE
9668 is its offset in .debug_info for this objfile. */
9669
9670 static hashval_t
9671 die_hash (const void *item)
9672 {
9673 const struct die_info *die = (const struct die_info *) item;
9674
9675 return to_underlying (die->sect_off);
9676 }
9677
9678 /* Trivial comparison function for die_info structures: two DIEs
9679 are equal if they have the same offset. */
9680
9681 static int
9682 die_eq (const void *item_lhs, const void *item_rhs)
9683 {
9684 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9685 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9686
9687 return die_lhs->sect_off == die_rhs->sect_off;
9688 }
9689
9690 /* die_reader_func for load_full_comp_unit.
9691 This is identical to read_signatured_type_reader,
9692 but is kept separate for now. */
9693
9694 static void
9695 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9696 const gdb_byte *info_ptr,
9697 struct die_info *comp_unit_die,
9698 int has_children,
9699 void *data)
9700 {
9701 struct dwarf2_cu *cu = reader->cu;
9702 enum language *language_ptr = (enum language *) data;
9703
9704 gdb_assert (cu->die_hash == NULL);
9705 cu->die_hash =
9706 htab_create_alloc_ex (cu->header.length / 12,
9707 die_hash,
9708 die_eq,
9709 NULL,
9710 &cu->comp_unit_obstack,
9711 hashtab_obstack_allocate,
9712 dummy_obstack_deallocate);
9713
9714 if (has_children)
9715 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9716 &info_ptr, comp_unit_die);
9717 cu->dies = comp_unit_die;
9718 /* comp_unit_die is not stored in die_hash, no need. */
9719
9720 /* We try not to read any attributes in this function, because not
9721 all CUs needed for references have been loaded yet, and symbol
9722 table processing isn't initialized. But we have to set the CU language,
9723 or we won't be able to build types correctly.
9724 Similarly, if we do not read the producer, we can not apply
9725 producer-specific interpretation. */
9726 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9727 }
9728
9729 /* Load the DIEs associated with PER_CU into memory. */
9730
9731 static void
9732 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9733 enum language pretend_language)
9734 {
9735 gdb_assert (! this_cu->is_debug_types);
9736
9737 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
9738 load_full_comp_unit_reader, &pretend_language);
9739 }
9740
9741 /* Add a DIE to the delayed physname list. */
9742
9743 static void
9744 add_to_method_list (struct type *type, int fnfield_index, int index,
9745 const char *name, struct die_info *die,
9746 struct dwarf2_cu *cu)
9747 {
9748 struct delayed_method_info mi;
9749 mi.type = type;
9750 mi.fnfield_index = fnfield_index;
9751 mi.index = index;
9752 mi.name = name;
9753 mi.die = die;
9754 cu->method_list.push_back (mi);
9755 }
9756
9757 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9758 "const" / "volatile". If so, decrements LEN by the length of the
9759 modifier and return true. Otherwise return false. */
9760
9761 template<size_t N>
9762 static bool
9763 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9764 {
9765 size_t mod_len = sizeof (mod) - 1;
9766 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9767 {
9768 len -= mod_len;
9769 return true;
9770 }
9771 return false;
9772 }
9773
9774 /* Compute the physnames of any methods on the CU's method list.
9775
9776 The computation of method physnames is delayed in order to avoid the
9777 (bad) condition that one of the method's formal parameters is of an as yet
9778 incomplete type. */
9779
9780 static void
9781 compute_delayed_physnames (struct dwarf2_cu *cu)
9782 {
9783 /* Only C++ delays computing physnames. */
9784 if (cu->method_list.empty ())
9785 return;
9786 gdb_assert (cu->language == language_cplus);
9787
9788 for (struct delayed_method_info &mi : cu->method_list)
9789 {
9790 const char *physname;
9791 struct fn_fieldlist *fn_flp
9792 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9793 physname = dwarf2_physname (mi.name, mi.die, cu);
9794 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9795 = physname ? physname : "";
9796
9797 /* Since there's no tag to indicate whether a method is a
9798 const/volatile overload, extract that information out of the
9799 demangled name. */
9800 if (physname != NULL)
9801 {
9802 size_t len = strlen (physname);
9803
9804 while (1)
9805 {
9806 if (physname[len] == ')') /* shortcut */
9807 break;
9808 else if (check_modifier (physname, len, " const"))
9809 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9810 else if (check_modifier (physname, len, " volatile"))
9811 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9812 else
9813 break;
9814 }
9815 }
9816 }
9817
9818 /* The list is no longer needed. */
9819 cu->method_list.clear ();
9820 }
9821
9822 /* Go objects should be embedded in a DW_TAG_module DIE,
9823 and it's not clear if/how imported objects will appear.
9824 To keep Go support simple until that's worked out,
9825 go back through what we've read and create something usable.
9826 We could do this while processing each DIE, and feels kinda cleaner,
9827 but that way is more invasive.
9828 This is to, for example, allow the user to type "p var" or "b main"
9829 without having to specify the package name, and allow lookups
9830 of module.object to work in contexts that use the expression
9831 parser. */
9832
9833 static void
9834 fixup_go_packaging (struct dwarf2_cu *cu)
9835 {
9836 char *package_name = NULL;
9837 struct pending *list;
9838 int i;
9839
9840 for (list = global_symbols; list != NULL; list = list->next)
9841 {
9842 for (i = 0; i < list->nsyms; ++i)
9843 {
9844 struct symbol *sym = list->symbol[i];
9845
9846 if (SYMBOL_LANGUAGE (sym) == language_go
9847 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9848 {
9849 char *this_package_name = go_symbol_package_name (sym);
9850
9851 if (this_package_name == NULL)
9852 continue;
9853 if (package_name == NULL)
9854 package_name = this_package_name;
9855 else
9856 {
9857 struct objfile *objfile
9858 = cu->per_cu->dwarf2_per_objfile->objfile;
9859 if (strcmp (package_name, this_package_name) != 0)
9860 complaint (&symfile_complaints,
9861 _("Symtab %s has objects from two different Go packages: %s and %s"),
9862 (symbol_symtab (sym) != NULL
9863 ? symtab_to_filename_for_display
9864 (symbol_symtab (sym))
9865 : objfile_name (objfile)),
9866 this_package_name, package_name);
9867 xfree (this_package_name);
9868 }
9869 }
9870 }
9871 }
9872
9873 if (package_name != NULL)
9874 {
9875 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9876 const char *saved_package_name
9877 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9878 package_name,
9879 strlen (package_name));
9880 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9881 saved_package_name);
9882 struct symbol *sym;
9883
9884 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9885
9886 sym = allocate_symbol (objfile);
9887 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9888 SYMBOL_SET_NAMES (sym, saved_package_name,
9889 strlen (saved_package_name), 0, objfile);
9890 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9891 e.g., "main" finds the "main" module and not C's main(). */
9892 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9893 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9894 SYMBOL_TYPE (sym) = type;
9895
9896 add_symbol_to_list (sym, &global_symbols);
9897
9898 xfree (package_name);
9899 }
9900 }
9901
9902 /* Allocate a fully-qualified name consisting of the two parts on the
9903 obstack. */
9904
9905 static const char *
9906 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9907 {
9908 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9909 }
9910
9911 /* A helper that allocates a struct discriminant_info to attach to a
9912 union type. */
9913
9914 static struct discriminant_info *
9915 alloc_discriminant_info (struct type *type, int discriminant_index,
9916 int default_index)
9917 {
9918 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9919 gdb_assert (discriminant_index == -1
9920 || (discriminant_index >= 0
9921 && discriminant_index < TYPE_NFIELDS (type)));
9922 gdb_assert (default_index == -1
9923 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9924
9925 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9926
9927 struct discriminant_info *disc
9928 = ((struct discriminant_info *)
9929 TYPE_ZALLOC (type,
9930 offsetof (struct discriminant_info, discriminants)
9931 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9932 disc->default_index = default_index;
9933 disc->discriminant_index = discriminant_index;
9934
9935 struct dynamic_prop prop;
9936 prop.kind = PROP_UNDEFINED;
9937 prop.data.baton = disc;
9938
9939 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9940
9941 return disc;
9942 }
9943
9944 /* Some versions of rustc emitted enums in an unusual way.
9945
9946 Ordinary enums were emitted as unions. The first element of each
9947 structure in the union was named "RUST$ENUM$DISR". This element
9948 held the discriminant.
9949
9950 These versions of Rust also implemented the "non-zero"
9951 optimization. When the enum had two values, and one is empty and
9952 the other holds a pointer that cannot be zero, the pointer is used
9953 as the discriminant, with a zero value meaning the empty variant.
9954 Here, the union's first member is of the form
9955 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9956 where the fieldnos are the indices of the fields that should be
9957 traversed in order to find the field (which may be several fields deep)
9958 and the variantname is the name of the variant of the case when the
9959 field is zero.
9960
9961 This function recognizes whether TYPE is of one of these forms,
9962 and, if so, smashes it to be a variant type. */
9963
9964 static void
9965 quirk_rust_enum (struct type *type, struct objfile *objfile)
9966 {
9967 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9968
9969 /* We don't need to deal with empty enums. */
9970 if (TYPE_NFIELDS (type) == 0)
9971 return;
9972
9973 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9974 if (TYPE_NFIELDS (type) == 1
9975 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9976 {
9977 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9978
9979 /* Decode the field name to find the offset of the
9980 discriminant. */
9981 ULONGEST bit_offset = 0;
9982 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9983 while (name[0] >= '0' && name[0] <= '9')
9984 {
9985 char *tail;
9986 unsigned long index = strtoul (name, &tail, 10);
9987 name = tail;
9988 if (*name != '$'
9989 || index >= TYPE_NFIELDS (field_type)
9990 || (TYPE_FIELD_LOC_KIND (field_type, index)
9991 != FIELD_LOC_KIND_BITPOS))
9992 {
9993 complaint (&symfile_complaints,
9994 _("Could not parse Rust enum encoding string \"%s\""
9995 "[in module %s]"),
9996 TYPE_FIELD_NAME (type, 0),
9997 objfile_name (objfile));
9998 return;
9999 }
10000 ++name;
10001
10002 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10003 field_type = TYPE_FIELD_TYPE (field_type, index);
10004 }
10005
10006 /* Make a union to hold the variants. */
10007 struct type *union_type = alloc_type (objfile);
10008 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10009 TYPE_NFIELDS (union_type) = 3;
10010 TYPE_FIELDS (union_type)
10011 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10012 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10013
10014 /* Put the discriminant must at index 0. */
10015 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10016 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10017 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10018 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10019
10020 /* The order of fields doesn't really matter, so put the real
10021 field at index 1 and the data-less field at index 2. */
10022 struct discriminant_info *disc
10023 = alloc_discriminant_info (union_type, 0, 1);
10024 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10025 TYPE_FIELD_NAME (union_type, 1)
10026 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10027 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10028 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10029 TYPE_FIELD_NAME (union_type, 1));
10030
10031 const char *dataless_name
10032 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10033 name);
10034 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10035 dataless_name);
10036 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10037 /* NAME points into the original discriminant name, which
10038 already has the correct lifetime. */
10039 TYPE_FIELD_NAME (union_type, 2) = name;
10040 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10041 disc->discriminants[2] = 0;
10042
10043 /* Smash this type to be a structure type. We have to do this
10044 because the type has already been recorded. */
10045 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10046 TYPE_NFIELDS (type) = 1;
10047 TYPE_FIELDS (type)
10048 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10049
10050 /* Install the variant part. */
10051 TYPE_FIELD_TYPE (type, 0) = union_type;
10052 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10053 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10054 }
10055 else if (TYPE_NFIELDS (type) == 1)
10056 {
10057 /* We assume that a union with a single field is a univariant
10058 enum. */
10059 /* Smash this type to be a structure type. We have to do this
10060 because the type has already been recorded. */
10061 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10062
10063 /* Make a union to hold the variants. */
10064 struct type *union_type = alloc_type (objfile);
10065 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10066 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10067 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10068 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10069
10070 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10071 const char *variant_name
10072 = rust_last_path_segment (TYPE_NAME (field_type));
10073 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10074 TYPE_NAME (field_type)
10075 = rust_fully_qualify (&objfile->objfile_obstack,
10076 TYPE_NAME (type), variant_name);
10077
10078 /* Install the union in the outer struct type. */
10079 TYPE_NFIELDS (type) = 1;
10080 TYPE_FIELDS (type)
10081 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10082 TYPE_FIELD_TYPE (type, 0) = union_type;
10083 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10084 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10085
10086 alloc_discriminant_info (union_type, -1, 0);
10087 }
10088 else
10089 {
10090 struct type *disr_type = nullptr;
10091 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10092 {
10093 disr_type = TYPE_FIELD_TYPE (type, i);
10094
10095 if (TYPE_NFIELDS (disr_type) == 0)
10096 {
10097 /* Could be data-less variant, so keep going. */
10098 }
10099 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10100 "RUST$ENUM$DISR") != 0)
10101 {
10102 /* Not a Rust enum. */
10103 return;
10104 }
10105 else
10106 {
10107 /* Found one. */
10108 break;
10109 }
10110 }
10111
10112 /* If we got here without a discriminant, then it's probably
10113 just a union. */
10114 if (disr_type == nullptr)
10115 return;
10116
10117 /* Smash this type to be a structure type. We have to do this
10118 because the type has already been recorded. */
10119 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10120
10121 /* Make a union to hold the variants. */
10122 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10123 struct type *union_type = alloc_type (objfile);
10124 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10125 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10126 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10127 TYPE_FIELDS (union_type)
10128 = (struct field *) TYPE_ZALLOC (union_type,
10129 (TYPE_NFIELDS (union_type)
10130 * sizeof (struct field)));
10131
10132 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10133 TYPE_NFIELDS (type) * sizeof (struct field));
10134
10135 /* Install the discriminant at index 0 in the union. */
10136 TYPE_FIELD (union_type, 0) = *disr_field;
10137 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10138 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10139
10140 /* Install the union in the outer struct type. */
10141 TYPE_FIELD_TYPE (type, 0) = union_type;
10142 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10143 TYPE_NFIELDS (type) = 1;
10144
10145 /* Set the size and offset of the union type. */
10146 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10147
10148 /* We need a way to find the correct discriminant given a
10149 variant name. For convenience we build a map here. */
10150 struct type *enum_type = FIELD_TYPE (*disr_field);
10151 std::unordered_map<std::string, ULONGEST> discriminant_map;
10152 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10153 {
10154 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10155 {
10156 const char *name
10157 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10158 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10159 }
10160 }
10161
10162 int n_fields = TYPE_NFIELDS (union_type);
10163 struct discriminant_info *disc
10164 = alloc_discriminant_info (union_type, 0, -1);
10165 /* Skip the discriminant here. */
10166 for (int i = 1; i < n_fields; ++i)
10167 {
10168 /* Find the final word in the name of this variant's type.
10169 That name can be used to look up the correct
10170 discriminant. */
10171 const char *variant_name
10172 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10173 i)));
10174
10175 auto iter = discriminant_map.find (variant_name);
10176 if (iter != discriminant_map.end ())
10177 disc->discriminants[i] = iter->second;
10178
10179 /* Remove the discriminant field. */
10180 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10181 --TYPE_NFIELDS (sub_type);
10182 ++TYPE_FIELDS (sub_type);
10183 TYPE_FIELD_NAME (union_type, i) = variant_name;
10184 TYPE_NAME (sub_type)
10185 = rust_fully_qualify (&objfile->objfile_obstack,
10186 TYPE_NAME (type), variant_name);
10187 }
10188 }
10189 }
10190
10191 /* Rewrite some Rust unions to be structures with variants parts. */
10192
10193 static void
10194 rust_union_quirks (struct dwarf2_cu *cu)
10195 {
10196 gdb_assert (cu->language == language_rust);
10197 for (struct type *type : cu->rust_unions)
10198 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10199 }
10200
10201 /* Return the symtab for PER_CU. This works properly regardless of
10202 whether we're using the index or psymtabs. */
10203
10204 static struct compunit_symtab *
10205 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10206 {
10207 return (per_cu->dwarf2_per_objfile->using_index
10208 ? per_cu->v.quick->compunit_symtab
10209 : per_cu->v.psymtab->compunit_symtab);
10210 }
10211
10212 /* A helper function for computing the list of all symbol tables
10213 included by PER_CU. */
10214
10215 static void
10216 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10217 htab_t all_children, htab_t all_type_symtabs,
10218 struct dwarf2_per_cu_data *per_cu,
10219 struct compunit_symtab *immediate_parent)
10220 {
10221 void **slot;
10222 int ix;
10223 struct compunit_symtab *cust;
10224 struct dwarf2_per_cu_data *iter;
10225
10226 slot = htab_find_slot (all_children, per_cu, INSERT);
10227 if (*slot != NULL)
10228 {
10229 /* This inclusion and its children have been processed. */
10230 return;
10231 }
10232
10233 *slot = per_cu;
10234 /* Only add a CU if it has a symbol table. */
10235 cust = get_compunit_symtab (per_cu);
10236 if (cust != NULL)
10237 {
10238 /* If this is a type unit only add its symbol table if we haven't
10239 seen it yet (type unit per_cu's can share symtabs). */
10240 if (per_cu->is_debug_types)
10241 {
10242 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10243 if (*slot == NULL)
10244 {
10245 *slot = cust;
10246 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10247 if (cust->user == NULL)
10248 cust->user = immediate_parent;
10249 }
10250 }
10251 else
10252 {
10253 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10254 if (cust->user == NULL)
10255 cust->user = immediate_parent;
10256 }
10257 }
10258
10259 for (ix = 0;
10260 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10261 ++ix)
10262 {
10263 recursively_compute_inclusions (result, all_children,
10264 all_type_symtabs, iter, cust);
10265 }
10266 }
10267
10268 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10269 PER_CU. */
10270
10271 static void
10272 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10273 {
10274 gdb_assert (! per_cu->is_debug_types);
10275
10276 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10277 {
10278 int ix, len;
10279 struct dwarf2_per_cu_data *per_cu_iter;
10280 struct compunit_symtab *compunit_symtab_iter;
10281 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10282 htab_t all_children, all_type_symtabs;
10283 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10284
10285 /* If we don't have a symtab, we can just skip this case. */
10286 if (cust == NULL)
10287 return;
10288
10289 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10290 NULL, xcalloc, xfree);
10291 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10292 NULL, xcalloc, xfree);
10293
10294 for (ix = 0;
10295 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10296 ix, per_cu_iter);
10297 ++ix)
10298 {
10299 recursively_compute_inclusions (&result_symtabs, all_children,
10300 all_type_symtabs, per_cu_iter,
10301 cust);
10302 }
10303
10304 /* Now we have a transitive closure of all the included symtabs. */
10305 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10306 cust->includes
10307 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10308 struct compunit_symtab *, len + 1);
10309 for (ix = 0;
10310 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10311 compunit_symtab_iter);
10312 ++ix)
10313 cust->includes[ix] = compunit_symtab_iter;
10314 cust->includes[len] = NULL;
10315
10316 VEC_free (compunit_symtab_ptr, result_symtabs);
10317 htab_delete (all_children);
10318 htab_delete (all_type_symtabs);
10319 }
10320 }
10321
10322 /* Compute the 'includes' field for the symtabs of all the CUs we just
10323 read. */
10324
10325 static void
10326 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10327 {
10328 int ix;
10329 struct dwarf2_per_cu_data *iter;
10330
10331 for (ix = 0;
10332 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10333 ix, iter);
10334 ++ix)
10335 {
10336 if (! iter->is_debug_types)
10337 compute_compunit_symtab_includes (iter);
10338 }
10339
10340 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10341 }
10342
10343 /* Generate full symbol information for PER_CU, whose DIEs have
10344 already been loaded into memory. */
10345
10346 static void
10347 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10348 enum language pretend_language)
10349 {
10350 struct dwarf2_cu *cu = per_cu->cu;
10351 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10352 struct objfile *objfile = dwarf2_per_objfile->objfile;
10353 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10354 CORE_ADDR lowpc, highpc;
10355 struct compunit_symtab *cust;
10356 CORE_ADDR baseaddr;
10357 struct block *static_block;
10358 CORE_ADDR addr;
10359
10360 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10361
10362 buildsym_init ();
10363 scoped_free_pendings free_pending;
10364
10365 /* Clear the list here in case something was left over. */
10366 cu->method_list.clear ();
10367
10368 cu->list_in_scope = &file_symbols;
10369
10370 cu->language = pretend_language;
10371 cu->language_defn = language_def (cu->language);
10372
10373 /* Do line number decoding in read_file_scope () */
10374 process_die (cu->dies, cu);
10375
10376 /* For now fudge the Go package. */
10377 if (cu->language == language_go)
10378 fixup_go_packaging (cu);
10379
10380 /* Now that we have processed all the DIEs in the CU, all the types
10381 should be complete, and it should now be safe to compute all of the
10382 physnames. */
10383 compute_delayed_physnames (cu);
10384
10385 if (cu->language == language_rust)
10386 rust_union_quirks (cu);
10387
10388 /* Some compilers don't define a DW_AT_high_pc attribute for the
10389 compilation unit. If the DW_AT_high_pc is missing, synthesize
10390 it, by scanning the DIE's below the compilation unit. */
10391 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10392
10393 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10394 static_block = end_symtab_get_static_block (addr, 0, 1);
10395
10396 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10397 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10398 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10399 addrmap to help ensure it has an accurate map of pc values belonging to
10400 this comp unit. */
10401 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10402
10403 cust = end_symtab_from_static_block (static_block,
10404 SECT_OFF_TEXT (objfile), 0);
10405
10406 if (cust != NULL)
10407 {
10408 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10409
10410 /* Set symtab language to language from DW_AT_language. If the
10411 compilation is from a C file generated by language preprocessors, do
10412 not set the language if it was already deduced by start_subfile. */
10413 if (!(cu->language == language_c
10414 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10415 COMPUNIT_FILETABS (cust)->language = cu->language;
10416
10417 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10418 produce DW_AT_location with location lists but it can be possibly
10419 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10420 there were bugs in prologue debug info, fixed later in GCC-4.5
10421 by "unwind info for epilogues" patch (which is not directly related).
10422
10423 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10424 needed, it would be wrong due to missing DW_AT_producer there.
10425
10426 Still one can confuse GDB by using non-standard GCC compilation
10427 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10428 */
10429 if (cu->has_loclist && gcc_4_minor >= 5)
10430 cust->locations_valid = 1;
10431
10432 if (gcc_4_minor >= 5)
10433 cust->epilogue_unwind_valid = 1;
10434
10435 cust->call_site_htab = cu->call_site_htab;
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 /* Push it for inclusion processing later. */
10448 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10449 }
10450
10451 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10452 already been loaded into memory. */
10453
10454 static void
10455 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10456 enum language pretend_language)
10457 {
10458 struct dwarf2_cu *cu = per_cu->cu;
10459 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10460 struct objfile *objfile = dwarf2_per_objfile->objfile;
10461 struct compunit_symtab *cust;
10462 struct signatured_type *sig_type;
10463
10464 gdb_assert (per_cu->is_debug_types);
10465 sig_type = (struct signatured_type *) per_cu;
10466
10467 buildsym_init ();
10468 scoped_free_pendings free_pending;
10469
10470 /* Clear the list here in case something was left over. */
10471 cu->method_list.clear ();
10472
10473 cu->list_in_scope = &file_symbols;
10474
10475 cu->language = pretend_language;
10476 cu->language_defn = language_def (cu->language);
10477
10478 /* The symbol tables are set up in read_type_unit_scope. */
10479 process_die (cu->dies, cu);
10480
10481 /* For now fudge the Go package. */
10482 if (cu->language == language_go)
10483 fixup_go_packaging (cu);
10484
10485 /* Now that we have processed all the DIEs in the CU, all the types
10486 should be complete, and it should now be safe to compute all of the
10487 physnames. */
10488 compute_delayed_physnames (cu);
10489
10490 if (cu->language == language_rust)
10491 rust_union_quirks (cu);
10492
10493 /* TUs share symbol tables.
10494 If this is the first TU to use this symtab, complete the construction
10495 of it with end_expandable_symtab. Otherwise, complete the addition of
10496 this TU's symbols to the existing symtab. */
10497 if (sig_type->type_unit_group->compunit_symtab == NULL)
10498 {
10499 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10500 sig_type->type_unit_group->compunit_symtab = cust;
10501
10502 if (cust != NULL)
10503 {
10504 /* Set symtab language to language from DW_AT_language. If the
10505 compilation is from a C file generated by language preprocessors,
10506 do not set the language if it was already deduced by
10507 start_subfile. */
10508 if (!(cu->language == language_c
10509 && COMPUNIT_FILETABS (cust)->language != language_c))
10510 COMPUNIT_FILETABS (cust)->language = cu->language;
10511 }
10512 }
10513 else
10514 {
10515 augment_type_symtab ();
10516 cust = sig_type->type_unit_group->compunit_symtab;
10517 }
10518
10519 if (dwarf2_per_objfile->using_index)
10520 per_cu->v.quick->compunit_symtab = cust;
10521 else
10522 {
10523 struct partial_symtab *pst = per_cu->v.psymtab;
10524 pst->compunit_symtab = cust;
10525 pst->readin = 1;
10526 }
10527 }
10528
10529 /* Process an imported unit DIE. */
10530
10531 static void
10532 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10533 {
10534 struct attribute *attr;
10535
10536 /* For now we don't handle imported units in type units. */
10537 if (cu->per_cu->is_debug_types)
10538 {
10539 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10540 " supported in type units [in module %s]"),
10541 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10542 }
10543
10544 attr = dwarf2_attr (die, DW_AT_import, cu);
10545 if (attr != NULL)
10546 {
10547 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10548 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10549 dwarf2_per_cu_data *per_cu
10550 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10551 cu->per_cu->dwarf2_per_objfile);
10552
10553 /* If necessary, add it to the queue and load its DIEs. */
10554 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10555 load_full_comp_unit (per_cu, cu->language);
10556
10557 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10558 per_cu);
10559 }
10560 }
10561
10562 /* RAII object that represents a process_die scope: i.e.,
10563 starts/finishes processing a DIE. */
10564 class process_die_scope
10565 {
10566 public:
10567 process_die_scope (die_info *die, dwarf2_cu *cu)
10568 : m_die (die), m_cu (cu)
10569 {
10570 /* We should only be processing DIEs not already in process. */
10571 gdb_assert (!m_die->in_process);
10572 m_die->in_process = true;
10573 }
10574
10575 ~process_die_scope ()
10576 {
10577 m_die->in_process = false;
10578
10579 /* If we're done processing the DIE for the CU that owns the line
10580 header, we don't need the line header anymore. */
10581 if (m_cu->line_header_die_owner == m_die)
10582 {
10583 delete m_cu->line_header;
10584 m_cu->line_header = NULL;
10585 m_cu->line_header_die_owner = NULL;
10586 }
10587 }
10588
10589 private:
10590 die_info *m_die;
10591 dwarf2_cu *m_cu;
10592 };
10593
10594 /* Process a die and its children. */
10595
10596 static void
10597 process_die (struct die_info *die, struct dwarf2_cu *cu)
10598 {
10599 process_die_scope scope (die, cu);
10600
10601 switch (die->tag)
10602 {
10603 case DW_TAG_padding:
10604 break;
10605 case DW_TAG_compile_unit:
10606 case DW_TAG_partial_unit:
10607 read_file_scope (die, cu);
10608 break;
10609 case DW_TAG_type_unit:
10610 read_type_unit_scope (die, cu);
10611 break;
10612 case DW_TAG_subprogram:
10613 case DW_TAG_inlined_subroutine:
10614 read_func_scope (die, cu);
10615 break;
10616 case DW_TAG_lexical_block:
10617 case DW_TAG_try_block:
10618 case DW_TAG_catch_block:
10619 read_lexical_block_scope (die, cu);
10620 break;
10621 case DW_TAG_call_site:
10622 case DW_TAG_GNU_call_site:
10623 read_call_site_scope (die, cu);
10624 break;
10625 case DW_TAG_class_type:
10626 case DW_TAG_interface_type:
10627 case DW_TAG_structure_type:
10628 case DW_TAG_union_type:
10629 process_structure_scope (die, cu);
10630 break;
10631 case DW_TAG_enumeration_type:
10632 process_enumeration_scope (die, cu);
10633 break;
10634
10635 /* These dies have a type, but processing them does not create
10636 a symbol or recurse to process the children. Therefore we can
10637 read them on-demand through read_type_die. */
10638 case DW_TAG_subroutine_type:
10639 case DW_TAG_set_type:
10640 case DW_TAG_array_type:
10641 case DW_TAG_pointer_type:
10642 case DW_TAG_ptr_to_member_type:
10643 case DW_TAG_reference_type:
10644 case DW_TAG_rvalue_reference_type:
10645 case DW_TAG_string_type:
10646 break;
10647
10648 case DW_TAG_base_type:
10649 case DW_TAG_subrange_type:
10650 case DW_TAG_typedef:
10651 /* Add a typedef symbol for the type definition, if it has a
10652 DW_AT_name. */
10653 new_symbol (die, read_type_die (die, cu), cu);
10654 break;
10655 case DW_TAG_common_block:
10656 read_common_block (die, cu);
10657 break;
10658 case DW_TAG_common_inclusion:
10659 break;
10660 case DW_TAG_namespace:
10661 cu->processing_has_namespace_info = 1;
10662 read_namespace (die, cu);
10663 break;
10664 case DW_TAG_module:
10665 cu->processing_has_namespace_info = 1;
10666 read_module (die, cu);
10667 break;
10668 case DW_TAG_imported_declaration:
10669 cu->processing_has_namespace_info = 1;
10670 if (read_namespace_alias (die, cu))
10671 break;
10672 /* The declaration is not a global namespace alias: fall through. */
10673 case DW_TAG_imported_module:
10674 cu->processing_has_namespace_info = 1;
10675 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10676 || cu->language != language_fortran))
10677 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10678 dwarf_tag_name (die->tag));
10679 read_import_statement (die, cu);
10680 break;
10681
10682 case DW_TAG_imported_unit:
10683 process_imported_unit_die (die, cu);
10684 break;
10685
10686 case DW_TAG_variable:
10687 read_variable (die, cu);
10688 break;
10689
10690 default:
10691 new_symbol (die, NULL, cu);
10692 break;
10693 }
10694 }
10695 \f
10696 /* DWARF name computation. */
10697
10698 /* A helper function for dwarf2_compute_name which determines whether DIE
10699 needs to have the name of the scope prepended to the name listed in the
10700 die. */
10701
10702 static int
10703 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10704 {
10705 struct attribute *attr;
10706
10707 switch (die->tag)
10708 {
10709 case DW_TAG_namespace:
10710 case DW_TAG_typedef:
10711 case DW_TAG_class_type:
10712 case DW_TAG_interface_type:
10713 case DW_TAG_structure_type:
10714 case DW_TAG_union_type:
10715 case DW_TAG_enumeration_type:
10716 case DW_TAG_enumerator:
10717 case DW_TAG_subprogram:
10718 case DW_TAG_inlined_subroutine:
10719 case DW_TAG_member:
10720 case DW_TAG_imported_declaration:
10721 return 1;
10722
10723 case DW_TAG_variable:
10724 case DW_TAG_constant:
10725 /* We only need to prefix "globally" visible variables. These include
10726 any variable marked with DW_AT_external or any variable that
10727 lives in a namespace. [Variables in anonymous namespaces
10728 require prefixing, but they are not DW_AT_external.] */
10729
10730 if (dwarf2_attr (die, DW_AT_specification, cu))
10731 {
10732 struct dwarf2_cu *spec_cu = cu;
10733
10734 return die_needs_namespace (die_specification (die, &spec_cu),
10735 spec_cu);
10736 }
10737
10738 attr = dwarf2_attr (die, DW_AT_external, cu);
10739 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10740 && die->parent->tag != DW_TAG_module)
10741 return 0;
10742 /* A variable in a lexical block of some kind does not need a
10743 namespace, even though in C++ such variables may be external
10744 and have a mangled name. */
10745 if (die->parent->tag == DW_TAG_lexical_block
10746 || die->parent->tag == DW_TAG_try_block
10747 || die->parent->tag == DW_TAG_catch_block
10748 || die->parent->tag == DW_TAG_subprogram)
10749 return 0;
10750 return 1;
10751
10752 default:
10753 return 0;
10754 }
10755 }
10756
10757 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10758 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10759 defined for the given DIE. */
10760
10761 static struct attribute *
10762 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10763 {
10764 struct attribute *attr;
10765
10766 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10767 if (attr == NULL)
10768 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10769
10770 return attr;
10771 }
10772
10773 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10774 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10775 defined for the given DIE. */
10776
10777 static const char *
10778 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10779 {
10780 const char *linkage_name;
10781
10782 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10783 if (linkage_name == NULL)
10784 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10785
10786 return linkage_name;
10787 }
10788
10789 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10790 compute the physname for the object, which include a method's:
10791 - formal parameters (C++),
10792 - receiver type (Go),
10793
10794 The term "physname" is a bit confusing.
10795 For C++, for example, it is the demangled name.
10796 For Go, for example, it's the mangled name.
10797
10798 For Ada, return the DIE's linkage name rather than the fully qualified
10799 name. PHYSNAME is ignored..
10800
10801 The result is allocated on the objfile_obstack and canonicalized. */
10802
10803 static const char *
10804 dwarf2_compute_name (const char *name,
10805 struct die_info *die, struct dwarf2_cu *cu,
10806 int physname)
10807 {
10808 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10809
10810 if (name == NULL)
10811 name = dwarf2_name (die, cu);
10812
10813 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10814 but otherwise compute it by typename_concat inside GDB.
10815 FIXME: Actually this is not really true, or at least not always true.
10816 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10817 Fortran names because there is no mangling standard. So new_symbol
10818 will set the demangled name to the result of dwarf2_full_name, and it is
10819 the demangled name that GDB uses if it exists. */
10820 if (cu->language == language_ada
10821 || (cu->language == language_fortran && physname))
10822 {
10823 /* For Ada unit, we prefer the linkage name over the name, as
10824 the former contains the exported name, which the user expects
10825 to be able to reference. Ideally, we want the user to be able
10826 to reference this entity using either natural or linkage name,
10827 but we haven't started looking at this enhancement yet. */
10828 const char *linkage_name = dw2_linkage_name (die, cu);
10829
10830 if (linkage_name != NULL)
10831 return linkage_name;
10832 }
10833
10834 /* These are the only languages we know how to qualify names in. */
10835 if (name != NULL
10836 && (cu->language == language_cplus
10837 || cu->language == language_fortran || cu->language == language_d
10838 || cu->language == language_rust))
10839 {
10840 if (die_needs_namespace (die, cu))
10841 {
10842 const char *prefix;
10843 const char *canonical_name = NULL;
10844
10845 string_file buf;
10846
10847 prefix = determine_prefix (die, cu);
10848 if (*prefix != '\0')
10849 {
10850 char *prefixed_name = typename_concat (NULL, prefix, name,
10851 physname, cu);
10852
10853 buf.puts (prefixed_name);
10854 xfree (prefixed_name);
10855 }
10856 else
10857 buf.puts (name);
10858
10859 /* Template parameters may be specified in the DIE's DW_AT_name, or
10860 as children with DW_TAG_template_type_param or
10861 DW_TAG_value_type_param. If the latter, add them to the name
10862 here. If the name already has template parameters, then
10863 skip this step; some versions of GCC emit both, and
10864 it is more efficient to use the pre-computed name.
10865
10866 Something to keep in mind about this process: it is very
10867 unlikely, or in some cases downright impossible, to produce
10868 something that will match the mangled name of a function.
10869 If the definition of the function has the same debug info,
10870 we should be able to match up with it anyway. But fallbacks
10871 using the minimal symbol, for instance to find a method
10872 implemented in a stripped copy of libstdc++, will not work.
10873 If we do not have debug info for the definition, we will have to
10874 match them up some other way.
10875
10876 When we do name matching there is a related problem with function
10877 templates; two instantiated function templates are allowed to
10878 differ only by their return types, which we do not add here. */
10879
10880 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10881 {
10882 struct attribute *attr;
10883 struct die_info *child;
10884 int first = 1;
10885
10886 die->building_fullname = 1;
10887
10888 for (child = die->child; child != NULL; child = child->sibling)
10889 {
10890 struct type *type;
10891 LONGEST value;
10892 const gdb_byte *bytes;
10893 struct dwarf2_locexpr_baton *baton;
10894 struct value *v;
10895
10896 if (child->tag != DW_TAG_template_type_param
10897 && child->tag != DW_TAG_template_value_param)
10898 continue;
10899
10900 if (first)
10901 {
10902 buf.puts ("<");
10903 first = 0;
10904 }
10905 else
10906 buf.puts (", ");
10907
10908 attr = dwarf2_attr (child, DW_AT_type, cu);
10909 if (attr == NULL)
10910 {
10911 complaint (&symfile_complaints,
10912 _("template parameter missing DW_AT_type"));
10913 buf.puts ("UNKNOWN_TYPE");
10914 continue;
10915 }
10916 type = die_type (child, cu);
10917
10918 if (child->tag == DW_TAG_template_type_param)
10919 {
10920 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10921 continue;
10922 }
10923
10924 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10925 if (attr == NULL)
10926 {
10927 complaint (&symfile_complaints,
10928 _("template parameter missing "
10929 "DW_AT_const_value"));
10930 buf.puts ("UNKNOWN_VALUE");
10931 continue;
10932 }
10933
10934 dwarf2_const_value_attr (attr, type, name,
10935 &cu->comp_unit_obstack, cu,
10936 &value, &bytes, &baton);
10937
10938 if (TYPE_NOSIGN (type))
10939 /* GDB prints characters as NUMBER 'CHAR'. If that's
10940 changed, this can use value_print instead. */
10941 c_printchar (value, type, &buf);
10942 else
10943 {
10944 struct value_print_options opts;
10945
10946 if (baton != NULL)
10947 v = dwarf2_evaluate_loc_desc (type, NULL,
10948 baton->data,
10949 baton->size,
10950 baton->per_cu);
10951 else if (bytes != NULL)
10952 {
10953 v = allocate_value (type);
10954 memcpy (value_contents_writeable (v), bytes,
10955 TYPE_LENGTH (type));
10956 }
10957 else
10958 v = value_from_longest (type, value);
10959
10960 /* Specify decimal so that we do not depend on
10961 the radix. */
10962 get_formatted_print_options (&opts, 'd');
10963 opts.raw = 1;
10964 value_print (v, &buf, &opts);
10965 release_value (v);
10966 }
10967 }
10968
10969 die->building_fullname = 0;
10970
10971 if (!first)
10972 {
10973 /* Close the argument list, with a space if necessary
10974 (nested templates). */
10975 if (!buf.empty () && buf.string ().back () == '>')
10976 buf.puts (" >");
10977 else
10978 buf.puts (">");
10979 }
10980 }
10981
10982 /* For C++ methods, append formal parameter type
10983 information, if PHYSNAME. */
10984
10985 if (physname && die->tag == DW_TAG_subprogram
10986 && cu->language == language_cplus)
10987 {
10988 struct type *type = read_type_die (die, cu);
10989
10990 c_type_print_args (type, &buf, 1, cu->language,
10991 &type_print_raw_options);
10992
10993 if (cu->language == language_cplus)
10994 {
10995 /* Assume that an artificial first parameter is
10996 "this", but do not crash if it is not. RealView
10997 marks unnamed (and thus unused) parameters as
10998 artificial; there is no way to differentiate
10999 the two cases. */
11000 if (TYPE_NFIELDS (type) > 0
11001 && TYPE_FIELD_ARTIFICIAL (type, 0)
11002 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11003 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11004 0))))
11005 buf.puts (" const");
11006 }
11007 }
11008
11009 const std::string &intermediate_name = buf.string ();
11010
11011 if (cu->language == language_cplus)
11012 canonical_name
11013 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11014 &objfile->per_bfd->storage_obstack);
11015
11016 /* If we only computed INTERMEDIATE_NAME, or if
11017 INTERMEDIATE_NAME is already canonical, then we need to
11018 copy it to the appropriate obstack. */
11019 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11020 name = ((const char *)
11021 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11022 intermediate_name.c_str (),
11023 intermediate_name.length ()));
11024 else
11025 name = canonical_name;
11026 }
11027 }
11028
11029 return name;
11030 }
11031
11032 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11033 If scope qualifiers are appropriate they will be added. The result
11034 will be allocated on the storage_obstack, or NULL if the DIE does
11035 not have a name. NAME may either be from a previous call to
11036 dwarf2_name or NULL.
11037
11038 The output string will be canonicalized (if C++). */
11039
11040 static const char *
11041 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11042 {
11043 return dwarf2_compute_name (name, die, cu, 0);
11044 }
11045
11046 /* Construct a physname for the given DIE in CU. NAME may either be
11047 from a previous call to dwarf2_name or NULL. The result will be
11048 allocated on the objfile_objstack or NULL if the DIE does not have a
11049 name.
11050
11051 The output string will be canonicalized (if C++). */
11052
11053 static const char *
11054 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11055 {
11056 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11057 const char *retval, *mangled = NULL, *canon = NULL;
11058 int need_copy = 1;
11059
11060 /* In this case dwarf2_compute_name is just a shortcut not building anything
11061 on its own. */
11062 if (!die_needs_namespace (die, cu))
11063 return dwarf2_compute_name (name, die, cu, 1);
11064
11065 mangled = dw2_linkage_name (die, cu);
11066
11067 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11068 See https://github.com/rust-lang/rust/issues/32925. */
11069 if (cu->language == language_rust && mangled != NULL
11070 && strchr (mangled, '{') != NULL)
11071 mangled = NULL;
11072
11073 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11074 has computed. */
11075 gdb::unique_xmalloc_ptr<char> demangled;
11076 if (mangled != NULL)
11077 {
11078
11079 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11080 {
11081 /* Do nothing (do not demangle the symbol name). */
11082 }
11083 else if (cu->language == language_go)
11084 {
11085 /* This is a lie, but we already lie to the caller new_symbol.
11086 new_symbol assumes we return the mangled name.
11087 This just undoes that lie until things are cleaned up. */
11088 }
11089 else
11090 {
11091 /* Use DMGL_RET_DROP for C++ template functions to suppress
11092 their return type. It is easier for GDB users to search
11093 for such functions as `name(params)' than `long name(params)'.
11094 In such case the minimal symbol names do not match the full
11095 symbol names but for template functions there is never a need
11096 to look up their definition from their declaration so
11097 the only disadvantage remains the minimal symbol variant
11098 `long name(params)' does not have the proper inferior type. */
11099 demangled.reset (gdb_demangle (mangled,
11100 (DMGL_PARAMS | DMGL_ANSI
11101 | DMGL_RET_DROP)));
11102 }
11103 if (demangled)
11104 canon = demangled.get ();
11105 else
11106 {
11107 canon = mangled;
11108 need_copy = 0;
11109 }
11110 }
11111
11112 if (canon == NULL || check_physname)
11113 {
11114 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11115
11116 if (canon != NULL && strcmp (physname, canon) != 0)
11117 {
11118 /* It may not mean a bug in GDB. The compiler could also
11119 compute DW_AT_linkage_name incorrectly. But in such case
11120 GDB would need to be bug-to-bug compatible. */
11121
11122 complaint (&symfile_complaints,
11123 _("Computed physname <%s> does not match demangled <%s> "
11124 "(from linkage <%s>) - DIE at %s [in module %s]"),
11125 physname, canon, mangled, sect_offset_str (die->sect_off),
11126 objfile_name (objfile));
11127
11128 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11129 is available here - over computed PHYSNAME. It is safer
11130 against both buggy GDB and buggy compilers. */
11131
11132 retval = canon;
11133 }
11134 else
11135 {
11136 retval = physname;
11137 need_copy = 0;
11138 }
11139 }
11140 else
11141 retval = canon;
11142
11143 if (need_copy)
11144 retval = ((const char *)
11145 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11146 retval, strlen (retval)));
11147
11148 return retval;
11149 }
11150
11151 /* Inspect DIE in CU for a namespace alias. If one exists, record
11152 a new symbol for it.
11153
11154 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11155
11156 static int
11157 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11158 {
11159 struct attribute *attr;
11160
11161 /* If the die does not have a name, this is not a namespace
11162 alias. */
11163 attr = dwarf2_attr (die, DW_AT_name, cu);
11164 if (attr != NULL)
11165 {
11166 int num;
11167 struct die_info *d = die;
11168 struct dwarf2_cu *imported_cu = cu;
11169
11170 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11171 keep inspecting DIEs until we hit the underlying import. */
11172 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11173 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11174 {
11175 attr = dwarf2_attr (d, DW_AT_import, cu);
11176 if (attr == NULL)
11177 break;
11178
11179 d = follow_die_ref (d, attr, &imported_cu);
11180 if (d->tag != DW_TAG_imported_declaration)
11181 break;
11182 }
11183
11184 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11185 {
11186 complaint (&symfile_complaints,
11187 _("DIE at %s has too many recursively imported "
11188 "declarations"), sect_offset_str (d->sect_off));
11189 return 0;
11190 }
11191
11192 if (attr != NULL)
11193 {
11194 struct type *type;
11195 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11196
11197 type = get_die_type_at_offset (sect_off, cu->per_cu);
11198 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11199 {
11200 /* This declaration is a global namespace alias. Add
11201 a symbol for it whose type is the aliased namespace. */
11202 new_symbol (die, type, cu);
11203 return 1;
11204 }
11205 }
11206 }
11207
11208 return 0;
11209 }
11210
11211 /* Return the using directives repository (global or local?) to use in the
11212 current context for LANGUAGE.
11213
11214 For Ada, imported declarations can materialize renamings, which *may* be
11215 global. However it is impossible (for now?) in DWARF to distinguish
11216 "external" imported declarations and "static" ones. As all imported
11217 declarations seem to be static in all other languages, make them all CU-wide
11218 global only in Ada. */
11219
11220 static struct using_direct **
11221 using_directives (enum language language)
11222 {
11223 if (language == language_ada && context_stack_depth == 0)
11224 return &global_using_directives;
11225 else
11226 return &local_using_directives;
11227 }
11228
11229 /* Read the import statement specified by the given die and record it. */
11230
11231 static void
11232 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11233 {
11234 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11235 struct attribute *import_attr;
11236 struct die_info *imported_die, *child_die;
11237 struct dwarf2_cu *imported_cu;
11238 const char *imported_name;
11239 const char *imported_name_prefix;
11240 const char *canonical_name;
11241 const char *import_alias;
11242 const char *imported_declaration = NULL;
11243 const char *import_prefix;
11244 std::vector<const char *> excludes;
11245
11246 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11247 if (import_attr == NULL)
11248 {
11249 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11250 dwarf_tag_name (die->tag));
11251 return;
11252 }
11253
11254 imported_cu = cu;
11255 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11256 imported_name = dwarf2_name (imported_die, imported_cu);
11257 if (imported_name == NULL)
11258 {
11259 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11260
11261 The import in the following code:
11262 namespace A
11263 {
11264 typedef int B;
11265 }
11266
11267 int main ()
11268 {
11269 using A::B;
11270 B b;
11271 return b;
11272 }
11273
11274 ...
11275 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11276 <52> DW_AT_decl_file : 1
11277 <53> DW_AT_decl_line : 6
11278 <54> DW_AT_import : <0x75>
11279 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11280 <59> DW_AT_name : B
11281 <5b> DW_AT_decl_file : 1
11282 <5c> DW_AT_decl_line : 2
11283 <5d> DW_AT_type : <0x6e>
11284 ...
11285 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11286 <76> DW_AT_byte_size : 4
11287 <77> DW_AT_encoding : 5 (signed)
11288
11289 imports the wrong die ( 0x75 instead of 0x58 ).
11290 This case will be ignored until the gcc bug is fixed. */
11291 return;
11292 }
11293
11294 /* Figure out the local name after import. */
11295 import_alias = dwarf2_name (die, cu);
11296
11297 /* Figure out where the statement is being imported to. */
11298 import_prefix = determine_prefix (die, cu);
11299
11300 /* Figure out what the scope of the imported die is and prepend it
11301 to the name of the imported die. */
11302 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11303
11304 if (imported_die->tag != DW_TAG_namespace
11305 && imported_die->tag != DW_TAG_module)
11306 {
11307 imported_declaration = imported_name;
11308 canonical_name = imported_name_prefix;
11309 }
11310 else if (strlen (imported_name_prefix) > 0)
11311 canonical_name = obconcat (&objfile->objfile_obstack,
11312 imported_name_prefix,
11313 (cu->language == language_d ? "." : "::"),
11314 imported_name, (char *) NULL);
11315 else
11316 canonical_name = imported_name;
11317
11318 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11319 for (child_die = die->child; child_die && child_die->tag;
11320 child_die = sibling_die (child_die))
11321 {
11322 /* DWARF-4: A Fortran use statement with a “rename list” may be
11323 represented by an imported module entry with an import attribute
11324 referring to the module and owned entries corresponding to those
11325 entities that are renamed as part of being imported. */
11326
11327 if (child_die->tag != DW_TAG_imported_declaration)
11328 {
11329 complaint (&symfile_complaints,
11330 _("child DW_TAG_imported_declaration expected "
11331 "- DIE at %s [in module %s]"),
11332 sect_offset_str (child_die->sect_off),
11333 objfile_name (objfile));
11334 continue;
11335 }
11336
11337 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11338 if (import_attr == NULL)
11339 {
11340 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11341 dwarf_tag_name (child_die->tag));
11342 continue;
11343 }
11344
11345 imported_cu = cu;
11346 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11347 &imported_cu);
11348 imported_name = dwarf2_name (imported_die, imported_cu);
11349 if (imported_name == NULL)
11350 {
11351 complaint (&symfile_complaints,
11352 _("child DW_TAG_imported_declaration has unknown "
11353 "imported name - DIE at %s [in module %s]"),
11354 sect_offset_str (child_die->sect_off),
11355 objfile_name (objfile));
11356 continue;
11357 }
11358
11359 excludes.push_back (imported_name);
11360
11361 process_die (child_die, cu);
11362 }
11363
11364 add_using_directive (using_directives (cu->language),
11365 import_prefix,
11366 canonical_name,
11367 import_alias,
11368 imported_declaration,
11369 excludes,
11370 0,
11371 &objfile->objfile_obstack);
11372 }
11373
11374 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11375 types, but gives them a size of zero. Starting with version 14,
11376 ICC is compatible with GCC. */
11377
11378 static int
11379 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11380 {
11381 if (!cu->checked_producer)
11382 check_producer (cu);
11383
11384 return cu->producer_is_icc_lt_14;
11385 }
11386
11387 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11388 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11389 this, it was first present in GCC release 4.3.0. */
11390
11391 static int
11392 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11393 {
11394 if (!cu->checked_producer)
11395 check_producer (cu);
11396
11397 return cu->producer_is_gcc_lt_4_3;
11398 }
11399
11400 static file_and_directory
11401 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11402 {
11403 file_and_directory res;
11404
11405 /* Find the filename. Do not use dwarf2_name here, since the filename
11406 is not a source language identifier. */
11407 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11408 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11409
11410 if (res.comp_dir == NULL
11411 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11412 && IS_ABSOLUTE_PATH (res.name))
11413 {
11414 res.comp_dir_storage = ldirname (res.name);
11415 if (!res.comp_dir_storage.empty ())
11416 res.comp_dir = res.comp_dir_storage.c_str ();
11417 }
11418 if (res.comp_dir != NULL)
11419 {
11420 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11421 directory, get rid of it. */
11422 const char *cp = strchr (res.comp_dir, ':');
11423
11424 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11425 res.comp_dir = cp + 1;
11426 }
11427
11428 if (res.name == NULL)
11429 res.name = "<unknown>";
11430
11431 return res;
11432 }
11433
11434 /* Handle DW_AT_stmt_list for a compilation unit.
11435 DIE is the DW_TAG_compile_unit die for CU.
11436 COMP_DIR is the compilation directory. LOWPC is passed to
11437 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11438
11439 static void
11440 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11441 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11442 {
11443 struct dwarf2_per_objfile *dwarf2_per_objfile
11444 = cu->per_cu->dwarf2_per_objfile;
11445 struct objfile *objfile = dwarf2_per_objfile->objfile;
11446 struct attribute *attr;
11447 struct line_header line_header_local;
11448 hashval_t line_header_local_hash;
11449 void **slot;
11450 int decode_mapping;
11451
11452 gdb_assert (! cu->per_cu->is_debug_types);
11453
11454 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11455 if (attr == NULL)
11456 return;
11457
11458 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11459
11460 /* The line header hash table is only created if needed (it exists to
11461 prevent redundant reading of the line table for partial_units).
11462 If we're given a partial_unit, we'll need it. If we're given a
11463 compile_unit, then use the line header hash table if it's already
11464 created, but don't create one just yet. */
11465
11466 if (dwarf2_per_objfile->line_header_hash == NULL
11467 && die->tag == DW_TAG_partial_unit)
11468 {
11469 dwarf2_per_objfile->line_header_hash
11470 = htab_create_alloc_ex (127, line_header_hash_voidp,
11471 line_header_eq_voidp,
11472 free_line_header_voidp,
11473 &objfile->objfile_obstack,
11474 hashtab_obstack_allocate,
11475 dummy_obstack_deallocate);
11476 }
11477
11478 line_header_local.sect_off = line_offset;
11479 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11480 line_header_local_hash = line_header_hash (&line_header_local);
11481 if (dwarf2_per_objfile->line_header_hash != NULL)
11482 {
11483 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11484 &line_header_local,
11485 line_header_local_hash, NO_INSERT);
11486
11487 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11488 is not present in *SLOT (since if there is something in *SLOT then
11489 it will be for a partial_unit). */
11490 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11491 {
11492 gdb_assert (*slot != NULL);
11493 cu->line_header = (struct line_header *) *slot;
11494 return;
11495 }
11496 }
11497
11498 /* dwarf_decode_line_header does not yet provide sufficient information.
11499 We always have to call also dwarf_decode_lines for it. */
11500 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11501 if (lh == NULL)
11502 return;
11503
11504 cu->line_header = lh.release ();
11505 cu->line_header_die_owner = die;
11506
11507 if (dwarf2_per_objfile->line_header_hash == NULL)
11508 slot = NULL;
11509 else
11510 {
11511 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11512 &line_header_local,
11513 line_header_local_hash, INSERT);
11514 gdb_assert (slot != NULL);
11515 }
11516 if (slot != NULL && *slot == NULL)
11517 {
11518 /* This newly decoded line number information unit will be owned
11519 by line_header_hash hash table. */
11520 *slot = cu->line_header;
11521 cu->line_header_die_owner = NULL;
11522 }
11523 else
11524 {
11525 /* We cannot free any current entry in (*slot) as that struct line_header
11526 may be already used by multiple CUs. Create only temporary decoded
11527 line_header for this CU - it may happen at most once for each line
11528 number information unit. And if we're not using line_header_hash
11529 then this is what we want as well. */
11530 gdb_assert (die->tag != DW_TAG_partial_unit);
11531 }
11532 decode_mapping = (die->tag != DW_TAG_partial_unit);
11533 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11534 decode_mapping);
11535
11536 }
11537
11538 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11539
11540 static void
11541 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11542 {
11543 struct dwarf2_per_objfile *dwarf2_per_objfile
11544 = cu->per_cu->dwarf2_per_objfile;
11545 struct objfile *objfile = dwarf2_per_objfile->objfile;
11546 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11547 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11548 CORE_ADDR highpc = ((CORE_ADDR) 0);
11549 struct attribute *attr;
11550 struct die_info *child_die;
11551 CORE_ADDR baseaddr;
11552
11553 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11554
11555 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11556
11557 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11558 from finish_block. */
11559 if (lowpc == ((CORE_ADDR) -1))
11560 lowpc = highpc;
11561 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11562
11563 file_and_directory fnd = find_file_and_directory (die, cu);
11564
11565 prepare_one_comp_unit (cu, die, cu->language);
11566
11567 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11568 standardised yet. As a workaround for the language detection we fall
11569 back to the DW_AT_producer string. */
11570 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11571 cu->language = language_opencl;
11572
11573 /* Similar hack for Go. */
11574 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11575 set_cu_language (DW_LANG_Go, cu);
11576
11577 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11578
11579 /* Decode line number information if present. We do this before
11580 processing child DIEs, so that the line header table is available
11581 for DW_AT_decl_file. */
11582 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11583
11584 /* Process all dies in compilation unit. */
11585 if (die->child != NULL)
11586 {
11587 child_die = die->child;
11588 while (child_die && child_die->tag)
11589 {
11590 process_die (child_die, cu);
11591 child_die = sibling_die (child_die);
11592 }
11593 }
11594
11595 /* Decode macro information, if present. Dwarf 2 macro information
11596 refers to information in the line number info statement program
11597 header, so we can only read it if we've read the header
11598 successfully. */
11599 attr = dwarf2_attr (die, DW_AT_macros, cu);
11600 if (attr == NULL)
11601 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11602 if (attr && cu->line_header)
11603 {
11604 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11605 complaint (&symfile_complaints,
11606 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11607
11608 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11609 }
11610 else
11611 {
11612 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11613 if (attr && cu->line_header)
11614 {
11615 unsigned int macro_offset = DW_UNSND (attr);
11616
11617 dwarf_decode_macros (cu, macro_offset, 0);
11618 }
11619 }
11620 }
11621
11622 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11623 Create the set of symtabs used by this TU, or if this TU is sharing
11624 symtabs with another TU and the symtabs have already been created
11625 then restore those symtabs in the line header.
11626 We don't need the pc/line-number mapping for type units. */
11627
11628 static void
11629 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11630 {
11631 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11632 struct type_unit_group *tu_group;
11633 int first_time;
11634 struct attribute *attr;
11635 unsigned int i;
11636 struct signatured_type *sig_type;
11637
11638 gdb_assert (per_cu->is_debug_types);
11639 sig_type = (struct signatured_type *) per_cu;
11640
11641 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11642
11643 /* If we're using .gdb_index (includes -readnow) then
11644 per_cu->type_unit_group may not have been set up yet. */
11645 if (sig_type->type_unit_group == NULL)
11646 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11647 tu_group = sig_type->type_unit_group;
11648
11649 /* If we've already processed this stmt_list there's no real need to
11650 do it again, we could fake it and just recreate the part we need
11651 (file name,index -> symtab mapping). If data shows this optimization
11652 is useful we can do it then. */
11653 first_time = tu_group->compunit_symtab == NULL;
11654
11655 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11656 debug info. */
11657 line_header_up lh;
11658 if (attr != NULL)
11659 {
11660 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11661 lh = dwarf_decode_line_header (line_offset, cu);
11662 }
11663 if (lh == NULL)
11664 {
11665 if (first_time)
11666 dwarf2_start_symtab (cu, "", NULL, 0);
11667 else
11668 {
11669 gdb_assert (tu_group->symtabs == NULL);
11670 restart_symtab (tu_group->compunit_symtab, "", 0);
11671 }
11672 return;
11673 }
11674
11675 cu->line_header = lh.release ();
11676 cu->line_header_die_owner = die;
11677
11678 if (first_time)
11679 {
11680 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11681
11682 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11683 still initializing it, and our caller (a few levels up)
11684 process_full_type_unit still needs to know if this is the first
11685 time. */
11686
11687 tu_group->num_symtabs = cu->line_header->file_names.size ();
11688 tu_group->symtabs = XNEWVEC (struct symtab *,
11689 cu->line_header->file_names.size ());
11690
11691 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11692 {
11693 file_entry &fe = cu->line_header->file_names[i];
11694
11695 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11696
11697 if (current_subfile->symtab == NULL)
11698 {
11699 /* NOTE: start_subfile will recognize when it's been
11700 passed a file it has already seen. So we can't
11701 assume there's a simple mapping from
11702 cu->line_header->file_names to subfiles, plus
11703 cu->line_header->file_names may contain dups. */
11704 current_subfile->symtab
11705 = allocate_symtab (cust, current_subfile->name);
11706 }
11707
11708 fe.symtab = current_subfile->symtab;
11709 tu_group->symtabs[i] = fe.symtab;
11710 }
11711 }
11712 else
11713 {
11714 restart_symtab (tu_group->compunit_symtab, "", 0);
11715
11716 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11717 {
11718 file_entry &fe = cu->line_header->file_names[i];
11719
11720 fe.symtab = tu_group->symtabs[i];
11721 }
11722 }
11723
11724 /* The main symtab is allocated last. Type units don't have DW_AT_name
11725 so they don't have a "real" (so to speak) symtab anyway.
11726 There is later code that will assign the main symtab to all symbols
11727 that don't have one. We need to handle the case of a symbol with a
11728 missing symtab (DW_AT_decl_file) anyway. */
11729 }
11730
11731 /* Process DW_TAG_type_unit.
11732 For TUs we want to skip the first top level sibling if it's not the
11733 actual type being defined by this TU. In this case the first top
11734 level sibling is there to provide context only. */
11735
11736 static void
11737 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11738 {
11739 struct die_info *child_die;
11740
11741 prepare_one_comp_unit (cu, die, language_minimal);
11742
11743 /* Initialize (or reinitialize) the machinery for building symtabs.
11744 We do this before processing child DIEs, so that the line header table
11745 is available for DW_AT_decl_file. */
11746 setup_type_unit_groups (die, cu);
11747
11748 if (die->child != NULL)
11749 {
11750 child_die = die->child;
11751 while (child_die && child_die->tag)
11752 {
11753 process_die (child_die, cu);
11754 child_die = sibling_die (child_die);
11755 }
11756 }
11757 }
11758 \f
11759 /* DWO/DWP files.
11760
11761 http://gcc.gnu.org/wiki/DebugFission
11762 http://gcc.gnu.org/wiki/DebugFissionDWP
11763
11764 To simplify handling of both DWO files ("object" files with the DWARF info)
11765 and DWP files (a file with the DWOs packaged up into one file), we treat
11766 DWP files as having a collection of virtual DWO files. */
11767
11768 static hashval_t
11769 hash_dwo_file (const void *item)
11770 {
11771 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11772 hashval_t hash;
11773
11774 hash = htab_hash_string (dwo_file->dwo_name);
11775 if (dwo_file->comp_dir != NULL)
11776 hash += htab_hash_string (dwo_file->comp_dir);
11777 return hash;
11778 }
11779
11780 static int
11781 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11782 {
11783 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11784 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11785
11786 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11787 return 0;
11788 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11789 return lhs->comp_dir == rhs->comp_dir;
11790 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11791 }
11792
11793 /* Allocate a hash table for DWO files. */
11794
11795 static htab_t
11796 allocate_dwo_file_hash_table (struct objfile *objfile)
11797 {
11798 return htab_create_alloc_ex (41,
11799 hash_dwo_file,
11800 eq_dwo_file,
11801 NULL,
11802 &objfile->objfile_obstack,
11803 hashtab_obstack_allocate,
11804 dummy_obstack_deallocate);
11805 }
11806
11807 /* Lookup DWO file DWO_NAME. */
11808
11809 static void **
11810 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11811 const char *dwo_name,
11812 const char *comp_dir)
11813 {
11814 struct dwo_file find_entry;
11815 void **slot;
11816
11817 if (dwarf2_per_objfile->dwo_files == NULL)
11818 dwarf2_per_objfile->dwo_files
11819 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11820
11821 memset (&find_entry, 0, sizeof (find_entry));
11822 find_entry.dwo_name = dwo_name;
11823 find_entry.comp_dir = comp_dir;
11824 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11825
11826 return slot;
11827 }
11828
11829 static hashval_t
11830 hash_dwo_unit (const void *item)
11831 {
11832 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11833
11834 /* This drops the top 32 bits of the id, but is ok for a hash. */
11835 return dwo_unit->signature;
11836 }
11837
11838 static int
11839 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11840 {
11841 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11842 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11843
11844 /* The signature is assumed to be unique within the DWO file.
11845 So while object file CU dwo_id's always have the value zero,
11846 that's OK, assuming each object file DWO file has only one CU,
11847 and that's the rule for now. */
11848 return lhs->signature == rhs->signature;
11849 }
11850
11851 /* Allocate a hash table for DWO CUs,TUs.
11852 There is one of these tables for each of CUs,TUs for each DWO file. */
11853
11854 static htab_t
11855 allocate_dwo_unit_table (struct objfile *objfile)
11856 {
11857 /* Start out with a pretty small number.
11858 Generally DWO files contain only one CU and maybe some TUs. */
11859 return htab_create_alloc_ex (3,
11860 hash_dwo_unit,
11861 eq_dwo_unit,
11862 NULL,
11863 &objfile->objfile_obstack,
11864 hashtab_obstack_allocate,
11865 dummy_obstack_deallocate);
11866 }
11867
11868 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11869
11870 struct create_dwo_cu_data
11871 {
11872 struct dwo_file *dwo_file;
11873 struct dwo_unit dwo_unit;
11874 };
11875
11876 /* die_reader_func for create_dwo_cu. */
11877
11878 static void
11879 create_dwo_cu_reader (const struct die_reader_specs *reader,
11880 const gdb_byte *info_ptr,
11881 struct die_info *comp_unit_die,
11882 int has_children,
11883 void *datap)
11884 {
11885 struct dwarf2_cu *cu = reader->cu;
11886 sect_offset sect_off = cu->per_cu->sect_off;
11887 struct dwarf2_section_info *section = cu->per_cu->section;
11888 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11889 struct dwo_file *dwo_file = data->dwo_file;
11890 struct dwo_unit *dwo_unit = &data->dwo_unit;
11891 struct attribute *attr;
11892
11893 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11894 if (attr == NULL)
11895 {
11896 complaint (&symfile_complaints,
11897 _("Dwarf Error: debug entry at offset %s is missing"
11898 " its dwo_id [in module %s]"),
11899 sect_offset_str (sect_off), dwo_file->dwo_name);
11900 return;
11901 }
11902
11903 dwo_unit->dwo_file = dwo_file;
11904 dwo_unit->signature = DW_UNSND (attr);
11905 dwo_unit->section = section;
11906 dwo_unit->sect_off = sect_off;
11907 dwo_unit->length = cu->per_cu->length;
11908
11909 if (dwarf_read_debug)
11910 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11911 sect_offset_str (sect_off),
11912 hex_string (dwo_unit->signature));
11913 }
11914
11915 /* Create the dwo_units for the CUs in a DWO_FILE.
11916 Note: This function processes DWO files only, not DWP files. */
11917
11918 static void
11919 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11920 struct dwo_file &dwo_file, dwarf2_section_info &section,
11921 htab_t &cus_htab)
11922 {
11923 struct objfile *objfile = dwarf2_per_objfile->objfile;
11924 const gdb_byte *info_ptr, *end_ptr;
11925
11926 dwarf2_read_section (objfile, &section);
11927 info_ptr = section.buffer;
11928
11929 if (info_ptr == NULL)
11930 return;
11931
11932 if (dwarf_read_debug)
11933 {
11934 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11935 get_section_name (&section),
11936 get_section_file_name (&section));
11937 }
11938
11939 end_ptr = info_ptr + section.size;
11940 while (info_ptr < end_ptr)
11941 {
11942 struct dwarf2_per_cu_data per_cu;
11943 struct create_dwo_cu_data create_dwo_cu_data;
11944 struct dwo_unit *dwo_unit;
11945 void **slot;
11946 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11947
11948 memset (&create_dwo_cu_data.dwo_unit, 0,
11949 sizeof (create_dwo_cu_data.dwo_unit));
11950 memset (&per_cu, 0, sizeof (per_cu));
11951 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11952 per_cu.is_debug_types = 0;
11953 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11954 per_cu.section = &section;
11955 create_dwo_cu_data.dwo_file = &dwo_file;
11956
11957 init_cutu_and_read_dies_no_follow (
11958 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11959 info_ptr += per_cu.length;
11960
11961 // If the unit could not be parsed, skip it.
11962 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11963 continue;
11964
11965 if (cus_htab == NULL)
11966 cus_htab = allocate_dwo_unit_table (objfile);
11967
11968 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11969 *dwo_unit = create_dwo_cu_data.dwo_unit;
11970 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11971 gdb_assert (slot != NULL);
11972 if (*slot != NULL)
11973 {
11974 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11975 sect_offset dup_sect_off = dup_cu->sect_off;
11976
11977 complaint (&symfile_complaints,
11978 _("debug cu entry at offset %s is duplicate to"
11979 " the entry at offset %s, signature %s"),
11980 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11981 hex_string (dwo_unit->signature));
11982 }
11983 *slot = (void *)dwo_unit;
11984 }
11985 }
11986
11987 /* DWP file .debug_{cu,tu}_index section format:
11988 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11989
11990 DWP Version 1:
11991
11992 Both index sections have the same format, and serve to map a 64-bit
11993 signature to a set of section numbers. Each section begins with a header,
11994 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11995 indexes, and a pool of 32-bit section numbers. The index sections will be
11996 aligned at 8-byte boundaries in the file.
11997
11998 The index section header consists of:
11999
12000 V, 32 bit version number
12001 -, 32 bits unused
12002 N, 32 bit number of compilation units or type units in the index
12003 M, 32 bit number of slots in the hash table
12004
12005 Numbers are recorded using the byte order of the application binary.
12006
12007 The hash table begins at offset 16 in the section, and consists of an array
12008 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12009 order of the application binary). Unused slots in the hash table are 0.
12010 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12011
12012 The parallel table begins immediately after the hash table
12013 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12014 array of 32-bit indexes (using the byte order of the application binary),
12015 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12016 table contains a 32-bit index into the pool of section numbers. For unused
12017 hash table slots, the corresponding entry in the parallel table will be 0.
12018
12019 The pool of section numbers begins immediately following the hash table
12020 (at offset 16 + 12 * M from the beginning of the section). The pool of
12021 section numbers consists of an array of 32-bit words (using the byte order
12022 of the application binary). Each item in the array is indexed starting
12023 from 0. The hash table entry provides the index of the first section
12024 number in the set. Additional section numbers in the set follow, and the
12025 set is terminated by a 0 entry (section number 0 is not used in ELF).
12026
12027 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12028 section must be the first entry in the set, and the .debug_abbrev.dwo must
12029 be the second entry. Other members of the set may follow in any order.
12030
12031 ---
12032
12033 DWP Version 2:
12034
12035 DWP Version 2 combines all the .debug_info, etc. sections into one,
12036 and the entries in the index tables are now offsets into these sections.
12037 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12038 section.
12039
12040 Index Section Contents:
12041 Header
12042 Hash Table of Signatures dwp_hash_table.hash_table
12043 Parallel Table of Indices dwp_hash_table.unit_table
12044 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12045 Table of Section Sizes dwp_hash_table.v2.sizes
12046
12047 The index section header consists of:
12048
12049 V, 32 bit version number
12050 L, 32 bit number of columns in the table of section offsets
12051 N, 32 bit number of compilation units or type units in the index
12052 M, 32 bit number of slots in the hash table
12053
12054 Numbers are recorded using the byte order of the application binary.
12055
12056 The hash table has the same format as version 1.
12057 The parallel table of indices has the same format as version 1,
12058 except that the entries are origin-1 indices into the table of sections
12059 offsets and the table of section sizes.
12060
12061 The table of offsets begins immediately following the parallel table
12062 (at offset 16 + 12 * M from the beginning of the section). The table is
12063 a two-dimensional array of 32-bit words (using the byte order of the
12064 application binary), with L columns and N+1 rows, in row-major order.
12065 Each row in the array is indexed starting from 0. The first row provides
12066 a key to the remaining rows: each column in this row provides an identifier
12067 for a debug section, and the offsets in the same column of subsequent rows
12068 refer to that section. The section identifiers are:
12069
12070 DW_SECT_INFO 1 .debug_info.dwo
12071 DW_SECT_TYPES 2 .debug_types.dwo
12072 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12073 DW_SECT_LINE 4 .debug_line.dwo
12074 DW_SECT_LOC 5 .debug_loc.dwo
12075 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12076 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12077 DW_SECT_MACRO 8 .debug_macro.dwo
12078
12079 The offsets provided by the CU and TU index sections are the base offsets
12080 for the contributions made by each CU or TU to the corresponding section
12081 in the package file. Each CU and TU header contains an abbrev_offset
12082 field, used to find the abbreviations table for that CU or TU within the
12083 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12084 be interpreted as relative to the base offset given in the index section.
12085 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12086 should be interpreted as relative to the base offset for .debug_line.dwo,
12087 and offsets into other debug sections obtained from DWARF attributes should
12088 also be interpreted as relative to the corresponding base offset.
12089
12090 The table of sizes begins immediately following the table of offsets.
12091 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12092 with L columns and N rows, in row-major order. Each row in the array is
12093 indexed starting from 1 (row 0 is shared by the two tables).
12094
12095 ---
12096
12097 Hash table lookup is handled the same in version 1 and 2:
12098
12099 We assume that N and M will not exceed 2^32 - 1.
12100 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12101
12102 Given a 64-bit compilation unit signature or a type signature S, an entry
12103 in the hash table is located as follows:
12104
12105 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12106 the low-order k bits all set to 1.
12107
12108 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12109
12110 3) If the hash table entry at index H matches the signature, use that
12111 entry. If the hash table entry at index H is unused (all zeroes),
12112 terminate the search: the signature is not present in the table.
12113
12114 4) Let H = (H + H') modulo M. Repeat at Step 3.
12115
12116 Because M > N and H' and M are relatively prime, the search is guaranteed
12117 to stop at an unused slot or find the match. */
12118
12119 /* Create a hash table to map DWO IDs to their CU/TU entry in
12120 .debug_{info,types}.dwo in DWP_FILE.
12121 Returns NULL if there isn't one.
12122 Note: This function processes DWP files only, not DWO files. */
12123
12124 static struct dwp_hash_table *
12125 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12126 struct dwp_file *dwp_file, int is_debug_types)
12127 {
12128 struct objfile *objfile = dwarf2_per_objfile->objfile;
12129 bfd *dbfd = dwp_file->dbfd;
12130 const gdb_byte *index_ptr, *index_end;
12131 struct dwarf2_section_info *index;
12132 uint32_t version, nr_columns, nr_units, nr_slots;
12133 struct dwp_hash_table *htab;
12134
12135 if (is_debug_types)
12136 index = &dwp_file->sections.tu_index;
12137 else
12138 index = &dwp_file->sections.cu_index;
12139
12140 if (dwarf2_section_empty_p (index))
12141 return NULL;
12142 dwarf2_read_section (objfile, index);
12143
12144 index_ptr = index->buffer;
12145 index_end = index_ptr + index->size;
12146
12147 version = read_4_bytes (dbfd, index_ptr);
12148 index_ptr += 4;
12149 if (version == 2)
12150 nr_columns = read_4_bytes (dbfd, index_ptr);
12151 else
12152 nr_columns = 0;
12153 index_ptr += 4;
12154 nr_units = read_4_bytes (dbfd, index_ptr);
12155 index_ptr += 4;
12156 nr_slots = read_4_bytes (dbfd, index_ptr);
12157 index_ptr += 4;
12158
12159 if (version != 1 && version != 2)
12160 {
12161 error (_("Dwarf Error: unsupported DWP file version (%s)"
12162 " [in module %s]"),
12163 pulongest (version), dwp_file->name);
12164 }
12165 if (nr_slots != (nr_slots & -nr_slots))
12166 {
12167 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12168 " is not power of 2 [in module %s]"),
12169 pulongest (nr_slots), dwp_file->name);
12170 }
12171
12172 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12173 htab->version = version;
12174 htab->nr_columns = nr_columns;
12175 htab->nr_units = nr_units;
12176 htab->nr_slots = nr_slots;
12177 htab->hash_table = index_ptr;
12178 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12179
12180 /* Exit early if the table is empty. */
12181 if (nr_slots == 0 || nr_units == 0
12182 || (version == 2 && nr_columns == 0))
12183 {
12184 /* All must be zero. */
12185 if (nr_slots != 0 || nr_units != 0
12186 || (version == 2 && nr_columns != 0))
12187 {
12188 complaint (&symfile_complaints,
12189 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12190 " all zero [in modules %s]"),
12191 dwp_file->name);
12192 }
12193 return htab;
12194 }
12195
12196 if (version == 1)
12197 {
12198 htab->section_pool.v1.indices =
12199 htab->unit_table + sizeof (uint32_t) * nr_slots;
12200 /* It's harder to decide whether the section is too small in v1.
12201 V1 is deprecated anyway so we punt. */
12202 }
12203 else
12204 {
12205 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12206 int *ids = htab->section_pool.v2.section_ids;
12207 /* Reverse map for error checking. */
12208 int ids_seen[DW_SECT_MAX + 1];
12209 int i;
12210
12211 if (nr_columns < 2)
12212 {
12213 error (_("Dwarf Error: bad DWP hash table, too few columns"
12214 " in section table [in module %s]"),
12215 dwp_file->name);
12216 }
12217 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12218 {
12219 error (_("Dwarf Error: bad DWP hash table, too many columns"
12220 " in section table [in module %s]"),
12221 dwp_file->name);
12222 }
12223 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12224 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12225 for (i = 0; i < nr_columns; ++i)
12226 {
12227 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12228
12229 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12230 {
12231 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12232 " in section table [in module %s]"),
12233 id, dwp_file->name);
12234 }
12235 if (ids_seen[id] != -1)
12236 {
12237 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12238 " id %d in section table [in module %s]"),
12239 id, dwp_file->name);
12240 }
12241 ids_seen[id] = i;
12242 ids[i] = id;
12243 }
12244 /* Must have exactly one info or types section. */
12245 if (((ids_seen[DW_SECT_INFO] != -1)
12246 + (ids_seen[DW_SECT_TYPES] != -1))
12247 != 1)
12248 {
12249 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12250 " DWO info/types section [in module %s]"),
12251 dwp_file->name);
12252 }
12253 /* Must have an abbrev section. */
12254 if (ids_seen[DW_SECT_ABBREV] == -1)
12255 {
12256 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12257 " section [in module %s]"),
12258 dwp_file->name);
12259 }
12260 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12261 htab->section_pool.v2.sizes =
12262 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12263 * nr_units * nr_columns);
12264 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12265 * nr_units * nr_columns))
12266 > index_end)
12267 {
12268 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12269 " [in module %s]"),
12270 dwp_file->name);
12271 }
12272 }
12273
12274 return htab;
12275 }
12276
12277 /* Update SECTIONS with the data from SECTP.
12278
12279 This function is like the other "locate" section routines that are
12280 passed to bfd_map_over_sections, but in this context the sections to
12281 read comes from the DWP V1 hash table, not the full ELF section table.
12282
12283 The result is non-zero for success, or zero if an error was found. */
12284
12285 static int
12286 locate_v1_virtual_dwo_sections (asection *sectp,
12287 struct virtual_v1_dwo_sections *sections)
12288 {
12289 const struct dwop_section_names *names = &dwop_section_names;
12290
12291 if (section_is_p (sectp->name, &names->abbrev_dwo))
12292 {
12293 /* There can be only one. */
12294 if (sections->abbrev.s.section != NULL)
12295 return 0;
12296 sections->abbrev.s.section = sectp;
12297 sections->abbrev.size = bfd_get_section_size (sectp);
12298 }
12299 else if (section_is_p (sectp->name, &names->info_dwo)
12300 || section_is_p (sectp->name, &names->types_dwo))
12301 {
12302 /* There can be only one. */
12303 if (sections->info_or_types.s.section != NULL)
12304 return 0;
12305 sections->info_or_types.s.section = sectp;
12306 sections->info_or_types.size = bfd_get_section_size (sectp);
12307 }
12308 else if (section_is_p (sectp->name, &names->line_dwo))
12309 {
12310 /* There can be only one. */
12311 if (sections->line.s.section != NULL)
12312 return 0;
12313 sections->line.s.section = sectp;
12314 sections->line.size = bfd_get_section_size (sectp);
12315 }
12316 else if (section_is_p (sectp->name, &names->loc_dwo))
12317 {
12318 /* There can be only one. */
12319 if (sections->loc.s.section != NULL)
12320 return 0;
12321 sections->loc.s.section = sectp;
12322 sections->loc.size = bfd_get_section_size (sectp);
12323 }
12324 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12325 {
12326 /* There can be only one. */
12327 if (sections->macinfo.s.section != NULL)
12328 return 0;
12329 sections->macinfo.s.section = sectp;
12330 sections->macinfo.size = bfd_get_section_size (sectp);
12331 }
12332 else if (section_is_p (sectp->name, &names->macro_dwo))
12333 {
12334 /* There can be only one. */
12335 if (sections->macro.s.section != NULL)
12336 return 0;
12337 sections->macro.s.section = sectp;
12338 sections->macro.size = bfd_get_section_size (sectp);
12339 }
12340 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12341 {
12342 /* There can be only one. */
12343 if (sections->str_offsets.s.section != NULL)
12344 return 0;
12345 sections->str_offsets.s.section = sectp;
12346 sections->str_offsets.size = bfd_get_section_size (sectp);
12347 }
12348 else
12349 {
12350 /* No other kind of section is valid. */
12351 return 0;
12352 }
12353
12354 return 1;
12355 }
12356
12357 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12358 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12359 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12360 This is for DWP version 1 files. */
12361
12362 static struct dwo_unit *
12363 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12364 struct dwp_file *dwp_file,
12365 uint32_t unit_index,
12366 const char *comp_dir,
12367 ULONGEST signature, int is_debug_types)
12368 {
12369 struct objfile *objfile = dwarf2_per_objfile->objfile;
12370 const struct dwp_hash_table *dwp_htab =
12371 is_debug_types ? dwp_file->tus : dwp_file->cus;
12372 bfd *dbfd = dwp_file->dbfd;
12373 const char *kind = is_debug_types ? "TU" : "CU";
12374 struct dwo_file *dwo_file;
12375 struct dwo_unit *dwo_unit;
12376 struct virtual_v1_dwo_sections sections;
12377 void **dwo_file_slot;
12378 int i;
12379
12380 gdb_assert (dwp_file->version == 1);
12381
12382 if (dwarf_read_debug)
12383 {
12384 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12385 kind,
12386 pulongest (unit_index), hex_string (signature),
12387 dwp_file->name);
12388 }
12389
12390 /* Fetch the sections of this DWO unit.
12391 Put a limit on the number of sections we look for so that bad data
12392 doesn't cause us to loop forever. */
12393
12394 #define MAX_NR_V1_DWO_SECTIONS \
12395 (1 /* .debug_info or .debug_types */ \
12396 + 1 /* .debug_abbrev */ \
12397 + 1 /* .debug_line */ \
12398 + 1 /* .debug_loc */ \
12399 + 1 /* .debug_str_offsets */ \
12400 + 1 /* .debug_macro or .debug_macinfo */ \
12401 + 1 /* trailing zero */)
12402
12403 memset (&sections, 0, sizeof (sections));
12404
12405 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12406 {
12407 asection *sectp;
12408 uint32_t section_nr =
12409 read_4_bytes (dbfd,
12410 dwp_htab->section_pool.v1.indices
12411 + (unit_index + i) * sizeof (uint32_t));
12412
12413 if (section_nr == 0)
12414 break;
12415 if (section_nr >= dwp_file->num_sections)
12416 {
12417 error (_("Dwarf Error: bad DWP hash table, section number too large"
12418 " [in module %s]"),
12419 dwp_file->name);
12420 }
12421
12422 sectp = dwp_file->elf_sections[section_nr];
12423 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12424 {
12425 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12426 " [in module %s]"),
12427 dwp_file->name);
12428 }
12429 }
12430
12431 if (i < 2
12432 || dwarf2_section_empty_p (&sections.info_or_types)
12433 || dwarf2_section_empty_p (&sections.abbrev))
12434 {
12435 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12436 " [in module %s]"),
12437 dwp_file->name);
12438 }
12439 if (i == MAX_NR_V1_DWO_SECTIONS)
12440 {
12441 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12442 " [in module %s]"),
12443 dwp_file->name);
12444 }
12445
12446 /* It's easier for the rest of the code if we fake a struct dwo_file and
12447 have dwo_unit "live" in that. At least for now.
12448
12449 The DWP file can be made up of a random collection of CUs and TUs.
12450 However, for each CU + set of TUs that came from the same original DWO
12451 file, we can combine them back into a virtual DWO file to save space
12452 (fewer struct dwo_file objects to allocate). Remember that for really
12453 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12454
12455 std::string virtual_dwo_name =
12456 string_printf ("virtual-dwo/%d-%d-%d-%d",
12457 get_section_id (&sections.abbrev),
12458 get_section_id (&sections.line),
12459 get_section_id (&sections.loc),
12460 get_section_id (&sections.str_offsets));
12461 /* Can we use an existing virtual DWO file? */
12462 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12463 virtual_dwo_name.c_str (),
12464 comp_dir);
12465 /* Create one if necessary. */
12466 if (*dwo_file_slot == NULL)
12467 {
12468 if (dwarf_read_debug)
12469 {
12470 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12471 virtual_dwo_name.c_str ());
12472 }
12473 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12474 dwo_file->dwo_name
12475 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12476 virtual_dwo_name.c_str (),
12477 virtual_dwo_name.size ());
12478 dwo_file->comp_dir = comp_dir;
12479 dwo_file->sections.abbrev = sections.abbrev;
12480 dwo_file->sections.line = sections.line;
12481 dwo_file->sections.loc = sections.loc;
12482 dwo_file->sections.macinfo = sections.macinfo;
12483 dwo_file->sections.macro = sections.macro;
12484 dwo_file->sections.str_offsets = sections.str_offsets;
12485 /* The "str" section is global to the entire DWP file. */
12486 dwo_file->sections.str = dwp_file->sections.str;
12487 /* The info or types section is assigned below to dwo_unit,
12488 there's no need to record it in dwo_file.
12489 Also, we can't simply record type sections in dwo_file because
12490 we record a pointer into the vector in dwo_unit. As we collect more
12491 types we'll grow the vector and eventually have to reallocate space
12492 for it, invalidating all copies of pointers into the previous
12493 contents. */
12494 *dwo_file_slot = dwo_file;
12495 }
12496 else
12497 {
12498 if (dwarf_read_debug)
12499 {
12500 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12501 virtual_dwo_name.c_str ());
12502 }
12503 dwo_file = (struct dwo_file *) *dwo_file_slot;
12504 }
12505
12506 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12507 dwo_unit->dwo_file = dwo_file;
12508 dwo_unit->signature = signature;
12509 dwo_unit->section =
12510 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12511 *dwo_unit->section = sections.info_or_types;
12512 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12513
12514 return dwo_unit;
12515 }
12516
12517 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12518 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12519 piece within that section used by a TU/CU, return a virtual section
12520 of just that piece. */
12521
12522 static struct dwarf2_section_info
12523 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12524 struct dwarf2_section_info *section,
12525 bfd_size_type offset, bfd_size_type size)
12526 {
12527 struct dwarf2_section_info result;
12528 asection *sectp;
12529
12530 gdb_assert (section != NULL);
12531 gdb_assert (!section->is_virtual);
12532
12533 memset (&result, 0, sizeof (result));
12534 result.s.containing_section = section;
12535 result.is_virtual = 1;
12536
12537 if (size == 0)
12538 return result;
12539
12540 sectp = get_section_bfd_section (section);
12541
12542 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12543 bounds of the real section. This is a pretty-rare event, so just
12544 flag an error (easier) instead of a warning and trying to cope. */
12545 if (sectp == NULL
12546 || offset + size > bfd_get_section_size (sectp))
12547 {
12548 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12549 " in section %s [in module %s]"),
12550 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12551 objfile_name (dwarf2_per_objfile->objfile));
12552 }
12553
12554 result.virtual_offset = offset;
12555 result.size = size;
12556 return result;
12557 }
12558
12559 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12560 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12561 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12562 This is for DWP version 2 files. */
12563
12564 static struct dwo_unit *
12565 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12566 struct dwp_file *dwp_file,
12567 uint32_t unit_index,
12568 const char *comp_dir,
12569 ULONGEST signature, int is_debug_types)
12570 {
12571 struct objfile *objfile = dwarf2_per_objfile->objfile;
12572 const struct dwp_hash_table *dwp_htab =
12573 is_debug_types ? dwp_file->tus : dwp_file->cus;
12574 bfd *dbfd = dwp_file->dbfd;
12575 const char *kind = is_debug_types ? "TU" : "CU";
12576 struct dwo_file *dwo_file;
12577 struct dwo_unit *dwo_unit;
12578 struct virtual_v2_dwo_sections sections;
12579 void **dwo_file_slot;
12580 int i;
12581
12582 gdb_assert (dwp_file->version == 2);
12583
12584 if (dwarf_read_debug)
12585 {
12586 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12587 kind,
12588 pulongest (unit_index), hex_string (signature),
12589 dwp_file->name);
12590 }
12591
12592 /* Fetch the section offsets of this DWO unit. */
12593
12594 memset (&sections, 0, sizeof (sections));
12595
12596 for (i = 0; i < dwp_htab->nr_columns; ++i)
12597 {
12598 uint32_t offset = read_4_bytes (dbfd,
12599 dwp_htab->section_pool.v2.offsets
12600 + (((unit_index - 1) * dwp_htab->nr_columns
12601 + i)
12602 * sizeof (uint32_t)));
12603 uint32_t size = read_4_bytes (dbfd,
12604 dwp_htab->section_pool.v2.sizes
12605 + (((unit_index - 1) * dwp_htab->nr_columns
12606 + i)
12607 * sizeof (uint32_t)));
12608
12609 switch (dwp_htab->section_pool.v2.section_ids[i])
12610 {
12611 case DW_SECT_INFO:
12612 case DW_SECT_TYPES:
12613 sections.info_or_types_offset = offset;
12614 sections.info_or_types_size = size;
12615 break;
12616 case DW_SECT_ABBREV:
12617 sections.abbrev_offset = offset;
12618 sections.abbrev_size = size;
12619 break;
12620 case DW_SECT_LINE:
12621 sections.line_offset = offset;
12622 sections.line_size = size;
12623 break;
12624 case DW_SECT_LOC:
12625 sections.loc_offset = offset;
12626 sections.loc_size = size;
12627 break;
12628 case DW_SECT_STR_OFFSETS:
12629 sections.str_offsets_offset = offset;
12630 sections.str_offsets_size = size;
12631 break;
12632 case DW_SECT_MACINFO:
12633 sections.macinfo_offset = offset;
12634 sections.macinfo_size = size;
12635 break;
12636 case DW_SECT_MACRO:
12637 sections.macro_offset = offset;
12638 sections.macro_size = size;
12639 break;
12640 }
12641 }
12642
12643 /* It's easier for the rest of the code if we fake a struct dwo_file and
12644 have dwo_unit "live" in that. At least for now.
12645
12646 The DWP file can be made up of a random collection of CUs and TUs.
12647 However, for each CU + set of TUs that came from the same original DWO
12648 file, we can combine them back into a virtual DWO file to save space
12649 (fewer struct dwo_file objects to allocate). Remember that for really
12650 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12651
12652 std::string virtual_dwo_name =
12653 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12654 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12655 (long) (sections.line_size ? sections.line_offset : 0),
12656 (long) (sections.loc_size ? sections.loc_offset : 0),
12657 (long) (sections.str_offsets_size
12658 ? sections.str_offsets_offset : 0));
12659 /* Can we use an existing virtual DWO file? */
12660 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12661 virtual_dwo_name.c_str (),
12662 comp_dir);
12663 /* Create one if necessary. */
12664 if (*dwo_file_slot == NULL)
12665 {
12666 if (dwarf_read_debug)
12667 {
12668 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12669 virtual_dwo_name.c_str ());
12670 }
12671 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12672 dwo_file->dwo_name
12673 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12674 virtual_dwo_name.c_str (),
12675 virtual_dwo_name.size ());
12676 dwo_file->comp_dir = comp_dir;
12677 dwo_file->sections.abbrev =
12678 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12679 sections.abbrev_offset, sections.abbrev_size);
12680 dwo_file->sections.line =
12681 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12682 sections.line_offset, sections.line_size);
12683 dwo_file->sections.loc =
12684 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12685 sections.loc_offset, sections.loc_size);
12686 dwo_file->sections.macinfo =
12687 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12688 sections.macinfo_offset, sections.macinfo_size);
12689 dwo_file->sections.macro =
12690 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12691 sections.macro_offset, sections.macro_size);
12692 dwo_file->sections.str_offsets =
12693 create_dwp_v2_section (dwarf2_per_objfile,
12694 &dwp_file->sections.str_offsets,
12695 sections.str_offsets_offset,
12696 sections.str_offsets_size);
12697 /* The "str" section is global to the entire DWP file. */
12698 dwo_file->sections.str = dwp_file->sections.str;
12699 /* The info or types section is assigned below to dwo_unit,
12700 there's no need to record it in dwo_file.
12701 Also, we can't simply record type sections in dwo_file because
12702 we record a pointer into the vector in dwo_unit. As we collect more
12703 types we'll grow the vector and eventually have to reallocate space
12704 for it, invalidating all copies of pointers into the previous
12705 contents. */
12706 *dwo_file_slot = dwo_file;
12707 }
12708 else
12709 {
12710 if (dwarf_read_debug)
12711 {
12712 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12713 virtual_dwo_name.c_str ());
12714 }
12715 dwo_file = (struct dwo_file *) *dwo_file_slot;
12716 }
12717
12718 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12719 dwo_unit->dwo_file = dwo_file;
12720 dwo_unit->signature = signature;
12721 dwo_unit->section =
12722 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12723 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12724 is_debug_types
12725 ? &dwp_file->sections.types
12726 : &dwp_file->sections.info,
12727 sections.info_or_types_offset,
12728 sections.info_or_types_size);
12729 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12730
12731 return dwo_unit;
12732 }
12733
12734 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12735 Returns NULL if the signature isn't found. */
12736
12737 static struct dwo_unit *
12738 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12739 struct dwp_file *dwp_file, const char *comp_dir,
12740 ULONGEST signature, int is_debug_types)
12741 {
12742 const struct dwp_hash_table *dwp_htab =
12743 is_debug_types ? dwp_file->tus : dwp_file->cus;
12744 bfd *dbfd = dwp_file->dbfd;
12745 uint32_t mask = dwp_htab->nr_slots - 1;
12746 uint32_t hash = signature & mask;
12747 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12748 unsigned int i;
12749 void **slot;
12750 struct dwo_unit find_dwo_cu;
12751
12752 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12753 find_dwo_cu.signature = signature;
12754 slot = htab_find_slot (is_debug_types
12755 ? dwp_file->loaded_tus
12756 : dwp_file->loaded_cus,
12757 &find_dwo_cu, INSERT);
12758
12759 if (*slot != NULL)
12760 return (struct dwo_unit *) *slot;
12761
12762 /* Use a for loop so that we don't loop forever on bad debug info. */
12763 for (i = 0; i < dwp_htab->nr_slots; ++i)
12764 {
12765 ULONGEST signature_in_table;
12766
12767 signature_in_table =
12768 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12769 if (signature_in_table == signature)
12770 {
12771 uint32_t unit_index =
12772 read_4_bytes (dbfd,
12773 dwp_htab->unit_table + hash * sizeof (uint32_t));
12774
12775 if (dwp_file->version == 1)
12776 {
12777 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12778 dwp_file, unit_index,
12779 comp_dir, signature,
12780 is_debug_types);
12781 }
12782 else
12783 {
12784 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12785 dwp_file, unit_index,
12786 comp_dir, signature,
12787 is_debug_types);
12788 }
12789 return (struct dwo_unit *) *slot;
12790 }
12791 if (signature_in_table == 0)
12792 return NULL;
12793 hash = (hash + hash2) & mask;
12794 }
12795
12796 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12797 " [in module %s]"),
12798 dwp_file->name);
12799 }
12800
12801 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12802 Open the file specified by FILE_NAME and hand it off to BFD for
12803 preliminary analysis. Return a newly initialized bfd *, which
12804 includes a canonicalized copy of FILE_NAME.
12805 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12806 SEARCH_CWD is true if the current directory is to be searched.
12807 It will be searched before debug-file-directory.
12808 If successful, the file is added to the bfd include table of the
12809 objfile's bfd (see gdb_bfd_record_inclusion).
12810 If unable to find/open the file, return NULL.
12811 NOTE: This function is derived from symfile_bfd_open. */
12812
12813 static gdb_bfd_ref_ptr
12814 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12815 const char *file_name, int is_dwp, int search_cwd)
12816 {
12817 int desc;
12818 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12819 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12820 to debug_file_directory. */
12821 const char *search_path;
12822 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12823
12824 gdb::unique_xmalloc_ptr<char> search_path_holder;
12825 if (search_cwd)
12826 {
12827 if (*debug_file_directory != '\0')
12828 {
12829 search_path_holder.reset (concat (".", dirname_separator_string,
12830 debug_file_directory,
12831 (char *) NULL));
12832 search_path = search_path_holder.get ();
12833 }
12834 else
12835 search_path = ".";
12836 }
12837 else
12838 search_path = debug_file_directory;
12839
12840 openp_flags flags = OPF_RETURN_REALPATH;
12841 if (is_dwp)
12842 flags |= OPF_SEARCH_IN_PATH;
12843
12844 gdb::unique_xmalloc_ptr<char> absolute_name;
12845 desc = openp (search_path, flags, file_name,
12846 O_RDONLY | O_BINARY, &absolute_name);
12847 if (desc < 0)
12848 return NULL;
12849
12850 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12851 gnutarget, desc));
12852 if (sym_bfd == NULL)
12853 return NULL;
12854 bfd_set_cacheable (sym_bfd.get (), 1);
12855
12856 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12857 return NULL;
12858
12859 /* Success. Record the bfd as having been included by the objfile's bfd.
12860 This is important because things like demangled_names_hash lives in the
12861 objfile's per_bfd space and may have references to things like symbol
12862 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12863 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12864
12865 return sym_bfd;
12866 }
12867
12868 /* Try to open DWO file FILE_NAME.
12869 COMP_DIR is the DW_AT_comp_dir attribute.
12870 The result is the bfd handle of the file.
12871 If there is a problem finding or opening the file, return NULL.
12872 Upon success, the canonicalized path of the file is stored in the bfd,
12873 same as symfile_bfd_open. */
12874
12875 static gdb_bfd_ref_ptr
12876 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12877 const char *file_name, const char *comp_dir)
12878 {
12879 if (IS_ABSOLUTE_PATH (file_name))
12880 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12881 0 /*is_dwp*/, 0 /*search_cwd*/);
12882
12883 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12884
12885 if (comp_dir != NULL)
12886 {
12887 char *path_to_try = concat (comp_dir, SLASH_STRING,
12888 file_name, (char *) NULL);
12889
12890 /* NOTE: If comp_dir is a relative path, this will also try the
12891 search path, which seems useful. */
12892 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12893 path_to_try,
12894 0 /*is_dwp*/,
12895 1 /*search_cwd*/));
12896 xfree (path_to_try);
12897 if (abfd != NULL)
12898 return abfd;
12899 }
12900
12901 /* That didn't work, try debug-file-directory, which, despite its name,
12902 is a list of paths. */
12903
12904 if (*debug_file_directory == '\0')
12905 return NULL;
12906
12907 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12908 0 /*is_dwp*/, 1 /*search_cwd*/);
12909 }
12910
12911 /* This function is mapped across the sections and remembers the offset and
12912 size of each of the DWO debugging sections we are interested in. */
12913
12914 static void
12915 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12916 {
12917 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12918 const struct dwop_section_names *names = &dwop_section_names;
12919
12920 if (section_is_p (sectp->name, &names->abbrev_dwo))
12921 {
12922 dwo_sections->abbrev.s.section = sectp;
12923 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12924 }
12925 else if (section_is_p (sectp->name, &names->info_dwo))
12926 {
12927 dwo_sections->info.s.section = sectp;
12928 dwo_sections->info.size = bfd_get_section_size (sectp);
12929 }
12930 else if (section_is_p (sectp->name, &names->line_dwo))
12931 {
12932 dwo_sections->line.s.section = sectp;
12933 dwo_sections->line.size = bfd_get_section_size (sectp);
12934 }
12935 else if (section_is_p (sectp->name, &names->loc_dwo))
12936 {
12937 dwo_sections->loc.s.section = sectp;
12938 dwo_sections->loc.size = bfd_get_section_size (sectp);
12939 }
12940 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12941 {
12942 dwo_sections->macinfo.s.section = sectp;
12943 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12944 }
12945 else if (section_is_p (sectp->name, &names->macro_dwo))
12946 {
12947 dwo_sections->macro.s.section = sectp;
12948 dwo_sections->macro.size = bfd_get_section_size (sectp);
12949 }
12950 else if (section_is_p (sectp->name, &names->str_dwo))
12951 {
12952 dwo_sections->str.s.section = sectp;
12953 dwo_sections->str.size = bfd_get_section_size (sectp);
12954 }
12955 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12956 {
12957 dwo_sections->str_offsets.s.section = sectp;
12958 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12959 }
12960 else if (section_is_p (sectp->name, &names->types_dwo))
12961 {
12962 struct dwarf2_section_info type_section;
12963
12964 memset (&type_section, 0, sizeof (type_section));
12965 type_section.s.section = sectp;
12966 type_section.size = bfd_get_section_size (sectp);
12967 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12968 &type_section);
12969 }
12970 }
12971
12972 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12973 by PER_CU. This is for the non-DWP case.
12974 The result is NULL if DWO_NAME can't be found. */
12975
12976 static struct dwo_file *
12977 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12978 const char *dwo_name, const char *comp_dir)
12979 {
12980 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12981 struct objfile *objfile = dwarf2_per_objfile->objfile;
12982
12983 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
12984 if (dbfd == NULL)
12985 {
12986 if (dwarf_read_debug)
12987 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12988 return NULL;
12989 }
12990
12991 /* We use a unique pointer here, despite the obstack allocation,
12992 because a dwo_file needs some cleanup if it is abandoned. */
12993 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12994 struct dwo_file));
12995 dwo_file->dwo_name = dwo_name;
12996 dwo_file->comp_dir = comp_dir;
12997 dwo_file->dbfd = dbfd.release ();
12998
12999 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13000 &dwo_file->sections);
13001
13002 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13003 dwo_file->cus);
13004
13005 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13006 dwo_file->sections.types, dwo_file->tus);
13007
13008 if (dwarf_read_debug)
13009 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13010
13011 return dwo_file.release ();
13012 }
13013
13014 /* This function is mapped across the sections and remembers the offset and
13015 size of each of the DWP debugging sections common to version 1 and 2 that
13016 we are interested in. */
13017
13018 static void
13019 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13020 void *dwp_file_ptr)
13021 {
13022 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13023 const struct dwop_section_names *names = &dwop_section_names;
13024 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13025
13026 /* Record the ELF section number for later lookup: this is what the
13027 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13028 gdb_assert (elf_section_nr < dwp_file->num_sections);
13029 dwp_file->elf_sections[elf_section_nr] = sectp;
13030
13031 /* Look for specific sections that we need. */
13032 if (section_is_p (sectp->name, &names->str_dwo))
13033 {
13034 dwp_file->sections.str.s.section = sectp;
13035 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13036 }
13037 else if (section_is_p (sectp->name, &names->cu_index))
13038 {
13039 dwp_file->sections.cu_index.s.section = sectp;
13040 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13041 }
13042 else if (section_is_p (sectp->name, &names->tu_index))
13043 {
13044 dwp_file->sections.tu_index.s.section = sectp;
13045 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13046 }
13047 }
13048
13049 /* This function is mapped across the sections and remembers the offset and
13050 size of each of the DWP version 2 debugging sections that we are interested
13051 in. This is split into a separate function because we don't know if we
13052 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13053
13054 static void
13055 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13056 {
13057 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13058 const struct dwop_section_names *names = &dwop_section_names;
13059 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13060
13061 /* Record the ELF section number for later lookup: this is what the
13062 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13063 gdb_assert (elf_section_nr < dwp_file->num_sections);
13064 dwp_file->elf_sections[elf_section_nr] = sectp;
13065
13066 /* Look for specific sections that we need. */
13067 if (section_is_p (sectp->name, &names->abbrev_dwo))
13068 {
13069 dwp_file->sections.abbrev.s.section = sectp;
13070 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13071 }
13072 else if (section_is_p (sectp->name, &names->info_dwo))
13073 {
13074 dwp_file->sections.info.s.section = sectp;
13075 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13076 }
13077 else if (section_is_p (sectp->name, &names->line_dwo))
13078 {
13079 dwp_file->sections.line.s.section = sectp;
13080 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13081 }
13082 else if (section_is_p (sectp->name, &names->loc_dwo))
13083 {
13084 dwp_file->sections.loc.s.section = sectp;
13085 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13086 }
13087 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13088 {
13089 dwp_file->sections.macinfo.s.section = sectp;
13090 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13091 }
13092 else if (section_is_p (sectp->name, &names->macro_dwo))
13093 {
13094 dwp_file->sections.macro.s.section = sectp;
13095 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13096 }
13097 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13098 {
13099 dwp_file->sections.str_offsets.s.section = sectp;
13100 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13101 }
13102 else if (section_is_p (sectp->name, &names->types_dwo))
13103 {
13104 dwp_file->sections.types.s.section = sectp;
13105 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13106 }
13107 }
13108
13109 /* Hash function for dwp_file loaded CUs/TUs. */
13110
13111 static hashval_t
13112 hash_dwp_loaded_cutus (const void *item)
13113 {
13114 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13115
13116 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13117 return dwo_unit->signature;
13118 }
13119
13120 /* Equality function for dwp_file loaded CUs/TUs. */
13121
13122 static int
13123 eq_dwp_loaded_cutus (const void *a, const void *b)
13124 {
13125 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13126 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13127
13128 return dua->signature == dub->signature;
13129 }
13130
13131 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13132
13133 static htab_t
13134 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13135 {
13136 return htab_create_alloc_ex (3,
13137 hash_dwp_loaded_cutus,
13138 eq_dwp_loaded_cutus,
13139 NULL,
13140 &objfile->objfile_obstack,
13141 hashtab_obstack_allocate,
13142 dummy_obstack_deallocate);
13143 }
13144
13145 /* Try to open DWP file FILE_NAME.
13146 The result is the bfd handle of the file.
13147 If there is a problem finding or opening the file, return NULL.
13148 Upon success, the canonicalized path of the file is stored in the bfd,
13149 same as symfile_bfd_open. */
13150
13151 static gdb_bfd_ref_ptr
13152 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13153 const char *file_name)
13154 {
13155 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13156 1 /*is_dwp*/,
13157 1 /*search_cwd*/));
13158 if (abfd != NULL)
13159 return abfd;
13160
13161 /* Work around upstream bug 15652.
13162 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13163 [Whether that's a "bug" is debatable, but it is getting in our way.]
13164 We have no real idea where the dwp file is, because gdb's realpath-ing
13165 of the executable's path may have discarded the needed info.
13166 [IWBN if the dwp file name was recorded in the executable, akin to
13167 .gnu_debuglink, but that doesn't exist yet.]
13168 Strip the directory from FILE_NAME and search again. */
13169 if (*debug_file_directory != '\0')
13170 {
13171 /* Don't implicitly search the current directory here.
13172 If the user wants to search "." to handle this case,
13173 it must be added to debug-file-directory. */
13174 return try_open_dwop_file (dwarf2_per_objfile,
13175 lbasename (file_name), 1 /*is_dwp*/,
13176 0 /*search_cwd*/);
13177 }
13178
13179 return NULL;
13180 }
13181
13182 /* Initialize the use of the DWP file for the current objfile.
13183 By convention the name of the DWP file is ${objfile}.dwp.
13184 The result is NULL if it can't be found. */
13185
13186 static struct dwp_file *
13187 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13188 {
13189 struct objfile *objfile = dwarf2_per_objfile->objfile;
13190 struct dwp_file *dwp_file;
13191
13192 /* Try to find first .dwp for the binary file before any symbolic links
13193 resolving. */
13194
13195 /* If the objfile is a debug file, find the name of the real binary
13196 file and get the name of dwp file from there. */
13197 std::string dwp_name;
13198 if (objfile->separate_debug_objfile_backlink != NULL)
13199 {
13200 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13201 const char *backlink_basename = lbasename (backlink->original_name);
13202
13203 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13204 }
13205 else
13206 dwp_name = objfile->original_name;
13207
13208 dwp_name += ".dwp";
13209
13210 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13211 if (dbfd == NULL
13212 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13213 {
13214 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13215 dwp_name = objfile_name (objfile);
13216 dwp_name += ".dwp";
13217 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13218 }
13219
13220 if (dbfd == NULL)
13221 {
13222 if (dwarf_read_debug)
13223 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13224 return NULL;
13225 }
13226 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13227 dwp_file->name = bfd_get_filename (dbfd.get ());
13228 dwp_file->dbfd = dbfd.release ();
13229
13230 /* +1: section 0 is unused */
13231 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13232 dwp_file->elf_sections =
13233 OBSTACK_CALLOC (&objfile->objfile_obstack,
13234 dwp_file->num_sections, asection *);
13235
13236 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13237 dwp_file);
13238
13239 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13240
13241 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13242
13243 /* The DWP file version is stored in the hash table. Oh well. */
13244 if (dwp_file->cus && dwp_file->tus
13245 && dwp_file->cus->version != dwp_file->tus->version)
13246 {
13247 /* Technically speaking, we should try to limp along, but this is
13248 pretty bizarre. We use pulongest here because that's the established
13249 portability solution (e.g, we cannot use %u for uint32_t). */
13250 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13251 " TU version %s [in DWP file %s]"),
13252 pulongest (dwp_file->cus->version),
13253 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13254 }
13255
13256 if (dwp_file->cus)
13257 dwp_file->version = dwp_file->cus->version;
13258 else if (dwp_file->tus)
13259 dwp_file->version = dwp_file->tus->version;
13260 else
13261 dwp_file->version = 2;
13262
13263 if (dwp_file->version == 2)
13264 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13265 dwp_file);
13266
13267 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13268 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13269
13270 if (dwarf_read_debug)
13271 {
13272 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13273 fprintf_unfiltered (gdb_stdlog,
13274 " %s CUs, %s TUs\n",
13275 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13276 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13277 }
13278
13279 return dwp_file;
13280 }
13281
13282 /* Wrapper around open_and_init_dwp_file, only open it once. */
13283
13284 static struct dwp_file *
13285 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13286 {
13287 if (! dwarf2_per_objfile->dwp_checked)
13288 {
13289 dwarf2_per_objfile->dwp_file
13290 = open_and_init_dwp_file (dwarf2_per_objfile);
13291 dwarf2_per_objfile->dwp_checked = 1;
13292 }
13293 return dwarf2_per_objfile->dwp_file;
13294 }
13295
13296 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13297 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13298 or in the DWP file for the objfile, referenced by THIS_UNIT.
13299 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13300 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13301
13302 This is called, for example, when wanting to read a variable with a
13303 complex location. Therefore we don't want to do file i/o for every call.
13304 Therefore we don't want to look for a DWO file on every call.
13305 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13306 then we check if we've already seen DWO_NAME, and only THEN do we check
13307 for a DWO file.
13308
13309 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13310 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13311
13312 static struct dwo_unit *
13313 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13314 const char *dwo_name, const char *comp_dir,
13315 ULONGEST signature, int is_debug_types)
13316 {
13317 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13318 struct objfile *objfile = dwarf2_per_objfile->objfile;
13319 const char *kind = is_debug_types ? "TU" : "CU";
13320 void **dwo_file_slot;
13321 struct dwo_file *dwo_file;
13322 struct dwp_file *dwp_file;
13323
13324 /* First see if there's a DWP file.
13325 If we have a DWP file but didn't find the DWO inside it, don't
13326 look for the original DWO file. It makes gdb behave differently
13327 depending on whether one is debugging in the build tree. */
13328
13329 dwp_file = get_dwp_file (dwarf2_per_objfile);
13330 if (dwp_file != NULL)
13331 {
13332 const struct dwp_hash_table *dwp_htab =
13333 is_debug_types ? dwp_file->tus : dwp_file->cus;
13334
13335 if (dwp_htab != NULL)
13336 {
13337 struct dwo_unit *dwo_cutu =
13338 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13339 signature, is_debug_types);
13340
13341 if (dwo_cutu != NULL)
13342 {
13343 if (dwarf_read_debug)
13344 {
13345 fprintf_unfiltered (gdb_stdlog,
13346 "Virtual DWO %s %s found: @%s\n",
13347 kind, hex_string (signature),
13348 host_address_to_string (dwo_cutu));
13349 }
13350 return dwo_cutu;
13351 }
13352 }
13353 }
13354 else
13355 {
13356 /* No DWP file, look for the DWO file. */
13357
13358 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13359 dwo_name, comp_dir);
13360 if (*dwo_file_slot == NULL)
13361 {
13362 /* Read in the file and build a table of the CUs/TUs it contains. */
13363 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13364 }
13365 /* NOTE: This will be NULL if unable to open the file. */
13366 dwo_file = (struct dwo_file *) *dwo_file_slot;
13367
13368 if (dwo_file != NULL)
13369 {
13370 struct dwo_unit *dwo_cutu = NULL;
13371
13372 if (is_debug_types && dwo_file->tus)
13373 {
13374 struct dwo_unit find_dwo_cutu;
13375
13376 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13377 find_dwo_cutu.signature = signature;
13378 dwo_cutu
13379 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13380 }
13381 else if (!is_debug_types && dwo_file->cus)
13382 {
13383 struct dwo_unit find_dwo_cutu;
13384
13385 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13386 find_dwo_cutu.signature = signature;
13387 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13388 &find_dwo_cutu);
13389 }
13390
13391 if (dwo_cutu != NULL)
13392 {
13393 if (dwarf_read_debug)
13394 {
13395 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13396 kind, dwo_name, hex_string (signature),
13397 host_address_to_string (dwo_cutu));
13398 }
13399 return dwo_cutu;
13400 }
13401 }
13402 }
13403
13404 /* We didn't find it. This could mean a dwo_id mismatch, or
13405 someone deleted the DWO/DWP file, or the search path isn't set up
13406 correctly to find the file. */
13407
13408 if (dwarf_read_debug)
13409 {
13410 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13411 kind, dwo_name, hex_string (signature));
13412 }
13413
13414 /* This is a warning and not a complaint because it can be caused by
13415 pilot error (e.g., user accidentally deleting the DWO). */
13416 {
13417 /* Print the name of the DWP file if we looked there, helps the user
13418 better diagnose the problem. */
13419 std::string dwp_text;
13420
13421 if (dwp_file != NULL)
13422 dwp_text = string_printf (" [in DWP file %s]",
13423 lbasename (dwp_file->name));
13424
13425 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13426 " [in module %s]"),
13427 kind, dwo_name, hex_string (signature),
13428 dwp_text.c_str (),
13429 this_unit->is_debug_types ? "TU" : "CU",
13430 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13431 }
13432 return NULL;
13433 }
13434
13435 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13436 See lookup_dwo_cutu_unit for details. */
13437
13438 static struct dwo_unit *
13439 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13440 const char *dwo_name, const char *comp_dir,
13441 ULONGEST signature)
13442 {
13443 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13444 }
13445
13446 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13447 See lookup_dwo_cutu_unit for details. */
13448
13449 static struct dwo_unit *
13450 lookup_dwo_type_unit (struct signatured_type *this_tu,
13451 const char *dwo_name, const char *comp_dir)
13452 {
13453 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13454 }
13455
13456 /* Traversal function for queue_and_load_all_dwo_tus. */
13457
13458 static int
13459 queue_and_load_dwo_tu (void **slot, void *info)
13460 {
13461 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13462 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13463 ULONGEST signature = dwo_unit->signature;
13464 struct signatured_type *sig_type =
13465 lookup_dwo_signatured_type (per_cu->cu, signature);
13466
13467 if (sig_type != NULL)
13468 {
13469 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13470
13471 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13472 a real dependency of PER_CU on SIG_TYPE. That is detected later
13473 while processing PER_CU. */
13474 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13475 load_full_type_unit (sig_cu);
13476 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13477 }
13478
13479 return 1;
13480 }
13481
13482 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13483 The DWO may have the only definition of the type, though it may not be
13484 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13485 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13486
13487 static void
13488 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13489 {
13490 struct dwo_unit *dwo_unit;
13491 struct dwo_file *dwo_file;
13492
13493 gdb_assert (!per_cu->is_debug_types);
13494 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13495 gdb_assert (per_cu->cu != NULL);
13496
13497 dwo_unit = per_cu->cu->dwo_unit;
13498 gdb_assert (dwo_unit != NULL);
13499
13500 dwo_file = dwo_unit->dwo_file;
13501 if (dwo_file->tus != NULL)
13502 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13503 }
13504
13505 /* Free all resources associated with DWO_FILE.
13506 Close the DWO file and munmap the sections. */
13507
13508 static void
13509 free_dwo_file (struct dwo_file *dwo_file)
13510 {
13511 /* Note: dbfd is NULL for virtual DWO files. */
13512 gdb_bfd_unref (dwo_file->dbfd);
13513
13514 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13515 }
13516
13517 /* Traversal function for free_dwo_files. */
13518
13519 static int
13520 free_dwo_file_from_slot (void **slot, void *info)
13521 {
13522 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13523
13524 free_dwo_file (dwo_file);
13525
13526 return 1;
13527 }
13528
13529 /* Free all resources associated with DWO_FILES. */
13530
13531 static void
13532 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13533 {
13534 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13535 }
13536 \f
13537 /* Read in various DIEs. */
13538
13539 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13540 Inherit only the children of the DW_AT_abstract_origin DIE not being
13541 already referenced by DW_AT_abstract_origin from the children of the
13542 current DIE. */
13543
13544 static void
13545 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13546 {
13547 struct die_info *child_die;
13548 sect_offset *offsetp;
13549 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13550 struct die_info *origin_die;
13551 /* Iterator of the ORIGIN_DIE children. */
13552 struct die_info *origin_child_die;
13553 struct attribute *attr;
13554 struct dwarf2_cu *origin_cu;
13555 struct pending **origin_previous_list_in_scope;
13556
13557 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13558 if (!attr)
13559 return;
13560
13561 /* Note that following die references may follow to a die in a
13562 different cu. */
13563
13564 origin_cu = cu;
13565 origin_die = follow_die_ref (die, attr, &origin_cu);
13566
13567 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13568 symbols in. */
13569 origin_previous_list_in_scope = origin_cu->list_in_scope;
13570 origin_cu->list_in_scope = cu->list_in_scope;
13571
13572 if (die->tag != origin_die->tag
13573 && !(die->tag == DW_TAG_inlined_subroutine
13574 && origin_die->tag == DW_TAG_subprogram))
13575 complaint (&symfile_complaints,
13576 _("DIE %s and its abstract origin %s have different tags"),
13577 sect_offset_str (die->sect_off),
13578 sect_offset_str (origin_die->sect_off));
13579
13580 std::vector<sect_offset> offsets;
13581
13582 for (child_die = die->child;
13583 child_die && child_die->tag;
13584 child_die = sibling_die (child_die))
13585 {
13586 struct die_info *child_origin_die;
13587 struct dwarf2_cu *child_origin_cu;
13588
13589 /* We are trying to process concrete instance entries:
13590 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13591 it's not relevant to our analysis here. i.e. detecting DIEs that are
13592 present in the abstract instance but not referenced in the concrete
13593 one. */
13594 if (child_die->tag == DW_TAG_call_site
13595 || child_die->tag == DW_TAG_GNU_call_site)
13596 continue;
13597
13598 /* For each CHILD_DIE, find the corresponding child of
13599 ORIGIN_DIE. If there is more than one layer of
13600 DW_AT_abstract_origin, follow them all; there shouldn't be,
13601 but GCC versions at least through 4.4 generate this (GCC PR
13602 40573). */
13603 child_origin_die = child_die;
13604 child_origin_cu = cu;
13605 while (1)
13606 {
13607 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13608 child_origin_cu);
13609 if (attr == NULL)
13610 break;
13611 child_origin_die = follow_die_ref (child_origin_die, attr,
13612 &child_origin_cu);
13613 }
13614
13615 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13616 counterpart may exist. */
13617 if (child_origin_die != child_die)
13618 {
13619 if (child_die->tag != child_origin_die->tag
13620 && !(child_die->tag == DW_TAG_inlined_subroutine
13621 && child_origin_die->tag == DW_TAG_subprogram))
13622 complaint (&symfile_complaints,
13623 _("Child DIE %s and its abstract origin %s have "
13624 "different tags"),
13625 sect_offset_str (child_die->sect_off),
13626 sect_offset_str (child_origin_die->sect_off));
13627 if (child_origin_die->parent != origin_die)
13628 complaint (&symfile_complaints,
13629 _("Child DIE %s and its abstract origin %s have "
13630 "different parents"),
13631 sect_offset_str (child_die->sect_off),
13632 sect_offset_str (child_origin_die->sect_off));
13633 else
13634 offsets.push_back (child_origin_die->sect_off);
13635 }
13636 }
13637 std::sort (offsets.begin (), offsets.end ());
13638 sect_offset *offsets_end = offsets.data () + offsets.size ();
13639 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13640 if (offsetp[-1] == *offsetp)
13641 complaint (&symfile_complaints,
13642 _("Multiple children of DIE %s refer "
13643 "to DIE %s as their abstract origin"),
13644 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13645
13646 offsetp = offsets.data ();
13647 origin_child_die = origin_die->child;
13648 while (origin_child_die && origin_child_die->tag)
13649 {
13650 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13651 while (offsetp < offsets_end
13652 && *offsetp < origin_child_die->sect_off)
13653 offsetp++;
13654 if (offsetp >= offsets_end
13655 || *offsetp > origin_child_die->sect_off)
13656 {
13657 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13658 Check whether we're already processing ORIGIN_CHILD_DIE.
13659 This can happen with mutually referenced abstract_origins.
13660 PR 16581. */
13661 if (!origin_child_die->in_process)
13662 process_die (origin_child_die, origin_cu);
13663 }
13664 origin_child_die = sibling_die (origin_child_die);
13665 }
13666 origin_cu->list_in_scope = origin_previous_list_in_scope;
13667 }
13668
13669 static void
13670 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13671 {
13672 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13673 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13674 struct context_stack *newobj;
13675 CORE_ADDR lowpc;
13676 CORE_ADDR highpc;
13677 struct die_info *child_die;
13678 struct attribute *attr, *call_line, *call_file;
13679 const char *name;
13680 CORE_ADDR baseaddr;
13681 struct block *block;
13682 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13683 std::vector<struct symbol *> template_args;
13684 struct template_symbol *templ_func = NULL;
13685
13686 if (inlined_func)
13687 {
13688 /* If we do not have call site information, we can't show the
13689 caller of this inlined function. That's too confusing, so
13690 only use the scope for local variables. */
13691 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13692 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13693 if (call_line == NULL || call_file == NULL)
13694 {
13695 read_lexical_block_scope (die, cu);
13696 return;
13697 }
13698 }
13699
13700 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13701
13702 name = dwarf2_name (die, cu);
13703
13704 /* Ignore functions with missing or empty names. These are actually
13705 illegal according to the DWARF standard. */
13706 if (name == NULL)
13707 {
13708 complaint (&symfile_complaints,
13709 _("missing name for subprogram DIE at %s"),
13710 sect_offset_str (die->sect_off));
13711 return;
13712 }
13713
13714 /* Ignore functions with missing or invalid low and high pc attributes. */
13715 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13716 <= PC_BOUNDS_INVALID)
13717 {
13718 attr = dwarf2_attr (die, DW_AT_external, cu);
13719 if (!attr || !DW_UNSND (attr))
13720 complaint (&symfile_complaints,
13721 _("cannot get low and high bounds "
13722 "for subprogram DIE at %s"),
13723 sect_offset_str (die->sect_off));
13724 return;
13725 }
13726
13727 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13728 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13729
13730 /* If we have any template arguments, then we must allocate a
13731 different sort of symbol. */
13732 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13733 {
13734 if (child_die->tag == DW_TAG_template_type_param
13735 || child_die->tag == DW_TAG_template_value_param)
13736 {
13737 templ_func = allocate_template_symbol (objfile);
13738 templ_func->subclass = SYMBOL_TEMPLATE;
13739 break;
13740 }
13741 }
13742
13743 newobj = push_context (0, lowpc);
13744 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13745 (struct symbol *) templ_func);
13746
13747 /* If there is a location expression for DW_AT_frame_base, record
13748 it. */
13749 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13750 if (attr)
13751 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13752
13753 /* If there is a location for the static link, record it. */
13754 newobj->static_link = NULL;
13755 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13756 if (attr)
13757 {
13758 newobj->static_link
13759 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13760 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13761 }
13762
13763 cu->list_in_scope = &local_symbols;
13764
13765 if (die->child != NULL)
13766 {
13767 child_die = die->child;
13768 while (child_die && child_die->tag)
13769 {
13770 if (child_die->tag == DW_TAG_template_type_param
13771 || child_die->tag == DW_TAG_template_value_param)
13772 {
13773 struct symbol *arg = new_symbol (child_die, NULL, cu);
13774
13775 if (arg != NULL)
13776 template_args.push_back (arg);
13777 }
13778 else
13779 process_die (child_die, cu);
13780 child_die = sibling_die (child_die);
13781 }
13782 }
13783
13784 inherit_abstract_dies (die, cu);
13785
13786 /* If we have a DW_AT_specification, we might need to import using
13787 directives from the context of the specification DIE. See the
13788 comment in determine_prefix. */
13789 if (cu->language == language_cplus
13790 && dwarf2_attr (die, DW_AT_specification, cu))
13791 {
13792 struct dwarf2_cu *spec_cu = cu;
13793 struct die_info *spec_die = die_specification (die, &spec_cu);
13794
13795 while (spec_die)
13796 {
13797 child_die = spec_die->child;
13798 while (child_die && child_die->tag)
13799 {
13800 if (child_die->tag == DW_TAG_imported_module)
13801 process_die (child_die, spec_cu);
13802 child_die = sibling_die (child_die);
13803 }
13804
13805 /* In some cases, GCC generates specification DIEs that
13806 themselves contain DW_AT_specification attributes. */
13807 spec_die = die_specification (spec_die, &spec_cu);
13808 }
13809 }
13810
13811 newobj = pop_context ();
13812 /* Make a block for the local symbols within. */
13813 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13814 newobj->static_link, lowpc, highpc);
13815
13816 /* For C++, set the block's scope. */
13817 if ((cu->language == language_cplus
13818 || cu->language == language_fortran
13819 || cu->language == language_d
13820 || cu->language == language_rust)
13821 && cu->processing_has_namespace_info)
13822 block_set_scope (block, determine_prefix (die, cu),
13823 &objfile->objfile_obstack);
13824
13825 /* If we have address ranges, record them. */
13826 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13827
13828 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13829
13830 /* Attach template arguments to function. */
13831 if (!template_args.empty ())
13832 {
13833 gdb_assert (templ_func != NULL);
13834
13835 templ_func->n_template_arguments = template_args.size ();
13836 templ_func->template_arguments
13837 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13838 templ_func->n_template_arguments);
13839 memcpy (templ_func->template_arguments,
13840 template_args.data (),
13841 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13842 }
13843
13844 /* In C++, we can have functions nested inside functions (e.g., when
13845 a function declares a class that has methods). This means that
13846 when we finish processing a function scope, we may need to go
13847 back to building a containing block's symbol lists. */
13848 local_symbols = newobj->locals;
13849 local_using_directives = newobj->local_using_directives;
13850
13851 /* If we've finished processing a top-level function, subsequent
13852 symbols go in the file symbol list. */
13853 if (outermost_context_p ())
13854 cu->list_in_scope = &file_symbols;
13855 }
13856
13857 /* Process all the DIES contained within a lexical block scope. Start
13858 a new scope, process the dies, and then close the scope. */
13859
13860 static void
13861 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13862 {
13863 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13864 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13865 struct context_stack *newobj;
13866 CORE_ADDR lowpc, highpc;
13867 struct die_info *child_die;
13868 CORE_ADDR baseaddr;
13869
13870 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13871
13872 /* Ignore blocks with missing or invalid low and high pc attributes. */
13873 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13874 as multiple lexical blocks? Handling children in a sane way would
13875 be nasty. Might be easier to properly extend generic blocks to
13876 describe ranges. */
13877 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13878 {
13879 case PC_BOUNDS_NOT_PRESENT:
13880 /* DW_TAG_lexical_block has no attributes, process its children as if
13881 there was no wrapping by that DW_TAG_lexical_block.
13882 GCC does no longer produces such DWARF since GCC r224161. */
13883 for (child_die = die->child;
13884 child_die != NULL && child_die->tag;
13885 child_die = sibling_die (child_die))
13886 process_die (child_die, cu);
13887 return;
13888 case PC_BOUNDS_INVALID:
13889 return;
13890 }
13891 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13892 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13893
13894 push_context (0, lowpc);
13895 if (die->child != NULL)
13896 {
13897 child_die = die->child;
13898 while (child_die && child_die->tag)
13899 {
13900 process_die (child_die, cu);
13901 child_die = sibling_die (child_die);
13902 }
13903 }
13904 inherit_abstract_dies (die, cu);
13905 newobj = pop_context ();
13906
13907 if (local_symbols != NULL || local_using_directives != NULL)
13908 {
13909 struct block *block
13910 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13911 newobj->start_addr, highpc);
13912
13913 /* Note that recording ranges after traversing children, as we
13914 do here, means that recording a parent's ranges entails
13915 walking across all its children's ranges as they appear in
13916 the address map, which is quadratic behavior.
13917
13918 It would be nicer to record the parent's ranges before
13919 traversing its children, simply overriding whatever you find
13920 there. But since we don't even decide whether to create a
13921 block until after we've traversed its children, that's hard
13922 to do. */
13923 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13924 }
13925 local_symbols = newobj->locals;
13926 local_using_directives = newobj->local_using_directives;
13927 }
13928
13929 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13930
13931 static void
13932 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13933 {
13934 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13935 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13936 CORE_ADDR pc, baseaddr;
13937 struct attribute *attr;
13938 struct call_site *call_site, call_site_local;
13939 void **slot;
13940 int nparams;
13941 struct die_info *child_die;
13942
13943 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13944
13945 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13946 if (attr == NULL)
13947 {
13948 /* This was a pre-DWARF-5 GNU extension alias
13949 for DW_AT_call_return_pc. */
13950 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13951 }
13952 if (!attr)
13953 {
13954 complaint (&symfile_complaints,
13955 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
13956 "DIE %s [in module %s]"),
13957 sect_offset_str (die->sect_off), objfile_name (objfile));
13958 return;
13959 }
13960 pc = attr_value_as_address (attr) + baseaddr;
13961 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13962
13963 if (cu->call_site_htab == NULL)
13964 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13965 NULL, &objfile->objfile_obstack,
13966 hashtab_obstack_allocate, NULL);
13967 call_site_local.pc = pc;
13968 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13969 if (*slot != NULL)
13970 {
13971 complaint (&symfile_complaints,
13972 _("Duplicate PC %s for DW_TAG_call_site "
13973 "DIE %s [in module %s]"),
13974 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13975 objfile_name (objfile));
13976 return;
13977 }
13978
13979 /* Count parameters at the caller. */
13980
13981 nparams = 0;
13982 for (child_die = die->child; child_die && child_die->tag;
13983 child_die = sibling_die (child_die))
13984 {
13985 if (child_die->tag != DW_TAG_call_site_parameter
13986 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13987 {
13988 complaint (&symfile_complaints,
13989 _("Tag %d is not DW_TAG_call_site_parameter in "
13990 "DW_TAG_call_site child DIE %s [in module %s]"),
13991 child_die->tag, sect_offset_str (child_die->sect_off),
13992 objfile_name (objfile));
13993 continue;
13994 }
13995
13996 nparams++;
13997 }
13998
13999 call_site
14000 = ((struct call_site *)
14001 obstack_alloc (&objfile->objfile_obstack,
14002 sizeof (*call_site)
14003 + (sizeof (*call_site->parameter) * (nparams - 1))));
14004 *slot = call_site;
14005 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14006 call_site->pc = pc;
14007
14008 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14009 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14010 {
14011 struct die_info *func_die;
14012
14013 /* Skip also over DW_TAG_inlined_subroutine. */
14014 for (func_die = die->parent;
14015 func_die && func_die->tag != DW_TAG_subprogram
14016 && func_die->tag != DW_TAG_subroutine_type;
14017 func_die = func_die->parent);
14018
14019 /* DW_AT_call_all_calls is a superset
14020 of DW_AT_call_all_tail_calls. */
14021 if (func_die
14022 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14023 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14024 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14025 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14026 {
14027 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14028 not complete. But keep CALL_SITE for look ups via call_site_htab,
14029 both the initial caller containing the real return address PC and
14030 the final callee containing the current PC of a chain of tail
14031 calls do not need to have the tail call list complete. But any
14032 function candidate for a virtual tail call frame searched via
14033 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14034 determined unambiguously. */
14035 }
14036 else
14037 {
14038 struct type *func_type = NULL;
14039
14040 if (func_die)
14041 func_type = get_die_type (func_die, cu);
14042 if (func_type != NULL)
14043 {
14044 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14045
14046 /* Enlist this call site to the function. */
14047 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14048 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14049 }
14050 else
14051 complaint (&symfile_complaints,
14052 _("Cannot find function owning DW_TAG_call_site "
14053 "DIE %s [in module %s]"),
14054 sect_offset_str (die->sect_off), objfile_name (objfile));
14055 }
14056 }
14057
14058 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14059 if (attr == NULL)
14060 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14061 if (attr == NULL)
14062 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14063 if (attr == NULL)
14064 {
14065 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14066 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14067 }
14068 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14069 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14070 /* Keep NULL DWARF_BLOCK. */;
14071 else if (attr_form_is_block (attr))
14072 {
14073 struct dwarf2_locexpr_baton *dlbaton;
14074
14075 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14076 dlbaton->data = DW_BLOCK (attr)->data;
14077 dlbaton->size = DW_BLOCK (attr)->size;
14078 dlbaton->per_cu = cu->per_cu;
14079
14080 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14081 }
14082 else if (attr_form_is_ref (attr))
14083 {
14084 struct dwarf2_cu *target_cu = cu;
14085 struct die_info *target_die;
14086
14087 target_die = follow_die_ref (die, attr, &target_cu);
14088 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14089 if (die_is_declaration (target_die, target_cu))
14090 {
14091 const char *target_physname;
14092
14093 /* Prefer the mangled name; otherwise compute the demangled one. */
14094 target_physname = dw2_linkage_name (target_die, target_cu);
14095 if (target_physname == NULL)
14096 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14097 if (target_physname == NULL)
14098 complaint (&symfile_complaints,
14099 _("DW_AT_call_target target DIE has invalid "
14100 "physname, for referencing DIE %s [in module %s]"),
14101 sect_offset_str (die->sect_off), objfile_name (objfile));
14102 else
14103 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14104 }
14105 else
14106 {
14107 CORE_ADDR lowpc;
14108
14109 /* DW_AT_entry_pc should be preferred. */
14110 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14111 <= PC_BOUNDS_INVALID)
14112 complaint (&symfile_complaints,
14113 _("DW_AT_call_target target DIE has invalid "
14114 "low pc, for referencing DIE %s [in module %s]"),
14115 sect_offset_str (die->sect_off), objfile_name (objfile));
14116 else
14117 {
14118 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14119 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14120 }
14121 }
14122 }
14123 else
14124 complaint (&symfile_complaints,
14125 _("DW_TAG_call_site DW_AT_call_target is neither "
14126 "block nor reference, for DIE %s [in module %s]"),
14127 sect_offset_str (die->sect_off), objfile_name (objfile));
14128
14129 call_site->per_cu = cu->per_cu;
14130
14131 for (child_die = die->child;
14132 child_die && child_die->tag;
14133 child_die = sibling_die (child_die))
14134 {
14135 struct call_site_parameter *parameter;
14136 struct attribute *loc, *origin;
14137
14138 if (child_die->tag != DW_TAG_call_site_parameter
14139 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14140 {
14141 /* Already printed the complaint above. */
14142 continue;
14143 }
14144
14145 gdb_assert (call_site->parameter_count < nparams);
14146 parameter = &call_site->parameter[call_site->parameter_count];
14147
14148 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14149 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14150 register is contained in DW_AT_call_value. */
14151
14152 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14153 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14154 if (origin == NULL)
14155 {
14156 /* This was a pre-DWARF-5 GNU extension alias
14157 for DW_AT_call_parameter. */
14158 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14159 }
14160 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14161 {
14162 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14163
14164 sect_offset sect_off
14165 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14166 if (!offset_in_cu_p (&cu->header, sect_off))
14167 {
14168 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14169 binding can be done only inside one CU. Such referenced DIE
14170 therefore cannot be even moved to DW_TAG_partial_unit. */
14171 complaint (&symfile_complaints,
14172 _("DW_AT_call_parameter offset is not in CU for "
14173 "DW_TAG_call_site child DIE %s [in module %s]"),
14174 sect_offset_str (child_die->sect_off),
14175 objfile_name (objfile));
14176 continue;
14177 }
14178 parameter->u.param_cu_off
14179 = (cu_offset) (sect_off - cu->header.sect_off);
14180 }
14181 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14182 {
14183 complaint (&symfile_complaints,
14184 _("No DW_FORM_block* DW_AT_location for "
14185 "DW_TAG_call_site child DIE %s [in module %s]"),
14186 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14187 continue;
14188 }
14189 else
14190 {
14191 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14192 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14193 if (parameter->u.dwarf_reg != -1)
14194 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14195 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14196 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14197 &parameter->u.fb_offset))
14198 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14199 else
14200 {
14201 complaint (&symfile_complaints,
14202 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14203 "for DW_FORM_block* DW_AT_location is supported for "
14204 "DW_TAG_call_site child DIE %s "
14205 "[in module %s]"),
14206 sect_offset_str (child_die->sect_off),
14207 objfile_name (objfile));
14208 continue;
14209 }
14210 }
14211
14212 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14213 if (attr == NULL)
14214 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14215 if (!attr_form_is_block (attr))
14216 {
14217 complaint (&symfile_complaints,
14218 _("No DW_FORM_block* DW_AT_call_value for "
14219 "DW_TAG_call_site child DIE %s [in module %s]"),
14220 sect_offset_str (child_die->sect_off),
14221 objfile_name (objfile));
14222 continue;
14223 }
14224 parameter->value = DW_BLOCK (attr)->data;
14225 parameter->value_size = DW_BLOCK (attr)->size;
14226
14227 /* Parameters are not pre-cleared by memset above. */
14228 parameter->data_value = NULL;
14229 parameter->data_value_size = 0;
14230 call_site->parameter_count++;
14231
14232 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14233 if (attr == NULL)
14234 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14235 if (attr)
14236 {
14237 if (!attr_form_is_block (attr))
14238 complaint (&symfile_complaints,
14239 _("No DW_FORM_block* DW_AT_call_data_value for "
14240 "DW_TAG_call_site child DIE %s [in module %s]"),
14241 sect_offset_str (child_die->sect_off),
14242 objfile_name (objfile));
14243 else
14244 {
14245 parameter->data_value = DW_BLOCK (attr)->data;
14246 parameter->data_value_size = DW_BLOCK (attr)->size;
14247 }
14248 }
14249 }
14250 }
14251
14252 /* Helper function for read_variable. If DIE represents a virtual
14253 table, then return the type of the concrete object that is
14254 associated with the virtual table. Otherwise, return NULL. */
14255
14256 static struct type *
14257 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14258 {
14259 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14260 if (attr == NULL)
14261 return NULL;
14262
14263 /* Find the type DIE. */
14264 struct die_info *type_die = NULL;
14265 struct dwarf2_cu *type_cu = cu;
14266
14267 if (attr_form_is_ref (attr))
14268 type_die = follow_die_ref (die, attr, &type_cu);
14269 if (type_die == NULL)
14270 return NULL;
14271
14272 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14273 return NULL;
14274 return die_containing_type (type_die, type_cu);
14275 }
14276
14277 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14278
14279 static void
14280 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14281 {
14282 struct rust_vtable_symbol *storage = NULL;
14283
14284 if (cu->language == language_rust)
14285 {
14286 struct type *containing_type = rust_containing_type (die, cu);
14287
14288 if (containing_type != NULL)
14289 {
14290 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14291
14292 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14293 struct rust_vtable_symbol);
14294 initialize_objfile_symbol (storage);
14295 storage->concrete_type = containing_type;
14296 storage->subclass = SYMBOL_RUST_VTABLE;
14297 }
14298 }
14299
14300 new_symbol (die, NULL, cu, storage);
14301 }
14302
14303 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14304 reading .debug_rnglists.
14305 Callback's type should be:
14306 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14307 Return true if the attributes are present and valid, otherwise,
14308 return false. */
14309
14310 template <typename Callback>
14311 static bool
14312 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14313 Callback &&callback)
14314 {
14315 struct dwarf2_per_objfile *dwarf2_per_objfile
14316 = cu->per_cu->dwarf2_per_objfile;
14317 struct objfile *objfile = dwarf2_per_objfile->objfile;
14318 bfd *obfd = objfile->obfd;
14319 /* Base address selection entry. */
14320 CORE_ADDR base;
14321 int found_base;
14322 const gdb_byte *buffer;
14323 CORE_ADDR baseaddr;
14324 bool overflow = false;
14325
14326 found_base = cu->base_known;
14327 base = cu->base_address;
14328
14329 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14330 if (offset >= dwarf2_per_objfile->rnglists.size)
14331 {
14332 complaint (&symfile_complaints,
14333 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14334 offset);
14335 return false;
14336 }
14337 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14338
14339 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14340
14341 while (1)
14342 {
14343 /* Initialize it due to a false compiler warning. */
14344 CORE_ADDR range_beginning = 0, range_end = 0;
14345 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14346 + dwarf2_per_objfile->rnglists.size);
14347 unsigned int bytes_read;
14348
14349 if (buffer == buf_end)
14350 {
14351 overflow = true;
14352 break;
14353 }
14354 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14355 switch (rlet)
14356 {
14357 case DW_RLE_end_of_list:
14358 break;
14359 case DW_RLE_base_address:
14360 if (buffer + cu->header.addr_size > buf_end)
14361 {
14362 overflow = true;
14363 break;
14364 }
14365 base = read_address (obfd, buffer, cu, &bytes_read);
14366 found_base = 1;
14367 buffer += bytes_read;
14368 break;
14369 case DW_RLE_start_length:
14370 if (buffer + cu->header.addr_size > buf_end)
14371 {
14372 overflow = true;
14373 break;
14374 }
14375 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14376 buffer += bytes_read;
14377 range_end = (range_beginning
14378 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14379 buffer += bytes_read;
14380 if (buffer > buf_end)
14381 {
14382 overflow = true;
14383 break;
14384 }
14385 break;
14386 case DW_RLE_offset_pair:
14387 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14388 buffer += bytes_read;
14389 if (buffer > buf_end)
14390 {
14391 overflow = true;
14392 break;
14393 }
14394 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14395 buffer += bytes_read;
14396 if (buffer > buf_end)
14397 {
14398 overflow = true;
14399 break;
14400 }
14401 break;
14402 case DW_RLE_start_end:
14403 if (buffer + 2 * cu->header.addr_size > buf_end)
14404 {
14405 overflow = true;
14406 break;
14407 }
14408 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14409 buffer += bytes_read;
14410 range_end = read_address (obfd, buffer, cu, &bytes_read);
14411 buffer += bytes_read;
14412 break;
14413 default:
14414 complaint (&symfile_complaints,
14415 _("Invalid .debug_rnglists data (no base address)"));
14416 return false;
14417 }
14418 if (rlet == DW_RLE_end_of_list || overflow)
14419 break;
14420 if (rlet == DW_RLE_base_address)
14421 continue;
14422
14423 if (!found_base)
14424 {
14425 /* We have no valid base address for the ranges
14426 data. */
14427 complaint (&symfile_complaints,
14428 _("Invalid .debug_rnglists data (no base address)"));
14429 return false;
14430 }
14431
14432 if (range_beginning > range_end)
14433 {
14434 /* Inverted range entries are invalid. */
14435 complaint (&symfile_complaints,
14436 _("Invalid .debug_rnglists data (inverted range)"));
14437 return false;
14438 }
14439
14440 /* Empty range entries have no effect. */
14441 if (range_beginning == range_end)
14442 continue;
14443
14444 range_beginning += base;
14445 range_end += base;
14446
14447 /* A not-uncommon case of bad debug info.
14448 Don't pollute the addrmap with bad data. */
14449 if (range_beginning + baseaddr == 0
14450 && !dwarf2_per_objfile->has_section_at_zero)
14451 {
14452 complaint (&symfile_complaints,
14453 _(".debug_rnglists entry has start address of zero"
14454 " [in module %s]"), objfile_name (objfile));
14455 continue;
14456 }
14457
14458 callback (range_beginning, range_end);
14459 }
14460
14461 if (overflow)
14462 {
14463 complaint (&symfile_complaints,
14464 _("Offset %d is not terminated "
14465 "for DW_AT_ranges attribute"),
14466 offset);
14467 return false;
14468 }
14469
14470 return true;
14471 }
14472
14473 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14474 Callback's type should be:
14475 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14476 Return 1 if the attributes are present and valid, otherwise, return 0. */
14477
14478 template <typename Callback>
14479 static int
14480 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14481 Callback &&callback)
14482 {
14483 struct dwarf2_per_objfile *dwarf2_per_objfile
14484 = cu->per_cu->dwarf2_per_objfile;
14485 struct objfile *objfile = dwarf2_per_objfile->objfile;
14486 struct comp_unit_head *cu_header = &cu->header;
14487 bfd *obfd = objfile->obfd;
14488 unsigned int addr_size = cu_header->addr_size;
14489 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14490 /* Base address selection entry. */
14491 CORE_ADDR base;
14492 int found_base;
14493 unsigned int dummy;
14494 const gdb_byte *buffer;
14495 CORE_ADDR baseaddr;
14496
14497 if (cu_header->version >= 5)
14498 return dwarf2_rnglists_process (offset, cu, callback);
14499
14500 found_base = cu->base_known;
14501 base = cu->base_address;
14502
14503 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14504 if (offset >= dwarf2_per_objfile->ranges.size)
14505 {
14506 complaint (&symfile_complaints,
14507 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14508 offset);
14509 return 0;
14510 }
14511 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14512
14513 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14514
14515 while (1)
14516 {
14517 CORE_ADDR range_beginning, range_end;
14518
14519 range_beginning = read_address (obfd, buffer, cu, &dummy);
14520 buffer += addr_size;
14521 range_end = read_address (obfd, buffer, cu, &dummy);
14522 buffer += addr_size;
14523 offset += 2 * addr_size;
14524
14525 /* An end of list marker is a pair of zero addresses. */
14526 if (range_beginning == 0 && range_end == 0)
14527 /* Found the end of list entry. */
14528 break;
14529
14530 /* Each base address selection entry is a pair of 2 values.
14531 The first is the largest possible address, the second is
14532 the base address. Check for a base address here. */
14533 if ((range_beginning & mask) == mask)
14534 {
14535 /* If we found the largest possible address, then we already
14536 have the base address in range_end. */
14537 base = range_end;
14538 found_base = 1;
14539 continue;
14540 }
14541
14542 if (!found_base)
14543 {
14544 /* We have no valid base address for the ranges
14545 data. */
14546 complaint (&symfile_complaints,
14547 _("Invalid .debug_ranges data (no base address)"));
14548 return 0;
14549 }
14550
14551 if (range_beginning > range_end)
14552 {
14553 /* Inverted range entries are invalid. */
14554 complaint (&symfile_complaints,
14555 _("Invalid .debug_ranges data (inverted range)"));
14556 return 0;
14557 }
14558
14559 /* Empty range entries have no effect. */
14560 if (range_beginning == range_end)
14561 continue;
14562
14563 range_beginning += base;
14564 range_end += base;
14565
14566 /* A not-uncommon case of bad debug info.
14567 Don't pollute the addrmap with bad data. */
14568 if (range_beginning + baseaddr == 0
14569 && !dwarf2_per_objfile->has_section_at_zero)
14570 {
14571 complaint (&symfile_complaints,
14572 _(".debug_ranges entry has start address of zero"
14573 " [in module %s]"), objfile_name (objfile));
14574 continue;
14575 }
14576
14577 callback (range_beginning, range_end);
14578 }
14579
14580 return 1;
14581 }
14582
14583 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14584 Return 1 if the attributes are present and valid, otherwise, return 0.
14585 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14586
14587 static int
14588 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14589 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14590 struct partial_symtab *ranges_pst)
14591 {
14592 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14593 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14594 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14595 SECT_OFF_TEXT (objfile));
14596 int low_set = 0;
14597 CORE_ADDR low = 0;
14598 CORE_ADDR high = 0;
14599 int retval;
14600
14601 retval = dwarf2_ranges_process (offset, cu,
14602 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14603 {
14604 if (ranges_pst != NULL)
14605 {
14606 CORE_ADDR lowpc;
14607 CORE_ADDR highpc;
14608
14609 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14610 range_beginning + baseaddr);
14611 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14612 range_end + baseaddr);
14613 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14614 ranges_pst);
14615 }
14616
14617 /* FIXME: This is recording everything as a low-high
14618 segment of consecutive addresses. We should have a
14619 data structure for discontiguous block ranges
14620 instead. */
14621 if (! low_set)
14622 {
14623 low = range_beginning;
14624 high = range_end;
14625 low_set = 1;
14626 }
14627 else
14628 {
14629 if (range_beginning < low)
14630 low = range_beginning;
14631 if (range_end > high)
14632 high = range_end;
14633 }
14634 });
14635 if (!retval)
14636 return 0;
14637
14638 if (! low_set)
14639 /* If the first entry is an end-of-list marker, the range
14640 describes an empty scope, i.e. no instructions. */
14641 return 0;
14642
14643 if (low_return)
14644 *low_return = low;
14645 if (high_return)
14646 *high_return = high;
14647 return 1;
14648 }
14649
14650 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14651 definition for the return value. *LOWPC and *HIGHPC are set iff
14652 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14653
14654 static enum pc_bounds_kind
14655 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14656 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14657 struct partial_symtab *pst)
14658 {
14659 struct dwarf2_per_objfile *dwarf2_per_objfile
14660 = cu->per_cu->dwarf2_per_objfile;
14661 struct attribute *attr;
14662 struct attribute *attr_high;
14663 CORE_ADDR low = 0;
14664 CORE_ADDR high = 0;
14665 enum pc_bounds_kind ret;
14666
14667 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14668 if (attr_high)
14669 {
14670 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14671 if (attr)
14672 {
14673 low = attr_value_as_address (attr);
14674 high = attr_value_as_address (attr_high);
14675 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14676 high += low;
14677 }
14678 else
14679 /* Found high w/o low attribute. */
14680 return PC_BOUNDS_INVALID;
14681
14682 /* Found consecutive range of addresses. */
14683 ret = PC_BOUNDS_HIGH_LOW;
14684 }
14685 else
14686 {
14687 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14688 if (attr != NULL)
14689 {
14690 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14691 We take advantage of the fact that DW_AT_ranges does not appear
14692 in DW_TAG_compile_unit of DWO files. */
14693 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14694 unsigned int ranges_offset = (DW_UNSND (attr)
14695 + (need_ranges_base
14696 ? cu->ranges_base
14697 : 0));
14698
14699 /* Value of the DW_AT_ranges attribute is the offset in the
14700 .debug_ranges section. */
14701 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14702 return PC_BOUNDS_INVALID;
14703 /* Found discontinuous range of addresses. */
14704 ret = PC_BOUNDS_RANGES;
14705 }
14706 else
14707 return PC_BOUNDS_NOT_PRESENT;
14708 }
14709
14710 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14711 if (high <= low)
14712 return PC_BOUNDS_INVALID;
14713
14714 /* When using the GNU linker, .gnu.linkonce. sections are used to
14715 eliminate duplicate copies of functions and vtables and such.
14716 The linker will arbitrarily choose one and discard the others.
14717 The AT_*_pc values for such functions refer to local labels in
14718 these sections. If the section from that file was discarded, the
14719 labels are not in the output, so the relocs get a value of 0.
14720 If this is a discarded function, mark the pc bounds as invalid,
14721 so that GDB will ignore it. */
14722 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14723 return PC_BOUNDS_INVALID;
14724
14725 *lowpc = low;
14726 if (highpc)
14727 *highpc = high;
14728 return ret;
14729 }
14730
14731 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14732 its low and high PC addresses. Do nothing if these addresses could not
14733 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14734 and HIGHPC to the high address if greater than HIGHPC. */
14735
14736 static void
14737 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14738 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14739 struct dwarf2_cu *cu)
14740 {
14741 CORE_ADDR low, high;
14742 struct die_info *child = die->child;
14743
14744 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14745 {
14746 *lowpc = std::min (*lowpc, low);
14747 *highpc = std::max (*highpc, high);
14748 }
14749
14750 /* If the language does not allow nested subprograms (either inside
14751 subprograms or lexical blocks), we're done. */
14752 if (cu->language != language_ada)
14753 return;
14754
14755 /* Check all the children of the given DIE. If it contains nested
14756 subprograms, then check their pc bounds. Likewise, we need to
14757 check lexical blocks as well, as they may also contain subprogram
14758 definitions. */
14759 while (child && child->tag)
14760 {
14761 if (child->tag == DW_TAG_subprogram
14762 || child->tag == DW_TAG_lexical_block)
14763 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14764 child = sibling_die (child);
14765 }
14766 }
14767
14768 /* Get the low and high pc's represented by the scope DIE, and store
14769 them in *LOWPC and *HIGHPC. If the correct values can't be
14770 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14771
14772 static void
14773 get_scope_pc_bounds (struct die_info *die,
14774 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14775 struct dwarf2_cu *cu)
14776 {
14777 CORE_ADDR best_low = (CORE_ADDR) -1;
14778 CORE_ADDR best_high = (CORE_ADDR) 0;
14779 CORE_ADDR current_low, current_high;
14780
14781 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14782 >= PC_BOUNDS_RANGES)
14783 {
14784 best_low = current_low;
14785 best_high = current_high;
14786 }
14787 else
14788 {
14789 struct die_info *child = die->child;
14790
14791 while (child && child->tag)
14792 {
14793 switch (child->tag) {
14794 case DW_TAG_subprogram:
14795 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14796 break;
14797 case DW_TAG_namespace:
14798 case DW_TAG_module:
14799 /* FIXME: carlton/2004-01-16: Should we do this for
14800 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14801 that current GCC's always emit the DIEs corresponding
14802 to definitions of methods of classes as children of a
14803 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14804 the DIEs giving the declarations, which could be
14805 anywhere). But I don't see any reason why the
14806 standards says that they have to be there. */
14807 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14808
14809 if (current_low != ((CORE_ADDR) -1))
14810 {
14811 best_low = std::min (best_low, current_low);
14812 best_high = std::max (best_high, current_high);
14813 }
14814 break;
14815 default:
14816 /* Ignore. */
14817 break;
14818 }
14819
14820 child = sibling_die (child);
14821 }
14822 }
14823
14824 *lowpc = best_low;
14825 *highpc = best_high;
14826 }
14827
14828 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14829 in DIE. */
14830
14831 static void
14832 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14833 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14834 {
14835 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14836 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14837 struct attribute *attr;
14838 struct attribute *attr_high;
14839
14840 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14841 if (attr_high)
14842 {
14843 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14844 if (attr)
14845 {
14846 CORE_ADDR low = attr_value_as_address (attr);
14847 CORE_ADDR high = attr_value_as_address (attr_high);
14848
14849 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14850 high += low;
14851
14852 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14853 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14854 record_block_range (block, low, high - 1);
14855 }
14856 }
14857
14858 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14859 if (attr)
14860 {
14861 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14862 We take advantage of the fact that DW_AT_ranges does not appear
14863 in DW_TAG_compile_unit of DWO files. */
14864 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14865
14866 /* The value of the DW_AT_ranges attribute is the offset of the
14867 address range list in the .debug_ranges section. */
14868 unsigned long offset = (DW_UNSND (attr)
14869 + (need_ranges_base ? cu->ranges_base : 0));
14870
14871 dwarf2_ranges_process (offset, cu,
14872 [&] (CORE_ADDR start, CORE_ADDR end)
14873 {
14874 start += baseaddr;
14875 end += baseaddr;
14876 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14877 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14878 record_block_range (block, start, end - 1);
14879 });
14880 }
14881 }
14882
14883 /* Check whether the producer field indicates either of GCC < 4.6, or the
14884 Intel C/C++ compiler, and cache the result in CU. */
14885
14886 static void
14887 check_producer (struct dwarf2_cu *cu)
14888 {
14889 int major, minor;
14890
14891 if (cu->producer == NULL)
14892 {
14893 /* For unknown compilers expect their behavior is DWARF version
14894 compliant.
14895
14896 GCC started to support .debug_types sections by -gdwarf-4 since
14897 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14898 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14899 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14900 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14901 }
14902 else if (producer_is_gcc (cu->producer, &major, &minor))
14903 {
14904 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14905 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14906 }
14907 else if (producer_is_icc (cu->producer, &major, &minor))
14908 cu->producer_is_icc_lt_14 = major < 14;
14909 else
14910 {
14911 /* For other non-GCC compilers, expect their behavior is DWARF version
14912 compliant. */
14913 }
14914
14915 cu->checked_producer = 1;
14916 }
14917
14918 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14919 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14920 during 4.6.0 experimental. */
14921
14922 static int
14923 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14924 {
14925 if (!cu->checked_producer)
14926 check_producer (cu);
14927
14928 return cu->producer_is_gxx_lt_4_6;
14929 }
14930
14931 /* Return the default accessibility type if it is not overriden by
14932 DW_AT_accessibility. */
14933
14934 static enum dwarf_access_attribute
14935 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14936 {
14937 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14938 {
14939 /* The default DWARF 2 accessibility for members is public, the default
14940 accessibility for inheritance is private. */
14941
14942 if (die->tag != DW_TAG_inheritance)
14943 return DW_ACCESS_public;
14944 else
14945 return DW_ACCESS_private;
14946 }
14947 else
14948 {
14949 /* DWARF 3+ defines the default accessibility a different way. The same
14950 rules apply now for DW_TAG_inheritance as for the members and it only
14951 depends on the container kind. */
14952
14953 if (die->parent->tag == DW_TAG_class_type)
14954 return DW_ACCESS_private;
14955 else
14956 return DW_ACCESS_public;
14957 }
14958 }
14959
14960 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14961 offset. If the attribute was not found return 0, otherwise return
14962 1. If it was found but could not properly be handled, set *OFFSET
14963 to 0. */
14964
14965 static int
14966 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14967 LONGEST *offset)
14968 {
14969 struct attribute *attr;
14970
14971 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14972 if (attr != NULL)
14973 {
14974 *offset = 0;
14975
14976 /* Note that we do not check for a section offset first here.
14977 This is because DW_AT_data_member_location is new in DWARF 4,
14978 so if we see it, we can assume that a constant form is really
14979 a constant and not a section offset. */
14980 if (attr_form_is_constant (attr))
14981 *offset = dwarf2_get_attr_constant_value (attr, 0);
14982 else if (attr_form_is_section_offset (attr))
14983 dwarf2_complex_location_expr_complaint ();
14984 else if (attr_form_is_block (attr))
14985 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14986 else
14987 dwarf2_complex_location_expr_complaint ();
14988
14989 return 1;
14990 }
14991
14992 return 0;
14993 }
14994
14995 /* Add an aggregate field to the field list. */
14996
14997 static void
14998 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14999 struct dwarf2_cu *cu)
15000 {
15001 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15002 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15003 struct nextfield *new_field;
15004 struct attribute *attr;
15005 struct field *fp;
15006 const char *fieldname = "";
15007
15008 if (die->tag == DW_TAG_inheritance)
15009 {
15010 fip->baseclasses.emplace_back ();
15011 new_field = &fip->baseclasses.back ();
15012 }
15013 else
15014 {
15015 fip->fields.emplace_back ();
15016 new_field = &fip->fields.back ();
15017 }
15018
15019 fip->nfields++;
15020
15021 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15022 if (attr)
15023 new_field->accessibility = DW_UNSND (attr);
15024 else
15025 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15026 if (new_field->accessibility != DW_ACCESS_public)
15027 fip->non_public_fields = 1;
15028
15029 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15030 if (attr)
15031 new_field->virtuality = DW_UNSND (attr);
15032 else
15033 new_field->virtuality = DW_VIRTUALITY_none;
15034
15035 fp = &new_field->field;
15036
15037 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15038 {
15039 LONGEST offset;
15040
15041 /* Data member other than a C++ static data member. */
15042
15043 /* Get type of field. */
15044 fp->type = die_type (die, cu);
15045
15046 SET_FIELD_BITPOS (*fp, 0);
15047
15048 /* Get bit size of field (zero if none). */
15049 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15050 if (attr)
15051 {
15052 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15053 }
15054 else
15055 {
15056 FIELD_BITSIZE (*fp) = 0;
15057 }
15058
15059 /* Get bit offset of field. */
15060 if (handle_data_member_location (die, cu, &offset))
15061 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15062 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15063 if (attr)
15064 {
15065 if (gdbarch_bits_big_endian (gdbarch))
15066 {
15067 /* For big endian bits, the DW_AT_bit_offset gives the
15068 additional bit offset from the MSB of the containing
15069 anonymous object to the MSB of the field. We don't
15070 have to do anything special since we don't need to
15071 know the size of the anonymous object. */
15072 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15073 }
15074 else
15075 {
15076 /* For little endian bits, compute the bit offset to the
15077 MSB of the anonymous object, subtract off the number of
15078 bits from the MSB of the field to the MSB of the
15079 object, and then subtract off the number of bits of
15080 the field itself. The result is the bit offset of
15081 the LSB of the field. */
15082 int anonymous_size;
15083 int bit_offset = DW_UNSND (attr);
15084
15085 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15086 if (attr)
15087 {
15088 /* The size of the anonymous object containing
15089 the bit field is explicit, so use the
15090 indicated size (in bytes). */
15091 anonymous_size = DW_UNSND (attr);
15092 }
15093 else
15094 {
15095 /* The size of the anonymous object containing
15096 the bit field must be inferred from the type
15097 attribute of the data member containing the
15098 bit field. */
15099 anonymous_size = TYPE_LENGTH (fp->type);
15100 }
15101 SET_FIELD_BITPOS (*fp,
15102 (FIELD_BITPOS (*fp)
15103 + anonymous_size * bits_per_byte
15104 - bit_offset - FIELD_BITSIZE (*fp)));
15105 }
15106 }
15107 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15108 if (attr != NULL)
15109 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15110 + dwarf2_get_attr_constant_value (attr, 0)));
15111
15112 /* Get name of field. */
15113 fieldname = dwarf2_name (die, cu);
15114 if (fieldname == NULL)
15115 fieldname = "";
15116
15117 /* The name is already allocated along with this objfile, so we don't
15118 need to duplicate it for the type. */
15119 fp->name = fieldname;
15120
15121 /* Change accessibility for artificial fields (e.g. virtual table
15122 pointer or virtual base class pointer) to private. */
15123 if (dwarf2_attr (die, DW_AT_artificial, cu))
15124 {
15125 FIELD_ARTIFICIAL (*fp) = 1;
15126 new_field->accessibility = DW_ACCESS_private;
15127 fip->non_public_fields = 1;
15128 }
15129 }
15130 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15131 {
15132 /* C++ static member. */
15133
15134 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15135 is a declaration, but all versions of G++ as of this writing
15136 (so through at least 3.2.1) incorrectly generate
15137 DW_TAG_variable tags. */
15138
15139 const char *physname;
15140
15141 /* Get name of field. */
15142 fieldname = dwarf2_name (die, cu);
15143 if (fieldname == NULL)
15144 return;
15145
15146 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15147 if (attr
15148 /* Only create a symbol if this is an external value.
15149 new_symbol checks this and puts the value in the global symbol
15150 table, which we want. If it is not external, new_symbol
15151 will try to put the value in cu->list_in_scope which is wrong. */
15152 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15153 {
15154 /* A static const member, not much different than an enum as far as
15155 we're concerned, except that we can support more types. */
15156 new_symbol (die, NULL, cu);
15157 }
15158
15159 /* Get physical name. */
15160 physname = dwarf2_physname (fieldname, die, cu);
15161
15162 /* The name is already allocated along with this objfile, so we don't
15163 need to duplicate it for the type. */
15164 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15165 FIELD_TYPE (*fp) = die_type (die, cu);
15166 FIELD_NAME (*fp) = fieldname;
15167 }
15168 else if (die->tag == DW_TAG_inheritance)
15169 {
15170 LONGEST offset;
15171
15172 /* C++ base class field. */
15173 if (handle_data_member_location (die, cu, &offset))
15174 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15175 FIELD_BITSIZE (*fp) = 0;
15176 FIELD_TYPE (*fp) = die_type (die, cu);
15177 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15178 }
15179 else if (die->tag == DW_TAG_variant_part)
15180 {
15181 /* process_structure_scope will treat this DIE as a union. */
15182 process_structure_scope (die, cu);
15183
15184 /* The variant part is relative to the start of the enclosing
15185 structure. */
15186 SET_FIELD_BITPOS (*fp, 0);
15187 fp->type = get_die_type (die, cu);
15188 fp->artificial = 1;
15189 fp->name = "<<variant>>";
15190 }
15191 else
15192 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15193 }
15194
15195 /* Can the type given by DIE define another type? */
15196
15197 static bool
15198 type_can_define_types (const struct die_info *die)
15199 {
15200 switch (die->tag)
15201 {
15202 case DW_TAG_typedef:
15203 case DW_TAG_class_type:
15204 case DW_TAG_structure_type:
15205 case DW_TAG_union_type:
15206 case DW_TAG_enumeration_type:
15207 return true;
15208
15209 default:
15210 return false;
15211 }
15212 }
15213
15214 /* Add a type definition defined in the scope of the FIP's class. */
15215
15216 static void
15217 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15218 struct dwarf2_cu *cu)
15219 {
15220 struct decl_field fp;
15221 memset (&fp, 0, sizeof (fp));
15222
15223 gdb_assert (type_can_define_types (die));
15224
15225 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15226 fp.name = dwarf2_name (die, cu);
15227 fp.type = read_type_die (die, cu);
15228
15229 /* Save accessibility. */
15230 enum dwarf_access_attribute accessibility;
15231 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15232 if (attr != NULL)
15233 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15234 else
15235 accessibility = dwarf2_default_access_attribute (die, cu);
15236 switch (accessibility)
15237 {
15238 case DW_ACCESS_public:
15239 /* The assumed value if neither private nor protected. */
15240 break;
15241 case DW_ACCESS_private:
15242 fp.is_private = 1;
15243 break;
15244 case DW_ACCESS_protected:
15245 fp.is_protected = 1;
15246 break;
15247 default:
15248 complaint (&symfile_complaints,
15249 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15250 }
15251
15252 if (die->tag == DW_TAG_typedef)
15253 fip->typedef_field_list.push_back (fp);
15254 else
15255 fip->nested_types_list.push_back (fp);
15256 }
15257
15258 /* Create the vector of fields, and attach it to the type. */
15259
15260 static void
15261 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15262 struct dwarf2_cu *cu)
15263 {
15264 int nfields = fip->nfields;
15265
15266 /* Record the field count, allocate space for the array of fields,
15267 and create blank accessibility bitfields if necessary. */
15268 TYPE_NFIELDS (type) = nfields;
15269 TYPE_FIELDS (type) = (struct field *)
15270 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15271
15272 if (fip->non_public_fields && cu->language != language_ada)
15273 {
15274 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15275
15276 TYPE_FIELD_PRIVATE_BITS (type) =
15277 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15278 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15279
15280 TYPE_FIELD_PROTECTED_BITS (type) =
15281 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15282 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15283
15284 TYPE_FIELD_IGNORE_BITS (type) =
15285 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15286 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15287 }
15288
15289 /* If the type has baseclasses, allocate and clear a bit vector for
15290 TYPE_FIELD_VIRTUAL_BITS. */
15291 if (!fip->baseclasses.empty () && cu->language != language_ada)
15292 {
15293 int num_bytes = B_BYTES (fip->baseclasses.size ());
15294 unsigned char *pointer;
15295
15296 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15297 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15298 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15299 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15300 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15301 }
15302
15303 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15304 {
15305 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15306
15307 for (int index = 0; index < nfields; ++index)
15308 {
15309 struct nextfield &field = fip->fields[index];
15310
15311 if (field.variant.is_discriminant)
15312 di->discriminant_index = index;
15313 else if (field.variant.default_branch)
15314 di->default_index = index;
15315 else
15316 di->discriminants[index] = field.variant.discriminant_value;
15317 }
15318 }
15319
15320 /* Copy the saved-up fields into the field vector. */
15321 for (int i = 0; i < nfields; ++i)
15322 {
15323 struct nextfield &field
15324 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15325 : fip->fields[i - fip->baseclasses.size ()]);
15326
15327 TYPE_FIELD (type, i) = field.field;
15328 switch (field.accessibility)
15329 {
15330 case DW_ACCESS_private:
15331 if (cu->language != language_ada)
15332 SET_TYPE_FIELD_PRIVATE (type, i);
15333 break;
15334
15335 case DW_ACCESS_protected:
15336 if (cu->language != language_ada)
15337 SET_TYPE_FIELD_PROTECTED (type, i);
15338 break;
15339
15340 case DW_ACCESS_public:
15341 break;
15342
15343 default:
15344 /* Unknown accessibility. Complain and treat it as public. */
15345 {
15346 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15347 field.accessibility);
15348 }
15349 break;
15350 }
15351 if (i < fip->baseclasses.size ())
15352 {
15353 switch (field.virtuality)
15354 {
15355 case DW_VIRTUALITY_virtual:
15356 case DW_VIRTUALITY_pure_virtual:
15357 if (cu->language == language_ada)
15358 error (_("unexpected virtuality in component of Ada type"));
15359 SET_TYPE_FIELD_VIRTUAL (type, i);
15360 break;
15361 }
15362 }
15363 }
15364 }
15365
15366 /* Return true if this member function is a constructor, false
15367 otherwise. */
15368
15369 static int
15370 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15371 {
15372 const char *fieldname;
15373 const char *type_name;
15374 int len;
15375
15376 if (die->parent == NULL)
15377 return 0;
15378
15379 if (die->parent->tag != DW_TAG_structure_type
15380 && die->parent->tag != DW_TAG_union_type
15381 && die->parent->tag != DW_TAG_class_type)
15382 return 0;
15383
15384 fieldname = dwarf2_name (die, cu);
15385 type_name = dwarf2_name (die->parent, cu);
15386 if (fieldname == NULL || type_name == NULL)
15387 return 0;
15388
15389 len = strlen (fieldname);
15390 return (strncmp (fieldname, type_name, len) == 0
15391 && (type_name[len] == '\0' || type_name[len] == '<'));
15392 }
15393
15394 /* Add a member function to the proper fieldlist. */
15395
15396 static void
15397 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15398 struct type *type, struct dwarf2_cu *cu)
15399 {
15400 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15401 struct attribute *attr;
15402 int i;
15403 struct fnfieldlist *flp = nullptr;
15404 struct fn_field *fnp;
15405 const char *fieldname;
15406 struct type *this_type;
15407 enum dwarf_access_attribute accessibility;
15408
15409 if (cu->language == language_ada)
15410 error (_("unexpected member function in Ada type"));
15411
15412 /* Get name of member function. */
15413 fieldname = dwarf2_name (die, cu);
15414 if (fieldname == NULL)
15415 return;
15416
15417 /* Look up member function name in fieldlist. */
15418 for (i = 0; i < fip->fnfieldlists.size (); i++)
15419 {
15420 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15421 {
15422 flp = &fip->fnfieldlists[i];
15423 break;
15424 }
15425 }
15426
15427 /* Create a new fnfieldlist if necessary. */
15428 if (flp == nullptr)
15429 {
15430 fip->fnfieldlists.emplace_back ();
15431 flp = &fip->fnfieldlists.back ();
15432 flp->name = fieldname;
15433 i = fip->fnfieldlists.size () - 1;
15434 }
15435
15436 /* Create a new member function field and add it to the vector of
15437 fnfieldlists. */
15438 flp->fnfields.emplace_back ();
15439 fnp = &flp->fnfields.back ();
15440
15441 /* Delay processing of the physname until later. */
15442 if (cu->language == language_cplus)
15443 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15444 die, cu);
15445 else
15446 {
15447 const char *physname = dwarf2_physname (fieldname, die, cu);
15448 fnp->physname = physname ? physname : "";
15449 }
15450
15451 fnp->type = alloc_type (objfile);
15452 this_type = read_type_die (die, cu);
15453 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15454 {
15455 int nparams = TYPE_NFIELDS (this_type);
15456
15457 /* TYPE is the domain of this method, and THIS_TYPE is the type
15458 of the method itself (TYPE_CODE_METHOD). */
15459 smash_to_method_type (fnp->type, type,
15460 TYPE_TARGET_TYPE (this_type),
15461 TYPE_FIELDS (this_type),
15462 TYPE_NFIELDS (this_type),
15463 TYPE_VARARGS (this_type));
15464
15465 /* Handle static member functions.
15466 Dwarf2 has no clean way to discern C++ static and non-static
15467 member functions. G++ helps GDB by marking the first
15468 parameter for non-static member functions (which is the this
15469 pointer) as artificial. We obtain this information from
15470 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15471 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15472 fnp->voffset = VOFFSET_STATIC;
15473 }
15474 else
15475 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15476 dwarf2_full_name (fieldname, die, cu));
15477
15478 /* Get fcontext from DW_AT_containing_type if present. */
15479 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15480 fnp->fcontext = die_containing_type (die, cu);
15481
15482 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15483 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15484
15485 /* Get accessibility. */
15486 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15487 if (attr)
15488 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15489 else
15490 accessibility = dwarf2_default_access_attribute (die, cu);
15491 switch (accessibility)
15492 {
15493 case DW_ACCESS_private:
15494 fnp->is_private = 1;
15495 break;
15496 case DW_ACCESS_protected:
15497 fnp->is_protected = 1;
15498 break;
15499 }
15500
15501 /* Check for artificial methods. */
15502 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15503 if (attr && DW_UNSND (attr) != 0)
15504 fnp->is_artificial = 1;
15505
15506 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15507
15508 /* Get index in virtual function table if it is a virtual member
15509 function. For older versions of GCC, this is an offset in the
15510 appropriate virtual table, as specified by DW_AT_containing_type.
15511 For everyone else, it is an expression to be evaluated relative
15512 to the object address. */
15513
15514 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15515 if (attr)
15516 {
15517 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15518 {
15519 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15520 {
15521 /* Old-style GCC. */
15522 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15523 }
15524 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15525 || (DW_BLOCK (attr)->size > 1
15526 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15527 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15528 {
15529 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15530 if ((fnp->voffset % cu->header.addr_size) != 0)
15531 dwarf2_complex_location_expr_complaint ();
15532 else
15533 fnp->voffset /= cu->header.addr_size;
15534 fnp->voffset += 2;
15535 }
15536 else
15537 dwarf2_complex_location_expr_complaint ();
15538
15539 if (!fnp->fcontext)
15540 {
15541 /* If there is no `this' field and no DW_AT_containing_type,
15542 we cannot actually find a base class context for the
15543 vtable! */
15544 if (TYPE_NFIELDS (this_type) == 0
15545 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15546 {
15547 complaint (&symfile_complaints,
15548 _("cannot determine context for virtual member "
15549 "function \"%s\" (offset %s)"),
15550 fieldname, sect_offset_str (die->sect_off));
15551 }
15552 else
15553 {
15554 fnp->fcontext
15555 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15556 }
15557 }
15558 }
15559 else if (attr_form_is_section_offset (attr))
15560 {
15561 dwarf2_complex_location_expr_complaint ();
15562 }
15563 else
15564 {
15565 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15566 fieldname);
15567 }
15568 }
15569 else
15570 {
15571 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15572 if (attr && DW_UNSND (attr))
15573 {
15574 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15575 complaint (&symfile_complaints,
15576 _("Member function \"%s\" (offset %s) is virtual "
15577 "but the vtable offset is not specified"),
15578 fieldname, sect_offset_str (die->sect_off));
15579 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15580 TYPE_CPLUS_DYNAMIC (type) = 1;
15581 }
15582 }
15583 }
15584
15585 /* Create the vector of member function fields, and attach it to the type. */
15586
15587 static void
15588 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15589 struct dwarf2_cu *cu)
15590 {
15591 if (cu->language == language_ada)
15592 error (_("unexpected member functions in Ada type"));
15593
15594 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15595 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15596 TYPE_ALLOC (type,
15597 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15598
15599 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15600 {
15601 struct fnfieldlist &nf = fip->fnfieldlists[i];
15602 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15603
15604 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15605 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15606 fn_flp->fn_fields = (struct fn_field *)
15607 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15608
15609 for (int k = 0; k < nf.fnfields.size (); ++k)
15610 fn_flp->fn_fields[k] = nf.fnfields[k];
15611 }
15612
15613 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15614 }
15615
15616 /* Returns non-zero if NAME is the name of a vtable member in CU's
15617 language, zero otherwise. */
15618 static int
15619 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15620 {
15621 static const char vptr[] = "_vptr";
15622
15623 /* Look for the C++ form of the vtable. */
15624 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15625 return 1;
15626
15627 return 0;
15628 }
15629
15630 /* GCC outputs unnamed structures that are really pointers to member
15631 functions, with the ABI-specified layout. If TYPE describes
15632 such a structure, smash it into a member function type.
15633
15634 GCC shouldn't do this; it should just output pointer to member DIEs.
15635 This is GCC PR debug/28767. */
15636
15637 static void
15638 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15639 {
15640 struct type *pfn_type, *self_type, *new_type;
15641
15642 /* Check for a structure with no name and two children. */
15643 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15644 return;
15645
15646 /* Check for __pfn and __delta members. */
15647 if (TYPE_FIELD_NAME (type, 0) == NULL
15648 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15649 || TYPE_FIELD_NAME (type, 1) == NULL
15650 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15651 return;
15652
15653 /* Find the type of the method. */
15654 pfn_type = TYPE_FIELD_TYPE (type, 0);
15655 if (pfn_type == NULL
15656 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15657 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15658 return;
15659
15660 /* Look for the "this" argument. */
15661 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15662 if (TYPE_NFIELDS (pfn_type) == 0
15663 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15664 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15665 return;
15666
15667 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15668 new_type = alloc_type (objfile);
15669 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15670 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15671 TYPE_VARARGS (pfn_type));
15672 smash_to_methodptr_type (type, new_type);
15673 }
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 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15786 {
15787 /* ICC<14 does not output the required DW_AT_declaration on
15788 incomplete types, but gives them a size of zero. */
15789 TYPE_STUB (type) = 1;
15790 }
15791 else
15792 TYPE_STUB_SUPPORTED (type) = 1;
15793
15794 if (die_is_declaration (die, cu))
15795 TYPE_STUB (type) = 1;
15796 else if (attr == NULL && die->child == NULL
15797 && producer_is_realview (cu->producer))
15798 /* RealView does not output the required DW_AT_declaration
15799 on incomplete types. */
15800 TYPE_STUB (type) = 1;
15801
15802 /* We need to add the type field to the die immediately so we don't
15803 infinitely recurse when dealing with pointers to the structure
15804 type within the structure itself. */
15805 set_die_type (die, type, cu);
15806
15807 /* set_die_type should be already done. */
15808 set_descriptive_type (type, die, cu);
15809
15810 return type;
15811 }
15812
15813 /* A helper for process_structure_scope that handles a single member
15814 DIE. */
15815
15816 static void
15817 handle_struct_member_die (struct die_info *child_die, struct type *type,
15818 struct field_info *fi,
15819 std::vector<struct symbol *> *template_args,
15820 struct dwarf2_cu *cu)
15821 {
15822 if (child_die->tag == DW_TAG_member
15823 || child_die->tag == DW_TAG_variable
15824 || child_die->tag == DW_TAG_variant_part)
15825 {
15826 /* NOTE: carlton/2002-11-05: A C++ static data member
15827 should be a DW_TAG_member that is a declaration, but
15828 all versions of G++ as of this writing (so through at
15829 least 3.2.1) incorrectly generate DW_TAG_variable
15830 tags for them instead. */
15831 dwarf2_add_field (fi, child_die, cu);
15832 }
15833 else if (child_die->tag == DW_TAG_subprogram)
15834 {
15835 /* Rust doesn't have member functions in the C++ sense.
15836 However, it does emit ordinary functions as children
15837 of a struct DIE. */
15838 if (cu->language == language_rust)
15839 read_func_scope (child_die, cu);
15840 else
15841 {
15842 /* C++ member function. */
15843 dwarf2_add_member_fn (fi, child_die, type, cu);
15844 }
15845 }
15846 else if (child_die->tag == DW_TAG_inheritance)
15847 {
15848 /* C++ base class field. */
15849 dwarf2_add_field (fi, child_die, cu);
15850 }
15851 else if (type_can_define_types (child_die))
15852 dwarf2_add_type_defn (fi, child_die, cu);
15853 else if (child_die->tag == DW_TAG_template_type_param
15854 || child_die->tag == DW_TAG_template_value_param)
15855 {
15856 struct symbol *arg = new_symbol (child_die, NULL, cu);
15857
15858 if (arg != NULL)
15859 template_args->push_back (arg);
15860 }
15861 else if (child_die->tag == DW_TAG_variant)
15862 {
15863 /* In a variant we want to get the discriminant and also add a
15864 field for our sole member child. */
15865 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15866
15867 for (struct die_info *variant_child = child_die->child;
15868 variant_child != NULL;
15869 variant_child = sibling_die (variant_child))
15870 {
15871 if (variant_child->tag == DW_TAG_member)
15872 {
15873 handle_struct_member_die (variant_child, type, fi,
15874 template_args, cu);
15875 /* Only handle the one. */
15876 break;
15877 }
15878 }
15879
15880 /* We don't handle this but we might as well report it if we see
15881 it. */
15882 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15883 complaint (&symfile_complaints,
15884 _("DW_AT_discr_list is not supported yet"
15885 " - DIE at %s [in module %s]"),
15886 sect_offset_str (child_die->sect_off),
15887 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15888
15889 /* The first field was just added, so we can stash the
15890 discriminant there. */
15891 gdb_assert (!fi->fields.empty ());
15892 if (discr == NULL)
15893 fi->fields.back ().variant.default_branch = true;
15894 else
15895 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15896 }
15897 }
15898
15899 /* Finish creating a structure or union type, including filling in
15900 its members and creating a symbol for it. */
15901
15902 static void
15903 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15904 {
15905 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15906 struct die_info *child_die;
15907 struct type *type;
15908
15909 type = get_die_type (die, cu);
15910 if (type == NULL)
15911 type = read_structure_type (die, cu);
15912
15913 /* When reading a DW_TAG_variant_part, we need to notice when we
15914 read the discriminant member, so we can record it later in the
15915 discriminant_info. */
15916 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15917 sect_offset discr_offset;
15918
15919 if (is_variant_part)
15920 {
15921 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15922 if (discr == NULL)
15923 {
15924 /* Maybe it's a univariant form, an extension we support.
15925 In this case arrange not to check the offset. */
15926 is_variant_part = false;
15927 }
15928 else if (attr_form_is_ref (discr))
15929 {
15930 struct dwarf2_cu *target_cu = cu;
15931 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15932
15933 discr_offset = target_die->sect_off;
15934 }
15935 else
15936 {
15937 complaint (&symfile_complaints,
15938 _("DW_AT_discr does not have DIE reference form"
15939 " - DIE at %s [in module %s]"),
15940 sect_offset_str (die->sect_off),
15941 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15942 is_variant_part = false;
15943 }
15944 }
15945
15946 if (die->child != NULL && ! die_is_declaration (die, cu))
15947 {
15948 struct field_info fi;
15949 std::vector<struct symbol *> template_args;
15950
15951 child_die = die->child;
15952
15953 while (child_die && child_die->tag)
15954 {
15955 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15956
15957 if (is_variant_part && discr_offset == child_die->sect_off)
15958 fi.fields.back ().variant.is_discriminant = true;
15959
15960 child_die = sibling_die (child_die);
15961 }
15962
15963 /* Attach template arguments to type. */
15964 if (!template_args.empty ())
15965 {
15966 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15967 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15968 TYPE_TEMPLATE_ARGUMENTS (type)
15969 = XOBNEWVEC (&objfile->objfile_obstack,
15970 struct symbol *,
15971 TYPE_N_TEMPLATE_ARGUMENTS (type));
15972 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15973 template_args.data (),
15974 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15975 * sizeof (struct symbol *)));
15976 }
15977
15978 /* Attach fields and member functions to the type. */
15979 if (fi.nfields)
15980 dwarf2_attach_fields_to_type (&fi, type, cu);
15981 if (!fi.fnfieldlists.empty ())
15982 {
15983 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15984
15985 /* Get the type which refers to the base class (possibly this
15986 class itself) which contains the vtable pointer for the current
15987 class from the DW_AT_containing_type attribute. This use of
15988 DW_AT_containing_type is a GNU extension. */
15989
15990 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15991 {
15992 struct type *t = die_containing_type (die, cu);
15993
15994 set_type_vptr_basetype (type, t);
15995 if (type == t)
15996 {
15997 int i;
15998
15999 /* Our own class provides vtbl ptr. */
16000 for (i = TYPE_NFIELDS (t) - 1;
16001 i >= TYPE_N_BASECLASSES (t);
16002 --i)
16003 {
16004 const char *fieldname = TYPE_FIELD_NAME (t, i);
16005
16006 if (is_vtable_name (fieldname, cu))
16007 {
16008 set_type_vptr_fieldno (type, i);
16009 break;
16010 }
16011 }
16012
16013 /* Complain if virtual function table field not found. */
16014 if (i < TYPE_N_BASECLASSES (t))
16015 complaint (&symfile_complaints,
16016 _("virtual function table pointer "
16017 "not found when defining class '%s'"),
16018 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16019 "");
16020 }
16021 else
16022 {
16023 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16024 }
16025 }
16026 else if (cu->producer
16027 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16028 {
16029 /* The IBM XLC compiler does not provide direct indication
16030 of the containing type, but the vtable pointer is
16031 always named __vfp. */
16032
16033 int i;
16034
16035 for (i = TYPE_NFIELDS (type) - 1;
16036 i >= TYPE_N_BASECLASSES (type);
16037 --i)
16038 {
16039 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16040 {
16041 set_type_vptr_fieldno (type, i);
16042 set_type_vptr_basetype (type, type);
16043 break;
16044 }
16045 }
16046 }
16047 }
16048
16049 /* Copy fi.typedef_field_list linked list elements content into the
16050 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16051 if (!fi.typedef_field_list.empty ())
16052 {
16053 int count = fi.typedef_field_list.size ();
16054
16055 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16056 TYPE_TYPEDEF_FIELD_ARRAY (type)
16057 = ((struct decl_field *)
16058 TYPE_ALLOC (type,
16059 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16060 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16061
16062 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16063 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16064 }
16065
16066 /* Copy fi.nested_types_list linked list elements content into the
16067 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16068 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16069 {
16070 int count = fi.nested_types_list.size ();
16071
16072 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16073 TYPE_NESTED_TYPES_ARRAY (type)
16074 = ((struct decl_field *)
16075 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16076 TYPE_NESTED_TYPES_COUNT (type) = count;
16077
16078 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16079 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16080 }
16081 }
16082
16083 quirk_gcc_member_function_pointer (type, objfile);
16084 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16085 cu->rust_unions.push_back (type);
16086
16087 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16088 snapshots) has been known to create a die giving a declaration
16089 for a class that has, as a child, a die giving a definition for a
16090 nested class. So we have to process our children even if the
16091 current die is a declaration. Normally, of course, a declaration
16092 won't have any children at all. */
16093
16094 child_die = die->child;
16095
16096 while (child_die != NULL && child_die->tag)
16097 {
16098 if (child_die->tag == DW_TAG_member
16099 || child_die->tag == DW_TAG_variable
16100 || child_die->tag == DW_TAG_inheritance
16101 || child_die->tag == DW_TAG_template_value_param
16102 || child_die->tag == DW_TAG_template_type_param)
16103 {
16104 /* Do nothing. */
16105 }
16106 else
16107 process_die (child_die, cu);
16108
16109 child_die = sibling_die (child_die);
16110 }
16111
16112 /* Do not consider external references. According to the DWARF standard,
16113 these DIEs are identified by the fact that they have no byte_size
16114 attribute, and a declaration attribute. */
16115 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16116 || !die_is_declaration (die, cu))
16117 new_symbol (die, type, cu);
16118 }
16119
16120 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16121 update TYPE using some information only available in DIE's children. */
16122
16123 static void
16124 update_enumeration_type_from_children (struct die_info *die,
16125 struct type *type,
16126 struct dwarf2_cu *cu)
16127 {
16128 struct die_info *child_die;
16129 int unsigned_enum = 1;
16130 int flag_enum = 1;
16131 ULONGEST mask = 0;
16132
16133 auto_obstack obstack;
16134
16135 for (child_die = die->child;
16136 child_die != NULL && child_die->tag;
16137 child_die = sibling_die (child_die))
16138 {
16139 struct attribute *attr;
16140 LONGEST value;
16141 const gdb_byte *bytes;
16142 struct dwarf2_locexpr_baton *baton;
16143 const char *name;
16144
16145 if (child_die->tag != DW_TAG_enumerator)
16146 continue;
16147
16148 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16149 if (attr == NULL)
16150 continue;
16151
16152 name = dwarf2_name (child_die, cu);
16153 if (name == NULL)
16154 name = "<anonymous enumerator>";
16155
16156 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16157 &value, &bytes, &baton);
16158 if (value < 0)
16159 {
16160 unsigned_enum = 0;
16161 flag_enum = 0;
16162 }
16163 else if ((mask & value) != 0)
16164 flag_enum = 0;
16165 else
16166 mask |= value;
16167
16168 /* If we already know that the enum type is neither unsigned, nor
16169 a flag type, no need to look at the rest of the enumerates. */
16170 if (!unsigned_enum && !flag_enum)
16171 break;
16172 }
16173
16174 if (unsigned_enum)
16175 TYPE_UNSIGNED (type) = 1;
16176 if (flag_enum)
16177 TYPE_FLAG_ENUM (type) = 1;
16178 }
16179
16180 /* Given a DW_AT_enumeration_type die, set its type. We do not
16181 complete the type's fields yet, or create any symbols. */
16182
16183 static struct type *
16184 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16185 {
16186 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16187 struct type *type;
16188 struct attribute *attr;
16189 const char *name;
16190
16191 /* If the definition of this type lives in .debug_types, read that type.
16192 Don't follow DW_AT_specification though, that will take us back up
16193 the chain and we want to go down. */
16194 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16195 if (attr)
16196 {
16197 type = get_DW_AT_signature_type (die, attr, cu);
16198
16199 /* The type's CU may not be the same as CU.
16200 Ensure TYPE is recorded with CU in die_type_hash. */
16201 return set_die_type (die, type, cu);
16202 }
16203
16204 type = alloc_type (objfile);
16205
16206 TYPE_CODE (type) = TYPE_CODE_ENUM;
16207 name = dwarf2_full_name (NULL, die, cu);
16208 if (name != NULL)
16209 TYPE_TAG_NAME (type) = name;
16210
16211 attr = dwarf2_attr (die, DW_AT_type, cu);
16212 if (attr != NULL)
16213 {
16214 struct type *underlying_type = die_type (die, cu);
16215
16216 TYPE_TARGET_TYPE (type) = underlying_type;
16217 }
16218
16219 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16220 if (attr)
16221 {
16222 TYPE_LENGTH (type) = DW_UNSND (attr);
16223 }
16224 else
16225 {
16226 TYPE_LENGTH (type) = 0;
16227 }
16228
16229 /* The enumeration DIE can be incomplete. In Ada, any type can be
16230 declared as private in the package spec, and then defined only
16231 inside the package body. Such types are known as Taft Amendment
16232 Types. When another package uses such a type, an incomplete DIE
16233 may be generated by the compiler. */
16234 if (die_is_declaration (die, cu))
16235 TYPE_STUB (type) = 1;
16236
16237 /* Finish the creation of this type by using the enum's children.
16238 We must call this even when the underlying type has been provided
16239 so that we can determine if we're looking at a "flag" enum. */
16240 update_enumeration_type_from_children (die, type, cu);
16241
16242 /* If this type has an underlying type that is not a stub, then we
16243 may use its attributes. We always use the "unsigned" attribute
16244 in this situation, because ordinarily we guess whether the type
16245 is unsigned -- but the guess can be wrong and the underlying type
16246 can tell us the reality. However, we defer to a local size
16247 attribute if one exists, because this lets the compiler override
16248 the underlying type if needed. */
16249 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16250 {
16251 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16252 if (TYPE_LENGTH (type) == 0)
16253 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16254 }
16255
16256 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16257
16258 return set_die_type (die, type, cu);
16259 }
16260
16261 /* Given a pointer to a die which begins an enumeration, process all
16262 the dies that define the members of the enumeration, and create the
16263 symbol for the enumeration type.
16264
16265 NOTE: We reverse the order of the element list. */
16266
16267 static void
16268 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16269 {
16270 struct type *this_type;
16271
16272 this_type = get_die_type (die, cu);
16273 if (this_type == NULL)
16274 this_type = read_enumeration_type (die, cu);
16275
16276 if (die->child != NULL)
16277 {
16278 struct die_info *child_die;
16279 struct symbol *sym;
16280 struct field *fields = NULL;
16281 int num_fields = 0;
16282 const char *name;
16283
16284 child_die = die->child;
16285 while (child_die && child_die->tag)
16286 {
16287 if (child_die->tag != DW_TAG_enumerator)
16288 {
16289 process_die (child_die, cu);
16290 }
16291 else
16292 {
16293 name = dwarf2_name (child_die, cu);
16294 if (name)
16295 {
16296 sym = new_symbol (child_die, this_type, cu);
16297
16298 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16299 {
16300 fields = (struct field *)
16301 xrealloc (fields,
16302 (num_fields + DW_FIELD_ALLOC_CHUNK)
16303 * sizeof (struct field));
16304 }
16305
16306 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16307 FIELD_TYPE (fields[num_fields]) = NULL;
16308 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16309 FIELD_BITSIZE (fields[num_fields]) = 0;
16310
16311 num_fields++;
16312 }
16313 }
16314
16315 child_die = sibling_die (child_die);
16316 }
16317
16318 if (num_fields)
16319 {
16320 TYPE_NFIELDS (this_type) = num_fields;
16321 TYPE_FIELDS (this_type) = (struct field *)
16322 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16323 memcpy (TYPE_FIELDS (this_type), fields,
16324 sizeof (struct field) * num_fields);
16325 xfree (fields);
16326 }
16327 }
16328
16329 /* If we are reading an enum from a .debug_types unit, and the enum
16330 is a declaration, and the enum is not the signatured type in the
16331 unit, then we do not want to add a symbol for it. Adding a
16332 symbol would in some cases obscure the true definition of the
16333 enum, giving users an incomplete type when the definition is
16334 actually available. Note that we do not want to do this for all
16335 enums which are just declarations, because C++0x allows forward
16336 enum declarations. */
16337 if (cu->per_cu->is_debug_types
16338 && die_is_declaration (die, cu))
16339 {
16340 struct signatured_type *sig_type;
16341
16342 sig_type = (struct signatured_type *) cu->per_cu;
16343 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16344 if (sig_type->type_offset_in_section != die->sect_off)
16345 return;
16346 }
16347
16348 new_symbol (die, this_type, cu);
16349 }
16350
16351 /* Extract all information from a DW_TAG_array_type DIE and put it in
16352 the DIE's type field. For now, this only handles one dimensional
16353 arrays. */
16354
16355 static struct type *
16356 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16357 {
16358 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16359 struct die_info *child_die;
16360 struct type *type;
16361 struct type *element_type, *range_type, *index_type;
16362 struct attribute *attr;
16363 const char *name;
16364 struct dynamic_prop *byte_stride_prop = NULL;
16365 unsigned int bit_stride = 0;
16366
16367 element_type = die_type (die, cu);
16368
16369 /* The die_type call above may have already set the type for this DIE. */
16370 type = get_die_type (die, cu);
16371 if (type)
16372 return type;
16373
16374 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16375 if (attr != NULL)
16376 {
16377 int stride_ok;
16378
16379 byte_stride_prop
16380 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16381 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16382 if (!stride_ok)
16383 {
16384 complaint (&symfile_complaints,
16385 _("unable to read array DW_AT_byte_stride "
16386 " - DIE at %s [in module %s]"),
16387 sect_offset_str (die->sect_off),
16388 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16389 /* Ignore this attribute. We will likely not be able to print
16390 arrays of this type correctly, but there is little we can do
16391 to help if we cannot read the attribute's value. */
16392 byte_stride_prop = NULL;
16393 }
16394 }
16395
16396 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16397 if (attr != NULL)
16398 bit_stride = DW_UNSND (attr);
16399
16400 /* Irix 6.2 native cc creates array types without children for
16401 arrays with unspecified length. */
16402 if (die->child == NULL)
16403 {
16404 index_type = objfile_type (objfile)->builtin_int;
16405 range_type = create_static_range_type (NULL, index_type, 0, -1);
16406 type = create_array_type_with_stride (NULL, element_type, range_type,
16407 byte_stride_prop, bit_stride);
16408 return set_die_type (die, type, cu);
16409 }
16410
16411 std::vector<struct type *> range_types;
16412 child_die = die->child;
16413 while (child_die && child_die->tag)
16414 {
16415 if (child_die->tag == DW_TAG_subrange_type)
16416 {
16417 struct type *child_type = read_type_die (child_die, cu);
16418
16419 if (child_type != NULL)
16420 {
16421 /* The range type was succesfully read. Save it for the
16422 array type creation. */
16423 range_types.push_back (child_type);
16424 }
16425 }
16426 child_die = sibling_die (child_die);
16427 }
16428
16429 /* Dwarf2 dimensions are output from left to right, create the
16430 necessary array types in backwards order. */
16431
16432 type = element_type;
16433
16434 if (read_array_order (die, cu) == DW_ORD_col_major)
16435 {
16436 int i = 0;
16437
16438 while (i < range_types.size ())
16439 type = create_array_type_with_stride (NULL, type, range_types[i++],
16440 byte_stride_prop, bit_stride);
16441 }
16442 else
16443 {
16444 size_t ndim = range_types.size ();
16445 while (ndim-- > 0)
16446 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16447 byte_stride_prop, bit_stride);
16448 }
16449
16450 /* Understand Dwarf2 support for vector types (like they occur on
16451 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16452 array type. This is not part of the Dwarf2/3 standard yet, but a
16453 custom vendor extension. The main difference between a regular
16454 array and the vector variant is that vectors are passed by value
16455 to functions. */
16456 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16457 if (attr)
16458 make_vector_type (type);
16459
16460 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16461 implementation may choose to implement triple vectors using this
16462 attribute. */
16463 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16464 if (attr)
16465 {
16466 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16467 TYPE_LENGTH (type) = DW_UNSND (attr);
16468 else
16469 complaint (&symfile_complaints,
16470 _("DW_AT_byte_size for array type smaller "
16471 "than the total size of elements"));
16472 }
16473
16474 name = dwarf2_name (die, cu);
16475 if (name)
16476 TYPE_NAME (type) = name;
16477
16478 /* Install the type in the die. */
16479 set_die_type (die, type, cu);
16480
16481 /* set_die_type should be already done. */
16482 set_descriptive_type (type, die, cu);
16483
16484 return type;
16485 }
16486
16487 static enum dwarf_array_dim_ordering
16488 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16489 {
16490 struct attribute *attr;
16491
16492 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16493
16494 if (attr)
16495 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16496
16497 /* GNU F77 is a special case, as at 08/2004 array type info is the
16498 opposite order to the dwarf2 specification, but data is still
16499 laid out as per normal fortran.
16500
16501 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16502 version checking. */
16503
16504 if (cu->language == language_fortran
16505 && cu->producer && strstr (cu->producer, "GNU F77"))
16506 {
16507 return DW_ORD_row_major;
16508 }
16509
16510 switch (cu->language_defn->la_array_ordering)
16511 {
16512 case array_column_major:
16513 return DW_ORD_col_major;
16514 case array_row_major:
16515 default:
16516 return DW_ORD_row_major;
16517 };
16518 }
16519
16520 /* Extract all information from a DW_TAG_set_type DIE and put it in
16521 the DIE's type field. */
16522
16523 static struct type *
16524 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16525 {
16526 struct type *domain_type, *set_type;
16527 struct attribute *attr;
16528
16529 domain_type = die_type (die, cu);
16530
16531 /* The die_type call above may have already set the type for this DIE. */
16532 set_type = get_die_type (die, cu);
16533 if (set_type)
16534 return set_type;
16535
16536 set_type = create_set_type (NULL, domain_type);
16537
16538 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16539 if (attr)
16540 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16541
16542 return set_die_type (die, set_type, cu);
16543 }
16544
16545 /* A helper for read_common_block that creates a locexpr baton.
16546 SYM is the symbol which we are marking as computed.
16547 COMMON_DIE is the DIE for the common block.
16548 COMMON_LOC is the location expression attribute for the common
16549 block itself.
16550 MEMBER_LOC is the location expression attribute for the particular
16551 member of the common block that we are processing.
16552 CU is the CU from which the above come. */
16553
16554 static void
16555 mark_common_block_symbol_computed (struct symbol *sym,
16556 struct die_info *common_die,
16557 struct attribute *common_loc,
16558 struct attribute *member_loc,
16559 struct dwarf2_cu *cu)
16560 {
16561 struct dwarf2_per_objfile *dwarf2_per_objfile
16562 = cu->per_cu->dwarf2_per_objfile;
16563 struct objfile *objfile = dwarf2_per_objfile->objfile;
16564 struct dwarf2_locexpr_baton *baton;
16565 gdb_byte *ptr;
16566 unsigned int cu_off;
16567 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16568 LONGEST offset = 0;
16569
16570 gdb_assert (common_loc && member_loc);
16571 gdb_assert (attr_form_is_block (common_loc));
16572 gdb_assert (attr_form_is_block (member_loc)
16573 || attr_form_is_constant (member_loc));
16574
16575 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16576 baton->per_cu = cu->per_cu;
16577 gdb_assert (baton->per_cu);
16578
16579 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16580
16581 if (attr_form_is_constant (member_loc))
16582 {
16583 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16584 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16585 }
16586 else
16587 baton->size += DW_BLOCK (member_loc)->size;
16588
16589 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16590 baton->data = ptr;
16591
16592 *ptr++ = DW_OP_call4;
16593 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16594 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16595 ptr += 4;
16596
16597 if (attr_form_is_constant (member_loc))
16598 {
16599 *ptr++ = DW_OP_addr;
16600 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16601 ptr += cu->header.addr_size;
16602 }
16603 else
16604 {
16605 /* We have to copy the data here, because DW_OP_call4 will only
16606 use a DW_AT_location attribute. */
16607 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16608 ptr += DW_BLOCK (member_loc)->size;
16609 }
16610
16611 *ptr++ = DW_OP_plus;
16612 gdb_assert (ptr - baton->data == baton->size);
16613
16614 SYMBOL_LOCATION_BATON (sym) = baton;
16615 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16616 }
16617
16618 /* Create appropriate locally-scoped variables for all the
16619 DW_TAG_common_block entries. Also create a struct common_block
16620 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16621 is used to sepate the common blocks name namespace from regular
16622 variable names. */
16623
16624 static void
16625 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16626 {
16627 struct attribute *attr;
16628
16629 attr = dwarf2_attr (die, DW_AT_location, cu);
16630 if (attr)
16631 {
16632 /* Support the .debug_loc offsets. */
16633 if (attr_form_is_block (attr))
16634 {
16635 /* Ok. */
16636 }
16637 else if (attr_form_is_section_offset (attr))
16638 {
16639 dwarf2_complex_location_expr_complaint ();
16640 attr = NULL;
16641 }
16642 else
16643 {
16644 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16645 "common block member");
16646 attr = NULL;
16647 }
16648 }
16649
16650 if (die->child != NULL)
16651 {
16652 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16653 struct die_info *child_die;
16654 size_t n_entries = 0, size;
16655 struct common_block *common_block;
16656 struct symbol *sym;
16657
16658 for (child_die = die->child;
16659 child_die && child_die->tag;
16660 child_die = sibling_die (child_die))
16661 ++n_entries;
16662
16663 size = (sizeof (struct common_block)
16664 + (n_entries - 1) * sizeof (struct symbol *));
16665 common_block
16666 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16667 size);
16668 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16669 common_block->n_entries = 0;
16670
16671 for (child_die = die->child;
16672 child_die && child_die->tag;
16673 child_die = sibling_die (child_die))
16674 {
16675 /* Create the symbol in the DW_TAG_common_block block in the current
16676 symbol scope. */
16677 sym = new_symbol (child_die, NULL, cu);
16678 if (sym != NULL)
16679 {
16680 struct attribute *member_loc;
16681
16682 common_block->contents[common_block->n_entries++] = sym;
16683
16684 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16685 cu);
16686 if (member_loc)
16687 {
16688 /* GDB has handled this for a long time, but it is
16689 not specified by DWARF. It seems to have been
16690 emitted by gfortran at least as recently as:
16691 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16692 complaint (&symfile_complaints,
16693 _("Variable in common block has "
16694 "DW_AT_data_member_location "
16695 "- DIE at %s [in module %s]"),
16696 sect_offset_str (child_die->sect_off),
16697 objfile_name (objfile));
16698
16699 if (attr_form_is_section_offset (member_loc))
16700 dwarf2_complex_location_expr_complaint ();
16701 else if (attr_form_is_constant (member_loc)
16702 || attr_form_is_block (member_loc))
16703 {
16704 if (attr)
16705 mark_common_block_symbol_computed (sym, die, attr,
16706 member_loc, cu);
16707 }
16708 else
16709 dwarf2_complex_location_expr_complaint ();
16710 }
16711 }
16712 }
16713
16714 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16715 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16716 }
16717 }
16718
16719 /* Create a type for a C++ namespace. */
16720
16721 static struct type *
16722 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16723 {
16724 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16725 const char *previous_prefix, *name;
16726 int is_anonymous;
16727 struct type *type;
16728
16729 /* For extensions, reuse the type of the original namespace. */
16730 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16731 {
16732 struct die_info *ext_die;
16733 struct dwarf2_cu *ext_cu = cu;
16734
16735 ext_die = dwarf2_extension (die, &ext_cu);
16736 type = read_type_die (ext_die, ext_cu);
16737
16738 /* EXT_CU may not be the same as CU.
16739 Ensure TYPE is recorded with CU in die_type_hash. */
16740 return set_die_type (die, type, cu);
16741 }
16742
16743 name = namespace_name (die, &is_anonymous, cu);
16744
16745 /* Now build the name of the current namespace. */
16746
16747 previous_prefix = determine_prefix (die, cu);
16748 if (previous_prefix[0] != '\0')
16749 name = typename_concat (&objfile->objfile_obstack,
16750 previous_prefix, name, 0, cu);
16751
16752 /* Create the type. */
16753 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16754 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16755
16756 return set_die_type (die, type, cu);
16757 }
16758
16759 /* Read a namespace scope. */
16760
16761 static void
16762 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16763 {
16764 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16765 int is_anonymous;
16766
16767 /* Add a symbol associated to this if we haven't seen the namespace
16768 before. Also, add a using directive if it's an anonymous
16769 namespace. */
16770
16771 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16772 {
16773 struct type *type;
16774
16775 type = read_type_die (die, cu);
16776 new_symbol (die, type, cu);
16777
16778 namespace_name (die, &is_anonymous, cu);
16779 if (is_anonymous)
16780 {
16781 const char *previous_prefix = determine_prefix (die, cu);
16782
16783 std::vector<const char *> excludes;
16784 add_using_directive (using_directives (cu->language),
16785 previous_prefix, TYPE_NAME (type), NULL,
16786 NULL, excludes, 0, &objfile->objfile_obstack);
16787 }
16788 }
16789
16790 if (die->child != NULL)
16791 {
16792 struct die_info *child_die = die->child;
16793
16794 while (child_die && child_die->tag)
16795 {
16796 process_die (child_die, cu);
16797 child_die = sibling_die (child_die);
16798 }
16799 }
16800 }
16801
16802 /* Read a Fortran module as type. This DIE can be only a declaration used for
16803 imported module. Still we need that type as local Fortran "use ... only"
16804 declaration imports depend on the created type in determine_prefix. */
16805
16806 static struct type *
16807 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16808 {
16809 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16810 const char *module_name;
16811 struct type *type;
16812
16813 module_name = dwarf2_name (die, cu);
16814 if (!module_name)
16815 complaint (&symfile_complaints,
16816 _("DW_TAG_module has no name, offset %s"),
16817 sect_offset_str (die->sect_off));
16818 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16819
16820 /* determine_prefix uses TYPE_TAG_NAME. */
16821 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16822
16823 return set_die_type (die, type, cu);
16824 }
16825
16826 /* Read a Fortran module. */
16827
16828 static void
16829 read_module (struct die_info *die, struct dwarf2_cu *cu)
16830 {
16831 struct die_info *child_die = die->child;
16832 struct type *type;
16833
16834 type = read_type_die (die, cu);
16835 new_symbol (die, type, cu);
16836
16837 while (child_die && child_die->tag)
16838 {
16839 process_die (child_die, cu);
16840 child_die = sibling_die (child_die);
16841 }
16842 }
16843
16844 /* Return the name of the namespace represented by DIE. Set
16845 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16846 namespace. */
16847
16848 static const char *
16849 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16850 {
16851 struct die_info *current_die;
16852 const char *name = NULL;
16853
16854 /* Loop through the extensions until we find a name. */
16855
16856 for (current_die = die;
16857 current_die != NULL;
16858 current_die = dwarf2_extension (die, &cu))
16859 {
16860 /* We don't use dwarf2_name here so that we can detect the absence
16861 of a name -> anonymous namespace. */
16862 name = dwarf2_string_attr (die, DW_AT_name, cu);
16863
16864 if (name != NULL)
16865 break;
16866 }
16867
16868 /* Is it an anonymous namespace? */
16869
16870 *is_anonymous = (name == NULL);
16871 if (*is_anonymous)
16872 name = CP_ANONYMOUS_NAMESPACE_STR;
16873
16874 return name;
16875 }
16876
16877 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16878 the user defined type vector. */
16879
16880 static struct type *
16881 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16882 {
16883 struct gdbarch *gdbarch
16884 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16885 struct comp_unit_head *cu_header = &cu->header;
16886 struct type *type;
16887 struct attribute *attr_byte_size;
16888 struct attribute *attr_address_class;
16889 int byte_size, addr_class;
16890 struct type *target_type;
16891
16892 target_type = die_type (die, cu);
16893
16894 /* The die_type call above may have already set the type for this DIE. */
16895 type = get_die_type (die, cu);
16896 if (type)
16897 return type;
16898
16899 type = lookup_pointer_type (target_type);
16900
16901 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16902 if (attr_byte_size)
16903 byte_size = DW_UNSND (attr_byte_size);
16904 else
16905 byte_size = cu_header->addr_size;
16906
16907 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16908 if (attr_address_class)
16909 addr_class = DW_UNSND (attr_address_class);
16910 else
16911 addr_class = DW_ADDR_none;
16912
16913 /* If the pointer size or address class is different than the
16914 default, create a type variant marked as such and set the
16915 length accordingly. */
16916 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
16917 {
16918 if (gdbarch_address_class_type_flags_p (gdbarch))
16919 {
16920 int type_flags;
16921
16922 type_flags = gdbarch_address_class_type_flags
16923 (gdbarch, byte_size, addr_class);
16924 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16925 == 0);
16926 type = make_type_with_address_space (type, type_flags);
16927 }
16928 else if (TYPE_LENGTH (type) != byte_size)
16929 {
16930 complaint (&symfile_complaints,
16931 _("invalid pointer size %d"), byte_size);
16932 }
16933 else
16934 {
16935 /* Should we also complain about unhandled address classes? */
16936 }
16937 }
16938
16939 TYPE_LENGTH (type) = byte_size;
16940 return set_die_type (die, type, cu);
16941 }
16942
16943 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16944 the user defined type vector. */
16945
16946 static struct type *
16947 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16948 {
16949 struct type *type;
16950 struct type *to_type;
16951 struct type *domain;
16952
16953 to_type = die_type (die, cu);
16954 domain = die_containing_type (die, cu);
16955
16956 /* The calls above may have already set the type for this DIE. */
16957 type = get_die_type (die, cu);
16958 if (type)
16959 return type;
16960
16961 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16962 type = lookup_methodptr_type (to_type);
16963 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16964 {
16965 struct type *new_type
16966 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16967
16968 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16969 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16970 TYPE_VARARGS (to_type));
16971 type = lookup_methodptr_type (new_type);
16972 }
16973 else
16974 type = lookup_memberptr_type (to_type, domain);
16975
16976 return set_die_type (die, type, cu);
16977 }
16978
16979 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16980 the user defined type vector. */
16981
16982 static struct type *
16983 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16984 enum type_code refcode)
16985 {
16986 struct comp_unit_head *cu_header = &cu->header;
16987 struct type *type, *target_type;
16988 struct attribute *attr;
16989
16990 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16991
16992 target_type = die_type (die, cu);
16993
16994 /* The die_type call above may have already set the type for this DIE. */
16995 type = get_die_type (die, cu);
16996 if (type)
16997 return type;
16998
16999 type = lookup_reference_type (target_type, refcode);
17000 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17001 if (attr)
17002 {
17003 TYPE_LENGTH (type) = DW_UNSND (attr);
17004 }
17005 else
17006 {
17007 TYPE_LENGTH (type) = cu_header->addr_size;
17008 }
17009 return set_die_type (die, type, cu);
17010 }
17011
17012 /* Add the given cv-qualifiers to the element type of the array. GCC
17013 outputs DWARF type qualifiers that apply to an array, not the
17014 element type. But GDB relies on the array element type to carry
17015 the cv-qualifiers. This mimics section 6.7.3 of the C99
17016 specification. */
17017
17018 static struct type *
17019 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17020 struct type *base_type, int cnst, int voltl)
17021 {
17022 struct type *el_type, *inner_array;
17023
17024 base_type = copy_type (base_type);
17025 inner_array = base_type;
17026
17027 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17028 {
17029 TYPE_TARGET_TYPE (inner_array) =
17030 copy_type (TYPE_TARGET_TYPE (inner_array));
17031 inner_array = TYPE_TARGET_TYPE (inner_array);
17032 }
17033
17034 el_type = TYPE_TARGET_TYPE (inner_array);
17035 cnst |= TYPE_CONST (el_type);
17036 voltl |= TYPE_VOLATILE (el_type);
17037 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17038
17039 return set_die_type (die, base_type, cu);
17040 }
17041
17042 static struct type *
17043 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17044 {
17045 struct type *base_type, *cv_type;
17046
17047 base_type = die_type (die, cu);
17048
17049 /* The die_type call above may have already set the type for this DIE. */
17050 cv_type = get_die_type (die, cu);
17051 if (cv_type)
17052 return cv_type;
17053
17054 /* In case the const qualifier is applied to an array type, the element type
17055 is so qualified, not the array type (section 6.7.3 of C99). */
17056 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17057 return add_array_cv_type (die, cu, base_type, 1, 0);
17058
17059 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17060 return set_die_type (die, cv_type, cu);
17061 }
17062
17063 static struct type *
17064 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17065 {
17066 struct type *base_type, *cv_type;
17067
17068 base_type = die_type (die, cu);
17069
17070 /* The die_type call above may have already set the type for this DIE. */
17071 cv_type = get_die_type (die, cu);
17072 if (cv_type)
17073 return cv_type;
17074
17075 /* In case the volatile qualifier is applied to an array type, the
17076 element type is so qualified, not the array type (section 6.7.3
17077 of C99). */
17078 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17079 return add_array_cv_type (die, cu, base_type, 0, 1);
17080
17081 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17082 return set_die_type (die, cv_type, cu);
17083 }
17084
17085 /* Handle DW_TAG_restrict_type. */
17086
17087 static struct type *
17088 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17089 {
17090 struct type *base_type, *cv_type;
17091
17092 base_type = die_type (die, cu);
17093
17094 /* The die_type call above may have already set the type for this DIE. */
17095 cv_type = get_die_type (die, cu);
17096 if (cv_type)
17097 return cv_type;
17098
17099 cv_type = make_restrict_type (base_type);
17100 return set_die_type (die, cv_type, cu);
17101 }
17102
17103 /* Handle DW_TAG_atomic_type. */
17104
17105 static struct type *
17106 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17107 {
17108 struct type *base_type, *cv_type;
17109
17110 base_type = die_type (die, cu);
17111
17112 /* The die_type call above may have already set the type for this DIE. */
17113 cv_type = get_die_type (die, cu);
17114 if (cv_type)
17115 return cv_type;
17116
17117 cv_type = make_atomic_type (base_type);
17118 return set_die_type (die, cv_type, cu);
17119 }
17120
17121 /* Extract all information from a DW_TAG_string_type DIE and add to
17122 the user defined type vector. It isn't really a user defined type,
17123 but it behaves like one, with other DIE's using an AT_user_def_type
17124 attribute to reference it. */
17125
17126 static struct type *
17127 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17128 {
17129 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17130 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17131 struct type *type, *range_type, *index_type, *char_type;
17132 struct attribute *attr;
17133 unsigned int length;
17134
17135 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17136 if (attr)
17137 {
17138 length = DW_UNSND (attr);
17139 }
17140 else
17141 {
17142 /* Check for the DW_AT_byte_size attribute. */
17143 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17144 if (attr)
17145 {
17146 length = DW_UNSND (attr);
17147 }
17148 else
17149 {
17150 length = 1;
17151 }
17152 }
17153
17154 index_type = objfile_type (objfile)->builtin_int;
17155 range_type = create_static_range_type (NULL, index_type, 1, length);
17156 char_type = language_string_char_type (cu->language_defn, gdbarch);
17157 type = create_string_type (NULL, char_type, range_type);
17158
17159 return set_die_type (die, type, cu);
17160 }
17161
17162 /* Assuming that DIE corresponds to a function, returns nonzero
17163 if the function is prototyped. */
17164
17165 static int
17166 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17167 {
17168 struct attribute *attr;
17169
17170 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17171 if (attr && (DW_UNSND (attr) != 0))
17172 return 1;
17173
17174 /* The DWARF standard implies that the DW_AT_prototyped attribute
17175 is only meaninful for C, but the concept also extends to other
17176 languages that allow unprototyped functions (Eg: Objective C).
17177 For all other languages, assume that functions are always
17178 prototyped. */
17179 if (cu->language != language_c
17180 && cu->language != language_objc
17181 && cu->language != language_opencl)
17182 return 1;
17183
17184 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17185 prototyped and unprototyped functions; default to prototyped,
17186 since that is more common in modern code (and RealView warns
17187 about unprototyped functions). */
17188 if (producer_is_realview (cu->producer))
17189 return 1;
17190
17191 return 0;
17192 }
17193
17194 /* Handle DIES due to C code like:
17195
17196 struct foo
17197 {
17198 int (*funcp)(int a, long l);
17199 int b;
17200 };
17201
17202 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17203
17204 static struct type *
17205 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17206 {
17207 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17208 struct type *type; /* Type that this function returns. */
17209 struct type *ftype; /* Function that returns above type. */
17210 struct attribute *attr;
17211
17212 type = die_type (die, cu);
17213
17214 /* The die_type call above may have already set the type for this DIE. */
17215 ftype = get_die_type (die, cu);
17216 if (ftype)
17217 return ftype;
17218
17219 ftype = lookup_function_type (type);
17220
17221 if (prototyped_function_p (die, cu))
17222 TYPE_PROTOTYPED (ftype) = 1;
17223
17224 /* Store the calling convention in the type if it's available in
17225 the subroutine die. Otherwise set the calling convention to
17226 the default value DW_CC_normal. */
17227 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17228 if (attr)
17229 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17230 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17231 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17232 else
17233 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17234
17235 /* Record whether the function returns normally to its caller or not
17236 if the DWARF producer set that information. */
17237 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17238 if (attr && (DW_UNSND (attr) != 0))
17239 TYPE_NO_RETURN (ftype) = 1;
17240
17241 /* We need to add the subroutine type to the die immediately so
17242 we don't infinitely recurse when dealing with parameters
17243 declared as the same subroutine type. */
17244 set_die_type (die, ftype, cu);
17245
17246 if (die->child != NULL)
17247 {
17248 struct type *void_type = objfile_type (objfile)->builtin_void;
17249 struct die_info *child_die;
17250 int nparams, iparams;
17251
17252 /* Count the number of parameters.
17253 FIXME: GDB currently ignores vararg functions, but knows about
17254 vararg member functions. */
17255 nparams = 0;
17256 child_die = die->child;
17257 while (child_die && child_die->tag)
17258 {
17259 if (child_die->tag == DW_TAG_formal_parameter)
17260 nparams++;
17261 else if (child_die->tag == DW_TAG_unspecified_parameters)
17262 TYPE_VARARGS (ftype) = 1;
17263 child_die = sibling_die (child_die);
17264 }
17265
17266 /* Allocate storage for parameters and fill them in. */
17267 TYPE_NFIELDS (ftype) = nparams;
17268 TYPE_FIELDS (ftype) = (struct field *)
17269 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17270
17271 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17272 even if we error out during the parameters reading below. */
17273 for (iparams = 0; iparams < nparams; iparams++)
17274 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17275
17276 iparams = 0;
17277 child_die = die->child;
17278 while (child_die && child_die->tag)
17279 {
17280 if (child_die->tag == DW_TAG_formal_parameter)
17281 {
17282 struct type *arg_type;
17283
17284 /* DWARF version 2 has no clean way to discern C++
17285 static and non-static member functions. G++ helps
17286 GDB by marking the first parameter for non-static
17287 member functions (which is the this pointer) as
17288 artificial. We pass this information to
17289 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17290
17291 DWARF version 3 added DW_AT_object_pointer, which GCC
17292 4.5 does not yet generate. */
17293 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17294 if (attr)
17295 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17296 else
17297 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17298 arg_type = die_type (child_die, cu);
17299
17300 /* RealView does not mark THIS as const, which the testsuite
17301 expects. GCC marks THIS as const in method definitions,
17302 but not in the class specifications (GCC PR 43053). */
17303 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17304 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17305 {
17306 int is_this = 0;
17307 struct dwarf2_cu *arg_cu = cu;
17308 const char *name = dwarf2_name (child_die, cu);
17309
17310 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17311 if (attr)
17312 {
17313 /* If the compiler emits this, use it. */
17314 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17315 is_this = 1;
17316 }
17317 else if (name && strcmp (name, "this") == 0)
17318 /* Function definitions will have the argument names. */
17319 is_this = 1;
17320 else if (name == NULL && iparams == 0)
17321 /* Declarations may not have the names, so like
17322 elsewhere in GDB, assume an artificial first
17323 argument is "this". */
17324 is_this = 1;
17325
17326 if (is_this)
17327 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17328 arg_type, 0);
17329 }
17330
17331 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17332 iparams++;
17333 }
17334 child_die = sibling_die (child_die);
17335 }
17336 }
17337
17338 return ftype;
17339 }
17340
17341 static struct type *
17342 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17343 {
17344 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17345 const char *name = NULL;
17346 struct type *this_type, *target_type;
17347
17348 name = dwarf2_full_name (NULL, die, cu);
17349 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17350 TYPE_TARGET_STUB (this_type) = 1;
17351 set_die_type (die, this_type, cu);
17352 target_type = die_type (die, cu);
17353 if (target_type != this_type)
17354 TYPE_TARGET_TYPE (this_type) = target_type;
17355 else
17356 {
17357 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17358 spec and cause infinite loops in GDB. */
17359 complaint (&symfile_complaints,
17360 _("Self-referential DW_TAG_typedef "
17361 "- DIE at %s [in module %s]"),
17362 sect_offset_str (die->sect_off), objfile_name (objfile));
17363 TYPE_TARGET_TYPE (this_type) = NULL;
17364 }
17365 return this_type;
17366 }
17367
17368 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17369 (which may be different from NAME) to the architecture back-end to allow
17370 it to guess the correct format if necessary. */
17371
17372 static struct type *
17373 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17374 const char *name_hint)
17375 {
17376 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17377 const struct floatformat **format;
17378 struct type *type;
17379
17380 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17381 if (format)
17382 type = init_float_type (objfile, bits, name, format);
17383 else
17384 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17385
17386 return type;
17387 }
17388
17389 /* Find a representation of a given base type and install
17390 it in the TYPE field of the die. */
17391
17392 static struct type *
17393 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17394 {
17395 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17396 struct type *type;
17397 struct attribute *attr;
17398 int encoding = 0, bits = 0;
17399 const char *name;
17400
17401 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17402 if (attr)
17403 {
17404 encoding = DW_UNSND (attr);
17405 }
17406 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17407 if (attr)
17408 {
17409 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17410 }
17411 name = dwarf2_name (die, cu);
17412 if (!name)
17413 {
17414 complaint (&symfile_complaints,
17415 _("DW_AT_name missing from DW_TAG_base_type"));
17416 }
17417
17418 switch (encoding)
17419 {
17420 case DW_ATE_address:
17421 /* Turn DW_ATE_address into a void * pointer. */
17422 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17423 type = init_pointer_type (objfile, bits, name, type);
17424 break;
17425 case DW_ATE_boolean:
17426 type = init_boolean_type (objfile, bits, 1, name);
17427 break;
17428 case DW_ATE_complex_float:
17429 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17430 type = init_complex_type (objfile, name, type);
17431 break;
17432 case DW_ATE_decimal_float:
17433 type = init_decfloat_type (objfile, bits, name);
17434 break;
17435 case DW_ATE_float:
17436 type = dwarf2_init_float_type (objfile, bits, name, name);
17437 break;
17438 case DW_ATE_signed:
17439 type = init_integer_type (objfile, bits, 0, name);
17440 break;
17441 case DW_ATE_unsigned:
17442 if (cu->language == language_fortran
17443 && name
17444 && startswith (name, "character("))
17445 type = init_character_type (objfile, bits, 1, name);
17446 else
17447 type = init_integer_type (objfile, bits, 1, name);
17448 break;
17449 case DW_ATE_signed_char:
17450 if (cu->language == language_ada || cu->language == language_m2
17451 || cu->language == language_pascal
17452 || cu->language == language_fortran)
17453 type = init_character_type (objfile, bits, 0, name);
17454 else
17455 type = init_integer_type (objfile, bits, 0, name);
17456 break;
17457 case DW_ATE_unsigned_char:
17458 if (cu->language == language_ada || cu->language == language_m2
17459 || cu->language == language_pascal
17460 || cu->language == language_fortran
17461 || cu->language == language_rust)
17462 type = init_character_type (objfile, bits, 1, name);
17463 else
17464 type = init_integer_type (objfile, bits, 1, name);
17465 break;
17466 case DW_ATE_UTF:
17467 {
17468 gdbarch *arch = get_objfile_arch (objfile);
17469
17470 if (bits == 16)
17471 type = builtin_type (arch)->builtin_char16;
17472 else if (bits == 32)
17473 type = builtin_type (arch)->builtin_char32;
17474 else
17475 {
17476 complaint (&symfile_complaints,
17477 _("unsupported DW_ATE_UTF bit size: '%d'"),
17478 bits);
17479 type = init_integer_type (objfile, bits, 1, name);
17480 }
17481 return set_die_type (die, type, cu);
17482 }
17483 break;
17484
17485 default:
17486 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17487 dwarf_type_encoding_name (encoding));
17488 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17489 break;
17490 }
17491
17492 if (name && strcmp (name, "char") == 0)
17493 TYPE_NOSIGN (type) = 1;
17494
17495 return set_die_type (die, type, cu);
17496 }
17497
17498 /* Parse dwarf attribute if it's a block, reference or constant and put the
17499 resulting value of the attribute into struct bound_prop.
17500 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17501
17502 static int
17503 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17504 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17505 {
17506 struct dwarf2_property_baton *baton;
17507 struct obstack *obstack
17508 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17509
17510 if (attr == NULL || prop == NULL)
17511 return 0;
17512
17513 if (attr_form_is_block (attr))
17514 {
17515 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17516 baton->referenced_type = NULL;
17517 baton->locexpr.per_cu = cu->per_cu;
17518 baton->locexpr.size = DW_BLOCK (attr)->size;
17519 baton->locexpr.data = DW_BLOCK (attr)->data;
17520 prop->data.baton = baton;
17521 prop->kind = PROP_LOCEXPR;
17522 gdb_assert (prop->data.baton != NULL);
17523 }
17524 else if (attr_form_is_ref (attr))
17525 {
17526 struct dwarf2_cu *target_cu = cu;
17527 struct die_info *target_die;
17528 struct attribute *target_attr;
17529
17530 target_die = follow_die_ref (die, attr, &target_cu);
17531 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17532 if (target_attr == NULL)
17533 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17534 target_cu);
17535 if (target_attr == NULL)
17536 return 0;
17537
17538 switch (target_attr->name)
17539 {
17540 case DW_AT_location:
17541 if (attr_form_is_section_offset (target_attr))
17542 {
17543 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17544 baton->referenced_type = die_type (target_die, target_cu);
17545 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17546 prop->data.baton = baton;
17547 prop->kind = PROP_LOCLIST;
17548 gdb_assert (prop->data.baton != NULL);
17549 }
17550 else if (attr_form_is_block (target_attr))
17551 {
17552 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17553 baton->referenced_type = die_type (target_die, target_cu);
17554 baton->locexpr.per_cu = cu->per_cu;
17555 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17556 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17557 prop->data.baton = baton;
17558 prop->kind = PROP_LOCEXPR;
17559 gdb_assert (prop->data.baton != NULL);
17560 }
17561 else
17562 {
17563 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17564 "dynamic property");
17565 return 0;
17566 }
17567 break;
17568 case DW_AT_data_member_location:
17569 {
17570 LONGEST offset;
17571
17572 if (!handle_data_member_location (target_die, target_cu,
17573 &offset))
17574 return 0;
17575
17576 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17577 baton->referenced_type = read_type_die (target_die->parent,
17578 target_cu);
17579 baton->offset_info.offset = offset;
17580 baton->offset_info.type = die_type (target_die, target_cu);
17581 prop->data.baton = baton;
17582 prop->kind = PROP_ADDR_OFFSET;
17583 break;
17584 }
17585 }
17586 }
17587 else if (attr_form_is_constant (attr))
17588 {
17589 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17590 prop->kind = PROP_CONST;
17591 }
17592 else
17593 {
17594 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17595 dwarf2_name (die, cu));
17596 return 0;
17597 }
17598
17599 return 1;
17600 }
17601
17602 /* Read the given DW_AT_subrange DIE. */
17603
17604 static struct type *
17605 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17606 {
17607 struct type *base_type, *orig_base_type;
17608 struct type *range_type;
17609 struct attribute *attr;
17610 struct dynamic_prop low, high;
17611 int low_default_is_valid;
17612 int high_bound_is_count = 0;
17613 const char *name;
17614 LONGEST negative_mask;
17615
17616 orig_base_type = die_type (die, cu);
17617 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17618 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17619 creating the range type, but we use the result of check_typedef
17620 when examining properties of the type. */
17621 base_type = check_typedef (orig_base_type);
17622
17623 /* The die_type call above may have already set the type for this DIE. */
17624 range_type = get_die_type (die, cu);
17625 if (range_type)
17626 return range_type;
17627
17628 low.kind = PROP_CONST;
17629 high.kind = PROP_CONST;
17630 high.data.const_val = 0;
17631
17632 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17633 omitting DW_AT_lower_bound. */
17634 switch (cu->language)
17635 {
17636 case language_c:
17637 case language_cplus:
17638 low.data.const_val = 0;
17639 low_default_is_valid = 1;
17640 break;
17641 case language_fortran:
17642 low.data.const_val = 1;
17643 low_default_is_valid = 1;
17644 break;
17645 case language_d:
17646 case language_objc:
17647 case language_rust:
17648 low.data.const_val = 0;
17649 low_default_is_valid = (cu->header.version >= 4);
17650 break;
17651 case language_ada:
17652 case language_m2:
17653 case language_pascal:
17654 low.data.const_val = 1;
17655 low_default_is_valid = (cu->header.version >= 4);
17656 break;
17657 default:
17658 low.data.const_val = 0;
17659 low_default_is_valid = 0;
17660 break;
17661 }
17662
17663 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17664 if (attr)
17665 attr_to_dynamic_prop (attr, die, cu, &low);
17666 else if (!low_default_is_valid)
17667 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17668 "- DIE at %s [in module %s]"),
17669 sect_offset_str (die->sect_off),
17670 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17671
17672 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17673 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17674 {
17675 attr = dwarf2_attr (die, DW_AT_count, cu);
17676 if (attr_to_dynamic_prop (attr, die, cu, &high))
17677 {
17678 /* If bounds are constant do the final calculation here. */
17679 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17680 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17681 else
17682 high_bound_is_count = 1;
17683 }
17684 }
17685
17686 /* Dwarf-2 specifications explicitly allows to create subrange types
17687 without specifying a base type.
17688 In that case, the base type must be set to the type of
17689 the lower bound, upper bound or count, in that order, if any of these
17690 three attributes references an object that has a type.
17691 If no base type is found, the Dwarf-2 specifications say that
17692 a signed integer type of size equal to the size of an address should
17693 be used.
17694 For the following C code: `extern char gdb_int [];'
17695 GCC produces an empty range DIE.
17696 FIXME: muller/2010-05-28: Possible references to object for low bound,
17697 high bound or count are not yet handled by this code. */
17698 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17699 {
17700 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17701 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17702 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17703 struct type *int_type = objfile_type (objfile)->builtin_int;
17704
17705 /* Test "int", "long int", and "long long int" objfile types,
17706 and select the first one having a size above or equal to the
17707 architecture address size. */
17708 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17709 base_type = int_type;
17710 else
17711 {
17712 int_type = objfile_type (objfile)->builtin_long;
17713 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17714 base_type = int_type;
17715 else
17716 {
17717 int_type = objfile_type (objfile)->builtin_long_long;
17718 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17719 base_type = int_type;
17720 }
17721 }
17722 }
17723
17724 /* Normally, the DWARF producers are expected to use a signed
17725 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17726 But this is unfortunately not always the case, as witnessed
17727 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17728 is used instead. To work around that ambiguity, we treat
17729 the bounds as signed, and thus sign-extend their values, when
17730 the base type is signed. */
17731 negative_mask =
17732 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17733 if (low.kind == PROP_CONST
17734 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17735 low.data.const_val |= negative_mask;
17736 if (high.kind == PROP_CONST
17737 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17738 high.data.const_val |= negative_mask;
17739
17740 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17741
17742 if (high_bound_is_count)
17743 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17744
17745 /* Ada expects an empty array on no boundary attributes. */
17746 if (attr == NULL && cu->language != language_ada)
17747 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17748
17749 name = dwarf2_name (die, cu);
17750 if (name)
17751 TYPE_NAME (range_type) = name;
17752
17753 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17754 if (attr)
17755 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17756
17757 set_die_type (die, range_type, cu);
17758
17759 /* set_die_type should be already done. */
17760 set_descriptive_type (range_type, die, cu);
17761
17762 return range_type;
17763 }
17764
17765 static struct type *
17766 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17767 {
17768 struct type *type;
17769
17770 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17771 NULL);
17772 TYPE_NAME (type) = dwarf2_name (die, cu);
17773
17774 /* In Ada, an unspecified type is typically used when the description
17775 of the type is defered to a different unit. When encountering
17776 such a type, we treat it as a stub, and try to resolve it later on,
17777 when needed. */
17778 if (cu->language == language_ada)
17779 TYPE_STUB (type) = 1;
17780
17781 return set_die_type (die, type, cu);
17782 }
17783
17784 /* Read a single die and all its descendents. Set the die's sibling
17785 field to NULL; set other fields in the die correctly, and set all
17786 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17787 location of the info_ptr after reading all of those dies. PARENT
17788 is the parent of the die in question. */
17789
17790 static struct die_info *
17791 read_die_and_children (const struct die_reader_specs *reader,
17792 const gdb_byte *info_ptr,
17793 const gdb_byte **new_info_ptr,
17794 struct die_info *parent)
17795 {
17796 struct die_info *die;
17797 const gdb_byte *cur_ptr;
17798 int has_children;
17799
17800 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17801 if (die == NULL)
17802 {
17803 *new_info_ptr = cur_ptr;
17804 return NULL;
17805 }
17806 store_in_ref_table (die, reader->cu);
17807
17808 if (has_children)
17809 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17810 else
17811 {
17812 die->child = NULL;
17813 *new_info_ptr = cur_ptr;
17814 }
17815
17816 die->sibling = NULL;
17817 die->parent = parent;
17818 return die;
17819 }
17820
17821 /* Read a die, all of its descendents, and all of its siblings; set
17822 all of the fields of all of the dies correctly. Arguments are as
17823 in read_die_and_children. */
17824
17825 static struct die_info *
17826 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17827 const gdb_byte *info_ptr,
17828 const gdb_byte **new_info_ptr,
17829 struct die_info *parent)
17830 {
17831 struct die_info *first_die, *last_sibling;
17832 const gdb_byte *cur_ptr;
17833
17834 cur_ptr = info_ptr;
17835 first_die = last_sibling = NULL;
17836
17837 while (1)
17838 {
17839 struct die_info *die
17840 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17841
17842 if (die == NULL)
17843 {
17844 *new_info_ptr = cur_ptr;
17845 return first_die;
17846 }
17847
17848 if (!first_die)
17849 first_die = die;
17850 else
17851 last_sibling->sibling = die;
17852
17853 last_sibling = die;
17854 }
17855 }
17856
17857 /* Read a die, all of its descendents, and all of its siblings; set
17858 all of the fields of all of the dies correctly. Arguments are as
17859 in read_die_and_children.
17860 This the main entry point for reading a DIE and all its children. */
17861
17862 static struct die_info *
17863 read_die_and_siblings (const struct die_reader_specs *reader,
17864 const gdb_byte *info_ptr,
17865 const gdb_byte **new_info_ptr,
17866 struct die_info *parent)
17867 {
17868 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17869 new_info_ptr, parent);
17870
17871 if (dwarf_die_debug)
17872 {
17873 fprintf_unfiltered (gdb_stdlog,
17874 "Read die from %s@0x%x of %s:\n",
17875 get_section_name (reader->die_section),
17876 (unsigned) (info_ptr - reader->die_section->buffer),
17877 bfd_get_filename (reader->abfd));
17878 dump_die (die, dwarf_die_debug);
17879 }
17880
17881 return die;
17882 }
17883
17884 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17885 attributes.
17886 The caller is responsible for filling in the extra attributes
17887 and updating (*DIEP)->num_attrs.
17888 Set DIEP to point to a newly allocated die with its information,
17889 except for its child, sibling, and parent fields.
17890 Set HAS_CHILDREN to tell whether the die has children or not. */
17891
17892 static const gdb_byte *
17893 read_full_die_1 (const struct die_reader_specs *reader,
17894 struct die_info **diep, const gdb_byte *info_ptr,
17895 int *has_children, int num_extra_attrs)
17896 {
17897 unsigned int abbrev_number, bytes_read, i;
17898 struct abbrev_info *abbrev;
17899 struct die_info *die;
17900 struct dwarf2_cu *cu = reader->cu;
17901 bfd *abfd = reader->abfd;
17902
17903 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17904 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17905 info_ptr += bytes_read;
17906 if (!abbrev_number)
17907 {
17908 *diep = NULL;
17909 *has_children = 0;
17910 return info_ptr;
17911 }
17912
17913 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17914 if (!abbrev)
17915 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17916 abbrev_number,
17917 bfd_get_filename (abfd));
17918
17919 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17920 die->sect_off = sect_off;
17921 die->tag = abbrev->tag;
17922 die->abbrev = abbrev_number;
17923
17924 /* Make the result usable.
17925 The caller needs to update num_attrs after adding the extra
17926 attributes. */
17927 die->num_attrs = abbrev->num_attrs;
17928
17929 for (i = 0; i < abbrev->num_attrs; ++i)
17930 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17931 info_ptr);
17932
17933 *diep = die;
17934 *has_children = abbrev->has_children;
17935 return info_ptr;
17936 }
17937
17938 /* Read a die and all its attributes.
17939 Set DIEP to point to a newly allocated die with its information,
17940 except for its child, sibling, and parent fields.
17941 Set HAS_CHILDREN to tell whether the die has children or not. */
17942
17943 static const gdb_byte *
17944 read_full_die (const struct die_reader_specs *reader,
17945 struct die_info **diep, const gdb_byte *info_ptr,
17946 int *has_children)
17947 {
17948 const gdb_byte *result;
17949
17950 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17951
17952 if (dwarf_die_debug)
17953 {
17954 fprintf_unfiltered (gdb_stdlog,
17955 "Read die from %s@0x%x of %s:\n",
17956 get_section_name (reader->die_section),
17957 (unsigned) (info_ptr - reader->die_section->buffer),
17958 bfd_get_filename (reader->abfd));
17959 dump_die (*diep, dwarf_die_debug);
17960 }
17961
17962 return result;
17963 }
17964 \f
17965 /* Abbreviation tables.
17966
17967 In DWARF version 2, the description of the debugging information is
17968 stored in a separate .debug_abbrev section. Before we read any
17969 dies from a section we read in all abbreviations and install them
17970 in a hash table. */
17971
17972 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17973
17974 struct abbrev_info *
17975 abbrev_table::alloc_abbrev ()
17976 {
17977 struct abbrev_info *abbrev;
17978
17979 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
17980 memset (abbrev, 0, sizeof (struct abbrev_info));
17981
17982 return abbrev;
17983 }
17984
17985 /* Add an abbreviation to the table. */
17986
17987 void
17988 abbrev_table::add_abbrev (unsigned int abbrev_number,
17989 struct abbrev_info *abbrev)
17990 {
17991 unsigned int hash_number;
17992
17993 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17994 abbrev->next = m_abbrevs[hash_number];
17995 m_abbrevs[hash_number] = abbrev;
17996 }
17997
17998 /* Look up an abbrev in the table.
17999 Returns NULL if the abbrev is not found. */
18000
18001 struct abbrev_info *
18002 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18003 {
18004 unsigned int hash_number;
18005 struct abbrev_info *abbrev;
18006
18007 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18008 abbrev = m_abbrevs[hash_number];
18009
18010 while (abbrev)
18011 {
18012 if (abbrev->number == abbrev_number)
18013 return abbrev;
18014 abbrev = abbrev->next;
18015 }
18016 return NULL;
18017 }
18018
18019 /* Read in an abbrev table. */
18020
18021 static abbrev_table_up
18022 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18023 struct dwarf2_section_info *section,
18024 sect_offset sect_off)
18025 {
18026 struct objfile *objfile = dwarf2_per_objfile->objfile;
18027 bfd *abfd = get_section_bfd_owner (section);
18028 const gdb_byte *abbrev_ptr;
18029 struct abbrev_info *cur_abbrev;
18030 unsigned int abbrev_number, bytes_read, abbrev_name;
18031 unsigned int abbrev_form;
18032 struct attr_abbrev *cur_attrs;
18033 unsigned int allocated_attrs;
18034
18035 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18036
18037 dwarf2_read_section (objfile, section);
18038 abbrev_ptr = section->buffer + to_underlying (sect_off);
18039 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18040 abbrev_ptr += bytes_read;
18041
18042 allocated_attrs = ATTR_ALLOC_CHUNK;
18043 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18044
18045 /* Loop until we reach an abbrev number of 0. */
18046 while (abbrev_number)
18047 {
18048 cur_abbrev = abbrev_table->alloc_abbrev ();
18049
18050 /* read in abbrev header */
18051 cur_abbrev->number = abbrev_number;
18052 cur_abbrev->tag
18053 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18054 abbrev_ptr += bytes_read;
18055 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18056 abbrev_ptr += 1;
18057
18058 /* now read in declarations */
18059 for (;;)
18060 {
18061 LONGEST implicit_const;
18062
18063 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18064 abbrev_ptr += bytes_read;
18065 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18066 abbrev_ptr += bytes_read;
18067 if (abbrev_form == DW_FORM_implicit_const)
18068 {
18069 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18070 &bytes_read);
18071 abbrev_ptr += bytes_read;
18072 }
18073 else
18074 {
18075 /* Initialize it due to a false compiler warning. */
18076 implicit_const = -1;
18077 }
18078
18079 if (abbrev_name == 0)
18080 break;
18081
18082 if (cur_abbrev->num_attrs == allocated_attrs)
18083 {
18084 allocated_attrs += ATTR_ALLOC_CHUNK;
18085 cur_attrs
18086 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18087 }
18088
18089 cur_attrs[cur_abbrev->num_attrs].name
18090 = (enum dwarf_attribute) abbrev_name;
18091 cur_attrs[cur_abbrev->num_attrs].form
18092 = (enum dwarf_form) abbrev_form;
18093 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18094 ++cur_abbrev->num_attrs;
18095 }
18096
18097 cur_abbrev->attrs =
18098 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18099 cur_abbrev->num_attrs);
18100 memcpy (cur_abbrev->attrs, cur_attrs,
18101 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18102
18103 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18104
18105 /* Get next abbreviation.
18106 Under Irix6 the abbreviations for a compilation unit are not
18107 always properly terminated with an abbrev number of 0.
18108 Exit loop if we encounter an abbreviation which we have
18109 already read (which means we are about to read the abbreviations
18110 for the next compile unit) or if the end of the abbreviation
18111 table is reached. */
18112 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18113 break;
18114 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18115 abbrev_ptr += bytes_read;
18116 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18117 break;
18118 }
18119
18120 xfree (cur_attrs);
18121 return abbrev_table;
18122 }
18123
18124 /* Returns nonzero if TAG represents a type that we might generate a partial
18125 symbol for. */
18126
18127 static int
18128 is_type_tag_for_partial (int tag)
18129 {
18130 switch (tag)
18131 {
18132 #if 0
18133 /* Some types that would be reasonable to generate partial symbols for,
18134 that we don't at present. */
18135 case DW_TAG_array_type:
18136 case DW_TAG_file_type:
18137 case DW_TAG_ptr_to_member_type:
18138 case DW_TAG_set_type:
18139 case DW_TAG_string_type:
18140 case DW_TAG_subroutine_type:
18141 #endif
18142 case DW_TAG_base_type:
18143 case DW_TAG_class_type:
18144 case DW_TAG_interface_type:
18145 case DW_TAG_enumeration_type:
18146 case DW_TAG_structure_type:
18147 case DW_TAG_subrange_type:
18148 case DW_TAG_typedef:
18149 case DW_TAG_union_type:
18150 return 1;
18151 default:
18152 return 0;
18153 }
18154 }
18155
18156 /* Load all DIEs that are interesting for partial symbols into memory. */
18157
18158 static struct partial_die_info *
18159 load_partial_dies (const struct die_reader_specs *reader,
18160 const gdb_byte *info_ptr, int building_psymtab)
18161 {
18162 struct dwarf2_cu *cu = reader->cu;
18163 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18164 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18165 unsigned int bytes_read;
18166 unsigned int load_all = 0;
18167 int nesting_level = 1;
18168
18169 parent_die = NULL;
18170 last_die = NULL;
18171
18172 gdb_assert (cu->per_cu != NULL);
18173 if (cu->per_cu->load_all_dies)
18174 load_all = 1;
18175
18176 cu->partial_dies
18177 = htab_create_alloc_ex (cu->header.length / 12,
18178 partial_die_hash,
18179 partial_die_eq,
18180 NULL,
18181 &cu->comp_unit_obstack,
18182 hashtab_obstack_allocate,
18183 dummy_obstack_deallocate);
18184
18185 while (1)
18186 {
18187 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18188
18189 /* A NULL abbrev means the end of a series of children. */
18190 if (abbrev == NULL)
18191 {
18192 if (--nesting_level == 0)
18193 return first_die;
18194
18195 info_ptr += bytes_read;
18196 last_die = parent_die;
18197 parent_die = parent_die->die_parent;
18198 continue;
18199 }
18200
18201 /* Check for template arguments. We never save these; if
18202 they're seen, we just mark the parent, and go on our way. */
18203 if (parent_die != NULL
18204 && cu->language == language_cplus
18205 && (abbrev->tag == DW_TAG_template_type_param
18206 || abbrev->tag == DW_TAG_template_value_param))
18207 {
18208 parent_die->has_template_arguments = 1;
18209
18210 if (!load_all)
18211 {
18212 /* We don't need a partial DIE for the template argument. */
18213 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18214 continue;
18215 }
18216 }
18217
18218 /* We only recurse into c++ subprograms looking for template arguments.
18219 Skip their other children. */
18220 if (!load_all
18221 && cu->language == language_cplus
18222 && parent_die != NULL
18223 && parent_die->tag == DW_TAG_subprogram)
18224 {
18225 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18226 continue;
18227 }
18228
18229 /* Check whether this DIE is interesting enough to save. Normally
18230 we would not be interested in members here, but there may be
18231 later variables referencing them via DW_AT_specification (for
18232 static members). */
18233 if (!load_all
18234 && !is_type_tag_for_partial (abbrev->tag)
18235 && abbrev->tag != DW_TAG_constant
18236 && abbrev->tag != DW_TAG_enumerator
18237 && abbrev->tag != DW_TAG_subprogram
18238 && abbrev->tag != DW_TAG_inlined_subroutine
18239 && abbrev->tag != DW_TAG_lexical_block
18240 && abbrev->tag != DW_TAG_variable
18241 && abbrev->tag != DW_TAG_namespace
18242 && abbrev->tag != DW_TAG_module
18243 && abbrev->tag != DW_TAG_member
18244 && abbrev->tag != DW_TAG_imported_unit
18245 && abbrev->tag != DW_TAG_imported_declaration)
18246 {
18247 /* Otherwise we skip to the next sibling, if any. */
18248 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18249 continue;
18250 }
18251
18252 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18253 abbrev);
18254
18255 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18256
18257 /* This two-pass algorithm for processing partial symbols has a
18258 high cost in cache pressure. Thus, handle some simple cases
18259 here which cover the majority of C partial symbols. DIEs
18260 which neither have specification tags in them, nor could have
18261 specification tags elsewhere pointing at them, can simply be
18262 processed and discarded.
18263
18264 This segment is also optional; scan_partial_symbols and
18265 add_partial_symbol will handle these DIEs if we chain
18266 them in normally. When compilers which do not emit large
18267 quantities of duplicate debug information are more common,
18268 this code can probably be removed. */
18269
18270 /* Any complete simple types at the top level (pretty much all
18271 of them, for a language without namespaces), can be processed
18272 directly. */
18273 if (parent_die == NULL
18274 && pdi.has_specification == 0
18275 && pdi.is_declaration == 0
18276 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18277 || pdi.tag == DW_TAG_base_type
18278 || pdi.tag == DW_TAG_subrange_type))
18279 {
18280 if (building_psymtab && pdi.name != NULL)
18281 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18282 VAR_DOMAIN, LOC_TYPEDEF,
18283 &objfile->static_psymbols,
18284 0, cu->language, objfile);
18285 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18286 continue;
18287 }
18288
18289 /* The exception for DW_TAG_typedef with has_children above is
18290 a workaround of GCC PR debug/47510. In the case of this complaint
18291 type_name_no_tag_or_error will error on such types later.
18292
18293 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18294 it could not find the child DIEs referenced later, this is checked
18295 above. In correct DWARF DW_TAG_typedef should have no children. */
18296
18297 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18298 complaint (&symfile_complaints,
18299 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18300 "- DIE at %s [in module %s]"),
18301 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18302
18303 /* If we're at the second level, and we're an enumerator, and
18304 our parent has no specification (meaning possibly lives in a
18305 namespace elsewhere), then we can add the partial symbol now
18306 instead of queueing it. */
18307 if (pdi.tag == DW_TAG_enumerator
18308 && parent_die != NULL
18309 && parent_die->die_parent == NULL
18310 && parent_die->tag == DW_TAG_enumeration_type
18311 && parent_die->has_specification == 0)
18312 {
18313 if (pdi.name == NULL)
18314 complaint (&symfile_complaints,
18315 _("malformed enumerator DIE ignored"));
18316 else if (building_psymtab)
18317 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18318 VAR_DOMAIN, LOC_CONST,
18319 cu->language == language_cplus
18320 ? &objfile->global_psymbols
18321 : &objfile->static_psymbols,
18322 0, cu->language, objfile);
18323
18324 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18325 continue;
18326 }
18327
18328 struct partial_die_info *part_die
18329 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18330
18331 /* We'll save this DIE so link it in. */
18332 part_die->die_parent = parent_die;
18333 part_die->die_sibling = NULL;
18334 part_die->die_child = NULL;
18335
18336 if (last_die && last_die == parent_die)
18337 last_die->die_child = part_die;
18338 else if (last_die)
18339 last_die->die_sibling = part_die;
18340
18341 last_die = part_die;
18342
18343 if (first_die == NULL)
18344 first_die = part_die;
18345
18346 /* Maybe add the DIE to the hash table. Not all DIEs that we
18347 find interesting need to be in the hash table, because we
18348 also have the parent/sibling/child chains; only those that we
18349 might refer to by offset later during partial symbol reading.
18350
18351 For now this means things that might have be the target of a
18352 DW_AT_specification, DW_AT_abstract_origin, or
18353 DW_AT_extension. DW_AT_extension will refer only to
18354 namespaces; DW_AT_abstract_origin refers to functions (and
18355 many things under the function DIE, but we do not recurse
18356 into function DIEs during partial symbol reading) and
18357 possibly variables as well; DW_AT_specification refers to
18358 declarations. Declarations ought to have the DW_AT_declaration
18359 flag. It happens that GCC forgets to put it in sometimes, but
18360 only for functions, not for types.
18361
18362 Adding more things than necessary to the hash table is harmless
18363 except for the performance cost. Adding too few will result in
18364 wasted time in find_partial_die, when we reread the compilation
18365 unit with load_all_dies set. */
18366
18367 if (load_all
18368 || abbrev->tag == DW_TAG_constant
18369 || abbrev->tag == DW_TAG_subprogram
18370 || abbrev->tag == DW_TAG_variable
18371 || abbrev->tag == DW_TAG_namespace
18372 || part_die->is_declaration)
18373 {
18374 void **slot;
18375
18376 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18377 to_underlying (part_die->sect_off),
18378 INSERT);
18379 *slot = part_die;
18380 }
18381
18382 /* For some DIEs we want to follow their children (if any). For C
18383 we have no reason to follow the children of structures; for other
18384 languages we have to, so that we can get at method physnames
18385 to infer fully qualified class names, for DW_AT_specification,
18386 and for C++ template arguments. For C++, we also look one level
18387 inside functions to find template arguments (if the name of the
18388 function does not already contain the template arguments).
18389
18390 For Ada, we need to scan the children of subprograms and lexical
18391 blocks as well because Ada allows the definition of nested
18392 entities that could be interesting for the debugger, such as
18393 nested subprograms for instance. */
18394 if (last_die->has_children
18395 && (load_all
18396 || last_die->tag == DW_TAG_namespace
18397 || last_die->tag == DW_TAG_module
18398 || last_die->tag == DW_TAG_enumeration_type
18399 || (cu->language == language_cplus
18400 && last_die->tag == DW_TAG_subprogram
18401 && (last_die->name == NULL
18402 || strchr (last_die->name, '<') == NULL))
18403 || (cu->language != language_c
18404 && (last_die->tag == DW_TAG_class_type
18405 || last_die->tag == DW_TAG_interface_type
18406 || last_die->tag == DW_TAG_structure_type
18407 || last_die->tag == DW_TAG_union_type))
18408 || (cu->language == language_ada
18409 && (last_die->tag == DW_TAG_subprogram
18410 || last_die->tag == DW_TAG_lexical_block))))
18411 {
18412 nesting_level++;
18413 parent_die = last_die;
18414 continue;
18415 }
18416
18417 /* Otherwise we skip to the next sibling, if any. */
18418 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18419
18420 /* Back to the top, do it again. */
18421 }
18422 }
18423
18424 partial_die_info::partial_die_info (sect_offset sect_off_,
18425 struct abbrev_info *abbrev)
18426 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18427 {
18428 }
18429
18430 /* Read a minimal amount of information into the minimal die structure.
18431 INFO_PTR should point just after the initial uleb128 of a DIE. */
18432
18433 const gdb_byte *
18434 partial_die_info::read (const struct die_reader_specs *reader,
18435 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18436 {
18437 struct dwarf2_cu *cu = reader->cu;
18438 struct dwarf2_per_objfile *dwarf2_per_objfile
18439 = cu->per_cu->dwarf2_per_objfile;
18440 unsigned int i;
18441 int has_low_pc_attr = 0;
18442 int has_high_pc_attr = 0;
18443 int high_pc_relative = 0;
18444
18445 for (i = 0; i < abbrev.num_attrs; ++i)
18446 {
18447 struct attribute attr;
18448
18449 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18450
18451 /* Store the data if it is of an attribute we want to keep in a
18452 partial symbol table. */
18453 switch (attr.name)
18454 {
18455 case DW_AT_name:
18456 switch (tag)
18457 {
18458 case DW_TAG_compile_unit:
18459 case DW_TAG_partial_unit:
18460 case DW_TAG_type_unit:
18461 /* Compilation units have a DW_AT_name that is a filename, not
18462 a source language identifier. */
18463 case DW_TAG_enumeration_type:
18464 case DW_TAG_enumerator:
18465 /* These tags always have simple identifiers already; no need
18466 to canonicalize them. */
18467 name = DW_STRING (&attr);
18468 break;
18469 default:
18470 {
18471 struct objfile *objfile = dwarf2_per_objfile->objfile;
18472
18473 name
18474 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18475 &objfile->per_bfd->storage_obstack);
18476 }
18477 break;
18478 }
18479 break;
18480 case DW_AT_linkage_name:
18481 case DW_AT_MIPS_linkage_name:
18482 /* Note that both forms of linkage name might appear. We
18483 assume they will be the same, and we only store the last
18484 one we see. */
18485 if (cu->language == language_ada)
18486 name = DW_STRING (&attr);
18487 linkage_name = DW_STRING (&attr);
18488 break;
18489 case DW_AT_low_pc:
18490 has_low_pc_attr = 1;
18491 lowpc = attr_value_as_address (&attr);
18492 break;
18493 case DW_AT_high_pc:
18494 has_high_pc_attr = 1;
18495 highpc = attr_value_as_address (&attr);
18496 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18497 high_pc_relative = 1;
18498 break;
18499 case DW_AT_location:
18500 /* Support the .debug_loc offsets. */
18501 if (attr_form_is_block (&attr))
18502 {
18503 d.locdesc = DW_BLOCK (&attr);
18504 }
18505 else if (attr_form_is_section_offset (&attr))
18506 {
18507 dwarf2_complex_location_expr_complaint ();
18508 }
18509 else
18510 {
18511 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18512 "partial symbol information");
18513 }
18514 break;
18515 case DW_AT_external:
18516 is_external = DW_UNSND (&attr);
18517 break;
18518 case DW_AT_declaration:
18519 is_declaration = DW_UNSND (&attr);
18520 break;
18521 case DW_AT_type:
18522 has_type = 1;
18523 break;
18524 case DW_AT_abstract_origin:
18525 case DW_AT_specification:
18526 case DW_AT_extension:
18527 has_specification = 1;
18528 spec_offset = dwarf2_get_ref_die_offset (&attr);
18529 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18530 || cu->per_cu->is_dwz);
18531 break;
18532 case DW_AT_sibling:
18533 /* Ignore absolute siblings, they might point outside of
18534 the current compile unit. */
18535 if (attr.form == DW_FORM_ref_addr)
18536 complaint (&symfile_complaints,
18537 _("ignoring absolute DW_AT_sibling"));
18538 else
18539 {
18540 const gdb_byte *buffer = reader->buffer;
18541 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18542 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18543
18544 if (sibling_ptr < info_ptr)
18545 complaint (&symfile_complaints,
18546 _("DW_AT_sibling points backwards"));
18547 else if (sibling_ptr > reader->buffer_end)
18548 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18549 else
18550 sibling = sibling_ptr;
18551 }
18552 break;
18553 case DW_AT_byte_size:
18554 has_byte_size = 1;
18555 break;
18556 case DW_AT_const_value:
18557 has_const_value = 1;
18558 break;
18559 case DW_AT_calling_convention:
18560 /* DWARF doesn't provide a way to identify a program's source-level
18561 entry point. DW_AT_calling_convention attributes are only meant
18562 to describe functions' calling conventions.
18563
18564 However, because it's a necessary piece of information in
18565 Fortran, and before DWARF 4 DW_CC_program was the only
18566 piece of debugging information whose definition refers to
18567 a 'main program' at all, several compilers marked Fortran
18568 main programs with DW_CC_program --- even when those
18569 functions use the standard calling conventions.
18570
18571 Although DWARF now specifies a way to provide this
18572 information, we support this practice for backward
18573 compatibility. */
18574 if (DW_UNSND (&attr) == DW_CC_program
18575 && cu->language == language_fortran)
18576 main_subprogram = 1;
18577 break;
18578 case DW_AT_inline:
18579 if (DW_UNSND (&attr) == DW_INL_inlined
18580 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18581 may_be_inlined = 1;
18582 break;
18583
18584 case DW_AT_import:
18585 if (tag == DW_TAG_imported_unit)
18586 {
18587 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18588 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18589 || cu->per_cu->is_dwz);
18590 }
18591 break;
18592
18593 case DW_AT_main_subprogram:
18594 main_subprogram = DW_UNSND (&attr);
18595 break;
18596
18597 default:
18598 break;
18599 }
18600 }
18601
18602 if (high_pc_relative)
18603 highpc += lowpc;
18604
18605 if (has_low_pc_attr && has_high_pc_attr)
18606 {
18607 /* When using the GNU linker, .gnu.linkonce. sections are used to
18608 eliminate duplicate copies of functions and vtables and such.
18609 The linker will arbitrarily choose one and discard the others.
18610 The AT_*_pc values for such functions refer to local labels in
18611 these sections. If the section from that file was discarded, the
18612 labels are not in the output, so the relocs get a value of 0.
18613 If this is a discarded function, mark the pc bounds as invalid,
18614 so that GDB will ignore it. */
18615 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18616 {
18617 struct objfile *objfile = dwarf2_per_objfile->objfile;
18618 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18619
18620 complaint (&symfile_complaints,
18621 _("DW_AT_low_pc %s is zero "
18622 "for DIE at %s [in module %s]"),
18623 paddress (gdbarch, lowpc),
18624 sect_offset_str (sect_off),
18625 objfile_name (objfile));
18626 }
18627 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18628 else if (lowpc >= highpc)
18629 {
18630 struct objfile *objfile = dwarf2_per_objfile->objfile;
18631 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18632
18633 complaint (&symfile_complaints,
18634 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18635 "for DIE at %s [in module %s]"),
18636 paddress (gdbarch, lowpc),
18637 paddress (gdbarch, highpc),
18638 sect_offset_str (sect_off),
18639 objfile_name (objfile));
18640 }
18641 else
18642 has_pc_info = 1;
18643 }
18644
18645 return info_ptr;
18646 }
18647
18648 /* Find a cached partial DIE at OFFSET in CU. */
18649
18650 struct partial_die_info *
18651 dwarf2_cu::find_partial_die (sect_offset sect_off)
18652 {
18653 struct partial_die_info *lookup_die = NULL;
18654 struct partial_die_info part_die (sect_off);
18655
18656 lookup_die = ((struct partial_die_info *)
18657 htab_find_with_hash (partial_dies, &part_die,
18658 to_underlying (sect_off)));
18659
18660 return lookup_die;
18661 }
18662
18663 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18664 except in the case of .debug_types DIEs which do not reference
18665 outside their CU (they do however referencing other types via
18666 DW_FORM_ref_sig8). */
18667
18668 static struct partial_die_info *
18669 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18670 {
18671 struct dwarf2_per_objfile *dwarf2_per_objfile
18672 = cu->per_cu->dwarf2_per_objfile;
18673 struct objfile *objfile = dwarf2_per_objfile->objfile;
18674 struct dwarf2_per_cu_data *per_cu = NULL;
18675 struct partial_die_info *pd = NULL;
18676
18677 if (offset_in_dwz == cu->per_cu->is_dwz
18678 && offset_in_cu_p (&cu->header, sect_off))
18679 {
18680 pd = cu->find_partial_die (sect_off);
18681 if (pd != NULL)
18682 return pd;
18683 /* We missed recording what we needed.
18684 Load all dies and try again. */
18685 per_cu = cu->per_cu;
18686 }
18687 else
18688 {
18689 /* TUs don't reference other CUs/TUs (except via type signatures). */
18690 if (cu->per_cu->is_debug_types)
18691 {
18692 error (_("Dwarf Error: Type Unit at offset %s contains"
18693 " external reference to offset %s [in module %s].\n"),
18694 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18695 bfd_get_filename (objfile->obfd));
18696 }
18697 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18698 dwarf2_per_objfile);
18699
18700 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18701 load_partial_comp_unit (per_cu);
18702
18703 per_cu->cu->last_used = 0;
18704 pd = per_cu->cu->find_partial_die (sect_off);
18705 }
18706
18707 /* If we didn't find it, and not all dies have been loaded,
18708 load them all and try again. */
18709
18710 if (pd == NULL && per_cu->load_all_dies == 0)
18711 {
18712 per_cu->load_all_dies = 1;
18713
18714 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18715 THIS_CU->cu may already be in use. So we can't just free it and
18716 replace its DIEs with the ones we read in. Instead, we leave those
18717 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18718 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18719 set. */
18720 load_partial_comp_unit (per_cu);
18721
18722 pd = per_cu->cu->find_partial_die (sect_off);
18723 }
18724
18725 if (pd == NULL)
18726 internal_error (__FILE__, __LINE__,
18727 _("could not find partial DIE %s "
18728 "in cache [from module %s]\n"),
18729 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18730 return pd;
18731 }
18732
18733 /* See if we can figure out if the class lives in a namespace. We do
18734 this by looking for a member function; its demangled name will
18735 contain namespace info, if there is any. */
18736
18737 static void
18738 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18739 struct dwarf2_cu *cu)
18740 {
18741 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18742 what template types look like, because the demangler
18743 frequently doesn't give the same name as the debug info. We
18744 could fix this by only using the demangled name to get the
18745 prefix (but see comment in read_structure_type). */
18746
18747 struct partial_die_info *real_pdi;
18748 struct partial_die_info *child_pdi;
18749
18750 /* If this DIE (this DIE's specification, if any) has a parent, then
18751 we should not do this. We'll prepend the parent's fully qualified
18752 name when we create the partial symbol. */
18753
18754 real_pdi = struct_pdi;
18755 while (real_pdi->has_specification)
18756 real_pdi = find_partial_die (real_pdi->spec_offset,
18757 real_pdi->spec_is_dwz, cu);
18758
18759 if (real_pdi->die_parent != NULL)
18760 return;
18761
18762 for (child_pdi = struct_pdi->die_child;
18763 child_pdi != NULL;
18764 child_pdi = child_pdi->die_sibling)
18765 {
18766 if (child_pdi->tag == DW_TAG_subprogram
18767 && child_pdi->linkage_name != NULL)
18768 {
18769 char *actual_class_name
18770 = language_class_name_from_physname (cu->language_defn,
18771 child_pdi->linkage_name);
18772 if (actual_class_name != NULL)
18773 {
18774 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18775 struct_pdi->name
18776 = ((const char *)
18777 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18778 actual_class_name,
18779 strlen (actual_class_name)));
18780 xfree (actual_class_name);
18781 }
18782 break;
18783 }
18784 }
18785 }
18786
18787 void
18788 partial_die_info::fixup (struct dwarf2_cu *cu)
18789 {
18790 /* Once we've fixed up a die, there's no point in doing so again.
18791 This also avoids a memory leak if we were to call
18792 guess_partial_die_structure_name multiple times. */
18793 if (fixup_called)
18794 return;
18795
18796 /* If we found a reference attribute and the DIE has no name, try
18797 to find a name in the referred to DIE. */
18798
18799 if (name == NULL && has_specification)
18800 {
18801 struct partial_die_info *spec_die;
18802
18803 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
18804
18805 spec_die->fixup (cu);
18806
18807 if (spec_die->name)
18808 {
18809 name = spec_die->name;
18810
18811 /* Copy DW_AT_external attribute if it is set. */
18812 if (spec_die->is_external)
18813 is_external = spec_die->is_external;
18814 }
18815 }
18816
18817 /* Set default names for some unnamed DIEs. */
18818
18819 if (name == NULL && tag == DW_TAG_namespace)
18820 name = CP_ANONYMOUS_NAMESPACE_STR;
18821
18822 /* If there is no parent die to provide a namespace, and there are
18823 children, see if we can determine the namespace from their linkage
18824 name. */
18825 if (cu->language == language_cplus
18826 && !VEC_empty (dwarf2_section_info_def,
18827 cu->per_cu->dwarf2_per_objfile->types)
18828 && die_parent == NULL
18829 && has_children
18830 && (tag == DW_TAG_class_type
18831 || tag == DW_TAG_structure_type
18832 || tag == DW_TAG_union_type))
18833 guess_partial_die_structure_name (this, cu);
18834
18835 /* GCC might emit a nameless struct or union that has a linkage
18836 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18837 if (name == NULL
18838 && (tag == DW_TAG_class_type
18839 || tag == DW_TAG_interface_type
18840 || tag == DW_TAG_structure_type
18841 || tag == DW_TAG_union_type)
18842 && linkage_name != NULL)
18843 {
18844 char *demangled;
18845
18846 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
18847 if (demangled)
18848 {
18849 const char *base;
18850
18851 /* Strip any leading namespaces/classes, keep only the base name.
18852 DW_AT_name for named DIEs does not contain the prefixes. */
18853 base = strrchr (demangled, ':');
18854 if (base && base > demangled && base[-1] == ':')
18855 base++;
18856 else
18857 base = demangled;
18858
18859 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18860 name
18861 = ((const char *)
18862 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18863 base, strlen (base)));
18864 xfree (demangled);
18865 }
18866 }
18867
18868 fixup_called = 1;
18869 }
18870
18871 /* Read an attribute value described by an attribute form. */
18872
18873 static const gdb_byte *
18874 read_attribute_value (const struct die_reader_specs *reader,
18875 struct attribute *attr, unsigned form,
18876 LONGEST implicit_const, const gdb_byte *info_ptr)
18877 {
18878 struct dwarf2_cu *cu = reader->cu;
18879 struct dwarf2_per_objfile *dwarf2_per_objfile
18880 = cu->per_cu->dwarf2_per_objfile;
18881 struct objfile *objfile = dwarf2_per_objfile->objfile;
18882 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18883 bfd *abfd = reader->abfd;
18884 struct comp_unit_head *cu_header = &cu->header;
18885 unsigned int bytes_read;
18886 struct dwarf_block *blk;
18887
18888 attr->form = (enum dwarf_form) form;
18889 switch (form)
18890 {
18891 case DW_FORM_ref_addr:
18892 if (cu->header.version == 2)
18893 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18894 else
18895 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18896 &cu->header, &bytes_read);
18897 info_ptr += bytes_read;
18898 break;
18899 case DW_FORM_GNU_ref_alt:
18900 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18901 info_ptr += bytes_read;
18902 break;
18903 case DW_FORM_addr:
18904 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18905 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18906 info_ptr += bytes_read;
18907 break;
18908 case DW_FORM_block2:
18909 blk = dwarf_alloc_block (cu);
18910 blk->size = read_2_bytes (abfd, info_ptr);
18911 info_ptr += 2;
18912 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18913 info_ptr += blk->size;
18914 DW_BLOCK (attr) = blk;
18915 break;
18916 case DW_FORM_block4:
18917 blk = dwarf_alloc_block (cu);
18918 blk->size = read_4_bytes (abfd, info_ptr);
18919 info_ptr += 4;
18920 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18921 info_ptr += blk->size;
18922 DW_BLOCK (attr) = blk;
18923 break;
18924 case DW_FORM_data2:
18925 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18926 info_ptr += 2;
18927 break;
18928 case DW_FORM_data4:
18929 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18930 info_ptr += 4;
18931 break;
18932 case DW_FORM_data8:
18933 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18934 info_ptr += 8;
18935 break;
18936 case DW_FORM_data16:
18937 blk = dwarf_alloc_block (cu);
18938 blk->size = 16;
18939 blk->data = read_n_bytes (abfd, info_ptr, 16);
18940 info_ptr += 16;
18941 DW_BLOCK (attr) = blk;
18942 break;
18943 case DW_FORM_sec_offset:
18944 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18945 info_ptr += bytes_read;
18946 break;
18947 case DW_FORM_string:
18948 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18949 DW_STRING_IS_CANONICAL (attr) = 0;
18950 info_ptr += bytes_read;
18951 break;
18952 case DW_FORM_strp:
18953 if (!cu->per_cu->is_dwz)
18954 {
18955 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18956 abfd, info_ptr, cu_header,
18957 &bytes_read);
18958 DW_STRING_IS_CANONICAL (attr) = 0;
18959 info_ptr += bytes_read;
18960 break;
18961 }
18962 /* FALLTHROUGH */
18963 case DW_FORM_line_strp:
18964 if (!cu->per_cu->is_dwz)
18965 {
18966 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18967 abfd, info_ptr,
18968 cu_header, &bytes_read);
18969 DW_STRING_IS_CANONICAL (attr) = 0;
18970 info_ptr += bytes_read;
18971 break;
18972 }
18973 /* FALLTHROUGH */
18974 case DW_FORM_GNU_strp_alt:
18975 {
18976 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18977 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18978 &bytes_read);
18979
18980 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18981 dwz, str_offset);
18982 DW_STRING_IS_CANONICAL (attr) = 0;
18983 info_ptr += bytes_read;
18984 }
18985 break;
18986 case DW_FORM_exprloc:
18987 case DW_FORM_block:
18988 blk = dwarf_alloc_block (cu);
18989 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18990 info_ptr += bytes_read;
18991 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18992 info_ptr += blk->size;
18993 DW_BLOCK (attr) = blk;
18994 break;
18995 case DW_FORM_block1:
18996 blk = dwarf_alloc_block (cu);
18997 blk->size = read_1_byte (abfd, info_ptr);
18998 info_ptr += 1;
18999 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19000 info_ptr += blk->size;
19001 DW_BLOCK (attr) = blk;
19002 break;
19003 case DW_FORM_data1:
19004 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19005 info_ptr += 1;
19006 break;
19007 case DW_FORM_flag:
19008 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19009 info_ptr += 1;
19010 break;
19011 case DW_FORM_flag_present:
19012 DW_UNSND (attr) = 1;
19013 break;
19014 case DW_FORM_sdata:
19015 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19016 info_ptr += bytes_read;
19017 break;
19018 case DW_FORM_udata:
19019 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19020 info_ptr += bytes_read;
19021 break;
19022 case DW_FORM_ref1:
19023 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19024 + read_1_byte (abfd, info_ptr));
19025 info_ptr += 1;
19026 break;
19027 case DW_FORM_ref2:
19028 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19029 + read_2_bytes (abfd, info_ptr));
19030 info_ptr += 2;
19031 break;
19032 case DW_FORM_ref4:
19033 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19034 + read_4_bytes (abfd, info_ptr));
19035 info_ptr += 4;
19036 break;
19037 case DW_FORM_ref8:
19038 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19039 + read_8_bytes (abfd, info_ptr));
19040 info_ptr += 8;
19041 break;
19042 case DW_FORM_ref_sig8:
19043 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19044 info_ptr += 8;
19045 break;
19046 case DW_FORM_ref_udata:
19047 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19048 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19049 info_ptr += bytes_read;
19050 break;
19051 case DW_FORM_indirect:
19052 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19053 info_ptr += bytes_read;
19054 if (form == DW_FORM_implicit_const)
19055 {
19056 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19057 info_ptr += bytes_read;
19058 }
19059 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19060 info_ptr);
19061 break;
19062 case DW_FORM_implicit_const:
19063 DW_SND (attr) = implicit_const;
19064 break;
19065 case DW_FORM_GNU_addr_index:
19066 if (reader->dwo_file == NULL)
19067 {
19068 /* For now flag a hard error.
19069 Later we can turn this into a complaint. */
19070 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19071 dwarf_form_name (form),
19072 bfd_get_filename (abfd));
19073 }
19074 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19075 info_ptr += bytes_read;
19076 break;
19077 case DW_FORM_GNU_str_index:
19078 if (reader->dwo_file == NULL)
19079 {
19080 /* For now flag a hard error.
19081 Later we can turn this into a complaint if warranted. */
19082 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19083 dwarf_form_name (form),
19084 bfd_get_filename (abfd));
19085 }
19086 {
19087 ULONGEST str_index =
19088 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19089
19090 DW_STRING (attr) = read_str_index (reader, str_index);
19091 DW_STRING_IS_CANONICAL (attr) = 0;
19092 info_ptr += bytes_read;
19093 }
19094 break;
19095 default:
19096 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19097 dwarf_form_name (form),
19098 bfd_get_filename (abfd));
19099 }
19100
19101 /* Super hack. */
19102 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19103 attr->form = DW_FORM_GNU_ref_alt;
19104
19105 /* We have seen instances where the compiler tried to emit a byte
19106 size attribute of -1 which ended up being encoded as an unsigned
19107 0xffffffff. Although 0xffffffff is technically a valid size value,
19108 an object of this size seems pretty unlikely so we can relatively
19109 safely treat these cases as if the size attribute was invalid and
19110 treat them as zero by default. */
19111 if (attr->name == DW_AT_byte_size
19112 && form == DW_FORM_data4
19113 && DW_UNSND (attr) >= 0xffffffff)
19114 {
19115 complaint
19116 (&symfile_complaints,
19117 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19118 hex_string (DW_UNSND (attr)));
19119 DW_UNSND (attr) = 0;
19120 }
19121
19122 return info_ptr;
19123 }
19124
19125 /* Read an attribute described by an abbreviated attribute. */
19126
19127 static const gdb_byte *
19128 read_attribute (const struct die_reader_specs *reader,
19129 struct attribute *attr, struct attr_abbrev *abbrev,
19130 const gdb_byte *info_ptr)
19131 {
19132 attr->name = abbrev->name;
19133 return read_attribute_value (reader, attr, abbrev->form,
19134 abbrev->implicit_const, info_ptr);
19135 }
19136
19137 /* Read dwarf information from a buffer. */
19138
19139 static unsigned int
19140 read_1_byte (bfd *abfd, const gdb_byte *buf)
19141 {
19142 return bfd_get_8 (abfd, buf);
19143 }
19144
19145 static int
19146 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19147 {
19148 return bfd_get_signed_8 (abfd, buf);
19149 }
19150
19151 static unsigned int
19152 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19153 {
19154 return bfd_get_16 (abfd, buf);
19155 }
19156
19157 static int
19158 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19159 {
19160 return bfd_get_signed_16 (abfd, buf);
19161 }
19162
19163 static unsigned int
19164 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19165 {
19166 return bfd_get_32 (abfd, buf);
19167 }
19168
19169 static int
19170 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19171 {
19172 return bfd_get_signed_32 (abfd, buf);
19173 }
19174
19175 static ULONGEST
19176 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19177 {
19178 return bfd_get_64 (abfd, buf);
19179 }
19180
19181 static CORE_ADDR
19182 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19183 unsigned int *bytes_read)
19184 {
19185 struct comp_unit_head *cu_header = &cu->header;
19186 CORE_ADDR retval = 0;
19187
19188 if (cu_header->signed_addr_p)
19189 {
19190 switch (cu_header->addr_size)
19191 {
19192 case 2:
19193 retval = bfd_get_signed_16 (abfd, buf);
19194 break;
19195 case 4:
19196 retval = bfd_get_signed_32 (abfd, buf);
19197 break;
19198 case 8:
19199 retval = bfd_get_signed_64 (abfd, buf);
19200 break;
19201 default:
19202 internal_error (__FILE__, __LINE__,
19203 _("read_address: bad switch, signed [in module %s]"),
19204 bfd_get_filename (abfd));
19205 }
19206 }
19207 else
19208 {
19209 switch (cu_header->addr_size)
19210 {
19211 case 2:
19212 retval = bfd_get_16 (abfd, buf);
19213 break;
19214 case 4:
19215 retval = bfd_get_32 (abfd, buf);
19216 break;
19217 case 8:
19218 retval = bfd_get_64 (abfd, buf);
19219 break;
19220 default:
19221 internal_error (__FILE__, __LINE__,
19222 _("read_address: bad switch, "
19223 "unsigned [in module %s]"),
19224 bfd_get_filename (abfd));
19225 }
19226 }
19227
19228 *bytes_read = cu_header->addr_size;
19229 return retval;
19230 }
19231
19232 /* Read the initial length from a section. The (draft) DWARF 3
19233 specification allows the initial length to take up either 4 bytes
19234 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19235 bytes describe the length and all offsets will be 8 bytes in length
19236 instead of 4.
19237
19238 An older, non-standard 64-bit format is also handled by this
19239 function. The older format in question stores the initial length
19240 as an 8-byte quantity without an escape value. Lengths greater
19241 than 2^32 aren't very common which means that the initial 4 bytes
19242 is almost always zero. Since a length value of zero doesn't make
19243 sense for the 32-bit format, this initial zero can be considered to
19244 be an escape value which indicates the presence of the older 64-bit
19245 format. As written, the code can't detect (old format) lengths
19246 greater than 4GB. If it becomes necessary to handle lengths
19247 somewhat larger than 4GB, we could allow other small values (such
19248 as the non-sensical values of 1, 2, and 3) to also be used as
19249 escape values indicating the presence of the old format.
19250
19251 The value returned via bytes_read should be used to increment the
19252 relevant pointer after calling read_initial_length().
19253
19254 [ Note: read_initial_length() and read_offset() are based on the
19255 document entitled "DWARF Debugging Information Format", revision
19256 3, draft 8, dated November 19, 2001. This document was obtained
19257 from:
19258
19259 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19260
19261 This document is only a draft and is subject to change. (So beware.)
19262
19263 Details regarding the older, non-standard 64-bit format were
19264 determined empirically by examining 64-bit ELF files produced by
19265 the SGI toolchain on an IRIX 6.5 machine.
19266
19267 - Kevin, July 16, 2002
19268 ] */
19269
19270 static LONGEST
19271 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19272 {
19273 LONGEST length = bfd_get_32 (abfd, buf);
19274
19275 if (length == 0xffffffff)
19276 {
19277 length = bfd_get_64 (abfd, buf + 4);
19278 *bytes_read = 12;
19279 }
19280 else if (length == 0)
19281 {
19282 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19283 length = bfd_get_64 (abfd, buf);
19284 *bytes_read = 8;
19285 }
19286 else
19287 {
19288 *bytes_read = 4;
19289 }
19290
19291 return length;
19292 }
19293
19294 /* Cover function for read_initial_length.
19295 Returns the length of the object at BUF, and stores the size of the
19296 initial length in *BYTES_READ and stores the size that offsets will be in
19297 *OFFSET_SIZE.
19298 If the initial length size is not equivalent to that specified in
19299 CU_HEADER then issue a complaint.
19300 This is useful when reading non-comp-unit headers. */
19301
19302 static LONGEST
19303 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19304 const struct comp_unit_head *cu_header,
19305 unsigned int *bytes_read,
19306 unsigned int *offset_size)
19307 {
19308 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19309
19310 gdb_assert (cu_header->initial_length_size == 4
19311 || cu_header->initial_length_size == 8
19312 || cu_header->initial_length_size == 12);
19313
19314 if (cu_header->initial_length_size != *bytes_read)
19315 complaint (&symfile_complaints,
19316 _("intermixed 32-bit and 64-bit DWARF sections"));
19317
19318 *offset_size = (*bytes_read == 4) ? 4 : 8;
19319 return length;
19320 }
19321
19322 /* Read an offset from the data stream. The size of the offset is
19323 given by cu_header->offset_size. */
19324
19325 static LONGEST
19326 read_offset (bfd *abfd, const gdb_byte *buf,
19327 const struct comp_unit_head *cu_header,
19328 unsigned int *bytes_read)
19329 {
19330 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19331
19332 *bytes_read = cu_header->offset_size;
19333 return offset;
19334 }
19335
19336 /* Read an offset from the data stream. */
19337
19338 static LONGEST
19339 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19340 {
19341 LONGEST retval = 0;
19342
19343 switch (offset_size)
19344 {
19345 case 4:
19346 retval = bfd_get_32 (abfd, buf);
19347 break;
19348 case 8:
19349 retval = bfd_get_64 (abfd, buf);
19350 break;
19351 default:
19352 internal_error (__FILE__, __LINE__,
19353 _("read_offset_1: bad switch [in module %s]"),
19354 bfd_get_filename (abfd));
19355 }
19356
19357 return retval;
19358 }
19359
19360 static const gdb_byte *
19361 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19362 {
19363 /* If the size of a host char is 8 bits, we can return a pointer
19364 to the buffer, otherwise we have to copy the data to a buffer
19365 allocated on the temporary obstack. */
19366 gdb_assert (HOST_CHAR_BIT == 8);
19367 return buf;
19368 }
19369
19370 static const char *
19371 read_direct_string (bfd *abfd, const gdb_byte *buf,
19372 unsigned int *bytes_read_ptr)
19373 {
19374 /* If the size of a host char is 8 bits, we can return a pointer
19375 to the string, otherwise we have to copy the string to a buffer
19376 allocated on the temporary obstack. */
19377 gdb_assert (HOST_CHAR_BIT == 8);
19378 if (*buf == '\0')
19379 {
19380 *bytes_read_ptr = 1;
19381 return NULL;
19382 }
19383 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19384 return (const char *) buf;
19385 }
19386
19387 /* Return pointer to string at section SECT offset STR_OFFSET with error
19388 reporting strings FORM_NAME and SECT_NAME. */
19389
19390 static const char *
19391 read_indirect_string_at_offset_from (struct objfile *objfile,
19392 bfd *abfd, LONGEST str_offset,
19393 struct dwarf2_section_info *sect,
19394 const char *form_name,
19395 const char *sect_name)
19396 {
19397 dwarf2_read_section (objfile, sect);
19398 if (sect->buffer == NULL)
19399 error (_("%s used without %s section [in module %s]"),
19400 form_name, sect_name, bfd_get_filename (abfd));
19401 if (str_offset >= sect->size)
19402 error (_("%s pointing outside of %s section [in module %s]"),
19403 form_name, sect_name, bfd_get_filename (abfd));
19404 gdb_assert (HOST_CHAR_BIT == 8);
19405 if (sect->buffer[str_offset] == '\0')
19406 return NULL;
19407 return (const char *) (sect->buffer + str_offset);
19408 }
19409
19410 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19411
19412 static const char *
19413 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19414 bfd *abfd, LONGEST str_offset)
19415 {
19416 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19417 abfd, str_offset,
19418 &dwarf2_per_objfile->str,
19419 "DW_FORM_strp", ".debug_str");
19420 }
19421
19422 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19423
19424 static const char *
19425 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19426 bfd *abfd, LONGEST str_offset)
19427 {
19428 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19429 abfd, str_offset,
19430 &dwarf2_per_objfile->line_str,
19431 "DW_FORM_line_strp",
19432 ".debug_line_str");
19433 }
19434
19435 /* Read a string at offset STR_OFFSET in the .debug_str section from
19436 the .dwz file DWZ. Throw an error if the offset is too large. If
19437 the string consists of a single NUL byte, return NULL; otherwise
19438 return a pointer to the string. */
19439
19440 static const char *
19441 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19442 LONGEST str_offset)
19443 {
19444 dwarf2_read_section (objfile, &dwz->str);
19445
19446 if (dwz->str.buffer == NULL)
19447 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19448 "section [in module %s]"),
19449 bfd_get_filename (dwz->dwz_bfd));
19450 if (str_offset >= dwz->str.size)
19451 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19452 ".debug_str section [in module %s]"),
19453 bfd_get_filename (dwz->dwz_bfd));
19454 gdb_assert (HOST_CHAR_BIT == 8);
19455 if (dwz->str.buffer[str_offset] == '\0')
19456 return NULL;
19457 return (const char *) (dwz->str.buffer + str_offset);
19458 }
19459
19460 /* Return pointer to string at .debug_str offset as read from BUF.
19461 BUF is assumed to be in a compilation unit described by CU_HEADER.
19462 Return *BYTES_READ_PTR count of bytes read from BUF. */
19463
19464 static const char *
19465 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19466 const gdb_byte *buf,
19467 const struct comp_unit_head *cu_header,
19468 unsigned int *bytes_read_ptr)
19469 {
19470 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19471
19472 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19473 }
19474
19475 /* Return pointer to string at .debug_line_str offset as read from BUF.
19476 BUF is assumed to be in a compilation unit described by CU_HEADER.
19477 Return *BYTES_READ_PTR count of bytes read from BUF. */
19478
19479 static const char *
19480 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19481 bfd *abfd, const gdb_byte *buf,
19482 const struct comp_unit_head *cu_header,
19483 unsigned int *bytes_read_ptr)
19484 {
19485 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19486
19487 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19488 str_offset);
19489 }
19490
19491 ULONGEST
19492 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19493 unsigned int *bytes_read_ptr)
19494 {
19495 ULONGEST result;
19496 unsigned int num_read;
19497 int shift;
19498 unsigned char byte;
19499
19500 result = 0;
19501 shift = 0;
19502 num_read = 0;
19503 while (1)
19504 {
19505 byte = bfd_get_8 (abfd, buf);
19506 buf++;
19507 num_read++;
19508 result |= ((ULONGEST) (byte & 127) << shift);
19509 if ((byte & 128) == 0)
19510 {
19511 break;
19512 }
19513 shift += 7;
19514 }
19515 *bytes_read_ptr = num_read;
19516 return result;
19517 }
19518
19519 static LONGEST
19520 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19521 unsigned int *bytes_read_ptr)
19522 {
19523 LONGEST result;
19524 int shift, num_read;
19525 unsigned char byte;
19526
19527 result = 0;
19528 shift = 0;
19529 num_read = 0;
19530 while (1)
19531 {
19532 byte = bfd_get_8 (abfd, buf);
19533 buf++;
19534 num_read++;
19535 result |= ((LONGEST) (byte & 127) << shift);
19536 shift += 7;
19537 if ((byte & 128) == 0)
19538 {
19539 break;
19540 }
19541 }
19542 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19543 result |= -(((LONGEST) 1) << shift);
19544 *bytes_read_ptr = num_read;
19545 return result;
19546 }
19547
19548 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19549 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19550 ADDR_SIZE is the size of addresses from the CU header. */
19551
19552 static CORE_ADDR
19553 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19554 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19555 {
19556 struct objfile *objfile = dwarf2_per_objfile->objfile;
19557 bfd *abfd = objfile->obfd;
19558 const gdb_byte *info_ptr;
19559
19560 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19561 if (dwarf2_per_objfile->addr.buffer == NULL)
19562 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19563 objfile_name (objfile));
19564 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19565 error (_("DW_FORM_addr_index pointing outside of "
19566 ".debug_addr section [in module %s]"),
19567 objfile_name (objfile));
19568 info_ptr = (dwarf2_per_objfile->addr.buffer
19569 + addr_base + addr_index * addr_size);
19570 if (addr_size == 4)
19571 return bfd_get_32 (abfd, info_ptr);
19572 else
19573 return bfd_get_64 (abfd, info_ptr);
19574 }
19575
19576 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19577
19578 static CORE_ADDR
19579 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19580 {
19581 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19582 cu->addr_base, cu->header.addr_size);
19583 }
19584
19585 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19586
19587 static CORE_ADDR
19588 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19589 unsigned int *bytes_read)
19590 {
19591 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19592 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19593
19594 return read_addr_index (cu, addr_index);
19595 }
19596
19597 /* Data structure to pass results from dwarf2_read_addr_index_reader
19598 back to dwarf2_read_addr_index. */
19599
19600 struct dwarf2_read_addr_index_data
19601 {
19602 ULONGEST addr_base;
19603 int addr_size;
19604 };
19605
19606 /* die_reader_func for dwarf2_read_addr_index. */
19607
19608 static void
19609 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19610 const gdb_byte *info_ptr,
19611 struct die_info *comp_unit_die,
19612 int has_children,
19613 void *data)
19614 {
19615 struct dwarf2_cu *cu = reader->cu;
19616 struct dwarf2_read_addr_index_data *aidata =
19617 (struct dwarf2_read_addr_index_data *) data;
19618
19619 aidata->addr_base = cu->addr_base;
19620 aidata->addr_size = cu->header.addr_size;
19621 }
19622
19623 /* Given an index in .debug_addr, fetch the value.
19624 NOTE: This can be called during dwarf expression evaluation,
19625 long after the debug information has been read, and thus per_cu->cu
19626 may no longer exist. */
19627
19628 CORE_ADDR
19629 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19630 unsigned int addr_index)
19631 {
19632 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19633 struct dwarf2_cu *cu = per_cu->cu;
19634 ULONGEST addr_base;
19635 int addr_size;
19636
19637 /* We need addr_base and addr_size.
19638 If we don't have PER_CU->cu, we have to get it.
19639 Nasty, but the alternative is storing the needed info in PER_CU,
19640 which at this point doesn't seem justified: it's not clear how frequently
19641 it would get used and it would increase the size of every PER_CU.
19642 Entry points like dwarf2_per_cu_addr_size do a similar thing
19643 so we're not in uncharted territory here.
19644 Alas we need to be a bit more complicated as addr_base is contained
19645 in the DIE.
19646
19647 We don't need to read the entire CU(/TU).
19648 We just need the header and top level die.
19649
19650 IWBN to use the aging mechanism to let us lazily later discard the CU.
19651 For now we skip this optimization. */
19652
19653 if (cu != NULL)
19654 {
19655 addr_base = cu->addr_base;
19656 addr_size = cu->header.addr_size;
19657 }
19658 else
19659 {
19660 struct dwarf2_read_addr_index_data aidata;
19661
19662 /* Note: We can't use init_cutu_and_read_dies_simple here,
19663 we need addr_base. */
19664 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19665 dwarf2_read_addr_index_reader, &aidata);
19666 addr_base = aidata.addr_base;
19667 addr_size = aidata.addr_size;
19668 }
19669
19670 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19671 addr_size);
19672 }
19673
19674 /* Given a DW_FORM_GNU_str_index, fetch the string.
19675 This is only used by the Fission support. */
19676
19677 static const char *
19678 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19679 {
19680 struct dwarf2_cu *cu = reader->cu;
19681 struct dwarf2_per_objfile *dwarf2_per_objfile
19682 = cu->per_cu->dwarf2_per_objfile;
19683 struct objfile *objfile = dwarf2_per_objfile->objfile;
19684 const char *objf_name = objfile_name (objfile);
19685 bfd *abfd = objfile->obfd;
19686 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19687 struct dwarf2_section_info *str_offsets_section =
19688 &reader->dwo_file->sections.str_offsets;
19689 const gdb_byte *info_ptr;
19690 ULONGEST str_offset;
19691 static const char form_name[] = "DW_FORM_GNU_str_index";
19692
19693 dwarf2_read_section (objfile, str_section);
19694 dwarf2_read_section (objfile, str_offsets_section);
19695 if (str_section->buffer == NULL)
19696 error (_("%s used without .debug_str.dwo section"
19697 " in CU at offset %s [in module %s]"),
19698 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19699 if (str_offsets_section->buffer == NULL)
19700 error (_("%s used without .debug_str_offsets.dwo section"
19701 " in CU at offset %s [in module %s]"),
19702 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19703 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19704 error (_("%s pointing outside of .debug_str_offsets.dwo"
19705 " section in CU at offset %s [in module %s]"),
19706 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19707 info_ptr = (str_offsets_section->buffer
19708 + str_index * cu->header.offset_size);
19709 if (cu->header.offset_size == 4)
19710 str_offset = bfd_get_32 (abfd, info_ptr);
19711 else
19712 str_offset = bfd_get_64 (abfd, info_ptr);
19713 if (str_offset >= str_section->size)
19714 error (_("Offset from %s pointing outside of"
19715 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19716 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19717 return (const char *) (str_section->buffer + str_offset);
19718 }
19719
19720 /* Return the length of an LEB128 number in BUF. */
19721
19722 static int
19723 leb128_size (const gdb_byte *buf)
19724 {
19725 const gdb_byte *begin = buf;
19726 gdb_byte byte;
19727
19728 while (1)
19729 {
19730 byte = *buf++;
19731 if ((byte & 128) == 0)
19732 return buf - begin;
19733 }
19734 }
19735
19736 static void
19737 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19738 {
19739 switch (lang)
19740 {
19741 case DW_LANG_C89:
19742 case DW_LANG_C99:
19743 case DW_LANG_C11:
19744 case DW_LANG_C:
19745 case DW_LANG_UPC:
19746 cu->language = language_c;
19747 break;
19748 case DW_LANG_Java:
19749 case DW_LANG_C_plus_plus:
19750 case DW_LANG_C_plus_plus_11:
19751 case DW_LANG_C_plus_plus_14:
19752 cu->language = language_cplus;
19753 break;
19754 case DW_LANG_D:
19755 cu->language = language_d;
19756 break;
19757 case DW_LANG_Fortran77:
19758 case DW_LANG_Fortran90:
19759 case DW_LANG_Fortran95:
19760 case DW_LANG_Fortran03:
19761 case DW_LANG_Fortran08:
19762 cu->language = language_fortran;
19763 break;
19764 case DW_LANG_Go:
19765 cu->language = language_go;
19766 break;
19767 case DW_LANG_Mips_Assembler:
19768 cu->language = language_asm;
19769 break;
19770 case DW_LANG_Ada83:
19771 case DW_LANG_Ada95:
19772 cu->language = language_ada;
19773 break;
19774 case DW_LANG_Modula2:
19775 cu->language = language_m2;
19776 break;
19777 case DW_LANG_Pascal83:
19778 cu->language = language_pascal;
19779 break;
19780 case DW_LANG_ObjC:
19781 cu->language = language_objc;
19782 break;
19783 case DW_LANG_Rust:
19784 case DW_LANG_Rust_old:
19785 cu->language = language_rust;
19786 break;
19787 case DW_LANG_Cobol74:
19788 case DW_LANG_Cobol85:
19789 default:
19790 cu->language = language_minimal;
19791 break;
19792 }
19793 cu->language_defn = language_def (cu->language);
19794 }
19795
19796 /* Return the named attribute or NULL if not there. */
19797
19798 static struct attribute *
19799 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19800 {
19801 for (;;)
19802 {
19803 unsigned int i;
19804 struct attribute *spec = NULL;
19805
19806 for (i = 0; i < die->num_attrs; ++i)
19807 {
19808 if (die->attrs[i].name == name)
19809 return &die->attrs[i];
19810 if (die->attrs[i].name == DW_AT_specification
19811 || die->attrs[i].name == DW_AT_abstract_origin)
19812 spec = &die->attrs[i];
19813 }
19814
19815 if (!spec)
19816 break;
19817
19818 die = follow_die_ref (die, spec, &cu);
19819 }
19820
19821 return NULL;
19822 }
19823
19824 /* Return the named attribute or NULL if not there,
19825 but do not follow DW_AT_specification, etc.
19826 This is for use in contexts where we're reading .debug_types dies.
19827 Following DW_AT_specification, DW_AT_abstract_origin will take us
19828 back up the chain, and we want to go down. */
19829
19830 static struct attribute *
19831 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19832 {
19833 unsigned int i;
19834
19835 for (i = 0; i < die->num_attrs; ++i)
19836 if (die->attrs[i].name == name)
19837 return &die->attrs[i];
19838
19839 return NULL;
19840 }
19841
19842 /* Return the string associated with a string-typed attribute, or NULL if it
19843 is either not found or is of an incorrect type. */
19844
19845 static const char *
19846 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19847 {
19848 struct attribute *attr;
19849 const char *str = NULL;
19850
19851 attr = dwarf2_attr (die, name, cu);
19852
19853 if (attr != NULL)
19854 {
19855 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19856 || attr->form == DW_FORM_string
19857 || attr->form == DW_FORM_GNU_str_index
19858 || attr->form == DW_FORM_GNU_strp_alt)
19859 str = DW_STRING (attr);
19860 else
19861 complaint (&symfile_complaints,
19862 _("string type expected for attribute %s for "
19863 "DIE at %s in module %s"),
19864 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19865 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19866 }
19867
19868 return str;
19869 }
19870
19871 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19872 and holds a non-zero value. This function should only be used for
19873 DW_FORM_flag or DW_FORM_flag_present attributes. */
19874
19875 static int
19876 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19877 {
19878 struct attribute *attr = dwarf2_attr (die, name, cu);
19879
19880 return (attr && DW_UNSND (attr));
19881 }
19882
19883 static int
19884 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19885 {
19886 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19887 which value is non-zero. However, we have to be careful with
19888 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19889 (via dwarf2_flag_true_p) follows this attribute. So we may
19890 end up accidently finding a declaration attribute that belongs
19891 to a different DIE referenced by the specification attribute,
19892 even though the given DIE does not have a declaration attribute. */
19893 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19894 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19895 }
19896
19897 /* Return the die giving the specification for DIE, if there is
19898 one. *SPEC_CU is the CU containing DIE on input, and the CU
19899 containing the return value on output. If there is no
19900 specification, but there is an abstract origin, that is
19901 returned. */
19902
19903 static struct die_info *
19904 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19905 {
19906 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19907 *spec_cu);
19908
19909 if (spec_attr == NULL)
19910 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19911
19912 if (spec_attr == NULL)
19913 return NULL;
19914 else
19915 return follow_die_ref (die, spec_attr, spec_cu);
19916 }
19917
19918 /* Stub for free_line_header to match void * callback types. */
19919
19920 static void
19921 free_line_header_voidp (void *arg)
19922 {
19923 struct line_header *lh = (struct line_header *) arg;
19924
19925 delete lh;
19926 }
19927
19928 void
19929 line_header::add_include_dir (const char *include_dir)
19930 {
19931 if (dwarf_line_debug >= 2)
19932 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19933 include_dirs.size () + 1, include_dir);
19934
19935 include_dirs.push_back (include_dir);
19936 }
19937
19938 void
19939 line_header::add_file_name (const char *name,
19940 dir_index d_index,
19941 unsigned int mod_time,
19942 unsigned int length)
19943 {
19944 if (dwarf_line_debug >= 2)
19945 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19946 (unsigned) file_names.size () + 1, name);
19947
19948 file_names.emplace_back (name, d_index, mod_time, length);
19949 }
19950
19951 /* A convenience function to find the proper .debug_line section for a CU. */
19952
19953 static struct dwarf2_section_info *
19954 get_debug_line_section (struct dwarf2_cu *cu)
19955 {
19956 struct dwarf2_section_info *section;
19957 struct dwarf2_per_objfile *dwarf2_per_objfile
19958 = cu->per_cu->dwarf2_per_objfile;
19959
19960 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19961 DWO file. */
19962 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19963 section = &cu->dwo_unit->dwo_file->sections.line;
19964 else if (cu->per_cu->is_dwz)
19965 {
19966 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19967
19968 section = &dwz->line;
19969 }
19970 else
19971 section = &dwarf2_per_objfile->line;
19972
19973 return section;
19974 }
19975
19976 /* Read directory or file name entry format, starting with byte of
19977 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19978 entries count and the entries themselves in the described entry
19979 format. */
19980
19981 static void
19982 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19983 bfd *abfd, const gdb_byte **bufp,
19984 struct line_header *lh,
19985 const struct comp_unit_head *cu_header,
19986 void (*callback) (struct line_header *lh,
19987 const char *name,
19988 dir_index d_index,
19989 unsigned int mod_time,
19990 unsigned int length))
19991 {
19992 gdb_byte format_count, formati;
19993 ULONGEST data_count, datai;
19994 const gdb_byte *buf = *bufp;
19995 const gdb_byte *format_header_data;
19996 unsigned int bytes_read;
19997
19998 format_count = read_1_byte (abfd, buf);
19999 buf += 1;
20000 format_header_data = buf;
20001 for (formati = 0; formati < format_count; formati++)
20002 {
20003 read_unsigned_leb128 (abfd, buf, &bytes_read);
20004 buf += bytes_read;
20005 read_unsigned_leb128 (abfd, buf, &bytes_read);
20006 buf += bytes_read;
20007 }
20008
20009 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20010 buf += bytes_read;
20011 for (datai = 0; datai < data_count; datai++)
20012 {
20013 const gdb_byte *format = format_header_data;
20014 struct file_entry fe;
20015
20016 for (formati = 0; formati < format_count; formati++)
20017 {
20018 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20019 format += bytes_read;
20020
20021 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20022 format += bytes_read;
20023
20024 gdb::optional<const char *> string;
20025 gdb::optional<unsigned int> uint;
20026
20027 switch (form)
20028 {
20029 case DW_FORM_string:
20030 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20031 buf += bytes_read;
20032 break;
20033
20034 case DW_FORM_line_strp:
20035 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20036 abfd, buf,
20037 cu_header,
20038 &bytes_read));
20039 buf += bytes_read;
20040 break;
20041
20042 case DW_FORM_data1:
20043 uint.emplace (read_1_byte (abfd, buf));
20044 buf += 1;
20045 break;
20046
20047 case DW_FORM_data2:
20048 uint.emplace (read_2_bytes (abfd, buf));
20049 buf += 2;
20050 break;
20051
20052 case DW_FORM_data4:
20053 uint.emplace (read_4_bytes (abfd, buf));
20054 buf += 4;
20055 break;
20056
20057 case DW_FORM_data8:
20058 uint.emplace (read_8_bytes (abfd, buf));
20059 buf += 8;
20060 break;
20061
20062 case DW_FORM_udata:
20063 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20064 buf += bytes_read;
20065 break;
20066
20067 case DW_FORM_block:
20068 /* It is valid only for DW_LNCT_timestamp which is ignored by
20069 current GDB. */
20070 break;
20071 }
20072
20073 switch (content_type)
20074 {
20075 case DW_LNCT_path:
20076 if (string.has_value ())
20077 fe.name = *string;
20078 break;
20079 case DW_LNCT_directory_index:
20080 if (uint.has_value ())
20081 fe.d_index = (dir_index) *uint;
20082 break;
20083 case DW_LNCT_timestamp:
20084 if (uint.has_value ())
20085 fe.mod_time = *uint;
20086 break;
20087 case DW_LNCT_size:
20088 if (uint.has_value ())
20089 fe.length = *uint;
20090 break;
20091 case DW_LNCT_MD5:
20092 break;
20093 default:
20094 complaint (&symfile_complaints,
20095 _("Unknown format content type %s"),
20096 pulongest (content_type));
20097 }
20098 }
20099
20100 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20101 }
20102
20103 *bufp = buf;
20104 }
20105
20106 /* Read the statement program header starting at OFFSET in
20107 .debug_line, or .debug_line.dwo. Return a pointer
20108 to a struct line_header, allocated using xmalloc.
20109 Returns NULL if there is a problem reading the header, e.g., if it
20110 has a version we don't understand.
20111
20112 NOTE: the strings in the include directory and file name tables of
20113 the returned object point into the dwarf line section buffer,
20114 and must not be freed. */
20115
20116 static line_header_up
20117 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20118 {
20119 const gdb_byte *line_ptr;
20120 unsigned int bytes_read, offset_size;
20121 int i;
20122 const char *cur_dir, *cur_file;
20123 struct dwarf2_section_info *section;
20124 bfd *abfd;
20125 struct dwarf2_per_objfile *dwarf2_per_objfile
20126 = cu->per_cu->dwarf2_per_objfile;
20127
20128 section = get_debug_line_section (cu);
20129 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20130 if (section->buffer == NULL)
20131 {
20132 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20133 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20134 else
20135 complaint (&symfile_complaints, _("missing .debug_line section"));
20136 return 0;
20137 }
20138
20139 /* We can't do this until we know the section is non-empty.
20140 Only then do we know we have such a section. */
20141 abfd = get_section_bfd_owner (section);
20142
20143 /* Make sure that at least there's room for the total_length field.
20144 That could be 12 bytes long, but we're just going to fudge that. */
20145 if (to_underlying (sect_off) + 4 >= section->size)
20146 {
20147 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20148 return 0;
20149 }
20150
20151 line_header_up lh (new line_header ());
20152
20153 lh->sect_off = sect_off;
20154 lh->offset_in_dwz = cu->per_cu->is_dwz;
20155
20156 line_ptr = section->buffer + to_underlying (sect_off);
20157
20158 /* Read in the header. */
20159 lh->total_length =
20160 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20161 &bytes_read, &offset_size);
20162 line_ptr += bytes_read;
20163 if (line_ptr + lh->total_length > (section->buffer + section->size))
20164 {
20165 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20166 return 0;
20167 }
20168 lh->statement_program_end = line_ptr + lh->total_length;
20169 lh->version = read_2_bytes (abfd, line_ptr);
20170 line_ptr += 2;
20171 if (lh->version > 5)
20172 {
20173 /* This is a version we don't understand. The format could have
20174 changed in ways we don't handle properly so just punt. */
20175 complaint (&symfile_complaints,
20176 _("unsupported version in .debug_line section"));
20177 return NULL;
20178 }
20179 if (lh->version >= 5)
20180 {
20181 gdb_byte segment_selector_size;
20182
20183 /* Skip address size. */
20184 read_1_byte (abfd, line_ptr);
20185 line_ptr += 1;
20186
20187 segment_selector_size = read_1_byte (abfd, line_ptr);
20188 line_ptr += 1;
20189 if (segment_selector_size != 0)
20190 {
20191 complaint (&symfile_complaints,
20192 _("unsupported segment selector size %u "
20193 "in .debug_line section"),
20194 segment_selector_size);
20195 return NULL;
20196 }
20197 }
20198 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20199 line_ptr += offset_size;
20200 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20201 line_ptr += 1;
20202 if (lh->version >= 4)
20203 {
20204 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20205 line_ptr += 1;
20206 }
20207 else
20208 lh->maximum_ops_per_instruction = 1;
20209
20210 if (lh->maximum_ops_per_instruction == 0)
20211 {
20212 lh->maximum_ops_per_instruction = 1;
20213 complaint (&symfile_complaints,
20214 _("invalid maximum_ops_per_instruction "
20215 "in `.debug_line' section"));
20216 }
20217
20218 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20219 line_ptr += 1;
20220 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20221 line_ptr += 1;
20222 lh->line_range = read_1_byte (abfd, line_ptr);
20223 line_ptr += 1;
20224 lh->opcode_base = read_1_byte (abfd, line_ptr);
20225 line_ptr += 1;
20226 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20227
20228 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20229 for (i = 1; i < lh->opcode_base; ++i)
20230 {
20231 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20232 line_ptr += 1;
20233 }
20234
20235 if (lh->version >= 5)
20236 {
20237 /* Read directory table. */
20238 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20239 &cu->header,
20240 [] (struct line_header *lh, const char *name,
20241 dir_index d_index, unsigned int mod_time,
20242 unsigned int length)
20243 {
20244 lh->add_include_dir (name);
20245 });
20246
20247 /* Read file name table. */
20248 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20249 &cu->header,
20250 [] (struct line_header *lh, const char *name,
20251 dir_index d_index, unsigned int mod_time,
20252 unsigned int length)
20253 {
20254 lh->add_file_name (name, d_index, mod_time, length);
20255 });
20256 }
20257 else
20258 {
20259 /* Read directory table. */
20260 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20261 {
20262 line_ptr += bytes_read;
20263 lh->add_include_dir (cur_dir);
20264 }
20265 line_ptr += bytes_read;
20266
20267 /* Read file name table. */
20268 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20269 {
20270 unsigned int mod_time, length;
20271 dir_index d_index;
20272
20273 line_ptr += bytes_read;
20274 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20275 line_ptr += bytes_read;
20276 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20277 line_ptr += bytes_read;
20278 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20279 line_ptr += bytes_read;
20280
20281 lh->add_file_name (cur_file, d_index, mod_time, length);
20282 }
20283 line_ptr += bytes_read;
20284 }
20285 lh->statement_program_start = line_ptr;
20286
20287 if (line_ptr > (section->buffer + section->size))
20288 complaint (&symfile_complaints,
20289 _("line number info header doesn't "
20290 "fit in `.debug_line' section"));
20291
20292 return lh;
20293 }
20294
20295 /* Subroutine of dwarf_decode_lines to simplify it.
20296 Return the file name of the psymtab for included file FILE_INDEX
20297 in line header LH of PST.
20298 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20299 If space for the result is malloc'd, *NAME_HOLDER will be set.
20300 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20301
20302 static const char *
20303 psymtab_include_file_name (const struct line_header *lh, int file_index,
20304 const struct partial_symtab *pst,
20305 const char *comp_dir,
20306 gdb::unique_xmalloc_ptr<char> *name_holder)
20307 {
20308 const file_entry &fe = lh->file_names[file_index];
20309 const char *include_name = fe.name;
20310 const char *include_name_to_compare = include_name;
20311 const char *pst_filename;
20312 int file_is_pst;
20313
20314 const char *dir_name = fe.include_dir (lh);
20315
20316 gdb::unique_xmalloc_ptr<char> hold_compare;
20317 if (!IS_ABSOLUTE_PATH (include_name)
20318 && (dir_name != NULL || comp_dir != NULL))
20319 {
20320 /* Avoid creating a duplicate psymtab for PST.
20321 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20322 Before we do the comparison, however, we need to account
20323 for DIR_NAME and COMP_DIR.
20324 First prepend dir_name (if non-NULL). If we still don't
20325 have an absolute path prepend comp_dir (if non-NULL).
20326 However, the directory we record in the include-file's
20327 psymtab does not contain COMP_DIR (to match the
20328 corresponding symtab(s)).
20329
20330 Example:
20331
20332 bash$ cd /tmp
20333 bash$ gcc -g ./hello.c
20334 include_name = "hello.c"
20335 dir_name = "."
20336 DW_AT_comp_dir = comp_dir = "/tmp"
20337 DW_AT_name = "./hello.c"
20338
20339 */
20340
20341 if (dir_name != NULL)
20342 {
20343 name_holder->reset (concat (dir_name, SLASH_STRING,
20344 include_name, (char *) NULL));
20345 include_name = name_holder->get ();
20346 include_name_to_compare = include_name;
20347 }
20348 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20349 {
20350 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20351 include_name, (char *) NULL));
20352 include_name_to_compare = hold_compare.get ();
20353 }
20354 }
20355
20356 pst_filename = pst->filename;
20357 gdb::unique_xmalloc_ptr<char> copied_name;
20358 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20359 {
20360 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20361 pst_filename, (char *) NULL));
20362 pst_filename = copied_name.get ();
20363 }
20364
20365 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20366
20367 if (file_is_pst)
20368 return NULL;
20369 return include_name;
20370 }
20371
20372 /* State machine to track the state of the line number program. */
20373
20374 class lnp_state_machine
20375 {
20376 public:
20377 /* Initialize a machine state for the start of a line number
20378 program. */
20379 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20380
20381 file_entry *current_file ()
20382 {
20383 /* lh->file_names is 0-based, but the file name numbers in the
20384 statement program are 1-based. */
20385 return m_line_header->file_name_at (m_file);
20386 }
20387
20388 /* Record the line in the state machine. END_SEQUENCE is true if
20389 we're processing the end of a sequence. */
20390 void record_line (bool end_sequence);
20391
20392 /* Check address and if invalid nop-out the rest of the lines in this
20393 sequence. */
20394 void check_line_address (struct dwarf2_cu *cu,
20395 const gdb_byte *line_ptr,
20396 CORE_ADDR lowpc, CORE_ADDR address);
20397
20398 void handle_set_discriminator (unsigned int discriminator)
20399 {
20400 m_discriminator = discriminator;
20401 m_line_has_non_zero_discriminator |= discriminator != 0;
20402 }
20403
20404 /* Handle DW_LNE_set_address. */
20405 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20406 {
20407 m_op_index = 0;
20408 address += baseaddr;
20409 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20410 }
20411
20412 /* Handle DW_LNS_advance_pc. */
20413 void handle_advance_pc (CORE_ADDR adjust);
20414
20415 /* Handle a special opcode. */
20416 void handle_special_opcode (unsigned char op_code);
20417
20418 /* Handle DW_LNS_advance_line. */
20419 void handle_advance_line (int line_delta)
20420 {
20421 advance_line (line_delta);
20422 }
20423
20424 /* Handle DW_LNS_set_file. */
20425 void handle_set_file (file_name_index file);
20426
20427 /* Handle DW_LNS_negate_stmt. */
20428 void handle_negate_stmt ()
20429 {
20430 m_is_stmt = !m_is_stmt;
20431 }
20432
20433 /* Handle DW_LNS_const_add_pc. */
20434 void handle_const_add_pc ();
20435
20436 /* Handle DW_LNS_fixed_advance_pc. */
20437 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20438 {
20439 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20440 m_op_index = 0;
20441 }
20442
20443 /* Handle DW_LNS_copy. */
20444 void handle_copy ()
20445 {
20446 record_line (false);
20447 m_discriminator = 0;
20448 }
20449
20450 /* Handle DW_LNE_end_sequence. */
20451 void handle_end_sequence ()
20452 {
20453 m_record_line_callback = ::record_line;
20454 }
20455
20456 private:
20457 /* Advance the line by LINE_DELTA. */
20458 void advance_line (int line_delta)
20459 {
20460 m_line += line_delta;
20461
20462 if (line_delta != 0)
20463 m_line_has_non_zero_discriminator = m_discriminator != 0;
20464 }
20465
20466 gdbarch *m_gdbarch;
20467
20468 /* True if we're recording lines.
20469 Otherwise we're building partial symtabs and are just interested in
20470 finding include files mentioned by the line number program. */
20471 bool m_record_lines_p;
20472
20473 /* The line number header. */
20474 line_header *m_line_header;
20475
20476 /* These are part of the standard DWARF line number state machine,
20477 and initialized according to the DWARF spec. */
20478
20479 unsigned char m_op_index = 0;
20480 /* The line table index (1-based) of the current file. */
20481 file_name_index m_file = (file_name_index) 1;
20482 unsigned int m_line = 1;
20483
20484 /* These are initialized in the constructor. */
20485
20486 CORE_ADDR m_address;
20487 bool m_is_stmt;
20488 unsigned int m_discriminator;
20489
20490 /* Additional bits of state we need to track. */
20491
20492 /* The last file that we called dwarf2_start_subfile for.
20493 This is only used for TLLs. */
20494 unsigned int m_last_file = 0;
20495 /* The last file a line number was recorded for. */
20496 struct subfile *m_last_subfile = NULL;
20497
20498 /* The function to call to record a line. */
20499 record_line_ftype *m_record_line_callback = NULL;
20500
20501 /* The last line number that was recorded, used to coalesce
20502 consecutive entries for the same line. This can happen, for
20503 example, when discriminators are present. PR 17276. */
20504 unsigned int m_last_line = 0;
20505 bool m_line_has_non_zero_discriminator = false;
20506 };
20507
20508 void
20509 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20510 {
20511 CORE_ADDR addr_adj = (((m_op_index + adjust)
20512 / m_line_header->maximum_ops_per_instruction)
20513 * m_line_header->minimum_instruction_length);
20514 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20515 m_op_index = ((m_op_index + adjust)
20516 % m_line_header->maximum_ops_per_instruction);
20517 }
20518
20519 void
20520 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20521 {
20522 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20523 CORE_ADDR addr_adj = (((m_op_index
20524 + (adj_opcode / m_line_header->line_range))
20525 / m_line_header->maximum_ops_per_instruction)
20526 * m_line_header->minimum_instruction_length);
20527 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20528 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20529 % m_line_header->maximum_ops_per_instruction);
20530
20531 int line_delta = (m_line_header->line_base
20532 + (adj_opcode % m_line_header->line_range));
20533 advance_line (line_delta);
20534 record_line (false);
20535 m_discriminator = 0;
20536 }
20537
20538 void
20539 lnp_state_machine::handle_set_file (file_name_index file)
20540 {
20541 m_file = file;
20542
20543 const file_entry *fe = current_file ();
20544 if (fe == NULL)
20545 dwarf2_debug_line_missing_file_complaint ();
20546 else if (m_record_lines_p)
20547 {
20548 const char *dir = fe->include_dir (m_line_header);
20549
20550 m_last_subfile = current_subfile;
20551 m_line_has_non_zero_discriminator = m_discriminator != 0;
20552 dwarf2_start_subfile (fe->name, dir);
20553 }
20554 }
20555
20556 void
20557 lnp_state_machine::handle_const_add_pc ()
20558 {
20559 CORE_ADDR adjust
20560 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20561
20562 CORE_ADDR addr_adj
20563 = (((m_op_index + adjust)
20564 / m_line_header->maximum_ops_per_instruction)
20565 * m_line_header->minimum_instruction_length);
20566
20567 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20568 m_op_index = ((m_op_index + adjust)
20569 % m_line_header->maximum_ops_per_instruction);
20570 }
20571
20572 /* Ignore this record_line request. */
20573
20574 static void
20575 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20576 {
20577 return;
20578 }
20579
20580 /* Return non-zero if we should add LINE to the line number table.
20581 LINE is the line to add, LAST_LINE is the last line that was added,
20582 LAST_SUBFILE is the subfile for LAST_LINE.
20583 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20584 had a non-zero discriminator.
20585
20586 We have to be careful in the presence of discriminators.
20587 E.g., for this line:
20588
20589 for (i = 0; i < 100000; i++);
20590
20591 clang can emit four line number entries for that one line,
20592 each with a different discriminator.
20593 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20594
20595 However, we want gdb to coalesce all four entries into one.
20596 Otherwise the user could stepi into the middle of the line and
20597 gdb would get confused about whether the pc really was in the
20598 middle of the line.
20599
20600 Things are further complicated by the fact that two consecutive
20601 line number entries for the same line is a heuristic used by gcc
20602 to denote the end of the prologue. So we can't just discard duplicate
20603 entries, we have to be selective about it. The heuristic we use is
20604 that we only collapse consecutive entries for the same line if at least
20605 one of those entries has a non-zero discriminator. PR 17276.
20606
20607 Note: Addresses in the line number state machine can never go backwards
20608 within one sequence, thus this coalescing is ok. */
20609
20610 static int
20611 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20612 int line_has_non_zero_discriminator,
20613 struct subfile *last_subfile)
20614 {
20615 if (current_subfile != last_subfile)
20616 return 1;
20617 if (line != last_line)
20618 return 1;
20619 /* Same line for the same file that we've seen already.
20620 As a last check, for pr 17276, only record the line if the line
20621 has never had a non-zero discriminator. */
20622 if (!line_has_non_zero_discriminator)
20623 return 1;
20624 return 0;
20625 }
20626
20627 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20628 in the line table of subfile SUBFILE. */
20629
20630 static void
20631 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20632 unsigned int line, CORE_ADDR address,
20633 record_line_ftype p_record_line)
20634 {
20635 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20636
20637 if (dwarf_line_debug)
20638 {
20639 fprintf_unfiltered (gdb_stdlog,
20640 "Recording line %u, file %s, address %s\n",
20641 line, lbasename (subfile->name),
20642 paddress (gdbarch, address));
20643 }
20644
20645 (*p_record_line) (subfile, line, addr);
20646 }
20647
20648 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20649 Mark the end of a set of line number records.
20650 The arguments are the same as for dwarf_record_line_1.
20651 If SUBFILE is NULL the request is ignored. */
20652
20653 static void
20654 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20655 CORE_ADDR address, record_line_ftype p_record_line)
20656 {
20657 if (subfile == NULL)
20658 return;
20659
20660 if (dwarf_line_debug)
20661 {
20662 fprintf_unfiltered (gdb_stdlog,
20663 "Finishing current line, file %s, address %s\n",
20664 lbasename (subfile->name),
20665 paddress (gdbarch, address));
20666 }
20667
20668 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20669 }
20670
20671 void
20672 lnp_state_machine::record_line (bool end_sequence)
20673 {
20674 if (dwarf_line_debug)
20675 {
20676 fprintf_unfiltered (gdb_stdlog,
20677 "Processing actual line %u: file %u,"
20678 " address %s, is_stmt %u, discrim %u\n",
20679 m_line, to_underlying (m_file),
20680 paddress (m_gdbarch, m_address),
20681 m_is_stmt, m_discriminator);
20682 }
20683
20684 file_entry *fe = current_file ();
20685
20686 if (fe == NULL)
20687 dwarf2_debug_line_missing_file_complaint ();
20688 /* For now we ignore lines not starting on an instruction boundary.
20689 But not when processing end_sequence for compatibility with the
20690 previous version of the code. */
20691 else if (m_op_index == 0 || end_sequence)
20692 {
20693 fe->included_p = 1;
20694 if (m_record_lines_p && m_is_stmt)
20695 {
20696 if (m_last_subfile != current_subfile || end_sequence)
20697 {
20698 dwarf_finish_line (m_gdbarch, m_last_subfile,
20699 m_address, m_record_line_callback);
20700 }
20701
20702 if (!end_sequence)
20703 {
20704 if (dwarf_record_line_p (m_line, m_last_line,
20705 m_line_has_non_zero_discriminator,
20706 m_last_subfile))
20707 {
20708 dwarf_record_line_1 (m_gdbarch, current_subfile,
20709 m_line, m_address,
20710 m_record_line_callback);
20711 }
20712 m_last_subfile = current_subfile;
20713 m_last_line = m_line;
20714 }
20715 }
20716 }
20717 }
20718
20719 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20720 bool record_lines_p)
20721 {
20722 m_gdbarch = arch;
20723 m_record_lines_p = record_lines_p;
20724 m_line_header = lh;
20725
20726 m_record_line_callback = ::record_line;
20727
20728 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20729 was a line entry for it so that the backend has a chance to adjust it
20730 and also record it in case it needs it. This is currently used by MIPS
20731 code, cf. `mips_adjust_dwarf2_line'. */
20732 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20733 m_is_stmt = lh->default_is_stmt;
20734 m_discriminator = 0;
20735 }
20736
20737 void
20738 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20739 const gdb_byte *line_ptr,
20740 CORE_ADDR lowpc, CORE_ADDR address)
20741 {
20742 /* If address < lowpc then it's not a usable value, it's outside the
20743 pc range of the CU. However, we restrict the test to only address
20744 values of zero to preserve GDB's previous behaviour which is to
20745 handle the specific case of a function being GC'd by the linker. */
20746
20747 if (address == 0 && address < lowpc)
20748 {
20749 /* This line table is for a function which has been
20750 GCd by the linker. Ignore it. PR gdb/12528 */
20751
20752 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20753 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20754
20755 complaint (&symfile_complaints,
20756 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20757 line_offset, objfile_name (objfile));
20758 m_record_line_callback = noop_record_line;
20759 /* Note: record_line_callback is left as noop_record_line until
20760 we see DW_LNE_end_sequence. */
20761 }
20762 }
20763
20764 /* Subroutine of dwarf_decode_lines to simplify it.
20765 Process the line number information in LH.
20766 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20767 program in order to set included_p for every referenced header. */
20768
20769 static void
20770 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20771 const int decode_for_pst_p, CORE_ADDR lowpc)
20772 {
20773 const gdb_byte *line_ptr, *extended_end;
20774 const gdb_byte *line_end;
20775 unsigned int bytes_read, extended_len;
20776 unsigned char op_code, extended_op;
20777 CORE_ADDR baseaddr;
20778 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20779 bfd *abfd = objfile->obfd;
20780 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20781 /* True if we're recording line info (as opposed to building partial
20782 symtabs and just interested in finding include files mentioned by
20783 the line number program). */
20784 bool record_lines_p = !decode_for_pst_p;
20785
20786 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20787
20788 line_ptr = lh->statement_program_start;
20789 line_end = lh->statement_program_end;
20790
20791 /* Read the statement sequences until there's nothing left. */
20792 while (line_ptr < line_end)
20793 {
20794 /* The DWARF line number program state machine. Reset the state
20795 machine at the start of each sequence. */
20796 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20797 bool end_sequence = false;
20798
20799 if (record_lines_p)
20800 {
20801 /* Start a subfile for the current file of the state
20802 machine. */
20803 const file_entry *fe = state_machine.current_file ();
20804
20805 if (fe != NULL)
20806 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20807 }
20808
20809 /* Decode the table. */
20810 while (line_ptr < line_end && !end_sequence)
20811 {
20812 op_code = read_1_byte (abfd, line_ptr);
20813 line_ptr += 1;
20814
20815 if (op_code >= lh->opcode_base)
20816 {
20817 /* Special opcode. */
20818 state_machine.handle_special_opcode (op_code);
20819 }
20820 else switch (op_code)
20821 {
20822 case DW_LNS_extended_op:
20823 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20824 &bytes_read);
20825 line_ptr += bytes_read;
20826 extended_end = line_ptr + extended_len;
20827 extended_op = read_1_byte (abfd, line_ptr);
20828 line_ptr += 1;
20829 switch (extended_op)
20830 {
20831 case DW_LNE_end_sequence:
20832 state_machine.handle_end_sequence ();
20833 end_sequence = true;
20834 break;
20835 case DW_LNE_set_address:
20836 {
20837 CORE_ADDR address
20838 = read_address (abfd, line_ptr, cu, &bytes_read);
20839 line_ptr += bytes_read;
20840
20841 state_machine.check_line_address (cu, line_ptr,
20842 lowpc, address);
20843 state_machine.handle_set_address (baseaddr, address);
20844 }
20845 break;
20846 case DW_LNE_define_file:
20847 {
20848 const char *cur_file;
20849 unsigned int mod_time, length;
20850 dir_index dindex;
20851
20852 cur_file = read_direct_string (abfd, line_ptr,
20853 &bytes_read);
20854 line_ptr += bytes_read;
20855 dindex = (dir_index)
20856 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20857 line_ptr += bytes_read;
20858 mod_time =
20859 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20860 line_ptr += bytes_read;
20861 length =
20862 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20863 line_ptr += bytes_read;
20864 lh->add_file_name (cur_file, dindex, mod_time, length);
20865 }
20866 break;
20867 case DW_LNE_set_discriminator:
20868 {
20869 /* The discriminator is not interesting to the
20870 debugger; just ignore it. We still need to
20871 check its value though:
20872 if there are consecutive entries for the same
20873 (non-prologue) line we want to coalesce them.
20874 PR 17276. */
20875 unsigned int discr
20876 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20877 line_ptr += bytes_read;
20878
20879 state_machine.handle_set_discriminator (discr);
20880 }
20881 break;
20882 default:
20883 complaint (&symfile_complaints,
20884 _("mangled .debug_line section"));
20885 return;
20886 }
20887 /* Make sure that we parsed the extended op correctly. If e.g.
20888 we expected a different address size than the producer used,
20889 we may have read the wrong number of bytes. */
20890 if (line_ptr != extended_end)
20891 {
20892 complaint (&symfile_complaints,
20893 _("mangled .debug_line section"));
20894 return;
20895 }
20896 break;
20897 case DW_LNS_copy:
20898 state_machine.handle_copy ();
20899 break;
20900 case DW_LNS_advance_pc:
20901 {
20902 CORE_ADDR adjust
20903 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20904 line_ptr += bytes_read;
20905
20906 state_machine.handle_advance_pc (adjust);
20907 }
20908 break;
20909 case DW_LNS_advance_line:
20910 {
20911 int line_delta
20912 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20913 line_ptr += bytes_read;
20914
20915 state_machine.handle_advance_line (line_delta);
20916 }
20917 break;
20918 case DW_LNS_set_file:
20919 {
20920 file_name_index file
20921 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20922 &bytes_read);
20923 line_ptr += bytes_read;
20924
20925 state_machine.handle_set_file (file);
20926 }
20927 break;
20928 case DW_LNS_set_column:
20929 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20930 line_ptr += bytes_read;
20931 break;
20932 case DW_LNS_negate_stmt:
20933 state_machine.handle_negate_stmt ();
20934 break;
20935 case DW_LNS_set_basic_block:
20936 break;
20937 /* Add to the address register of the state machine the
20938 address increment value corresponding to special opcode
20939 255. I.e., this value is scaled by the minimum
20940 instruction length since special opcode 255 would have
20941 scaled the increment. */
20942 case DW_LNS_const_add_pc:
20943 state_machine.handle_const_add_pc ();
20944 break;
20945 case DW_LNS_fixed_advance_pc:
20946 {
20947 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20948 line_ptr += 2;
20949
20950 state_machine.handle_fixed_advance_pc (addr_adj);
20951 }
20952 break;
20953 default:
20954 {
20955 /* Unknown standard opcode, ignore it. */
20956 int i;
20957
20958 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20959 {
20960 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20961 line_ptr += bytes_read;
20962 }
20963 }
20964 }
20965 }
20966
20967 if (!end_sequence)
20968 dwarf2_debug_line_missing_end_sequence_complaint ();
20969
20970 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20971 in which case we still finish recording the last line). */
20972 state_machine.record_line (true);
20973 }
20974 }
20975
20976 /* Decode the Line Number Program (LNP) for the given line_header
20977 structure and CU. The actual information extracted and the type
20978 of structures created from the LNP depends on the value of PST.
20979
20980 1. If PST is NULL, then this procedure uses the data from the program
20981 to create all necessary symbol tables, and their linetables.
20982
20983 2. If PST is not NULL, this procedure reads the program to determine
20984 the list of files included by the unit represented by PST, and
20985 builds all the associated partial symbol tables.
20986
20987 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20988 It is used for relative paths in the line table.
20989 NOTE: When processing partial symtabs (pst != NULL),
20990 comp_dir == pst->dirname.
20991
20992 NOTE: It is important that psymtabs have the same file name (via strcmp)
20993 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20994 symtab we don't use it in the name of the psymtabs we create.
20995 E.g. expand_line_sal requires this when finding psymtabs to expand.
20996 A good testcase for this is mb-inline.exp.
20997
20998 LOWPC is the lowest address in CU (or 0 if not known).
20999
21000 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21001 for its PC<->lines mapping information. Otherwise only the filename
21002 table is read in. */
21003
21004 static void
21005 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21006 struct dwarf2_cu *cu, struct partial_symtab *pst,
21007 CORE_ADDR lowpc, int decode_mapping)
21008 {
21009 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21010 const int decode_for_pst_p = (pst != NULL);
21011
21012 if (decode_mapping)
21013 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21014
21015 if (decode_for_pst_p)
21016 {
21017 int file_index;
21018
21019 /* Now that we're done scanning the Line Header Program, we can
21020 create the psymtab of each included file. */
21021 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21022 if (lh->file_names[file_index].included_p == 1)
21023 {
21024 gdb::unique_xmalloc_ptr<char> name_holder;
21025 const char *include_name =
21026 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21027 &name_holder);
21028 if (include_name != NULL)
21029 dwarf2_create_include_psymtab (include_name, pst, objfile);
21030 }
21031 }
21032 else
21033 {
21034 /* Make sure a symtab is created for every file, even files
21035 which contain only variables (i.e. no code with associated
21036 line numbers). */
21037 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21038 int i;
21039
21040 for (i = 0; i < lh->file_names.size (); i++)
21041 {
21042 file_entry &fe = lh->file_names[i];
21043
21044 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21045
21046 if (current_subfile->symtab == NULL)
21047 {
21048 current_subfile->symtab
21049 = allocate_symtab (cust, current_subfile->name);
21050 }
21051 fe.symtab = current_subfile->symtab;
21052 }
21053 }
21054 }
21055
21056 /* Start a subfile for DWARF. FILENAME is the name of the file and
21057 DIRNAME the name of the source directory which contains FILENAME
21058 or NULL if not known.
21059 This routine tries to keep line numbers from identical absolute and
21060 relative file names in a common subfile.
21061
21062 Using the `list' example from the GDB testsuite, which resides in
21063 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21064 of /srcdir/list0.c yields the following debugging information for list0.c:
21065
21066 DW_AT_name: /srcdir/list0.c
21067 DW_AT_comp_dir: /compdir
21068 files.files[0].name: list0.h
21069 files.files[0].dir: /srcdir
21070 files.files[1].name: list0.c
21071 files.files[1].dir: /srcdir
21072
21073 The line number information for list0.c has to end up in a single
21074 subfile, so that `break /srcdir/list0.c:1' works as expected.
21075 start_subfile will ensure that this happens provided that we pass the
21076 concatenation of files.files[1].dir and files.files[1].name as the
21077 subfile's name. */
21078
21079 static void
21080 dwarf2_start_subfile (const char *filename, const char *dirname)
21081 {
21082 char *copy = NULL;
21083
21084 /* In order not to lose the line information directory,
21085 we concatenate it to the filename when it makes sense.
21086 Note that the Dwarf3 standard says (speaking of filenames in line
21087 information): ``The directory index is ignored for file names
21088 that represent full path names''. Thus ignoring dirname in the
21089 `else' branch below isn't an issue. */
21090
21091 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21092 {
21093 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21094 filename = copy;
21095 }
21096
21097 start_subfile (filename);
21098
21099 if (copy != NULL)
21100 xfree (copy);
21101 }
21102
21103 /* Start a symtab for DWARF.
21104 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21105
21106 static struct compunit_symtab *
21107 dwarf2_start_symtab (struct dwarf2_cu *cu,
21108 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21109 {
21110 struct compunit_symtab *cust
21111 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21112 low_pc, cu->language);
21113
21114 record_debugformat ("DWARF 2");
21115 record_producer (cu->producer);
21116
21117 /* We assume that we're processing GCC output. */
21118 processing_gcc_compilation = 2;
21119
21120 cu->processing_has_namespace_info = 0;
21121
21122 return cust;
21123 }
21124
21125 static void
21126 var_decode_location (struct attribute *attr, struct symbol *sym,
21127 struct dwarf2_cu *cu)
21128 {
21129 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21130 struct comp_unit_head *cu_header = &cu->header;
21131
21132 /* NOTE drow/2003-01-30: There used to be a comment and some special
21133 code here to turn a symbol with DW_AT_external and a
21134 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21135 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21136 with some versions of binutils) where shared libraries could have
21137 relocations against symbols in their debug information - the
21138 minimal symbol would have the right address, but the debug info
21139 would not. It's no longer necessary, because we will explicitly
21140 apply relocations when we read in the debug information now. */
21141
21142 /* A DW_AT_location attribute with no contents indicates that a
21143 variable has been optimized away. */
21144 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21145 {
21146 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21147 return;
21148 }
21149
21150 /* Handle one degenerate form of location expression specially, to
21151 preserve GDB's previous behavior when section offsets are
21152 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21153 then mark this symbol as LOC_STATIC. */
21154
21155 if (attr_form_is_block (attr)
21156 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21157 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21158 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21159 && (DW_BLOCK (attr)->size
21160 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21161 {
21162 unsigned int dummy;
21163
21164 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21165 SYMBOL_VALUE_ADDRESS (sym) =
21166 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21167 else
21168 SYMBOL_VALUE_ADDRESS (sym) =
21169 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21170 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21171 fixup_symbol_section (sym, objfile);
21172 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21173 SYMBOL_SECTION (sym));
21174 return;
21175 }
21176
21177 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21178 expression evaluator, and use LOC_COMPUTED only when necessary
21179 (i.e. when the value of a register or memory location is
21180 referenced, or a thread-local block, etc.). Then again, it might
21181 not be worthwhile. I'm assuming that it isn't unless performance
21182 or memory numbers show me otherwise. */
21183
21184 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21185
21186 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21187 cu->has_loclist = 1;
21188 }
21189
21190 /* Given a pointer to a DWARF information entry, figure out if we need
21191 to make a symbol table entry for it, and if so, create a new entry
21192 and return a pointer to it.
21193 If TYPE is NULL, determine symbol type from the die, otherwise
21194 used the passed type.
21195 If SPACE is not NULL, use it to hold the new symbol. If it is
21196 NULL, allocate a new symbol on the objfile's obstack. */
21197
21198 static struct symbol *
21199 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21200 struct symbol *space)
21201 {
21202 struct dwarf2_per_objfile *dwarf2_per_objfile
21203 = cu->per_cu->dwarf2_per_objfile;
21204 struct objfile *objfile = dwarf2_per_objfile->objfile;
21205 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21206 struct symbol *sym = NULL;
21207 const char *name;
21208 struct attribute *attr = NULL;
21209 struct attribute *attr2 = NULL;
21210 CORE_ADDR baseaddr;
21211 struct pending **list_to_add = NULL;
21212
21213 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21214
21215 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21216
21217 name = dwarf2_name (die, cu);
21218 if (name)
21219 {
21220 const char *linkagename;
21221 int suppress_add = 0;
21222
21223 if (space)
21224 sym = space;
21225 else
21226 sym = allocate_symbol (objfile);
21227 OBJSTAT (objfile, n_syms++);
21228
21229 /* Cache this symbol's name and the name's demangled form (if any). */
21230 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21231 linkagename = dwarf2_physname (name, die, cu);
21232 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21233
21234 /* Fortran does not have mangling standard and the mangling does differ
21235 between gfortran, iFort etc. */
21236 if (cu->language == language_fortran
21237 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21238 symbol_set_demangled_name (&(sym->ginfo),
21239 dwarf2_full_name (name, die, cu),
21240 NULL);
21241
21242 /* Default assumptions.
21243 Use the passed type or decode it from the die. */
21244 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21245 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21246 if (type != NULL)
21247 SYMBOL_TYPE (sym) = type;
21248 else
21249 SYMBOL_TYPE (sym) = die_type (die, cu);
21250 attr = dwarf2_attr (die,
21251 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21252 cu);
21253 if (attr)
21254 {
21255 SYMBOL_LINE (sym) = DW_UNSND (attr);
21256 }
21257
21258 attr = dwarf2_attr (die,
21259 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21260 cu);
21261 if (attr)
21262 {
21263 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21264 struct file_entry *fe;
21265
21266 if (cu->line_header != NULL)
21267 fe = cu->line_header->file_name_at (file_index);
21268 else
21269 fe = NULL;
21270
21271 if (fe == NULL)
21272 complaint (&symfile_complaints,
21273 _("file index out of range"));
21274 else
21275 symbol_set_symtab (sym, fe->symtab);
21276 }
21277
21278 switch (die->tag)
21279 {
21280 case DW_TAG_label:
21281 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21282 if (attr)
21283 {
21284 CORE_ADDR addr;
21285
21286 addr = attr_value_as_address (attr);
21287 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21288 SYMBOL_VALUE_ADDRESS (sym) = addr;
21289 }
21290 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21291 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21292 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21293 add_symbol_to_list (sym, cu->list_in_scope);
21294 break;
21295 case DW_TAG_subprogram:
21296 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21297 finish_block. */
21298 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21299 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21300 if ((attr2 && (DW_UNSND (attr2) != 0))
21301 || cu->language == language_ada)
21302 {
21303 /* Subprograms marked external are stored as a global symbol.
21304 Ada subprograms, whether marked external or not, are always
21305 stored as a global symbol, because we want to be able to
21306 access them globally. For instance, we want to be able
21307 to break on a nested subprogram without having to
21308 specify the context. */
21309 list_to_add = &global_symbols;
21310 }
21311 else
21312 {
21313 list_to_add = cu->list_in_scope;
21314 }
21315 break;
21316 case DW_TAG_inlined_subroutine:
21317 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21318 finish_block. */
21319 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21320 SYMBOL_INLINED (sym) = 1;
21321 list_to_add = cu->list_in_scope;
21322 break;
21323 case DW_TAG_template_value_param:
21324 suppress_add = 1;
21325 /* Fall through. */
21326 case DW_TAG_constant:
21327 case DW_TAG_variable:
21328 case DW_TAG_member:
21329 /* Compilation with minimal debug info may result in
21330 variables with missing type entries. Change the
21331 misleading `void' type to something sensible. */
21332 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21333 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21334
21335 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21336 /* In the case of DW_TAG_member, we should only be called for
21337 static const members. */
21338 if (die->tag == DW_TAG_member)
21339 {
21340 /* dwarf2_add_field uses die_is_declaration,
21341 so we do the same. */
21342 gdb_assert (die_is_declaration (die, cu));
21343 gdb_assert (attr);
21344 }
21345 if (attr)
21346 {
21347 dwarf2_const_value (attr, sym, cu);
21348 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21349 if (!suppress_add)
21350 {
21351 if (attr2 && (DW_UNSND (attr2) != 0))
21352 list_to_add = &global_symbols;
21353 else
21354 list_to_add = cu->list_in_scope;
21355 }
21356 break;
21357 }
21358 attr = dwarf2_attr (die, DW_AT_location, cu);
21359 if (attr)
21360 {
21361 var_decode_location (attr, sym, cu);
21362 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21363
21364 /* Fortran explicitly imports any global symbols to the local
21365 scope by DW_TAG_common_block. */
21366 if (cu->language == language_fortran && die->parent
21367 && die->parent->tag == DW_TAG_common_block)
21368 attr2 = NULL;
21369
21370 if (SYMBOL_CLASS (sym) == LOC_STATIC
21371 && SYMBOL_VALUE_ADDRESS (sym) == 0
21372 && !dwarf2_per_objfile->has_section_at_zero)
21373 {
21374 /* When a static variable is eliminated by the linker,
21375 the corresponding debug information is not stripped
21376 out, but the variable address is set to null;
21377 do not add such variables into symbol table. */
21378 }
21379 else if (attr2 && (DW_UNSND (attr2) != 0))
21380 {
21381 /* Workaround gfortran PR debug/40040 - it uses
21382 DW_AT_location for variables in -fPIC libraries which may
21383 get overriden by other libraries/executable and get
21384 a different address. Resolve it by the minimal symbol
21385 which may come from inferior's executable using copy
21386 relocation. Make this workaround only for gfortran as for
21387 other compilers GDB cannot guess the minimal symbol
21388 Fortran mangling kind. */
21389 if (cu->language == language_fortran && die->parent
21390 && die->parent->tag == DW_TAG_module
21391 && cu->producer
21392 && startswith (cu->producer, "GNU Fortran"))
21393 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21394
21395 /* A variable with DW_AT_external is never static,
21396 but it may be block-scoped. */
21397 list_to_add = (cu->list_in_scope == &file_symbols
21398 ? &global_symbols : cu->list_in_scope);
21399 }
21400 else
21401 list_to_add = cu->list_in_scope;
21402 }
21403 else
21404 {
21405 /* We do not know the address of this symbol.
21406 If it is an external symbol and we have type information
21407 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21408 The address of the variable will then be determined from
21409 the minimal symbol table whenever the variable is
21410 referenced. */
21411 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21412
21413 /* Fortran explicitly imports any global symbols to the local
21414 scope by DW_TAG_common_block. */
21415 if (cu->language == language_fortran && die->parent
21416 && die->parent->tag == DW_TAG_common_block)
21417 {
21418 /* SYMBOL_CLASS doesn't matter here because
21419 read_common_block is going to reset it. */
21420 if (!suppress_add)
21421 list_to_add = cu->list_in_scope;
21422 }
21423 else if (attr2 && (DW_UNSND (attr2) != 0)
21424 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21425 {
21426 /* A variable with DW_AT_external is never static, but it
21427 may be block-scoped. */
21428 list_to_add = (cu->list_in_scope == &file_symbols
21429 ? &global_symbols : cu->list_in_scope);
21430
21431 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21432 }
21433 else if (!die_is_declaration (die, cu))
21434 {
21435 /* Use the default LOC_OPTIMIZED_OUT class. */
21436 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21437 if (!suppress_add)
21438 list_to_add = cu->list_in_scope;
21439 }
21440 }
21441 break;
21442 case DW_TAG_formal_parameter:
21443 /* If we are inside a function, mark this as an argument. If
21444 not, we might be looking at an argument to an inlined function
21445 when we do not have enough information to show inlined frames;
21446 pretend it's a local variable in that case so that the user can
21447 still see it. */
21448 if (context_stack_depth > 0
21449 && context_stack[context_stack_depth - 1].name != NULL)
21450 SYMBOL_IS_ARGUMENT (sym) = 1;
21451 attr = dwarf2_attr (die, DW_AT_location, cu);
21452 if (attr)
21453 {
21454 var_decode_location (attr, sym, cu);
21455 }
21456 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21457 if (attr)
21458 {
21459 dwarf2_const_value (attr, sym, cu);
21460 }
21461
21462 list_to_add = cu->list_in_scope;
21463 break;
21464 case DW_TAG_unspecified_parameters:
21465 /* From varargs functions; gdb doesn't seem to have any
21466 interest in this information, so just ignore it for now.
21467 (FIXME?) */
21468 break;
21469 case DW_TAG_template_type_param:
21470 suppress_add = 1;
21471 /* Fall through. */
21472 case DW_TAG_class_type:
21473 case DW_TAG_interface_type:
21474 case DW_TAG_structure_type:
21475 case DW_TAG_union_type:
21476 case DW_TAG_set_type:
21477 case DW_TAG_enumeration_type:
21478 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21479 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21480
21481 {
21482 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21483 really ever be static objects: otherwise, if you try
21484 to, say, break of a class's method and you're in a file
21485 which doesn't mention that class, it won't work unless
21486 the check for all static symbols in lookup_symbol_aux
21487 saves you. See the OtherFileClass tests in
21488 gdb.c++/namespace.exp. */
21489
21490 if (!suppress_add)
21491 {
21492 list_to_add = (cu->list_in_scope == &file_symbols
21493 && cu->language == language_cplus
21494 ? &global_symbols : cu->list_in_scope);
21495
21496 /* The semantics of C++ state that "struct foo {
21497 ... }" also defines a typedef for "foo". */
21498 if (cu->language == language_cplus
21499 || cu->language == language_ada
21500 || cu->language == language_d
21501 || cu->language == language_rust)
21502 {
21503 /* The symbol's name is already allocated along
21504 with this objfile, so we don't need to
21505 duplicate it for the type. */
21506 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21507 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21508 }
21509 }
21510 }
21511 break;
21512 case DW_TAG_typedef:
21513 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21514 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21515 list_to_add = cu->list_in_scope;
21516 break;
21517 case DW_TAG_base_type:
21518 case DW_TAG_subrange_type:
21519 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21520 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21521 list_to_add = cu->list_in_scope;
21522 break;
21523 case DW_TAG_enumerator:
21524 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21525 if (attr)
21526 {
21527 dwarf2_const_value (attr, sym, cu);
21528 }
21529 {
21530 /* NOTE: carlton/2003-11-10: See comment above in the
21531 DW_TAG_class_type, etc. block. */
21532
21533 list_to_add = (cu->list_in_scope == &file_symbols
21534 && cu->language == language_cplus
21535 ? &global_symbols : cu->list_in_scope);
21536 }
21537 break;
21538 case DW_TAG_imported_declaration:
21539 case DW_TAG_namespace:
21540 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21541 list_to_add = &global_symbols;
21542 break;
21543 case DW_TAG_module:
21544 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21545 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21546 list_to_add = &global_symbols;
21547 break;
21548 case DW_TAG_common_block:
21549 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21550 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21551 add_symbol_to_list (sym, cu->list_in_scope);
21552 break;
21553 default:
21554 /* Not a tag we recognize. Hopefully we aren't processing
21555 trash data, but since we must specifically ignore things
21556 we don't recognize, there is nothing else we should do at
21557 this point. */
21558 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21559 dwarf_tag_name (die->tag));
21560 break;
21561 }
21562
21563 if (suppress_add)
21564 {
21565 sym->hash_next = objfile->template_symbols;
21566 objfile->template_symbols = sym;
21567 list_to_add = NULL;
21568 }
21569
21570 if (list_to_add != NULL)
21571 add_symbol_to_list (sym, list_to_add);
21572
21573 /* For the benefit of old versions of GCC, check for anonymous
21574 namespaces based on the demangled name. */
21575 if (!cu->processing_has_namespace_info
21576 && cu->language == language_cplus)
21577 cp_scan_for_anonymous_namespaces (sym, objfile);
21578 }
21579 return (sym);
21580 }
21581
21582 /* Given an attr with a DW_FORM_dataN value in host byte order,
21583 zero-extend it as appropriate for the symbol's type. The DWARF
21584 standard (v4) is not entirely clear about the meaning of using
21585 DW_FORM_dataN for a constant with a signed type, where the type is
21586 wider than the data. The conclusion of a discussion on the DWARF
21587 list was that this is unspecified. We choose to always zero-extend
21588 because that is the interpretation long in use by GCC. */
21589
21590 static gdb_byte *
21591 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21592 struct dwarf2_cu *cu, LONGEST *value, int bits)
21593 {
21594 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21595 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21596 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21597 LONGEST l = DW_UNSND (attr);
21598
21599 if (bits < sizeof (*value) * 8)
21600 {
21601 l &= ((LONGEST) 1 << bits) - 1;
21602 *value = l;
21603 }
21604 else if (bits == sizeof (*value) * 8)
21605 *value = l;
21606 else
21607 {
21608 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21609 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21610 return bytes;
21611 }
21612
21613 return NULL;
21614 }
21615
21616 /* Read a constant value from an attribute. Either set *VALUE, or if
21617 the value does not fit in *VALUE, set *BYTES - either already
21618 allocated on the objfile obstack, or newly allocated on OBSTACK,
21619 or, set *BATON, if we translated the constant to a location
21620 expression. */
21621
21622 static void
21623 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21624 const char *name, struct obstack *obstack,
21625 struct dwarf2_cu *cu,
21626 LONGEST *value, const gdb_byte **bytes,
21627 struct dwarf2_locexpr_baton **baton)
21628 {
21629 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21630 struct comp_unit_head *cu_header = &cu->header;
21631 struct dwarf_block *blk;
21632 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21633 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21634
21635 *value = 0;
21636 *bytes = NULL;
21637 *baton = NULL;
21638
21639 switch (attr->form)
21640 {
21641 case DW_FORM_addr:
21642 case DW_FORM_GNU_addr_index:
21643 {
21644 gdb_byte *data;
21645
21646 if (TYPE_LENGTH (type) != cu_header->addr_size)
21647 dwarf2_const_value_length_mismatch_complaint (name,
21648 cu_header->addr_size,
21649 TYPE_LENGTH (type));
21650 /* Symbols of this form are reasonably rare, so we just
21651 piggyback on the existing location code rather than writing
21652 a new implementation of symbol_computed_ops. */
21653 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21654 (*baton)->per_cu = cu->per_cu;
21655 gdb_assert ((*baton)->per_cu);
21656
21657 (*baton)->size = 2 + cu_header->addr_size;
21658 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21659 (*baton)->data = data;
21660
21661 data[0] = DW_OP_addr;
21662 store_unsigned_integer (&data[1], cu_header->addr_size,
21663 byte_order, DW_ADDR (attr));
21664 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21665 }
21666 break;
21667 case DW_FORM_string:
21668 case DW_FORM_strp:
21669 case DW_FORM_GNU_str_index:
21670 case DW_FORM_GNU_strp_alt:
21671 /* DW_STRING is already allocated on the objfile obstack, point
21672 directly to it. */
21673 *bytes = (const gdb_byte *) DW_STRING (attr);
21674 break;
21675 case DW_FORM_block1:
21676 case DW_FORM_block2:
21677 case DW_FORM_block4:
21678 case DW_FORM_block:
21679 case DW_FORM_exprloc:
21680 case DW_FORM_data16:
21681 blk = DW_BLOCK (attr);
21682 if (TYPE_LENGTH (type) != blk->size)
21683 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21684 TYPE_LENGTH (type));
21685 *bytes = blk->data;
21686 break;
21687
21688 /* The DW_AT_const_value attributes are supposed to carry the
21689 symbol's value "represented as it would be on the target
21690 architecture." By the time we get here, it's already been
21691 converted to host endianness, so we just need to sign- or
21692 zero-extend it as appropriate. */
21693 case DW_FORM_data1:
21694 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21695 break;
21696 case DW_FORM_data2:
21697 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21698 break;
21699 case DW_FORM_data4:
21700 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21701 break;
21702 case DW_FORM_data8:
21703 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21704 break;
21705
21706 case DW_FORM_sdata:
21707 case DW_FORM_implicit_const:
21708 *value = DW_SND (attr);
21709 break;
21710
21711 case DW_FORM_udata:
21712 *value = DW_UNSND (attr);
21713 break;
21714
21715 default:
21716 complaint (&symfile_complaints,
21717 _("unsupported const value attribute form: '%s'"),
21718 dwarf_form_name (attr->form));
21719 *value = 0;
21720 break;
21721 }
21722 }
21723
21724
21725 /* Copy constant value from an attribute to a symbol. */
21726
21727 static void
21728 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21729 struct dwarf2_cu *cu)
21730 {
21731 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21732 LONGEST value;
21733 const gdb_byte *bytes;
21734 struct dwarf2_locexpr_baton *baton;
21735
21736 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21737 SYMBOL_PRINT_NAME (sym),
21738 &objfile->objfile_obstack, cu,
21739 &value, &bytes, &baton);
21740
21741 if (baton != NULL)
21742 {
21743 SYMBOL_LOCATION_BATON (sym) = baton;
21744 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21745 }
21746 else if (bytes != NULL)
21747 {
21748 SYMBOL_VALUE_BYTES (sym) = bytes;
21749 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21750 }
21751 else
21752 {
21753 SYMBOL_VALUE (sym) = value;
21754 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21755 }
21756 }
21757
21758 /* Return the type of the die in question using its DW_AT_type attribute. */
21759
21760 static struct type *
21761 die_type (struct die_info *die, struct dwarf2_cu *cu)
21762 {
21763 struct attribute *type_attr;
21764
21765 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21766 if (!type_attr)
21767 {
21768 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21769 /* A missing DW_AT_type represents a void type. */
21770 return objfile_type (objfile)->builtin_void;
21771 }
21772
21773 return lookup_die_type (die, type_attr, cu);
21774 }
21775
21776 /* True iff CU's producer generates GNAT Ada auxiliary information
21777 that allows to find parallel types through that information instead
21778 of having to do expensive parallel lookups by type name. */
21779
21780 static int
21781 need_gnat_info (struct dwarf2_cu *cu)
21782 {
21783 /* Assume that the Ada compiler was GNAT, which always produces
21784 the auxiliary information. */
21785 return (cu->language == language_ada);
21786 }
21787
21788 /* Return the auxiliary type of the die in question using its
21789 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21790 attribute is not present. */
21791
21792 static struct type *
21793 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21794 {
21795 struct attribute *type_attr;
21796
21797 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21798 if (!type_attr)
21799 return NULL;
21800
21801 return lookup_die_type (die, type_attr, cu);
21802 }
21803
21804 /* If DIE has a descriptive_type attribute, then set the TYPE's
21805 descriptive type accordingly. */
21806
21807 static void
21808 set_descriptive_type (struct type *type, struct die_info *die,
21809 struct dwarf2_cu *cu)
21810 {
21811 struct type *descriptive_type = die_descriptive_type (die, cu);
21812
21813 if (descriptive_type)
21814 {
21815 ALLOCATE_GNAT_AUX_TYPE (type);
21816 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21817 }
21818 }
21819
21820 /* Return the containing type of the die in question using its
21821 DW_AT_containing_type attribute. */
21822
21823 static struct type *
21824 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21825 {
21826 struct attribute *type_attr;
21827 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21828
21829 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21830 if (!type_attr)
21831 error (_("Dwarf Error: Problem turning containing type into gdb type "
21832 "[in module %s]"), objfile_name (objfile));
21833
21834 return lookup_die_type (die, type_attr, cu);
21835 }
21836
21837 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21838
21839 static struct type *
21840 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21841 {
21842 struct dwarf2_per_objfile *dwarf2_per_objfile
21843 = cu->per_cu->dwarf2_per_objfile;
21844 struct objfile *objfile = dwarf2_per_objfile->objfile;
21845 char *message, *saved;
21846
21847 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
21848 objfile_name (objfile),
21849 sect_offset_str (cu->header.sect_off),
21850 sect_offset_str (die->sect_off));
21851 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21852 message, strlen (message));
21853 xfree (message);
21854
21855 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21856 }
21857
21858 /* Look up the type of DIE in CU using its type attribute ATTR.
21859 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21860 DW_AT_containing_type.
21861 If there is no type substitute an error marker. */
21862
21863 static struct type *
21864 lookup_die_type (struct die_info *die, const struct attribute *attr,
21865 struct dwarf2_cu *cu)
21866 {
21867 struct dwarf2_per_objfile *dwarf2_per_objfile
21868 = cu->per_cu->dwarf2_per_objfile;
21869 struct objfile *objfile = dwarf2_per_objfile->objfile;
21870 struct type *this_type;
21871
21872 gdb_assert (attr->name == DW_AT_type
21873 || attr->name == DW_AT_GNAT_descriptive_type
21874 || attr->name == DW_AT_containing_type);
21875
21876 /* First see if we have it cached. */
21877
21878 if (attr->form == DW_FORM_GNU_ref_alt)
21879 {
21880 struct dwarf2_per_cu_data *per_cu;
21881 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21882
21883 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21884 dwarf2_per_objfile);
21885 this_type = get_die_type_at_offset (sect_off, per_cu);
21886 }
21887 else if (attr_form_is_ref (attr))
21888 {
21889 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21890
21891 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21892 }
21893 else if (attr->form == DW_FORM_ref_sig8)
21894 {
21895 ULONGEST signature = DW_SIGNATURE (attr);
21896
21897 return get_signatured_type (die, signature, cu);
21898 }
21899 else
21900 {
21901 complaint (&symfile_complaints,
21902 _("Dwarf Error: Bad type attribute %s in DIE"
21903 " at %s [in module %s]"),
21904 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21905 objfile_name (objfile));
21906 return build_error_marker_type (cu, die);
21907 }
21908
21909 /* If not cached we need to read it in. */
21910
21911 if (this_type == NULL)
21912 {
21913 struct die_info *type_die = NULL;
21914 struct dwarf2_cu *type_cu = cu;
21915
21916 if (attr_form_is_ref (attr))
21917 type_die = follow_die_ref (die, attr, &type_cu);
21918 if (type_die == NULL)
21919 return build_error_marker_type (cu, die);
21920 /* If we find the type now, it's probably because the type came
21921 from an inter-CU reference and the type's CU got expanded before
21922 ours. */
21923 this_type = read_type_die (type_die, type_cu);
21924 }
21925
21926 /* If we still don't have a type use an error marker. */
21927
21928 if (this_type == NULL)
21929 return build_error_marker_type (cu, die);
21930
21931 return this_type;
21932 }
21933
21934 /* Return the type in DIE, CU.
21935 Returns NULL for invalid types.
21936
21937 This first does a lookup in die_type_hash,
21938 and only reads the die in if necessary.
21939
21940 NOTE: This can be called when reading in partial or full symbols. */
21941
21942 static struct type *
21943 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21944 {
21945 struct type *this_type;
21946
21947 this_type = get_die_type (die, cu);
21948 if (this_type)
21949 return this_type;
21950
21951 return read_type_die_1 (die, cu);
21952 }
21953
21954 /* Read the type in DIE, CU.
21955 Returns NULL for invalid types. */
21956
21957 static struct type *
21958 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21959 {
21960 struct type *this_type = NULL;
21961
21962 switch (die->tag)
21963 {
21964 case DW_TAG_class_type:
21965 case DW_TAG_interface_type:
21966 case DW_TAG_structure_type:
21967 case DW_TAG_union_type:
21968 this_type = read_structure_type (die, cu);
21969 break;
21970 case DW_TAG_enumeration_type:
21971 this_type = read_enumeration_type (die, cu);
21972 break;
21973 case DW_TAG_subprogram:
21974 case DW_TAG_subroutine_type:
21975 case DW_TAG_inlined_subroutine:
21976 this_type = read_subroutine_type (die, cu);
21977 break;
21978 case DW_TAG_array_type:
21979 this_type = read_array_type (die, cu);
21980 break;
21981 case DW_TAG_set_type:
21982 this_type = read_set_type (die, cu);
21983 break;
21984 case DW_TAG_pointer_type:
21985 this_type = read_tag_pointer_type (die, cu);
21986 break;
21987 case DW_TAG_ptr_to_member_type:
21988 this_type = read_tag_ptr_to_member_type (die, cu);
21989 break;
21990 case DW_TAG_reference_type:
21991 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21992 break;
21993 case DW_TAG_rvalue_reference_type:
21994 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21995 break;
21996 case DW_TAG_const_type:
21997 this_type = read_tag_const_type (die, cu);
21998 break;
21999 case DW_TAG_volatile_type:
22000 this_type = read_tag_volatile_type (die, cu);
22001 break;
22002 case DW_TAG_restrict_type:
22003 this_type = read_tag_restrict_type (die, cu);
22004 break;
22005 case DW_TAG_string_type:
22006 this_type = read_tag_string_type (die, cu);
22007 break;
22008 case DW_TAG_typedef:
22009 this_type = read_typedef (die, cu);
22010 break;
22011 case DW_TAG_subrange_type:
22012 this_type = read_subrange_type (die, cu);
22013 break;
22014 case DW_TAG_base_type:
22015 this_type = read_base_type (die, cu);
22016 break;
22017 case DW_TAG_unspecified_type:
22018 this_type = read_unspecified_type (die, cu);
22019 break;
22020 case DW_TAG_namespace:
22021 this_type = read_namespace_type (die, cu);
22022 break;
22023 case DW_TAG_module:
22024 this_type = read_module_type (die, cu);
22025 break;
22026 case DW_TAG_atomic_type:
22027 this_type = read_tag_atomic_type (die, cu);
22028 break;
22029 default:
22030 complaint (&symfile_complaints,
22031 _("unexpected tag in read_type_die: '%s'"),
22032 dwarf_tag_name (die->tag));
22033 break;
22034 }
22035
22036 return this_type;
22037 }
22038
22039 /* See if we can figure out if the class lives in a namespace. We do
22040 this by looking for a member function; its demangled name will
22041 contain namespace info, if there is any.
22042 Return the computed name or NULL.
22043 Space for the result is allocated on the objfile's obstack.
22044 This is the full-die version of guess_partial_die_structure_name.
22045 In this case we know DIE has no useful parent. */
22046
22047 static char *
22048 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22049 {
22050 struct die_info *spec_die;
22051 struct dwarf2_cu *spec_cu;
22052 struct die_info *child;
22053 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22054
22055 spec_cu = cu;
22056 spec_die = die_specification (die, &spec_cu);
22057 if (spec_die != NULL)
22058 {
22059 die = spec_die;
22060 cu = spec_cu;
22061 }
22062
22063 for (child = die->child;
22064 child != NULL;
22065 child = child->sibling)
22066 {
22067 if (child->tag == DW_TAG_subprogram)
22068 {
22069 const char *linkage_name = dw2_linkage_name (child, cu);
22070
22071 if (linkage_name != NULL)
22072 {
22073 char *actual_name
22074 = language_class_name_from_physname (cu->language_defn,
22075 linkage_name);
22076 char *name = NULL;
22077
22078 if (actual_name != NULL)
22079 {
22080 const char *die_name = dwarf2_name (die, cu);
22081
22082 if (die_name != NULL
22083 && strcmp (die_name, actual_name) != 0)
22084 {
22085 /* Strip off the class name from the full name.
22086 We want the prefix. */
22087 int die_name_len = strlen (die_name);
22088 int actual_name_len = strlen (actual_name);
22089
22090 /* Test for '::' as a sanity check. */
22091 if (actual_name_len > die_name_len + 2
22092 && actual_name[actual_name_len
22093 - die_name_len - 1] == ':')
22094 name = (char *) obstack_copy0 (
22095 &objfile->per_bfd->storage_obstack,
22096 actual_name, actual_name_len - die_name_len - 2);
22097 }
22098 }
22099 xfree (actual_name);
22100 return name;
22101 }
22102 }
22103 }
22104
22105 return NULL;
22106 }
22107
22108 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22109 prefix part in such case. See
22110 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22111
22112 static const char *
22113 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22114 {
22115 struct attribute *attr;
22116 const char *base;
22117
22118 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22119 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22120 return NULL;
22121
22122 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22123 return NULL;
22124
22125 attr = dw2_linkage_name_attr (die, cu);
22126 if (attr == NULL || DW_STRING (attr) == NULL)
22127 return NULL;
22128
22129 /* dwarf2_name had to be already called. */
22130 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22131
22132 /* Strip the base name, keep any leading namespaces/classes. */
22133 base = strrchr (DW_STRING (attr), ':');
22134 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22135 return "";
22136
22137 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22138 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22139 DW_STRING (attr),
22140 &base[-1] - DW_STRING (attr));
22141 }
22142
22143 /* Return the name of the namespace/class that DIE is defined within,
22144 or "" if we can't tell. The caller should not xfree the result.
22145
22146 For example, if we're within the method foo() in the following
22147 code:
22148
22149 namespace N {
22150 class C {
22151 void foo () {
22152 }
22153 };
22154 }
22155
22156 then determine_prefix on foo's die will return "N::C". */
22157
22158 static const char *
22159 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22160 {
22161 struct dwarf2_per_objfile *dwarf2_per_objfile
22162 = cu->per_cu->dwarf2_per_objfile;
22163 struct die_info *parent, *spec_die;
22164 struct dwarf2_cu *spec_cu;
22165 struct type *parent_type;
22166 const char *retval;
22167
22168 if (cu->language != language_cplus
22169 && cu->language != language_fortran && cu->language != language_d
22170 && cu->language != language_rust)
22171 return "";
22172
22173 retval = anonymous_struct_prefix (die, cu);
22174 if (retval)
22175 return retval;
22176
22177 /* We have to be careful in the presence of DW_AT_specification.
22178 For example, with GCC 3.4, given the code
22179
22180 namespace N {
22181 void foo() {
22182 // Definition of N::foo.
22183 }
22184 }
22185
22186 then we'll have a tree of DIEs like this:
22187
22188 1: DW_TAG_compile_unit
22189 2: DW_TAG_namespace // N
22190 3: DW_TAG_subprogram // declaration of N::foo
22191 4: DW_TAG_subprogram // definition of N::foo
22192 DW_AT_specification // refers to die #3
22193
22194 Thus, when processing die #4, we have to pretend that we're in
22195 the context of its DW_AT_specification, namely the contex of die
22196 #3. */
22197 spec_cu = cu;
22198 spec_die = die_specification (die, &spec_cu);
22199 if (spec_die == NULL)
22200 parent = die->parent;
22201 else
22202 {
22203 parent = spec_die->parent;
22204 cu = spec_cu;
22205 }
22206
22207 if (parent == NULL)
22208 return "";
22209 else if (parent->building_fullname)
22210 {
22211 const char *name;
22212 const char *parent_name;
22213
22214 /* It has been seen on RealView 2.2 built binaries,
22215 DW_TAG_template_type_param types actually _defined_ as
22216 children of the parent class:
22217
22218 enum E {};
22219 template class <class Enum> Class{};
22220 Class<enum E> class_e;
22221
22222 1: DW_TAG_class_type (Class)
22223 2: DW_TAG_enumeration_type (E)
22224 3: DW_TAG_enumerator (enum1:0)
22225 3: DW_TAG_enumerator (enum2:1)
22226 ...
22227 2: DW_TAG_template_type_param
22228 DW_AT_type DW_FORM_ref_udata (E)
22229
22230 Besides being broken debug info, it can put GDB into an
22231 infinite loop. Consider:
22232
22233 When we're building the full name for Class<E>, we'll start
22234 at Class, and go look over its template type parameters,
22235 finding E. We'll then try to build the full name of E, and
22236 reach here. We're now trying to build the full name of E,
22237 and look over the parent DIE for containing scope. In the
22238 broken case, if we followed the parent DIE of E, we'd again
22239 find Class, and once again go look at its template type
22240 arguments, etc., etc. Simply don't consider such parent die
22241 as source-level parent of this die (it can't be, the language
22242 doesn't allow it), and break the loop here. */
22243 name = dwarf2_name (die, cu);
22244 parent_name = dwarf2_name (parent, cu);
22245 complaint (&symfile_complaints,
22246 _("template param type '%s' defined within parent '%s'"),
22247 name ? name : "<unknown>",
22248 parent_name ? parent_name : "<unknown>");
22249 return "";
22250 }
22251 else
22252 switch (parent->tag)
22253 {
22254 case DW_TAG_namespace:
22255 parent_type = read_type_die (parent, cu);
22256 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22257 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22258 Work around this problem here. */
22259 if (cu->language == language_cplus
22260 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22261 return "";
22262 /* We give a name to even anonymous namespaces. */
22263 return TYPE_TAG_NAME (parent_type);
22264 case DW_TAG_class_type:
22265 case DW_TAG_interface_type:
22266 case DW_TAG_structure_type:
22267 case DW_TAG_union_type:
22268 case DW_TAG_module:
22269 parent_type = read_type_die (parent, cu);
22270 if (TYPE_TAG_NAME (parent_type) != NULL)
22271 return TYPE_TAG_NAME (parent_type);
22272 else
22273 /* An anonymous structure is only allowed non-static data
22274 members; no typedefs, no member functions, et cetera.
22275 So it does not need a prefix. */
22276 return "";
22277 case DW_TAG_compile_unit:
22278 case DW_TAG_partial_unit:
22279 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22280 if (cu->language == language_cplus
22281 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22282 && die->child != NULL
22283 && (die->tag == DW_TAG_class_type
22284 || die->tag == DW_TAG_structure_type
22285 || die->tag == DW_TAG_union_type))
22286 {
22287 char *name = guess_full_die_structure_name (die, cu);
22288 if (name != NULL)
22289 return name;
22290 }
22291 return "";
22292 case DW_TAG_enumeration_type:
22293 parent_type = read_type_die (parent, cu);
22294 if (TYPE_DECLARED_CLASS (parent_type))
22295 {
22296 if (TYPE_TAG_NAME (parent_type) != NULL)
22297 return TYPE_TAG_NAME (parent_type);
22298 return "";
22299 }
22300 /* Fall through. */
22301 default:
22302 return determine_prefix (parent, cu);
22303 }
22304 }
22305
22306 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22307 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22308 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22309 an obconcat, otherwise allocate storage for the result. The CU argument is
22310 used to determine the language and hence, the appropriate separator. */
22311
22312 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22313
22314 static char *
22315 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22316 int physname, struct dwarf2_cu *cu)
22317 {
22318 const char *lead = "";
22319 const char *sep;
22320
22321 if (suffix == NULL || suffix[0] == '\0'
22322 || prefix == NULL || prefix[0] == '\0')
22323 sep = "";
22324 else if (cu->language == language_d)
22325 {
22326 /* For D, the 'main' function could be defined in any module, but it
22327 should never be prefixed. */
22328 if (strcmp (suffix, "D main") == 0)
22329 {
22330 prefix = "";
22331 sep = "";
22332 }
22333 else
22334 sep = ".";
22335 }
22336 else if (cu->language == language_fortran && physname)
22337 {
22338 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22339 DW_AT_MIPS_linkage_name is preferred and used instead. */
22340
22341 lead = "__";
22342 sep = "_MOD_";
22343 }
22344 else
22345 sep = "::";
22346
22347 if (prefix == NULL)
22348 prefix = "";
22349 if (suffix == NULL)
22350 suffix = "";
22351
22352 if (obs == NULL)
22353 {
22354 char *retval
22355 = ((char *)
22356 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22357
22358 strcpy (retval, lead);
22359 strcat (retval, prefix);
22360 strcat (retval, sep);
22361 strcat (retval, suffix);
22362 return retval;
22363 }
22364 else
22365 {
22366 /* We have an obstack. */
22367 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22368 }
22369 }
22370
22371 /* Return sibling of die, NULL if no sibling. */
22372
22373 static struct die_info *
22374 sibling_die (struct die_info *die)
22375 {
22376 return die->sibling;
22377 }
22378
22379 /* Get name of a die, return NULL if not found. */
22380
22381 static const char *
22382 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22383 struct obstack *obstack)
22384 {
22385 if (name && cu->language == language_cplus)
22386 {
22387 std::string canon_name = cp_canonicalize_string (name);
22388
22389 if (!canon_name.empty ())
22390 {
22391 if (canon_name != name)
22392 name = (const char *) obstack_copy0 (obstack,
22393 canon_name.c_str (),
22394 canon_name.length ());
22395 }
22396 }
22397
22398 return name;
22399 }
22400
22401 /* Get name of a die, return NULL if not found.
22402 Anonymous namespaces are converted to their magic string. */
22403
22404 static const char *
22405 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22406 {
22407 struct attribute *attr;
22408 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22409
22410 attr = dwarf2_attr (die, DW_AT_name, cu);
22411 if ((!attr || !DW_STRING (attr))
22412 && die->tag != DW_TAG_namespace
22413 && die->tag != DW_TAG_class_type
22414 && die->tag != DW_TAG_interface_type
22415 && die->tag != DW_TAG_structure_type
22416 && die->tag != DW_TAG_union_type)
22417 return NULL;
22418
22419 switch (die->tag)
22420 {
22421 case DW_TAG_compile_unit:
22422 case DW_TAG_partial_unit:
22423 /* Compilation units have a DW_AT_name that is a filename, not
22424 a source language identifier. */
22425 case DW_TAG_enumeration_type:
22426 case DW_TAG_enumerator:
22427 /* These tags always have simple identifiers already; no need
22428 to canonicalize them. */
22429 return DW_STRING (attr);
22430
22431 case DW_TAG_namespace:
22432 if (attr != NULL && DW_STRING (attr) != NULL)
22433 return DW_STRING (attr);
22434 return CP_ANONYMOUS_NAMESPACE_STR;
22435
22436 case DW_TAG_class_type:
22437 case DW_TAG_interface_type:
22438 case DW_TAG_structure_type:
22439 case DW_TAG_union_type:
22440 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22441 structures or unions. These were of the form "._%d" in GCC 4.1,
22442 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22443 and GCC 4.4. We work around this problem by ignoring these. */
22444 if (attr && DW_STRING (attr)
22445 && (startswith (DW_STRING (attr), "._")
22446 || startswith (DW_STRING (attr), "<anonymous")))
22447 return NULL;
22448
22449 /* GCC might emit a nameless typedef that has a linkage name. See
22450 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22451 if (!attr || DW_STRING (attr) == NULL)
22452 {
22453 char *demangled = NULL;
22454
22455 attr = dw2_linkage_name_attr (die, cu);
22456 if (attr == NULL || DW_STRING (attr) == NULL)
22457 return NULL;
22458
22459 /* Avoid demangling DW_STRING (attr) the second time on a second
22460 call for the same DIE. */
22461 if (!DW_STRING_IS_CANONICAL (attr))
22462 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22463
22464 if (demangled)
22465 {
22466 const char *base;
22467
22468 /* FIXME: we already did this for the partial symbol... */
22469 DW_STRING (attr)
22470 = ((const char *)
22471 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22472 demangled, strlen (demangled)));
22473 DW_STRING_IS_CANONICAL (attr) = 1;
22474 xfree (demangled);
22475
22476 /* Strip any leading namespaces/classes, keep only the base name.
22477 DW_AT_name for named DIEs does not contain the prefixes. */
22478 base = strrchr (DW_STRING (attr), ':');
22479 if (base && base > DW_STRING (attr) && base[-1] == ':')
22480 return &base[1];
22481 else
22482 return DW_STRING (attr);
22483 }
22484 }
22485 break;
22486
22487 default:
22488 break;
22489 }
22490
22491 if (!DW_STRING_IS_CANONICAL (attr))
22492 {
22493 DW_STRING (attr)
22494 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22495 &objfile->per_bfd->storage_obstack);
22496 DW_STRING_IS_CANONICAL (attr) = 1;
22497 }
22498 return DW_STRING (attr);
22499 }
22500
22501 /* Return the die that this die in an extension of, or NULL if there
22502 is none. *EXT_CU is the CU containing DIE on input, and the CU
22503 containing the return value on output. */
22504
22505 static struct die_info *
22506 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22507 {
22508 struct attribute *attr;
22509
22510 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22511 if (attr == NULL)
22512 return NULL;
22513
22514 return follow_die_ref (die, attr, ext_cu);
22515 }
22516
22517 /* Convert a DIE tag into its string name. */
22518
22519 static const char *
22520 dwarf_tag_name (unsigned tag)
22521 {
22522 const char *name = get_DW_TAG_name (tag);
22523
22524 if (name == NULL)
22525 return "DW_TAG_<unknown>";
22526
22527 return name;
22528 }
22529
22530 /* Convert a DWARF attribute code into its string name. */
22531
22532 static const char *
22533 dwarf_attr_name (unsigned attr)
22534 {
22535 const char *name;
22536
22537 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22538 if (attr == DW_AT_MIPS_fde)
22539 return "DW_AT_MIPS_fde";
22540 #else
22541 if (attr == DW_AT_HP_block_index)
22542 return "DW_AT_HP_block_index";
22543 #endif
22544
22545 name = get_DW_AT_name (attr);
22546
22547 if (name == NULL)
22548 return "DW_AT_<unknown>";
22549
22550 return name;
22551 }
22552
22553 /* Convert a DWARF value form code into its string name. */
22554
22555 static const char *
22556 dwarf_form_name (unsigned form)
22557 {
22558 const char *name = get_DW_FORM_name (form);
22559
22560 if (name == NULL)
22561 return "DW_FORM_<unknown>";
22562
22563 return name;
22564 }
22565
22566 static const char *
22567 dwarf_bool_name (unsigned mybool)
22568 {
22569 if (mybool)
22570 return "TRUE";
22571 else
22572 return "FALSE";
22573 }
22574
22575 /* Convert a DWARF type code into its string name. */
22576
22577 static const char *
22578 dwarf_type_encoding_name (unsigned enc)
22579 {
22580 const char *name = get_DW_ATE_name (enc);
22581
22582 if (name == NULL)
22583 return "DW_ATE_<unknown>";
22584
22585 return name;
22586 }
22587
22588 static void
22589 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22590 {
22591 unsigned int i;
22592
22593 print_spaces (indent, f);
22594 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22595 dwarf_tag_name (die->tag), die->abbrev,
22596 sect_offset_str (die->sect_off));
22597
22598 if (die->parent != NULL)
22599 {
22600 print_spaces (indent, f);
22601 fprintf_unfiltered (f, " parent at offset: %s\n",
22602 sect_offset_str (die->parent->sect_off));
22603 }
22604
22605 print_spaces (indent, f);
22606 fprintf_unfiltered (f, " has children: %s\n",
22607 dwarf_bool_name (die->child != NULL));
22608
22609 print_spaces (indent, f);
22610 fprintf_unfiltered (f, " attributes:\n");
22611
22612 for (i = 0; i < die->num_attrs; ++i)
22613 {
22614 print_spaces (indent, f);
22615 fprintf_unfiltered (f, " %s (%s) ",
22616 dwarf_attr_name (die->attrs[i].name),
22617 dwarf_form_name (die->attrs[i].form));
22618
22619 switch (die->attrs[i].form)
22620 {
22621 case DW_FORM_addr:
22622 case DW_FORM_GNU_addr_index:
22623 fprintf_unfiltered (f, "address: ");
22624 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22625 break;
22626 case DW_FORM_block2:
22627 case DW_FORM_block4:
22628 case DW_FORM_block:
22629 case DW_FORM_block1:
22630 fprintf_unfiltered (f, "block: size %s",
22631 pulongest (DW_BLOCK (&die->attrs[i])->size));
22632 break;
22633 case DW_FORM_exprloc:
22634 fprintf_unfiltered (f, "expression: size %s",
22635 pulongest (DW_BLOCK (&die->attrs[i])->size));
22636 break;
22637 case DW_FORM_data16:
22638 fprintf_unfiltered (f, "constant of 16 bytes");
22639 break;
22640 case DW_FORM_ref_addr:
22641 fprintf_unfiltered (f, "ref address: ");
22642 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22643 break;
22644 case DW_FORM_GNU_ref_alt:
22645 fprintf_unfiltered (f, "alt ref address: ");
22646 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22647 break;
22648 case DW_FORM_ref1:
22649 case DW_FORM_ref2:
22650 case DW_FORM_ref4:
22651 case DW_FORM_ref8:
22652 case DW_FORM_ref_udata:
22653 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22654 (long) (DW_UNSND (&die->attrs[i])));
22655 break;
22656 case DW_FORM_data1:
22657 case DW_FORM_data2:
22658 case DW_FORM_data4:
22659 case DW_FORM_data8:
22660 case DW_FORM_udata:
22661 case DW_FORM_sdata:
22662 fprintf_unfiltered (f, "constant: %s",
22663 pulongest (DW_UNSND (&die->attrs[i])));
22664 break;
22665 case DW_FORM_sec_offset:
22666 fprintf_unfiltered (f, "section offset: %s",
22667 pulongest (DW_UNSND (&die->attrs[i])));
22668 break;
22669 case DW_FORM_ref_sig8:
22670 fprintf_unfiltered (f, "signature: %s",
22671 hex_string (DW_SIGNATURE (&die->attrs[i])));
22672 break;
22673 case DW_FORM_string:
22674 case DW_FORM_strp:
22675 case DW_FORM_line_strp:
22676 case DW_FORM_GNU_str_index:
22677 case DW_FORM_GNU_strp_alt:
22678 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22679 DW_STRING (&die->attrs[i])
22680 ? DW_STRING (&die->attrs[i]) : "",
22681 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22682 break;
22683 case DW_FORM_flag:
22684 if (DW_UNSND (&die->attrs[i]))
22685 fprintf_unfiltered (f, "flag: TRUE");
22686 else
22687 fprintf_unfiltered (f, "flag: FALSE");
22688 break;
22689 case DW_FORM_flag_present:
22690 fprintf_unfiltered (f, "flag: TRUE");
22691 break;
22692 case DW_FORM_indirect:
22693 /* The reader will have reduced the indirect form to
22694 the "base form" so this form should not occur. */
22695 fprintf_unfiltered (f,
22696 "unexpected attribute form: DW_FORM_indirect");
22697 break;
22698 case DW_FORM_implicit_const:
22699 fprintf_unfiltered (f, "constant: %s",
22700 plongest (DW_SND (&die->attrs[i])));
22701 break;
22702 default:
22703 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22704 die->attrs[i].form);
22705 break;
22706 }
22707 fprintf_unfiltered (f, "\n");
22708 }
22709 }
22710
22711 static void
22712 dump_die_for_error (struct die_info *die)
22713 {
22714 dump_die_shallow (gdb_stderr, 0, die);
22715 }
22716
22717 static void
22718 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22719 {
22720 int indent = level * 4;
22721
22722 gdb_assert (die != NULL);
22723
22724 if (level >= max_level)
22725 return;
22726
22727 dump_die_shallow (f, indent, die);
22728
22729 if (die->child != NULL)
22730 {
22731 print_spaces (indent, f);
22732 fprintf_unfiltered (f, " Children:");
22733 if (level + 1 < max_level)
22734 {
22735 fprintf_unfiltered (f, "\n");
22736 dump_die_1 (f, level + 1, max_level, die->child);
22737 }
22738 else
22739 {
22740 fprintf_unfiltered (f,
22741 " [not printed, max nesting level reached]\n");
22742 }
22743 }
22744
22745 if (die->sibling != NULL && level > 0)
22746 {
22747 dump_die_1 (f, level, max_level, die->sibling);
22748 }
22749 }
22750
22751 /* This is called from the pdie macro in gdbinit.in.
22752 It's not static so gcc will keep a copy callable from gdb. */
22753
22754 void
22755 dump_die (struct die_info *die, int max_level)
22756 {
22757 dump_die_1 (gdb_stdlog, 0, max_level, die);
22758 }
22759
22760 static void
22761 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22762 {
22763 void **slot;
22764
22765 slot = htab_find_slot_with_hash (cu->die_hash, die,
22766 to_underlying (die->sect_off),
22767 INSERT);
22768
22769 *slot = die;
22770 }
22771
22772 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22773 required kind. */
22774
22775 static sect_offset
22776 dwarf2_get_ref_die_offset (const struct attribute *attr)
22777 {
22778 if (attr_form_is_ref (attr))
22779 return (sect_offset) DW_UNSND (attr);
22780
22781 complaint (&symfile_complaints,
22782 _("unsupported die ref attribute form: '%s'"),
22783 dwarf_form_name (attr->form));
22784 return {};
22785 }
22786
22787 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22788 * the value held by the attribute is not constant. */
22789
22790 static LONGEST
22791 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22792 {
22793 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22794 return DW_SND (attr);
22795 else if (attr->form == DW_FORM_udata
22796 || attr->form == DW_FORM_data1
22797 || attr->form == DW_FORM_data2
22798 || attr->form == DW_FORM_data4
22799 || attr->form == DW_FORM_data8)
22800 return DW_UNSND (attr);
22801 else
22802 {
22803 /* For DW_FORM_data16 see attr_form_is_constant. */
22804 complaint (&symfile_complaints,
22805 _("Attribute value is not a constant (%s)"),
22806 dwarf_form_name (attr->form));
22807 return default_value;
22808 }
22809 }
22810
22811 /* Follow reference or signature attribute ATTR of SRC_DIE.
22812 On entry *REF_CU is the CU of SRC_DIE.
22813 On exit *REF_CU is the CU of the result. */
22814
22815 static struct die_info *
22816 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22817 struct dwarf2_cu **ref_cu)
22818 {
22819 struct die_info *die;
22820
22821 if (attr_form_is_ref (attr))
22822 die = follow_die_ref (src_die, attr, ref_cu);
22823 else if (attr->form == DW_FORM_ref_sig8)
22824 die = follow_die_sig (src_die, attr, ref_cu);
22825 else
22826 {
22827 dump_die_for_error (src_die);
22828 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22829 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22830 }
22831
22832 return die;
22833 }
22834
22835 /* Follow reference OFFSET.
22836 On entry *REF_CU is the CU of the source die referencing OFFSET.
22837 On exit *REF_CU is the CU of the result.
22838 Returns NULL if OFFSET is invalid. */
22839
22840 static struct die_info *
22841 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22842 struct dwarf2_cu **ref_cu)
22843 {
22844 struct die_info temp_die;
22845 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22846 struct dwarf2_per_objfile *dwarf2_per_objfile
22847 = cu->per_cu->dwarf2_per_objfile;
22848
22849 gdb_assert (cu->per_cu != NULL);
22850
22851 target_cu = cu;
22852
22853 if (cu->per_cu->is_debug_types)
22854 {
22855 /* .debug_types CUs cannot reference anything outside their CU.
22856 If they need to, they have to reference a signatured type via
22857 DW_FORM_ref_sig8. */
22858 if (!offset_in_cu_p (&cu->header, sect_off))
22859 return NULL;
22860 }
22861 else if (offset_in_dwz != cu->per_cu->is_dwz
22862 || !offset_in_cu_p (&cu->header, sect_off))
22863 {
22864 struct dwarf2_per_cu_data *per_cu;
22865
22866 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22867 dwarf2_per_objfile);
22868
22869 /* If necessary, add it to the queue and load its DIEs. */
22870 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22871 load_full_comp_unit (per_cu, cu->language);
22872
22873 target_cu = per_cu->cu;
22874 }
22875 else if (cu->dies == NULL)
22876 {
22877 /* We're loading full DIEs during partial symbol reading. */
22878 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22879 load_full_comp_unit (cu->per_cu, language_minimal);
22880 }
22881
22882 *ref_cu = target_cu;
22883 temp_die.sect_off = sect_off;
22884 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22885 &temp_die,
22886 to_underlying (sect_off));
22887 }
22888
22889 /* Follow reference attribute ATTR of SRC_DIE.
22890 On entry *REF_CU is the CU of SRC_DIE.
22891 On exit *REF_CU is the CU of the result. */
22892
22893 static struct die_info *
22894 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22895 struct dwarf2_cu **ref_cu)
22896 {
22897 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22898 struct dwarf2_cu *cu = *ref_cu;
22899 struct die_info *die;
22900
22901 die = follow_die_offset (sect_off,
22902 (attr->form == DW_FORM_GNU_ref_alt
22903 || cu->per_cu->is_dwz),
22904 ref_cu);
22905 if (!die)
22906 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22907 "at %s [in module %s]"),
22908 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22909 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22910
22911 return die;
22912 }
22913
22914 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22915 Returned value is intended for DW_OP_call*. Returned
22916 dwarf2_locexpr_baton->data has lifetime of
22917 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22918
22919 struct dwarf2_locexpr_baton
22920 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22921 struct dwarf2_per_cu_data *per_cu,
22922 CORE_ADDR (*get_frame_pc) (void *baton),
22923 void *baton)
22924 {
22925 struct dwarf2_cu *cu;
22926 struct die_info *die;
22927 struct attribute *attr;
22928 struct dwarf2_locexpr_baton retval;
22929 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22930 struct dwarf2_per_objfile *dwarf2_per_objfile
22931 = get_dwarf2_per_objfile (objfile);
22932
22933 if (per_cu->cu == NULL)
22934 load_cu (per_cu);
22935 cu = per_cu->cu;
22936 if (cu == NULL)
22937 {
22938 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22939 Instead just throw an error, not much else we can do. */
22940 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22941 sect_offset_str (sect_off), objfile_name (objfile));
22942 }
22943
22944 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22945 if (!die)
22946 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22947 sect_offset_str (sect_off), objfile_name (objfile));
22948
22949 attr = dwarf2_attr (die, DW_AT_location, cu);
22950 if (!attr)
22951 {
22952 /* DWARF: "If there is no such attribute, then there is no effect.".
22953 DATA is ignored if SIZE is 0. */
22954
22955 retval.data = NULL;
22956 retval.size = 0;
22957 }
22958 else if (attr_form_is_section_offset (attr))
22959 {
22960 struct dwarf2_loclist_baton loclist_baton;
22961 CORE_ADDR pc = (*get_frame_pc) (baton);
22962 size_t size;
22963
22964 fill_in_loclist_baton (cu, &loclist_baton, attr);
22965
22966 retval.data = dwarf2_find_location_expression (&loclist_baton,
22967 &size, pc);
22968 retval.size = size;
22969 }
22970 else
22971 {
22972 if (!attr_form_is_block (attr))
22973 error (_("Dwarf Error: DIE at %s referenced in module %s "
22974 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22975 sect_offset_str (sect_off), objfile_name (objfile));
22976
22977 retval.data = DW_BLOCK (attr)->data;
22978 retval.size = DW_BLOCK (attr)->size;
22979 }
22980 retval.per_cu = cu->per_cu;
22981
22982 age_cached_comp_units (dwarf2_per_objfile);
22983
22984 return retval;
22985 }
22986
22987 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22988 offset. */
22989
22990 struct dwarf2_locexpr_baton
22991 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22992 struct dwarf2_per_cu_data *per_cu,
22993 CORE_ADDR (*get_frame_pc) (void *baton),
22994 void *baton)
22995 {
22996 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22997
22998 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22999 }
23000
23001 /* Write a constant of a given type as target-ordered bytes into
23002 OBSTACK. */
23003
23004 static const gdb_byte *
23005 write_constant_as_bytes (struct obstack *obstack,
23006 enum bfd_endian byte_order,
23007 struct type *type,
23008 ULONGEST value,
23009 LONGEST *len)
23010 {
23011 gdb_byte *result;
23012
23013 *len = TYPE_LENGTH (type);
23014 result = (gdb_byte *) obstack_alloc (obstack, *len);
23015 store_unsigned_integer (result, *len, byte_order, value);
23016
23017 return result;
23018 }
23019
23020 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23021 pointer to the constant bytes and set LEN to the length of the
23022 data. If memory is needed, allocate it on OBSTACK. If the DIE
23023 does not have a DW_AT_const_value, return NULL. */
23024
23025 const gdb_byte *
23026 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23027 struct dwarf2_per_cu_data *per_cu,
23028 struct obstack *obstack,
23029 LONGEST *len)
23030 {
23031 struct dwarf2_cu *cu;
23032 struct die_info *die;
23033 struct attribute *attr;
23034 const gdb_byte *result = NULL;
23035 struct type *type;
23036 LONGEST value;
23037 enum bfd_endian byte_order;
23038 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23039
23040 if (per_cu->cu == NULL)
23041 load_cu (per_cu);
23042 cu = per_cu->cu;
23043 if (cu == NULL)
23044 {
23045 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23046 Instead just throw an error, not much else we can do. */
23047 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23048 sect_offset_str (sect_off), objfile_name (objfile));
23049 }
23050
23051 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23052 if (!die)
23053 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23054 sect_offset_str (sect_off), objfile_name (objfile));
23055
23056 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23057 if (attr == NULL)
23058 return NULL;
23059
23060 byte_order = (bfd_big_endian (objfile->obfd)
23061 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23062
23063 switch (attr->form)
23064 {
23065 case DW_FORM_addr:
23066 case DW_FORM_GNU_addr_index:
23067 {
23068 gdb_byte *tem;
23069
23070 *len = cu->header.addr_size;
23071 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23072 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23073 result = tem;
23074 }
23075 break;
23076 case DW_FORM_string:
23077 case DW_FORM_strp:
23078 case DW_FORM_GNU_str_index:
23079 case DW_FORM_GNU_strp_alt:
23080 /* DW_STRING is already allocated on the objfile obstack, point
23081 directly to it. */
23082 result = (const gdb_byte *) DW_STRING (attr);
23083 *len = strlen (DW_STRING (attr));
23084 break;
23085 case DW_FORM_block1:
23086 case DW_FORM_block2:
23087 case DW_FORM_block4:
23088 case DW_FORM_block:
23089 case DW_FORM_exprloc:
23090 case DW_FORM_data16:
23091 result = DW_BLOCK (attr)->data;
23092 *len = DW_BLOCK (attr)->size;
23093 break;
23094
23095 /* The DW_AT_const_value attributes are supposed to carry the
23096 symbol's value "represented as it would be on the target
23097 architecture." By the time we get here, it's already been
23098 converted to host endianness, so we just need to sign- or
23099 zero-extend it as appropriate. */
23100 case DW_FORM_data1:
23101 type = die_type (die, cu);
23102 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23103 if (result == NULL)
23104 result = write_constant_as_bytes (obstack, byte_order,
23105 type, value, len);
23106 break;
23107 case DW_FORM_data2:
23108 type = die_type (die, cu);
23109 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23110 if (result == NULL)
23111 result = write_constant_as_bytes (obstack, byte_order,
23112 type, value, len);
23113 break;
23114 case DW_FORM_data4:
23115 type = die_type (die, cu);
23116 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23117 if (result == NULL)
23118 result = write_constant_as_bytes (obstack, byte_order,
23119 type, value, len);
23120 break;
23121 case DW_FORM_data8:
23122 type = die_type (die, cu);
23123 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23124 if (result == NULL)
23125 result = write_constant_as_bytes (obstack, byte_order,
23126 type, value, len);
23127 break;
23128
23129 case DW_FORM_sdata:
23130 case DW_FORM_implicit_const:
23131 type = die_type (die, cu);
23132 result = write_constant_as_bytes (obstack, byte_order,
23133 type, DW_SND (attr), len);
23134 break;
23135
23136 case DW_FORM_udata:
23137 type = die_type (die, cu);
23138 result = write_constant_as_bytes (obstack, byte_order,
23139 type, DW_UNSND (attr), len);
23140 break;
23141
23142 default:
23143 complaint (&symfile_complaints,
23144 _("unsupported const value attribute form: '%s'"),
23145 dwarf_form_name (attr->form));
23146 break;
23147 }
23148
23149 return result;
23150 }
23151
23152 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23153 valid type for this die is found. */
23154
23155 struct type *
23156 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23157 struct dwarf2_per_cu_data *per_cu)
23158 {
23159 struct dwarf2_cu *cu;
23160 struct die_info *die;
23161
23162 if (per_cu->cu == NULL)
23163 load_cu (per_cu);
23164 cu = per_cu->cu;
23165 if (!cu)
23166 return NULL;
23167
23168 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23169 if (!die)
23170 return NULL;
23171
23172 return die_type (die, cu);
23173 }
23174
23175 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23176 PER_CU. */
23177
23178 struct type *
23179 dwarf2_get_die_type (cu_offset die_offset,
23180 struct dwarf2_per_cu_data *per_cu)
23181 {
23182 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23183 return get_die_type_at_offset (die_offset_sect, per_cu);
23184 }
23185
23186 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23187 On entry *REF_CU is the CU of SRC_DIE.
23188 On exit *REF_CU is the CU of the result.
23189 Returns NULL if the referenced DIE isn't found. */
23190
23191 static struct die_info *
23192 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23193 struct dwarf2_cu **ref_cu)
23194 {
23195 struct die_info temp_die;
23196 struct dwarf2_cu *sig_cu;
23197 struct die_info *die;
23198
23199 /* While it might be nice to assert sig_type->type == NULL here,
23200 we can get here for DW_AT_imported_declaration where we need
23201 the DIE not the type. */
23202
23203 /* If necessary, add it to the queue and load its DIEs. */
23204
23205 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23206 read_signatured_type (sig_type);
23207
23208 sig_cu = sig_type->per_cu.cu;
23209 gdb_assert (sig_cu != NULL);
23210 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23211 temp_die.sect_off = sig_type->type_offset_in_section;
23212 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23213 to_underlying (temp_die.sect_off));
23214 if (die)
23215 {
23216 struct dwarf2_per_objfile *dwarf2_per_objfile
23217 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23218
23219 /* For .gdb_index version 7 keep track of included TUs.
23220 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23221 if (dwarf2_per_objfile->index_table != NULL
23222 && dwarf2_per_objfile->index_table->version <= 7)
23223 {
23224 VEC_safe_push (dwarf2_per_cu_ptr,
23225 (*ref_cu)->per_cu->imported_symtabs,
23226 sig_cu->per_cu);
23227 }
23228
23229 *ref_cu = sig_cu;
23230 return die;
23231 }
23232
23233 return NULL;
23234 }
23235
23236 /* Follow signatured type referenced by ATTR in SRC_DIE.
23237 On entry *REF_CU is the CU of SRC_DIE.
23238 On exit *REF_CU is the CU of the result.
23239 The result is the DIE of the type.
23240 If the referenced type cannot be found an error is thrown. */
23241
23242 static struct die_info *
23243 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23244 struct dwarf2_cu **ref_cu)
23245 {
23246 ULONGEST signature = DW_SIGNATURE (attr);
23247 struct signatured_type *sig_type;
23248 struct die_info *die;
23249
23250 gdb_assert (attr->form == DW_FORM_ref_sig8);
23251
23252 sig_type = lookup_signatured_type (*ref_cu, signature);
23253 /* sig_type will be NULL if the signatured type is missing from
23254 the debug info. */
23255 if (sig_type == NULL)
23256 {
23257 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23258 " from DIE at %s [in module %s]"),
23259 hex_string (signature), sect_offset_str (src_die->sect_off),
23260 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23261 }
23262
23263 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23264 if (die == NULL)
23265 {
23266 dump_die_for_error (src_die);
23267 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23268 " from DIE at %s [in module %s]"),
23269 hex_string (signature), sect_offset_str (src_die->sect_off),
23270 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23271 }
23272
23273 return die;
23274 }
23275
23276 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23277 reading in and processing the type unit if necessary. */
23278
23279 static struct type *
23280 get_signatured_type (struct die_info *die, ULONGEST signature,
23281 struct dwarf2_cu *cu)
23282 {
23283 struct dwarf2_per_objfile *dwarf2_per_objfile
23284 = cu->per_cu->dwarf2_per_objfile;
23285 struct signatured_type *sig_type;
23286 struct dwarf2_cu *type_cu;
23287 struct die_info *type_die;
23288 struct type *type;
23289
23290 sig_type = lookup_signatured_type (cu, signature);
23291 /* sig_type will be NULL if the signatured type is missing from
23292 the debug info. */
23293 if (sig_type == NULL)
23294 {
23295 complaint (&symfile_complaints,
23296 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23297 " from DIE at %s [in module %s]"),
23298 hex_string (signature), sect_offset_str (die->sect_off),
23299 objfile_name (dwarf2_per_objfile->objfile));
23300 return build_error_marker_type (cu, die);
23301 }
23302
23303 /* If we already know the type we're done. */
23304 if (sig_type->type != NULL)
23305 return sig_type->type;
23306
23307 type_cu = cu;
23308 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23309 if (type_die != NULL)
23310 {
23311 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23312 is created. This is important, for example, because for c++ classes
23313 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23314 type = read_type_die (type_die, type_cu);
23315 if (type == NULL)
23316 {
23317 complaint (&symfile_complaints,
23318 _("Dwarf Error: Cannot build signatured type %s"
23319 " referenced from DIE at %s [in module %s]"),
23320 hex_string (signature), sect_offset_str (die->sect_off),
23321 objfile_name (dwarf2_per_objfile->objfile));
23322 type = build_error_marker_type (cu, die);
23323 }
23324 }
23325 else
23326 {
23327 complaint (&symfile_complaints,
23328 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23329 " from DIE at %s [in module %s]"),
23330 hex_string (signature), sect_offset_str (die->sect_off),
23331 objfile_name (dwarf2_per_objfile->objfile));
23332 type = build_error_marker_type (cu, die);
23333 }
23334 sig_type->type = type;
23335
23336 return type;
23337 }
23338
23339 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23340 reading in and processing the type unit if necessary. */
23341
23342 static struct type *
23343 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23344 struct dwarf2_cu *cu) /* ARI: editCase function */
23345 {
23346 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23347 if (attr_form_is_ref (attr))
23348 {
23349 struct dwarf2_cu *type_cu = cu;
23350 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23351
23352 return read_type_die (type_die, type_cu);
23353 }
23354 else if (attr->form == DW_FORM_ref_sig8)
23355 {
23356 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23357 }
23358 else
23359 {
23360 struct dwarf2_per_objfile *dwarf2_per_objfile
23361 = cu->per_cu->dwarf2_per_objfile;
23362
23363 complaint (&symfile_complaints,
23364 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23365 " at %s [in module %s]"),
23366 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23367 objfile_name (dwarf2_per_objfile->objfile));
23368 return build_error_marker_type (cu, die);
23369 }
23370 }
23371
23372 /* Load the DIEs associated with type unit PER_CU into memory. */
23373
23374 static void
23375 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23376 {
23377 struct signatured_type *sig_type;
23378
23379 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23380 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23381
23382 /* We have the per_cu, but we need the signatured_type.
23383 Fortunately this is an easy translation. */
23384 gdb_assert (per_cu->is_debug_types);
23385 sig_type = (struct signatured_type *) per_cu;
23386
23387 gdb_assert (per_cu->cu == NULL);
23388
23389 read_signatured_type (sig_type);
23390
23391 gdb_assert (per_cu->cu != NULL);
23392 }
23393
23394 /* die_reader_func for read_signatured_type.
23395 This is identical to load_full_comp_unit_reader,
23396 but is kept separate for now. */
23397
23398 static void
23399 read_signatured_type_reader (const struct die_reader_specs *reader,
23400 const gdb_byte *info_ptr,
23401 struct die_info *comp_unit_die,
23402 int has_children,
23403 void *data)
23404 {
23405 struct dwarf2_cu *cu = reader->cu;
23406
23407 gdb_assert (cu->die_hash == NULL);
23408 cu->die_hash =
23409 htab_create_alloc_ex (cu->header.length / 12,
23410 die_hash,
23411 die_eq,
23412 NULL,
23413 &cu->comp_unit_obstack,
23414 hashtab_obstack_allocate,
23415 dummy_obstack_deallocate);
23416
23417 if (has_children)
23418 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23419 &info_ptr, comp_unit_die);
23420 cu->dies = comp_unit_die;
23421 /* comp_unit_die is not stored in die_hash, no need. */
23422
23423 /* We try not to read any attributes in this function, because not
23424 all CUs needed for references have been loaded yet, and symbol
23425 table processing isn't initialized. But we have to set the CU language,
23426 or we won't be able to build types correctly.
23427 Similarly, if we do not read the producer, we can not apply
23428 producer-specific interpretation. */
23429 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23430 }
23431
23432 /* Read in a signatured type and build its CU and DIEs.
23433 If the type is a stub for the real type in a DWO file,
23434 read in the real type from the DWO file as well. */
23435
23436 static void
23437 read_signatured_type (struct signatured_type *sig_type)
23438 {
23439 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23440
23441 gdb_assert (per_cu->is_debug_types);
23442 gdb_assert (per_cu->cu == NULL);
23443
23444 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23445 read_signatured_type_reader, NULL);
23446 sig_type->per_cu.tu_read = 1;
23447 }
23448
23449 /* Decode simple location descriptions.
23450 Given a pointer to a dwarf block that defines a location, compute
23451 the location and return the value.
23452
23453 NOTE drow/2003-11-18: This function is called in two situations
23454 now: for the address of static or global variables (partial symbols
23455 only) and for offsets into structures which are expected to be
23456 (more or less) constant. The partial symbol case should go away,
23457 and only the constant case should remain. That will let this
23458 function complain more accurately. A few special modes are allowed
23459 without complaint for global variables (for instance, global
23460 register values and thread-local values).
23461
23462 A location description containing no operations indicates that the
23463 object is optimized out. The return value is 0 for that case.
23464 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23465 callers will only want a very basic result and this can become a
23466 complaint.
23467
23468 Note that stack[0] is unused except as a default error return. */
23469
23470 static CORE_ADDR
23471 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23472 {
23473 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23474 size_t i;
23475 size_t size = blk->size;
23476 const gdb_byte *data = blk->data;
23477 CORE_ADDR stack[64];
23478 int stacki;
23479 unsigned int bytes_read, unsnd;
23480 gdb_byte op;
23481
23482 i = 0;
23483 stacki = 0;
23484 stack[stacki] = 0;
23485 stack[++stacki] = 0;
23486
23487 while (i < size)
23488 {
23489 op = data[i++];
23490 switch (op)
23491 {
23492 case DW_OP_lit0:
23493 case DW_OP_lit1:
23494 case DW_OP_lit2:
23495 case DW_OP_lit3:
23496 case DW_OP_lit4:
23497 case DW_OP_lit5:
23498 case DW_OP_lit6:
23499 case DW_OP_lit7:
23500 case DW_OP_lit8:
23501 case DW_OP_lit9:
23502 case DW_OP_lit10:
23503 case DW_OP_lit11:
23504 case DW_OP_lit12:
23505 case DW_OP_lit13:
23506 case DW_OP_lit14:
23507 case DW_OP_lit15:
23508 case DW_OP_lit16:
23509 case DW_OP_lit17:
23510 case DW_OP_lit18:
23511 case DW_OP_lit19:
23512 case DW_OP_lit20:
23513 case DW_OP_lit21:
23514 case DW_OP_lit22:
23515 case DW_OP_lit23:
23516 case DW_OP_lit24:
23517 case DW_OP_lit25:
23518 case DW_OP_lit26:
23519 case DW_OP_lit27:
23520 case DW_OP_lit28:
23521 case DW_OP_lit29:
23522 case DW_OP_lit30:
23523 case DW_OP_lit31:
23524 stack[++stacki] = op - DW_OP_lit0;
23525 break;
23526
23527 case DW_OP_reg0:
23528 case DW_OP_reg1:
23529 case DW_OP_reg2:
23530 case DW_OP_reg3:
23531 case DW_OP_reg4:
23532 case DW_OP_reg5:
23533 case DW_OP_reg6:
23534 case DW_OP_reg7:
23535 case DW_OP_reg8:
23536 case DW_OP_reg9:
23537 case DW_OP_reg10:
23538 case DW_OP_reg11:
23539 case DW_OP_reg12:
23540 case DW_OP_reg13:
23541 case DW_OP_reg14:
23542 case DW_OP_reg15:
23543 case DW_OP_reg16:
23544 case DW_OP_reg17:
23545 case DW_OP_reg18:
23546 case DW_OP_reg19:
23547 case DW_OP_reg20:
23548 case DW_OP_reg21:
23549 case DW_OP_reg22:
23550 case DW_OP_reg23:
23551 case DW_OP_reg24:
23552 case DW_OP_reg25:
23553 case DW_OP_reg26:
23554 case DW_OP_reg27:
23555 case DW_OP_reg28:
23556 case DW_OP_reg29:
23557 case DW_OP_reg30:
23558 case DW_OP_reg31:
23559 stack[++stacki] = op - DW_OP_reg0;
23560 if (i < size)
23561 dwarf2_complex_location_expr_complaint ();
23562 break;
23563
23564 case DW_OP_regx:
23565 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23566 i += bytes_read;
23567 stack[++stacki] = unsnd;
23568 if (i < size)
23569 dwarf2_complex_location_expr_complaint ();
23570 break;
23571
23572 case DW_OP_addr:
23573 stack[++stacki] = read_address (objfile->obfd, &data[i],
23574 cu, &bytes_read);
23575 i += bytes_read;
23576 break;
23577
23578 case DW_OP_const1u:
23579 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23580 i += 1;
23581 break;
23582
23583 case DW_OP_const1s:
23584 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23585 i += 1;
23586 break;
23587
23588 case DW_OP_const2u:
23589 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23590 i += 2;
23591 break;
23592
23593 case DW_OP_const2s:
23594 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23595 i += 2;
23596 break;
23597
23598 case DW_OP_const4u:
23599 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23600 i += 4;
23601 break;
23602
23603 case DW_OP_const4s:
23604 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23605 i += 4;
23606 break;
23607
23608 case DW_OP_const8u:
23609 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23610 i += 8;
23611 break;
23612
23613 case DW_OP_constu:
23614 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23615 &bytes_read);
23616 i += bytes_read;
23617 break;
23618
23619 case DW_OP_consts:
23620 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23621 i += bytes_read;
23622 break;
23623
23624 case DW_OP_dup:
23625 stack[stacki + 1] = stack[stacki];
23626 stacki++;
23627 break;
23628
23629 case DW_OP_plus:
23630 stack[stacki - 1] += stack[stacki];
23631 stacki--;
23632 break;
23633
23634 case DW_OP_plus_uconst:
23635 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23636 &bytes_read);
23637 i += bytes_read;
23638 break;
23639
23640 case DW_OP_minus:
23641 stack[stacki - 1] -= stack[stacki];
23642 stacki--;
23643 break;
23644
23645 case DW_OP_deref:
23646 /* If we're not the last op, then we definitely can't encode
23647 this using GDB's address_class enum. This is valid for partial
23648 global symbols, although the variable's address will be bogus
23649 in the psymtab. */
23650 if (i < size)
23651 dwarf2_complex_location_expr_complaint ();
23652 break;
23653
23654 case DW_OP_GNU_push_tls_address:
23655 case DW_OP_form_tls_address:
23656 /* The top of the stack has the offset from the beginning
23657 of the thread control block at which the variable is located. */
23658 /* Nothing should follow this operator, so the top of stack would
23659 be returned. */
23660 /* This is valid for partial global symbols, but the variable's
23661 address will be bogus in the psymtab. Make it always at least
23662 non-zero to not look as a variable garbage collected by linker
23663 which have DW_OP_addr 0. */
23664 if (i < size)
23665 dwarf2_complex_location_expr_complaint ();
23666 stack[stacki]++;
23667 break;
23668
23669 case DW_OP_GNU_uninit:
23670 break;
23671
23672 case DW_OP_GNU_addr_index:
23673 case DW_OP_GNU_const_index:
23674 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23675 &bytes_read);
23676 i += bytes_read;
23677 break;
23678
23679 default:
23680 {
23681 const char *name = get_DW_OP_name (op);
23682
23683 if (name)
23684 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23685 name);
23686 else
23687 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23688 op);
23689 }
23690
23691 return (stack[stacki]);
23692 }
23693
23694 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23695 outside of the allocated space. Also enforce minimum>0. */
23696 if (stacki >= ARRAY_SIZE (stack) - 1)
23697 {
23698 complaint (&symfile_complaints,
23699 _("location description stack overflow"));
23700 return 0;
23701 }
23702
23703 if (stacki <= 0)
23704 {
23705 complaint (&symfile_complaints,
23706 _("location description stack underflow"));
23707 return 0;
23708 }
23709 }
23710 return (stack[stacki]);
23711 }
23712
23713 /* memory allocation interface */
23714
23715 static struct dwarf_block *
23716 dwarf_alloc_block (struct dwarf2_cu *cu)
23717 {
23718 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23719 }
23720
23721 static struct die_info *
23722 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23723 {
23724 struct die_info *die;
23725 size_t size = sizeof (struct die_info);
23726
23727 if (num_attrs > 1)
23728 size += (num_attrs - 1) * sizeof (struct attribute);
23729
23730 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23731 memset (die, 0, sizeof (struct die_info));
23732 return (die);
23733 }
23734
23735 \f
23736 /* Macro support. */
23737
23738 /* Return file name relative to the compilation directory of file number I in
23739 *LH's file name table. The result is allocated using xmalloc; the caller is
23740 responsible for freeing it. */
23741
23742 static char *
23743 file_file_name (int file, struct line_header *lh)
23744 {
23745 /* Is the file number a valid index into the line header's file name
23746 table? Remember that file numbers start with one, not zero. */
23747 if (1 <= file && file <= lh->file_names.size ())
23748 {
23749 const file_entry &fe = lh->file_names[file - 1];
23750
23751 if (!IS_ABSOLUTE_PATH (fe.name))
23752 {
23753 const char *dir = fe.include_dir (lh);
23754 if (dir != NULL)
23755 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23756 }
23757 return xstrdup (fe.name);
23758 }
23759 else
23760 {
23761 /* The compiler produced a bogus file number. We can at least
23762 record the macro definitions made in the file, even if we
23763 won't be able to find the file by name. */
23764 char fake_name[80];
23765
23766 xsnprintf (fake_name, sizeof (fake_name),
23767 "<bad macro file number %d>", file);
23768
23769 complaint (&symfile_complaints,
23770 _("bad file number in macro information (%d)"),
23771 file);
23772
23773 return xstrdup (fake_name);
23774 }
23775 }
23776
23777 /* Return the full name of file number I in *LH's file name table.
23778 Use COMP_DIR as the name of the current directory of the
23779 compilation. The result is allocated using xmalloc; the caller is
23780 responsible for freeing it. */
23781 static char *
23782 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23783 {
23784 /* Is the file number a valid index into the line header's file name
23785 table? Remember that file numbers start with one, not zero. */
23786 if (1 <= file && file <= lh->file_names.size ())
23787 {
23788 char *relative = file_file_name (file, lh);
23789
23790 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23791 return relative;
23792 return reconcat (relative, comp_dir, SLASH_STRING,
23793 relative, (char *) NULL);
23794 }
23795 else
23796 return file_file_name (file, lh);
23797 }
23798
23799
23800 static struct macro_source_file *
23801 macro_start_file (int file, int line,
23802 struct macro_source_file *current_file,
23803 struct line_header *lh)
23804 {
23805 /* File name relative to the compilation directory of this source file. */
23806 char *file_name = file_file_name (file, lh);
23807
23808 if (! current_file)
23809 {
23810 /* Note: We don't create a macro table for this compilation unit
23811 at all until we actually get a filename. */
23812 struct macro_table *macro_table = get_macro_table ();
23813
23814 /* If we have no current file, then this must be the start_file
23815 directive for the compilation unit's main source file. */
23816 current_file = macro_set_main (macro_table, file_name);
23817 macro_define_special (macro_table);
23818 }
23819 else
23820 current_file = macro_include (current_file, line, file_name);
23821
23822 xfree (file_name);
23823
23824 return current_file;
23825 }
23826
23827 static const char *
23828 consume_improper_spaces (const char *p, const char *body)
23829 {
23830 if (*p == ' ')
23831 {
23832 complaint (&symfile_complaints,
23833 _("macro definition contains spaces "
23834 "in formal argument list:\n`%s'"),
23835 body);
23836
23837 while (*p == ' ')
23838 p++;
23839 }
23840
23841 return p;
23842 }
23843
23844
23845 static void
23846 parse_macro_definition (struct macro_source_file *file, int line,
23847 const char *body)
23848 {
23849 const char *p;
23850
23851 /* The body string takes one of two forms. For object-like macro
23852 definitions, it should be:
23853
23854 <macro name> " " <definition>
23855
23856 For function-like macro definitions, it should be:
23857
23858 <macro name> "() " <definition>
23859 or
23860 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23861
23862 Spaces may appear only where explicitly indicated, and in the
23863 <definition>.
23864
23865 The Dwarf 2 spec says that an object-like macro's name is always
23866 followed by a space, but versions of GCC around March 2002 omit
23867 the space when the macro's definition is the empty string.
23868
23869 The Dwarf 2 spec says that there should be no spaces between the
23870 formal arguments in a function-like macro's formal argument list,
23871 but versions of GCC around March 2002 include spaces after the
23872 commas. */
23873
23874
23875 /* Find the extent of the macro name. The macro name is terminated
23876 by either a space or null character (for an object-like macro) or
23877 an opening paren (for a function-like macro). */
23878 for (p = body; *p; p++)
23879 if (*p == ' ' || *p == '(')
23880 break;
23881
23882 if (*p == ' ' || *p == '\0')
23883 {
23884 /* It's an object-like macro. */
23885 int name_len = p - body;
23886 char *name = savestring (body, name_len);
23887 const char *replacement;
23888
23889 if (*p == ' ')
23890 replacement = body + name_len + 1;
23891 else
23892 {
23893 dwarf2_macro_malformed_definition_complaint (body);
23894 replacement = body + name_len;
23895 }
23896
23897 macro_define_object (file, line, name, replacement);
23898
23899 xfree (name);
23900 }
23901 else if (*p == '(')
23902 {
23903 /* It's a function-like macro. */
23904 char *name = savestring (body, p - body);
23905 int argc = 0;
23906 int argv_size = 1;
23907 char **argv = XNEWVEC (char *, argv_size);
23908
23909 p++;
23910
23911 p = consume_improper_spaces (p, body);
23912
23913 /* Parse the formal argument list. */
23914 while (*p && *p != ')')
23915 {
23916 /* Find the extent of the current argument name. */
23917 const char *arg_start = p;
23918
23919 while (*p && *p != ',' && *p != ')' && *p != ' ')
23920 p++;
23921
23922 if (! *p || p == arg_start)
23923 dwarf2_macro_malformed_definition_complaint (body);
23924 else
23925 {
23926 /* Make sure argv has room for the new argument. */
23927 if (argc >= argv_size)
23928 {
23929 argv_size *= 2;
23930 argv = XRESIZEVEC (char *, argv, argv_size);
23931 }
23932
23933 argv[argc++] = savestring (arg_start, p - arg_start);
23934 }
23935
23936 p = consume_improper_spaces (p, body);
23937
23938 /* Consume the comma, if present. */
23939 if (*p == ',')
23940 {
23941 p++;
23942
23943 p = consume_improper_spaces (p, body);
23944 }
23945 }
23946
23947 if (*p == ')')
23948 {
23949 p++;
23950
23951 if (*p == ' ')
23952 /* Perfectly formed definition, no complaints. */
23953 macro_define_function (file, line, name,
23954 argc, (const char **) argv,
23955 p + 1);
23956 else if (*p == '\0')
23957 {
23958 /* Complain, but do define it. */
23959 dwarf2_macro_malformed_definition_complaint (body);
23960 macro_define_function (file, line, name,
23961 argc, (const char **) argv,
23962 p);
23963 }
23964 else
23965 /* Just complain. */
23966 dwarf2_macro_malformed_definition_complaint (body);
23967 }
23968 else
23969 /* Just complain. */
23970 dwarf2_macro_malformed_definition_complaint (body);
23971
23972 xfree (name);
23973 {
23974 int i;
23975
23976 for (i = 0; i < argc; i++)
23977 xfree (argv[i]);
23978 }
23979 xfree (argv);
23980 }
23981 else
23982 dwarf2_macro_malformed_definition_complaint (body);
23983 }
23984
23985 /* Skip some bytes from BYTES according to the form given in FORM.
23986 Returns the new pointer. */
23987
23988 static const gdb_byte *
23989 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23990 enum dwarf_form form,
23991 unsigned int offset_size,
23992 struct dwarf2_section_info *section)
23993 {
23994 unsigned int bytes_read;
23995
23996 switch (form)
23997 {
23998 case DW_FORM_data1:
23999 case DW_FORM_flag:
24000 ++bytes;
24001 break;
24002
24003 case DW_FORM_data2:
24004 bytes += 2;
24005 break;
24006
24007 case DW_FORM_data4:
24008 bytes += 4;
24009 break;
24010
24011 case DW_FORM_data8:
24012 bytes += 8;
24013 break;
24014
24015 case DW_FORM_data16:
24016 bytes += 16;
24017 break;
24018
24019 case DW_FORM_string:
24020 read_direct_string (abfd, bytes, &bytes_read);
24021 bytes += bytes_read;
24022 break;
24023
24024 case DW_FORM_sec_offset:
24025 case DW_FORM_strp:
24026 case DW_FORM_GNU_strp_alt:
24027 bytes += offset_size;
24028 break;
24029
24030 case DW_FORM_block:
24031 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24032 bytes += bytes_read;
24033 break;
24034
24035 case DW_FORM_block1:
24036 bytes += 1 + read_1_byte (abfd, bytes);
24037 break;
24038 case DW_FORM_block2:
24039 bytes += 2 + read_2_bytes (abfd, bytes);
24040 break;
24041 case DW_FORM_block4:
24042 bytes += 4 + read_4_bytes (abfd, bytes);
24043 break;
24044
24045 case DW_FORM_sdata:
24046 case DW_FORM_udata:
24047 case DW_FORM_GNU_addr_index:
24048 case DW_FORM_GNU_str_index:
24049 bytes = gdb_skip_leb128 (bytes, buffer_end);
24050 if (bytes == NULL)
24051 {
24052 dwarf2_section_buffer_overflow_complaint (section);
24053 return NULL;
24054 }
24055 break;
24056
24057 case DW_FORM_implicit_const:
24058 break;
24059
24060 default:
24061 {
24062 complaint (&symfile_complaints,
24063 _("invalid form 0x%x in `%s'"),
24064 form, get_section_name (section));
24065 return NULL;
24066 }
24067 }
24068
24069 return bytes;
24070 }
24071
24072 /* A helper for dwarf_decode_macros that handles skipping an unknown
24073 opcode. Returns an updated pointer to the macro data buffer; or,
24074 on error, issues a complaint and returns NULL. */
24075
24076 static const gdb_byte *
24077 skip_unknown_opcode (unsigned int opcode,
24078 const gdb_byte **opcode_definitions,
24079 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24080 bfd *abfd,
24081 unsigned int offset_size,
24082 struct dwarf2_section_info *section)
24083 {
24084 unsigned int bytes_read, i;
24085 unsigned long arg;
24086 const gdb_byte *defn;
24087
24088 if (opcode_definitions[opcode] == NULL)
24089 {
24090 complaint (&symfile_complaints,
24091 _("unrecognized DW_MACFINO opcode 0x%x"),
24092 opcode);
24093 return NULL;
24094 }
24095
24096 defn = opcode_definitions[opcode];
24097 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24098 defn += bytes_read;
24099
24100 for (i = 0; i < arg; ++i)
24101 {
24102 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24103 (enum dwarf_form) defn[i], offset_size,
24104 section);
24105 if (mac_ptr == NULL)
24106 {
24107 /* skip_form_bytes already issued the complaint. */
24108 return NULL;
24109 }
24110 }
24111
24112 return mac_ptr;
24113 }
24114
24115 /* A helper function which parses the header of a macro section.
24116 If the macro section is the extended (for now called "GNU") type,
24117 then this updates *OFFSET_SIZE. Returns a pointer to just after
24118 the header, or issues a complaint and returns NULL on error. */
24119
24120 static const gdb_byte *
24121 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24122 bfd *abfd,
24123 const gdb_byte *mac_ptr,
24124 unsigned int *offset_size,
24125 int section_is_gnu)
24126 {
24127 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24128
24129 if (section_is_gnu)
24130 {
24131 unsigned int version, flags;
24132
24133 version = read_2_bytes (abfd, mac_ptr);
24134 if (version != 4 && version != 5)
24135 {
24136 complaint (&symfile_complaints,
24137 _("unrecognized version `%d' in .debug_macro section"),
24138 version);
24139 return NULL;
24140 }
24141 mac_ptr += 2;
24142
24143 flags = read_1_byte (abfd, mac_ptr);
24144 ++mac_ptr;
24145 *offset_size = (flags & 1) ? 8 : 4;
24146
24147 if ((flags & 2) != 0)
24148 /* We don't need the line table offset. */
24149 mac_ptr += *offset_size;
24150
24151 /* Vendor opcode descriptions. */
24152 if ((flags & 4) != 0)
24153 {
24154 unsigned int i, count;
24155
24156 count = read_1_byte (abfd, mac_ptr);
24157 ++mac_ptr;
24158 for (i = 0; i < count; ++i)
24159 {
24160 unsigned int opcode, bytes_read;
24161 unsigned long arg;
24162
24163 opcode = read_1_byte (abfd, mac_ptr);
24164 ++mac_ptr;
24165 opcode_definitions[opcode] = mac_ptr;
24166 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24167 mac_ptr += bytes_read;
24168 mac_ptr += arg;
24169 }
24170 }
24171 }
24172
24173 return mac_ptr;
24174 }
24175
24176 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24177 including DW_MACRO_import. */
24178
24179 static void
24180 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24181 bfd *abfd,
24182 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24183 struct macro_source_file *current_file,
24184 struct line_header *lh,
24185 struct dwarf2_section_info *section,
24186 int section_is_gnu, int section_is_dwz,
24187 unsigned int offset_size,
24188 htab_t include_hash)
24189 {
24190 struct objfile *objfile = dwarf2_per_objfile->objfile;
24191 enum dwarf_macro_record_type macinfo_type;
24192 int at_commandline;
24193 const gdb_byte *opcode_definitions[256];
24194
24195 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24196 &offset_size, section_is_gnu);
24197 if (mac_ptr == NULL)
24198 {
24199 /* We already issued a complaint. */
24200 return;
24201 }
24202
24203 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24204 GDB is still reading the definitions from command line. First
24205 DW_MACINFO_start_file will need to be ignored as it was already executed
24206 to create CURRENT_FILE for the main source holding also the command line
24207 definitions. On first met DW_MACINFO_start_file this flag is reset to
24208 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24209
24210 at_commandline = 1;
24211
24212 do
24213 {
24214 /* Do we at least have room for a macinfo type byte? */
24215 if (mac_ptr >= mac_end)
24216 {
24217 dwarf2_section_buffer_overflow_complaint (section);
24218 break;
24219 }
24220
24221 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24222 mac_ptr++;
24223
24224 /* Note that we rely on the fact that the corresponding GNU and
24225 DWARF constants are the same. */
24226 DIAGNOSTIC_PUSH
24227 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24228 switch (macinfo_type)
24229 {
24230 /* A zero macinfo type indicates the end of the macro
24231 information. */
24232 case 0:
24233 break;
24234
24235 case DW_MACRO_define:
24236 case DW_MACRO_undef:
24237 case DW_MACRO_define_strp:
24238 case DW_MACRO_undef_strp:
24239 case DW_MACRO_define_sup:
24240 case DW_MACRO_undef_sup:
24241 {
24242 unsigned int bytes_read;
24243 int line;
24244 const char *body;
24245 int is_define;
24246
24247 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24248 mac_ptr += bytes_read;
24249
24250 if (macinfo_type == DW_MACRO_define
24251 || macinfo_type == DW_MACRO_undef)
24252 {
24253 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24254 mac_ptr += bytes_read;
24255 }
24256 else
24257 {
24258 LONGEST str_offset;
24259
24260 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24261 mac_ptr += offset_size;
24262
24263 if (macinfo_type == DW_MACRO_define_sup
24264 || macinfo_type == DW_MACRO_undef_sup
24265 || section_is_dwz)
24266 {
24267 struct dwz_file *dwz
24268 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24269
24270 body = read_indirect_string_from_dwz (objfile,
24271 dwz, str_offset);
24272 }
24273 else
24274 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24275 abfd, str_offset);
24276 }
24277
24278 is_define = (macinfo_type == DW_MACRO_define
24279 || macinfo_type == DW_MACRO_define_strp
24280 || macinfo_type == DW_MACRO_define_sup);
24281 if (! current_file)
24282 {
24283 /* DWARF violation as no main source is present. */
24284 complaint (&symfile_complaints,
24285 _("debug info with no main source gives macro %s "
24286 "on line %d: %s"),
24287 is_define ? _("definition") : _("undefinition"),
24288 line, body);
24289 break;
24290 }
24291 if ((line == 0 && !at_commandline)
24292 || (line != 0 && at_commandline))
24293 complaint (&symfile_complaints,
24294 _("debug info gives %s macro %s with %s line %d: %s"),
24295 at_commandline ? _("command-line") : _("in-file"),
24296 is_define ? _("definition") : _("undefinition"),
24297 line == 0 ? _("zero") : _("non-zero"), line, body);
24298
24299 if (is_define)
24300 parse_macro_definition (current_file, line, body);
24301 else
24302 {
24303 gdb_assert (macinfo_type == DW_MACRO_undef
24304 || macinfo_type == DW_MACRO_undef_strp
24305 || macinfo_type == DW_MACRO_undef_sup);
24306 macro_undef (current_file, line, body);
24307 }
24308 }
24309 break;
24310
24311 case DW_MACRO_start_file:
24312 {
24313 unsigned int bytes_read;
24314 int line, file;
24315
24316 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24317 mac_ptr += bytes_read;
24318 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24319 mac_ptr += bytes_read;
24320
24321 if ((line == 0 && !at_commandline)
24322 || (line != 0 && at_commandline))
24323 complaint (&symfile_complaints,
24324 _("debug info gives source %d included "
24325 "from %s at %s line %d"),
24326 file, at_commandline ? _("command-line") : _("file"),
24327 line == 0 ? _("zero") : _("non-zero"), line);
24328
24329 if (at_commandline)
24330 {
24331 /* This DW_MACRO_start_file was executed in the
24332 pass one. */
24333 at_commandline = 0;
24334 }
24335 else
24336 current_file = macro_start_file (file, line, current_file, lh);
24337 }
24338 break;
24339
24340 case DW_MACRO_end_file:
24341 if (! current_file)
24342 complaint (&symfile_complaints,
24343 _("macro debug info has an unmatched "
24344 "`close_file' directive"));
24345 else
24346 {
24347 current_file = current_file->included_by;
24348 if (! current_file)
24349 {
24350 enum dwarf_macro_record_type next_type;
24351
24352 /* GCC circa March 2002 doesn't produce the zero
24353 type byte marking the end of the compilation
24354 unit. Complain if it's not there, but exit no
24355 matter what. */
24356
24357 /* Do we at least have room for a macinfo type byte? */
24358 if (mac_ptr >= mac_end)
24359 {
24360 dwarf2_section_buffer_overflow_complaint (section);
24361 return;
24362 }
24363
24364 /* We don't increment mac_ptr here, so this is just
24365 a look-ahead. */
24366 next_type
24367 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24368 mac_ptr);
24369 if (next_type != 0)
24370 complaint (&symfile_complaints,
24371 _("no terminating 0-type entry for "
24372 "macros in `.debug_macinfo' section"));
24373
24374 return;
24375 }
24376 }
24377 break;
24378
24379 case DW_MACRO_import:
24380 case DW_MACRO_import_sup:
24381 {
24382 LONGEST offset;
24383 void **slot;
24384 bfd *include_bfd = abfd;
24385 struct dwarf2_section_info *include_section = section;
24386 const gdb_byte *include_mac_end = mac_end;
24387 int is_dwz = section_is_dwz;
24388 const gdb_byte *new_mac_ptr;
24389
24390 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24391 mac_ptr += offset_size;
24392
24393 if (macinfo_type == DW_MACRO_import_sup)
24394 {
24395 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24396
24397 dwarf2_read_section (objfile, &dwz->macro);
24398
24399 include_section = &dwz->macro;
24400 include_bfd = get_section_bfd_owner (include_section);
24401 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24402 is_dwz = 1;
24403 }
24404
24405 new_mac_ptr = include_section->buffer + offset;
24406 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24407
24408 if (*slot != NULL)
24409 {
24410 /* This has actually happened; see
24411 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24412 complaint (&symfile_complaints,
24413 _("recursive DW_MACRO_import in "
24414 ".debug_macro section"));
24415 }
24416 else
24417 {
24418 *slot = (void *) new_mac_ptr;
24419
24420 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24421 include_bfd, new_mac_ptr,
24422 include_mac_end, current_file, lh,
24423 section, section_is_gnu, is_dwz,
24424 offset_size, include_hash);
24425
24426 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24427 }
24428 }
24429 break;
24430
24431 case DW_MACINFO_vendor_ext:
24432 if (!section_is_gnu)
24433 {
24434 unsigned int bytes_read;
24435
24436 /* This reads the constant, but since we don't recognize
24437 any vendor extensions, we ignore it. */
24438 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24439 mac_ptr += bytes_read;
24440 read_direct_string (abfd, mac_ptr, &bytes_read);
24441 mac_ptr += bytes_read;
24442
24443 /* We don't recognize any vendor extensions. */
24444 break;
24445 }
24446 /* FALLTHROUGH */
24447
24448 default:
24449 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24450 mac_ptr, mac_end, abfd, offset_size,
24451 section);
24452 if (mac_ptr == NULL)
24453 return;
24454 break;
24455 }
24456 DIAGNOSTIC_POP
24457 } while (macinfo_type != 0);
24458 }
24459
24460 static void
24461 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24462 int section_is_gnu)
24463 {
24464 struct dwarf2_per_objfile *dwarf2_per_objfile
24465 = cu->per_cu->dwarf2_per_objfile;
24466 struct objfile *objfile = dwarf2_per_objfile->objfile;
24467 struct line_header *lh = cu->line_header;
24468 bfd *abfd;
24469 const gdb_byte *mac_ptr, *mac_end;
24470 struct macro_source_file *current_file = 0;
24471 enum dwarf_macro_record_type macinfo_type;
24472 unsigned int offset_size = cu->header.offset_size;
24473 const gdb_byte *opcode_definitions[256];
24474 void **slot;
24475 struct dwarf2_section_info *section;
24476 const char *section_name;
24477
24478 if (cu->dwo_unit != NULL)
24479 {
24480 if (section_is_gnu)
24481 {
24482 section = &cu->dwo_unit->dwo_file->sections.macro;
24483 section_name = ".debug_macro.dwo";
24484 }
24485 else
24486 {
24487 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24488 section_name = ".debug_macinfo.dwo";
24489 }
24490 }
24491 else
24492 {
24493 if (section_is_gnu)
24494 {
24495 section = &dwarf2_per_objfile->macro;
24496 section_name = ".debug_macro";
24497 }
24498 else
24499 {
24500 section = &dwarf2_per_objfile->macinfo;
24501 section_name = ".debug_macinfo";
24502 }
24503 }
24504
24505 dwarf2_read_section (objfile, section);
24506 if (section->buffer == NULL)
24507 {
24508 complaint (&symfile_complaints, _("missing %s section"), section_name);
24509 return;
24510 }
24511 abfd = get_section_bfd_owner (section);
24512
24513 /* First pass: Find the name of the base filename.
24514 This filename is needed in order to process all macros whose definition
24515 (or undefinition) comes from the command line. These macros are defined
24516 before the first DW_MACINFO_start_file entry, and yet still need to be
24517 associated to the base file.
24518
24519 To determine the base file name, we scan the macro definitions until we
24520 reach the first DW_MACINFO_start_file entry. We then initialize
24521 CURRENT_FILE accordingly so that any macro definition found before the
24522 first DW_MACINFO_start_file can still be associated to the base file. */
24523
24524 mac_ptr = section->buffer + offset;
24525 mac_end = section->buffer + section->size;
24526
24527 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24528 &offset_size, section_is_gnu);
24529 if (mac_ptr == NULL)
24530 {
24531 /* We already issued a complaint. */
24532 return;
24533 }
24534
24535 do
24536 {
24537 /* Do we at least have room for a macinfo type byte? */
24538 if (mac_ptr >= mac_end)
24539 {
24540 /* Complaint is printed during the second pass as GDB will probably
24541 stop the first pass earlier upon finding
24542 DW_MACINFO_start_file. */
24543 break;
24544 }
24545
24546 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24547 mac_ptr++;
24548
24549 /* Note that we rely on the fact that the corresponding GNU and
24550 DWARF constants are the same. */
24551 DIAGNOSTIC_PUSH
24552 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24553 switch (macinfo_type)
24554 {
24555 /* A zero macinfo type indicates the end of the macro
24556 information. */
24557 case 0:
24558 break;
24559
24560 case DW_MACRO_define:
24561 case DW_MACRO_undef:
24562 /* Only skip the data by MAC_PTR. */
24563 {
24564 unsigned int bytes_read;
24565
24566 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24567 mac_ptr += bytes_read;
24568 read_direct_string (abfd, mac_ptr, &bytes_read);
24569 mac_ptr += bytes_read;
24570 }
24571 break;
24572
24573 case DW_MACRO_start_file:
24574 {
24575 unsigned int bytes_read;
24576 int line, file;
24577
24578 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24579 mac_ptr += bytes_read;
24580 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24581 mac_ptr += bytes_read;
24582
24583 current_file = macro_start_file (file, line, current_file, lh);
24584 }
24585 break;
24586
24587 case DW_MACRO_end_file:
24588 /* No data to skip by MAC_PTR. */
24589 break;
24590
24591 case DW_MACRO_define_strp:
24592 case DW_MACRO_undef_strp:
24593 case DW_MACRO_define_sup:
24594 case DW_MACRO_undef_sup:
24595 {
24596 unsigned int bytes_read;
24597
24598 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24599 mac_ptr += bytes_read;
24600 mac_ptr += offset_size;
24601 }
24602 break;
24603
24604 case DW_MACRO_import:
24605 case DW_MACRO_import_sup:
24606 /* Note that, according to the spec, a transparent include
24607 chain cannot call DW_MACRO_start_file. So, we can just
24608 skip this opcode. */
24609 mac_ptr += offset_size;
24610 break;
24611
24612 case DW_MACINFO_vendor_ext:
24613 /* Only skip the data by MAC_PTR. */
24614 if (!section_is_gnu)
24615 {
24616 unsigned int bytes_read;
24617
24618 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24619 mac_ptr += bytes_read;
24620 read_direct_string (abfd, mac_ptr, &bytes_read);
24621 mac_ptr += bytes_read;
24622 }
24623 /* FALLTHROUGH */
24624
24625 default:
24626 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24627 mac_ptr, mac_end, abfd, offset_size,
24628 section);
24629 if (mac_ptr == NULL)
24630 return;
24631 break;
24632 }
24633 DIAGNOSTIC_POP
24634 } while (macinfo_type != 0 && current_file == NULL);
24635
24636 /* Second pass: Process all entries.
24637
24638 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24639 command-line macro definitions/undefinitions. This flag is unset when we
24640 reach the first DW_MACINFO_start_file entry. */
24641
24642 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24643 htab_eq_pointer,
24644 NULL, xcalloc, xfree));
24645 mac_ptr = section->buffer + offset;
24646 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24647 *slot = (void *) mac_ptr;
24648 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24649 abfd, mac_ptr, mac_end,
24650 current_file, lh, section,
24651 section_is_gnu, 0, offset_size,
24652 include_hash.get ());
24653 }
24654
24655 /* Check if the attribute's form is a DW_FORM_block*
24656 if so return true else false. */
24657
24658 static int
24659 attr_form_is_block (const struct attribute *attr)
24660 {
24661 return (attr == NULL ? 0 :
24662 attr->form == DW_FORM_block1
24663 || attr->form == DW_FORM_block2
24664 || attr->form == DW_FORM_block4
24665 || attr->form == DW_FORM_block
24666 || attr->form == DW_FORM_exprloc);
24667 }
24668
24669 /* Return non-zero if ATTR's value is a section offset --- classes
24670 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24671 You may use DW_UNSND (attr) to retrieve such offsets.
24672
24673 Section 7.5.4, "Attribute Encodings", explains that no attribute
24674 may have a value that belongs to more than one of these classes; it
24675 would be ambiguous if we did, because we use the same forms for all
24676 of them. */
24677
24678 static int
24679 attr_form_is_section_offset (const struct attribute *attr)
24680 {
24681 return (attr->form == DW_FORM_data4
24682 || attr->form == DW_FORM_data8
24683 || attr->form == DW_FORM_sec_offset);
24684 }
24685
24686 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24687 zero otherwise. When this function returns true, you can apply
24688 dwarf2_get_attr_constant_value to it.
24689
24690 However, note that for some attributes you must check
24691 attr_form_is_section_offset before using this test. DW_FORM_data4
24692 and DW_FORM_data8 are members of both the constant class, and of
24693 the classes that contain offsets into other debug sections
24694 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24695 that, if an attribute's can be either a constant or one of the
24696 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24697 taken as section offsets, not constants.
24698
24699 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24700 cannot handle that. */
24701
24702 static int
24703 attr_form_is_constant (const struct attribute *attr)
24704 {
24705 switch (attr->form)
24706 {
24707 case DW_FORM_sdata:
24708 case DW_FORM_udata:
24709 case DW_FORM_data1:
24710 case DW_FORM_data2:
24711 case DW_FORM_data4:
24712 case DW_FORM_data8:
24713 case DW_FORM_implicit_const:
24714 return 1;
24715 default:
24716 return 0;
24717 }
24718 }
24719
24720
24721 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24722 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24723
24724 static int
24725 attr_form_is_ref (const struct attribute *attr)
24726 {
24727 switch (attr->form)
24728 {
24729 case DW_FORM_ref_addr:
24730 case DW_FORM_ref1:
24731 case DW_FORM_ref2:
24732 case DW_FORM_ref4:
24733 case DW_FORM_ref8:
24734 case DW_FORM_ref_udata:
24735 case DW_FORM_GNU_ref_alt:
24736 return 1;
24737 default:
24738 return 0;
24739 }
24740 }
24741
24742 /* Return the .debug_loc section to use for CU.
24743 For DWO files use .debug_loc.dwo. */
24744
24745 static struct dwarf2_section_info *
24746 cu_debug_loc_section (struct dwarf2_cu *cu)
24747 {
24748 struct dwarf2_per_objfile *dwarf2_per_objfile
24749 = cu->per_cu->dwarf2_per_objfile;
24750
24751 if (cu->dwo_unit)
24752 {
24753 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24754
24755 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24756 }
24757 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24758 : &dwarf2_per_objfile->loc);
24759 }
24760
24761 /* A helper function that fills in a dwarf2_loclist_baton. */
24762
24763 static void
24764 fill_in_loclist_baton (struct dwarf2_cu *cu,
24765 struct dwarf2_loclist_baton *baton,
24766 const struct attribute *attr)
24767 {
24768 struct dwarf2_per_objfile *dwarf2_per_objfile
24769 = cu->per_cu->dwarf2_per_objfile;
24770 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24771
24772 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24773
24774 baton->per_cu = cu->per_cu;
24775 gdb_assert (baton->per_cu);
24776 /* We don't know how long the location list is, but make sure we
24777 don't run off the edge of the section. */
24778 baton->size = section->size - DW_UNSND (attr);
24779 baton->data = section->buffer + DW_UNSND (attr);
24780 baton->base_address = cu->base_address;
24781 baton->from_dwo = cu->dwo_unit != NULL;
24782 }
24783
24784 static void
24785 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24786 struct dwarf2_cu *cu, int is_block)
24787 {
24788 struct dwarf2_per_objfile *dwarf2_per_objfile
24789 = cu->per_cu->dwarf2_per_objfile;
24790 struct objfile *objfile = dwarf2_per_objfile->objfile;
24791 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24792
24793 if (attr_form_is_section_offset (attr)
24794 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24795 the section. If so, fall through to the complaint in the
24796 other branch. */
24797 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24798 {
24799 struct dwarf2_loclist_baton *baton;
24800
24801 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24802
24803 fill_in_loclist_baton (cu, baton, attr);
24804
24805 if (cu->base_known == 0)
24806 complaint (&symfile_complaints,
24807 _("Location list used without "
24808 "specifying the CU base address."));
24809
24810 SYMBOL_ACLASS_INDEX (sym) = (is_block
24811 ? dwarf2_loclist_block_index
24812 : dwarf2_loclist_index);
24813 SYMBOL_LOCATION_BATON (sym) = baton;
24814 }
24815 else
24816 {
24817 struct dwarf2_locexpr_baton *baton;
24818
24819 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24820 baton->per_cu = cu->per_cu;
24821 gdb_assert (baton->per_cu);
24822
24823 if (attr_form_is_block (attr))
24824 {
24825 /* Note that we're just copying the block's data pointer
24826 here, not the actual data. We're still pointing into the
24827 info_buffer for SYM's objfile; right now we never release
24828 that buffer, but when we do clean up properly this may
24829 need to change. */
24830 baton->size = DW_BLOCK (attr)->size;
24831 baton->data = DW_BLOCK (attr)->data;
24832 }
24833 else
24834 {
24835 dwarf2_invalid_attrib_class_complaint ("location description",
24836 SYMBOL_NATURAL_NAME (sym));
24837 baton->size = 0;
24838 }
24839
24840 SYMBOL_ACLASS_INDEX (sym) = (is_block
24841 ? dwarf2_locexpr_block_index
24842 : dwarf2_locexpr_index);
24843 SYMBOL_LOCATION_BATON (sym) = baton;
24844 }
24845 }
24846
24847 /* Return the OBJFILE associated with the compilation unit CU. If CU
24848 came from a separate debuginfo file, then the master objfile is
24849 returned. */
24850
24851 struct objfile *
24852 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24853 {
24854 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24855
24856 /* Return the master objfile, so that we can report and look up the
24857 correct file containing this variable. */
24858 if (objfile->separate_debug_objfile_backlink)
24859 objfile = objfile->separate_debug_objfile_backlink;
24860
24861 return objfile;
24862 }
24863
24864 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24865 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24866 CU_HEADERP first. */
24867
24868 static const struct comp_unit_head *
24869 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24870 struct dwarf2_per_cu_data *per_cu)
24871 {
24872 const gdb_byte *info_ptr;
24873
24874 if (per_cu->cu)
24875 return &per_cu->cu->header;
24876
24877 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24878
24879 memset (cu_headerp, 0, sizeof (*cu_headerp));
24880 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24881 rcuh_kind::COMPILE);
24882
24883 return cu_headerp;
24884 }
24885
24886 /* Return the address size given in the compilation unit header for CU. */
24887
24888 int
24889 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24890 {
24891 struct comp_unit_head cu_header_local;
24892 const struct comp_unit_head *cu_headerp;
24893
24894 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24895
24896 return cu_headerp->addr_size;
24897 }
24898
24899 /* Return the offset size given in the compilation unit header for CU. */
24900
24901 int
24902 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24903 {
24904 struct comp_unit_head cu_header_local;
24905 const struct comp_unit_head *cu_headerp;
24906
24907 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24908
24909 return cu_headerp->offset_size;
24910 }
24911
24912 /* See its dwarf2loc.h declaration. */
24913
24914 int
24915 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24916 {
24917 struct comp_unit_head cu_header_local;
24918 const struct comp_unit_head *cu_headerp;
24919
24920 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24921
24922 if (cu_headerp->version == 2)
24923 return cu_headerp->addr_size;
24924 else
24925 return cu_headerp->offset_size;
24926 }
24927
24928 /* Return the text offset of the CU. The returned offset comes from
24929 this CU's objfile. If this objfile came from a separate debuginfo
24930 file, then the offset may be different from the corresponding
24931 offset in the parent objfile. */
24932
24933 CORE_ADDR
24934 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24935 {
24936 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24937
24938 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24939 }
24940
24941 /* Return DWARF version number of PER_CU. */
24942
24943 short
24944 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24945 {
24946 return per_cu->dwarf_version;
24947 }
24948
24949 /* Locate the .debug_info compilation unit from CU's objfile which contains
24950 the DIE at OFFSET. Raises an error on failure. */
24951
24952 static struct dwarf2_per_cu_data *
24953 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24954 unsigned int offset_in_dwz,
24955 struct dwarf2_per_objfile *dwarf2_per_objfile)
24956 {
24957 struct dwarf2_per_cu_data *this_cu;
24958 int low, high;
24959 const sect_offset *cu_off;
24960
24961 low = 0;
24962 high = dwarf2_per_objfile->n_comp_units - 1;
24963 while (high > low)
24964 {
24965 struct dwarf2_per_cu_data *mid_cu;
24966 int mid = low + (high - low) / 2;
24967
24968 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24969 cu_off = &mid_cu->sect_off;
24970 if (mid_cu->is_dwz > offset_in_dwz
24971 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
24972 high = mid;
24973 else
24974 low = mid + 1;
24975 }
24976 gdb_assert (low == high);
24977 this_cu = dwarf2_per_objfile->all_comp_units[low];
24978 cu_off = &this_cu->sect_off;
24979 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
24980 {
24981 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24982 error (_("Dwarf Error: could not find partial DIE containing "
24983 "offset %s [in module %s]"),
24984 sect_offset_str (sect_off),
24985 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24986
24987 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24988 <= sect_off);
24989 return dwarf2_per_objfile->all_comp_units[low-1];
24990 }
24991 else
24992 {
24993 this_cu = dwarf2_per_objfile->all_comp_units[low];
24994 if (low == dwarf2_per_objfile->n_comp_units - 1
24995 && sect_off >= this_cu->sect_off + this_cu->length)
24996 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24997 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24998 return this_cu;
24999 }
25000 }
25001
25002 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25003
25004 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25005 : per_cu (per_cu_),
25006 mark (0),
25007 has_loclist (0),
25008 checked_producer (0),
25009 producer_is_gxx_lt_4_6 (0),
25010 producer_is_gcc_lt_4_3 (0),
25011 producer_is_icc_lt_14 (0),
25012 processing_has_namespace_info (0)
25013 {
25014 per_cu->cu = this;
25015 }
25016
25017 /* Destroy a dwarf2_cu. */
25018
25019 dwarf2_cu::~dwarf2_cu ()
25020 {
25021 per_cu->cu = NULL;
25022 }
25023
25024 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25025
25026 static void
25027 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25028 enum language pretend_language)
25029 {
25030 struct attribute *attr;
25031
25032 /* Set the language we're debugging. */
25033 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25034 if (attr)
25035 set_cu_language (DW_UNSND (attr), cu);
25036 else
25037 {
25038 cu->language = pretend_language;
25039 cu->language_defn = language_def (cu->language);
25040 }
25041
25042 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25043 }
25044
25045 /* Increase the age counter on each cached compilation unit, and free
25046 any that are too old. */
25047
25048 static void
25049 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25050 {
25051 struct dwarf2_per_cu_data *per_cu, **last_chain;
25052
25053 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25054 per_cu = dwarf2_per_objfile->read_in_chain;
25055 while (per_cu != NULL)
25056 {
25057 per_cu->cu->last_used ++;
25058 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25059 dwarf2_mark (per_cu->cu);
25060 per_cu = per_cu->cu->read_in_chain;
25061 }
25062
25063 per_cu = dwarf2_per_objfile->read_in_chain;
25064 last_chain = &dwarf2_per_objfile->read_in_chain;
25065 while (per_cu != NULL)
25066 {
25067 struct dwarf2_per_cu_data *next_cu;
25068
25069 next_cu = per_cu->cu->read_in_chain;
25070
25071 if (!per_cu->cu->mark)
25072 {
25073 delete per_cu->cu;
25074 *last_chain = next_cu;
25075 }
25076 else
25077 last_chain = &per_cu->cu->read_in_chain;
25078
25079 per_cu = next_cu;
25080 }
25081 }
25082
25083 /* Remove a single compilation unit from the cache. */
25084
25085 static void
25086 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25087 {
25088 struct dwarf2_per_cu_data *per_cu, **last_chain;
25089 struct dwarf2_per_objfile *dwarf2_per_objfile
25090 = target_per_cu->dwarf2_per_objfile;
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 == target_per_cu)
25101 {
25102 delete per_cu->cu;
25103 per_cu->cu = NULL;
25104 *last_chain = next_cu;
25105 break;
25106 }
25107 else
25108 last_chain = &per_cu->cu->read_in_chain;
25109
25110 per_cu = next_cu;
25111 }
25112 }
25113
25114 /* Release all extra memory associated with OBJFILE. */
25115
25116 void
25117 dwarf2_free_objfile (struct objfile *objfile)
25118 {
25119 struct dwarf2_per_objfile *dwarf2_per_objfile
25120 = get_dwarf2_per_objfile (objfile);
25121
25122 delete dwarf2_per_objfile;
25123 }
25124
25125 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25126 We store these in a hash table separate from the DIEs, and preserve them
25127 when the DIEs are flushed out of cache.
25128
25129 The CU "per_cu" pointer is needed because offset alone is not enough to
25130 uniquely identify the type. A file may have multiple .debug_types sections,
25131 or the type may come from a DWO file. Furthermore, while it's more logical
25132 to use per_cu->section+offset, with Fission the section with the data is in
25133 the DWO file but we don't know that section at the point we need it.
25134 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25135 because we can enter the lookup routine, get_die_type_at_offset, from
25136 outside this file, and thus won't necessarily have PER_CU->cu.
25137 Fortunately, PER_CU is stable for the life of the objfile. */
25138
25139 struct dwarf2_per_cu_offset_and_type
25140 {
25141 const struct dwarf2_per_cu_data *per_cu;
25142 sect_offset sect_off;
25143 struct type *type;
25144 };
25145
25146 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25147
25148 static hashval_t
25149 per_cu_offset_and_type_hash (const void *item)
25150 {
25151 const struct dwarf2_per_cu_offset_and_type *ofs
25152 = (const struct dwarf2_per_cu_offset_and_type *) item;
25153
25154 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25155 }
25156
25157 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25158
25159 static int
25160 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25161 {
25162 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25163 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25164 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25165 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25166
25167 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25168 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25169 }
25170
25171 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25172 table if necessary. For convenience, return TYPE.
25173
25174 The DIEs reading must have careful ordering to:
25175 * Not cause infite loops trying to read in DIEs as a prerequisite for
25176 reading current DIE.
25177 * Not trying to dereference contents of still incompletely read in types
25178 while reading in other DIEs.
25179 * Enable referencing still incompletely read in types just by a pointer to
25180 the type without accessing its fields.
25181
25182 Therefore caller should follow these rules:
25183 * Try to fetch any prerequisite types we may need to build this DIE type
25184 before building the type and calling set_die_type.
25185 * After building type call set_die_type for current DIE as soon as
25186 possible before fetching more types to complete the current type.
25187 * Make the type as complete as possible before fetching more types. */
25188
25189 static struct type *
25190 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25191 {
25192 struct dwarf2_per_objfile *dwarf2_per_objfile
25193 = cu->per_cu->dwarf2_per_objfile;
25194 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25195 struct objfile *objfile = dwarf2_per_objfile->objfile;
25196 struct attribute *attr;
25197 struct dynamic_prop prop;
25198
25199 /* For Ada types, make sure that the gnat-specific data is always
25200 initialized (if not already set). There are a few types where
25201 we should not be doing so, because the type-specific area is
25202 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25203 where the type-specific area is used to store the floatformat).
25204 But this is not a problem, because the gnat-specific information
25205 is actually not needed for these types. */
25206 if (need_gnat_info (cu)
25207 && TYPE_CODE (type) != TYPE_CODE_FUNC
25208 && TYPE_CODE (type) != TYPE_CODE_FLT
25209 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25210 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25211 && TYPE_CODE (type) != TYPE_CODE_METHOD
25212 && !HAVE_GNAT_AUX_INFO (type))
25213 INIT_GNAT_SPECIFIC (type);
25214
25215 /* Read DW_AT_allocated and set in type. */
25216 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25217 if (attr_form_is_block (attr))
25218 {
25219 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25220 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25221 }
25222 else if (attr != NULL)
25223 {
25224 complaint (&symfile_complaints,
25225 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25226 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25227 sect_offset_str (die->sect_off));
25228 }
25229
25230 /* Read DW_AT_associated and set in type. */
25231 attr = dwarf2_attr (die, DW_AT_associated, cu);
25232 if (attr_form_is_block (attr))
25233 {
25234 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25235 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25236 }
25237 else if (attr != NULL)
25238 {
25239 complaint (&symfile_complaints,
25240 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25241 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25242 sect_offset_str (die->sect_off));
25243 }
25244
25245 /* Read DW_AT_data_location and set in type. */
25246 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25247 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25248 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25249
25250 if (dwarf2_per_objfile->die_type_hash == NULL)
25251 {
25252 dwarf2_per_objfile->die_type_hash =
25253 htab_create_alloc_ex (127,
25254 per_cu_offset_and_type_hash,
25255 per_cu_offset_and_type_eq,
25256 NULL,
25257 &objfile->objfile_obstack,
25258 hashtab_obstack_allocate,
25259 dummy_obstack_deallocate);
25260 }
25261
25262 ofs.per_cu = cu->per_cu;
25263 ofs.sect_off = die->sect_off;
25264 ofs.type = type;
25265 slot = (struct dwarf2_per_cu_offset_and_type **)
25266 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25267 if (*slot)
25268 complaint (&symfile_complaints,
25269 _("A problem internal to GDB: DIE %s has type already set"),
25270 sect_offset_str (die->sect_off));
25271 *slot = XOBNEW (&objfile->objfile_obstack,
25272 struct dwarf2_per_cu_offset_and_type);
25273 **slot = ofs;
25274 return type;
25275 }
25276
25277 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25278 or return NULL if the die does not have a saved type. */
25279
25280 static struct type *
25281 get_die_type_at_offset (sect_offset sect_off,
25282 struct dwarf2_per_cu_data *per_cu)
25283 {
25284 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25285 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25286
25287 if (dwarf2_per_objfile->die_type_hash == NULL)
25288 return NULL;
25289
25290 ofs.per_cu = per_cu;
25291 ofs.sect_off = sect_off;
25292 slot = ((struct dwarf2_per_cu_offset_and_type *)
25293 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25294 if (slot)
25295 return slot->type;
25296 else
25297 return NULL;
25298 }
25299
25300 /* Look up the type for DIE in CU in die_type_hash,
25301 or return NULL if DIE does not have a saved type. */
25302
25303 static struct type *
25304 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25305 {
25306 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25307 }
25308
25309 /* Add a dependence relationship from CU to REF_PER_CU. */
25310
25311 static void
25312 dwarf2_add_dependence (struct dwarf2_cu *cu,
25313 struct dwarf2_per_cu_data *ref_per_cu)
25314 {
25315 void **slot;
25316
25317 if (cu->dependencies == NULL)
25318 cu->dependencies
25319 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25320 NULL, &cu->comp_unit_obstack,
25321 hashtab_obstack_allocate,
25322 dummy_obstack_deallocate);
25323
25324 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25325 if (*slot == NULL)
25326 *slot = ref_per_cu;
25327 }
25328
25329 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25330 Set the mark field in every compilation unit in the
25331 cache that we must keep because we are keeping CU. */
25332
25333 static int
25334 dwarf2_mark_helper (void **slot, void *data)
25335 {
25336 struct dwarf2_per_cu_data *per_cu;
25337
25338 per_cu = (struct dwarf2_per_cu_data *) *slot;
25339
25340 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25341 reading of the chain. As such dependencies remain valid it is not much
25342 useful to track and undo them during QUIT cleanups. */
25343 if (per_cu->cu == NULL)
25344 return 1;
25345
25346 if (per_cu->cu->mark)
25347 return 1;
25348 per_cu->cu->mark = 1;
25349
25350 if (per_cu->cu->dependencies != NULL)
25351 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25352
25353 return 1;
25354 }
25355
25356 /* Set the mark field in CU and in every other compilation unit in the
25357 cache that we must keep because we are keeping CU. */
25358
25359 static void
25360 dwarf2_mark (struct dwarf2_cu *cu)
25361 {
25362 if (cu->mark)
25363 return;
25364 cu->mark = 1;
25365 if (cu->dependencies != NULL)
25366 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25367 }
25368
25369 static void
25370 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25371 {
25372 while (per_cu)
25373 {
25374 per_cu->cu->mark = 0;
25375 per_cu = per_cu->cu->read_in_chain;
25376 }
25377 }
25378
25379 /* Trivial hash function for partial_die_info: the hash value of a DIE
25380 is its offset in .debug_info for this objfile. */
25381
25382 static hashval_t
25383 partial_die_hash (const void *item)
25384 {
25385 const struct partial_die_info *part_die
25386 = (const struct partial_die_info *) item;
25387
25388 return to_underlying (part_die->sect_off);
25389 }
25390
25391 /* Trivial comparison function for partial_die_info structures: two DIEs
25392 are equal if they have the same offset. */
25393
25394 static int
25395 partial_die_eq (const void *item_lhs, const void *item_rhs)
25396 {
25397 const struct partial_die_info *part_die_lhs
25398 = (const struct partial_die_info *) item_lhs;
25399 const struct partial_die_info *part_die_rhs
25400 = (const struct partial_die_info *) item_rhs;
25401
25402 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25403 }
25404
25405 static struct cmd_list_element *set_dwarf_cmdlist;
25406 static struct cmd_list_element *show_dwarf_cmdlist;
25407
25408 static void
25409 set_dwarf_cmd (const char *args, int from_tty)
25410 {
25411 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25412 gdb_stdout);
25413 }
25414
25415 static void
25416 show_dwarf_cmd (const char *args, int from_tty)
25417 {
25418 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25419 }
25420
25421 int dwarf_always_disassemble;
25422
25423 static void
25424 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25425 struct cmd_list_element *c, const char *value)
25426 {
25427 fprintf_filtered (file,
25428 _("Whether to always disassemble "
25429 "DWARF expressions is %s.\n"),
25430 value);
25431 }
25432
25433 static void
25434 show_check_physname (struct ui_file *file, int from_tty,
25435 struct cmd_list_element *c, const char *value)
25436 {
25437 fprintf_filtered (file,
25438 _("Whether to check \"physname\" is %s.\n"),
25439 value);
25440 }
25441
25442 void
25443 _initialize_dwarf2_read (void)
25444 {
25445
25446 dwarf2_objfile_data_key = register_objfile_data ();
25447
25448 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25449 Set DWARF specific variables.\n\
25450 Configure DWARF variables such as the cache size"),
25451 &set_dwarf_cmdlist, "maintenance set dwarf ",
25452 0/*allow-unknown*/, &maintenance_set_cmdlist);
25453
25454 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25455 Show DWARF specific variables\n\
25456 Show DWARF variables such as the cache size"),
25457 &show_dwarf_cmdlist, "maintenance show dwarf ",
25458 0/*allow-unknown*/, &maintenance_show_cmdlist);
25459
25460 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25461 &dwarf_max_cache_age, _("\
25462 Set the upper bound on the age of cached DWARF compilation units."), _("\
25463 Show the upper bound on the age of cached DWARF compilation units."), _("\
25464 A higher limit means that cached compilation units will be stored\n\
25465 in memory longer, and more total memory will be used. Zero disables\n\
25466 caching, which can slow down startup."),
25467 NULL,
25468 show_dwarf_max_cache_age,
25469 &set_dwarf_cmdlist,
25470 &show_dwarf_cmdlist);
25471
25472 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25473 &dwarf_always_disassemble, _("\
25474 Set whether `info address' always disassembles DWARF expressions."), _("\
25475 Show whether `info address' always disassembles DWARF expressions."), _("\
25476 When enabled, DWARF expressions are always printed in an assembly-like\n\
25477 syntax. When disabled, expressions will be printed in a more\n\
25478 conversational style, when possible."),
25479 NULL,
25480 show_dwarf_always_disassemble,
25481 &set_dwarf_cmdlist,
25482 &show_dwarf_cmdlist);
25483
25484 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25485 Set debugging of the DWARF reader."), _("\
25486 Show debugging of the DWARF reader."), _("\
25487 When enabled (non-zero), debugging messages are printed during DWARF\n\
25488 reading and symtab expansion. A value of 1 (one) provides basic\n\
25489 information. A value greater than 1 provides more verbose information."),
25490 NULL,
25491 NULL,
25492 &setdebuglist, &showdebuglist);
25493
25494 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25495 Set debugging of the DWARF DIE reader."), _("\
25496 Show debugging of the DWARF DIE reader."), _("\
25497 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25498 The value is the maximum depth to print."),
25499 NULL,
25500 NULL,
25501 &setdebuglist, &showdebuglist);
25502
25503 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25504 Set debugging of the dwarf line reader."), _("\
25505 Show debugging of the dwarf line reader."), _("\
25506 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25507 A value of 1 (one) provides basic information.\n\
25508 A value greater than 1 provides more verbose information."),
25509 NULL,
25510 NULL,
25511 &setdebuglist, &showdebuglist);
25512
25513 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25514 Set cross-checking of \"physname\" code against demangler."), _("\
25515 Show cross-checking of \"physname\" code against demangler."), _("\
25516 When enabled, GDB's internal \"physname\" code is checked against\n\
25517 the demangler."),
25518 NULL, show_check_physname,
25519 &setdebuglist, &showdebuglist);
25520
25521 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25522 no_class, &use_deprecated_index_sections, _("\
25523 Set whether to use deprecated gdb_index sections."), _("\
25524 Show whether to use deprecated gdb_index sections."), _("\
25525 When enabled, deprecated .gdb_index sections are used anyway.\n\
25526 Normally they are ignored either because of a missing feature or\n\
25527 performance issue.\n\
25528 Warning: This option must be enabled before gdb reads the file."),
25529 NULL,
25530 NULL,
25531 &setlist, &showlist);
25532
25533 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25534 &dwarf2_locexpr_funcs);
25535 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25536 &dwarf2_loclist_funcs);
25537
25538 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25539 &dwarf2_block_frame_base_locexpr_funcs);
25540 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25541 &dwarf2_block_frame_base_loclist_funcs);
25542
25543 #if GDB_SELF_TEST
25544 selftests::register_test ("dw2_expand_symtabs_matching",
25545 selftests::dw2_expand_symtabs_matching::run_test);
25546 #endif
25547 }
This page took 0.513529 seconds and 5 git commands to generate.