Remove some unused variables in dwarf2read.c
[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 /* Return the CU/TU given its index.
2919
2920 This is intended for loops like:
2921
2922 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2923 + dwarf2_per_objfile->n_type_units); ++i)
2924 {
2925 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
2926
2927 ...;
2928 }
2929 */
2930
2931 static struct dwarf2_per_cu_data *
2932 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
2933 int index)
2934 {
2935 if (index >= dwarf2_per_objfile->n_comp_units)
2936 {
2937 index -= dwarf2_per_objfile->n_comp_units;
2938 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2939 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2940 }
2941
2942 return dwarf2_per_objfile->all_comp_units[index];
2943 }
2944
2945 /* Return the CU given its index.
2946 This differs from dw2_get_cutu in that it's for when you know INDEX
2947 refers to a CU. */
2948
2949 static struct dwarf2_per_cu_data *
2950 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
2951 {
2952 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
2953
2954 return dwarf2_per_objfile->all_comp_units[index];
2955 }
2956
2957 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2958 objfile_obstack, and constructed with the specified field
2959 values. */
2960
2961 static dwarf2_per_cu_data *
2962 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2963 struct dwarf2_section_info *section,
2964 int is_dwz,
2965 sect_offset sect_off, ULONGEST length)
2966 {
2967 struct objfile *objfile = dwarf2_per_objfile->objfile;
2968 dwarf2_per_cu_data *the_cu
2969 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2970 struct dwarf2_per_cu_data);
2971 the_cu->sect_off = sect_off;
2972 the_cu->length = length;
2973 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2974 the_cu->section = section;
2975 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2976 struct dwarf2_per_cu_quick_data);
2977 the_cu->is_dwz = is_dwz;
2978 return the_cu;
2979 }
2980
2981 /* A helper for create_cus_from_index that handles a given list of
2982 CUs. */
2983
2984 static void
2985 create_cus_from_index_list (struct objfile *objfile,
2986 const gdb_byte *cu_list, offset_type n_elements,
2987 struct dwarf2_section_info *section,
2988 int is_dwz,
2989 int base_offset)
2990 {
2991 offset_type i;
2992 struct dwarf2_per_objfile *dwarf2_per_objfile
2993 = get_dwarf2_per_objfile (objfile);
2994
2995 for (i = 0; i < n_elements; i += 2)
2996 {
2997 gdb_static_assert (sizeof (ULONGEST) >= 8);
2998
2999 sect_offset sect_off
3000 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3001 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3002 cu_list += 2 * 8;
3003
3004 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3005 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3006 sect_off, length);
3007 }
3008 }
3009
3010 /* Read the CU list from the mapped index, and use it to create all
3011 the CU objects for this objfile. */
3012
3013 static void
3014 create_cus_from_index (struct objfile *objfile,
3015 const gdb_byte *cu_list, offset_type cu_list_elements,
3016 const gdb_byte *dwz_list, offset_type dwz_elements)
3017 {
3018 struct dwz_file *dwz;
3019 struct dwarf2_per_objfile *dwarf2_per_objfile
3020 = get_dwarf2_per_objfile (objfile);
3021
3022 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3023 dwarf2_per_objfile->all_comp_units =
3024 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3025 dwarf2_per_objfile->n_comp_units);
3026
3027 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3028 &dwarf2_per_objfile->info, 0, 0);
3029
3030 if (dwz_elements == 0)
3031 return;
3032
3033 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3034 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3035 cu_list_elements / 2);
3036 }
3037
3038 /* Create the signatured type hash table from the index. */
3039
3040 static void
3041 create_signatured_type_table_from_index (struct objfile *objfile,
3042 struct dwarf2_section_info *section,
3043 const gdb_byte *bytes,
3044 offset_type elements)
3045 {
3046 offset_type i;
3047 htab_t sig_types_hash;
3048 struct dwarf2_per_objfile *dwarf2_per_objfile
3049 = get_dwarf2_per_objfile (objfile);
3050
3051 dwarf2_per_objfile->n_type_units
3052 = dwarf2_per_objfile->n_allocated_type_units
3053 = elements / 3;
3054 dwarf2_per_objfile->all_type_units =
3055 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3056
3057 sig_types_hash = allocate_signatured_type_table (objfile);
3058
3059 for (i = 0; i < elements; i += 3)
3060 {
3061 struct signatured_type *sig_type;
3062 ULONGEST signature;
3063 void **slot;
3064 cu_offset type_offset_in_tu;
3065
3066 gdb_static_assert (sizeof (ULONGEST) >= 8);
3067 sect_offset sect_off
3068 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3069 type_offset_in_tu
3070 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3071 BFD_ENDIAN_LITTLE);
3072 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3073 bytes += 3 * 8;
3074
3075 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3076 struct signatured_type);
3077 sig_type->signature = signature;
3078 sig_type->type_offset_in_tu = type_offset_in_tu;
3079 sig_type->per_cu.is_debug_types = 1;
3080 sig_type->per_cu.section = section;
3081 sig_type->per_cu.sect_off = sect_off;
3082 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3083 sig_type->per_cu.v.quick
3084 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3085 struct dwarf2_per_cu_quick_data);
3086
3087 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3088 *slot = sig_type;
3089
3090 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3091 }
3092
3093 dwarf2_per_objfile->signatured_types = sig_types_hash;
3094 }
3095
3096 /* Create the signatured type hash table from .debug_names. */
3097
3098 static void
3099 create_signatured_type_table_from_debug_names
3100 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3101 const mapped_debug_names &map,
3102 struct dwarf2_section_info *section,
3103 struct dwarf2_section_info *abbrev_section)
3104 {
3105 struct objfile *objfile = dwarf2_per_objfile->objfile;
3106
3107 dwarf2_read_section (objfile, section);
3108 dwarf2_read_section (objfile, abbrev_section);
3109
3110 dwarf2_per_objfile->n_type_units
3111 = dwarf2_per_objfile->n_allocated_type_units
3112 = map.tu_count;
3113 dwarf2_per_objfile->all_type_units
3114 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3115
3116 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3117
3118 for (uint32_t i = 0; i < map.tu_count; ++i)
3119 {
3120 struct signatured_type *sig_type;
3121 void **slot;
3122
3123 sect_offset sect_off
3124 = (sect_offset) (extract_unsigned_integer
3125 (map.tu_table_reordered + i * map.offset_size,
3126 map.offset_size,
3127 map.dwarf5_byte_order));
3128
3129 comp_unit_head cu_header;
3130 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3131 abbrev_section,
3132 section->buffer + to_underlying (sect_off),
3133 rcuh_kind::TYPE);
3134
3135 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3136 struct signatured_type);
3137 sig_type->signature = cu_header.signature;
3138 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3139 sig_type->per_cu.is_debug_types = 1;
3140 sig_type->per_cu.section = section;
3141 sig_type->per_cu.sect_off = sect_off;
3142 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3143 sig_type->per_cu.v.quick
3144 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3145 struct dwarf2_per_cu_quick_data);
3146
3147 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3148 *slot = sig_type;
3149
3150 dwarf2_per_objfile->all_type_units[i] = sig_type;
3151 }
3152
3153 dwarf2_per_objfile->signatured_types = sig_types_hash;
3154 }
3155
3156 /* Read the address map data from the mapped index, and use it to
3157 populate the objfile's psymtabs_addrmap. */
3158
3159 static void
3160 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3161 struct mapped_index *index)
3162 {
3163 struct objfile *objfile = dwarf2_per_objfile->objfile;
3164 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3165 const gdb_byte *iter, *end;
3166 struct addrmap *mutable_map;
3167 CORE_ADDR baseaddr;
3168
3169 auto_obstack temp_obstack;
3170
3171 mutable_map = addrmap_create_mutable (&temp_obstack);
3172
3173 iter = index->address_table.data ();
3174 end = iter + index->address_table.size ();
3175
3176 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3177
3178 while (iter < end)
3179 {
3180 ULONGEST hi, lo, cu_index;
3181 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3182 iter += 8;
3183 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3184 iter += 8;
3185 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3186 iter += 4;
3187
3188 if (lo > hi)
3189 {
3190 complaint (&symfile_complaints,
3191 _(".gdb_index address table has invalid range (%s - %s)"),
3192 hex_string (lo), hex_string (hi));
3193 continue;
3194 }
3195
3196 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3197 {
3198 complaint (&symfile_complaints,
3199 _(".gdb_index address table has invalid CU number %u"),
3200 (unsigned) cu_index);
3201 continue;
3202 }
3203
3204 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3205 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3206 addrmap_set_empty (mutable_map, lo, hi - 1,
3207 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3208 }
3209
3210 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3211 &objfile->objfile_obstack);
3212 }
3213
3214 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3215 populate the objfile's psymtabs_addrmap. */
3216
3217 static void
3218 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3219 struct dwarf2_section_info *section)
3220 {
3221 struct objfile *objfile = dwarf2_per_objfile->objfile;
3222 bfd *abfd = objfile->obfd;
3223 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3224 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3225 SECT_OFF_TEXT (objfile));
3226
3227 auto_obstack temp_obstack;
3228 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3229
3230 std::unordered_map<sect_offset,
3231 dwarf2_per_cu_data *,
3232 gdb::hash_enum<sect_offset>>
3233 debug_info_offset_to_per_cu;
3234 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3235 {
3236 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3237 const auto insertpair
3238 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3239 if (!insertpair.second)
3240 {
3241 warning (_("Section .debug_aranges in %s has duplicate "
3242 "debug_info_offset %s, ignoring .debug_aranges."),
3243 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3244 return;
3245 }
3246 }
3247
3248 dwarf2_read_section (objfile, section);
3249
3250 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3251
3252 const gdb_byte *addr = section->buffer;
3253
3254 while (addr < section->buffer + section->size)
3255 {
3256 const gdb_byte *const entry_addr = addr;
3257 unsigned int bytes_read;
3258
3259 const LONGEST entry_length = read_initial_length (abfd, addr,
3260 &bytes_read);
3261 addr += bytes_read;
3262
3263 const gdb_byte *const entry_end = addr + entry_length;
3264 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3265 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3266 if (addr + entry_length > section->buffer + section->size)
3267 {
3268 warning (_("Section .debug_aranges in %s entry at offset %zu "
3269 "length %s exceeds section length %s, "
3270 "ignoring .debug_aranges."),
3271 objfile_name (objfile), entry_addr - section->buffer,
3272 plongest (bytes_read + entry_length),
3273 pulongest (section->size));
3274 return;
3275 }
3276
3277 /* The version number. */
3278 const uint16_t version = read_2_bytes (abfd, addr);
3279 addr += 2;
3280 if (version != 2)
3281 {
3282 warning (_("Section .debug_aranges in %s entry at offset %zu "
3283 "has unsupported version %d, ignoring .debug_aranges."),
3284 objfile_name (objfile), entry_addr - section->buffer,
3285 version);
3286 return;
3287 }
3288
3289 const uint64_t debug_info_offset
3290 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3291 addr += offset_size;
3292 const auto per_cu_it
3293 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3294 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3295 {
3296 warning (_("Section .debug_aranges in %s entry at offset %zu "
3297 "debug_info_offset %s does not exists, "
3298 "ignoring .debug_aranges."),
3299 objfile_name (objfile), entry_addr - section->buffer,
3300 pulongest (debug_info_offset));
3301 return;
3302 }
3303 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3304
3305 const uint8_t address_size = *addr++;
3306 if (address_size < 1 || address_size > 8)
3307 {
3308 warning (_("Section .debug_aranges in %s entry at offset %zu "
3309 "address_size %u is invalid, ignoring .debug_aranges."),
3310 objfile_name (objfile), entry_addr - section->buffer,
3311 address_size);
3312 return;
3313 }
3314
3315 const uint8_t segment_selector_size = *addr++;
3316 if (segment_selector_size != 0)
3317 {
3318 warning (_("Section .debug_aranges in %s entry at offset %zu "
3319 "segment_selector_size %u is not supported, "
3320 "ignoring .debug_aranges."),
3321 objfile_name (objfile), entry_addr - section->buffer,
3322 segment_selector_size);
3323 return;
3324 }
3325
3326 /* Must pad to an alignment boundary that is twice the address
3327 size. It is undocumented by the DWARF standard but GCC does
3328 use it. */
3329 for (size_t padding = ((-(addr - section->buffer))
3330 & (2 * address_size - 1));
3331 padding > 0; padding--)
3332 if (*addr++ != 0)
3333 {
3334 warning (_("Section .debug_aranges in %s entry at offset %zu "
3335 "padding is not zero, ignoring .debug_aranges."),
3336 objfile_name (objfile), entry_addr - section->buffer);
3337 return;
3338 }
3339
3340 for (;;)
3341 {
3342 if (addr + 2 * address_size > entry_end)
3343 {
3344 warning (_("Section .debug_aranges in %s entry at offset %zu "
3345 "address list is not properly terminated, "
3346 "ignoring .debug_aranges."),
3347 objfile_name (objfile), entry_addr - section->buffer);
3348 return;
3349 }
3350 ULONGEST start = extract_unsigned_integer (addr, address_size,
3351 dwarf5_byte_order);
3352 addr += address_size;
3353 ULONGEST length = extract_unsigned_integer (addr, address_size,
3354 dwarf5_byte_order);
3355 addr += address_size;
3356 if (start == 0 && length == 0)
3357 break;
3358 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3359 {
3360 /* Symbol was eliminated due to a COMDAT group. */
3361 continue;
3362 }
3363 ULONGEST end = start + length;
3364 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3365 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3366 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3367 }
3368 }
3369
3370 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3371 &objfile->objfile_obstack);
3372 }
3373
3374 /* Find a slot in the mapped index INDEX for the object named NAME.
3375 If NAME is found, set *VEC_OUT to point to the CU vector in the
3376 constant pool and return true. If NAME cannot be found, return
3377 false. */
3378
3379 static bool
3380 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3381 offset_type **vec_out)
3382 {
3383 offset_type hash;
3384 offset_type slot, step;
3385 int (*cmp) (const char *, const char *);
3386
3387 gdb::unique_xmalloc_ptr<char> without_params;
3388 if (current_language->la_language == language_cplus
3389 || current_language->la_language == language_fortran
3390 || current_language->la_language == language_d)
3391 {
3392 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3393 not contain any. */
3394
3395 if (strchr (name, '(') != NULL)
3396 {
3397 without_params = cp_remove_params (name);
3398
3399 if (without_params != NULL)
3400 name = without_params.get ();
3401 }
3402 }
3403
3404 /* Index version 4 did not support case insensitive searches. But the
3405 indices for case insensitive languages are built in lowercase, therefore
3406 simulate our NAME being searched is also lowercased. */
3407 hash = mapped_index_string_hash ((index->version == 4
3408 && case_sensitivity == case_sensitive_off
3409 ? 5 : index->version),
3410 name);
3411
3412 slot = hash & (index->symbol_table.size () - 1);
3413 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3414 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3415
3416 for (;;)
3417 {
3418 const char *str;
3419
3420 const auto &bucket = index->symbol_table[slot];
3421 if (bucket.name == 0 && bucket.vec == 0)
3422 return false;
3423
3424 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3425 if (!cmp (name, str))
3426 {
3427 *vec_out = (offset_type *) (index->constant_pool
3428 + MAYBE_SWAP (bucket.vec));
3429 return true;
3430 }
3431
3432 slot = (slot + step) & (index->symbol_table.size () - 1);
3433 }
3434 }
3435
3436 /* A helper function that reads the .gdb_index from SECTION and fills
3437 in MAP. FILENAME is the name of the file containing the section;
3438 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3439 ok to use deprecated sections.
3440
3441 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3442 out parameters that are filled in with information about the CU and
3443 TU lists in the section.
3444
3445 Returns 1 if all went well, 0 otherwise. */
3446
3447 static int
3448 read_index_from_section (struct objfile *objfile,
3449 const char *filename,
3450 int deprecated_ok,
3451 struct dwarf2_section_info *section,
3452 struct mapped_index *map,
3453 const gdb_byte **cu_list,
3454 offset_type *cu_list_elements,
3455 const gdb_byte **types_list,
3456 offset_type *types_list_elements)
3457 {
3458 const gdb_byte *addr;
3459 offset_type version;
3460 offset_type *metadata;
3461 int i;
3462
3463 if (dwarf2_section_empty_p (section))
3464 return 0;
3465
3466 /* Older elfutils strip versions could keep the section in the main
3467 executable while splitting it for the separate debug info file. */
3468 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3469 return 0;
3470
3471 dwarf2_read_section (objfile, section);
3472
3473 addr = section->buffer;
3474 /* Version check. */
3475 version = MAYBE_SWAP (*(offset_type *) addr);
3476 /* Versions earlier than 3 emitted every copy of a psymbol. This
3477 causes the index to behave very poorly for certain requests. Version 3
3478 contained incomplete addrmap. So, it seems better to just ignore such
3479 indices. */
3480 if (version < 4)
3481 {
3482 static int warning_printed = 0;
3483 if (!warning_printed)
3484 {
3485 warning (_("Skipping obsolete .gdb_index section in %s."),
3486 filename);
3487 warning_printed = 1;
3488 }
3489 return 0;
3490 }
3491 /* Index version 4 uses a different hash function than index version
3492 5 and later.
3493
3494 Versions earlier than 6 did not emit psymbols for inlined
3495 functions. Using these files will cause GDB not to be able to
3496 set breakpoints on inlined functions by name, so we ignore these
3497 indices unless the user has done
3498 "set use-deprecated-index-sections on". */
3499 if (version < 6 && !deprecated_ok)
3500 {
3501 static int warning_printed = 0;
3502 if (!warning_printed)
3503 {
3504 warning (_("\
3505 Skipping deprecated .gdb_index section in %s.\n\
3506 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3507 to use the section anyway."),
3508 filename);
3509 warning_printed = 1;
3510 }
3511 return 0;
3512 }
3513 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3514 of the TU (for symbols coming from TUs),
3515 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3516 Plus gold-generated indices can have duplicate entries for global symbols,
3517 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3518 These are just performance bugs, and we can't distinguish gdb-generated
3519 indices from gold-generated ones, so issue no warning here. */
3520
3521 /* Indexes with higher version than the one supported by GDB may be no
3522 longer backward compatible. */
3523 if (version > 8)
3524 return 0;
3525
3526 map->version = version;
3527 map->total_size = section->size;
3528
3529 metadata = (offset_type *) (addr + sizeof (offset_type));
3530
3531 i = 0;
3532 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3533 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3534 / 8);
3535 ++i;
3536
3537 *types_list = addr + MAYBE_SWAP (metadata[i]);
3538 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3539 - MAYBE_SWAP (metadata[i]))
3540 / 8);
3541 ++i;
3542
3543 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3544 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3545 map->address_table
3546 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3547 ++i;
3548
3549 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3550 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3551 map->symbol_table
3552 = gdb::array_view<mapped_index::symbol_table_slot>
3553 ((mapped_index::symbol_table_slot *) symbol_table,
3554 (mapped_index::symbol_table_slot *) symbol_table_end);
3555
3556 ++i;
3557 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3558
3559 return 1;
3560 }
3561
3562 /* Read .gdb_index. If everything went ok, initialize the "quick"
3563 elements of all the CUs and return 1. Otherwise, return 0. */
3564
3565 static int
3566 dwarf2_read_index (struct objfile *objfile)
3567 {
3568 struct mapped_index local_map, *map;
3569 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3570 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3571 struct dwz_file *dwz;
3572 struct dwarf2_per_objfile *dwarf2_per_objfile
3573 = get_dwarf2_per_objfile (objfile);
3574
3575 if (!read_index_from_section (objfile, objfile_name (objfile),
3576 use_deprecated_index_sections,
3577 &dwarf2_per_objfile->gdb_index, &local_map,
3578 &cu_list, &cu_list_elements,
3579 &types_list, &types_list_elements))
3580 return 0;
3581
3582 /* Don't use the index if it's empty. */
3583 if (local_map.symbol_table.empty ())
3584 return 0;
3585
3586 /* If there is a .dwz file, read it so we can get its CU list as
3587 well. */
3588 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3589 if (dwz != NULL)
3590 {
3591 struct mapped_index dwz_map;
3592 const gdb_byte *dwz_types_ignore;
3593 offset_type dwz_types_elements_ignore;
3594
3595 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3596 1,
3597 &dwz->gdb_index, &dwz_map,
3598 &dwz_list, &dwz_list_elements,
3599 &dwz_types_ignore,
3600 &dwz_types_elements_ignore))
3601 {
3602 warning (_("could not read '.gdb_index' section from %s; skipping"),
3603 bfd_get_filename (dwz->dwz_bfd));
3604 return 0;
3605 }
3606 }
3607
3608 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3609 dwz_list_elements);
3610
3611 if (types_list_elements)
3612 {
3613 struct dwarf2_section_info *section;
3614
3615 /* We can only handle a single .debug_types when we have an
3616 index. */
3617 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3618 return 0;
3619
3620 section = VEC_index (dwarf2_section_info_def,
3621 dwarf2_per_objfile->types, 0);
3622
3623 create_signatured_type_table_from_index (objfile, section, types_list,
3624 types_list_elements);
3625 }
3626
3627 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
3628
3629 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3630 map = new (map) mapped_index ();
3631 *map = local_map;
3632
3633 dwarf2_per_objfile->index_table = map;
3634 dwarf2_per_objfile->using_index = 1;
3635 dwarf2_per_objfile->quick_file_names_table =
3636 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3637
3638 return 1;
3639 }
3640
3641 /* die_reader_func for dw2_get_file_names. */
3642
3643 static void
3644 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3645 const gdb_byte *info_ptr,
3646 struct die_info *comp_unit_die,
3647 int has_children,
3648 void *data)
3649 {
3650 struct dwarf2_cu *cu = reader->cu;
3651 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3652 struct dwarf2_per_objfile *dwarf2_per_objfile
3653 = cu->per_cu->dwarf2_per_objfile;
3654 struct objfile *objfile = dwarf2_per_objfile->objfile;
3655 struct dwarf2_per_cu_data *lh_cu;
3656 struct attribute *attr;
3657 int i;
3658 void **slot;
3659 struct quick_file_names *qfn;
3660
3661 gdb_assert (! this_cu->is_debug_types);
3662
3663 /* Our callers never want to match partial units -- instead they
3664 will match the enclosing full CU. */
3665 if (comp_unit_die->tag == DW_TAG_partial_unit)
3666 {
3667 this_cu->v.quick->no_file_data = 1;
3668 return;
3669 }
3670
3671 lh_cu = this_cu;
3672 slot = NULL;
3673
3674 line_header_up lh;
3675 sect_offset line_offset {};
3676
3677 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3678 if (attr)
3679 {
3680 struct quick_file_names find_entry;
3681
3682 line_offset = (sect_offset) DW_UNSND (attr);
3683
3684 /* We may have already read in this line header (TU line header sharing).
3685 If we have we're done. */
3686 find_entry.hash.dwo_unit = cu->dwo_unit;
3687 find_entry.hash.line_sect_off = line_offset;
3688 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3689 &find_entry, INSERT);
3690 if (*slot != NULL)
3691 {
3692 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3693 return;
3694 }
3695
3696 lh = dwarf_decode_line_header (line_offset, cu);
3697 }
3698 if (lh == NULL)
3699 {
3700 lh_cu->v.quick->no_file_data = 1;
3701 return;
3702 }
3703
3704 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3705 qfn->hash.dwo_unit = cu->dwo_unit;
3706 qfn->hash.line_sect_off = line_offset;
3707 gdb_assert (slot != NULL);
3708 *slot = qfn;
3709
3710 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3711
3712 qfn->num_file_names = lh->file_names.size ();
3713 qfn->file_names =
3714 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3715 for (i = 0; i < lh->file_names.size (); ++i)
3716 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3717 qfn->real_names = NULL;
3718
3719 lh_cu->v.quick->file_names = qfn;
3720 }
3721
3722 /* A helper for the "quick" functions which attempts to read the line
3723 table for THIS_CU. */
3724
3725 static struct quick_file_names *
3726 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3727 {
3728 /* This should never be called for TUs. */
3729 gdb_assert (! this_cu->is_debug_types);
3730 /* Nor type unit groups. */
3731 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3732
3733 if (this_cu->v.quick->file_names != NULL)
3734 return this_cu->v.quick->file_names;
3735 /* If we know there is no line data, no point in looking again. */
3736 if (this_cu->v.quick->no_file_data)
3737 return NULL;
3738
3739 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3740
3741 if (this_cu->v.quick->no_file_data)
3742 return NULL;
3743 return this_cu->v.quick->file_names;
3744 }
3745
3746 /* A helper for the "quick" functions which computes and caches the
3747 real path for a given file name from the line table. */
3748
3749 static const char *
3750 dw2_get_real_path (struct objfile *objfile,
3751 struct quick_file_names *qfn, int index)
3752 {
3753 if (qfn->real_names == NULL)
3754 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3755 qfn->num_file_names, const char *);
3756
3757 if (qfn->real_names[index] == NULL)
3758 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3759
3760 return qfn->real_names[index];
3761 }
3762
3763 static struct symtab *
3764 dw2_find_last_source_symtab (struct objfile *objfile)
3765 {
3766 struct dwarf2_per_objfile *dwarf2_per_objfile
3767 = get_dwarf2_per_objfile (objfile);
3768 int index = dwarf2_per_objfile->n_comp_units - 1;
3769 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
3770 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
3771
3772 if (cust == NULL)
3773 return NULL;
3774
3775 return compunit_primary_filetab (cust);
3776 }
3777
3778 /* Traversal function for dw2_forget_cached_source_info. */
3779
3780 static int
3781 dw2_free_cached_file_names (void **slot, void *info)
3782 {
3783 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3784
3785 if (file_data->real_names)
3786 {
3787 int i;
3788
3789 for (i = 0; i < file_data->num_file_names; ++i)
3790 {
3791 xfree ((void*) file_data->real_names[i]);
3792 file_data->real_names[i] = NULL;
3793 }
3794 }
3795
3796 return 1;
3797 }
3798
3799 static void
3800 dw2_forget_cached_source_info (struct objfile *objfile)
3801 {
3802 struct dwarf2_per_objfile *dwarf2_per_objfile
3803 = get_dwarf2_per_objfile (objfile);
3804
3805 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3806 dw2_free_cached_file_names, NULL);
3807 }
3808
3809 /* Helper function for dw2_map_symtabs_matching_filename that expands
3810 the symtabs and calls the iterator. */
3811
3812 static int
3813 dw2_map_expand_apply (struct objfile *objfile,
3814 struct dwarf2_per_cu_data *per_cu,
3815 const char *name, const char *real_path,
3816 gdb::function_view<bool (symtab *)> callback)
3817 {
3818 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3819
3820 /* Don't visit already-expanded CUs. */
3821 if (per_cu->v.quick->compunit_symtab)
3822 return 0;
3823
3824 /* This may expand more than one symtab, and we want to iterate over
3825 all of them. */
3826 dw2_instantiate_symtab (per_cu);
3827
3828 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3829 last_made, callback);
3830 }
3831
3832 /* Implementation of the map_symtabs_matching_filename method. */
3833
3834 static bool
3835 dw2_map_symtabs_matching_filename
3836 (struct objfile *objfile, const char *name, const char *real_path,
3837 gdb::function_view<bool (symtab *)> callback)
3838 {
3839 const char *name_basename = lbasename (name);
3840 struct dwarf2_per_objfile *dwarf2_per_objfile
3841 = get_dwarf2_per_objfile (objfile);
3842
3843 /* The rule is CUs specify all the files, including those used by
3844 any TU, so there's no need to scan TUs here. */
3845
3846 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3847 {
3848 int j;
3849 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
3850 struct quick_file_names *file_data;
3851
3852 /* We only need to look at symtabs not already expanded. */
3853 if (per_cu->v.quick->compunit_symtab)
3854 continue;
3855
3856 file_data = dw2_get_file_names (per_cu);
3857 if (file_data == NULL)
3858 continue;
3859
3860 for (j = 0; j < file_data->num_file_names; ++j)
3861 {
3862 const char *this_name = file_data->file_names[j];
3863 const char *this_real_name;
3864
3865 if (compare_filenames_for_search (this_name, name))
3866 {
3867 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3868 callback))
3869 return true;
3870 continue;
3871 }
3872
3873 /* Before we invoke realpath, which can get expensive when many
3874 files are involved, do a quick comparison of the basenames. */
3875 if (! basenames_may_differ
3876 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3877 continue;
3878
3879 this_real_name = dw2_get_real_path (objfile, file_data, j);
3880 if (compare_filenames_for_search (this_real_name, name))
3881 {
3882 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3883 callback))
3884 return true;
3885 continue;
3886 }
3887
3888 if (real_path != NULL)
3889 {
3890 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3891 gdb_assert (IS_ABSOLUTE_PATH (name));
3892 if (this_real_name != NULL
3893 && FILENAME_CMP (real_path, this_real_name) == 0)
3894 {
3895 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3896 callback))
3897 return true;
3898 continue;
3899 }
3900 }
3901 }
3902 }
3903
3904 return false;
3905 }
3906
3907 /* Struct used to manage iterating over all CUs looking for a symbol. */
3908
3909 struct dw2_symtab_iterator
3910 {
3911 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3912 struct dwarf2_per_objfile *dwarf2_per_objfile;
3913 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3914 int want_specific_block;
3915 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3916 Unused if !WANT_SPECIFIC_BLOCK. */
3917 int block_index;
3918 /* The kind of symbol we're looking for. */
3919 domain_enum domain;
3920 /* The list of CUs from the index entry of the symbol,
3921 or NULL if not found. */
3922 offset_type *vec;
3923 /* The next element in VEC to look at. */
3924 int next;
3925 /* The number of elements in VEC, or zero if there is no match. */
3926 int length;
3927 /* Have we seen a global version of the symbol?
3928 If so we can ignore all further global instances.
3929 This is to work around gold/15646, inefficient gold-generated
3930 indices. */
3931 int global_seen;
3932 };
3933
3934 /* Initialize the index symtab iterator ITER.
3935 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3936 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3937
3938 static void
3939 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3940 struct dwarf2_per_objfile *dwarf2_per_objfile,
3941 int want_specific_block,
3942 int block_index,
3943 domain_enum domain,
3944 const char *name)
3945 {
3946 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3947 iter->want_specific_block = want_specific_block;
3948 iter->block_index = block_index;
3949 iter->domain = domain;
3950 iter->next = 0;
3951 iter->global_seen = 0;
3952
3953 mapped_index *index = dwarf2_per_objfile->index_table;
3954
3955 /* index is NULL if OBJF_READNOW. */
3956 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3957 iter->length = MAYBE_SWAP (*iter->vec);
3958 else
3959 {
3960 iter->vec = NULL;
3961 iter->length = 0;
3962 }
3963 }
3964
3965 /* Return the next matching CU or NULL if there are no more. */
3966
3967 static struct dwarf2_per_cu_data *
3968 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3969 {
3970 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3971
3972 for ( ; iter->next < iter->length; ++iter->next)
3973 {
3974 offset_type cu_index_and_attrs =
3975 MAYBE_SWAP (iter->vec[iter->next + 1]);
3976 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3977 struct dwarf2_per_cu_data *per_cu;
3978 int want_static = iter->block_index != GLOBAL_BLOCK;
3979 /* This value is only valid for index versions >= 7. */
3980 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3981 gdb_index_symbol_kind symbol_kind =
3982 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3983 /* Only check the symbol attributes if they're present.
3984 Indices prior to version 7 don't record them,
3985 and indices >= 7 may elide them for certain symbols
3986 (gold does this). */
3987 int attrs_valid =
3988 (dwarf2_per_objfile->index_table->version >= 7
3989 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3990
3991 /* Don't crash on bad data. */
3992 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3993 + dwarf2_per_objfile->n_type_units))
3994 {
3995 complaint (&symfile_complaints,
3996 _(".gdb_index entry has bad CU index"
3997 " [in module %s]"),
3998 objfile_name (dwarf2_per_objfile->objfile));
3999 continue;
4000 }
4001
4002 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4003
4004 /* Skip if already read in. */
4005 if (per_cu->v.quick->compunit_symtab)
4006 continue;
4007
4008 /* Check static vs global. */
4009 if (attrs_valid)
4010 {
4011 if (iter->want_specific_block
4012 && want_static != is_static)
4013 continue;
4014 /* Work around gold/15646. */
4015 if (!is_static && iter->global_seen)
4016 continue;
4017 if (!is_static)
4018 iter->global_seen = 1;
4019 }
4020
4021 /* Only check the symbol's kind if it has one. */
4022 if (attrs_valid)
4023 {
4024 switch (iter->domain)
4025 {
4026 case VAR_DOMAIN:
4027 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4028 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4029 /* Some types are also in VAR_DOMAIN. */
4030 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4031 continue;
4032 break;
4033 case STRUCT_DOMAIN:
4034 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4035 continue;
4036 break;
4037 case LABEL_DOMAIN:
4038 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4039 continue;
4040 break;
4041 default:
4042 break;
4043 }
4044 }
4045
4046 ++iter->next;
4047 return per_cu;
4048 }
4049
4050 return NULL;
4051 }
4052
4053 static struct compunit_symtab *
4054 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4055 const char *name, domain_enum domain)
4056 {
4057 struct compunit_symtab *stab_best = NULL;
4058 struct dwarf2_per_objfile *dwarf2_per_objfile
4059 = get_dwarf2_per_objfile (objfile);
4060
4061 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4062
4063 struct dw2_symtab_iterator iter;
4064 struct dwarf2_per_cu_data *per_cu;
4065
4066 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4067
4068 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4069 {
4070 struct symbol *sym, *with_opaque = NULL;
4071 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4072 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4073 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4074
4075 sym = block_find_symbol (block, name, domain,
4076 block_find_non_opaque_type_preferred,
4077 &with_opaque);
4078
4079 /* Some caution must be observed with overloaded functions
4080 and methods, since the index will not contain any overload
4081 information (but NAME might contain it). */
4082
4083 if (sym != NULL
4084 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4085 return stab;
4086 if (with_opaque != NULL
4087 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4088 stab_best = stab;
4089
4090 /* Keep looking through other CUs. */
4091 }
4092
4093 return stab_best;
4094 }
4095
4096 static void
4097 dw2_print_stats (struct objfile *objfile)
4098 {
4099 struct dwarf2_per_objfile *dwarf2_per_objfile
4100 = get_dwarf2_per_objfile (objfile);
4101 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4102 int count = 0;
4103
4104 for (int i = 0; i < total; ++i)
4105 {
4106 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4107
4108 if (!per_cu->v.quick->compunit_symtab)
4109 ++count;
4110 }
4111 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4112 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4113 }
4114
4115 /* This dumps minimal information about the index.
4116 It is called via "mt print objfiles".
4117 One use is to verify .gdb_index has been loaded by the
4118 gdb.dwarf2/gdb-index.exp testcase. */
4119
4120 static void
4121 dw2_dump (struct objfile *objfile)
4122 {
4123 struct dwarf2_per_objfile *dwarf2_per_objfile
4124 = get_dwarf2_per_objfile (objfile);
4125
4126 gdb_assert (dwarf2_per_objfile->using_index);
4127 printf_filtered (".gdb_index:");
4128 if (dwarf2_per_objfile->index_table != NULL)
4129 {
4130 printf_filtered (" version %d\n",
4131 dwarf2_per_objfile->index_table->version);
4132 }
4133 else
4134 printf_filtered (" faked for \"readnow\"\n");
4135 printf_filtered ("\n");
4136 }
4137
4138 static void
4139 dw2_relocate (struct objfile *objfile,
4140 const struct section_offsets *new_offsets,
4141 const struct section_offsets *delta)
4142 {
4143 /* There's nothing to relocate here. */
4144 }
4145
4146 static void
4147 dw2_expand_symtabs_for_function (struct objfile *objfile,
4148 const char *func_name)
4149 {
4150 struct dwarf2_per_objfile *dwarf2_per_objfile
4151 = get_dwarf2_per_objfile (objfile);
4152
4153 struct dw2_symtab_iterator iter;
4154 struct dwarf2_per_cu_data *per_cu;
4155
4156 /* Note: It doesn't matter what we pass for block_index here. */
4157 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4158 func_name);
4159
4160 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4161 dw2_instantiate_symtab (per_cu);
4162
4163 }
4164
4165 static void
4166 dw2_expand_all_symtabs (struct objfile *objfile)
4167 {
4168 struct dwarf2_per_objfile *dwarf2_per_objfile
4169 = get_dwarf2_per_objfile (objfile);
4170 int total_units = (dwarf2_per_objfile->n_comp_units
4171 + dwarf2_per_objfile->n_type_units);
4172
4173 for (int i = 0; i < total_units; ++i)
4174 {
4175 struct dwarf2_per_cu_data *per_cu
4176 = dw2_get_cutu (dwarf2_per_objfile, i);
4177
4178 dw2_instantiate_symtab (per_cu);
4179 }
4180 }
4181
4182 static void
4183 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4184 const char *fullname)
4185 {
4186 struct dwarf2_per_objfile *dwarf2_per_objfile
4187 = get_dwarf2_per_objfile (objfile);
4188
4189 /* We don't need to consider type units here.
4190 This is only called for examining code, e.g. expand_line_sal.
4191 There can be an order of magnitude (or more) more type units
4192 than comp units, and we avoid them if we can. */
4193
4194 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4195 {
4196 int j;
4197 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4198 struct quick_file_names *file_data;
4199
4200 /* We only need to look at symtabs not already expanded. */
4201 if (per_cu->v.quick->compunit_symtab)
4202 continue;
4203
4204 file_data = dw2_get_file_names (per_cu);
4205 if (file_data == NULL)
4206 continue;
4207
4208 for (j = 0; j < file_data->num_file_names; ++j)
4209 {
4210 const char *this_fullname = file_data->file_names[j];
4211
4212 if (filename_cmp (this_fullname, fullname) == 0)
4213 {
4214 dw2_instantiate_symtab (per_cu);
4215 break;
4216 }
4217 }
4218 }
4219 }
4220
4221 static void
4222 dw2_map_matching_symbols (struct objfile *objfile,
4223 const char * name, domain_enum domain,
4224 int global,
4225 int (*callback) (struct block *,
4226 struct symbol *, void *),
4227 void *data, symbol_name_match_type match,
4228 symbol_compare_ftype *ordered_compare)
4229 {
4230 /* Currently unimplemented; used for Ada. The function can be called if the
4231 current language is Ada for a non-Ada objfile using GNU index. As Ada
4232 does not look for non-Ada symbols this function should just return. */
4233 }
4234
4235 /* Symbol name matcher for .gdb_index names.
4236
4237 Symbol names in .gdb_index have a few particularities:
4238
4239 - There's no indication of which is the language of each symbol.
4240
4241 Since each language has its own symbol name matching algorithm,
4242 and we don't know which language is the right one, we must match
4243 each symbol against all languages. This would be a potential
4244 performance problem if it were not mitigated by the
4245 mapped_index::name_components lookup table, which significantly
4246 reduces the number of times we need to call into this matcher,
4247 making it a non-issue.
4248
4249 - Symbol names in the index have no overload (parameter)
4250 information. I.e., in C++, "foo(int)" and "foo(long)" both
4251 appear as "foo" in the index, for example.
4252
4253 This means that the lookup names passed to the symbol name
4254 matcher functions must have no parameter information either
4255 because (e.g.) symbol search name "foo" does not match
4256 lookup-name "foo(int)" [while swapping search name for lookup
4257 name would match].
4258 */
4259 class gdb_index_symbol_name_matcher
4260 {
4261 public:
4262 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4263 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4264
4265 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4266 Returns true if any matcher matches. */
4267 bool matches (const char *symbol_name);
4268
4269 private:
4270 /* A reference to the lookup name we're matching against. */
4271 const lookup_name_info &m_lookup_name;
4272
4273 /* A vector holding all the different symbol name matchers, for all
4274 languages. */
4275 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4276 };
4277
4278 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4279 (const lookup_name_info &lookup_name)
4280 : m_lookup_name (lookup_name)
4281 {
4282 /* Prepare the vector of comparison functions upfront, to avoid
4283 doing the same work for each symbol. Care is taken to avoid
4284 matching with the same matcher more than once if/when multiple
4285 languages use the same matcher function. */
4286 auto &matchers = m_symbol_name_matcher_funcs;
4287 matchers.reserve (nr_languages);
4288
4289 matchers.push_back (default_symbol_name_matcher);
4290
4291 for (int i = 0; i < nr_languages; i++)
4292 {
4293 const language_defn *lang = language_def ((enum language) i);
4294 symbol_name_matcher_ftype *name_matcher
4295 = get_symbol_name_matcher (lang, m_lookup_name);
4296
4297 /* Don't insert the same comparison routine more than once.
4298 Note that we do this linear walk instead of a seemingly
4299 cheaper sorted insert, or use a std::set or something like
4300 that, because relative order of function addresses is not
4301 stable. This is not a problem in practice because the number
4302 of supported languages is low, and the cost here is tiny
4303 compared to the number of searches we'll do afterwards using
4304 this object. */
4305 if (name_matcher != default_symbol_name_matcher
4306 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4307 == matchers.end ()))
4308 matchers.push_back (name_matcher);
4309 }
4310 }
4311
4312 bool
4313 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4314 {
4315 for (auto matches_name : m_symbol_name_matcher_funcs)
4316 if (matches_name (symbol_name, m_lookup_name, NULL))
4317 return true;
4318
4319 return false;
4320 }
4321
4322 /* Starting from a search name, return the string that finds the upper
4323 bound of all strings that start with SEARCH_NAME in a sorted name
4324 list. Returns the empty string to indicate that the upper bound is
4325 the end of the list. */
4326
4327 static std::string
4328 make_sort_after_prefix_name (const char *search_name)
4329 {
4330 /* When looking to complete "func", we find the upper bound of all
4331 symbols that start with "func" by looking for where we'd insert
4332 the closest string that would follow "func" in lexicographical
4333 order. Usually, that's "func"-with-last-character-incremented,
4334 i.e. "fund". Mind non-ASCII characters, though. Usually those
4335 will be UTF-8 multi-byte sequences, but we can't be certain.
4336 Especially mind the 0xff character, which is a valid character in
4337 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4338 rule out compilers allowing it in identifiers. Note that
4339 conveniently, strcmp/strcasecmp are specified to compare
4340 characters interpreted as unsigned char. So what we do is treat
4341 the whole string as a base 256 number composed of a sequence of
4342 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4343 to 0, and carries 1 to the following more-significant position.
4344 If the very first character in SEARCH_NAME ends up incremented
4345 and carries/overflows, then the upper bound is the end of the
4346 list. The string after the empty string is also the empty
4347 string.
4348
4349 Some examples of this operation:
4350
4351 SEARCH_NAME => "+1" RESULT
4352
4353 "abc" => "abd"
4354 "ab\xff" => "ac"
4355 "\xff" "a" "\xff" => "\xff" "b"
4356 "\xff" => ""
4357 "\xff\xff" => ""
4358 "" => ""
4359
4360 Then, with these symbols for example:
4361
4362 func
4363 func1
4364 fund
4365
4366 completing "func" looks for symbols between "func" and
4367 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4368 which finds "func" and "func1", but not "fund".
4369
4370 And with:
4371
4372 funcÿ (Latin1 'ÿ' [0xff])
4373 funcÿ1
4374 fund
4375
4376 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4377 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4378
4379 And with:
4380
4381 ÿÿ (Latin1 'ÿ' [0xff])
4382 ÿÿ1
4383
4384 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4385 the end of the list.
4386 */
4387 std::string after = search_name;
4388 while (!after.empty () && (unsigned char) after.back () == 0xff)
4389 after.pop_back ();
4390 if (!after.empty ())
4391 after.back () = (unsigned char) after.back () + 1;
4392 return after;
4393 }
4394
4395 /* See declaration. */
4396
4397 std::pair<std::vector<name_component>::const_iterator,
4398 std::vector<name_component>::const_iterator>
4399 mapped_index_base::find_name_components_bounds
4400 (const lookup_name_info &lookup_name_without_params) const
4401 {
4402 auto *name_cmp
4403 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4404
4405 const char *cplus
4406 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4407
4408 /* Comparison function object for lower_bound that matches against a
4409 given symbol name. */
4410 auto lookup_compare_lower = [&] (const name_component &elem,
4411 const char *name)
4412 {
4413 const char *elem_qualified = this->symbol_name_at (elem.idx);
4414 const char *elem_name = elem_qualified + elem.name_offset;
4415 return name_cmp (elem_name, name) < 0;
4416 };
4417
4418 /* Comparison function object for upper_bound that matches against a
4419 given symbol name. */
4420 auto lookup_compare_upper = [&] (const char *name,
4421 const name_component &elem)
4422 {
4423 const char *elem_qualified = this->symbol_name_at (elem.idx);
4424 const char *elem_name = elem_qualified + elem.name_offset;
4425 return name_cmp (name, elem_name) < 0;
4426 };
4427
4428 auto begin = this->name_components.begin ();
4429 auto end = this->name_components.end ();
4430
4431 /* Find the lower bound. */
4432 auto lower = [&] ()
4433 {
4434 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4435 return begin;
4436 else
4437 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4438 } ();
4439
4440 /* Find the upper bound. */
4441 auto upper = [&] ()
4442 {
4443 if (lookup_name_without_params.completion_mode ())
4444 {
4445 /* In completion mode, we want UPPER to point past all
4446 symbols names that have the same prefix. I.e., with
4447 these symbols, and completing "func":
4448
4449 function << lower bound
4450 function1
4451 other_function << upper bound
4452
4453 We find the upper bound by looking for the insertion
4454 point of "func"-with-last-character-incremented,
4455 i.e. "fund". */
4456 std::string after = make_sort_after_prefix_name (cplus);
4457 if (after.empty ())
4458 return end;
4459 return std::lower_bound (lower, end, after.c_str (),
4460 lookup_compare_lower);
4461 }
4462 else
4463 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4464 } ();
4465
4466 return {lower, upper};
4467 }
4468
4469 /* See declaration. */
4470
4471 void
4472 mapped_index_base::build_name_components ()
4473 {
4474 if (!this->name_components.empty ())
4475 return;
4476
4477 this->name_components_casing = case_sensitivity;
4478 auto *name_cmp
4479 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4480
4481 /* The code below only knows how to break apart components of C++
4482 symbol names (and other languages that use '::' as
4483 namespace/module separator). If we add support for wild matching
4484 to some language that uses some other operator (E.g., Ada, Go and
4485 D use '.'), then we'll need to try splitting the symbol name
4486 according to that language too. Note that Ada does support wild
4487 matching, but doesn't currently support .gdb_index. */
4488 auto count = this->symbol_name_count ();
4489 for (offset_type idx = 0; idx < count; idx++)
4490 {
4491 if (this->symbol_name_slot_invalid (idx))
4492 continue;
4493
4494 const char *name = this->symbol_name_at (idx);
4495
4496 /* Add each name component to the name component table. */
4497 unsigned int previous_len = 0;
4498 for (unsigned int current_len = cp_find_first_component (name);
4499 name[current_len] != '\0';
4500 current_len += cp_find_first_component (name + current_len))
4501 {
4502 gdb_assert (name[current_len] == ':');
4503 this->name_components.push_back ({previous_len, idx});
4504 /* Skip the '::'. */
4505 current_len += 2;
4506 previous_len = current_len;
4507 }
4508 this->name_components.push_back ({previous_len, idx});
4509 }
4510
4511 /* Sort name_components elements by name. */
4512 auto name_comp_compare = [&] (const name_component &left,
4513 const name_component &right)
4514 {
4515 const char *left_qualified = this->symbol_name_at (left.idx);
4516 const char *right_qualified = this->symbol_name_at (right.idx);
4517
4518 const char *left_name = left_qualified + left.name_offset;
4519 const char *right_name = right_qualified + right.name_offset;
4520
4521 return name_cmp (left_name, right_name) < 0;
4522 };
4523
4524 std::sort (this->name_components.begin (),
4525 this->name_components.end (),
4526 name_comp_compare);
4527 }
4528
4529 /* Helper for dw2_expand_symtabs_matching that works with a
4530 mapped_index_base instead of the containing objfile. This is split
4531 to a separate function in order to be able to unit test the
4532 name_components matching using a mock mapped_index_base. For each
4533 symbol name that matches, calls MATCH_CALLBACK, passing it the
4534 symbol's index in the mapped_index_base symbol table. */
4535
4536 static void
4537 dw2_expand_symtabs_matching_symbol
4538 (mapped_index_base &index,
4539 const lookup_name_info &lookup_name_in,
4540 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4541 enum search_domain kind,
4542 gdb::function_view<void (offset_type)> match_callback)
4543 {
4544 lookup_name_info lookup_name_without_params
4545 = lookup_name_in.make_ignore_params ();
4546 gdb_index_symbol_name_matcher lookup_name_matcher
4547 (lookup_name_without_params);
4548
4549 /* Build the symbol name component sorted vector, if we haven't
4550 yet. */
4551 index.build_name_components ();
4552
4553 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4554
4555 /* Now for each symbol name in range, check to see if we have a name
4556 match, and if so, call the MATCH_CALLBACK callback. */
4557
4558 /* The same symbol may appear more than once in the range though.
4559 E.g., if we're looking for symbols that complete "w", and we have
4560 a symbol named "w1::w2", we'll find the two name components for
4561 that same symbol in the range. To be sure we only call the
4562 callback once per symbol, we first collect the symbol name
4563 indexes that matched in a temporary vector and ignore
4564 duplicates. */
4565 std::vector<offset_type> matches;
4566 matches.reserve (std::distance (bounds.first, bounds.second));
4567
4568 for (; bounds.first != bounds.second; ++bounds.first)
4569 {
4570 const char *qualified = index.symbol_name_at (bounds.first->idx);
4571
4572 if (!lookup_name_matcher.matches (qualified)
4573 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4574 continue;
4575
4576 matches.push_back (bounds.first->idx);
4577 }
4578
4579 std::sort (matches.begin (), matches.end ());
4580
4581 /* Finally call the callback, once per match. */
4582 ULONGEST prev = -1;
4583 for (offset_type idx : matches)
4584 {
4585 if (prev != idx)
4586 {
4587 match_callback (idx);
4588 prev = idx;
4589 }
4590 }
4591
4592 /* Above we use a type wider than idx's for 'prev', since 0 and
4593 (offset_type)-1 are both possible values. */
4594 static_assert (sizeof (prev) > sizeof (offset_type), "");
4595 }
4596
4597 #if GDB_SELF_TEST
4598
4599 namespace selftests { namespace dw2_expand_symtabs_matching {
4600
4601 /* A mock .gdb_index/.debug_names-like name index table, enough to
4602 exercise dw2_expand_symtabs_matching_symbol, which works with the
4603 mapped_index_base interface. Builds an index from the symbol list
4604 passed as parameter to the constructor. */
4605 class mock_mapped_index : public mapped_index_base
4606 {
4607 public:
4608 mock_mapped_index (gdb::array_view<const char *> symbols)
4609 : m_symbol_table (symbols)
4610 {}
4611
4612 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4613
4614 /* Return the number of names in the symbol table. */
4615 virtual size_t symbol_name_count () const
4616 {
4617 return m_symbol_table.size ();
4618 }
4619
4620 /* Get the name of the symbol at IDX in the symbol table. */
4621 virtual const char *symbol_name_at (offset_type idx) const
4622 {
4623 return m_symbol_table[idx];
4624 }
4625
4626 private:
4627 gdb::array_view<const char *> m_symbol_table;
4628 };
4629
4630 /* Convenience function that converts a NULL pointer to a "<null>"
4631 string, to pass to print routines. */
4632
4633 static const char *
4634 string_or_null (const char *str)
4635 {
4636 return str != NULL ? str : "<null>";
4637 }
4638
4639 /* Check if a lookup_name_info built from
4640 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4641 index. EXPECTED_LIST is the list of expected matches, in expected
4642 matching order. If no match expected, then an empty list is
4643 specified. Returns true on success. On failure prints a warning
4644 indicating the file:line that failed, and returns false. */
4645
4646 static bool
4647 check_match (const char *file, int line,
4648 mock_mapped_index &mock_index,
4649 const char *name, symbol_name_match_type match_type,
4650 bool completion_mode,
4651 std::initializer_list<const char *> expected_list)
4652 {
4653 lookup_name_info lookup_name (name, match_type, completion_mode);
4654
4655 bool matched = true;
4656
4657 auto mismatch = [&] (const char *expected_str,
4658 const char *got)
4659 {
4660 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4661 "expected=\"%s\", got=\"%s\"\n"),
4662 file, line,
4663 (match_type == symbol_name_match_type::FULL
4664 ? "FULL" : "WILD"),
4665 name, string_or_null (expected_str), string_or_null (got));
4666 matched = false;
4667 };
4668
4669 auto expected_it = expected_list.begin ();
4670 auto expected_end = expected_list.end ();
4671
4672 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4673 NULL, ALL_DOMAIN,
4674 [&] (offset_type idx)
4675 {
4676 const char *matched_name = mock_index.symbol_name_at (idx);
4677 const char *expected_str
4678 = expected_it == expected_end ? NULL : *expected_it++;
4679
4680 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4681 mismatch (expected_str, matched_name);
4682 });
4683
4684 const char *expected_str
4685 = expected_it == expected_end ? NULL : *expected_it++;
4686 if (expected_str != NULL)
4687 mismatch (expected_str, NULL);
4688
4689 return matched;
4690 }
4691
4692 /* The symbols added to the mock mapped_index for testing (in
4693 canonical form). */
4694 static const char *test_symbols[] = {
4695 "function",
4696 "std::bar",
4697 "std::zfunction",
4698 "std::zfunction2",
4699 "w1::w2",
4700 "ns::foo<char*>",
4701 "ns::foo<int>",
4702 "ns::foo<long>",
4703 "ns2::tmpl<int>::foo2",
4704 "(anonymous namespace)::A::B::C",
4705
4706 /* These are used to check that the increment-last-char in the
4707 matching algorithm for completion doesn't match "t1_fund" when
4708 completing "t1_func". */
4709 "t1_func",
4710 "t1_func1",
4711 "t1_fund",
4712 "t1_fund1",
4713
4714 /* A UTF-8 name with multi-byte sequences to make sure that
4715 cp-name-parser understands this as a single identifier ("função"
4716 is "function" in PT). */
4717 u8"u8função",
4718
4719 /* \377 (0xff) is Latin1 'ÿ'. */
4720 "yfunc\377",
4721
4722 /* \377 (0xff) is Latin1 'ÿ'. */
4723 "\377",
4724 "\377\377123",
4725
4726 /* A name with all sorts of complications. Starts with "z" to make
4727 it easier for the completion tests below. */
4728 #define Z_SYM_NAME \
4729 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4730 "::tuple<(anonymous namespace)::ui*, " \
4731 "std::default_delete<(anonymous namespace)::ui>, void>"
4732
4733 Z_SYM_NAME
4734 };
4735
4736 /* Returns true if the mapped_index_base::find_name_component_bounds
4737 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4738 in completion mode. */
4739
4740 static bool
4741 check_find_bounds_finds (mapped_index_base &index,
4742 const char *search_name,
4743 gdb::array_view<const char *> expected_syms)
4744 {
4745 lookup_name_info lookup_name (search_name,
4746 symbol_name_match_type::FULL, true);
4747
4748 auto bounds = index.find_name_components_bounds (lookup_name);
4749
4750 size_t distance = std::distance (bounds.first, bounds.second);
4751 if (distance != expected_syms.size ())
4752 return false;
4753
4754 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4755 {
4756 auto nc_elem = bounds.first + exp_elem;
4757 const char *qualified = index.symbol_name_at (nc_elem->idx);
4758 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4759 return false;
4760 }
4761
4762 return true;
4763 }
4764
4765 /* Test the lower-level mapped_index::find_name_component_bounds
4766 method. */
4767
4768 static void
4769 test_mapped_index_find_name_component_bounds ()
4770 {
4771 mock_mapped_index mock_index (test_symbols);
4772
4773 mock_index.build_name_components ();
4774
4775 /* Test the lower-level mapped_index::find_name_component_bounds
4776 method in completion mode. */
4777 {
4778 static const char *expected_syms[] = {
4779 "t1_func",
4780 "t1_func1",
4781 };
4782
4783 SELF_CHECK (check_find_bounds_finds (mock_index,
4784 "t1_func", expected_syms));
4785 }
4786
4787 /* Check that the increment-last-char in the name matching algorithm
4788 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4789 {
4790 static const char *expected_syms1[] = {
4791 "\377",
4792 "\377\377123",
4793 };
4794 SELF_CHECK (check_find_bounds_finds (mock_index,
4795 "\377", expected_syms1));
4796
4797 static const char *expected_syms2[] = {
4798 "\377\377123",
4799 };
4800 SELF_CHECK (check_find_bounds_finds (mock_index,
4801 "\377\377", expected_syms2));
4802 }
4803 }
4804
4805 /* Test dw2_expand_symtabs_matching_symbol. */
4806
4807 static void
4808 test_dw2_expand_symtabs_matching_symbol ()
4809 {
4810 mock_mapped_index mock_index (test_symbols);
4811
4812 /* We let all tests run until the end even if some fails, for debug
4813 convenience. */
4814 bool any_mismatch = false;
4815
4816 /* Create the expected symbols list (an initializer_list). Needed
4817 because lists have commas, and we need to pass them to CHECK,
4818 which is a macro. */
4819 #define EXPECT(...) { __VA_ARGS__ }
4820
4821 /* Wrapper for check_match that passes down the current
4822 __FILE__/__LINE__. */
4823 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4824 any_mismatch |= !check_match (__FILE__, __LINE__, \
4825 mock_index, \
4826 NAME, MATCH_TYPE, COMPLETION_MODE, \
4827 EXPECTED_LIST)
4828
4829 /* Identity checks. */
4830 for (const char *sym : test_symbols)
4831 {
4832 /* Should be able to match all existing symbols. */
4833 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4834 EXPECT (sym));
4835
4836 /* Should be able to match all existing symbols with
4837 parameters. */
4838 std::string with_params = std::string (sym) + "(int)";
4839 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4840 EXPECT (sym));
4841
4842 /* Should be able to match all existing symbols with
4843 parameters and qualifiers. */
4844 with_params = std::string (sym) + " ( int ) const";
4845 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4846 EXPECT (sym));
4847
4848 /* This should really find sym, but cp-name-parser.y doesn't
4849 know about lvalue/rvalue qualifiers yet. */
4850 with_params = std::string (sym) + " ( int ) &&";
4851 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4852 {});
4853 }
4854
4855 /* Check that the name matching algorithm for completion doesn't get
4856 confused with Latin1 'ÿ' / 0xff. */
4857 {
4858 static const char str[] = "\377";
4859 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4860 EXPECT ("\377", "\377\377123"));
4861 }
4862
4863 /* Check that the increment-last-char in the matching algorithm for
4864 completion doesn't match "t1_fund" when completing "t1_func". */
4865 {
4866 static const char str[] = "t1_func";
4867 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4868 EXPECT ("t1_func", "t1_func1"));
4869 }
4870
4871 /* Check that completion mode works at each prefix of the expected
4872 symbol name. */
4873 {
4874 static const char str[] = "function(int)";
4875 size_t len = strlen (str);
4876 std::string lookup;
4877
4878 for (size_t i = 1; i < len; i++)
4879 {
4880 lookup.assign (str, i);
4881 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4882 EXPECT ("function"));
4883 }
4884 }
4885
4886 /* While "w" is a prefix of both components, the match function
4887 should still only be called once. */
4888 {
4889 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4890 EXPECT ("w1::w2"));
4891 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4892 EXPECT ("w1::w2"));
4893 }
4894
4895 /* Same, with a "complicated" symbol. */
4896 {
4897 static const char str[] = Z_SYM_NAME;
4898 size_t len = strlen (str);
4899 std::string lookup;
4900
4901 for (size_t i = 1; i < len; i++)
4902 {
4903 lookup.assign (str, i);
4904 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4905 EXPECT (Z_SYM_NAME));
4906 }
4907 }
4908
4909 /* In FULL mode, an incomplete symbol doesn't match. */
4910 {
4911 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4912 {});
4913 }
4914
4915 /* A complete symbol with parameters matches any overload, since the
4916 index has no overload info. */
4917 {
4918 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4919 EXPECT ("std::zfunction", "std::zfunction2"));
4920 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4921 EXPECT ("std::zfunction", "std::zfunction2"));
4922 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4923 EXPECT ("std::zfunction", "std::zfunction2"));
4924 }
4925
4926 /* Check that whitespace is ignored appropriately. A symbol with a
4927 template argument list. */
4928 {
4929 static const char expected[] = "ns::foo<int>";
4930 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4931 EXPECT (expected));
4932 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4933 EXPECT (expected));
4934 }
4935
4936 /* Check that whitespace is ignored appropriately. A symbol with a
4937 template argument list that includes a pointer. */
4938 {
4939 static const char expected[] = "ns::foo<char*>";
4940 /* Try both completion and non-completion modes. */
4941 static const bool completion_mode[2] = {false, true};
4942 for (size_t i = 0; i < 2; i++)
4943 {
4944 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4945 completion_mode[i], EXPECT (expected));
4946 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4947 completion_mode[i], EXPECT (expected));
4948
4949 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4950 completion_mode[i], EXPECT (expected));
4951 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4952 completion_mode[i], EXPECT (expected));
4953 }
4954 }
4955
4956 {
4957 /* Check method qualifiers are ignored. */
4958 static const char expected[] = "ns::foo<char*>";
4959 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4960 symbol_name_match_type::FULL, true, EXPECT (expected));
4961 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4962 symbol_name_match_type::FULL, true, EXPECT (expected));
4963 CHECK_MATCH ("foo < char * > ( int ) const",
4964 symbol_name_match_type::WILD, true, EXPECT (expected));
4965 CHECK_MATCH ("foo < char * > ( int ) &&",
4966 symbol_name_match_type::WILD, true, EXPECT (expected));
4967 }
4968
4969 /* Test lookup names that don't match anything. */
4970 {
4971 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4972 {});
4973
4974 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4975 {});
4976 }
4977
4978 /* Some wild matching tests, exercising "(anonymous namespace)",
4979 which should not be confused with a parameter list. */
4980 {
4981 static const char *syms[] = {
4982 "A::B::C",
4983 "B::C",
4984 "C",
4985 "A :: B :: C ( int )",
4986 "B :: C ( int )",
4987 "C ( int )",
4988 };
4989
4990 for (const char *s : syms)
4991 {
4992 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4993 EXPECT ("(anonymous namespace)::A::B::C"));
4994 }
4995 }
4996
4997 {
4998 static const char expected[] = "ns2::tmpl<int>::foo2";
4999 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5000 EXPECT (expected));
5001 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5002 EXPECT (expected));
5003 }
5004
5005 SELF_CHECK (!any_mismatch);
5006
5007 #undef EXPECT
5008 #undef CHECK_MATCH
5009 }
5010
5011 static void
5012 run_test ()
5013 {
5014 test_mapped_index_find_name_component_bounds ();
5015 test_dw2_expand_symtabs_matching_symbol ();
5016 }
5017
5018 }} // namespace selftests::dw2_expand_symtabs_matching
5019
5020 #endif /* GDB_SELF_TEST */
5021
5022 /* If FILE_MATCHER is NULL or if PER_CU has
5023 dwarf2_per_cu_quick_data::MARK set (see
5024 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5025 EXPANSION_NOTIFY on it. */
5026
5027 static void
5028 dw2_expand_symtabs_matching_one
5029 (struct dwarf2_per_cu_data *per_cu,
5030 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5031 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5032 {
5033 if (file_matcher == NULL || per_cu->v.quick->mark)
5034 {
5035 bool symtab_was_null
5036 = (per_cu->v.quick->compunit_symtab == NULL);
5037
5038 dw2_instantiate_symtab (per_cu);
5039
5040 if (expansion_notify != NULL
5041 && symtab_was_null
5042 && per_cu->v.quick->compunit_symtab != NULL)
5043 expansion_notify (per_cu->v.quick->compunit_symtab);
5044 }
5045 }
5046
5047 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5048 matched, to expand corresponding CUs that were marked. IDX is the
5049 index of the symbol name that matched. */
5050
5051 static void
5052 dw2_expand_marked_cus
5053 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5054 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5055 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5056 search_domain kind)
5057 {
5058 offset_type *vec, vec_len, vec_idx;
5059 bool global_seen = false;
5060 mapped_index &index = *dwarf2_per_objfile->index_table;
5061
5062 vec = (offset_type *) (index.constant_pool
5063 + MAYBE_SWAP (index.symbol_table[idx].vec));
5064 vec_len = MAYBE_SWAP (vec[0]);
5065 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5066 {
5067 struct dwarf2_per_cu_data *per_cu;
5068 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5069 /* This value is only valid for index versions >= 7. */
5070 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5071 gdb_index_symbol_kind symbol_kind =
5072 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5073 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5074 /* Only check the symbol attributes if they're present.
5075 Indices prior to version 7 don't record them,
5076 and indices >= 7 may elide them for certain symbols
5077 (gold does this). */
5078 int attrs_valid =
5079 (index.version >= 7
5080 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5081
5082 /* Work around gold/15646. */
5083 if (attrs_valid)
5084 {
5085 if (!is_static && global_seen)
5086 continue;
5087 if (!is_static)
5088 global_seen = true;
5089 }
5090
5091 /* Only check the symbol's kind if it has one. */
5092 if (attrs_valid)
5093 {
5094 switch (kind)
5095 {
5096 case VARIABLES_DOMAIN:
5097 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5098 continue;
5099 break;
5100 case FUNCTIONS_DOMAIN:
5101 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5102 continue;
5103 break;
5104 case TYPES_DOMAIN:
5105 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5106 continue;
5107 break;
5108 default:
5109 break;
5110 }
5111 }
5112
5113 /* Don't crash on bad data. */
5114 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5115 + dwarf2_per_objfile->n_type_units))
5116 {
5117 complaint (&symfile_complaints,
5118 _(".gdb_index entry has bad CU index"
5119 " [in module %s]"),
5120 objfile_name (dwarf2_per_objfile->objfile));
5121 continue;
5122 }
5123
5124 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5125 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5126 expansion_notify);
5127 }
5128 }
5129
5130 /* If FILE_MATCHER is non-NULL, set all the
5131 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5132 that match FILE_MATCHER. */
5133
5134 static void
5135 dw_expand_symtabs_matching_file_matcher
5136 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5137 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5138 {
5139 if (file_matcher == NULL)
5140 return;
5141
5142 objfile *const objfile = dwarf2_per_objfile->objfile;
5143
5144 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5145 htab_eq_pointer,
5146 NULL, xcalloc, xfree));
5147 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5148 htab_eq_pointer,
5149 NULL, xcalloc, xfree));
5150
5151 /* The rule is CUs specify all the files, including those used by
5152 any TU, so there's no need to scan TUs here. */
5153
5154 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5155 {
5156 int j;
5157 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5158 struct quick_file_names *file_data;
5159 void **slot;
5160
5161 QUIT;
5162
5163 per_cu->v.quick->mark = 0;
5164
5165 /* We only need to look at symtabs not already expanded. */
5166 if (per_cu->v.quick->compunit_symtab)
5167 continue;
5168
5169 file_data = dw2_get_file_names (per_cu);
5170 if (file_data == NULL)
5171 continue;
5172
5173 if (htab_find (visited_not_found.get (), file_data) != NULL)
5174 continue;
5175 else if (htab_find (visited_found.get (), file_data) != NULL)
5176 {
5177 per_cu->v.quick->mark = 1;
5178 continue;
5179 }
5180
5181 for (j = 0; j < file_data->num_file_names; ++j)
5182 {
5183 const char *this_real_name;
5184
5185 if (file_matcher (file_data->file_names[j], false))
5186 {
5187 per_cu->v.quick->mark = 1;
5188 break;
5189 }
5190
5191 /* Before we invoke realpath, which can get expensive when many
5192 files are involved, do a quick comparison of the basenames. */
5193 if (!basenames_may_differ
5194 && !file_matcher (lbasename (file_data->file_names[j]),
5195 true))
5196 continue;
5197
5198 this_real_name = dw2_get_real_path (objfile, file_data, j);
5199 if (file_matcher (this_real_name, false))
5200 {
5201 per_cu->v.quick->mark = 1;
5202 break;
5203 }
5204 }
5205
5206 slot = htab_find_slot (per_cu->v.quick->mark
5207 ? visited_found.get ()
5208 : visited_not_found.get (),
5209 file_data, INSERT);
5210 *slot = file_data;
5211 }
5212 }
5213
5214 static void
5215 dw2_expand_symtabs_matching
5216 (struct objfile *objfile,
5217 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5218 const lookup_name_info &lookup_name,
5219 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5220 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5221 enum search_domain kind)
5222 {
5223 struct dwarf2_per_objfile *dwarf2_per_objfile
5224 = get_dwarf2_per_objfile (objfile);
5225
5226 /* index_table is NULL if OBJF_READNOW. */
5227 if (!dwarf2_per_objfile->index_table)
5228 return;
5229
5230 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5231
5232 mapped_index &index = *dwarf2_per_objfile->index_table;
5233
5234 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5235 symbol_matcher,
5236 kind, [&] (offset_type idx)
5237 {
5238 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5239 expansion_notify, kind);
5240 });
5241 }
5242
5243 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5244 symtab. */
5245
5246 static struct compunit_symtab *
5247 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5248 CORE_ADDR pc)
5249 {
5250 int i;
5251
5252 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5253 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5254 return cust;
5255
5256 if (cust->includes == NULL)
5257 return NULL;
5258
5259 for (i = 0; cust->includes[i]; ++i)
5260 {
5261 struct compunit_symtab *s = cust->includes[i];
5262
5263 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5264 if (s != NULL)
5265 return s;
5266 }
5267
5268 return NULL;
5269 }
5270
5271 static struct compunit_symtab *
5272 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5273 struct bound_minimal_symbol msymbol,
5274 CORE_ADDR pc,
5275 struct obj_section *section,
5276 int warn_if_readin)
5277 {
5278 struct dwarf2_per_cu_data *data;
5279 struct compunit_symtab *result;
5280
5281 if (!objfile->psymtabs_addrmap)
5282 return NULL;
5283
5284 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5285 pc);
5286 if (!data)
5287 return NULL;
5288
5289 if (warn_if_readin && data->v.quick->compunit_symtab)
5290 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5291 paddress (get_objfile_arch (objfile), pc));
5292
5293 result
5294 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5295 pc);
5296 gdb_assert (result != NULL);
5297 return result;
5298 }
5299
5300 static void
5301 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5302 void *data, int need_fullname)
5303 {
5304 struct dwarf2_per_objfile *dwarf2_per_objfile
5305 = get_dwarf2_per_objfile (objfile);
5306
5307 if (!dwarf2_per_objfile->filenames_cache)
5308 {
5309 dwarf2_per_objfile->filenames_cache.emplace ();
5310
5311 htab_up visited (htab_create_alloc (10,
5312 htab_hash_pointer, htab_eq_pointer,
5313 NULL, xcalloc, xfree));
5314
5315 /* The rule is CUs specify all the files, including those used
5316 by any TU, so there's no need to scan TUs here. We can
5317 ignore file names coming from already-expanded CUs. */
5318
5319 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5320 {
5321 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5322
5323 if (per_cu->v.quick->compunit_symtab)
5324 {
5325 void **slot = htab_find_slot (visited.get (),
5326 per_cu->v.quick->file_names,
5327 INSERT);
5328
5329 *slot = per_cu->v.quick->file_names;
5330 }
5331 }
5332
5333 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5334 {
5335 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5336 struct quick_file_names *file_data;
5337 void **slot;
5338
5339 /* We only need to look at symtabs not already expanded. */
5340 if (per_cu->v.quick->compunit_symtab)
5341 continue;
5342
5343 file_data = dw2_get_file_names (per_cu);
5344 if (file_data == NULL)
5345 continue;
5346
5347 slot = htab_find_slot (visited.get (), file_data, INSERT);
5348 if (*slot)
5349 {
5350 /* Already visited. */
5351 continue;
5352 }
5353 *slot = file_data;
5354
5355 for (int j = 0; j < file_data->num_file_names; ++j)
5356 {
5357 const char *filename = file_data->file_names[j];
5358 dwarf2_per_objfile->filenames_cache->seen (filename);
5359 }
5360 }
5361 }
5362
5363 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5364 {
5365 gdb::unique_xmalloc_ptr<char> this_real_name;
5366
5367 if (need_fullname)
5368 this_real_name = gdb_realpath (filename);
5369 (*fun) (filename, this_real_name.get (), data);
5370 });
5371 }
5372
5373 static int
5374 dw2_has_symbols (struct objfile *objfile)
5375 {
5376 return 1;
5377 }
5378
5379 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5380 {
5381 dw2_has_symbols,
5382 dw2_find_last_source_symtab,
5383 dw2_forget_cached_source_info,
5384 dw2_map_symtabs_matching_filename,
5385 dw2_lookup_symbol,
5386 dw2_print_stats,
5387 dw2_dump,
5388 dw2_relocate,
5389 dw2_expand_symtabs_for_function,
5390 dw2_expand_all_symtabs,
5391 dw2_expand_symtabs_with_fullname,
5392 dw2_map_matching_symbols,
5393 dw2_expand_symtabs_matching,
5394 dw2_find_pc_sect_compunit_symtab,
5395 NULL,
5396 dw2_map_symbol_filenames
5397 };
5398
5399 /* DWARF-5 debug_names reader. */
5400
5401 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5402 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5403
5404 /* A helper function that reads the .debug_names section in SECTION
5405 and fills in MAP. FILENAME is the name of the file containing the
5406 section; it is used for error reporting.
5407
5408 Returns true if all went well, false otherwise. */
5409
5410 static bool
5411 read_debug_names_from_section (struct objfile *objfile,
5412 const char *filename,
5413 struct dwarf2_section_info *section,
5414 mapped_debug_names &map)
5415 {
5416 if (dwarf2_section_empty_p (section))
5417 return false;
5418
5419 /* Older elfutils strip versions could keep the section in the main
5420 executable while splitting it for the separate debug info file. */
5421 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5422 return false;
5423
5424 dwarf2_read_section (objfile, section);
5425
5426 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5427
5428 const gdb_byte *addr = section->buffer;
5429
5430 bfd *const abfd = get_section_bfd_owner (section);
5431
5432 unsigned int bytes_read;
5433 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5434 addr += bytes_read;
5435
5436 map.dwarf5_is_dwarf64 = bytes_read != 4;
5437 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5438 if (bytes_read + length != section->size)
5439 {
5440 /* There may be multiple per-CU indices. */
5441 warning (_("Section .debug_names in %s length %s does not match "
5442 "section length %s, ignoring .debug_names."),
5443 filename, plongest (bytes_read + length),
5444 pulongest (section->size));
5445 return false;
5446 }
5447
5448 /* The version number. */
5449 uint16_t version = read_2_bytes (abfd, addr);
5450 addr += 2;
5451 if (version != 5)
5452 {
5453 warning (_("Section .debug_names in %s has unsupported version %d, "
5454 "ignoring .debug_names."),
5455 filename, version);
5456 return false;
5457 }
5458
5459 /* Padding. */
5460 uint16_t padding = read_2_bytes (abfd, addr);
5461 addr += 2;
5462 if (padding != 0)
5463 {
5464 warning (_("Section .debug_names in %s has unsupported padding %d, "
5465 "ignoring .debug_names."),
5466 filename, padding);
5467 return false;
5468 }
5469
5470 /* comp_unit_count - The number of CUs in the CU list. */
5471 map.cu_count = read_4_bytes (abfd, addr);
5472 addr += 4;
5473
5474 /* local_type_unit_count - The number of TUs in the local TU
5475 list. */
5476 map.tu_count = read_4_bytes (abfd, addr);
5477 addr += 4;
5478
5479 /* foreign_type_unit_count - The number of TUs in the foreign TU
5480 list. */
5481 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5482 addr += 4;
5483 if (foreign_tu_count != 0)
5484 {
5485 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5486 "ignoring .debug_names."),
5487 filename, static_cast<unsigned long> (foreign_tu_count));
5488 return false;
5489 }
5490
5491 /* bucket_count - The number of hash buckets in the hash lookup
5492 table. */
5493 map.bucket_count = read_4_bytes (abfd, addr);
5494 addr += 4;
5495
5496 /* name_count - The number of unique names in the index. */
5497 map.name_count = read_4_bytes (abfd, addr);
5498 addr += 4;
5499
5500 /* abbrev_table_size - The size in bytes of the abbreviations
5501 table. */
5502 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5503 addr += 4;
5504
5505 /* augmentation_string_size - The size in bytes of the augmentation
5506 string. This value is rounded up to a multiple of 4. */
5507 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5508 addr += 4;
5509 map.augmentation_is_gdb = ((augmentation_string_size
5510 == sizeof (dwarf5_augmentation))
5511 && memcmp (addr, dwarf5_augmentation,
5512 sizeof (dwarf5_augmentation)) == 0);
5513 augmentation_string_size += (-augmentation_string_size) & 3;
5514 addr += augmentation_string_size;
5515
5516 /* List of CUs */
5517 map.cu_table_reordered = addr;
5518 addr += map.cu_count * map.offset_size;
5519
5520 /* List of Local TUs */
5521 map.tu_table_reordered = addr;
5522 addr += map.tu_count * map.offset_size;
5523
5524 /* Hash Lookup Table */
5525 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5526 addr += map.bucket_count * 4;
5527 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5528 addr += map.name_count * 4;
5529
5530 /* Name Table */
5531 map.name_table_string_offs_reordered = addr;
5532 addr += map.name_count * map.offset_size;
5533 map.name_table_entry_offs_reordered = addr;
5534 addr += map.name_count * map.offset_size;
5535
5536 const gdb_byte *abbrev_table_start = addr;
5537 for (;;)
5538 {
5539 unsigned int bytes_read;
5540 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5541 addr += bytes_read;
5542 if (index_num == 0)
5543 break;
5544
5545 const auto insertpair
5546 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5547 if (!insertpair.second)
5548 {
5549 warning (_("Section .debug_names in %s has duplicate index %s, "
5550 "ignoring .debug_names."),
5551 filename, pulongest (index_num));
5552 return false;
5553 }
5554 mapped_debug_names::index_val &indexval = insertpair.first->second;
5555 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5556 addr += bytes_read;
5557
5558 for (;;)
5559 {
5560 mapped_debug_names::index_val::attr attr;
5561 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5562 addr += bytes_read;
5563 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5564 addr += bytes_read;
5565 if (attr.form == DW_FORM_implicit_const)
5566 {
5567 attr.implicit_const = read_signed_leb128 (abfd, addr,
5568 &bytes_read);
5569 addr += bytes_read;
5570 }
5571 if (attr.dw_idx == 0 && attr.form == 0)
5572 break;
5573 indexval.attr_vec.push_back (std::move (attr));
5574 }
5575 }
5576 if (addr != abbrev_table_start + abbrev_table_size)
5577 {
5578 warning (_("Section .debug_names in %s has abbreviation_table "
5579 "of size %zu vs. written as %u, ignoring .debug_names."),
5580 filename, addr - abbrev_table_start, abbrev_table_size);
5581 return false;
5582 }
5583 map.entry_pool = addr;
5584
5585 return true;
5586 }
5587
5588 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5589 list. */
5590
5591 static void
5592 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5593 const mapped_debug_names &map,
5594 dwarf2_section_info &section,
5595 bool is_dwz, int base_offset)
5596 {
5597 sect_offset sect_off_prev;
5598 for (uint32_t i = 0; i <= map.cu_count; ++i)
5599 {
5600 sect_offset sect_off_next;
5601 if (i < map.cu_count)
5602 {
5603 sect_off_next
5604 = (sect_offset) (extract_unsigned_integer
5605 (map.cu_table_reordered + i * map.offset_size,
5606 map.offset_size,
5607 map.dwarf5_byte_order));
5608 }
5609 else
5610 sect_off_next = (sect_offset) section.size;
5611 if (i >= 1)
5612 {
5613 const ULONGEST length = sect_off_next - sect_off_prev;
5614 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5615 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5616 sect_off_prev, length);
5617 }
5618 sect_off_prev = sect_off_next;
5619 }
5620 }
5621
5622 /* Read the CU list from the mapped index, and use it to create all
5623 the CU objects for this dwarf2_per_objfile. */
5624
5625 static void
5626 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5627 const mapped_debug_names &map,
5628 const mapped_debug_names &dwz_map)
5629 {
5630 struct objfile *objfile = dwarf2_per_objfile->objfile;
5631
5632 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5633 dwarf2_per_objfile->all_comp_units
5634 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5635 dwarf2_per_objfile->n_comp_units);
5636
5637 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5638 dwarf2_per_objfile->info,
5639 false /* is_dwz */,
5640 0 /* base_offset */);
5641
5642 if (dwz_map.cu_count == 0)
5643 return;
5644
5645 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5646 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5647 true /* is_dwz */,
5648 map.cu_count /* base_offset */);
5649 }
5650
5651 /* Read .debug_names. If everything went ok, initialize the "quick"
5652 elements of all the CUs and return true. Otherwise, return false. */
5653
5654 static bool
5655 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5656 {
5657 mapped_debug_names local_map (dwarf2_per_objfile);
5658 mapped_debug_names dwz_map (dwarf2_per_objfile);
5659 struct objfile *objfile = dwarf2_per_objfile->objfile;
5660
5661 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5662 &dwarf2_per_objfile->debug_names,
5663 local_map))
5664 return false;
5665
5666 /* Don't use the index if it's empty. */
5667 if (local_map.name_count == 0)
5668 return false;
5669
5670 /* If there is a .dwz file, read it so we can get its CU list as
5671 well. */
5672 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5673 if (dwz != NULL)
5674 {
5675 if (!read_debug_names_from_section (objfile,
5676 bfd_get_filename (dwz->dwz_bfd),
5677 &dwz->debug_names, dwz_map))
5678 {
5679 warning (_("could not read '.debug_names' section from %s; skipping"),
5680 bfd_get_filename (dwz->dwz_bfd));
5681 return false;
5682 }
5683 }
5684
5685 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
5686
5687 if (local_map.tu_count != 0)
5688 {
5689 /* We can only handle a single .debug_types when we have an
5690 index. */
5691 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5692 return false;
5693
5694 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5695 dwarf2_per_objfile->types, 0);
5696
5697 create_signatured_type_table_from_debug_names
5698 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5699 }
5700
5701 create_addrmap_from_aranges (dwarf2_per_objfile,
5702 &dwarf2_per_objfile->debug_aranges);
5703
5704 dwarf2_per_objfile->debug_names_table.reset
5705 (new mapped_debug_names (dwarf2_per_objfile));
5706 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5707 dwarf2_per_objfile->using_index = 1;
5708 dwarf2_per_objfile->quick_file_names_table =
5709 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5710
5711 return true;
5712 }
5713
5714 /* Type used to manage iterating over all CUs looking for a symbol for
5715 .debug_names. */
5716
5717 class dw2_debug_names_iterator
5718 {
5719 public:
5720 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5721 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5722 dw2_debug_names_iterator (const mapped_debug_names &map,
5723 bool want_specific_block,
5724 block_enum block_index, domain_enum domain,
5725 const char *name)
5726 : m_map (map), m_want_specific_block (want_specific_block),
5727 m_block_index (block_index), m_domain (domain),
5728 m_addr (find_vec_in_debug_names (map, name))
5729 {}
5730
5731 dw2_debug_names_iterator (const mapped_debug_names &map,
5732 search_domain search, uint32_t namei)
5733 : m_map (map),
5734 m_search (search),
5735 m_addr (find_vec_in_debug_names (map, namei))
5736 {}
5737
5738 /* Return the next matching CU or NULL if there are no more. */
5739 dwarf2_per_cu_data *next ();
5740
5741 private:
5742 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5743 const char *name);
5744 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5745 uint32_t namei);
5746
5747 /* The internalized form of .debug_names. */
5748 const mapped_debug_names &m_map;
5749
5750 /* If true, only look for symbols that match BLOCK_INDEX. */
5751 const bool m_want_specific_block = false;
5752
5753 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5754 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5755 value. */
5756 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5757
5758 /* The kind of symbol we're looking for. */
5759 const domain_enum m_domain = UNDEF_DOMAIN;
5760 const search_domain m_search = ALL_DOMAIN;
5761
5762 /* The list of CUs from the index entry of the symbol, or NULL if
5763 not found. */
5764 const gdb_byte *m_addr;
5765 };
5766
5767 const char *
5768 mapped_debug_names::namei_to_name (uint32_t namei) const
5769 {
5770 const ULONGEST namei_string_offs
5771 = extract_unsigned_integer ((name_table_string_offs_reordered
5772 + namei * offset_size),
5773 offset_size,
5774 dwarf5_byte_order);
5775 return read_indirect_string_at_offset
5776 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5777 }
5778
5779 /* Find a slot in .debug_names for the object named NAME. If NAME is
5780 found, return pointer to its pool data. If NAME cannot be found,
5781 return NULL. */
5782
5783 const gdb_byte *
5784 dw2_debug_names_iterator::find_vec_in_debug_names
5785 (const mapped_debug_names &map, const char *name)
5786 {
5787 int (*cmp) (const char *, const char *);
5788
5789 if (current_language->la_language == language_cplus
5790 || current_language->la_language == language_fortran
5791 || current_language->la_language == language_d)
5792 {
5793 /* NAME is already canonical. Drop any qualifiers as
5794 .debug_names does not contain any. */
5795
5796 if (strchr (name, '(') != NULL)
5797 {
5798 gdb::unique_xmalloc_ptr<char> without_params
5799 = cp_remove_params (name);
5800
5801 if (without_params != NULL)
5802 {
5803 name = without_params.get();
5804 }
5805 }
5806 }
5807
5808 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5809
5810 const uint32_t full_hash = dwarf5_djb_hash (name);
5811 uint32_t namei
5812 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5813 (map.bucket_table_reordered
5814 + (full_hash % map.bucket_count)), 4,
5815 map.dwarf5_byte_order);
5816 if (namei == 0)
5817 return NULL;
5818 --namei;
5819 if (namei >= map.name_count)
5820 {
5821 complaint (&symfile_complaints,
5822 _("Wrong .debug_names with name index %u but name_count=%u "
5823 "[in module %s]"),
5824 namei, map.name_count,
5825 objfile_name (map.dwarf2_per_objfile->objfile));
5826 return NULL;
5827 }
5828
5829 for (;;)
5830 {
5831 const uint32_t namei_full_hash
5832 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5833 (map.hash_table_reordered + namei), 4,
5834 map.dwarf5_byte_order);
5835 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5836 return NULL;
5837
5838 if (full_hash == namei_full_hash)
5839 {
5840 const char *const namei_string = map.namei_to_name (namei);
5841
5842 #if 0 /* An expensive sanity check. */
5843 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5844 {
5845 complaint (&symfile_complaints,
5846 _("Wrong .debug_names hash for string at index %u "
5847 "[in module %s]"),
5848 namei, objfile_name (dwarf2_per_objfile->objfile));
5849 return NULL;
5850 }
5851 #endif
5852
5853 if (cmp (namei_string, name) == 0)
5854 {
5855 const ULONGEST namei_entry_offs
5856 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5857 + namei * map.offset_size),
5858 map.offset_size, map.dwarf5_byte_order);
5859 return map.entry_pool + namei_entry_offs;
5860 }
5861 }
5862
5863 ++namei;
5864 if (namei >= map.name_count)
5865 return NULL;
5866 }
5867 }
5868
5869 const gdb_byte *
5870 dw2_debug_names_iterator::find_vec_in_debug_names
5871 (const mapped_debug_names &map, uint32_t namei)
5872 {
5873 if (namei >= map.name_count)
5874 {
5875 complaint (&symfile_complaints,
5876 _("Wrong .debug_names with name index %u but name_count=%u "
5877 "[in module %s]"),
5878 namei, map.name_count,
5879 objfile_name (map.dwarf2_per_objfile->objfile));
5880 return NULL;
5881 }
5882
5883 const ULONGEST namei_entry_offs
5884 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5885 + namei * map.offset_size),
5886 map.offset_size, map.dwarf5_byte_order);
5887 return map.entry_pool + namei_entry_offs;
5888 }
5889
5890 /* See dw2_debug_names_iterator. */
5891
5892 dwarf2_per_cu_data *
5893 dw2_debug_names_iterator::next ()
5894 {
5895 if (m_addr == NULL)
5896 return NULL;
5897
5898 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5899 struct objfile *objfile = dwarf2_per_objfile->objfile;
5900 bfd *const abfd = objfile->obfd;
5901
5902 again:
5903
5904 unsigned int bytes_read;
5905 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5906 m_addr += bytes_read;
5907 if (abbrev == 0)
5908 return NULL;
5909
5910 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5911 if (indexval_it == m_map.abbrev_map.cend ())
5912 {
5913 complaint (&symfile_complaints,
5914 _("Wrong .debug_names undefined abbrev code %s "
5915 "[in module %s]"),
5916 pulongest (abbrev), objfile_name (objfile));
5917 return NULL;
5918 }
5919 const mapped_debug_names::index_val &indexval = indexval_it->second;
5920 bool have_is_static = false;
5921 bool is_static;
5922 dwarf2_per_cu_data *per_cu = NULL;
5923 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5924 {
5925 ULONGEST ull;
5926 switch (attr.form)
5927 {
5928 case DW_FORM_implicit_const:
5929 ull = attr.implicit_const;
5930 break;
5931 case DW_FORM_flag_present:
5932 ull = 1;
5933 break;
5934 case DW_FORM_udata:
5935 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5936 m_addr += bytes_read;
5937 break;
5938 default:
5939 complaint (&symfile_complaints,
5940 _("Unsupported .debug_names form %s [in module %s]"),
5941 dwarf_form_name (attr.form),
5942 objfile_name (objfile));
5943 return NULL;
5944 }
5945 switch (attr.dw_idx)
5946 {
5947 case DW_IDX_compile_unit:
5948 /* Don't crash on bad data. */
5949 if (ull >= dwarf2_per_objfile->n_comp_units)
5950 {
5951 complaint (&symfile_complaints,
5952 _(".debug_names entry has bad CU index %s"
5953 " [in module %s]"),
5954 pulongest (ull),
5955 objfile_name (dwarf2_per_objfile->objfile));
5956 continue;
5957 }
5958 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
5959 break;
5960 case DW_IDX_type_unit:
5961 /* Don't crash on bad data. */
5962 if (ull >= dwarf2_per_objfile->n_type_units)
5963 {
5964 complaint (&symfile_complaints,
5965 _(".debug_names entry has bad TU index %s"
5966 " [in module %s]"),
5967 pulongest (ull),
5968 objfile_name (dwarf2_per_objfile->objfile));
5969 continue;
5970 }
5971 per_cu = dw2_get_cutu (dwarf2_per_objfile,
5972 dwarf2_per_objfile->n_comp_units + ull);
5973 break;
5974 case DW_IDX_GNU_internal:
5975 if (!m_map.augmentation_is_gdb)
5976 break;
5977 have_is_static = true;
5978 is_static = true;
5979 break;
5980 case DW_IDX_GNU_external:
5981 if (!m_map.augmentation_is_gdb)
5982 break;
5983 have_is_static = true;
5984 is_static = false;
5985 break;
5986 }
5987 }
5988
5989 /* Skip if already read in. */
5990 if (per_cu->v.quick->compunit_symtab)
5991 goto again;
5992
5993 /* Check static vs global. */
5994 if (have_is_static)
5995 {
5996 const bool want_static = m_block_index != GLOBAL_BLOCK;
5997 if (m_want_specific_block && want_static != is_static)
5998 goto again;
5999 }
6000
6001 /* Match dw2_symtab_iter_next, symbol_kind
6002 and debug_names::psymbol_tag. */
6003 switch (m_domain)
6004 {
6005 case VAR_DOMAIN:
6006 switch (indexval.dwarf_tag)
6007 {
6008 case DW_TAG_variable:
6009 case DW_TAG_subprogram:
6010 /* Some types are also in VAR_DOMAIN. */
6011 case DW_TAG_typedef:
6012 case DW_TAG_structure_type:
6013 break;
6014 default:
6015 goto again;
6016 }
6017 break;
6018 case STRUCT_DOMAIN:
6019 switch (indexval.dwarf_tag)
6020 {
6021 case DW_TAG_typedef:
6022 case DW_TAG_structure_type:
6023 break;
6024 default:
6025 goto again;
6026 }
6027 break;
6028 case LABEL_DOMAIN:
6029 switch (indexval.dwarf_tag)
6030 {
6031 case 0:
6032 case DW_TAG_variable:
6033 break;
6034 default:
6035 goto again;
6036 }
6037 break;
6038 default:
6039 break;
6040 }
6041
6042 /* Match dw2_expand_symtabs_matching, symbol_kind and
6043 debug_names::psymbol_tag. */
6044 switch (m_search)
6045 {
6046 case VARIABLES_DOMAIN:
6047 switch (indexval.dwarf_tag)
6048 {
6049 case DW_TAG_variable:
6050 break;
6051 default:
6052 goto again;
6053 }
6054 break;
6055 case FUNCTIONS_DOMAIN:
6056 switch (indexval.dwarf_tag)
6057 {
6058 case DW_TAG_subprogram:
6059 break;
6060 default:
6061 goto again;
6062 }
6063 break;
6064 case TYPES_DOMAIN:
6065 switch (indexval.dwarf_tag)
6066 {
6067 case DW_TAG_typedef:
6068 case DW_TAG_structure_type:
6069 break;
6070 default:
6071 goto again;
6072 }
6073 break;
6074 default:
6075 break;
6076 }
6077
6078 return per_cu;
6079 }
6080
6081 static struct compunit_symtab *
6082 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6083 const char *name, domain_enum domain)
6084 {
6085 const block_enum block_index = static_cast<block_enum> (block_index_int);
6086 struct dwarf2_per_objfile *dwarf2_per_objfile
6087 = get_dwarf2_per_objfile (objfile);
6088
6089 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6090 if (!mapp)
6091 {
6092 /* index is NULL if OBJF_READNOW. */
6093 return NULL;
6094 }
6095 const auto &map = *mapp;
6096
6097 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6098 block_index, domain, name);
6099
6100 struct compunit_symtab *stab_best = NULL;
6101 struct dwarf2_per_cu_data *per_cu;
6102 while ((per_cu = iter.next ()) != NULL)
6103 {
6104 struct symbol *sym, *with_opaque = NULL;
6105 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6106 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6107 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6108
6109 sym = block_find_symbol (block, name, domain,
6110 block_find_non_opaque_type_preferred,
6111 &with_opaque);
6112
6113 /* Some caution must be observed with overloaded functions and
6114 methods, since the index will not contain any overload
6115 information (but NAME might contain it). */
6116
6117 if (sym != NULL
6118 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6119 return stab;
6120 if (with_opaque != NULL
6121 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6122 stab_best = stab;
6123
6124 /* Keep looking through other CUs. */
6125 }
6126
6127 return stab_best;
6128 }
6129
6130 /* This dumps minimal information about .debug_names. It is called
6131 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6132 uses this to verify that .debug_names has been loaded. */
6133
6134 static void
6135 dw2_debug_names_dump (struct objfile *objfile)
6136 {
6137 struct dwarf2_per_objfile *dwarf2_per_objfile
6138 = get_dwarf2_per_objfile (objfile);
6139
6140 gdb_assert (dwarf2_per_objfile->using_index);
6141 printf_filtered (".debug_names:");
6142 if (dwarf2_per_objfile->debug_names_table)
6143 printf_filtered (" exists\n");
6144 else
6145 printf_filtered (" faked for \"readnow\"\n");
6146 printf_filtered ("\n");
6147 }
6148
6149 static void
6150 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6151 const char *func_name)
6152 {
6153 struct dwarf2_per_objfile *dwarf2_per_objfile
6154 = get_dwarf2_per_objfile (objfile);
6155
6156 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6157 if (dwarf2_per_objfile->debug_names_table)
6158 {
6159 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6160
6161 /* Note: It doesn't matter what we pass for block_index here. */
6162 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6163 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6164
6165 struct dwarf2_per_cu_data *per_cu;
6166 while ((per_cu = iter.next ()) != NULL)
6167 dw2_instantiate_symtab (per_cu);
6168 }
6169 }
6170
6171 static void
6172 dw2_debug_names_expand_symtabs_matching
6173 (struct objfile *objfile,
6174 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6175 const lookup_name_info &lookup_name,
6176 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6177 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6178 enum search_domain kind)
6179 {
6180 struct dwarf2_per_objfile *dwarf2_per_objfile
6181 = get_dwarf2_per_objfile (objfile);
6182
6183 /* debug_names_table is NULL if OBJF_READNOW. */
6184 if (!dwarf2_per_objfile->debug_names_table)
6185 return;
6186
6187 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6188
6189 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6190
6191 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6192 symbol_matcher,
6193 kind, [&] (offset_type namei)
6194 {
6195 /* The name was matched, now expand corresponding CUs that were
6196 marked. */
6197 dw2_debug_names_iterator iter (map, kind, namei);
6198
6199 struct dwarf2_per_cu_data *per_cu;
6200 while ((per_cu = iter.next ()) != NULL)
6201 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6202 expansion_notify);
6203 });
6204 }
6205
6206 const struct quick_symbol_functions dwarf2_debug_names_functions =
6207 {
6208 dw2_has_symbols,
6209 dw2_find_last_source_symtab,
6210 dw2_forget_cached_source_info,
6211 dw2_map_symtabs_matching_filename,
6212 dw2_debug_names_lookup_symbol,
6213 dw2_print_stats,
6214 dw2_debug_names_dump,
6215 dw2_relocate,
6216 dw2_debug_names_expand_symtabs_for_function,
6217 dw2_expand_all_symtabs,
6218 dw2_expand_symtabs_with_fullname,
6219 dw2_map_matching_symbols,
6220 dw2_debug_names_expand_symtabs_matching,
6221 dw2_find_pc_sect_compunit_symtab,
6222 NULL,
6223 dw2_map_symbol_filenames
6224 };
6225
6226 /* See symfile.h. */
6227
6228 bool
6229 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6230 {
6231 struct dwarf2_per_objfile *dwarf2_per_objfile
6232 = get_dwarf2_per_objfile (objfile);
6233
6234 /* If we're about to read full symbols, don't bother with the
6235 indices. In this case we also don't care if some other debug
6236 format is making psymtabs, because they are all about to be
6237 expanded anyway. */
6238 if ((objfile->flags & OBJF_READNOW))
6239 {
6240 int i;
6241
6242 dwarf2_per_objfile->using_index = 1;
6243 create_all_comp_units (dwarf2_per_objfile);
6244 create_all_type_units (dwarf2_per_objfile);
6245 dwarf2_per_objfile->quick_file_names_table =
6246 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6247
6248 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6249 + dwarf2_per_objfile->n_type_units); ++i)
6250 {
6251 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6252
6253 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6254 struct dwarf2_per_cu_quick_data);
6255 }
6256
6257 /* Return 1 so that gdb sees the "quick" functions. However,
6258 these functions will be no-ops because we will have expanded
6259 all symtabs. */
6260 *index_kind = dw_index_kind::GDB_INDEX;
6261 return true;
6262 }
6263
6264 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6265 {
6266 *index_kind = dw_index_kind::DEBUG_NAMES;
6267 return true;
6268 }
6269
6270 if (dwarf2_read_index (objfile))
6271 {
6272 *index_kind = dw_index_kind::GDB_INDEX;
6273 return true;
6274 }
6275
6276 return false;
6277 }
6278
6279 \f
6280
6281 /* Build a partial symbol table. */
6282
6283 void
6284 dwarf2_build_psymtabs (struct objfile *objfile)
6285 {
6286 struct dwarf2_per_objfile *dwarf2_per_objfile
6287 = get_dwarf2_per_objfile (objfile);
6288
6289 if (objfile->global_psymbols.capacity () == 0
6290 && objfile->static_psymbols.capacity () == 0)
6291 init_psymbol_list (objfile, 1024);
6292
6293 TRY
6294 {
6295 /* This isn't really ideal: all the data we allocate on the
6296 objfile's obstack is still uselessly kept around. However,
6297 freeing it seems unsafe. */
6298 psymtab_discarder psymtabs (objfile);
6299 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6300 psymtabs.keep ();
6301 }
6302 CATCH (except, RETURN_MASK_ERROR)
6303 {
6304 exception_print (gdb_stderr, except);
6305 }
6306 END_CATCH
6307 }
6308
6309 /* Return the total length of the CU described by HEADER. */
6310
6311 static unsigned int
6312 get_cu_length (const struct comp_unit_head *header)
6313 {
6314 return header->initial_length_size + header->length;
6315 }
6316
6317 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6318
6319 static inline bool
6320 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6321 {
6322 sect_offset bottom = cu_header->sect_off;
6323 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6324
6325 return sect_off >= bottom && sect_off < top;
6326 }
6327
6328 /* Find the base address of the compilation unit for range lists and
6329 location lists. It will normally be specified by DW_AT_low_pc.
6330 In DWARF-3 draft 4, the base address could be overridden by
6331 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6332 compilation units with discontinuous ranges. */
6333
6334 static void
6335 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6336 {
6337 struct attribute *attr;
6338
6339 cu->base_known = 0;
6340 cu->base_address = 0;
6341
6342 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6343 if (attr)
6344 {
6345 cu->base_address = attr_value_as_address (attr);
6346 cu->base_known = 1;
6347 }
6348 else
6349 {
6350 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6351 if (attr)
6352 {
6353 cu->base_address = attr_value_as_address (attr);
6354 cu->base_known = 1;
6355 }
6356 }
6357 }
6358
6359 /* Read in the comp unit header information from the debug_info at info_ptr.
6360 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6361 NOTE: This leaves members offset, first_die_offset to be filled in
6362 by the caller. */
6363
6364 static const gdb_byte *
6365 read_comp_unit_head (struct comp_unit_head *cu_header,
6366 const gdb_byte *info_ptr,
6367 struct dwarf2_section_info *section,
6368 rcuh_kind section_kind)
6369 {
6370 int signed_addr;
6371 unsigned int bytes_read;
6372 const char *filename = get_section_file_name (section);
6373 bfd *abfd = get_section_bfd_owner (section);
6374
6375 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6376 cu_header->initial_length_size = bytes_read;
6377 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6378 info_ptr += bytes_read;
6379 cu_header->version = read_2_bytes (abfd, info_ptr);
6380 info_ptr += 2;
6381 if (cu_header->version < 5)
6382 switch (section_kind)
6383 {
6384 case rcuh_kind::COMPILE:
6385 cu_header->unit_type = DW_UT_compile;
6386 break;
6387 case rcuh_kind::TYPE:
6388 cu_header->unit_type = DW_UT_type;
6389 break;
6390 default:
6391 internal_error (__FILE__, __LINE__,
6392 _("read_comp_unit_head: invalid section_kind"));
6393 }
6394 else
6395 {
6396 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6397 (read_1_byte (abfd, info_ptr));
6398 info_ptr += 1;
6399 switch (cu_header->unit_type)
6400 {
6401 case DW_UT_compile:
6402 if (section_kind != rcuh_kind::COMPILE)
6403 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6404 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6405 filename);
6406 break;
6407 case DW_UT_type:
6408 section_kind = rcuh_kind::TYPE;
6409 break;
6410 default:
6411 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6412 "(is %d, should be %d or %d) [in module %s]"),
6413 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6414 }
6415
6416 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6417 info_ptr += 1;
6418 }
6419 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6420 cu_header,
6421 &bytes_read);
6422 info_ptr += bytes_read;
6423 if (cu_header->version < 5)
6424 {
6425 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6426 info_ptr += 1;
6427 }
6428 signed_addr = bfd_get_sign_extend_vma (abfd);
6429 if (signed_addr < 0)
6430 internal_error (__FILE__, __LINE__,
6431 _("read_comp_unit_head: dwarf from non elf file"));
6432 cu_header->signed_addr_p = signed_addr;
6433
6434 if (section_kind == rcuh_kind::TYPE)
6435 {
6436 LONGEST type_offset;
6437
6438 cu_header->signature = read_8_bytes (abfd, info_ptr);
6439 info_ptr += 8;
6440
6441 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6442 info_ptr += bytes_read;
6443 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6444 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6445 error (_("Dwarf Error: Too big type_offset in compilation unit "
6446 "header (is %s) [in module %s]"), plongest (type_offset),
6447 filename);
6448 }
6449
6450 return info_ptr;
6451 }
6452
6453 /* Helper function that returns the proper abbrev section for
6454 THIS_CU. */
6455
6456 static struct dwarf2_section_info *
6457 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6458 {
6459 struct dwarf2_section_info *abbrev;
6460 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6461
6462 if (this_cu->is_dwz)
6463 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6464 else
6465 abbrev = &dwarf2_per_objfile->abbrev;
6466
6467 return abbrev;
6468 }
6469
6470 /* Subroutine of read_and_check_comp_unit_head and
6471 read_and_check_type_unit_head to simplify them.
6472 Perform various error checking on the header. */
6473
6474 static void
6475 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6476 struct comp_unit_head *header,
6477 struct dwarf2_section_info *section,
6478 struct dwarf2_section_info *abbrev_section)
6479 {
6480 const char *filename = get_section_file_name (section);
6481
6482 if (header->version < 2 || header->version > 5)
6483 error (_("Dwarf Error: wrong version in compilation unit header "
6484 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6485 filename);
6486
6487 if (to_underlying (header->abbrev_sect_off)
6488 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6489 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6490 "(offset %s + 6) [in module %s]"),
6491 sect_offset_str (header->abbrev_sect_off),
6492 sect_offset_str (header->sect_off),
6493 filename);
6494
6495 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6496 avoid potential 32-bit overflow. */
6497 if (((ULONGEST) header->sect_off + get_cu_length (header))
6498 > section->size)
6499 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6500 "(offset %s + 0) [in module %s]"),
6501 header->length, sect_offset_str (header->sect_off),
6502 filename);
6503 }
6504
6505 /* Read in a CU/TU header and perform some basic error checking.
6506 The contents of the header are stored in HEADER.
6507 The result is a pointer to the start of the first DIE. */
6508
6509 static const gdb_byte *
6510 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6511 struct comp_unit_head *header,
6512 struct dwarf2_section_info *section,
6513 struct dwarf2_section_info *abbrev_section,
6514 const gdb_byte *info_ptr,
6515 rcuh_kind section_kind)
6516 {
6517 const gdb_byte *beg_of_comp_unit = info_ptr;
6518
6519 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6520
6521 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6522
6523 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6524
6525 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6526 abbrev_section);
6527
6528 return info_ptr;
6529 }
6530
6531 /* Fetch the abbreviation table offset from a comp or type unit header. */
6532
6533 static sect_offset
6534 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6535 struct dwarf2_section_info *section,
6536 sect_offset sect_off)
6537 {
6538 bfd *abfd = get_section_bfd_owner (section);
6539 const gdb_byte *info_ptr;
6540 unsigned int initial_length_size, offset_size;
6541 uint16_t version;
6542
6543 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6544 info_ptr = section->buffer + to_underlying (sect_off);
6545 read_initial_length (abfd, info_ptr, &initial_length_size);
6546 offset_size = initial_length_size == 4 ? 4 : 8;
6547 info_ptr += initial_length_size;
6548
6549 version = read_2_bytes (abfd, info_ptr);
6550 info_ptr += 2;
6551 if (version >= 5)
6552 {
6553 /* Skip unit type and address size. */
6554 info_ptr += 2;
6555 }
6556
6557 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6558 }
6559
6560 /* Allocate a new partial symtab for file named NAME and mark this new
6561 partial symtab as being an include of PST. */
6562
6563 static void
6564 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6565 struct objfile *objfile)
6566 {
6567 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6568
6569 if (!IS_ABSOLUTE_PATH (subpst->filename))
6570 {
6571 /* It shares objfile->objfile_obstack. */
6572 subpst->dirname = pst->dirname;
6573 }
6574
6575 subpst->textlow = 0;
6576 subpst->texthigh = 0;
6577
6578 subpst->dependencies
6579 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6580 subpst->dependencies[0] = pst;
6581 subpst->number_of_dependencies = 1;
6582
6583 subpst->globals_offset = 0;
6584 subpst->n_global_syms = 0;
6585 subpst->statics_offset = 0;
6586 subpst->n_static_syms = 0;
6587 subpst->compunit_symtab = NULL;
6588 subpst->read_symtab = pst->read_symtab;
6589 subpst->readin = 0;
6590
6591 /* No private part is necessary for include psymtabs. This property
6592 can be used to differentiate between such include psymtabs and
6593 the regular ones. */
6594 subpst->read_symtab_private = NULL;
6595 }
6596
6597 /* Read the Line Number Program data and extract the list of files
6598 included by the source file represented by PST. Build an include
6599 partial symtab for each of these included files. */
6600
6601 static void
6602 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6603 struct die_info *die,
6604 struct partial_symtab *pst)
6605 {
6606 line_header_up lh;
6607 struct attribute *attr;
6608
6609 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6610 if (attr)
6611 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6612 if (lh == NULL)
6613 return; /* No linetable, so no includes. */
6614
6615 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6616 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6617 }
6618
6619 static hashval_t
6620 hash_signatured_type (const void *item)
6621 {
6622 const struct signatured_type *sig_type
6623 = (const struct signatured_type *) item;
6624
6625 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6626 return sig_type->signature;
6627 }
6628
6629 static int
6630 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6631 {
6632 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6633 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6634
6635 return lhs->signature == rhs->signature;
6636 }
6637
6638 /* Allocate a hash table for signatured types. */
6639
6640 static htab_t
6641 allocate_signatured_type_table (struct objfile *objfile)
6642 {
6643 return htab_create_alloc_ex (41,
6644 hash_signatured_type,
6645 eq_signatured_type,
6646 NULL,
6647 &objfile->objfile_obstack,
6648 hashtab_obstack_allocate,
6649 dummy_obstack_deallocate);
6650 }
6651
6652 /* A helper function to add a signatured type CU to a table. */
6653
6654 static int
6655 add_signatured_type_cu_to_table (void **slot, void *datum)
6656 {
6657 struct signatured_type *sigt = (struct signatured_type *) *slot;
6658 struct signatured_type ***datap = (struct signatured_type ***) datum;
6659
6660 **datap = sigt;
6661 ++*datap;
6662
6663 return 1;
6664 }
6665
6666 /* A helper for create_debug_types_hash_table. Read types from SECTION
6667 and fill them into TYPES_HTAB. It will process only type units,
6668 therefore DW_UT_type. */
6669
6670 static void
6671 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6672 struct dwo_file *dwo_file,
6673 dwarf2_section_info *section, htab_t &types_htab,
6674 rcuh_kind section_kind)
6675 {
6676 struct objfile *objfile = dwarf2_per_objfile->objfile;
6677 struct dwarf2_section_info *abbrev_section;
6678 bfd *abfd;
6679 const gdb_byte *info_ptr, *end_ptr;
6680
6681 abbrev_section = (dwo_file != NULL
6682 ? &dwo_file->sections.abbrev
6683 : &dwarf2_per_objfile->abbrev);
6684
6685 if (dwarf_read_debug)
6686 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6687 get_section_name (section),
6688 get_section_file_name (abbrev_section));
6689
6690 dwarf2_read_section (objfile, section);
6691 info_ptr = section->buffer;
6692
6693 if (info_ptr == NULL)
6694 return;
6695
6696 /* We can't set abfd until now because the section may be empty or
6697 not present, in which case the bfd is unknown. */
6698 abfd = get_section_bfd_owner (section);
6699
6700 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6701 because we don't need to read any dies: the signature is in the
6702 header. */
6703
6704 end_ptr = info_ptr + section->size;
6705 while (info_ptr < end_ptr)
6706 {
6707 struct signatured_type *sig_type;
6708 struct dwo_unit *dwo_tu;
6709 void **slot;
6710 const gdb_byte *ptr = info_ptr;
6711 struct comp_unit_head header;
6712 unsigned int length;
6713
6714 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6715
6716 /* Initialize it due to a false compiler warning. */
6717 header.signature = -1;
6718 header.type_cu_offset_in_tu = (cu_offset) -1;
6719
6720 /* We need to read the type's signature in order to build the hash
6721 table, but we don't need anything else just yet. */
6722
6723 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6724 abbrev_section, ptr, section_kind);
6725
6726 length = get_cu_length (&header);
6727
6728 /* Skip dummy type units. */
6729 if (ptr >= info_ptr + length
6730 || peek_abbrev_code (abfd, ptr) == 0
6731 || header.unit_type != DW_UT_type)
6732 {
6733 info_ptr += length;
6734 continue;
6735 }
6736
6737 if (types_htab == NULL)
6738 {
6739 if (dwo_file)
6740 types_htab = allocate_dwo_unit_table (objfile);
6741 else
6742 types_htab = allocate_signatured_type_table (objfile);
6743 }
6744
6745 if (dwo_file)
6746 {
6747 sig_type = NULL;
6748 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6749 struct dwo_unit);
6750 dwo_tu->dwo_file = dwo_file;
6751 dwo_tu->signature = header.signature;
6752 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6753 dwo_tu->section = section;
6754 dwo_tu->sect_off = sect_off;
6755 dwo_tu->length = length;
6756 }
6757 else
6758 {
6759 /* N.B.: type_offset is not usable if this type uses a DWO file.
6760 The real type_offset is in the DWO file. */
6761 dwo_tu = NULL;
6762 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6763 struct signatured_type);
6764 sig_type->signature = header.signature;
6765 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6766 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6767 sig_type->per_cu.is_debug_types = 1;
6768 sig_type->per_cu.section = section;
6769 sig_type->per_cu.sect_off = sect_off;
6770 sig_type->per_cu.length = length;
6771 }
6772
6773 slot = htab_find_slot (types_htab,
6774 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6775 INSERT);
6776 gdb_assert (slot != NULL);
6777 if (*slot != NULL)
6778 {
6779 sect_offset dup_sect_off;
6780
6781 if (dwo_file)
6782 {
6783 const struct dwo_unit *dup_tu
6784 = (const struct dwo_unit *) *slot;
6785
6786 dup_sect_off = dup_tu->sect_off;
6787 }
6788 else
6789 {
6790 const struct signatured_type *dup_tu
6791 = (const struct signatured_type *) *slot;
6792
6793 dup_sect_off = dup_tu->per_cu.sect_off;
6794 }
6795
6796 complaint (&symfile_complaints,
6797 _("debug type entry at offset %s is duplicate to"
6798 " the entry at offset %s, signature %s"),
6799 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6800 hex_string (header.signature));
6801 }
6802 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6803
6804 if (dwarf_read_debug > 1)
6805 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6806 sect_offset_str (sect_off),
6807 hex_string (header.signature));
6808
6809 info_ptr += length;
6810 }
6811 }
6812
6813 /* Create the hash table of all entries in the .debug_types
6814 (or .debug_types.dwo) section(s).
6815 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6816 otherwise it is NULL.
6817
6818 The result is a pointer to the hash table or NULL if there are no types.
6819
6820 Note: This function processes DWO files only, not DWP files. */
6821
6822 static void
6823 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6824 struct dwo_file *dwo_file,
6825 VEC (dwarf2_section_info_def) *types,
6826 htab_t &types_htab)
6827 {
6828 int ix;
6829 struct dwarf2_section_info *section;
6830
6831 if (VEC_empty (dwarf2_section_info_def, types))
6832 return;
6833
6834 for (ix = 0;
6835 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6836 ++ix)
6837 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6838 types_htab, rcuh_kind::TYPE);
6839 }
6840
6841 /* Create the hash table of all entries in the .debug_types section,
6842 and initialize all_type_units.
6843 The result is zero if there is an error (e.g. missing .debug_types section),
6844 otherwise non-zero. */
6845
6846 static int
6847 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6848 {
6849 htab_t types_htab = NULL;
6850 struct signatured_type **iter;
6851
6852 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6853 &dwarf2_per_objfile->info, types_htab,
6854 rcuh_kind::COMPILE);
6855 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6856 dwarf2_per_objfile->types, types_htab);
6857 if (types_htab == NULL)
6858 {
6859 dwarf2_per_objfile->signatured_types = NULL;
6860 return 0;
6861 }
6862
6863 dwarf2_per_objfile->signatured_types = types_htab;
6864
6865 dwarf2_per_objfile->n_type_units
6866 = dwarf2_per_objfile->n_allocated_type_units
6867 = htab_elements (types_htab);
6868 dwarf2_per_objfile->all_type_units =
6869 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
6870 iter = &dwarf2_per_objfile->all_type_units[0];
6871 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
6872 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
6873 == dwarf2_per_objfile->n_type_units);
6874
6875 return 1;
6876 }
6877
6878 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6879 If SLOT is non-NULL, it is the entry to use in the hash table.
6880 Otherwise we find one. */
6881
6882 static struct signatured_type *
6883 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6884 void **slot)
6885 {
6886 struct objfile *objfile = dwarf2_per_objfile->objfile;
6887 int n_type_units = dwarf2_per_objfile->n_type_units;
6888 struct signatured_type *sig_type;
6889
6890 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
6891 ++n_type_units;
6892 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
6893 {
6894 if (dwarf2_per_objfile->n_allocated_type_units == 0)
6895 dwarf2_per_objfile->n_allocated_type_units = 1;
6896 dwarf2_per_objfile->n_allocated_type_units *= 2;
6897 dwarf2_per_objfile->all_type_units
6898 = XRESIZEVEC (struct signatured_type *,
6899 dwarf2_per_objfile->all_type_units,
6900 dwarf2_per_objfile->n_allocated_type_units);
6901 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6902 }
6903 dwarf2_per_objfile->n_type_units = n_type_units;
6904
6905 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6906 struct signatured_type);
6907 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
6908 sig_type->signature = sig;
6909 sig_type->per_cu.is_debug_types = 1;
6910 if (dwarf2_per_objfile->using_index)
6911 {
6912 sig_type->per_cu.v.quick =
6913 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6914 struct dwarf2_per_cu_quick_data);
6915 }
6916
6917 if (slot == NULL)
6918 {
6919 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6920 sig_type, INSERT);
6921 }
6922 gdb_assert (*slot == NULL);
6923 *slot = sig_type;
6924 /* The rest of sig_type must be filled in by the caller. */
6925 return sig_type;
6926 }
6927
6928 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6929 Fill in SIG_ENTRY with DWO_ENTRY. */
6930
6931 static void
6932 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6933 struct signatured_type *sig_entry,
6934 struct dwo_unit *dwo_entry)
6935 {
6936 /* Make sure we're not clobbering something we don't expect to. */
6937 gdb_assert (! sig_entry->per_cu.queued);
6938 gdb_assert (sig_entry->per_cu.cu == NULL);
6939 if (dwarf2_per_objfile->using_index)
6940 {
6941 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6942 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6943 }
6944 else
6945 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6946 gdb_assert (sig_entry->signature == dwo_entry->signature);
6947 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6948 gdb_assert (sig_entry->type_unit_group == NULL);
6949 gdb_assert (sig_entry->dwo_unit == NULL);
6950
6951 sig_entry->per_cu.section = dwo_entry->section;
6952 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6953 sig_entry->per_cu.length = dwo_entry->length;
6954 sig_entry->per_cu.reading_dwo_directly = 1;
6955 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6956 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6957 sig_entry->dwo_unit = dwo_entry;
6958 }
6959
6960 /* Subroutine of lookup_signatured_type.
6961 If we haven't read the TU yet, create the signatured_type data structure
6962 for a TU to be read in directly from a DWO file, bypassing the stub.
6963 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6964 using .gdb_index, then when reading a CU we want to stay in the DWO file
6965 containing that CU. Otherwise we could end up reading several other DWO
6966 files (due to comdat folding) to process the transitive closure of all the
6967 mentioned TUs, and that can be slow. The current DWO file will have every
6968 type signature that it needs.
6969 We only do this for .gdb_index because in the psymtab case we already have
6970 to read all the DWOs to build the type unit groups. */
6971
6972 static struct signatured_type *
6973 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6974 {
6975 struct dwarf2_per_objfile *dwarf2_per_objfile
6976 = cu->per_cu->dwarf2_per_objfile;
6977 struct objfile *objfile = dwarf2_per_objfile->objfile;
6978 struct dwo_file *dwo_file;
6979 struct dwo_unit find_dwo_entry, *dwo_entry;
6980 struct signatured_type find_sig_entry, *sig_entry;
6981 void **slot;
6982
6983 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6984
6985 /* If TU skeletons have been removed then we may not have read in any
6986 TUs yet. */
6987 if (dwarf2_per_objfile->signatured_types == NULL)
6988 {
6989 dwarf2_per_objfile->signatured_types
6990 = allocate_signatured_type_table (objfile);
6991 }
6992
6993 /* We only ever need to read in one copy of a signatured type.
6994 Use the global signatured_types array to do our own comdat-folding
6995 of types. If this is the first time we're reading this TU, and
6996 the TU has an entry in .gdb_index, replace the recorded data from
6997 .gdb_index with this TU. */
6998
6999 find_sig_entry.signature = sig;
7000 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7001 &find_sig_entry, INSERT);
7002 sig_entry = (struct signatured_type *) *slot;
7003
7004 /* We can get here with the TU already read, *or* in the process of being
7005 read. Don't reassign the global entry to point to this DWO if that's
7006 the case. Also note that if the TU is already being read, it may not
7007 have come from a DWO, the program may be a mix of Fission-compiled
7008 code and non-Fission-compiled code. */
7009
7010 /* Have we already tried to read this TU?
7011 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7012 needn't exist in the global table yet). */
7013 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7014 return sig_entry;
7015
7016 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7017 dwo_unit of the TU itself. */
7018 dwo_file = cu->dwo_unit->dwo_file;
7019
7020 /* Ok, this is the first time we're reading this TU. */
7021 if (dwo_file->tus == NULL)
7022 return NULL;
7023 find_dwo_entry.signature = sig;
7024 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7025 if (dwo_entry == NULL)
7026 return NULL;
7027
7028 /* If the global table doesn't have an entry for this TU, add one. */
7029 if (sig_entry == NULL)
7030 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7031
7032 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7033 sig_entry->per_cu.tu_read = 1;
7034 return sig_entry;
7035 }
7036
7037 /* Subroutine of lookup_signatured_type.
7038 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7039 then try the DWP file. If the TU stub (skeleton) has been removed then
7040 it won't be in .gdb_index. */
7041
7042 static struct signatured_type *
7043 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7044 {
7045 struct dwarf2_per_objfile *dwarf2_per_objfile
7046 = cu->per_cu->dwarf2_per_objfile;
7047 struct objfile *objfile = dwarf2_per_objfile->objfile;
7048 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7049 struct dwo_unit *dwo_entry;
7050 struct signatured_type find_sig_entry, *sig_entry;
7051 void **slot;
7052
7053 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7054 gdb_assert (dwp_file != NULL);
7055
7056 /* If TU skeletons have been removed then we may not have read in any
7057 TUs yet. */
7058 if (dwarf2_per_objfile->signatured_types == NULL)
7059 {
7060 dwarf2_per_objfile->signatured_types
7061 = allocate_signatured_type_table (objfile);
7062 }
7063
7064 find_sig_entry.signature = sig;
7065 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7066 &find_sig_entry, INSERT);
7067 sig_entry = (struct signatured_type *) *slot;
7068
7069 /* Have we already tried to read this TU?
7070 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7071 needn't exist in the global table yet). */
7072 if (sig_entry != NULL)
7073 return sig_entry;
7074
7075 if (dwp_file->tus == NULL)
7076 return NULL;
7077 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7078 sig, 1 /* is_debug_types */);
7079 if (dwo_entry == NULL)
7080 return NULL;
7081
7082 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7083 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7084
7085 return sig_entry;
7086 }
7087
7088 /* Lookup a signature based type for DW_FORM_ref_sig8.
7089 Returns NULL if signature SIG is not present in the table.
7090 It is up to the caller to complain about this. */
7091
7092 static struct signatured_type *
7093 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7094 {
7095 struct dwarf2_per_objfile *dwarf2_per_objfile
7096 = cu->per_cu->dwarf2_per_objfile;
7097
7098 if (cu->dwo_unit
7099 && dwarf2_per_objfile->using_index)
7100 {
7101 /* We're in a DWO/DWP file, and we're using .gdb_index.
7102 These cases require special processing. */
7103 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7104 return lookup_dwo_signatured_type (cu, sig);
7105 else
7106 return lookup_dwp_signatured_type (cu, sig);
7107 }
7108 else
7109 {
7110 struct signatured_type find_entry, *entry;
7111
7112 if (dwarf2_per_objfile->signatured_types == NULL)
7113 return NULL;
7114 find_entry.signature = sig;
7115 entry = ((struct signatured_type *)
7116 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7117 return entry;
7118 }
7119 }
7120 \f
7121 /* Low level DIE reading support. */
7122
7123 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7124
7125 static void
7126 init_cu_die_reader (struct die_reader_specs *reader,
7127 struct dwarf2_cu *cu,
7128 struct dwarf2_section_info *section,
7129 struct dwo_file *dwo_file,
7130 struct abbrev_table *abbrev_table)
7131 {
7132 gdb_assert (section->readin && section->buffer != NULL);
7133 reader->abfd = get_section_bfd_owner (section);
7134 reader->cu = cu;
7135 reader->dwo_file = dwo_file;
7136 reader->die_section = section;
7137 reader->buffer = section->buffer;
7138 reader->buffer_end = section->buffer + section->size;
7139 reader->comp_dir = NULL;
7140 reader->abbrev_table = abbrev_table;
7141 }
7142
7143 /* Subroutine of init_cutu_and_read_dies to simplify it.
7144 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7145 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7146 already.
7147
7148 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7149 from it to the DIE in the DWO. If NULL we are skipping the stub.
7150 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7151 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7152 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7153 STUB_COMP_DIR may be non-NULL.
7154 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7155 are filled in with the info of the DIE from the DWO file.
7156 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7157 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7158 kept around for at least as long as *RESULT_READER.
7159
7160 The result is non-zero if a valid (non-dummy) DIE was found. */
7161
7162 static int
7163 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7164 struct dwo_unit *dwo_unit,
7165 struct die_info *stub_comp_unit_die,
7166 const char *stub_comp_dir,
7167 struct die_reader_specs *result_reader,
7168 const gdb_byte **result_info_ptr,
7169 struct die_info **result_comp_unit_die,
7170 int *result_has_children,
7171 abbrev_table_up *result_dwo_abbrev_table)
7172 {
7173 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7174 struct objfile *objfile = dwarf2_per_objfile->objfile;
7175 struct dwarf2_cu *cu = this_cu->cu;
7176 bfd *abfd;
7177 const gdb_byte *begin_info_ptr, *info_ptr;
7178 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7179 int i,num_extra_attrs;
7180 struct dwarf2_section_info *dwo_abbrev_section;
7181 struct attribute *attr;
7182 struct die_info *comp_unit_die;
7183
7184 /* At most one of these may be provided. */
7185 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7186
7187 /* These attributes aren't processed until later:
7188 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7189 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7190 referenced later. However, these attributes are found in the stub
7191 which we won't have later. In order to not impose this complication
7192 on the rest of the code, we read them here and copy them to the
7193 DWO CU/TU die. */
7194
7195 stmt_list = NULL;
7196 low_pc = NULL;
7197 high_pc = NULL;
7198 ranges = NULL;
7199 comp_dir = NULL;
7200
7201 if (stub_comp_unit_die != NULL)
7202 {
7203 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7204 DWO file. */
7205 if (! this_cu->is_debug_types)
7206 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7207 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7208 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7209 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7210 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7211
7212 /* There should be a DW_AT_addr_base attribute here (if needed).
7213 We need the value before we can process DW_FORM_GNU_addr_index. */
7214 cu->addr_base = 0;
7215 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7216 if (attr)
7217 cu->addr_base = DW_UNSND (attr);
7218
7219 /* There should be a DW_AT_ranges_base attribute here (if needed).
7220 We need the value before we can process DW_AT_ranges. */
7221 cu->ranges_base = 0;
7222 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7223 if (attr)
7224 cu->ranges_base = DW_UNSND (attr);
7225 }
7226 else if (stub_comp_dir != NULL)
7227 {
7228 /* Reconstruct the comp_dir attribute to simplify the code below. */
7229 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7230 comp_dir->name = DW_AT_comp_dir;
7231 comp_dir->form = DW_FORM_string;
7232 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7233 DW_STRING (comp_dir) = stub_comp_dir;
7234 }
7235
7236 /* Set up for reading the DWO CU/TU. */
7237 cu->dwo_unit = dwo_unit;
7238 dwarf2_section_info *section = dwo_unit->section;
7239 dwarf2_read_section (objfile, section);
7240 abfd = get_section_bfd_owner (section);
7241 begin_info_ptr = info_ptr = (section->buffer
7242 + to_underlying (dwo_unit->sect_off));
7243 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7244
7245 if (this_cu->is_debug_types)
7246 {
7247 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7248
7249 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7250 &cu->header, section,
7251 dwo_abbrev_section,
7252 info_ptr, rcuh_kind::TYPE);
7253 /* This is not an assert because it can be caused by bad debug info. */
7254 if (sig_type->signature != cu->header.signature)
7255 {
7256 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7257 " TU at offset %s [in module %s]"),
7258 hex_string (sig_type->signature),
7259 hex_string (cu->header.signature),
7260 sect_offset_str (dwo_unit->sect_off),
7261 bfd_get_filename (abfd));
7262 }
7263 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7264 /* For DWOs coming from DWP files, we don't know the CU length
7265 nor the type's offset in the TU until now. */
7266 dwo_unit->length = get_cu_length (&cu->header);
7267 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7268
7269 /* Establish the type offset that can be used to lookup the type.
7270 For DWO files, we don't know it until now. */
7271 sig_type->type_offset_in_section
7272 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7273 }
7274 else
7275 {
7276 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7277 &cu->header, section,
7278 dwo_abbrev_section,
7279 info_ptr, rcuh_kind::COMPILE);
7280 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7281 /* For DWOs coming from DWP files, we don't know the CU length
7282 until now. */
7283 dwo_unit->length = get_cu_length (&cu->header);
7284 }
7285
7286 *result_dwo_abbrev_table
7287 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7288 cu->header.abbrev_sect_off);
7289 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7290 result_dwo_abbrev_table->get ());
7291
7292 /* Read in the die, but leave space to copy over the attributes
7293 from the stub. This has the benefit of simplifying the rest of
7294 the code - all the work to maintain the illusion of a single
7295 DW_TAG_{compile,type}_unit DIE is done here. */
7296 num_extra_attrs = ((stmt_list != NULL)
7297 + (low_pc != NULL)
7298 + (high_pc != NULL)
7299 + (ranges != NULL)
7300 + (comp_dir != NULL));
7301 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7302 result_has_children, num_extra_attrs);
7303
7304 /* Copy over the attributes from the stub to the DIE we just read in. */
7305 comp_unit_die = *result_comp_unit_die;
7306 i = comp_unit_die->num_attrs;
7307 if (stmt_list != NULL)
7308 comp_unit_die->attrs[i++] = *stmt_list;
7309 if (low_pc != NULL)
7310 comp_unit_die->attrs[i++] = *low_pc;
7311 if (high_pc != NULL)
7312 comp_unit_die->attrs[i++] = *high_pc;
7313 if (ranges != NULL)
7314 comp_unit_die->attrs[i++] = *ranges;
7315 if (comp_dir != NULL)
7316 comp_unit_die->attrs[i++] = *comp_dir;
7317 comp_unit_die->num_attrs += num_extra_attrs;
7318
7319 if (dwarf_die_debug)
7320 {
7321 fprintf_unfiltered (gdb_stdlog,
7322 "Read die from %s@0x%x of %s:\n",
7323 get_section_name (section),
7324 (unsigned) (begin_info_ptr - section->buffer),
7325 bfd_get_filename (abfd));
7326 dump_die (comp_unit_die, dwarf_die_debug);
7327 }
7328
7329 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7330 TUs by skipping the stub and going directly to the entry in the DWO file.
7331 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7332 to get it via circuitous means. Blech. */
7333 if (comp_dir != NULL)
7334 result_reader->comp_dir = DW_STRING (comp_dir);
7335
7336 /* Skip dummy compilation units. */
7337 if (info_ptr >= begin_info_ptr + dwo_unit->length
7338 || peek_abbrev_code (abfd, info_ptr) == 0)
7339 return 0;
7340
7341 *result_info_ptr = info_ptr;
7342 return 1;
7343 }
7344
7345 /* Subroutine of init_cutu_and_read_dies to simplify it.
7346 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7347 Returns NULL if the specified DWO unit cannot be found. */
7348
7349 static struct dwo_unit *
7350 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7351 struct die_info *comp_unit_die)
7352 {
7353 struct dwarf2_cu *cu = this_cu->cu;
7354 ULONGEST signature;
7355 struct dwo_unit *dwo_unit;
7356 const char *comp_dir, *dwo_name;
7357
7358 gdb_assert (cu != NULL);
7359
7360 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7361 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7362 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7363
7364 if (this_cu->is_debug_types)
7365 {
7366 struct signatured_type *sig_type;
7367
7368 /* Since this_cu is the first member of struct signatured_type,
7369 we can go from a pointer to one to a pointer to the other. */
7370 sig_type = (struct signatured_type *) this_cu;
7371 signature = sig_type->signature;
7372 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7373 }
7374 else
7375 {
7376 struct attribute *attr;
7377
7378 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7379 if (! attr)
7380 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7381 " [in module %s]"),
7382 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7383 signature = DW_UNSND (attr);
7384 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7385 signature);
7386 }
7387
7388 return dwo_unit;
7389 }
7390
7391 /* Subroutine of init_cutu_and_read_dies to simplify it.
7392 See it for a description of the parameters.
7393 Read a TU directly from a DWO file, bypassing the stub. */
7394
7395 static void
7396 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7397 int use_existing_cu, int keep,
7398 die_reader_func_ftype *die_reader_func,
7399 void *data)
7400 {
7401 std::unique_ptr<dwarf2_cu> new_cu;
7402 struct signatured_type *sig_type;
7403 struct die_reader_specs reader;
7404 const gdb_byte *info_ptr;
7405 struct die_info *comp_unit_die;
7406 int has_children;
7407 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7408
7409 /* Verify we can do the following downcast, and that we have the
7410 data we need. */
7411 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7412 sig_type = (struct signatured_type *) this_cu;
7413 gdb_assert (sig_type->dwo_unit != NULL);
7414
7415 if (use_existing_cu && this_cu->cu != NULL)
7416 {
7417 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7418 /* There's no need to do the rereading_dwo_cu handling that
7419 init_cutu_and_read_dies does since we don't read the stub. */
7420 }
7421 else
7422 {
7423 /* If !use_existing_cu, this_cu->cu must be NULL. */
7424 gdb_assert (this_cu->cu == NULL);
7425 new_cu.reset (new dwarf2_cu (this_cu));
7426 }
7427
7428 /* A future optimization, if needed, would be to use an existing
7429 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7430 could share abbrev tables. */
7431
7432 /* The abbreviation table used by READER, this must live at least as long as
7433 READER. */
7434 abbrev_table_up dwo_abbrev_table;
7435
7436 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7437 NULL /* stub_comp_unit_die */,
7438 sig_type->dwo_unit->dwo_file->comp_dir,
7439 &reader, &info_ptr,
7440 &comp_unit_die, &has_children,
7441 &dwo_abbrev_table) == 0)
7442 {
7443 /* Dummy die. */
7444 return;
7445 }
7446
7447 /* All the "real" work is done here. */
7448 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7449
7450 /* This duplicates the code in init_cutu_and_read_dies,
7451 but the alternative is making the latter more complex.
7452 This function is only for the special case of using DWO files directly:
7453 no point in overly complicating the general case just to handle this. */
7454 if (new_cu != NULL && keep)
7455 {
7456 /* Link this CU into read_in_chain. */
7457 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7458 dwarf2_per_objfile->read_in_chain = this_cu;
7459 /* The chain owns it now. */
7460 new_cu.release ();
7461 }
7462 }
7463
7464 /* Initialize a CU (or TU) and read its DIEs.
7465 If the CU defers to a DWO file, read the DWO file as well.
7466
7467 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7468 Otherwise the table specified in the comp unit header is read in and used.
7469 This is an optimization for when we already have the abbrev table.
7470
7471 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7472 Otherwise, a new CU is allocated with xmalloc.
7473
7474 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7475 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7476
7477 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7478 linker) then DIE_READER_FUNC will not get called. */
7479
7480 static void
7481 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7482 struct abbrev_table *abbrev_table,
7483 int use_existing_cu, int keep,
7484 die_reader_func_ftype *die_reader_func,
7485 void *data)
7486 {
7487 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7488 struct objfile *objfile = dwarf2_per_objfile->objfile;
7489 struct dwarf2_section_info *section = this_cu->section;
7490 bfd *abfd = get_section_bfd_owner (section);
7491 struct dwarf2_cu *cu;
7492 const gdb_byte *begin_info_ptr, *info_ptr;
7493 struct die_reader_specs reader;
7494 struct die_info *comp_unit_die;
7495 int has_children;
7496 struct attribute *attr;
7497 struct signatured_type *sig_type = NULL;
7498 struct dwarf2_section_info *abbrev_section;
7499 /* Non-zero if CU currently points to a DWO file and we need to
7500 reread it. When this happens we need to reread the skeleton die
7501 before we can reread the DWO file (this only applies to CUs, not TUs). */
7502 int rereading_dwo_cu = 0;
7503
7504 if (dwarf_die_debug)
7505 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7506 this_cu->is_debug_types ? "type" : "comp",
7507 sect_offset_str (this_cu->sect_off));
7508
7509 if (use_existing_cu)
7510 gdb_assert (keep);
7511
7512 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7513 file (instead of going through the stub), short-circuit all of this. */
7514 if (this_cu->reading_dwo_directly)
7515 {
7516 /* Narrow down the scope of possibilities to have to understand. */
7517 gdb_assert (this_cu->is_debug_types);
7518 gdb_assert (abbrev_table == NULL);
7519 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7520 die_reader_func, data);
7521 return;
7522 }
7523
7524 /* This is cheap if the section is already read in. */
7525 dwarf2_read_section (objfile, section);
7526
7527 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7528
7529 abbrev_section = get_abbrev_section_for_cu (this_cu);
7530
7531 std::unique_ptr<dwarf2_cu> new_cu;
7532 if (use_existing_cu && this_cu->cu != NULL)
7533 {
7534 cu = this_cu->cu;
7535 /* If this CU is from a DWO file we need to start over, we need to
7536 refetch the attributes from the skeleton CU.
7537 This could be optimized by retrieving those attributes from when we
7538 were here the first time: the previous comp_unit_die was stored in
7539 comp_unit_obstack. But there's no data yet that we need this
7540 optimization. */
7541 if (cu->dwo_unit != NULL)
7542 rereading_dwo_cu = 1;
7543 }
7544 else
7545 {
7546 /* If !use_existing_cu, this_cu->cu must be NULL. */
7547 gdb_assert (this_cu->cu == NULL);
7548 new_cu.reset (new dwarf2_cu (this_cu));
7549 cu = new_cu.get ();
7550 }
7551
7552 /* Get the header. */
7553 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7554 {
7555 /* We already have the header, there's no need to read it in again. */
7556 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7557 }
7558 else
7559 {
7560 if (this_cu->is_debug_types)
7561 {
7562 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7563 &cu->header, section,
7564 abbrev_section, info_ptr,
7565 rcuh_kind::TYPE);
7566
7567 /* Since per_cu is the first member of struct signatured_type,
7568 we can go from a pointer to one to a pointer to the other. */
7569 sig_type = (struct signatured_type *) this_cu;
7570 gdb_assert (sig_type->signature == cu->header.signature);
7571 gdb_assert (sig_type->type_offset_in_tu
7572 == cu->header.type_cu_offset_in_tu);
7573 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7574
7575 /* LENGTH has not been set yet for type units if we're
7576 using .gdb_index. */
7577 this_cu->length = get_cu_length (&cu->header);
7578
7579 /* Establish the type offset that can be used to lookup the type. */
7580 sig_type->type_offset_in_section =
7581 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7582
7583 this_cu->dwarf_version = cu->header.version;
7584 }
7585 else
7586 {
7587 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7588 &cu->header, section,
7589 abbrev_section,
7590 info_ptr,
7591 rcuh_kind::COMPILE);
7592
7593 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7594 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7595 this_cu->dwarf_version = cu->header.version;
7596 }
7597 }
7598
7599 /* Skip dummy compilation units. */
7600 if (info_ptr >= begin_info_ptr + this_cu->length
7601 || peek_abbrev_code (abfd, info_ptr) == 0)
7602 return;
7603
7604 /* If we don't have them yet, read the abbrevs for this compilation unit.
7605 And if we need to read them now, make sure they're freed when we're
7606 done (own the table through ABBREV_TABLE_HOLDER). */
7607 abbrev_table_up abbrev_table_holder;
7608 if (abbrev_table != NULL)
7609 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7610 else
7611 {
7612 abbrev_table_holder
7613 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7614 cu->header.abbrev_sect_off);
7615 abbrev_table = abbrev_table_holder.get ();
7616 }
7617
7618 /* Read the top level CU/TU die. */
7619 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7620 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7621
7622 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7623 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7624 table from the DWO file and pass the ownership over to us. It will be
7625 referenced from READER, so we must make sure to free it after we're done
7626 with READER.
7627
7628 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7629 DWO CU, that this test will fail (the attribute will not be present). */
7630 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7631 abbrev_table_up dwo_abbrev_table;
7632 if (attr)
7633 {
7634 struct dwo_unit *dwo_unit;
7635 struct die_info *dwo_comp_unit_die;
7636
7637 if (has_children)
7638 {
7639 complaint (&symfile_complaints,
7640 _("compilation unit with DW_AT_GNU_dwo_name"
7641 " has children (offset %s) [in module %s]"),
7642 sect_offset_str (this_cu->sect_off),
7643 bfd_get_filename (abfd));
7644 }
7645 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7646 if (dwo_unit != NULL)
7647 {
7648 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7649 comp_unit_die, NULL,
7650 &reader, &info_ptr,
7651 &dwo_comp_unit_die, &has_children,
7652 &dwo_abbrev_table) == 0)
7653 {
7654 /* Dummy die. */
7655 return;
7656 }
7657 comp_unit_die = dwo_comp_unit_die;
7658 }
7659 else
7660 {
7661 /* Yikes, we couldn't find the rest of the DIE, we only have
7662 the stub. A complaint has already been logged. There's
7663 not much more we can do except pass on the stub DIE to
7664 die_reader_func. We don't want to throw an error on bad
7665 debug info. */
7666 }
7667 }
7668
7669 /* All of the above is setup for this call. Yikes. */
7670 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7671
7672 /* Done, clean up. */
7673 if (new_cu != NULL && keep)
7674 {
7675 /* Link this CU into read_in_chain. */
7676 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7677 dwarf2_per_objfile->read_in_chain = this_cu;
7678 /* The chain owns it now. */
7679 new_cu.release ();
7680 }
7681 }
7682
7683 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7684 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7685 to have already done the lookup to find the DWO file).
7686
7687 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7688 THIS_CU->is_debug_types, but nothing else.
7689
7690 We fill in THIS_CU->length.
7691
7692 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7693 linker) then DIE_READER_FUNC will not get called.
7694
7695 THIS_CU->cu is always freed when done.
7696 This is done in order to not leave THIS_CU->cu in a state where we have
7697 to care whether it refers to the "main" CU or the DWO CU. */
7698
7699 static void
7700 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7701 struct dwo_file *dwo_file,
7702 die_reader_func_ftype *die_reader_func,
7703 void *data)
7704 {
7705 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7706 struct objfile *objfile = dwarf2_per_objfile->objfile;
7707 struct dwarf2_section_info *section = this_cu->section;
7708 bfd *abfd = get_section_bfd_owner (section);
7709 struct dwarf2_section_info *abbrev_section;
7710 const gdb_byte *begin_info_ptr, *info_ptr;
7711 struct die_reader_specs reader;
7712 struct die_info *comp_unit_die;
7713 int has_children;
7714
7715 if (dwarf_die_debug)
7716 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7717 this_cu->is_debug_types ? "type" : "comp",
7718 sect_offset_str (this_cu->sect_off));
7719
7720 gdb_assert (this_cu->cu == NULL);
7721
7722 abbrev_section = (dwo_file != NULL
7723 ? &dwo_file->sections.abbrev
7724 : get_abbrev_section_for_cu (this_cu));
7725
7726 /* This is cheap if the section is already read in. */
7727 dwarf2_read_section (objfile, section);
7728
7729 struct dwarf2_cu cu (this_cu);
7730
7731 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7732 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7733 &cu.header, section,
7734 abbrev_section, info_ptr,
7735 (this_cu->is_debug_types
7736 ? rcuh_kind::TYPE
7737 : rcuh_kind::COMPILE));
7738
7739 this_cu->length = get_cu_length (&cu.header);
7740
7741 /* Skip dummy compilation units. */
7742 if (info_ptr >= begin_info_ptr + this_cu->length
7743 || peek_abbrev_code (abfd, info_ptr) == 0)
7744 return;
7745
7746 abbrev_table_up abbrev_table
7747 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7748 cu.header.abbrev_sect_off);
7749
7750 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7751 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7752
7753 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7754 }
7755
7756 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7757 does not lookup the specified DWO file.
7758 This cannot be used to read DWO files.
7759
7760 THIS_CU->cu is always freed when done.
7761 This is done in order to not leave THIS_CU->cu in a state where we have
7762 to care whether it refers to the "main" CU or the DWO CU.
7763 We can revisit this if the data shows there's a performance issue. */
7764
7765 static void
7766 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7767 die_reader_func_ftype *die_reader_func,
7768 void *data)
7769 {
7770 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7771 }
7772 \f
7773 /* Type Unit Groups.
7774
7775 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7776 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7777 so that all types coming from the same compilation (.o file) are grouped
7778 together. A future step could be to put the types in the same symtab as
7779 the CU the types ultimately came from. */
7780
7781 static hashval_t
7782 hash_type_unit_group (const void *item)
7783 {
7784 const struct type_unit_group *tu_group
7785 = (const struct type_unit_group *) item;
7786
7787 return hash_stmt_list_entry (&tu_group->hash);
7788 }
7789
7790 static int
7791 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7792 {
7793 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7794 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7795
7796 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7797 }
7798
7799 /* Allocate a hash table for type unit groups. */
7800
7801 static htab_t
7802 allocate_type_unit_groups_table (struct objfile *objfile)
7803 {
7804 return htab_create_alloc_ex (3,
7805 hash_type_unit_group,
7806 eq_type_unit_group,
7807 NULL,
7808 &objfile->objfile_obstack,
7809 hashtab_obstack_allocate,
7810 dummy_obstack_deallocate);
7811 }
7812
7813 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7814 partial symtabs. We combine several TUs per psymtab to not let the size
7815 of any one psymtab grow too big. */
7816 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7817 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7818
7819 /* Helper routine for get_type_unit_group.
7820 Create the type_unit_group object used to hold one or more TUs. */
7821
7822 static struct type_unit_group *
7823 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7824 {
7825 struct dwarf2_per_objfile *dwarf2_per_objfile
7826 = cu->per_cu->dwarf2_per_objfile;
7827 struct objfile *objfile = dwarf2_per_objfile->objfile;
7828 struct dwarf2_per_cu_data *per_cu;
7829 struct type_unit_group *tu_group;
7830
7831 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7832 struct type_unit_group);
7833 per_cu = &tu_group->per_cu;
7834 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7835
7836 if (dwarf2_per_objfile->using_index)
7837 {
7838 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7839 struct dwarf2_per_cu_quick_data);
7840 }
7841 else
7842 {
7843 unsigned int line_offset = to_underlying (line_offset_struct);
7844 struct partial_symtab *pst;
7845 char *name;
7846
7847 /* Give the symtab a useful name for debug purposes. */
7848 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7849 name = xstrprintf ("<type_units_%d>",
7850 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7851 else
7852 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7853
7854 pst = create_partial_symtab (per_cu, name);
7855 pst->anonymous = 1;
7856
7857 xfree (name);
7858 }
7859
7860 tu_group->hash.dwo_unit = cu->dwo_unit;
7861 tu_group->hash.line_sect_off = line_offset_struct;
7862
7863 return tu_group;
7864 }
7865
7866 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7867 STMT_LIST is a DW_AT_stmt_list attribute. */
7868
7869 static struct type_unit_group *
7870 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7871 {
7872 struct dwarf2_per_objfile *dwarf2_per_objfile
7873 = cu->per_cu->dwarf2_per_objfile;
7874 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7875 struct type_unit_group *tu_group;
7876 void **slot;
7877 unsigned int line_offset;
7878 struct type_unit_group type_unit_group_for_lookup;
7879
7880 if (dwarf2_per_objfile->type_unit_groups == NULL)
7881 {
7882 dwarf2_per_objfile->type_unit_groups =
7883 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7884 }
7885
7886 /* Do we need to create a new group, or can we use an existing one? */
7887
7888 if (stmt_list)
7889 {
7890 line_offset = DW_UNSND (stmt_list);
7891 ++tu_stats->nr_symtab_sharers;
7892 }
7893 else
7894 {
7895 /* Ugh, no stmt_list. Rare, but we have to handle it.
7896 We can do various things here like create one group per TU or
7897 spread them over multiple groups to split up the expansion work.
7898 To avoid worst case scenarios (too many groups or too large groups)
7899 we, umm, group them in bunches. */
7900 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7901 | (tu_stats->nr_stmt_less_type_units
7902 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7903 ++tu_stats->nr_stmt_less_type_units;
7904 }
7905
7906 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7907 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7908 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7909 &type_unit_group_for_lookup, INSERT);
7910 if (*slot != NULL)
7911 {
7912 tu_group = (struct type_unit_group *) *slot;
7913 gdb_assert (tu_group != NULL);
7914 }
7915 else
7916 {
7917 sect_offset line_offset_struct = (sect_offset) line_offset;
7918 tu_group = create_type_unit_group (cu, line_offset_struct);
7919 *slot = tu_group;
7920 ++tu_stats->nr_symtabs;
7921 }
7922
7923 return tu_group;
7924 }
7925 \f
7926 /* Partial symbol tables. */
7927
7928 /* Create a psymtab named NAME and assign it to PER_CU.
7929
7930 The caller must fill in the following details:
7931 dirname, textlow, texthigh. */
7932
7933 static struct partial_symtab *
7934 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7935 {
7936 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7937 struct partial_symtab *pst;
7938
7939 pst = start_psymtab_common (objfile, name, 0,
7940 objfile->global_psymbols,
7941 objfile->static_psymbols);
7942
7943 pst->psymtabs_addrmap_supported = 1;
7944
7945 /* This is the glue that links PST into GDB's symbol API. */
7946 pst->read_symtab_private = per_cu;
7947 pst->read_symtab = dwarf2_read_symtab;
7948 per_cu->v.psymtab = pst;
7949
7950 return pst;
7951 }
7952
7953 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7954 type. */
7955
7956 struct process_psymtab_comp_unit_data
7957 {
7958 /* True if we are reading a DW_TAG_partial_unit. */
7959
7960 int want_partial_unit;
7961
7962 /* The "pretend" language that is used if the CU doesn't declare a
7963 language. */
7964
7965 enum language pretend_language;
7966 };
7967
7968 /* die_reader_func for process_psymtab_comp_unit. */
7969
7970 static void
7971 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7972 const gdb_byte *info_ptr,
7973 struct die_info *comp_unit_die,
7974 int has_children,
7975 void *data)
7976 {
7977 struct dwarf2_cu *cu = reader->cu;
7978 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7979 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7980 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7981 CORE_ADDR baseaddr;
7982 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7983 struct partial_symtab *pst;
7984 enum pc_bounds_kind cu_bounds_kind;
7985 const char *filename;
7986 struct process_psymtab_comp_unit_data *info
7987 = (struct process_psymtab_comp_unit_data *) data;
7988
7989 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7990 return;
7991
7992 gdb_assert (! per_cu->is_debug_types);
7993
7994 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7995
7996 cu->list_in_scope = &file_symbols;
7997
7998 /* Allocate a new partial symbol table structure. */
7999 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8000 if (filename == NULL)
8001 filename = "";
8002
8003 pst = create_partial_symtab (per_cu, filename);
8004
8005 /* This must be done before calling dwarf2_build_include_psymtabs. */
8006 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8007
8008 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8009
8010 dwarf2_find_base_address (comp_unit_die, cu);
8011
8012 /* Possibly set the default values of LOWPC and HIGHPC from
8013 `DW_AT_ranges'. */
8014 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8015 &best_highpc, cu, pst);
8016 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8017 /* Store the contiguous range if it is not empty; it can be empty for
8018 CUs with no code. */
8019 addrmap_set_empty (objfile->psymtabs_addrmap,
8020 gdbarch_adjust_dwarf2_addr (gdbarch,
8021 best_lowpc + baseaddr),
8022 gdbarch_adjust_dwarf2_addr (gdbarch,
8023 best_highpc + baseaddr) - 1,
8024 pst);
8025
8026 /* Check if comp unit has_children.
8027 If so, read the rest of the partial symbols from this comp unit.
8028 If not, there's no more debug_info for this comp unit. */
8029 if (has_children)
8030 {
8031 struct partial_die_info *first_die;
8032 CORE_ADDR lowpc, highpc;
8033
8034 lowpc = ((CORE_ADDR) -1);
8035 highpc = ((CORE_ADDR) 0);
8036
8037 first_die = load_partial_dies (reader, info_ptr, 1);
8038
8039 scan_partial_symbols (first_die, &lowpc, &highpc,
8040 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8041
8042 /* If we didn't find a lowpc, set it to highpc to avoid
8043 complaints from `maint check'. */
8044 if (lowpc == ((CORE_ADDR) -1))
8045 lowpc = highpc;
8046
8047 /* If the compilation unit didn't have an explicit address range,
8048 then use the information extracted from its child dies. */
8049 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8050 {
8051 best_lowpc = lowpc;
8052 best_highpc = highpc;
8053 }
8054 }
8055 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8056 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8057
8058 end_psymtab_common (objfile, pst);
8059
8060 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8061 {
8062 int i;
8063 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8064 struct dwarf2_per_cu_data *iter;
8065
8066 /* Fill in 'dependencies' here; we fill in 'users' in a
8067 post-pass. */
8068 pst->number_of_dependencies = len;
8069 pst->dependencies =
8070 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8071 for (i = 0;
8072 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8073 i, iter);
8074 ++i)
8075 pst->dependencies[i] = iter->v.psymtab;
8076
8077 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8078 }
8079
8080 /* Get the list of files included in the current compilation unit,
8081 and build a psymtab for each of them. */
8082 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8083
8084 if (dwarf_read_debug)
8085 {
8086 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8087
8088 fprintf_unfiltered (gdb_stdlog,
8089 "Psymtab for %s unit @%s: %s - %s"
8090 ", %d global, %d static syms\n",
8091 per_cu->is_debug_types ? "type" : "comp",
8092 sect_offset_str (per_cu->sect_off),
8093 paddress (gdbarch, pst->textlow),
8094 paddress (gdbarch, pst->texthigh),
8095 pst->n_global_syms, pst->n_static_syms);
8096 }
8097 }
8098
8099 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8100 Process compilation unit THIS_CU for a psymtab. */
8101
8102 static void
8103 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8104 int want_partial_unit,
8105 enum language pretend_language)
8106 {
8107 /* If this compilation unit was already read in, free the
8108 cached copy in order to read it in again. This is
8109 necessary because we skipped some symbols when we first
8110 read in the compilation unit (see load_partial_dies).
8111 This problem could be avoided, but the benefit is unclear. */
8112 if (this_cu->cu != NULL)
8113 free_one_cached_comp_unit (this_cu);
8114
8115 if (this_cu->is_debug_types)
8116 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8117 NULL);
8118 else
8119 {
8120 process_psymtab_comp_unit_data info;
8121 info.want_partial_unit = want_partial_unit;
8122 info.pretend_language = pretend_language;
8123 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8124 process_psymtab_comp_unit_reader, &info);
8125 }
8126
8127 /* Age out any secondary CUs. */
8128 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8129 }
8130
8131 /* Reader function for build_type_psymtabs. */
8132
8133 static void
8134 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8135 const gdb_byte *info_ptr,
8136 struct die_info *type_unit_die,
8137 int has_children,
8138 void *data)
8139 {
8140 struct dwarf2_per_objfile *dwarf2_per_objfile
8141 = reader->cu->per_cu->dwarf2_per_objfile;
8142 struct objfile *objfile = dwarf2_per_objfile->objfile;
8143 struct dwarf2_cu *cu = reader->cu;
8144 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8145 struct signatured_type *sig_type;
8146 struct type_unit_group *tu_group;
8147 struct attribute *attr;
8148 struct partial_die_info *first_die;
8149 CORE_ADDR lowpc, highpc;
8150 struct partial_symtab *pst;
8151
8152 gdb_assert (data == NULL);
8153 gdb_assert (per_cu->is_debug_types);
8154 sig_type = (struct signatured_type *) per_cu;
8155
8156 if (! has_children)
8157 return;
8158
8159 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8160 tu_group = get_type_unit_group (cu, attr);
8161
8162 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8163
8164 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8165 cu->list_in_scope = &file_symbols;
8166 pst = create_partial_symtab (per_cu, "");
8167 pst->anonymous = 1;
8168
8169 first_die = load_partial_dies (reader, info_ptr, 1);
8170
8171 lowpc = (CORE_ADDR) -1;
8172 highpc = (CORE_ADDR) 0;
8173 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8174
8175 end_psymtab_common (objfile, pst);
8176 }
8177
8178 /* Struct used to sort TUs by their abbreviation table offset. */
8179
8180 struct tu_abbrev_offset
8181 {
8182 struct signatured_type *sig_type;
8183 sect_offset abbrev_offset;
8184 };
8185
8186 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8187
8188 static bool
8189 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8190 const struct tu_abbrev_offset &b)
8191 {
8192 return a.abbrev_offset < b.abbrev_offset;
8193 }
8194
8195 /* Efficiently read all the type units.
8196 This does the bulk of the work for build_type_psymtabs.
8197
8198 The efficiency is because we sort TUs by the abbrev table they use and
8199 only read each abbrev table once. In one program there are 200K TUs
8200 sharing 8K abbrev tables.
8201
8202 The main purpose of this function is to support building the
8203 dwarf2_per_objfile->type_unit_groups table.
8204 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8205 can collapse the search space by grouping them by stmt_list.
8206 The savings can be significant, in the same program from above the 200K TUs
8207 share 8K stmt_list tables.
8208
8209 FUNC is expected to call get_type_unit_group, which will create the
8210 struct type_unit_group if necessary and add it to
8211 dwarf2_per_objfile->type_unit_groups. */
8212
8213 static void
8214 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8215 {
8216 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8217 abbrev_table_up abbrev_table;
8218 sect_offset abbrev_offset;
8219 int i;
8220
8221 /* It's up to the caller to not call us multiple times. */
8222 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8223
8224 if (dwarf2_per_objfile->n_type_units == 0)
8225 return;
8226
8227 /* TUs typically share abbrev tables, and there can be way more TUs than
8228 abbrev tables. Sort by abbrev table to reduce the number of times we
8229 read each abbrev table in.
8230 Alternatives are to punt or to maintain a cache of abbrev tables.
8231 This is simpler and efficient enough for now.
8232
8233 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8234 symtab to use). Typically TUs with the same abbrev offset have the same
8235 stmt_list value too so in practice this should work well.
8236
8237 The basic algorithm here is:
8238
8239 sort TUs by abbrev table
8240 for each TU with same abbrev table:
8241 read abbrev table if first user
8242 read TU top level DIE
8243 [IWBN if DWO skeletons had DW_AT_stmt_list]
8244 call FUNC */
8245
8246 if (dwarf_read_debug)
8247 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8248
8249 /* Sort in a separate table to maintain the order of all_type_units
8250 for .gdb_index: TU indices directly index all_type_units. */
8251 std::vector<struct tu_abbrev_offset> sorted_by_abbrev
8252 (dwarf2_per_objfile->n_type_units);
8253 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8254 {
8255 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8256
8257 sorted_by_abbrev[i].sig_type = sig_type;
8258 sorted_by_abbrev[i].abbrev_offset =
8259 read_abbrev_offset (dwarf2_per_objfile,
8260 sig_type->per_cu.section,
8261 sig_type->per_cu.sect_off);
8262 }
8263 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8264 sort_tu_by_abbrev_offset);
8265
8266 abbrev_offset = (sect_offset) ~(unsigned) 0;
8267
8268 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8269 {
8270 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8271
8272 /* Switch to the next abbrev table if necessary. */
8273 if (abbrev_table == NULL
8274 || tu->abbrev_offset != abbrev_offset)
8275 {
8276 abbrev_offset = tu->abbrev_offset;
8277 abbrev_table =
8278 abbrev_table_read_table (dwarf2_per_objfile,
8279 &dwarf2_per_objfile->abbrev,
8280 abbrev_offset);
8281 ++tu_stats->nr_uniq_abbrev_tables;
8282 }
8283
8284 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8285 0, 0, build_type_psymtabs_reader, NULL);
8286 }
8287 }
8288
8289 /* Print collected type unit statistics. */
8290
8291 static void
8292 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8293 {
8294 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8295
8296 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8297 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8298 dwarf2_per_objfile->n_type_units);
8299 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8300 tu_stats->nr_uniq_abbrev_tables);
8301 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8302 tu_stats->nr_symtabs);
8303 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8304 tu_stats->nr_symtab_sharers);
8305 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8306 tu_stats->nr_stmt_less_type_units);
8307 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8308 tu_stats->nr_all_type_units_reallocs);
8309 }
8310
8311 /* Traversal function for build_type_psymtabs. */
8312
8313 static int
8314 build_type_psymtab_dependencies (void **slot, void *info)
8315 {
8316 struct dwarf2_per_objfile *dwarf2_per_objfile
8317 = (struct dwarf2_per_objfile *) info;
8318 struct objfile *objfile = dwarf2_per_objfile->objfile;
8319 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8320 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8321 struct partial_symtab *pst = per_cu->v.psymtab;
8322 int len = VEC_length (sig_type_ptr, tu_group->tus);
8323 struct signatured_type *iter;
8324 int i;
8325
8326 gdb_assert (len > 0);
8327 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8328
8329 pst->number_of_dependencies = len;
8330 pst->dependencies =
8331 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8332 for (i = 0;
8333 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8334 ++i)
8335 {
8336 gdb_assert (iter->per_cu.is_debug_types);
8337 pst->dependencies[i] = iter->per_cu.v.psymtab;
8338 iter->type_unit_group = tu_group;
8339 }
8340
8341 VEC_free (sig_type_ptr, tu_group->tus);
8342
8343 return 1;
8344 }
8345
8346 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8347 Build partial symbol tables for the .debug_types comp-units. */
8348
8349 static void
8350 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8351 {
8352 if (! create_all_type_units (dwarf2_per_objfile))
8353 return;
8354
8355 build_type_psymtabs_1 (dwarf2_per_objfile);
8356 }
8357
8358 /* Traversal function for process_skeletonless_type_unit.
8359 Read a TU in a DWO file and build partial symbols for it. */
8360
8361 static int
8362 process_skeletonless_type_unit (void **slot, void *info)
8363 {
8364 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8365 struct dwarf2_per_objfile *dwarf2_per_objfile
8366 = (struct dwarf2_per_objfile *) info;
8367 struct signatured_type find_entry, *entry;
8368
8369 /* If this TU doesn't exist in the global table, add it and read it in. */
8370
8371 if (dwarf2_per_objfile->signatured_types == NULL)
8372 {
8373 dwarf2_per_objfile->signatured_types
8374 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8375 }
8376
8377 find_entry.signature = dwo_unit->signature;
8378 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8379 INSERT);
8380 /* If we've already seen this type there's nothing to do. What's happening
8381 is we're doing our own version of comdat-folding here. */
8382 if (*slot != NULL)
8383 return 1;
8384
8385 /* This does the job that create_all_type_units would have done for
8386 this TU. */
8387 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8388 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8389 *slot = entry;
8390
8391 /* This does the job that build_type_psymtabs_1 would have done. */
8392 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8393 build_type_psymtabs_reader, NULL);
8394
8395 return 1;
8396 }
8397
8398 /* Traversal function for process_skeletonless_type_units. */
8399
8400 static int
8401 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8402 {
8403 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8404
8405 if (dwo_file->tus != NULL)
8406 {
8407 htab_traverse_noresize (dwo_file->tus,
8408 process_skeletonless_type_unit, info);
8409 }
8410
8411 return 1;
8412 }
8413
8414 /* Scan all TUs of DWO files, verifying we've processed them.
8415 This is needed in case a TU was emitted without its skeleton.
8416 Note: This can't be done until we know what all the DWO files are. */
8417
8418 static void
8419 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8420 {
8421 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8422 if (get_dwp_file (dwarf2_per_objfile) == NULL
8423 && dwarf2_per_objfile->dwo_files != NULL)
8424 {
8425 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8426 process_dwo_file_for_skeletonless_type_units,
8427 dwarf2_per_objfile);
8428 }
8429 }
8430
8431 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8432
8433 static void
8434 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8435 {
8436 int i;
8437
8438 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8439 {
8440 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8441 struct partial_symtab *pst = per_cu->v.psymtab;
8442 int j;
8443
8444 if (pst == NULL)
8445 continue;
8446
8447 for (j = 0; j < pst->number_of_dependencies; ++j)
8448 {
8449 /* Set the 'user' field only if it is not already set. */
8450 if (pst->dependencies[j]->user == NULL)
8451 pst->dependencies[j]->user = pst;
8452 }
8453 }
8454 }
8455
8456 /* Build the partial symbol table by doing a quick pass through the
8457 .debug_info and .debug_abbrev sections. */
8458
8459 static void
8460 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8461 {
8462 int i;
8463 struct objfile *objfile = dwarf2_per_objfile->objfile;
8464
8465 if (dwarf_read_debug)
8466 {
8467 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8468 objfile_name (objfile));
8469 }
8470
8471 dwarf2_per_objfile->reading_partial_symbols = 1;
8472
8473 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8474
8475 /* Any cached compilation units will be linked by the per-objfile
8476 read_in_chain. Make sure to free them when we're done. */
8477 free_cached_comp_units freer (dwarf2_per_objfile);
8478
8479 build_type_psymtabs (dwarf2_per_objfile);
8480
8481 create_all_comp_units (dwarf2_per_objfile);
8482
8483 /* Create a temporary address map on a temporary obstack. We later
8484 copy this to the final obstack. */
8485 auto_obstack temp_obstack;
8486
8487 scoped_restore save_psymtabs_addrmap
8488 = make_scoped_restore (&objfile->psymtabs_addrmap,
8489 addrmap_create_mutable (&temp_obstack));
8490
8491 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8492 {
8493 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8494
8495 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8496 }
8497
8498 /* This has to wait until we read the CUs, we need the list of DWOs. */
8499 process_skeletonless_type_units (dwarf2_per_objfile);
8500
8501 /* Now that all TUs have been processed we can fill in the dependencies. */
8502 if (dwarf2_per_objfile->type_unit_groups != NULL)
8503 {
8504 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8505 build_type_psymtab_dependencies, dwarf2_per_objfile);
8506 }
8507
8508 if (dwarf_read_debug)
8509 print_tu_stats (dwarf2_per_objfile);
8510
8511 set_partial_user (dwarf2_per_objfile);
8512
8513 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8514 &objfile->objfile_obstack);
8515 /* At this point we want to keep the address map. */
8516 save_psymtabs_addrmap.release ();
8517
8518 if (dwarf_read_debug)
8519 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8520 objfile_name (objfile));
8521 }
8522
8523 /* die_reader_func for load_partial_comp_unit. */
8524
8525 static void
8526 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8527 const gdb_byte *info_ptr,
8528 struct die_info *comp_unit_die,
8529 int has_children,
8530 void *data)
8531 {
8532 struct dwarf2_cu *cu = reader->cu;
8533
8534 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8535
8536 /* Check if comp unit has_children.
8537 If so, read the rest of the partial symbols from this comp unit.
8538 If not, there's no more debug_info for this comp unit. */
8539 if (has_children)
8540 load_partial_dies (reader, info_ptr, 0);
8541 }
8542
8543 /* Load the partial DIEs for a secondary CU into memory.
8544 This is also used when rereading a primary CU with load_all_dies. */
8545
8546 static void
8547 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8548 {
8549 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8550 load_partial_comp_unit_reader, NULL);
8551 }
8552
8553 static void
8554 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8555 struct dwarf2_section_info *section,
8556 struct dwarf2_section_info *abbrev_section,
8557 unsigned int is_dwz,
8558 int *n_allocated,
8559 int *n_comp_units,
8560 struct dwarf2_per_cu_data ***all_comp_units)
8561 {
8562 const gdb_byte *info_ptr;
8563 struct objfile *objfile = dwarf2_per_objfile->objfile;
8564
8565 if (dwarf_read_debug)
8566 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8567 get_section_name (section),
8568 get_section_file_name (section));
8569
8570 dwarf2_read_section (objfile, section);
8571
8572 info_ptr = section->buffer;
8573
8574 while (info_ptr < section->buffer + section->size)
8575 {
8576 struct dwarf2_per_cu_data *this_cu;
8577
8578 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8579
8580 comp_unit_head cu_header;
8581 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8582 abbrev_section, info_ptr,
8583 rcuh_kind::COMPILE);
8584
8585 /* Save the compilation unit for later lookup. */
8586 if (cu_header.unit_type != DW_UT_type)
8587 {
8588 this_cu = XOBNEW (&objfile->objfile_obstack,
8589 struct dwarf2_per_cu_data);
8590 memset (this_cu, 0, sizeof (*this_cu));
8591 }
8592 else
8593 {
8594 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8595 struct signatured_type);
8596 memset (sig_type, 0, sizeof (*sig_type));
8597 sig_type->signature = cu_header.signature;
8598 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8599 this_cu = &sig_type->per_cu;
8600 }
8601 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8602 this_cu->sect_off = sect_off;
8603 this_cu->length = cu_header.length + cu_header.initial_length_size;
8604 this_cu->is_dwz = is_dwz;
8605 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8606 this_cu->section = section;
8607
8608 if (*n_comp_units == *n_allocated)
8609 {
8610 *n_allocated *= 2;
8611 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8612 *all_comp_units, *n_allocated);
8613 }
8614 (*all_comp_units)[*n_comp_units] = this_cu;
8615 ++*n_comp_units;
8616
8617 info_ptr = info_ptr + this_cu->length;
8618 }
8619 }
8620
8621 /* Create a list of all compilation units in OBJFILE.
8622 This is only done for -readnow and building partial symtabs. */
8623
8624 static void
8625 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8626 {
8627 int n_allocated;
8628 int n_comp_units;
8629 struct dwarf2_per_cu_data **all_comp_units;
8630 struct dwz_file *dwz;
8631 struct objfile *objfile = dwarf2_per_objfile->objfile;
8632
8633 n_comp_units = 0;
8634 n_allocated = 10;
8635 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
8636
8637 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8638 &dwarf2_per_objfile->abbrev, 0,
8639 &n_allocated, &n_comp_units, &all_comp_units);
8640
8641 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8642 if (dwz != NULL)
8643 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8644 1, &n_allocated, &n_comp_units,
8645 &all_comp_units);
8646
8647 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8648 struct dwarf2_per_cu_data *,
8649 n_comp_units);
8650 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8651 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8652 xfree (all_comp_units);
8653 dwarf2_per_objfile->n_comp_units = n_comp_units;
8654 }
8655
8656 /* Process all loaded DIEs for compilation unit CU, starting at
8657 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8658 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8659 DW_AT_ranges). See the comments of add_partial_subprogram on how
8660 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8661
8662 static void
8663 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8664 CORE_ADDR *highpc, int set_addrmap,
8665 struct dwarf2_cu *cu)
8666 {
8667 struct partial_die_info *pdi;
8668
8669 /* Now, march along the PDI's, descending into ones which have
8670 interesting children but skipping the children of the other ones,
8671 until we reach the end of the compilation unit. */
8672
8673 pdi = first_die;
8674
8675 while (pdi != NULL)
8676 {
8677 pdi->fixup (cu);
8678
8679 /* Anonymous namespaces or modules have no name but have interesting
8680 children, so we need to look at them. Ditto for anonymous
8681 enums. */
8682
8683 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8684 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8685 || pdi->tag == DW_TAG_imported_unit
8686 || pdi->tag == DW_TAG_inlined_subroutine)
8687 {
8688 switch (pdi->tag)
8689 {
8690 case DW_TAG_subprogram:
8691 case DW_TAG_inlined_subroutine:
8692 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8693 break;
8694 case DW_TAG_constant:
8695 case DW_TAG_variable:
8696 case DW_TAG_typedef:
8697 case DW_TAG_union_type:
8698 if (!pdi->is_declaration)
8699 {
8700 add_partial_symbol (pdi, cu);
8701 }
8702 break;
8703 case DW_TAG_class_type:
8704 case DW_TAG_interface_type:
8705 case DW_TAG_structure_type:
8706 if (!pdi->is_declaration)
8707 {
8708 add_partial_symbol (pdi, cu);
8709 }
8710 if ((cu->language == language_rust
8711 || cu->language == language_cplus) && pdi->has_children)
8712 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8713 set_addrmap, cu);
8714 break;
8715 case DW_TAG_enumeration_type:
8716 if (!pdi->is_declaration)
8717 add_partial_enumeration (pdi, cu);
8718 break;
8719 case DW_TAG_base_type:
8720 case DW_TAG_subrange_type:
8721 /* File scope base type definitions are added to the partial
8722 symbol table. */
8723 add_partial_symbol (pdi, cu);
8724 break;
8725 case DW_TAG_namespace:
8726 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8727 break;
8728 case DW_TAG_module:
8729 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8730 break;
8731 case DW_TAG_imported_unit:
8732 {
8733 struct dwarf2_per_cu_data *per_cu;
8734
8735 /* For now we don't handle imported units in type units. */
8736 if (cu->per_cu->is_debug_types)
8737 {
8738 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8739 " supported in type units [in module %s]"),
8740 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8741 }
8742
8743 per_cu = dwarf2_find_containing_comp_unit
8744 (pdi->d.sect_off, pdi->is_dwz,
8745 cu->per_cu->dwarf2_per_objfile);
8746
8747 /* Go read the partial unit, if needed. */
8748 if (per_cu->v.psymtab == NULL)
8749 process_psymtab_comp_unit (per_cu, 1, cu->language);
8750
8751 VEC_safe_push (dwarf2_per_cu_ptr,
8752 cu->per_cu->imported_symtabs, per_cu);
8753 }
8754 break;
8755 case DW_TAG_imported_declaration:
8756 add_partial_symbol (pdi, cu);
8757 break;
8758 default:
8759 break;
8760 }
8761 }
8762
8763 /* If the die has a sibling, skip to the sibling. */
8764
8765 pdi = pdi->die_sibling;
8766 }
8767 }
8768
8769 /* Functions used to compute the fully scoped name of a partial DIE.
8770
8771 Normally, this is simple. For C++, the parent DIE's fully scoped
8772 name is concatenated with "::" and the partial DIE's name.
8773 Enumerators are an exception; they use the scope of their parent
8774 enumeration type, i.e. the name of the enumeration type is not
8775 prepended to the enumerator.
8776
8777 There are two complexities. One is DW_AT_specification; in this
8778 case "parent" means the parent of the target of the specification,
8779 instead of the direct parent of the DIE. The other is compilers
8780 which do not emit DW_TAG_namespace; in this case we try to guess
8781 the fully qualified name of structure types from their members'
8782 linkage names. This must be done using the DIE's children rather
8783 than the children of any DW_AT_specification target. We only need
8784 to do this for structures at the top level, i.e. if the target of
8785 any DW_AT_specification (if any; otherwise the DIE itself) does not
8786 have a parent. */
8787
8788 /* Compute the scope prefix associated with PDI's parent, in
8789 compilation unit CU. The result will be allocated on CU's
8790 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8791 field. NULL is returned if no prefix is necessary. */
8792 static const char *
8793 partial_die_parent_scope (struct partial_die_info *pdi,
8794 struct dwarf2_cu *cu)
8795 {
8796 const char *grandparent_scope;
8797 struct partial_die_info *parent, *real_pdi;
8798
8799 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8800 then this means the parent of the specification DIE. */
8801
8802 real_pdi = pdi;
8803 while (real_pdi->has_specification)
8804 real_pdi = find_partial_die (real_pdi->spec_offset,
8805 real_pdi->spec_is_dwz, cu);
8806
8807 parent = real_pdi->die_parent;
8808 if (parent == NULL)
8809 return NULL;
8810
8811 if (parent->scope_set)
8812 return parent->scope;
8813
8814 parent->fixup (cu);
8815
8816 grandparent_scope = partial_die_parent_scope (parent, cu);
8817
8818 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8819 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8820 Work around this problem here. */
8821 if (cu->language == language_cplus
8822 && parent->tag == DW_TAG_namespace
8823 && strcmp (parent->name, "::") == 0
8824 && grandparent_scope == NULL)
8825 {
8826 parent->scope = NULL;
8827 parent->scope_set = 1;
8828 return NULL;
8829 }
8830
8831 if (pdi->tag == DW_TAG_enumerator)
8832 /* Enumerators should not get the name of the enumeration as a prefix. */
8833 parent->scope = grandparent_scope;
8834 else if (parent->tag == DW_TAG_namespace
8835 || parent->tag == DW_TAG_module
8836 || parent->tag == DW_TAG_structure_type
8837 || parent->tag == DW_TAG_class_type
8838 || parent->tag == DW_TAG_interface_type
8839 || parent->tag == DW_TAG_union_type
8840 || parent->tag == DW_TAG_enumeration_type)
8841 {
8842 if (grandparent_scope == NULL)
8843 parent->scope = parent->name;
8844 else
8845 parent->scope = typename_concat (&cu->comp_unit_obstack,
8846 grandparent_scope,
8847 parent->name, 0, cu);
8848 }
8849 else
8850 {
8851 /* FIXME drow/2004-04-01: What should we be doing with
8852 function-local names? For partial symbols, we should probably be
8853 ignoring them. */
8854 complaint (&symfile_complaints,
8855 _("unhandled containing DIE tag %d for DIE at %s"),
8856 parent->tag, sect_offset_str (pdi->sect_off));
8857 parent->scope = grandparent_scope;
8858 }
8859
8860 parent->scope_set = 1;
8861 return parent->scope;
8862 }
8863
8864 /* Return the fully scoped name associated with PDI, from compilation unit
8865 CU. The result will be allocated with malloc. */
8866
8867 static char *
8868 partial_die_full_name (struct partial_die_info *pdi,
8869 struct dwarf2_cu *cu)
8870 {
8871 const char *parent_scope;
8872
8873 /* If this is a template instantiation, we can not work out the
8874 template arguments from partial DIEs. So, unfortunately, we have
8875 to go through the full DIEs. At least any work we do building
8876 types here will be reused if full symbols are loaded later. */
8877 if (pdi->has_template_arguments)
8878 {
8879 pdi->fixup (cu);
8880
8881 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8882 {
8883 struct die_info *die;
8884 struct attribute attr;
8885 struct dwarf2_cu *ref_cu = cu;
8886
8887 /* DW_FORM_ref_addr is using section offset. */
8888 attr.name = (enum dwarf_attribute) 0;
8889 attr.form = DW_FORM_ref_addr;
8890 attr.u.unsnd = to_underlying (pdi->sect_off);
8891 die = follow_die_ref (NULL, &attr, &ref_cu);
8892
8893 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8894 }
8895 }
8896
8897 parent_scope = partial_die_parent_scope (pdi, cu);
8898 if (parent_scope == NULL)
8899 return NULL;
8900 else
8901 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8902 }
8903
8904 static void
8905 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8906 {
8907 struct dwarf2_per_objfile *dwarf2_per_objfile
8908 = cu->per_cu->dwarf2_per_objfile;
8909 struct objfile *objfile = dwarf2_per_objfile->objfile;
8910 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8911 CORE_ADDR addr = 0;
8912 const char *actual_name = NULL;
8913 CORE_ADDR baseaddr;
8914 char *built_actual_name;
8915
8916 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8917
8918 built_actual_name = partial_die_full_name (pdi, cu);
8919 if (built_actual_name != NULL)
8920 actual_name = built_actual_name;
8921
8922 if (actual_name == NULL)
8923 actual_name = pdi->name;
8924
8925 switch (pdi->tag)
8926 {
8927 case DW_TAG_inlined_subroutine:
8928 case DW_TAG_subprogram:
8929 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
8930 if (pdi->is_external || cu->language == language_ada)
8931 {
8932 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8933 of the global scope. But in Ada, we want to be able to access
8934 nested procedures globally. So all Ada subprograms are stored
8935 in the global scope. */
8936 add_psymbol_to_list (actual_name, strlen (actual_name),
8937 built_actual_name != NULL,
8938 VAR_DOMAIN, LOC_BLOCK,
8939 &objfile->global_psymbols,
8940 addr, cu->language, objfile);
8941 }
8942 else
8943 {
8944 add_psymbol_to_list (actual_name, strlen (actual_name),
8945 built_actual_name != NULL,
8946 VAR_DOMAIN, LOC_BLOCK,
8947 &objfile->static_psymbols,
8948 addr, cu->language, objfile);
8949 }
8950
8951 if (pdi->main_subprogram && actual_name != NULL)
8952 set_objfile_main_name (objfile, actual_name, cu->language);
8953 break;
8954 case DW_TAG_constant:
8955 {
8956 std::vector<partial_symbol *> *list;
8957
8958 if (pdi->is_external)
8959 list = &objfile->global_psymbols;
8960 else
8961 list = &objfile->static_psymbols;
8962 add_psymbol_to_list (actual_name, strlen (actual_name),
8963 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8964 list, 0, cu->language, objfile);
8965 }
8966 break;
8967 case DW_TAG_variable:
8968 if (pdi->d.locdesc)
8969 addr = decode_locdesc (pdi->d.locdesc, cu);
8970
8971 if (pdi->d.locdesc
8972 && addr == 0
8973 && !dwarf2_per_objfile->has_section_at_zero)
8974 {
8975 /* A global or static variable may also have been stripped
8976 out by the linker if unused, in which case its address
8977 will be nullified; do not add such variables into partial
8978 symbol table then. */
8979 }
8980 else if (pdi->is_external)
8981 {
8982 /* Global Variable.
8983 Don't enter into the minimal symbol tables as there is
8984 a minimal symbol table entry from the ELF symbols already.
8985 Enter into partial symbol table if it has a location
8986 descriptor or a type.
8987 If the location descriptor is missing, new_symbol will create
8988 a LOC_UNRESOLVED symbol, the address of the variable will then
8989 be determined from the minimal symbol table whenever the variable
8990 is referenced.
8991 The address for the partial symbol table entry is not
8992 used by GDB, but it comes in handy for debugging partial symbol
8993 table building. */
8994
8995 if (pdi->d.locdesc || pdi->has_type)
8996 add_psymbol_to_list (actual_name, strlen (actual_name),
8997 built_actual_name != NULL,
8998 VAR_DOMAIN, LOC_STATIC,
8999 &objfile->global_psymbols,
9000 addr + baseaddr,
9001 cu->language, objfile);
9002 }
9003 else
9004 {
9005 int has_loc = pdi->d.locdesc != NULL;
9006
9007 /* Static Variable. Skip symbols whose value we cannot know (those
9008 without location descriptors or constant values). */
9009 if (!has_loc && !pdi->has_const_value)
9010 {
9011 xfree (built_actual_name);
9012 return;
9013 }
9014
9015 add_psymbol_to_list (actual_name, strlen (actual_name),
9016 built_actual_name != NULL,
9017 VAR_DOMAIN, LOC_STATIC,
9018 &objfile->static_psymbols,
9019 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9020 cu->language, objfile);
9021 }
9022 break;
9023 case DW_TAG_typedef:
9024 case DW_TAG_base_type:
9025 case DW_TAG_subrange_type:
9026 add_psymbol_to_list (actual_name, strlen (actual_name),
9027 built_actual_name != NULL,
9028 VAR_DOMAIN, LOC_TYPEDEF,
9029 &objfile->static_psymbols,
9030 0, cu->language, objfile);
9031 break;
9032 case DW_TAG_imported_declaration:
9033 case DW_TAG_namespace:
9034 add_psymbol_to_list (actual_name, strlen (actual_name),
9035 built_actual_name != NULL,
9036 VAR_DOMAIN, LOC_TYPEDEF,
9037 &objfile->global_psymbols,
9038 0, cu->language, objfile);
9039 break;
9040 case DW_TAG_module:
9041 add_psymbol_to_list (actual_name, strlen (actual_name),
9042 built_actual_name != NULL,
9043 MODULE_DOMAIN, LOC_TYPEDEF,
9044 &objfile->global_psymbols,
9045 0, cu->language, objfile);
9046 break;
9047 case DW_TAG_class_type:
9048 case DW_TAG_interface_type:
9049 case DW_TAG_structure_type:
9050 case DW_TAG_union_type:
9051 case DW_TAG_enumeration_type:
9052 /* Skip external references. The DWARF standard says in the section
9053 about "Structure, Union, and Class Type Entries": "An incomplete
9054 structure, union or class type is represented by a structure,
9055 union or class entry that does not have a byte size attribute
9056 and that has a DW_AT_declaration attribute." */
9057 if (!pdi->has_byte_size && pdi->is_declaration)
9058 {
9059 xfree (built_actual_name);
9060 return;
9061 }
9062
9063 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9064 static vs. global. */
9065 add_psymbol_to_list (actual_name, strlen (actual_name),
9066 built_actual_name != NULL,
9067 STRUCT_DOMAIN, LOC_TYPEDEF,
9068 cu->language == language_cplus
9069 ? &objfile->global_psymbols
9070 : &objfile->static_psymbols,
9071 0, cu->language, objfile);
9072
9073 break;
9074 case DW_TAG_enumerator:
9075 add_psymbol_to_list (actual_name, strlen (actual_name),
9076 built_actual_name != NULL,
9077 VAR_DOMAIN, LOC_CONST,
9078 cu->language == language_cplus
9079 ? &objfile->global_psymbols
9080 : &objfile->static_psymbols,
9081 0, cu->language, objfile);
9082 break;
9083 default:
9084 break;
9085 }
9086
9087 xfree (built_actual_name);
9088 }
9089
9090 /* Read a partial die corresponding to a namespace; also, add a symbol
9091 corresponding to that namespace to the symbol table. NAMESPACE is
9092 the name of the enclosing namespace. */
9093
9094 static void
9095 add_partial_namespace (struct partial_die_info *pdi,
9096 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9097 int set_addrmap, struct dwarf2_cu *cu)
9098 {
9099 /* Add a symbol for the namespace. */
9100
9101 add_partial_symbol (pdi, cu);
9102
9103 /* Now scan partial symbols in that namespace. */
9104
9105 if (pdi->has_children)
9106 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9107 }
9108
9109 /* Read a partial die corresponding to a Fortran module. */
9110
9111 static void
9112 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9113 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9114 {
9115 /* Add a symbol for the namespace. */
9116
9117 add_partial_symbol (pdi, cu);
9118
9119 /* Now scan partial symbols in that module. */
9120
9121 if (pdi->has_children)
9122 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9123 }
9124
9125 /* Read a partial die corresponding to a subprogram or an inlined
9126 subprogram and create a partial symbol for that subprogram.
9127 When the CU language allows it, this routine also defines a partial
9128 symbol for each nested subprogram that this subprogram contains.
9129 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9130 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9131
9132 PDI may also be a lexical block, in which case we simply search
9133 recursively for subprograms defined inside that lexical block.
9134 Again, this is only performed when the CU language allows this
9135 type of definitions. */
9136
9137 static void
9138 add_partial_subprogram (struct partial_die_info *pdi,
9139 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9140 int set_addrmap, struct dwarf2_cu *cu)
9141 {
9142 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9143 {
9144 if (pdi->has_pc_info)
9145 {
9146 if (pdi->lowpc < *lowpc)
9147 *lowpc = pdi->lowpc;
9148 if (pdi->highpc > *highpc)
9149 *highpc = pdi->highpc;
9150 if (set_addrmap)
9151 {
9152 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9153 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9154 CORE_ADDR baseaddr;
9155 CORE_ADDR highpc;
9156 CORE_ADDR lowpc;
9157
9158 baseaddr = ANOFFSET (objfile->section_offsets,
9159 SECT_OFF_TEXT (objfile));
9160 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9161 pdi->lowpc + baseaddr);
9162 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9163 pdi->highpc + baseaddr);
9164 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9165 cu->per_cu->v.psymtab);
9166 }
9167 }
9168
9169 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9170 {
9171 if (!pdi->is_declaration)
9172 /* Ignore subprogram DIEs that do not have a name, they are
9173 illegal. Do not emit a complaint at this point, we will
9174 do so when we convert this psymtab into a symtab. */
9175 if (pdi->name)
9176 add_partial_symbol (pdi, cu);
9177 }
9178 }
9179
9180 if (! pdi->has_children)
9181 return;
9182
9183 if (cu->language == language_ada)
9184 {
9185 pdi = pdi->die_child;
9186 while (pdi != NULL)
9187 {
9188 pdi->fixup (cu);
9189 if (pdi->tag == DW_TAG_subprogram
9190 || pdi->tag == DW_TAG_inlined_subroutine
9191 || pdi->tag == DW_TAG_lexical_block)
9192 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9193 pdi = pdi->die_sibling;
9194 }
9195 }
9196 }
9197
9198 /* Read a partial die corresponding to an enumeration type. */
9199
9200 static void
9201 add_partial_enumeration (struct partial_die_info *enum_pdi,
9202 struct dwarf2_cu *cu)
9203 {
9204 struct partial_die_info *pdi;
9205
9206 if (enum_pdi->name != NULL)
9207 add_partial_symbol (enum_pdi, cu);
9208
9209 pdi = enum_pdi->die_child;
9210 while (pdi)
9211 {
9212 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9213 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9214 else
9215 add_partial_symbol (pdi, cu);
9216 pdi = pdi->die_sibling;
9217 }
9218 }
9219
9220 /* Return the initial uleb128 in the die at INFO_PTR. */
9221
9222 static unsigned int
9223 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9224 {
9225 unsigned int bytes_read;
9226
9227 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9228 }
9229
9230 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9231 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9232
9233 Return the corresponding abbrev, or NULL if the number is zero (indicating
9234 an empty DIE). In either case *BYTES_READ will be set to the length of
9235 the initial number. */
9236
9237 static struct abbrev_info *
9238 peek_die_abbrev (const die_reader_specs &reader,
9239 const gdb_byte *info_ptr, unsigned int *bytes_read)
9240 {
9241 dwarf2_cu *cu = reader.cu;
9242 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9243 unsigned int abbrev_number
9244 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9245
9246 if (abbrev_number == 0)
9247 return NULL;
9248
9249 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9250 if (!abbrev)
9251 {
9252 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9253 " at offset %s [in module %s]"),
9254 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9255 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9256 }
9257
9258 return abbrev;
9259 }
9260
9261 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9262 Returns a pointer to the end of a series of DIEs, terminated by an empty
9263 DIE. Any children of the skipped DIEs will also be skipped. */
9264
9265 static const gdb_byte *
9266 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9267 {
9268 while (1)
9269 {
9270 unsigned int bytes_read;
9271 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9272
9273 if (abbrev == NULL)
9274 return info_ptr + bytes_read;
9275 else
9276 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9277 }
9278 }
9279
9280 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9281 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9282 abbrev corresponding to that skipped uleb128 should be passed in
9283 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9284 children. */
9285
9286 static const gdb_byte *
9287 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9288 struct abbrev_info *abbrev)
9289 {
9290 unsigned int bytes_read;
9291 struct attribute attr;
9292 bfd *abfd = reader->abfd;
9293 struct dwarf2_cu *cu = reader->cu;
9294 const gdb_byte *buffer = reader->buffer;
9295 const gdb_byte *buffer_end = reader->buffer_end;
9296 unsigned int form, i;
9297
9298 for (i = 0; i < abbrev->num_attrs; i++)
9299 {
9300 /* The only abbrev we care about is DW_AT_sibling. */
9301 if (abbrev->attrs[i].name == DW_AT_sibling)
9302 {
9303 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9304 if (attr.form == DW_FORM_ref_addr)
9305 complaint (&symfile_complaints,
9306 _("ignoring absolute DW_AT_sibling"));
9307 else
9308 {
9309 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9310 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9311
9312 if (sibling_ptr < info_ptr)
9313 complaint (&symfile_complaints,
9314 _("DW_AT_sibling points backwards"));
9315 else if (sibling_ptr > reader->buffer_end)
9316 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9317 else
9318 return sibling_ptr;
9319 }
9320 }
9321
9322 /* If it isn't DW_AT_sibling, skip this attribute. */
9323 form = abbrev->attrs[i].form;
9324 skip_attribute:
9325 switch (form)
9326 {
9327 case DW_FORM_ref_addr:
9328 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9329 and later it is offset sized. */
9330 if (cu->header.version == 2)
9331 info_ptr += cu->header.addr_size;
9332 else
9333 info_ptr += cu->header.offset_size;
9334 break;
9335 case DW_FORM_GNU_ref_alt:
9336 info_ptr += cu->header.offset_size;
9337 break;
9338 case DW_FORM_addr:
9339 info_ptr += cu->header.addr_size;
9340 break;
9341 case DW_FORM_data1:
9342 case DW_FORM_ref1:
9343 case DW_FORM_flag:
9344 info_ptr += 1;
9345 break;
9346 case DW_FORM_flag_present:
9347 case DW_FORM_implicit_const:
9348 break;
9349 case DW_FORM_data2:
9350 case DW_FORM_ref2:
9351 info_ptr += 2;
9352 break;
9353 case DW_FORM_data4:
9354 case DW_FORM_ref4:
9355 info_ptr += 4;
9356 break;
9357 case DW_FORM_data8:
9358 case DW_FORM_ref8:
9359 case DW_FORM_ref_sig8:
9360 info_ptr += 8;
9361 break;
9362 case DW_FORM_data16:
9363 info_ptr += 16;
9364 break;
9365 case DW_FORM_string:
9366 read_direct_string (abfd, info_ptr, &bytes_read);
9367 info_ptr += bytes_read;
9368 break;
9369 case DW_FORM_sec_offset:
9370 case DW_FORM_strp:
9371 case DW_FORM_GNU_strp_alt:
9372 info_ptr += cu->header.offset_size;
9373 break;
9374 case DW_FORM_exprloc:
9375 case DW_FORM_block:
9376 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9377 info_ptr += bytes_read;
9378 break;
9379 case DW_FORM_block1:
9380 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9381 break;
9382 case DW_FORM_block2:
9383 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9384 break;
9385 case DW_FORM_block4:
9386 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9387 break;
9388 case DW_FORM_sdata:
9389 case DW_FORM_udata:
9390 case DW_FORM_ref_udata:
9391 case DW_FORM_GNU_addr_index:
9392 case DW_FORM_GNU_str_index:
9393 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9394 break;
9395 case DW_FORM_indirect:
9396 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9397 info_ptr += bytes_read;
9398 /* We need to continue parsing from here, so just go back to
9399 the top. */
9400 goto skip_attribute;
9401
9402 default:
9403 error (_("Dwarf Error: Cannot handle %s "
9404 "in DWARF reader [in module %s]"),
9405 dwarf_form_name (form),
9406 bfd_get_filename (abfd));
9407 }
9408 }
9409
9410 if (abbrev->has_children)
9411 return skip_children (reader, info_ptr);
9412 else
9413 return info_ptr;
9414 }
9415
9416 /* Locate ORIG_PDI's sibling.
9417 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9418
9419 static const gdb_byte *
9420 locate_pdi_sibling (const struct die_reader_specs *reader,
9421 struct partial_die_info *orig_pdi,
9422 const gdb_byte *info_ptr)
9423 {
9424 /* Do we know the sibling already? */
9425
9426 if (orig_pdi->sibling)
9427 return orig_pdi->sibling;
9428
9429 /* Are there any children to deal with? */
9430
9431 if (!orig_pdi->has_children)
9432 return info_ptr;
9433
9434 /* Skip the children the long way. */
9435
9436 return skip_children (reader, info_ptr);
9437 }
9438
9439 /* Expand this partial symbol table into a full symbol table. SELF is
9440 not NULL. */
9441
9442 static void
9443 dwarf2_read_symtab (struct partial_symtab *self,
9444 struct objfile *objfile)
9445 {
9446 struct dwarf2_per_objfile *dwarf2_per_objfile
9447 = get_dwarf2_per_objfile (objfile);
9448
9449 if (self->readin)
9450 {
9451 warning (_("bug: psymtab for %s is already read in."),
9452 self->filename);
9453 }
9454 else
9455 {
9456 if (info_verbose)
9457 {
9458 printf_filtered (_("Reading in symbols for %s..."),
9459 self->filename);
9460 gdb_flush (gdb_stdout);
9461 }
9462
9463 /* If this psymtab is constructed from a debug-only objfile, the
9464 has_section_at_zero flag will not necessarily be correct. We
9465 can get the correct value for this flag by looking at the data
9466 associated with the (presumably stripped) associated objfile. */
9467 if (objfile->separate_debug_objfile_backlink)
9468 {
9469 struct dwarf2_per_objfile *dpo_backlink
9470 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9471
9472 dwarf2_per_objfile->has_section_at_zero
9473 = dpo_backlink->has_section_at_zero;
9474 }
9475
9476 dwarf2_per_objfile->reading_partial_symbols = 0;
9477
9478 psymtab_to_symtab_1 (self);
9479
9480 /* Finish up the debug error message. */
9481 if (info_verbose)
9482 printf_filtered (_("done.\n"));
9483 }
9484
9485 process_cu_includes (dwarf2_per_objfile);
9486 }
9487 \f
9488 /* Reading in full CUs. */
9489
9490 /* Add PER_CU to the queue. */
9491
9492 static void
9493 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9494 enum language pretend_language)
9495 {
9496 struct dwarf2_queue_item *item;
9497
9498 per_cu->queued = 1;
9499 item = XNEW (struct dwarf2_queue_item);
9500 item->per_cu = per_cu;
9501 item->pretend_language = pretend_language;
9502 item->next = NULL;
9503
9504 if (dwarf2_queue == NULL)
9505 dwarf2_queue = item;
9506 else
9507 dwarf2_queue_tail->next = item;
9508
9509 dwarf2_queue_tail = item;
9510 }
9511
9512 /* If PER_CU is not yet queued, add it to the queue.
9513 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9514 dependency.
9515 The result is non-zero if PER_CU was queued, otherwise the result is zero
9516 meaning either PER_CU is already queued or it is already loaded.
9517
9518 N.B. There is an invariant here that if a CU is queued then it is loaded.
9519 The caller is required to load PER_CU if we return non-zero. */
9520
9521 static int
9522 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9523 struct dwarf2_per_cu_data *per_cu,
9524 enum language pretend_language)
9525 {
9526 /* We may arrive here during partial symbol reading, if we need full
9527 DIEs to process an unusual case (e.g. template arguments). Do
9528 not queue PER_CU, just tell our caller to load its DIEs. */
9529 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9530 {
9531 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9532 return 1;
9533 return 0;
9534 }
9535
9536 /* Mark the dependence relation so that we don't flush PER_CU
9537 too early. */
9538 if (dependent_cu != NULL)
9539 dwarf2_add_dependence (dependent_cu, per_cu);
9540
9541 /* If it's already on the queue, we have nothing to do. */
9542 if (per_cu->queued)
9543 return 0;
9544
9545 /* If the compilation unit is already loaded, just mark it as
9546 used. */
9547 if (per_cu->cu != NULL)
9548 {
9549 per_cu->cu->last_used = 0;
9550 return 0;
9551 }
9552
9553 /* Add it to the queue. */
9554 queue_comp_unit (per_cu, pretend_language);
9555
9556 return 1;
9557 }
9558
9559 /* Process the queue. */
9560
9561 static void
9562 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9563 {
9564 struct dwarf2_queue_item *item, *next_item;
9565
9566 if (dwarf_read_debug)
9567 {
9568 fprintf_unfiltered (gdb_stdlog,
9569 "Expanding one or more symtabs of objfile %s ...\n",
9570 objfile_name (dwarf2_per_objfile->objfile));
9571 }
9572
9573 /* The queue starts out with one item, but following a DIE reference
9574 may load a new CU, adding it to the end of the queue. */
9575 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9576 {
9577 if ((dwarf2_per_objfile->using_index
9578 ? !item->per_cu->v.quick->compunit_symtab
9579 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9580 /* Skip dummy CUs. */
9581 && item->per_cu->cu != NULL)
9582 {
9583 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9584 unsigned int debug_print_threshold;
9585 char buf[100];
9586
9587 if (per_cu->is_debug_types)
9588 {
9589 struct signatured_type *sig_type =
9590 (struct signatured_type *) per_cu;
9591
9592 sprintf (buf, "TU %s at offset %s",
9593 hex_string (sig_type->signature),
9594 sect_offset_str (per_cu->sect_off));
9595 /* There can be 100s of TUs.
9596 Only print them in verbose mode. */
9597 debug_print_threshold = 2;
9598 }
9599 else
9600 {
9601 sprintf (buf, "CU at offset %s",
9602 sect_offset_str (per_cu->sect_off));
9603 debug_print_threshold = 1;
9604 }
9605
9606 if (dwarf_read_debug >= debug_print_threshold)
9607 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9608
9609 if (per_cu->is_debug_types)
9610 process_full_type_unit (per_cu, item->pretend_language);
9611 else
9612 process_full_comp_unit (per_cu, item->pretend_language);
9613
9614 if (dwarf_read_debug >= debug_print_threshold)
9615 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9616 }
9617
9618 item->per_cu->queued = 0;
9619 next_item = item->next;
9620 xfree (item);
9621 }
9622
9623 dwarf2_queue_tail = NULL;
9624
9625 if (dwarf_read_debug)
9626 {
9627 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9628 objfile_name (dwarf2_per_objfile->objfile));
9629 }
9630 }
9631
9632 /* Read in full symbols for PST, and anything it depends on. */
9633
9634 static void
9635 psymtab_to_symtab_1 (struct partial_symtab *pst)
9636 {
9637 struct dwarf2_per_cu_data *per_cu;
9638 int i;
9639
9640 if (pst->readin)
9641 return;
9642
9643 for (i = 0; i < pst->number_of_dependencies; i++)
9644 if (!pst->dependencies[i]->readin
9645 && pst->dependencies[i]->user == NULL)
9646 {
9647 /* Inform about additional files that need to be read in. */
9648 if (info_verbose)
9649 {
9650 /* FIXME: i18n: Need to make this a single string. */
9651 fputs_filtered (" ", gdb_stdout);
9652 wrap_here ("");
9653 fputs_filtered ("and ", gdb_stdout);
9654 wrap_here ("");
9655 printf_filtered ("%s...", pst->dependencies[i]->filename);
9656 wrap_here (""); /* Flush output. */
9657 gdb_flush (gdb_stdout);
9658 }
9659 psymtab_to_symtab_1 (pst->dependencies[i]);
9660 }
9661
9662 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9663
9664 if (per_cu == NULL)
9665 {
9666 /* It's an include file, no symbols to read for it.
9667 Everything is in the parent symtab. */
9668 pst->readin = 1;
9669 return;
9670 }
9671
9672 dw2_do_instantiate_symtab (per_cu);
9673 }
9674
9675 /* Trivial hash function for die_info: the hash value of a DIE
9676 is its offset in .debug_info for this objfile. */
9677
9678 static hashval_t
9679 die_hash (const void *item)
9680 {
9681 const struct die_info *die = (const struct die_info *) item;
9682
9683 return to_underlying (die->sect_off);
9684 }
9685
9686 /* Trivial comparison function for die_info structures: two DIEs
9687 are equal if they have the same offset. */
9688
9689 static int
9690 die_eq (const void *item_lhs, const void *item_rhs)
9691 {
9692 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9693 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9694
9695 return die_lhs->sect_off == die_rhs->sect_off;
9696 }
9697
9698 /* die_reader_func for load_full_comp_unit.
9699 This is identical to read_signatured_type_reader,
9700 but is kept separate for now. */
9701
9702 static void
9703 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9704 const gdb_byte *info_ptr,
9705 struct die_info *comp_unit_die,
9706 int has_children,
9707 void *data)
9708 {
9709 struct dwarf2_cu *cu = reader->cu;
9710 enum language *language_ptr = (enum language *) data;
9711
9712 gdb_assert (cu->die_hash == NULL);
9713 cu->die_hash =
9714 htab_create_alloc_ex (cu->header.length / 12,
9715 die_hash,
9716 die_eq,
9717 NULL,
9718 &cu->comp_unit_obstack,
9719 hashtab_obstack_allocate,
9720 dummy_obstack_deallocate);
9721
9722 if (has_children)
9723 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9724 &info_ptr, comp_unit_die);
9725 cu->dies = comp_unit_die;
9726 /* comp_unit_die is not stored in die_hash, no need. */
9727
9728 /* We try not to read any attributes in this function, because not
9729 all CUs needed for references have been loaded yet, and symbol
9730 table processing isn't initialized. But we have to set the CU language,
9731 or we won't be able to build types correctly.
9732 Similarly, if we do not read the producer, we can not apply
9733 producer-specific interpretation. */
9734 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9735 }
9736
9737 /* Load the DIEs associated with PER_CU into memory. */
9738
9739 static void
9740 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9741 enum language pretend_language)
9742 {
9743 gdb_assert (! this_cu->is_debug_types);
9744
9745 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
9746 load_full_comp_unit_reader, &pretend_language);
9747 }
9748
9749 /* Add a DIE to the delayed physname list. */
9750
9751 static void
9752 add_to_method_list (struct type *type, int fnfield_index, int index,
9753 const char *name, struct die_info *die,
9754 struct dwarf2_cu *cu)
9755 {
9756 struct delayed_method_info mi;
9757 mi.type = type;
9758 mi.fnfield_index = fnfield_index;
9759 mi.index = index;
9760 mi.name = name;
9761 mi.die = die;
9762 cu->method_list.push_back (mi);
9763 }
9764
9765 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9766 "const" / "volatile". If so, decrements LEN by the length of the
9767 modifier and return true. Otherwise return false. */
9768
9769 template<size_t N>
9770 static bool
9771 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9772 {
9773 size_t mod_len = sizeof (mod) - 1;
9774 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9775 {
9776 len -= mod_len;
9777 return true;
9778 }
9779 return false;
9780 }
9781
9782 /* Compute the physnames of any methods on the CU's method list.
9783
9784 The computation of method physnames is delayed in order to avoid the
9785 (bad) condition that one of the method's formal parameters is of an as yet
9786 incomplete type. */
9787
9788 static void
9789 compute_delayed_physnames (struct dwarf2_cu *cu)
9790 {
9791 /* Only C++ delays computing physnames. */
9792 if (cu->method_list.empty ())
9793 return;
9794 gdb_assert (cu->language == language_cplus);
9795
9796 for (struct delayed_method_info &mi : cu->method_list)
9797 {
9798 const char *physname;
9799 struct fn_fieldlist *fn_flp
9800 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9801 physname = dwarf2_physname (mi.name, mi.die, cu);
9802 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9803 = physname ? physname : "";
9804
9805 /* Since there's no tag to indicate whether a method is a
9806 const/volatile overload, extract that information out of the
9807 demangled name. */
9808 if (physname != NULL)
9809 {
9810 size_t len = strlen (physname);
9811
9812 while (1)
9813 {
9814 if (physname[len] == ')') /* shortcut */
9815 break;
9816 else if (check_modifier (physname, len, " const"))
9817 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9818 else if (check_modifier (physname, len, " volatile"))
9819 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9820 else
9821 break;
9822 }
9823 }
9824 }
9825
9826 /* The list is no longer needed. */
9827 cu->method_list.clear ();
9828 }
9829
9830 /* Go objects should be embedded in a DW_TAG_module DIE,
9831 and it's not clear if/how imported objects will appear.
9832 To keep Go support simple until that's worked out,
9833 go back through what we've read and create something usable.
9834 We could do this while processing each DIE, and feels kinda cleaner,
9835 but that way is more invasive.
9836 This is to, for example, allow the user to type "p var" or "b main"
9837 without having to specify the package name, and allow lookups
9838 of module.object to work in contexts that use the expression
9839 parser. */
9840
9841 static void
9842 fixup_go_packaging (struct dwarf2_cu *cu)
9843 {
9844 char *package_name = NULL;
9845 struct pending *list;
9846 int i;
9847
9848 for (list = global_symbols; list != NULL; list = list->next)
9849 {
9850 for (i = 0; i < list->nsyms; ++i)
9851 {
9852 struct symbol *sym = list->symbol[i];
9853
9854 if (SYMBOL_LANGUAGE (sym) == language_go
9855 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9856 {
9857 char *this_package_name = go_symbol_package_name (sym);
9858
9859 if (this_package_name == NULL)
9860 continue;
9861 if (package_name == NULL)
9862 package_name = this_package_name;
9863 else
9864 {
9865 struct objfile *objfile
9866 = cu->per_cu->dwarf2_per_objfile->objfile;
9867 if (strcmp (package_name, this_package_name) != 0)
9868 complaint (&symfile_complaints,
9869 _("Symtab %s has objects from two different Go packages: %s and %s"),
9870 (symbol_symtab (sym) != NULL
9871 ? symtab_to_filename_for_display
9872 (symbol_symtab (sym))
9873 : objfile_name (objfile)),
9874 this_package_name, package_name);
9875 xfree (this_package_name);
9876 }
9877 }
9878 }
9879 }
9880
9881 if (package_name != NULL)
9882 {
9883 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9884 const char *saved_package_name
9885 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9886 package_name,
9887 strlen (package_name));
9888 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9889 saved_package_name);
9890 struct symbol *sym;
9891
9892 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9893
9894 sym = allocate_symbol (objfile);
9895 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9896 SYMBOL_SET_NAMES (sym, saved_package_name,
9897 strlen (saved_package_name), 0, objfile);
9898 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9899 e.g., "main" finds the "main" module and not C's main(). */
9900 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9901 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9902 SYMBOL_TYPE (sym) = type;
9903
9904 add_symbol_to_list (sym, &global_symbols);
9905
9906 xfree (package_name);
9907 }
9908 }
9909
9910 /* Allocate a fully-qualified name consisting of the two parts on the
9911 obstack. */
9912
9913 static const char *
9914 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9915 {
9916 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9917 }
9918
9919 /* A helper that allocates a struct discriminant_info to attach to a
9920 union type. */
9921
9922 static struct discriminant_info *
9923 alloc_discriminant_info (struct type *type, int discriminant_index,
9924 int default_index)
9925 {
9926 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9927 gdb_assert (discriminant_index == -1
9928 || (discriminant_index >= 0
9929 && discriminant_index < TYPE_NFIELDS (type)));
9930 gdb_assert (default_index == -1
9931 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9932
9933 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9934
9935 struct discriminant_info *disc
9936 = ((struct discriminant_info *)
9937 TYPE_ZALLOC (type,
9938 offsetof (struct discriminant_info, discriminants)
9939 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9940 disc->default_index = default_index;
9941 disc->discriminant_index = discriminant_index;
9942
9943 struct dynamic_prop prop;
9944 prop.kind = PROP_UNDEFINED;
9945 prop.data.baton = disc;
9946
9947 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9948
9949 return disc;
9950 }
9951
9952 /* Some versions of rustc emitted enums in an unusual way.
9953
9954 Ordinary enums were emitted as unions. The first element of each
9955 structure in the union was named "RUST$ENUM$DISR". This element
9956 held the discriminant.
9957
9958 These versions of Rust also implemented the "non-zero"
9959 optimization. When the enum had two values, and one is empty and
9960 the other holds a pointer that cannot be zero, the pointer is used
9961 as the discriminant, with a zero value meaning the empty variant.
9962 Here, the union's first member is of the form
9963 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9964 where the fieldnos are the indices of the fields that should be
9965 traversed in order to find the field (which may be several fields deep)
9966 and the variantname is the name of the variant of the case when the
9967 field is zero.
9968
9969 This function recognizes whether TYPE is of one of these forms,
9970 and, if so, smashes it to be a variant type. */
9971
9972 static void
9973 quirk_rust_enum (struct type *type, struct objfile *objfile)
9974 {
9975 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9976
9977 /* We don't need to deal with empty enums. */
9978 if (TYPE_NFIELDS (type) == 0)
9979 return;
9980
9981 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9982 if (TYPE_NFIELDS (type) == 1
9983 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9984 {
9985 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9986
9987 /* Decode the field name to find the offset of the
9988 discriminant. */
9989 ULONGEST bit_offset = 0;
9990 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9991 while (name[0] >= '0' && name[0] <= '9')
9992 {
9993 char *tail;
9994 unsigned long index = strtoul (name, &tail, 10);
9995 name = tail;
9996 if (*name != '$'
9997 || index >= TYPE_NFIELDS (field_type)
9998 || (TYPE_FIELD_LOC_KIND (field_type, index)
9999 != FIELD_LOC_KIND_BITPOS))
10000 {
10001 complaint (&symfile_complaints,
10002 _("Could not parse Rust enum encoding string \"%s\""
10003 "[in module %s]"),
10004 TYPE_FIELD_NAME (type, 0),
10005 objfile_name (objfile));
10006 return;
10007 }
10008 ++name;
10009
10010 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10011 field_type = TYPE_FIELD_TYPE (field_type, index);
10012 }
10013
10014 /* Make a union to hold the variants. */
10015 struct type *union_type = alloc_type (objfile);
10016 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10017 TYPE_NFIELDS (union_type) = 3;
10018 TYPE_FIELDS (union_type)
10019 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10020 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10021
10022 /* Put the discriminant must at index 0. */
10023 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10024 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10025 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10026 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10027
10028 /* The order of fields doesn't really matter, so put the real
10029 field at index 1 and the data-less field at index 2. */
10030 struct discriminant_info *disc
10031 = alloc_discriminant_info (union_type, 0, 1);
10032 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10033 TYPE_FIELD_NAME (union_type, 1)
10034 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10035 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10036 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10037 TYPE_FIELD_NAME (union_type, 1));
10038
10039 const char *dataless_name
10040 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10041 name);
10042 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10043 dataless_name);
10044 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10045 /* NAME points into the original discriminant name, which
10046 already has the correct lifetime. */
10047 TYPE_FIELD_NAME (union_type, 2) = name;
10048 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10049 disc->discriminants[2] = 0;
10050
10051 /* Smash this type to be a structure type. We have to do this
10052 because the type has already been recorded. */
10053 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10054 TYPE_NFIELDS (type) = 1;
10055 TYPE_FIELDS (type)
10056 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10057
10058 /* Install the variant part. */
10059 TYPE_FIELD_TYPE (type, 0) = union_type;
10060 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10061 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10062 }
10063 else if (TYPE_NFIELDS (type) == 1)
10064 {
10065 /* We assume that a union with a single field is a univariant
10066 enum. */
10067 /* Smash this type to be a structure type. We have to do this
10068 because the type has already been recorded. */
10069 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10070
10071 /* Make a union to hold the variants. */
10072 struct type *union_type = alloc_type (objfile);
10073 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10074 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10075 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10076 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10077
10078 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10079 const char *variant_name
10080 = rust_last_path_segment (TYPE_NAME (field_type));
10081 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10082 TYPE_NAME (field_type)
10083 = rust_fully_qualify (&objfile->objfile_obstack,
10084 TYPE_NAME (type), variant_name);
10085
10086 /* Install the union in the outer struct type. */
10087 TYPE_NFIELDS (type) = 1;
10088 TYPE_FIELDS (type)
10089 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10090 TYPE_FIELD_TYPE (type, 0) = union_type;
10091 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10092 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10093
10094 alloc_discriminant_info (union_type, -1, 0);
10095 }
10096 else
10097 {
10098 struct type *disr_type = nullptr;
10099 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10100 {
10101 disr_type = TYPE_FIELD_TYPE (type, i);
10102
10103 if (TYPE_NFIELDS (disr_type) == 0)
10104 {
10105 /* Could be data-less variant, so keep going. */
10106 }
10107 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10108 "RUST$ENUM$DISR") != 0)
10109 {
10110 /* Not a Rust enum. */
10111 return;
10112 }
10113 else
10114 {
10115 /* Found one. */
10116 break;
10117 }
10118 }
10119
10120 /* If we got here without a discriminant, then it's probably
10121 just a union. */
10122 if (disr_type == nullptr)
10123 return;
10124
10125 /* Smash this type to be a structure type. We have to do this
10126 because the type has already been recorded. */
10127 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10128
10129 /* Make a union to hold the variants. */
10130 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10131 struct type *union_type = alloc_type (objfile);
10132 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10133 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10134 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10135 TYPE_FIELDS (union_type)
10136 = (struct field *) TYPE_ZALLOC (union_type,
10137 (TYPE_NFIELDS (union_type)
10138 * sizeof (struct field)));
10139
10140 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10141 TYPE_NFIELDS (type) * sizeof (struct field));
10142
10143 /* Install the discriminant at index 0 in the union. */
10144 TYPE_FIELD (union_type, 0) = *disr_field;
10145 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10146 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10147
10148 /* Install the union in the outer struct type. */
10149 TYPE_FIELD_TYPE (type, 0) = union_type;
10150 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10151 TYPE_NFIELDS (type) = 1;
10152
10153 /* Set the size and offset of the union type. */
10154 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10155
10156 /* We need a way to find the correct discriminant given a
10157 variant name. For convenience we build a map here. */
10158 struct type *enum_type = FIELD_TYPE (*disr_field);
10159 std::unordered_map<std::string, ULONGEST> discriminant_map;
10160 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10161 {
10162 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10163 {
10164 const char *name
10165 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10166 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10167 }
10168 }
10169
10170 int n_fields = TYPE_NFIELDS (union_type);
10171 struct discriminant_info *disc
10172 = alloc_discriminant_info (union_type, 0, -1);
10173 /* Skip the discriminant here. */
10174 for (int i = 1; i < n_fields; ++i)
10175 {
10176 /* Find the final word in the name of this variant's type.
10177 That name can be used to look up the correct
10178 discriminant. */
10179 const char *variant_name
10180 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10181 i)));
10182
10183 auto iter = discriminant_map.find (variant_name);
10184 if (iter != discriminant_map.end ())
10185 disc->discriminants[i] = iter->second;
10186
10187 /* Remove the discriminant field. */
10188 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10189 --TYPE_NFIELDS (sub_type);
10190 ++TYPE_FIELDS (sub_type);
10191 TYPE_FIELD_NAME (union_type, i) = variant_name;
10192 TYPE_NAME (sub_type)
10193 = rust_fully_qualify (&objfile->objfile_obstack,
10194 TYPE_NAME (type), variant_name);
10195 }
10196 }
10197 }
10198
10199 /* Rewrite some Rust unions to be structures with variants parts. */
10200
10201 static void
10202 rust_union_quirks (struct dwarf2_cu *cu)
10203 {
10204 gdb_assert (cu->language == language_rust);
10205 for (struct type *type : cu->rust_unions)
10206 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10207 }
10208
10209 /* Return the symtab for PER_CU. This works properly regardless of
10210 whether we're using the index or psymtabs. */
10211
10212 static struct compunit_symtab *
10213 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10214 {
10215 return (per_cu->dwarf2_per_objfile->using_index
10216 ? per_cu->v.quick->compunit_symtab
10217 : per_cu->v.psymtab->compunit_symtab);
10218 }
10219
10220 /* A helper function for computing the list of all symbol tables
10221 included by PER_CU. */
10222
10223 static void
10224 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10225 htab_t all_children, htab_t all_type_symtabs,
10226 struct dwarf2_per_cu_data *per_cu,
10227 struct compunit_symtab *immediate_parent)
10228 {
10229 void **slot;
10230 int ix;
10231 struct compunit_symtab *cust;
10232 struct dwarf2_per_cu_data *iter;
10233
10234 slot = htab_find_slot (all_children, per_cu, INSERT);
10235 if (*slot != NULL)
10236 {
10237 /* This inclusion and its children have been processed. */
10238 return;
10239 }
10240
10241 *slot = per_cu;
10242 /* Only add a CU if it has a symbol table. */
10243 cust = get_compunit_symtab (per_cu);
10244 if (cust != NULL)
10245 {
10246 /* If this is a type unit only add its symbol table if we haven't
10247 seen it yet (type unit per_cu's can share symtabs). */
10248 if (per_cu->is_debug_types)
10249 {
10250 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10251 if (*slot == NULL)
10252 {
10253 *slot = cust;
10254 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10255 if (cust->user == NULL)
10256 cust->user = immediate_parent;
10257 }
10258 }
10259 else
10260 {
10261 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10262 if (cust->user == NULL)
10263 cust->user = immediate_parent;
10264 }
10265 }
10266
10267 for (ix = 0;
10268 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10269 ++ix)
10270 {
10271 recursively_compute_inclusions (result, all_children,
10272 all_type_symtabs, iter, cust);
10273 }
10274 }
10275
10276 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10277 PER_CU. */
10278
10279 static void
10280 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10281 {
10282 gdb_assert (! per_cu->is_debug_types);
10283
10284 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10285 {
10286 int ix, len;
10287 struct dwarf2_per_cu_data *per_cu_iter;
10288 struct compunit_symtab *compunit_symtab_iter;
10289 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10290 htab_t all_children, all_type_symtabs;
10291 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10292
10293 /* If we don't have a symtab, we can just skip this case. */
10294 if (cust == NULL)
10295 return;
10296
10297 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10298 NULL, xcalloc, xfree);
10299 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10300 NULL, xcalloc, xfree);
10301
10302 for (ix = 0;
10303 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10304 ix, per_cu_iter);
10305 ++ix)
10306 {
10307 recursively_compute_inclusions (&result_symtabs, all_children,
10308 all_type_symtabs, per_cu_iter,
10309 cust);
10310 }
10311
10312 /* Now we have a transitive closure of all the included symtabs. */
10313 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10314 cust->includes
10315 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10316 struct compunit_symtab *, len + 1);
10317 for (ix = 0;
10318 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10319 compunit_symtab_iter);
10320 ++ix)
10321 cust->includes[ix] = compunit_symtab_iter;
10322 cust->includes[len] = NULL;
10323
10324 VEC_free (compunit_symtab_ptr, result_symtabs);
10325 htab_delete (all_children);
10326 htab_delete (all_type_symtabs);
10327 }
10328 }
10329
10330 /* Compute the 'includes' field for the symtabs of all the CUs we just
10331 read. */
10332
10333 static void
10334 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10335 {
10336 int ix;
10337 struct dwarf2_per_cu_data *iter;
10338
10339 for (ix = 0;
10340 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10341 ix, iter);
10342 ++ix)
10343 {
10344 if (! iter->is_debug_types)
10345 compute_compunit_symtab_includes (iter);
10346 }
10347
10348 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10349 }
10350
10351 /* Generate full symbol information for PER_CU, whose DIEs have
10352 already been loaded into memory. */
10353
10354 static void
10355 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10356 enum language pretend_language)
10357 {
10358 struct dwarf2_cu *cu = per_cu->cu;
10359 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10360 struct objfile *objfile = dwarf2_per_objfile->objfile;
10361 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10362 CORE_ADDR lowpc, highpc;
10363 struct compunit_symtab *cust;
10364 CORE_ADDR baseaddr;
10365 struct block *static_block;
10366 CORE_ADDR addr;
10367
10368 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10369
10370 buildsym_init ();
10371 scoped_free_pendings free_pending;
10372
10373 /* Clear the list here in case something was left over. */
10374 cu->method_list.clear ();
10375
10376 cu->list_in_scope = &file_symbols;
10377
10378 cu->language = pretend_language;
10379 cu->language_defn = language_def (cu->language);
10380
10381 /* Do line number decoding in read_file_scope () */
10382 process_die (cu->dies, cu);
10383
10384 /* For now fudge the Go package. */
10385 if (cu->language == language_go)
10386 fixup_go_packaging (cu);
10387
10388 /* Now that we have processed all the DIEs in the CU, all the types
10389 should be complete, and it should now be safe to compute all of the
10390 physnames. */
10391 compute_delayed_physnames (cu);
10392
10393 if (cu->language == language_rust)
10394 rust_union_quirks (cu);
10395
10396 /* Some compilers don't define a DW_AT_high_pc attribute for the
10397 compilation unit. If the DW_AT_high_pc is missing, synthesize
10398 it, by scanning the DIE's below the compilation unit. */
10399 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10400
10401 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10402 static_block = end_symtab_get_static_block (addr, 0, 1);
10403
10404 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10405 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10406 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10407 addrmap to help ensure it has an accurate map of pc values belonging to
10408 this comp unit. */
10409 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10410
10411 cust = end_symtab_from_static_block (static_block,
10412 SECT_OFF_TEXT (objfile), 0);
10413
10414 if (cust != NULL)
10415 {
10416 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10417
10418 /* Set symtab language to language from DW_AT_language. If the
10419 compilation is from a C file generated by language preprocessors, do
10420 not set the language if it was already deduced by start_subfile. */
10421 if (!(cu->language == language_c
10422 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10423 COMPUNIT_FILETABS (cust)->language = cu->language;
10424
10425 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10426 produce DW_AT_location with location lists but it can be possibly
10427 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10428 there were bugs in prologue debug info, fixed later in GCC-4.5
10429 by "unwind info for epilogues" patch (which is not directly related).
10430
10431 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10432 needed, it would be wrong due to missing DW_AT_producer there.
10433
10434 Still one can confuse GDB by using non-standard GCC compilation
10435 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10436 */
10437 if (cu->has_loclist && gcc_4_minor >= 5)
10438 cust->locations_valid = 1;
10439
10440 if (gcc_4_minor >= 5)
10441 cust->epilogue_unwind_valid = 1;
10442
10443 cust->call_site_htab = cu->call_site_htab;
10444 }
10445
10446 if (dwarf2_per_objfile->using_index)
10447 per_cu->v.quick->compunit_symtab = cust;
10448 else
10449 {
10450 struct partial_symtab *pst = per_cu->v.psymtab;
10451 pst->compunit_symtab = cust;
10452 pst->readin = 1;
10453 }
10454
10455 /* Push it for inclusion processing later. */
10456 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10457 }
10458
10459 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10460 already been loaded into memory. */
10461
10462 static void
10463 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10464 enum language pretend_language)
10465 {
10466 struct dwarf2_cu *cu = per_cu->cu;
10467 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10468 struct objfile *objfile = dwarf2_per_objfile->objfile;
10469 struct compunit_symtab *cust;
10470 struct signatured_type *sig_type;
10471
10472 gdb_assert (per_cu->is_debug_types);
10473 sig_type = (struct signatured_type *) per_cu;
10474
10475 buildsym_init ();
10476 scoped_free_pendings free_pending;
10477
10478 /* Clear the list here in case something was left over. */
10479 cu->method_list.clear ();
10480
10481 cu->list_in_scope = &file_symbols;
10482
10483 cu->language = pretend_language;
10484 cu->language_defn = language_def (cu->language);
10485
10486 /* The symbol tables are set up in read_type_unit_scope. */
10487 process_die (cu->dies, cu);
10488
10489 /* For now fudge the Go package. */
10490 if (cu->language == language_go)
10491 fixup_go_packaging (cu);
10492
10493 /* Now that we have processed all the DIEs in the CU, all the types
10494 should be complete, and it should now be safe to compute all of the
10495 physnames. */
10496 compute_delayed_physnames (cu);
10497
10498 if (cu->language == language_rust)
10499 rust_union_quirks (cu);
10500
10501 /* TUs share symbol tables.
10502 If this is the first TU to use this symtab, complete the construction
10503 of it with end_expandable_symtab. Otherwise, complete the addition of
10504 this TU's symbols to the existing symtab. */
10505 if (sig_type->type_unit_group->compunit_symtab == NULL)
10506 {
10507 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10508 sig_type->type_unit_group->compunit_symtab = cust;
10509
10510 if (cust != NULL)
10511 {
10512 /* Set symtab language to language from DW_AT_language. If the
10513 compilation is from a C file generated by language preprocessors,
10514 do not set the language if it was already deduced by
10515 start_subfile. */
10516 if (!(cu->language == language_c
10517 && COMPUNIT_FILETABS (cust)->language != language_c))
10518 COMPUNIT_FILETABS (cust)->language = cu->language;
10519 }
10520 }
10521 else
10522 {
10523 augment_type_symtab ();
10524 cust = sig_type->type_unit_group->compunit_symtab;
10525 }
10526
10527 if (dwarf2_per_objfile->using_index)
10528 per_cu->v.quick->compunit_symtab = cust;
10529 else
10530 {
10531 struct partial_symtab *pst = per_cu->v.psymtab;
10532 pst->compunit_symtab = cust;
10533 pst->readin = 1;
10534 }
10535 }
10536
10537 /* Process an imported unit DIE. */
10538
10539 static void
10540 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10541 {
10542 struct attribute *attr;
10543
10544 /* For now we don't handle imported units in type units. */
10545 if (cu->per_cu->is_debug_types)
10546 {
10547 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10548 " supported in type units [in module %s]"),
10549 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10550 }
10551
10552 attr = dwarf2_attr (die, DW_AT_import, cu);
10553 if (attr != NULL)
10554 {
10555 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10556 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10557 dwarf2_per_cu_data *per_cu
10558 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10559 cu->per_cu->dwarf2_per_objfile);
10560
10561 /* If necessary, add it to the queue and load its DIEs. */
10562 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10563 load_full_comp_unit (per_cu, cu->language);
10564
10565 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10566 per_cu);
10567 }
10568 }
10569
10570 /* RAII object that represents a process_die scope: i.e.,
10571 starts/finishes processing a DIE. */
10572 class process_die_scope
10573 {
10574 public:
10575 process_die_scope (die_info *die, dwarf2_cu *cu)
10576 : m_die (die), m_cu (cu)
10577 {
10578 /* We should only be processing DIEs not already in process. */
10579 gdb_assert (!m_die->in_process);
10580 m_die->in_process = true;
10581 }
10582
10583 ~process_die_scope ()
10584 {
10585 m_die->in_process = false;
10586
10587 /* If we're done processing the DIE for the CU that owns the line
10588 header, we don't need the line header anymore. */
10589 if (m_cu->line_header_die_owner == m_die)
10590 {
10591 delete m_cu->line_header;
10592 m_cu->line_header = NULL;
10593 m_cu->line_header_die_owner = NULL;
10594 }
10595 }
10596
10597 private:
10598 die_info *m_die;
10599 dwarf2_cu *m_cu;
10600 };
10601
10602 /* Process a die and its children. */
10603
10604 static void
10605 process_die (struct die_info *die, struct dwarf2_cu *cu)
10606 {
10607 process_die_scope scope (die, cu);
10608
10609 switch (die->tag)
10610 {
10611 case DW_TAG_padding:
10612 break;
10613 case DW_TAG_compile_unit:
10614 case DW_TAG_partial_unit:
10615 read_file_scope (die, cu);
10616 break;
10617 case DW_TAG_type_unit:
10618 read_type_unit_scope (die, cu);
10619 break;
10620 case DW_TAG_subprogram:
10621 case DW_TAG_inlined_subroutine:
10622 read_func_scope (die, cu);
10623 break;
10624 case DW_TAG_lexical_block:
10625 case DW_TAG_try_block:
10626 case DW_TAG_catch_block:
10627 read_lexical_block_scope (die, cu);
10628 break;
10629 case DW_TAG_call_site:
10630 case DW_TAG_GNU_call_site:
10631 read_call_site_scope (die, cu);
10632 break;
10633 case DW_TAG_class_type:
10634 case DW_TAG_interface_type:
10635 case DW_TAG_structure_type:
10636 case DW_TAG_union_type:
10637 process_structure_scope (die, cu);
10638 break;
10639 case DW_TAG_enumeration_type:
10640 process_enumeration_scope (die, cu);
10641 break;
10642
10643 /* These dies have a type, but processing them does not create
10644 a symbol or recurse to process the children. Therefore we can
10645 read them on-demand through read_type_die. */
10646 case DW_TAG_subroutine_type:
10647 case DW_TAG_set_type:
10648 case DW_TAG_array_type:
10649 case DW_TAG_pointer_type:
10650 case DW_TAG_ptr_to_member_type:
10651 case DW_TAG_reference_type:
10652 case DW_TAG_rvalue_reference_type:
10653 case DW_TAG_string_type:
10654 break;
10655
10656 case DW_TAG_base_type:
10657 case DW_TAG_subrange_type:
10658 case DW_TAG_typedef:
10659 /* Add a typedef symbol for the type definition, if it has a
10660 DW_AT_name. */
10661 new_symbol (die, read_type_die (die, cu), cu);
10662 break;
10663 case DW_TAG_common_block:
10664 read_common_block (die, cu);
10665 break;
10666 case DW_TAG_common_inclusion:
10667 break;
10668 case DW_TAG_namespace:
10669 cu->processing_has_namespace_info = 1;
10670 read_namespace (die, cu);
10671 break;
10672 case DW_TAG_module:
10673 cu->processing_has_namespace_info = 1;
10674 read_module (die, cu);
10675 break;
10676 case DW_TAG_imported_declaration:
10677 cu->processing_has_namespace_info = 1;
10678 if (read_namespace_alias (die, cu))
10679 break;
10680 /* The declaration is not a global namespace alias: fall through. */
10681 case DW_TAG_imported_module:
10682 cu->processing_has_namespace_info = 1;
10683 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10684 || cu->language != language_fortran))
10685 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10686 dwarf_tag_name (die->tag));
10687 read_import_statement (die, cu);
10688 break;
10689
10690 case DW_TAG_imported_unit:
10691 process_imported_unit_die (die, cu);
10692 break;
10693
10694 case DW_TAG_variable:
10695 read_variable (die, cu);
10696 break;
10697
10698 default:
10699 new_symbol (die, NULL, cu);
10700 break;
10701 }
10702 }
10703 \f
10704 /* DWARF name computation. */
10705
10706 /* A helper function for dwarf2_compute_name which determines whether DIE
10707 needs to have the name of the scope prepended to the name listed in the
10708 die. */
10709
10710 static int
10711 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10712 {
10713 struct attribute *attr;
10714
10715 switch (die->tag)
10716 {
10717 case DW_TAG_namespace:
10718 case DW_TAG_typedef:
10719 case DW_TAG_class_type:
10720 case DW_TAG_interface_type:
10721 case DW_TAG_structure_type:
10722 case DW_TAG_union_type:
10723 case DW_TAG_enumeration_type:
10724 case DW_TAG_enumerator:
10725 case DW_TAG_subprogram:
10726 case DW_TAG_inlined_subroutine:
10727 case DW_TAG_member:
10728 case DW_TAG_imported_declaration:
10729 return 1;
10730
10731 case DW_TAG_variable:
10732 case DW_TAG_constant:
10733 /* We only need to prefix "globally" visible variables. These include
10734 any variable marked with DW_AT_external or any variable that
10735 lives in a namespace. [Variables in anonymous namespaces
10736 require prefixing, but they are not DW_AT_external.] */
10737
10738 if (dwarf2_attr (die, DW_AT_specification, cu))
10739 {
10740 struct dwarf2_cu *spec_cu = cu;
10741
10742 return die_needs_namespace (die_specification (die, &spec_cu),
10743 spec_cu);
10744 }
10745
10746 attr = dwarf2_attr (die, DW_AT_external, cu);
10747 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10748 && die->parent->tag != DW_TAG_module)
10749 return 0;
10750 /* A variable in a lexical block of some kind does not need a
10751 namespace, even though in C++ such variables may be external
10752 and have a mangled name. */
10753 if (die->parent->tag == DW_TAG_lexical_block
10754 || die->parent->tag == DW_TAG_try_block
10755 || die->parent->tag == DW_TAG_catch_block
10756 || die->parent->tag == DW_TAG_subprogram)
10757 return 0;
10758 return 1;
10759
10760 default:
10761 return 0;
10762 }
10763 }
10764
10765 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10766 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10767 defined for the given DIE. */
10768
10769 static struct attribute *
10770 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10771 {
10772 struct attribute *attr;
10773
10774 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10775 if (attr == NULL)
10776 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10777
10778 return attr;
10779 }
10780
10781 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10782 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10783 defined for the given DIE. */
10784
10785 static const char *
10786 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10787 {
10788 const char *linkage_name;
10789
10790 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10791 if (linkage_name == NULL)
10792 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10793
10794 return linkage_name;
10795 }
10796
10797 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10798 compute the physname for the object, which include a method's:
10799 - formal parameters (C++),
10800 - receiver type (Go),
10801
10802 The term "physname" is a bit confusing.
10803 For C++, for example, it is the demangled name.
10804 For Go, for example, it's the mangled name.
10805
10806 For Ada, return the DIE's linkage name rather than the fully qualified
10807 name. PHYSNAME is ignored..
10808
10809 The result is allocated on the objfile_obstack and canonicalized. */
10810
10811 static const char *
10812 dwarf2_compute_name (const char *name,
10813 struct die_info *die, struct dwarf2_cu *cu,
10814 int physname)
10815 {
10816 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10817
10818 if (name == NULL)
10819 name = dwarf2_name (die, cu);
10820
10821 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10822 but otherwise compute it by typename_concat inside GDB.
10823 FIXME: Actually this is not really true, or at least not always true.
10824 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10825 Fortran names because there is no mangling standard. So new_symbol
10826 will set the demangled name to the result of dwarf2_full_name, and it is
10827 the demangled name that GDB uses if it exists. */
10828 if (cu->language == language_ada
10829 || (cu->language == language_fortran && physname))
10830 {
10831 /* For Ada unit, we prefer the linkage name over the name, as
10832 the former contains the exported name, which the user expects
10833 to be able to reference. Ideally, we want the user to be able
10834 to reference this entity using either natural or linkage name,
10835 but we haven't started looking at this enhancement yet. */
10836 const char *linkage_name = dw2_linkage_name (die, cu);
10837
10838 if (linkage_name != NULL)
10839 return linkage_name;
10840 }
10841
10842 /* These are the only languages we know how to qualify names in. */
10843 if (name != NULL
10844 && (cu->language == language_cplus
10845 || cu->language == language_fortran || cu->language == language_d
10846 || cu->language == language_rust))
10847 {
10848 if (die_needs_namespace (die, cu))
10849 {
10850 const char *prefix;
10851 const char *canonical_name = NULL;
10852
10853 string_file buf;
10854
10855 prefix = determine_prefix (die, cu);
10856 if (*prefix != '\0')
10857 {
10858 char *prefixed_name = typename_concat (NULL, prefix, name,
10859 physname, cu);
10860
10861 buf.puts (prefixed_name);
10862 xfree (prefixed_name);
10863 }
10864 else
10865 buf.puts (name);
10866
10867 /* Template parameters may be specified in the DIE's DW_AT_name, or
10868 as children with DW_TAG_template_type_param or
10869 DW_TAG_value_type_param. If the latter, add them to the name
10870 here. If the name already has template parameters, then
10871 skip this step; some versions of GCC emit both, and
10872 it is more efficient to use the pre-computed name.
10873
10874 Something to keep in mind about this process: it is very
10875 unlikely, or in some cases downright impossible, to produce
10876 something that will match the mangled name of a function.
10877 If the definition of the function has the same debug info,
10878 we should be able to match up with it anyway. But fallbacks
10879 using the minimal symbol, for instance to find a method
10880 implemented in a stripped copy of libstdc++, will not work.
10881 If we do not have debug info for the definition, we will have to
10882 match them up some other way.
10883
10884 When we do name matching there is a related problem with function
10885 templates; two instantiated function templates are allowed to
10886 differ only by their return types, which we do not add here. */
10887
10888 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10889 {
10890 struct attribute *attr;
10891 struct die_info *child;
10892 int first = 1;
10893
10894 die->building_fullname = 1;
10895
10896 for (child = die->child; child != NULL; child = child->sibling)
10897 {
10898 struct type *type;
10899 LONGEST value;
10900 const gdb_byte *bytes;
10901 struct dwarf2_locexpr_baton *baton;
10902 struct value *v;
10903
10904 if (child->tag != DW_TAG_template_type_param
10905 && child->tag != DW_TAG_template_value_param)
10906 continue;
10907
10908 if (first)
10909 {
10910 buf.puts ("<");
10911 first = 0;
10912 }
10913 else
10914 buf.puts (", ");
10915
10916 attr = dwarf2_attr (child, DW_AT_type, cu);
10917 if (attr == NULL)
10918 {
10919 complaint (&symfile_complaints,
10920 _("template parameter missing DW_AT_type"));
10921 buf.puts ("UNKNOWN_TYPE");
10922 continue;
10923 }
10924 type = die_type (child, cu);
10925
10926 if (child->tag == DW_TAG_template_type_param)
10927 {
10928 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10929 continue;
10930 }
10931
10932 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10933 if (attr == NULL)
10934 {
10935 complaint (&symfile_complaints,
10936 _("template parameter missing "
10937 "DW_AT_const_value"));
10938 buf.puts ("UNKNOWN_VALUE");
10939 continue;
10940 }
10941
10942 dwarf2_const_value_attr (attr, type, name,
10943 &cu->comp_unit_obstack, cu,
10944 &value, &bytes, &baton);
10945
10946 if (TYPE_NOSIGN (type))
10947 /* GDB prints characters as NUMBER 'CHAR'. If that's
10948 changed, this can use value_print instead. */
10949 c_printchar (value, type, &buf);
10950 else
10951 {
10952 struct value_print_options opts;
10953
10954 if (baton != NULL)
10955 v = dwarf2_evaluate_loc_desc (type, NULL,
10956 baton->data,
10957 baton->size,
10958 baton->per_cu);
10959 else if (bytes != NULL)
10960 {
10961 v = allocate_value (type);
10962 memcpy (value_contents_writeable (v), bytes,
10963 TYPE_LENGTH (type));
10964 }
10965 else
10966 v = value_from_longest (type, value);
10967
10968 /* Specify decimal so that we do not depend on
10969 the radix. */
10970 get_formatted_print_options (&opts, 'd');
10971 opts.raw = 1;
10972 value_print (v, &buf, &opts);
10973 release_value (v);
10974 }
10975 }
10976
10977 die->building_fullname = 0;
10978
10979 if (!first)
10980 {
10981 /* Close the argument list, with a space if necessary
10982 (nested templates). */
10983 if (!buf.empty () && buf.string ().back () == '>')
10984 buf.puts (" >");
10985 else
10986 buf.puts (">");
10987 }
10988 }
10989
10990 /* For C++ methods, append formal parameter type
10991 information, if PHYSNAME. */
10992
10993 if (physname && die->tag == DW_TAG_subprogram
10994 && cu->language == language_cplus)
10995 {
10996 struct type *type = read_type_die (die, cu);
10997
10998 c_type_print_args (type, &buf, 1, cu->language,
10999 &type_print_raw_options);
11000
11001 if (cu->language == language_cplus)
11002 {
11003 /* Assume that an artificial first parameter is
11004 "this", but do not crash if it is not. RealView
11005 marks unnamed (and thus unused) parameters as
11006 artificial; there is no way to differentiate
11007 the two cases. */
11008 if (TYPE_NFIELDS (type) > 0
11009 && TYPE_FIELD_ARTIFICIAL (type, 0)
11010 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11011 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11012 0))))
11013 buf.puts (" const");
11014 }
11015 }
11016
11017 const std::string &intermediate_name = buf.string ();
11018
11019 if (cu->language == language_cplus)
11020 canonical_name
11021 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11022 &objfile->per_bfd->storage_obstack);
11023
11024 /* If we only computed INTERMEDIATE_NAME, or if
11025 INTERMEDIATE_NAME is already canonical, then we need to
11026 copy it to the appropriate obstack. */
11027 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11028 name = ((const char *)
11029 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11030 intermediate_name.c_str (),
11031 intermediate_name.length ()));
11032 else
11033 name = canonical_name;
11034 }
11035 }
11036
11037 return name;
11038 }
11039
11040 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11041 If scope qualifiers are appropriate they will be added. The result
11042 will be allocated on the storage_obstack, or NULL if the DIE does
11043 not have a name. NAME may either be from a previous call to
11044 dwarf2_name or NULL.
11045
11046 The output string will be canonicalized (if C++). */
11047
11048 static const char *
11049 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11050 {
11051 return dwarf2_compute_name (name, die, cu, 0);
11052 }
11053
11054 /* Construct a physname for the given DIE in CU. NAME may either be
11055 from a previous call to dwarf2_name or NULL. The result will be
11056 allocated on the objfile_objstack or NULL if the DIE does not have a
11057 name.
11058
11059 The output string will be canonicalized (if C++). */
11060
11061 static const char *
11062 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11063 {
11064 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11065 const char *retval, *mangled = NULL, *canon = NULL;
11066 int need_copy = 1;
11067
11068 /* In this case dwarf2_compute_name is just a shortcut not building anything
11069 on its own. */
11070 if (!die_needs_namespace (die, cu))
11071 return dwarf2_compute_name (name, die, cu, 1);
11072
11073 mangled = dw2_linkage_name (die, cu);
11074
11075 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11076 See https://github.com/rust-lang/rust/issues/32925. */
11077 if (cu->language == language_rust && mangled != NULL
11078 && strchr (mangled, '{') != NULL)
11079 mangled = NULL;
11080
11081 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11082 has computed. */
11083 gdb::unique_xmalloc_ptr<char> demangled;
11084 if (mangled != NULL)
11085 {
11086
11087 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11088 {
11089 /* Do nothing (do not demangle the symbol name). */
11090 }
11091 else if (cu->language == language_go)
11092 {
11093 /* This is a lie, but we already lie to the caller new_symbol.
11094 new_symbol assumes we return the mangled name.
11095 This just undoes that lie until things are cleaned up. */
11096 }
11097 else
11098 {
11099 /* Use DMGL_RET_DROP for C++ template functions to suppress
11100 their return type. It is easier for GDB users to search
11101 for such functions as `name(params)' than `long name(params)'.
11102 In such case the minimal symbol names do not match the full
11103 symbol names but for template functions there is never a need
11104 to look up their definition from their declaration so
11105 the only disadvantage remains the minimal symbol variant
11106 `long name(params)' does not have the proper inferior type. */
11107 demangled.reset (gdb_demangle (mangled,
11108 (DMGL_PARAMS | DMGL_ANSI
11109 | DMGL_RET_DROP)));
11110 }
11111 if (demangled)
11112 canon = demangled.get ();
11113 else
11114 {
11115 canon = mangled;
11116 need_copy = 0;
11117 }
11118 }
11119
11120 if (canon == NULL || check_physname)
11121 {
11122 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11123
11124 if (canon != NULL && strcmp (physname, canon) != 0)
11125 {
11126 /* It may not mean a bug in GDB. The compiler could also
11127 compute DW_AT_linkage_name incorrectly. But in such case
11128 GDB would need to be bug-to-bug compatible. */
11129
11130 complaint (&symfile_complaints,
11131 _("Computed physname <%s> does not match demangled <%s> "
11132 "(from linkage <%s>) - DIE at %s [in module %s]"),
11133 physname, canon, mangled, sect_offset_str (die->sect_off),
11134 objfile_name (objfile));
11135
11136 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11137 is available here - over computed PHYSNAME. It is safer
11138 against both buggy GDB and buggy compilers. */
11139
11140 retval = canon;
11141 }
11142 else
11143 {
11144 retval = physname;
11145 need_copy = 0;
11146 }
11147 }
11148 else
11149 retval = canon;
11150
11151 if (need_copy)
11152 retval = ((const char *)
11153 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11154 retval, strlen (retval)));
11155
11156 return retval;
11157 }
11158
11159 /* Inspect DIE in CU for a namespace alias. If one exists, record
11160 a new symbol for it.
11161
11162 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11163
11164 static int
11165 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11166 {
11167 struct attribute *attr;
11168
11169 /* If the die does not have a name, this is not a namespace
11170 alias. */
11171 attr = dwarf2_attr (die, DW_AT_name, cu);
11172 if (attr != NULL)
11173 {
11174 int num;
11175 struct die_info *d = die;
11176 struct dwarf2_cu *imported_cu = cu;
11177
11178 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11179 keep inspecting DIEs until we hit the underlying import. */
11180 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11181 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11182 {
11183 attr = dwarf2_attr (d, DW_AT_import, cu);
11184 if (attr == NULL)
11185 break;
11186
11187 d = follow_die_ref (d, attr, &imported_cu);
11188 if (d->tag != DW_TAG_imported_declaration)
11189 break;
11190 }
11191
11192 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11193 {
11194 complaint (&symfile_complaints,
11195 _("DIE at %s has too many recursively imported "
11196 "declarations"), sect_offset_str (d->sect_off));
11197 return 0;
11198 }
11199
11200 if (attr != NULL)
11201 {
11202 struct type *type;
11203 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11204
11205 type = get_die_type_at_offset (sect_off, cu->per_cu);
11206 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11207 {
11208 /* This declaration is a global namespace alias. Add
11209 a symbol for it whose type is the aliased namespace. */
11210 new_symbol (die, type, cu);
11211 return 1;
11212 }
11213 }
11214 }
11215
11216 return 0;
11217 }
11218
11219 /* Return the using directives repository (global or local?) to use in the
11220 current context for LANGUAGE.
11221
11222 For Ada, imported declarations can materialize renamings, which *may* be
11223 global. However it is impossible (for now?) in DWARF to distinguish
11224 "external" imported declarations and "static" ones. As all imported
11225 declarations seem to be static in all other languages, make them all CU-wide
11226 global only in Ada. */
11227
11228 static struct using_direct **
11229 using_directives (enum language language)
11230 {
11231 if (language == language_ada && context_stack_depth == 0)
11232 return &global_using_directives;
11233 else
11234 return &local_using_directives;
11235 }
11236
11237 /* Read the import statement specified by the given die and record it. */
11238
11239 static void
11240 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11241 {
11242 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11243 struct attribute *import_attr;
11244 struct die_info *imported_die, *child_die;
11245 struct dwarf2_cu *imported_cu;
11246 const char *imported_name;
11247 const char *imported_name_prefix;
11248 const char *canonical_name;
11249 const char *import_alias;
11250 const char *imported_declaration = NULL;
11251 const char *import_prefix;
11252 std::vector<const char *> excludes;
11253
11254 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11255 if (import_attr == NULL)
11256 {
11257 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11258 dwarf_tag_name (die->tag));
11259 return;
11260 }
11261
11262 imported_cu = cu;
11263 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11264 imported_name = dwarf2_name (imported_die, imported_cu);
11265 if (imported_name == NULL)
11266 {
11267 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11268
11269 The import in the following code:
11270 namespace A
11271 {
11272 typedef int B;
11273 }
11274
11275 int main ()
11276 {
11277 using A::B;
11278 B b;
11279 return b;
11280 }
11281
11282 ...
11283 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11284 <52> DW_AT_decl_file : 1
11285 <53> DW_AT_decl_line : 6
11286 <54> DW_AT_import : <0x75>
11287 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11288 <59> DW_AT_name : B
11289 <5b> DW_AT_decl_file : 1
11290 <5c> DW_AT_decl_line : 2
11291 <5d> DW_AT_type : <0x6e>
11292 ...
11293 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11294 <76> DW_AT_byte_size : 4
11295 <77> DW_AT_encoding : 5 (signed)
11296
11297 imports the wrong die ( 0x75 instead of 0x58 ).
11298 This case will be ignored until the gcc bug is fixed. */
11299 return;
11300 }
11301
11302 /* Figure out the local name after import. */
11303 import_alias = dwarf2_name (die, cu);
11304
11305 /* Figure out where the statement is being imported to. */
11306 import_prefix = determine_prefix (die, cu);
11307
11308 /* Figure out what the scope of the imported die is and prepend it
11309 to the name of the imported die. */
11310 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11311
11312 if (imported_die->tag != DW_TAG_namespace
11313 && imported_die->tag != DW_TAG_module)
11314 {
11315 imported_declaration = imported_name;
11316 canonical_name = imported_name_prefix;
11317 }
11318 else if (strlen (imported_name_prefix) > 0)
11319 canonical_name = obconcat (&objfile->objfile_obstack,
11320 imported_name_prefix,
11321 (cu->language == language_d ? "." : "::"),
11322 imported_name, (char *) NULL);
11323 else
11324 canonical_name = imported_name;
11325
11326 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11327 for (child_die = die->child; child_die && child_die->tag;
11328 child_die = sibling_die (child_die))
11329 {
11330 /* DWARF-4: A Fortran use statement with a “rename list” may be
11331 represented by an imported module entry with an import attribute
11332 referring to the module and owned entries corresponding to those
11333 entities that are renamed as part of being imported. */
11334
11335 if (child_die->tag != DW_TAG_imported_declaration)
11336 {
11337 complaint (&symfile_complaints,
11338 _("child DW_TAG_imported_declaration expected "
11339 "- DIE at %s [in module %s]"),
11340 sect_offset_str (child_die->sect_off),
11341 objfile_name (objfile));
11342 continue;
11343 }
11344
11345 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11346 if (import_attr == NULL)
11347 {
11348 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11349 dwarf_tag_name (child_die->tag));
11350 continue;
11351 }
11352
11353 imported_cu = cu;
11354 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11355 &imported_cu);
11356 imported_name = dwarf2_name (imported_die, imported_cu);
11357 if (imported_name == NULL)
11358 {
11359 complaint (&symfile_complaints,
11360 _("child DW_TAG_imported_declaration has unknown "
11361 "imported name - DIE at %s [in module %s]"),
11362 sect_offset_str (child_die->sect_off),
11363 objfile_name (objfile));
11364 continue;
11365 }
11366
11367 excludes.push_back (imported_name);
11368
11369 process_die (child_die, cu);
11370 }
11371
11372 add_using_directive (using_directives (cu->language),
11373 import_prefix,
11374 canonical_name,
11375 import_alias,
11376 imported_declaration,
11377 excludes,
11378 0,
11379 &objfile->objfile_obstack);
11380 }
11381
11382 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11383 types, but gives them a size of zero. Starting with version 14,
11384 ICC is compatible with GCC. */
11385
11386 static int
11387 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11388 {
11389 if (!cu->checked_producer)
11390 check_producer (cu);
11391
11392 return cu->producer_is_icc_lt_14;
11393 }
11394
11395 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11396 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11397 this, it was first present in GCC release 4.3.0. */
11398
11399 static int
11400 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11401 {
11402 if (!cu->checked_producer)
11403 check_producer (cu);
11404
11405 return cu->producer_is_gcc_lt_4_3;
11406 }
11407
11408 static file_and_directory
11409 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11410 {
11411 file_and_directory res;
11412
11413 /* Find the filename. Do not use dwarf2_name here, since the filename
11414 is not a source language identifier. */
11415 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11416 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11417
11418 if (res.comp_dir == NULL
11419 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11420 && IS_ABSOLUTE_PATH (res.name))
11421 {
11422 res.comp_dir_storage = ldirname (res.name);
11423 if (!res.comp_dir_storage.empty ())
11424 res.comp_dir = res.comp_dir_storage.c_str ();
11425 }
11426 if (res.comp_dir != NULL)
11427 {
11428 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11429 directory, get rid of it. */
11430 const char *cp = strchr (res.comp_dir, ':');
11431
11432 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11433 res.comp_dir = cp + 1;
11434 }
11435
11436 if (res.name == NULL)
11437 res.name = "<unknown>";
11438
11439 return res;
11440 }
11441
11442 /* Handle DW_AT_stmt_list for a compilation unit.
11443 DIE is the DW_TAG_compile_unit die for CU.
11444 COMP_DIR is the compilation directory. LOWPC is passed to
11445 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11446
11447 static void
11448 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11449 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11450 {
11451 struct dwarf2_per_objfile *dwarf2_per_objfile
11452 = cu->per_cu->dwarf2_per_objfile;
11453 struct objfile *objfile = dwarf2_per_objfile->objfile;
11454 struct attribute *attr;
11455 struct line_header line_header_local;
11456 hashval_t line_header_local_hash;
11457 void **slot;
11458 int decode_mapping;
11459
11460 gdb_assert (! cu->per_cu->is_debug_types);
11461
11462 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11463 if (attr == NULL)
11464 return;
11465
11466 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11467
11468 /* The line header hash table is only created if needed (it exists to
11469 prevent redundant reading of the line table for partial_units).
11470 If we're given a partial_unit, we'll need it. If we're given a
11471 compile_unit, then use the line header hash table if it's already
11472 created, but don't create one just yet. */
11473
11474 if (dwarf2_per_objfile->line_header_hash == NULL
11475 && die->tag == DW_TAG_partial_unit)
11476 {
11477 dwarf2_per_objfile->line_header_hash
11478 = htab_create_alloc_ex (127, line_header_hash_voidp,
11479 line_header_eq_voidp,
11480 free_line_header_voidp,
11481 &objfile->objfile_obstack,
11482 hashtab_obstack_allocate,
11483 dummy_obstack_deallocate);
11484 }
11485
11486 line_header_local.sect_off = line_offset;
11487 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11488 line_header_local_hash = line_header_hash (&line_header_local);
11489 if (dwarf2_per_objfile->line_header_hash != NULL)
11490 {
11491 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11492 &line_header_local,
11493 line_header_local_hash, NO_INSERT);
11494
11495 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11496 is not present in *SLOT (since if there is something in *SLOT then
11497 it will be for a partial_unit). */
11498 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11499 {
11500 gdb_assert (*slot != NULL);
11501 cu->line_header = (struct line_header *) *slot;
11502 return;
11503 }
11504 }
11505
11506 /* dwarf_decode_line_header does not yet provide sufficient information.
11507 We always have to call also dwarf_decode_lines for it. */
11508 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11509 if (lh == NULL)
11510 return;
11511
11512 cu->line_header = lh.release ();
11513 cu->line_header_die_owner = die;
11514
11515 if (dwarf2_per_objfile->line_header_hash == NULL)
11516 slot = NULL;
11517 else
11518 {
11519 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11520 &line_header_local,
11521 line_header_local_hash, INSERT);
11522 gdb_assert (slot != NULL);
11523 }
11524 if (slot != NULL && *slot == NULL)
11525 {
11526 /* This newly decoded line number information unit will be owned
11527 by line_header_hash hash table. */
11528 *slot = cu->line_header;
11529 cu->line_header_die_owner = NULL;
11530 }
11531 else
11532 {
11533 /* We cannot free any current entry in (*slot) as that struct line_header
11534 may be already used by multiple CUs. Create only temporary decoded
11535 line_header for this CU - it may happen at most once for each line
11536 number information unit. And if we're not using line_header_hash
11537 then this is what we want as well. */
11538 gdb_assert (die->tag != DW_TAG_partial_unit);
11539 }
11540 decode_mapping = (die->tag != DW_TAG_partial_unit);
11541 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11542 decode_mapping);
11543
11544 }
11545
11546 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11547
11548 static void
11549 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11550 {
11551 struct dwarf2_per_objfile *dwarf2_per_objfile
11552 = cu->per_cu->dwarf2_per_objfile;
11553 struct objfile *objfile = dwarf2_per_objfile->objfile;
11554 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11555 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11556 CORE_ADDR highpc = ((CORE_ADDR) 0);
11557 struct attribute *attr;
11558 struct die_info *child_die;
11559 CORE_ADDR baseaddr;
11560
11561 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11562
11563 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11564
11565 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11566 from finish_block. */
11567 if (lowpc == ((CORE_ADDR) -1))
11568 lowpc = highpc;
11569 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11570
11571 file_and_directory fnd = find_file_and_directory (die, cu);
11572
11573 prepare_one_comp_unit (cu, die, cu->language);
11574
11575 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11576 standardised yet. As a workaround for the language detection we fall
11577 back to the DW_AT_producer string. */
11578 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11579 cu->language = language_opencl;
11580
11581 /* Similar hack for Go. */
11582 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11583 set_cu_language (DW_LANG_Go, cu);
11584
11585 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11586
11587 /* Decode line number information if present. We do this before
11588 processing child DIEs, so that the line header table is available
11589 for DW_AT_decl_file. */
11590 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11591
11592 /* Process all dies in compilation unit. */
11593 if (die->child != NULL)
11594 {
11595 child_die = die->child;
11596 while (child_die && child_die->tag)
11597 {
11598 process_die (child_die, cu);
11599 child_die = sibling_die (child_die);
11600 }
11601 }
11602
11603 /* Decode macro information, if present. Dwarf 2 macro information
11604 refers to information in the line number info statement program
11605 header, so we can only read it if we've read the header
11606 successfully. */
11607 attr = dwarf2_attr (die, DW_AT_macros, cu);
11608 if (attr == NULL)
11609 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11610 if (attr && cu->line_header)
11611 {
11612 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11613 complaint (&symfile_complaints,
11614 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11615
11616 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11617 }
11618 else
11619 {
11620 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11621 if (attr && cu->line_header)
11622 {
11623 unsigned int macro_offset = DW_UNSND (attr);
11624
11625 dwarf_decode_macros (cu, macro_offset, 0);
11626 }
11627 }
11628 }
11629
11630 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11631 Create the set of symtabs used by this TU, or if this TU is sharing
11632 symtabs with another TU and the symtabs have already been created
11633 then restore those symtabs in the line header.
11634 We don't need the pc/line-number mapping for type units. */
11635
11636 static void
11637 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11638 {
11639 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11640 struct type_unit_group *tu_group;
11641 int first_time;
11642 struct attribute *attr;
11643 unsigned int i;
11644 struct signatured_type *sig_type;
11645
11646 gdb_assert (per_cu->is_debug_types);
11647 sig_type = (struct signatured_type *) per_cu;
11648
11649 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11650
11651 /* If we're using .gdb_index (includes -readnow) then
11652 per_cu->type_unit_group may not have been set up yet. */
11653 if (sig_type->type_unit_group == NULL)
11654 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11655 tu_group = sig_type->type_unit_group;
11656
11657 /* If we've already processed this stmt_list there's no real need to
11658 do it again, we could fake it and just recreate the part we need
11659 (file name,index -> symtab mapping). If data shows this optimization
11660 is useful we can do it then. */
11661 first_time = tu_group->compunit_symtab == NULL;
11662
11663 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11664 debug info. */
11665 line_header_up lh;
11666 if (attr != NULL)
11667 {
11668 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11669 lh = dwarf_decode_line_header (line_offset, cu);
11670 }
11671 if (lh == NULL)
11672 {
11673 if (first_time)
11674 dwarf2_start_symtab (cu, "", NULL, 0);
11675 else
11676 {
11677 gdb_assert (tu_group->symtabs == NULL);
11678 restart_symtab (tu_group->compunit_symtab, "", 0);
11679 }
11680 return;
11681 }
11682
11683 cu->line_header = lh.release ();
11684 cu->line_header_die_owner = die;
11685
11686 if (first_time)
11687 {
11688 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11689
11690 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11691 still initializing it, and our caller (a few levels up)
11692 process_full_type_unit still needs to know if this is the first
11693 time. */
11694
11695 tu_group->num_symtabs = cu->line_header->file_names.size ();
11696 tu_group->symtabs = XNEWVEC (struct symtab *,
11697 cu->line_header->file_names.size ());
11698
11699 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11700 {
11701 file_entry &fe = cu->line_header->file_names[i];
11702
11703 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11704
11705 if (current_subfile->symtab == NULL)
11706 {
11707 /* NOTE: start_subfile will recognize when it's been
11708 passed a file it has already seen. So we can't
11709 assume there's a simple mapping from
11710 cu->line_header->file_names to subfiles, plus
11711 cu->line_header->file_names may contain dups. */
11712 current_subfile->symtab
11713 = allocate_symtab (cust, current_subfile->name);
11714 }
11715
11716 fe.symtab = current_subfile->symtab;
11717 tu_group->symtabs[i] = fe.symtab;
11718 }
11719 }
11720 else
11721 {
11722 restart_symtab (tu_group->compunit_symtab, "", 0);
11723
11724 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11725 {
11726 file_entry &fe = cu->line_header->file_names[i];
11727
11728 fe.symtab = tu_group->symtabs[i];
11729 }
11730 }
11731
11732 /* The main symtab is allocated last. Type units don't have DW_AT_name
11733 so they don't have a "real" (so to speak) symtab anyway.
11734 There is later code that will assign the main symtab to all symbols
11735 that don't have one. We need to handle the case of a symbol with a
11736 missing symtab (DW_AT_decl_file) anyway. */
11737 }
11738
11739 /* Process DW_TAG_type_unit.
11740 For TUs we want to skip the first top level sibling if it's not the
11741 actual type being defined by this TU. In this case the first top
11742 level sibling is there to provide context only. */
11743
11744 static void
11745 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11746 {
11747 struct die_info *child_die;
11748
11749 prepare_one_comp_unit (cu, die, language_minimal);
11750
11751 /* Initialize (or reinitialize) the machinery for building symtabs.
11752 We do this before processing child DIEs, so that the line header table
11753 is available for DW_AT_decl_file. */
11754 setup_type_unit_groups (die, cu);
11755
11756 if (die->child != NULL)
11757 {
11758 child_die = die->child;
11759 while (child_die && child_die->tag)
11760 {
11761 process_die (child_die, cu);
11762 child_die = sibling_die (child_die);
11763 }
11764 }
11765 }
11766 \f
11767 /* DWO/DWP files.
11768
11769 http://gcc.gnu.org/wiki/DebugFission
11770 http://gcc.gnu.org/wiki/DebugFissionDWP
11771
11772 To simplify handling of both DWO files ("object" files with the DWARF info)
11773 and DWP files (a file with the DWOs packaged up into one file), we treat
11774 DWP files as having a collection of virtual DWO files. */
11775
11776 static hashval_t
11777 hash_dwo_file (const void *item)
11778 {
11779 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11780 hashval_t hash;
11781
11782 hash = htab_hash_string (dwo_file->dwo_name);
11783 if (dwo_file->comp_dir != NULL)
11784 hash += htab_hash_string (dwo_file->comp_dir);
11785 return hash;
11786 }
11787
11788 static int
11789 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11790 {
11791 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11792 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11793
11794 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11795 return 0;
11796 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11797 return lhs->comp_dir == rhs->comp_dir;
11798 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11799 }
11800
11801 /* Allocate a hash table for DWO files. */
11802
11803 static htab_t
11804 allocate_dwo_file_hash_table (struct objfile *objfile)
11805 {
11806 return htab_create_alloc_ex (41,
11807 hash_dwo_file,
11808 eq_dwo_file,
11809 NULL,
11810 &objfile->objfile_obstack,
11811 hashtab_obstack_allocate,
11812 dummy_obstack_deallocate);
11813 }
11814
11815 /* Lookup DWO file DWO_NAME. */
11816
11817 static void **
11818 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11819 const char *dwo_name,
11820 const char *comp_dir)
11821 {
11822 struct dwo_file find_entry;
11823 void **slot;
11824
11825 if (dwarf2_per_objfile->dwo_files == NULL)
11826 dwarf2_per_objfile->dwo_files
11827 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11828
11829 memset (&find_entry, 0, sizeof (find_entry));
11830 find_entry.dwo_name = dwo_name;
11831 find_entry.comp_dir = comp_dir;
11832 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11833
11834 return slot;
11835 }
11836
11837 static hashval_t
11838 hash_dwo_unit (const void *item)
11839 {
11840 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11841
11842 /* This drops the top 32 bits of the id, but is ok for a hash. */
11843 return dwo_unit->signature;
11844 }
11845
11846 static int
11847 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11848 {
11849 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11850 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11851
11852 /* The signature is assumed to be unique within the DWO file.
11853 So while object file CU dwo_id's always have the value zero,
11854 that's OK, assuming each object file DWO file has only one CU,
11855 and that's the rule for now. */
11856 return lhs->signature == rhs->signature;
11857 }
11858
11859 /* Allocate a hash table for DWO CUs,TUs.
11860 There is one of these tables for each of CUs,TUs for each DWO file. */
11861
11862 static htab_t
11863 allocate_dwo_unit_table (struct objfile *objfile)
11864 {
11865 /* Start out with a pretty small number.
11866 Generally DWO files contain only one CU and maybe some TUs. */
11867 return htab_create_alloc_ex (3,
11868 hash_dwo_unit,
11869 eq_dwo_unit,
11870 NULL,
11871 &objfile->objfile_obstack,
11872 hashtab_obstack_allocate,
11873 dummy_obstack_deallocate);
11874 }
11875
11876 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11877
11878 struct create_dwo_cu_data
11879 {
11880 struct dwo_file *dwo_file;
11881 struct dwo_unit dwo_unit;
11882 };
11883
11884 /* die_reader_func for create_dwo_cu. */
11885
11886 static void
11887 create_dwo_cu_reader (const struct die_reader_specs *reader,
11888 const gdb_byte *info_ptr,
11889 struct die_info *comp_unit_die,
11890 int has_children,
11891 void *datap)
11892 {
11893 struct dwarf2_cu *cu = reader->cu;
11894 sect_offset sect_off = cu->per_cu->sect_off;
11895 struct dwarf2_section_info *section = cu->per_cu->section;
11896 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11897 struct dwo_file *dwo_file = data->dwo_file;
11898 struct dwo_unit *dwo_unit = &data->dwo_unit;
11899 struct attribute *attr;
11900
11901 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11902 if (attr == NULL)
11903 {
11904 complaint (&symfile_complaints,
11905 _("Dwarf Error: debug entry at offset %s is missing"
11906 " its dwo_id [in module %s]"),
11907 sect_offset_str (sect_off), dwo_file->dwo_name);
11908 return;
11909 }
11910
11911 dwo_unit->dwo_file = dwo_file;
11912 dwo_unit->signature = DW_UNSND (attr);
11913 dwo_unit->section = section;
11914 dwo_unit->sect_off = sect_off;
11915 dwo_unit->length = cu->per_cu->length;
11916
11917 if (dwarf_read_debug)
11918 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11919 sect_offset_str (sect_off),
11920 hex_string (dwo_unit->signature));
11921 }
11922
11923 /* Create the dwo_units for the CUs in a DWO_FILE.
11924 Note: This function processes DWO files only, not DWP files. */
11925
11926 static void
11927 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11928 struct dwo_file &dwo_file, dwarf2_section_info &section,
11929 htab_t &cus_htab)
11930 {
11931 struct objfile *objfile = dwarf2_per_objfile->objfile;
11932 const gdb_byte *info_ptr, *end_ptr;
11933
11934 dwarf2_read_section (objfile, &section);
11935 info_ptr = section.buffer;
11936
11937 if (info_ptr == NULL)
11938 return;
11939
11940 if (dwarf_read_debug)
11941 {
11942 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11943 get_section_name (&section),
11944 get_section_file_name (&section));
11945 }
11946
11947 end_ptr = info_ptr + section.size;
11948 while (info_ptr < end_ptr)
11949 {
11950 struct dwarf2_per_cu_data per_cu;
11951 struct create_dwo_cu_data create_dwo_cu_data;
11952 struct dwo_unit *dwo_unit;
11953 void **slot;
11954 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11955
11956 memset (&create_dwo_cu_data.dwo_unit, 0,
11957 sizeof (create_dwo_cu_data.dwo_unit));
11958 memset (&per_cu, 0, sizeof (per_cu));
11959 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11960 per_cu.is_debug_types = 0;
11961 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11962 per_cu.section = &section;
11963 create_dwo_cu_data.dwo_file = &dwo_file;
11964
11965 init_cutu_and_read_dies_no_follow (
11966 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11967 info_ptr += per_cu.length;
11968
11969 // If the unit could not be parsed, skip it.
11970 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11971 continue;
11972
11973 if (cus_htab == NULL)
11974 cus_htab = allocate_dwo_unit_table (objfile);
11975
11976 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11977 *dwo_unit = create_dwo_cu_data.dwo_unit;
11978 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11979 gdb_assert (slot != NULL);
11980 if (*slot != NULL)
11981 {
11982 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11983 sect_offset dup_sect_off = dup_cu->sect_off;
11984
11985 complaint (&symfile_complaints,
11986 _("debug cu entry at offset %s is duplicate to"
11987 " the entry at offset %s, signature %s"),
11988 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11989 hex_string (dwo_unit->signature));
11990 }
11991 *slot = (void *)dwo_unit;
11992 }
11993 }
11994
11995 /* DWP file .debug_{cu,tu}_index section format:
11996 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11997
11998 DWP Version 1:
11999
12000 Both index sections have the same format, and serve to map a 64-bit
12001 signature to a set of section numbers. Each section begins with a header,
12002 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12003 indexes, and a pool of 32-bit section numbers. The index sections will be
12004 aligned at 8-byte boundaries in the file.
12005
12006 The index section header consists of:
12007
12008 V, 32 bit version number
12009 -, 32 bits unused
12010 N, 32 bit number of compilation units or type units in the index
12011 M, 32 bit number of slots in the hash table
12012
12013 Numbers are recorded using the byte order of the application binary.
12014
12015 The hash table begins at offset 16 in the section, and consists of an array
12016 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12017 order of the application binary). Unused slots in the hash table are 0.
12018 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12019
12020 The parallel table begins immediately after the hash table
12021 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12022 array of 32-bit indexes (using the byte order of the application binary),
12023 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12024 table contains a 32-bit index into the pool of section numbers. For unused
12025 hash table slots, the corresponding entry in the parallel table will be 0.
12026
12027 The pool of section numbers begins immediately following the hash table
12028 (at offset 16 + 12 * M from the beginning of the section). The pool of
12029 section numbers consists of an array of 32-bit words (using the byte order
12030 of the application binary). Each item in the array is indexed starting
12031 from 0. The hash table entry provides the index of the first section
12032 number in the set. Additional section numbers in the set follow, and the
12033 set is terminated by a 0 entry (section number 0 is not used in ELF).
12034
12035 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12036 section must be the first entry in the set, and the .debug_abbrev.dwo must
12037 be the second entry. Other members of the set may follow in any order.
12038
12039 ---
12040
12041 DWP Version 2:
12042
12043 DWP Version 2 combines all the .debug_info, etc. sections into one,
12044 and the entries in the index tables are now offsets into these sections.
12045 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12046 section.
12047
12048 Index Section Contents:
12049 Header
12050 Hash Table of Signatures dwp_hash_table.hash_table
12051 Parallel Table of Indices dwp_hash_table.unit_table
12052 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12053 Table of Section Sizes dwp_hash_table.v2.sizes
12054
12055 The index section header consists of:
12056
12057 V, 32 bit version number
12058 L, 32 bit number of columns in the table of section offsets
12059 N, 32 bit number of compilation units or type units in the index
12060 M, 32 bit number of slots in the hash table
12061
12062 Numbers are recorded using the byte order of the application binary.
12063
12064 The hash table has the same format as version 1.
12065 The parallel table of indices has the same format as version 1,
12066 except that the entries are origin-1 indices into the table of sections
12067 offsets and the table of section sizes.
12068
12069 The table of offsets begins immediately following the parallel table
12070 (at offset 16 + 12 * M from the beginning of the section). The table is
12071 a two-dimensional array of 32-bit words (using the byte order of the
12072 application binary), with L columns and N+1 rows, in row-major order.
12073 Each row in the array is indexed starting from 0. The first row provides
12074 a key to the remaining rows: each column in this row provides an identifier
12075 for a debug section, and the offsets in the same column of subsequent rows
12076 refer to that section. The section identifiers are:
12077
12078 DW_SECT_INFO 1 .debug_info.dwo
12079 DW_SECT_TYPES 2 .debug_types.dwo
12080 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12081 DW_SECT_LINE 4 .debug_line.dwo
12082 DW_SECT_LOC 5 .debug_loc.dwo
12083 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12084 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12085 DW_SECT_MACRO 8 .debug_macro.dwo
12086
12087 The offsets provided by the CU and TU index sections are the base offsets
12088 for the contributions made by each CU or TU to the corresponding section
12089 in the package file. Each CU and TU header contains an abbrev_offset
12090 field, used to find the abbreviations table for that CU or TU within the
12091 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12092 be interpreted as relative to the base offset given in the index section.
12093 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12094 should be interpreted as relative to the base offset for .debug_line.dwo,
12095 and offsets into other debug sections obtained from DWARF attributes should
12096 also be interpreted as relative to the corresponding base offset.
12097
12098 The table of sizes begins immediately following the table of offsets.
12099 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12100 with L columns and N rows, in row-major order. Each row in the array is
12101 indexed starting from 1 (row 0 is shared by the two tables).
12102
12103 ---
12104
12105 Hash table lookup is handled the same in version 1 and 2:
12106
12107 We assume that N and M will not exceed 2^32 - 1.
12108 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12109
12110 Given a 64-bit compilation unit signature or a type signature S, an entry
12111 in the hash table is located as follows:
12112
12113 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12114 the low-order k bits all set to 1.
12115
12116 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12117
12118 3) If the hash table entry at index H matches the signature, use that
12119 entry. If the hash table entry at index H is unused (all zeroes),
12120 terminate the search: the signature is not present in the table.
12121
12122 4) Let H = (H + H') modulo M. Repeat at Step 3.
12123
12124 Because M > N and H' and M are relatively prime, the search is guaranteed
12125 to stop at an unused slot or find the match. */
12126
12127 /* Create a hash table to map DWO IDs to their CU/TU entry in
12128 .debug_{info,types}.dwo in DWP_FILE.
12129 Returns NULL if there isn't one.
12130 Note: This function processes DWP files only, not DWO files. */
12131
12132 static struct dwp_hash_table *
12133 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12134 struct dwp_file *dwp_file, int is_debug_types)
12135 {
12136 struct objfile *objfile = dwarf2_per_objfile->objfile;
12137 bfd *dbfd = dwp_file->dbfd;
12138 const gdb_byte *index_ptr, *index_end;
12139 struct dwarf2_section_info *index;
12140 uint32_t version, nr_columns, nr_units, nr_slots;
12141 struct dwp_hash_table *htab;
12142
12143 if (is_debug_types)
12144 index = &dwp_file->sections.tu_index;
12145 else
12146 index = &dwp_file->sections.cu_index;
12147
12148 if (dwarf2_section_empty_p (index))
12149 return NULL;
12150 dwarf2_read_section (objfile, index);
12151
12152 index_ptr = index->buffer;
12153 index_end = index_ptr + index->size;
12154
12155 version = read_4_bytes (dbfd, index_ptr);
12156 index_ptr += 4;
12157 if (version == 2)
12158 nr_columns = read_4_bytes (dbfd, index_ptr);
12159 else
12160 nr_columns = 0;
12161 index_ptr += 4;
12162 nr_units = read_4_bytes (dbfd, index_ptr);
12163 index_ptr += 4;
12164 nr_slots = read_4_bytes (dbfd, index_ptr);
12165 index_ptr += 4;
12166
12167 if (version != 1 && version != 2)
12168 {
12169 error (_("Dwarf Error: unsupported DWP file version (%s)"
12170 " [in module %s]"),
12171 pulongest (version), dwp_file->name);
12172 }
12173 if (nr_slots != (nr_slots & -nr_slots))
12174 {
12175 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12176 " is not power of 2 [in module %s]"),
12177 pulongest (nr_slots), dwp_file->name);
12178 }
12179
12180 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12181 htab->version = version;
12182 htab->nr_columns = nr_columns;
12183 htab->nr_units = nr_units;
12184 htab->nr_slots = nr_slots;
12185 htab->hash_table = index_ptr;
12186 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12187
12188 /* Exit early if the table is empty. */
12189 if (nr_slots == 0 || nr_units == 0
12190 || (version == 2 && nr_columns == 0))
12191 {
12192 /* All must be zero. */
12193 if (nr_slots != 0 || nr_units != 0
12194 || (version == 2 && nr_columns != 0))
12195 {
12196 complaint (&symfile_complaints,
12197 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12198 " all zero [in modules %s]"),
12199 dwp_file->name);
12200 }
12201 return htab;
12202 }
12203
12204 if (version == 1)
12205 {
12206 htab->section_pool.v1.indices =
12207 htab->unit_table + sizeof (uint32_t) * nr_slots;
12208 /* It's harder to decide whether the section is too small in v1.
12209 V1 is deprecated anyway so we punt. */
12210 }
12211 else
12212 {
12213 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12214 int *ids = htab->section_pool.v2.section_ids;
12215 /* Reverse map for error checking. */
12216 int ids_seen[DW_SECT_MAX + 1];
12217 int i;
12218
12219 if (nr_columns < 2)
12220 {
12221 error (_("Dwarf Error: bad DWP hash table, too few columns"
12222 " in section table [in module %s]"),
12223 dwp_file->name);
12224 }
12225 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12226 {
12227 error (_("Dwarf Error: bad DWP hash table, too many columns"
12228 " in section table [in module %s]"),
12229 dwp_file->name);
12230 }
12231 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12232 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12233 for (i = 0; i < nr_columns; ++i)
12234 {
12235 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12236
12237 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12238 {
12239 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12240 " in section table [in module %s]"),
12241 id, dwp_file->name);
12242 }
12243 if (ids_seen[id] != -1)
12244 {
12245 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12246 " id %d in section table [in module %s]"),
12247 id, dwp_file->name);
12248 }
12249 ids_seen[id] = i;
12250 ids[i] = id;
12251 }
12252 /* Must have exactly one info or types section. */
12253 if (((ids_seen[DW_SECT_INFO] != -1)
12254 + (ids_seen[DW_SECT_TYPES] != -1))
12255 != 1)
12256 {
12257 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12258 " DWO info/types section [in module %s]"),
12259 dwp_file->name);
12260 }
12261 /* Must have an abbrev section. */
12262 if (ids_seen[DW_SECT_ABBREV] == -1)
12263 {
12264 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12265 " section [in module %s]"),
12266 dwp_file->name);
12267 }
12268 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12269 htab->section_pool.v2.sizes =
12270 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12271 * nr_units * nr_columns);
12272 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12273 * nr_units * nr_columns))
12274 > index_end)
12275 {
12276 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12277 " [in module %s]"),
12278 dwp_file->name);
12279 }
12280 }
12281
12282 return htab;
12283 }
12284
12285 /* Update SECTIONS with the data from SECTP.
12286
12287 This function is like the other "locate" section routines that are
12288 passed to bfd_map_over_sections, but in this context the sections to
12289 read comes from the DWP V1 hash table, not the full ELF section table.
12290
12291 The result is non-zero for success, or zero if an error was found. */
12292
12293 static int
12294 locate_v1_virtual_dwo_sections (asection *sectp,
12295 struct virtual_v1_dwo_sections *sections)
12296 {
12297 const struct dwop_section_names *names = &dwop_section_names;
12298
12299 if (section_is_p (sectp->name, &names->abbrev_dwo))
12300 {
12301 /* There can be only one. */
12302 if (sections->abbrev.s.section != NULL)
12303 return 0;
12304 sections->abbrev.s.section = sectp;
12305 sections->abbrev.size = bfd_get_section_size (sectp);
12306 }
12307 else if (section_is_p (sectp->name, &names->info_dwo)
12308 || section_is_p (sectp->name, &names->types_dwo))
12309 {
12310 /* There can be only one. */
12311 if (sections->info_or_types.s.section != NULL)
12312 return 0;
12313 sections->info_or_types.s.section = sectp;
12314 sections->info_or_types.size = bfd_get_section_size (sectp);
12315 }
12316 else if (section_is_p (sectp->name, &names->line_dwo))
12317 {
12318 /* There can be only one. */
12319 if (sections->line.s.section != NULL)
12320 return 0;
12321 sections->line.s.section = sectp;
12322 sections->line.size = bfd_get_section_size (sectp);
12323 }
12324 else if (section_is_p (sectp->name, &names->loc_dwo))
12325 {
12326 /* There can be only one. */
12327 if (sections->loc.s.section != NULL)
12328 return 0;
12329 sections->loc.s.section = sectp;
12330 sections->loc.size = bfd_get_section_size (sectp);
12331 }
12332 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12333 {
12334 /* There can be only one. */
12335 if (sections->macinfo.s.section != NULL)
12336 return 0;
12337 sections->macinfo.s.section = sectp;
12338 sections->macinfo.size = bfd_get_section_size (sectp);
12339 }
12340 else if (section_is_p (sectp->name, &names->macro_dwo))
12341 {
12342 /* There can be only one. */
12343 if (sections->macro.s.section != NULL)
12344 return 0;
12345 sections->macro.s.section = sectp;
12346 sections->macro.size = bfd_get_section_size (sectp);
12347 }
12348 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12349 {
12350 /* There can be only one. */
12351 if (sections->str_offsets.s.section != NULL)
12352 return 0;
12353 sections->str_offsets.s.section = sectp;
12354 sections->str_offsets.size = bfd_get_section_size (sectp);
12355 }
12356 else
12357 {
12358 /* No other kind of section is valid. */
12359 return 0;
12360 }
12361
12362 return 1;
12363 }
12364
12365 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12366 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12367 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12368 This is for DWP version 1 files. */
12369
12370 static struct dwo_unit *
12371 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12372 struct dwp_file *dwp_file,
12373 uint32_t unit_index,
12374 const char *comp_dir,
12375 ULONGEST signature, int is_debug_types)
12376 {
12377 struct objfile *objfile = dwarf2_per_objfile->objfile;
12378 const struct dwp_hash_table *dwp_htab =
12379 is_debug_types ? dwp_file->tus : dwp_file->cus;
12380 bfd *dbfd = dwp_file->dbfd;
12381 const char *kind = is_debug_types ? "TU" : "CU";
12382 struct dwo_file *dwo_file;
12383 struct dwo_unit *dwo_unit;
12384 struct virtual_v1_dwo_sections sections;
12385 void **dwo_file_slot;
12386 int i;
12387
12388 gdb_assert (dwp_file->version == 1);
12389
12390 if (dwarf_read_debug)
12391 {
12392 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12393 kind,
12394 pulongest (unit_index), hex_string (signature),
12395 dwp_file->name);
12396 }
12397
12398 /* Fetch the sections of this DWO unit.
12399 Put a limit on the number of sections we look for so that bad data
12400 doesn't cause us to loop forever. */
12401
12402 #define MAX_NR_V1_DWO_SECTIONS \
12403 (1 /* .debug_info or .debug_types */ \
12404 + 1 /* .debug_abbrev */ \
12405 + 1 /* .debug_line */ \
12406 + 1 /* .debug_loc */ \
12407 + 1 /* .debug_str_offsets */ \
12408 + 1 /* .debug_macro or .debug_macinfo */ \
12409 + 1 /* trailing zero */)
12410
12411 memset (&sections, 0, sizeof (sections));
12412
12413 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12414 {
12415 asection *sectp;
12416 uint32_t section_nr =
12417 read_4_bytes (dbfd,
12418 dwp_htab->section_pool.v1.indices
12419 + (unit_index + i) * sizeof (uint32_t));
12420
12421 if (section_nr == 0)
12422 break;
12423 if (section_nr >= dwp_file->num_sections)
12424 {
12425 error (_("Dwarf Error: bad DWP hash table, section number too large"
12426 " [in module %s]"),
12427 dwp_file->name);
12428 }
12429
12430 sectp = dwp_file->elf_sections[section_nr];
12431 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12432 {
12433 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12434 " [in module %s]"),
12435 dwp_file->name);
12436 }
12437 }
12438
12439 if (i < 2
12440 || dwarf2_section_empty_p (&sections.info_or_types)
12441 || dwarf2_section_empty_p (&sections.abbrev))
12442 {
12443 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12444 " [in module %s]"),
12445 dwp_file->name);
12446 }
12447 if (i == MAX_NR_V1_DWO_SECTIONS)
12448 {
12449 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12450 " [in module %s]"),
12451 dwp_file->name);
12452 }
12453
12454 /* It's easier for the rest of the code if we fake a struct dwo_file and
12455 have dwo_unit "live" in that. At least for now.
12456
12457 The DWP file can be made up of a random collection of CUs and TUs.
12458 However, for each CU + set of TUs that came from the same original DWO
12459 file, we can combine them back into a virtual DWO file to save space
12460 (fewer struct dwo_file objects to allocate). Remember that for really
12461 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12462
12463 std::string virtual_dwo_name =
12464 string_printf ("virtual-dwo/%d-%d-%d-%d",
12465 get_section_id (&sections.abbrev),
12466 get_section_id (&sections.line),
12467 get_section_id (&sections.loc),
12468 get_section_id (&sections.str_offsets));
12469 /* Can we use an existing virtual DWO file? */
12470 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12471 virtual_dwo_name.c_str (),
12472 comp_dir);
12473 /* Create one if necessary. */
12474 if (*dwo_file_slot == NULL)
12475 {
12476 if (dwarf_read_debug)
12477 {
12478 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12479 virtual_dwo_name.c_str ());
12480 }
12481 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12482 dwo_file->dwo_name
12483 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12484 virtual_dwo_name.c_str (),
12485 virtual_dwo_name.size ());
12486 dwo_file->comp_dir = comp_dir;
12487 dwo_file->sections.abbrev = sections.abbrev;
12488 dwo_file->sections.line = sections.line;
12489 dwo_file->sections.loc = sections.loc;
12490 dwo_file->sections.macinfo = sections.macinfo;
12491 dwo_file->sections.macro = sections.macro;
12492 dwo_file->sections.str_offsets = sections.str_offsets;
12493 /* The "str" section is global to the entire DWP file. */
12494 dwo_file->sections.str = dwp_file->sections.str;
12495 /* The info or types section is assigned below to dwo_unit,
12496 there's no need to record it in dwo_file.
12497 Also, we can't simply record type sections in dwo_file because
12498 we record a pointer into the vector in dwo_unit. As we collect more
12499 types we'll grow the vector and eventually have to reallocate space
12500 for it, invalidating all copies of pointers into the previous
12501 contents. */
12502 *dwo_file_slot = dwo_file;
12503 }
12504 else
12505 {
12506 if (dwarf_read_debug)
12507 {
12508 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12509 virtual_dwo_name.c_str ());
12510 }
12511 dwo_file = (struct dwo_file *) *dwo_file_slot;
12512 }
12513
12514 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12515 dwo_unit->dwo_file = dwo_file;
12516 dwo_unit->signature = signature;
12517 dwo_unit->section =
12518 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12519 *dwo_unit->section = sections.info_or_types;
12520 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12521
12522 return dwo_unit;
12523 }
12524
12525 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12526 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12527 piece within that section used by a TU/CU, return a virtual section
12528 of just that piece. */
12529
12530 static struct dwarf2_section_info
12531 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12532 struct dwarf2_section_info *section,
12533 bfd_size_type offset, bfd_size_type size)
12534 {
12535 struct dwarf2_section_info result;
12536 asection *sectp;
12537
12538 gdb_assert (section != NULL);
12539 gdb_assert (!section->is_virtual);
12540
12541 memset (&result, 0, sizeof (result));
12542 result.s.containing_section = section;
12543 result.is_virtual = 1;
12544
12545 if (size == 0)
12546 return result;
12547
12548 sectp = get_section_bfd_section (section);
12549
12550 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12551 bounds of the real section. This is a pretty-rare event, so just
12552 flag an error (easier) instead of a warning and trying to cope. */
12553 if (sectp == NULL
12554 || offset + size > bfd_get_section_size (sectp))
12555 {
12556 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12557 " in section %s [in module %s]"),
12558 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12559 objfile_name (dwarf2_per_objfile->objfile));
12560 }
12561
12562 result.virtual_offset = offset;
12563 result.size = size;
12564 return result;
12565 }
12566
12567 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12568 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12569 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12570 This is for DWP version 2 files. */
12571
12572 static struct dwo_unit *
12573 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12574 struct dwp_file *dwp_file,
12575 uint32_t unit_index,
12576 const char *comp_dir,
12577 ULONGEST signature, int is_debug_types)
12578 {
12579 struct objfile *objfile = dwarf2_per_objfile->objfile;
12580 const struct dwp_hash_table *dwp_htab =
12581 is_debug_types ? dwp_file->tus : dwp_file->cus;
12582 bfd *dbfd = dwp_file->dbfd;
12583 const char *kind = is_debug_types ? "TU" : "CU";
12584 struct dwo_file *dwo_file;
12585 struct dwo_unit *dwo_unit;
12586 struct virtual_v2_dwo_sections sections;
12587 void **dwo_file_slot;
12588 int i;
12589
12590 gdb_assert (dwp_file->version == 2);
12591
12592 if (dwarf_read_debug)
12593 {
12594 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12595 kind,
12596 pulongest (unit_index), hex_string (signature),
12597 dwp_file->name);
12598 }
12599
12600 /* Fetch the section offsets of this DWO unit. */
12601
12602 memset (&sections, 0, sizeof (sections));
12603
12604 for (i = 0; i < dwp_htab->nr_columns; ++i)
12605 {
12606 uint32_t offset = read_4_bytes (dbfd,
12607 dwp_htab->section_pool.v2.offsets
12608 + (((unit_index - 1) * dwp_htab->nr_columns
12609 + i)
12610 * sizeof (uint32_t)));
12611 uint32_t size = read_4_bytes (dbfd,
12612 dwp_htab->section_pool.v2.sizes
12613 + (((unit_index - 1) * dwp_htab->nr_columns
12614 + i)
12615 * sizeof (uint32_t)));
12616
12617 switch (dwp_htab->section_pool.v2.section_ids[i])
12618 {
12619 case DW_SECT_INFO:
12620 case DW_SECT_TYPES:
12621 sections.info_or_types_offset = offset;
12622 sections.info_or_types_size = size;
12623 break;
12624 case DW_SECT_ABBREV:
12625 sections.abbrev_offset = offset;
12626 sections.abbrev_size = size;
12627 break;
12628 case DW_SECT_LINE:
12629 sections.line_offset = offset;
12630 sections.line_size = size;
12631 break;
12632 case DW_SECT_LOC:
12633 sections.loc_offset = offset;
12634 sections.loc_size = size;
12635 break;
12636 case DW_SECT_STR_OFFSETS:
12637 sections.str_offsets_offset = offset;
12638 sections.str_offsets_size = size;
12639 break;
12640 case DW_SECT_MACINFO:
12641 sections.macinfo_offset = offset;
12642 sections.macinfo_size = size;
12643 break;
12644 case DW_SECT_MACRO:
12645 sections.macro_offset = offset;
12646 sections.macro_size = size;
12647 break;
12648 }
12649 }
12650
12651 /* It's easier for the rest of the code if we fake a struct dwo_file and
12652 have dwo_unit "live" in that. At least for now.
12653
12654 The DWP file can be made up of a random collection of CUs and TUs.
12655 However, for each CU + set of TUs that came from the same original DWO
12656 file, we can combine them back into a virtual DWO file to save space
12657 (fewer struct dwo_file objects to allocate). Remember that for really
12658 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12659
12660 std::string virtual_dwo_name =
12661 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12662 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12663 (long) (sections.line_size ? sections.line_offset : 0),
12664 (long) (sections.loc_size ? sections.loc_offset : 0),
12665 (long) (sections.str_offsets_size
12666 ? sections.str_offsets_offset : 0));
12667 /* Can we use an existing virtual DWO file? */
12668 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12669 virtual_dwo_name.c_str (),
12670 comp_dir);
12671 /* Create one if necessary. */
12672 if (*dwo_file_slot == NULL)
12673 {
12674 if (dwarf_read_debug)
12675 {
12676 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12677 virtual_dwo_name.c_str ());
12678 }
12679 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12680 dwo_file->dwo_name
12681 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12682 virtual_dwo_name.c_str (),
12683 virtual_dwo_name.size ());
12684 dwo_file->comp_dir = comp_dir;
12685 dwo_file->sections.abbrev =
12686 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12687 sections.abbrev_offset, sections.abbrev_size);
12688 dwo_file->sections.line =
12689 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12690 sections.line_offset, sections.line_size);
12691 dwo_file->sections.loc =
12692 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12693 sections.loc_offset, sections.loc_size);
12694 dwo_file->sections.macinfo =
12695 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12696 sections.macinfo_offset, sections.macinfo_size);
12697 dwo_file->sections.macro =
12698 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12699 sections.macro_offset, sections.macro_size);
12700 dwo_file->sections.str_offsets =
12701 create_dwp_v2_section (dwarf2_per_objfile,
12702 &dwp_file->sections.str_offsets,
12703 sections.str_offsets_offset,
12704 sections.str_offsets_size);
12705 /* The "str" section is global to the entire DWP file. */
12706 dwo_file->sections.str = dwp_file->sections.str;
12707 /* The info or types section is assigned below to dwo_unit,
12708 there's no need to record it in dwo_file.
12709 Also, we can't simply record type sections in dwo_file because
12710 we record a pointer into the vector in dwo_unit. As we collect more
12711 types we'll grow the vector and eventually have to reallocate space
12712 for it, invalidating all copies of pointers into the previous
12713 contents. */
12714 *dwo_file_slot = dwo_file;
12715 }
12716 else
12717 {
12718 if (dwarf_read_debug)
12719 {
12720 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12721 virtual_dwo_name.c_str ());
12722 }
12723 dwo_file = (struct dwo_file *) *dwo_file_slot;
12724 }
12725
12726 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12727 dwo_unit->dwo_file = dwo_file;
12728 dwo_unit->signature = signature;
12729 dwo_unit->section =
12730 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12731 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12732 is_debug_types
12733 ? &dwp_file->sections.types
12734 : &dwp_file->sections.info,
12735 sections.info_or_types_offset,
12736 sections.info_or_types_size);
12737 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12738
12739 return dwo_unit;
12740 }
12741
12742 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12743 Returns NULL if the signature isn't found. */
12744
12745 static struct dwo_unit *
12746 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12747 struct dwp_file *dwp_file, const char *comp_dir,
12748 ULONGEST signature, int is_debug_types)
12749 {
12750 const struct dwp_hash_table *dwp_htab =
12751 is_debug_types ? dwp_file->tus : dwp_file->cus;
12752 bfd *dbfd = dwp_file->dbfd;
12753 uint32_t mask = dwp_htab->nr_slots - 1;
12754 uint32_t hash = signature & mask;
12755 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12756 unsigned int i;
12757 void **slot;
12758 struct dwo_unit find_dwo_cu;
12759
12760 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12761 find_dwo_cu.signature = signature;
12762 slot = htab_find_slot (is_debug_types
12763 ? dwp_file->loaded_tus
12764 : dwp_file->loaded_cus,
12765 &find_dwo_cu, INSERT);
12766
12767 if (*slot != NULL)
12768 return (struct dwo_unit *) *slot;
12769
12770 /* Use a for loop so that we don't loop forever on bad debug info. */
12771 for (i = 0; i < dwp_htab->nr_slots; ++i)
12772 {
12773 ULONGEST signature_in_table;
12774
12775 signature_in_table =
12776 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12777 if (signature_in_table == signature)
12778 {
12779 uint32_t unit_index =
12780 read_4_bytes (dbfd,
12781 dwp_htab->unit_table + hash * sizeof (uint32_t));
12782
12783 if (dwp_file->version == 1)
12784 {
12785 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12786 dwp_file, unit_index,
12787 comp_dir, signature,
12788 is_debug_types);
12789 }
12790 else
12791 {
12792 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12793 dwp_file, unit_index,
12794 comp_dir, signature,
12795 is_debug_types);
12796 }
12797 return (struct dwo_unit *) *slot;
12798 }
12799 if (signature_in_table == 0)
12800 return NULL;
12801 hash = (hash + hash2) & mask;
12802 }
12803
12804 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12805 " [in module %s]"),
12806 dwp_file->name);
12807 }
12808
12809 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12810 Open the file specified by FILE_NAME and hand it off to BFD for
12811 preliminary analysis. Return a newly initialized bfd *, which
12812 includes a canonicalized copy of FILE_NAME.
12813 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12814 SEARCH_CWD is true if the current directory is to be searched.
12815 It will be searched before debug-file-directory.
12816 If successful, the file is added to the bfd include table of the
12817 objfile's bfd (see gdb_bfd_record_inclusion).
12818 If unable to find/open the file, return NULL.
12819 NOTE: This function is derived from symfile_bfd_open. */
12820
12821 static gdb_bfd_ref_ptr
12822 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12823 const char *file_name, int is_dwp, int search_cwd)
12824 {
12825 int desc;
12826 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12827 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12828 to debug_file_directory. */
12829 const char *search_path;
12830 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12831
12832 gdb::unique_xmalloc_ptr<char> search_path_holder;
12833 if (search_cwd)
12834 {
12835 if (*debug_file_directory != '\0')
12836 {
12837 search_path_holder.reset (concat (".", dirname_separator_string,
12838 debug_file_directory,
12839 (char *) NULL));
12840 search_path = search_path_holder.get ();
12841 }
12842 else
12843 search_path = ".";
12844 }
12845 else
12846 search_path = debug_file_directory;
12847
12848 openp_flags flags = OPF_RETURN_REALPATH;
12849 if (is_dwp)
12850 flags |= OPF_SEARCH_IN_PATH;
12851
12852 gdb::unique_xmalloc_ptr<char> absolute_name;
12853 desc = openp (search_path, flags, file_name,
12854 O_RDONLY | O_BINARY, &absolute_name);
12855 if (desc < 0)
12856 return NULL;
12857
12858 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12859 gnutarget, desc));
12860 if (sym_bfd == NULL)
12861 return NULL;
12862 bfd_set_cacheable (sym_bfd.get (), 1);
12863
12864 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12865 return NULL;
12866
12867 /* Success. Record the bfd as having been included by the objfile's bfd.
12868 This is important because things like demangled_names_hash lives in the
12869 objfile's per_bfd space and may have references to things like symbol
12870 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12871 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12872
12873 return sym_bfd;
12874 }
12875
12876 /* Try to open DWO file FILE_NAME.
12877 COMP_DIR is the DW_AT_comp_dir attribute.
12878 The result is the bfd handle of the file.
12879 If there is a problem finding or opening the file, return NULL.
12880 Upon success, the canonicalized path of the file is stored in the bfd,
12881 same as symfile_bfd_open. */
12882
12883 static gdb_bfd_ref_ptr
12884 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12885 const char *file_name, const char *comp_dir)
12886 {
12887 if (IS_ABSOLUTE_PATH (file_name))
12888 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12889 0 /*is_dwp*/, 0 /*search_cwd*/);
12890
12891 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12892
12893 if (comp_dir != NULL)
12894 {
12895 char *path_to_try = concat (comp_dir, SLASH_STRING,
12896 file_name, (char *) NULL);
12897
12898 /* NOTE: If comp_dir is a relative path, this will also try the
12899 search path, which seems useful. */
12900 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12901 path_to_try,
12902 0 /*is_dwp*/,
12903 1 /*search_cwd*/));
12904 xfree (path_to_try);
12905 if (abfd != NULL)
12906 return abfd;
12907 }
12908
12909 /* That didn't work, try debug-file-directory, which, despite its name,
12910 is a list of paths. */
12911
12912 if (*debug_file_directory == '\0')
12913 return NULL;
12914
12915 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12916 0 /*is_dwp*/, 1 /*search_cwd*/);
12917 }
12918
12919 /* This function is mapped across the sections and remembers the offset and
12920 size of each of the DWO debugging sections we are interested in. */
12921
12922 static void
12923 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12924 {
12925 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12926 const struct dwop_section_names *names = &dwop_section_names;
12927
12928 if (section_is_p (sectp->name, &names->abbrev_dwo))
12929 {
12930 dwo_sections->abbrev.s.section = sectp;
12931 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12932 }
12933 else if (section_is_p (sectp->name, &names->info_dwo))
12934 {
12935 dwo_sections->info.s.section = sectp;
12936 dwo_sections->info.size = bfd_get_section_size (sectp);
12937 }
12938 else if (section_is_p (sectp->name, &names->line_dwo))
12939 {
12940 dwo_sections->line.s.section = sectp;
12941 dwo_sections->line.size = bfd_get_section_size (sectp);
12942 }
12943 else if (section_is_p (sectp->name, &names->loc_dwo))
12944 {
12945 dwo_sections->loc.s.section = sectp;
12946 dwo_sections->loc.size = bfd_get_section_size (sectp);
12947 }
12948 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12949 {
12950 dwo_sections->macinfo.s.section = sectp;
12951 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12952 }
12953 else if (section_is_p (sectp->name, &names->macro_dwo))
12954 {
12955 dwo_sections->macro.s.section = sectp;
12956 dwo_sections->macro.size = bfd_get_section_size (sectp);
12957 }
12958 else if (section_is_p (sectp->name, &names->str_dwo))
12959 {
12960 dwo_sections->str.s.section = sectp;
12961 dwo_sections->str.size = bfd_get_section_size (sectp);
12962 }
12963 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12964 {
12965 dwo_sections->str_offsets.s.section = sectp;
12966 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12967 }
12968 else if (section_is_p (sectp->name, &names->types_dwo))
12969 {
12970 struct dwarf2_section_info type_section;
12971
12972 memset (&type_section, 0, sizeof (type_section));
12973 type_section.s.section = sectp;
12974 type_section.size = bfd_get_section_size (sectp);
12975 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12976 &type_section);
12977 }
12978 }
12979
12980 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12981 by PER_CU. This is for the non-DWP case.
12982 The result is NULL if DWO_NAME can't be found. */
12983
12984 static struct dwo_file *
12985 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12986 const char *dwo_name, const char *comp_dir)
12987 {
12988 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12989 struct objfile *objfile = dwarf2_per_objfile->objfile;
12990
12991 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
12992 if (dbfd == NULL)
12993 {
12994 if (dwarf_read_debug)
12995 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12996 return NULL;
12997 }
12998
12999 /* We use a unique pointer here, despite the obstack allocation,
13000 because a dwo_file needs some cleanup if it is abandoned. */
13001 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
13002 struct dwo_file));
13003 dwo_file->dwo_name = dwo_name;
13004 dwo_file->comp_dir = comp_dir;
13005 dwo_file->dbfd = dbfd.release ();
13006
13007 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13008 &dwo_file->sections);
13009
13010 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13011 dwo_file->cus);
13012
13013 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13014 dwo_file->sections.types, dwo_file->tus);
13015
13016 if (dwarf_read_debug)
13017 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13018
13019 return dwo_file.release ();
13020 }
13021
13022 /* This function is mapped across the sections and remembers the offset and
13023 size of each of the DWP debugging sections common to version 1 and 2 that
13024 we are interested in. */
13025
13026 static void
13027 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13028 void *dwp_file_ptr)
13029 {
13030 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13031 const struct dwop_section_names *names = &dwop_section_names;
13032 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13033
13034 /* Record the ELF section number for later lookup: this is what the
13035 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13036 gdb_assert (elf_section_nr < dwp_file->num_sections);
13037 dwp_file->elf_sections[elf_section_nr] = sectp;
13038
13039 /* Look for specific sections that we need. */
13040 if (section_is_p (sectp->name, &names->str_dwo))
13041 {
13042 dwp_file->sections.str.s.section = sectp;
13043 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13044 }
13045 else if (section_is_p (sectp->name, &names->cu_index))
13046 {
13047 dwp_file->sections.cu_index.s.section = sectp;
13048 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13049 }
13050 else if (section_is_p (sectp->name, &names->tu_index))
13051 {
13052 dwp_file->sections.tu_index.s.section = sectp;
13053 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13054 }
13055 }
13056
13057 /* This function is mapped across the sections and remembers the offset and
13058 size of each of the DWP version 2 debugging sections that we are interested
13059 in. This is split into a separate function because we don't know if we
13060 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13061
13062 static void
13063 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13064 {
13065 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13066 const struct dwop_section_names *names = &dwop_section_names;
13067 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13068
13069 /* Record the ELF section number for later lookup: this is what the
13070 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13071 gdb_assert (elf_section_nr < dwp_file->num_sections);
13072 dwp_file->elf_sections[elf_section_nr] = sectp;
13073
13074 /* Look for specific sections that we need. */
13075 if (section_is_p (sectp->name, &names->abbrev_dwo))
13076 {
13077 dwp_file->sections.abbrev.s.section = sectp;
13078 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13079 }
13080 else if (section_is_p (sectp->name, &names->info_dwo))
13081 {
13082 dwp_file->sections.info.s.section = sectp;
13083 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13084 }
13085 else if (section_is_p (sectp->name, &names->line_dwo))
13086 {
13087 dwp_file->sections.line.s.section = sectp;
13088 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13089 }
13090 else if (section_is_p (sectp->name, &names->loc_dwo))
13091 {
13092 dwp_file->sections.loc.s.section = sectp;
13093 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13094 }
13095 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13096 {
13097 dwp_file->sections.macinfo.s.section = sectp;
13098 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13099 }
13100 else if (section_is_p (sectp->name, &names->macro_dwo))
13101 {
13102 dwp_file->sections.macro.s.section = sectp;
13103 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13104 }
13105 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13106 {
13107 dwp_file->sections.str_offsets.s.section = sectp;
13108 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13109 }
13110 else if (section_is_p (sectp->name, &names->types_dwo))
13111 {
13112 dwp_file->sections.types.s.section = sectp;
13113 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13114 }
13115 }
13116
13117 /* Hash function for dwp_file loaded CUs/TUs. */
13118
13119 static hashval_t
13120 hash_dwp_loaded_cutus (const void *item)
13121 {
13122 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13123
13124 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13125 return dwo_unit->signature;
13126 }
13127
13128 /* Equality function for dwp_file loaded CUs/TUs. */
13129
13130 static int
13131 eq_dwp_loaded_cutus (const void *a, const void *b)
13132 {
13133 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13134 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13135
13136 return dua->signature == dub->signature;
13137 }
13138
13139 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13140
13141 static htab_t
13142 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13143 {
13144 return htab_create_alloc_ex (3,
13145 hash_dwp_loaded_cutus,
13146 eq_dwp_loaded_cutus,
13147 NULL,
13148 &objfile->objfile_obstack,
13149 hashtab_obstack_allocate,
13150 dummy_obstack_deallocate);
13151 }
13152
13153 /* Try to open DWP file FILE_NAME.
13154 The result is the bfd handle of the file.
13155 If there is a problem finding or opening the file, return NULL.
13156 Upon success, the canonicalized path of the file is stored in the bfd,
13157 same as symfile_bfd_open. */
13158
13159 static gdb_bfd_ref_ptr
13160 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13161 const char *file_name)
13162 {
13163 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13164 1 /*is_dwp*/,
13165 1 /*search_cwd*/));
13166 if (abfd != NULL)
13167 return abfd;
13168
13169 /* Work around upstream bug 15652.
13170 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13171 [Whether that's a "bug" is debatable, but it is getting in our way.]
13172 We have no real idea where the dwp file is, because gdb's realpath-ing
13173 of the executable's path may have discarded the needed info.
13174 [IWBN if the dwp file name was recorded in the executable, akin to
13175 .gnu_debuglink, but that doesn't exist yet.]
13176 Strip the directory from FILE_NAME and search again. */
13177 if (*debug_file_directory != '\0')
13178 {
13179 /* Don't implicitly search the current directory here.
13180 If the user wants to search "." to handle this case,
13181 it must be added to debug-file-directory. */
13182 return try_open_dwop_file (dwarf2_per_objfile,
13183 lbasename (file_name), 1 /*is_dwp*/,
13184 0 /*search_cwd*/);
13185 }
13186
13187 return NULL;
13188 }
13189
13190 /* Initialize the use of the DWP file for the current objfile.
13191 By convention the name of the DWP file is ${objfile}.dwp.
13192 The result is NULL if it can't be found. */
13193
13194 static struct dwp_file *
13195 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13196 {
13197 struct objfile *objfile = dwarf2_per_objfile->objfile;
13198 struct dwp_file *dwp_file;
13199
13200 /* Try to find first .dwp for the binary file before any symbolic links
13201 resolving. */
13202
13203 /* If the objfile is a debug file, find the name of the real binary
13204 file and get the name of dwp file from there. */
13205 std::string dwp_name;
13206 if (objfile->separate_debug_objfile_backlink != NULL)
13207 {
13208 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13209 const char *backlink_basename = lbasename (backlink->original_name);
13210
13211 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13212 }
13213 else
13214 dwp_name = objfile->original_name;
13215
13216 dwp_name += ".dwp";
13217
13218 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13219 if (dbfd == NULL
13220 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13221 {
13222 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13223 dwp_name = objfile_name (objfile);
13224 dwp_name += ".dwp";
13225 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13226 }
13227
13228 if (dbfd == NULL)
13229 {
13230 if (dwarf_read_debug)
13231 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13232 return NULL;
13233 }
13234 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13235 dwp_file->name = bfd_get_filename (dbfd.get ());
13236 dwp_file->dbfd = dbfd.release ();
13237
13238 /* +1: section 0 is unused */
13239 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13240 dwp_file->elf_sections =
13241 OBSTACK_CALLOC (&objfile->objfile_obstack,
13242 dwp_file->num_sections, asection *);
13243
13244 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13245 dwp_file);
13246
13247 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13248
13249 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13250
13251 /* The DWP file version is stored in the hash table. Oh well. */
13252 if (dwp_file->cus && dwp_file->tus
13253 && dwp_file->cus->version != dwp_file->tus->version)
13254 {
13255 /* Technically speaking, we should try to limp along, but this is
13256 pretty bizarre. We use pulongest here because that's the established
13257 portability solution (e.g, we cannot use %u for uint32_t). */
13258 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13259 " TU version %s [in DWP file %s]"),
13260 pulongest (dwp_file->cus->version),
13261 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13262 }
13263
13264 if (dwp_file->cus)
13265 dwp_file->version = dwp_file->cus->version;
13266 else if (dwp_file->tus)
13267 dwp_file->version = dwp_file->tus->version;
13268 else
13269 dwp_file->version = 2;
13270
13271 if (dwp_file->version == 2)
13272 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13273 dwp_file);
13274
13275 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13276 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13277
13278 if (dwarf_read_debug)
13279 {
13280 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13281 fprintf_unfiltered (gdb_stdlog,
13282 " %s CUs, %s TUs\n",
13283 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13284 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13285 }
13286
13287 return dwp_file;
13288 }
13289
13290 /* Wrapper around open_and_init_dwp_file, only open it once. */
13291
13292 static struct dwp_file *
13293 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13294 {
13295 if (! dwarf2_per_objfile->dwp_checked)
13296 {
13297 dwarf2_per_objfile->dwp_file
13298 = open_and_init_dwp_file (dwarf2_per_objfile);
13299 dwarf2_per_objfile->dwp_checked = 1;
13300 }
13301 return dwarf2_per_objfile->dwp_file;
13302 }
13303
13304 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13305 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13306 or in the DWP file for the objfile, referenced by THIS_UNIT.
13307 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13308 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13309
13310 This is called, for example, when wanting to read a variable with a
13311 complex location. Therefore we don't want to do file i/o for every call.
13312 Therefore we don't want to look for a DWO file on every call.
13313 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13314 then we check if we've already seen DWO_NAME, and only THEN do we check
13315 for a DWO file.
13316
13317 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13318 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13319
13320 static struct dwo_unit *
13321 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13322 const char *dwo_name, const char *comp_dir,
13323 ULONGEST signature, int is_debug_types)
13324 {
13325 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13326 struct objfile *objfile = dwarf2_per_objfile->objfile;
13327 const char *kind = is_debug_types ? "TU" : "CU";
13328 void **dwo_file_slot;
13329 struct dwo_file *dwo_file;
13330 struct dwp_file *dwp_file;
13331
13332 /* First see if there's a DWP file.
13333 If we have a DWP file but didn't find the DWO inside it, don't
13334 look for the original DWO file. It makes gdb behave differently
13335 depending on whether one is debugging in the build tree. */
13336
13337 dwp_file = get_dwp_file (dwarf2_per_objfile);
13338 if (dwp_file != NULL)
13339 {
13340 const struct dwp_hash_table *dwp_htab =
13341 is_debug_types ? dwp_file->tus : dwp_file->cus;
13342
13343 if (dwp_htab != NULL)
13344 {
13345 struct dwo_unit *dwo_cutu =
13346 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13347 signature, is_debug_types);
13348
13349 if (dwo_cutu != NULL)
13350 {
13351 if (dwarf_read_debug)
13352 {
13353 fprintf_unfiltered (gdb_stdlog,
13354 "Virtual DWO %s %s found: @%s\n",
13355 kind, hex_string (signature),
13356 host_address_to_string (dwo_cutu));
13357 }
13358 return dwo_cutu;
13359 }
13360 }
13361 }
13362 else
13363 {
13364 /* No DWP file, look for the DWO file. */
13365
13366 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13367 dwo_name, comp_dir);
13368 if (*dwo_file_slot == NULL)
13369 {
13370 /* Read in the file and build a table of the CUs/TUs it contains. */
13371 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13372 }
13373 /* NOTE: This will be NULL if unable to open the file. */
13374 dwo_file = (struct dwo_file *) *dwo_file_slot;
13375
13376 if (dwo_file != NULL)
13377 {
13378 struct dwo_unit *dwo_cutu = NULL;
13379
13380 if (is_debug_types && dwo_file->tus)
13381 {
13382 struct dwo_unit find_dwo_cutu;
13383
13384 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13385 find_dwo_cutu.signature = signature;
13386 dwo_cutu
13387 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13388 }
13389 else if (!is_debug_types && dwo_file->cus)
13390 {
13391 struct dwo_unit find_dwo_cutu;
13392
13393 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13394 find_dwo_cutu.signature = signature;
13395 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13396 &find_dwo_cutu);
13397 }
13398
13399 if (dwo_cutu != NULL)
13400 {
13401 if (dwarf_read_debug)
13402 {
13403 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13404 kind, dwo_name, hex_string (signature),
13405 host_address_to_string (dwo_cutu));
13406 }
13407 return dwo_cutu;
13408 }
13409 }
13410 }
13411
13412 /* We didn't find it. This could mean a dwo_id mismatch, or
13413 someone deleted the DWO/DWP file, or the search path isn't set up
13414 correctly to find the file. */
13415
13416 if (dwarf_read_debug)
13417 {
13418 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13419 kind, dwo_name, hex_string (signature));
13420 }
13421
13422 /* This is a warning and not a complaint because it can be caused by
13423 pilot error (e.g., user accidentally deleting the DWO). */
13424 {
13425 /* Print the name of the DWP file if we looked there, helps the user
13426 better diagnose the problem. */
13427 std::string dwp_text;
13428
13429 if (dwp_file != NULL)
13430 dwp_text = string_printf (" [in DWP file %s]",
13431 lbasename (dwp_file->name));
13432
13433 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13434 " [in module %s]"),
13435 kind, dwo_name, hex_string (signature),
13436 dwp_text.c_str (),
13437 this_unit->is_debug_types ? "TU" : "CU",
13438 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13439 }
13440 return NULL;
13441 }
13442
13443 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13444 See lookup_dwo_cutu_unit for details. */
13445
13446 static struct dwo_unit *
13447 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13448 const char *dwo_name, const char *comp_dir,
13449 ULONGEST signature)
13450 {
13451 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13452 }
13453
13454 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13455 See lookup_dwo_cutu_unit for details. */
13456
13457 static struct dwo_unit *
13458 lookup_dwo_type_unit (struct signatured_type *this_tu,
13459 const char *dwo_name, const char *comp_dir)
13460 {
13461 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13462 }
13463
13464 /* Traversal function for queue_and_load_all_dwo_tus. */
13465
13466 static int
13467 queue_and_load_dwo_tu (void **slot, void *info)
13468 {
13469 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13470 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13471 ULONGEST signature = dwo_unit->signature;
13472 struct signatured_type *sig_type =
13473 lookup_dwo_signatured_type (per_cu->cu, signature);
13474
13475 if (sig_type != NULL)
13476 {
13477 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13478
13479 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13480 a real dependency of PER_CU on SIG_TYPE. That is detected later
13481 while processing PER_CU. */
13482 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13483 load_full_type_unit (sig_cu);
13484 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13485 }
13486
13487 return 1;
13488 }
13489
13490 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13491 The DWO may have the only definition of the type, though it may not be
13492 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13493 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13494
13495 static void
13496 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13497 {
13498 struct dwo_unit *dwo_unit;
13499 struct dwo_file *dwo_file;
13500
13501 gdb_assert (!per_cu->is_debug_types);
13502 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13503 gdb_assert (per_cu->cu != NULL);
13504
13505 dwo_unit = per_cu->cu->dwo_unit;
13506 gdb_assert (dwo_unit != NULL);
13507
13508 dwo_file = dwo_unit->dwo_file;
13509 if (dwo_file->tus != NULL)
13510 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13511 }
13512
13513 /* Free all resources associated with DWO_FILE.
13514 Close the DWO file and munmap the sections. */
13515
13516 static void
13517 free_dwo_file (struct dwo_file *dwo_file)
13518 {
13519 /* Note: dbfd is NULL for virtual DWO files. */
13520 gdb_bfd_unref (dwo_file->dbfd);
13521
13522 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13523 }
13524
13525 /* Traversal function for free_dwo_files. */
13526
13527 static int
13528 free_dwo_file_from_slot (void **slot, void *info)
13529 {
13530 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13531
13532 free_dwo_file (dwo_file);
13533
13534 return 1;
13535 }
13536
13537 /* Free all resources associated with DWO_FILES. */
13538
13539 static void
13540 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13541 {
13542 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13543 }
13544 \f
13545 /* Read in various DIEs. */
13546
13547 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13548 Inherit only the children of the DW_AT_abstract_origin DIE not being
13549 already referenced by DW_AT_abstract_origin from the children of the
13550 current DIE. */
13551
13552 static void
13553 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13554 {
13555 struct die_info *child_die;
13556 sect_offset *offsetp;
13557 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13558 struct die_info *origin_die;
13559 /* Iterator of the ORIGIN_DIE children. */
13560 struct die_info *origin_child_die;
13561 struct attribute *attr;
13562 struct dwarf2_cu *origin_cu;
13563 struct pending **origin_previous_list_in_scope;
13564
13565 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13566 if (!attr)
13567 return;
13568
13569 /* Note that following die references may follow to a die in a
13570 different cu. */
13571
13572 origin_cu = cu;
13573 origin_die = follow_die_ref (die, attr, &origin_cu);
13574
13575 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13576 symbols in. */
13577 origin_previous_list_in_scope = origin_cu->list_in_scope;
13578 origin_cu->list_in_scope = cu->list_in_scope;
13579
13580 if (die->tag != origin_die->tag
13581 && !(die->tag == DW_TAG_inlined_subroutine
13582 && origin_die->tag == DW_TAG_subprogram))
13583 complaint (&symfile_complaints,
13584 _("DIE %s and its abstract origin %s have different tags"),
13585 sect_offset_str (die->sect_off),
13586 sect_offset_str (origin_die->sect_off));
13587
13588 std::vector<sect_offset> offsets;
13589
13590 for (child_die = die->child;
13591 child_die && child_die->tag;
13592 child_die = sibling_die (child_die))
13593 {
13594 struct die_info *child_origin_die;
13595 struct dwarf2_cu *child_origin_cu;
13596
13597 /* We are trying to process concrete instance entries:
13598 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13599 it's not relevant to our analysis here. i.e. detecting DIEs that are
13600 present in the abstract instance but not referenced in the concrete
13601 one. */
13602 if (child_die->tag == DW_TAG_call_site
13603 || child_die->tag == DW_TAG_GNU_call_site)
13604 continue;
13605
13606 /* For each CHILD_DIE, find the corresponding child of
13607 ORIGIN_DIE. If there is more than one layer of
13608 DW_AT_abstract_origin, follow them all; there shouldn't be,
13609 but GCC versions at least through 4.4 generate this (GCC PR
13610 40573). */
13611 child_origin_die = child_die;
13612 child_origin_cu = cu;
13613 while (1)
13614 {
13615 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13616 child_origin_cu);
13617 if (attr == NULL)
13618 break;
13619 child_origin_die = follow_die_ref (child_origin_die, attr,
13620 &child_origin_cu);
13621 }
13622
13623 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13624 counterpart may exist. */
13625 if (child_origin_die != child_die)
13626 {
13627 if (child_die->tag != child_origin_die->tag
13628 && !(child_die->tag == DW_TAG_inlined_subroutine
13629 && child_origin_die->tag == DW_TAG_subprogram))
13630 complaint (&symfile_complaints,
13631 _("Child DIE %s and its abstract origin %s have "
13632 "different tags"),
13633 sect_offset_str (child_die->sect_off),
13634 sect_offset_str (child_origin_die->sect_off));
13635 if (child_origin_die->parent != origin_die)
13636 complaint (&symfile_complaints,
13637 _("Child DIE %s and its abstract origin %s have "
13638 "different parents"),
13639 sect_offset_str (child_die->sect_off),
13640 sect_offset_str (child_origin_die->sect_off));
13641 else
13642 offsets.push_back (child_origin_die->sect_off);
13643 }
13644 }
13645 std::sort (offsets.begin (), offsets.end ());
13646 sect_offset *offsets_end = offsets.data () + offsets.size ();
13647 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13648 if (offsetp[-1] == *offsetp)
13649 complaint (&symfile_complaints,
13650 _("Multiple children of DIE %s refer "
13651 "to DIE %s as their abstract origin"),
13652 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13653
13654 offsetp = offsets.data ();
13655 origin_child_die = origin_die->child;
13656 while (origin_child_die && origin_child_die->tag)
13657 {
13658 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13659 while (offsetp < offsets_end
13660 && *offsetp < origin_child_die->sect_off)
13661 offsetp++;
13662 if (offsetp >= offsets_end
13663 || *offsetp > origin_child_die->sect_off)
13664 {
13665 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13666 Check whether we're already processing ORIGIN_CHILD_DIE.
13667 This can happen with mutually referenced abstract_origins.
13668 PR 16581. */
13669 if (!origin_child_die->in_process)
13670 process_die (origin_child_die, origin_cu);
13671 }
13672 origin_child_die = sibling_die (origin_child_die);
13673 }
13674 origin_cu->list_in_scope = origin_previous_list_in_scope;
13675 }
13676
13677 static void
13678 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13679 {
13680 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13681 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13682 struct context_stack *newobj;
13683 CORE_ADDR lowpc;
13684 CORE_ADDR highpc;
13685 struct die_info *child_die;
13686 struct attribute *attr, *call_line, *call_file;
13687 const char *name;
13688 CORE_ADDR baseaddr;
13689 struct block *block;
13690 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13691 std::vector<struct symbol *> template_args;
13692 struct template_symbol *templ_func = NULL;
13693
13694 if (inlined_func)
13695 {
13696 /* If we do not have call site information, we can't show the
13697 caller of this inlined function. That's too confusing, so
13698 only use the scope for local variables. */
13699 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13700 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13701 if (call_line == NULL || call_file == NULL)
13702 {
13703 read_lexical_block_scope (die, cu);
13704 return;
13705 }
13706 }
13707
13708 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13709
13710 name = dwarf2_name (die, cu);
13711
13712 /* Ignore functions with missing or empty names. These are actually
13713 illegal according to the DWARF standard. */
13714 if (name == NULL)
13715 {
13716 complaint (&symfile_complaints,
13717 _("missing name for subprogram DIE at %s"),
13718 sect_offset_str (die->sect_off));
13719 return;
13720 }
13721
13722 /* Ignore functions with missing or invalid low and high pc attributes. */
13723 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13724 <= PC_BOUNDS_INVALID)
13725 {
13726 attr = dwarf2_attr (die, DW_AT_external, cu);
13727 if (!attr || !DW_UNSND (attr))
13728 complaint (&symfile_complaints,
13729 _("cannot get low and high bounds "
13730 "for subprogram DIE at %s"),
13731 sect_offset_str (die->sect_off));
13732 return;
13733 }
13734
13735 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13736 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13737
13738 /* If we have any template arguments, then we must allocate a
13739 different sort of symbol. */
13740 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13741 {
13742 if (child_die->tag == DW_TAG_template_type_param
13743 || child_die->tag == DW_TAG_template_value_param)
13744 {
13745 templ_func = allocate_template_symbol (objfile);
13746 templ_func->subclass = SYMBOL_TEMPLATE;
13747 break;
13748 }
13749 }
13750
13751 newobj = push_context (0, lowpc);
13752 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13753 (struct symbol *) templ_func);
13754
13755 /* If there is a location expression for DW_AT_frame_base, record
13756 it. */
13757 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13758 if (attr)
13759 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13760
13761 /* If there is a location for the static link, record it. */
13762 newobj->static_link = NULL;
13763 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13764 if (attr)
13765 {
13766 newobj->static_link
13767 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13768 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13769 }
13770
13771 cu->list_in_scope = &local_symbols;
13772
13773 if (die->child != NULL)
13774 {
13775 child_die = die->child;
13776 while (child_die && child_die->tag)
13777 {
13778 if (child_die->tag == DW_TAG_template_type_param
13779 || child_die->tag == DW_TAG_template_value_param)
13780 {
13781 struct symbol *arg = new_symbol (child_die, NULL, cu);
13782
13783 if (arg != NULL)
13784 template_args.push_back (arg);
13785 }
13786 else
13787 process_die (child_die, cu);
13788 child_die = sibling_die (child_die);
13789 }
13790 }
13791
13792 inherit_abstract_dies (die, cu);
13793
13794 /* If we have a DW_AT_specification, we might need to import using
13795 directives from the context of the specification DIE. See the
13796 comment in determine_prefix. */
13797 if (cu->language == language_cplus
13798 && dwarf2_attr (die, DW_AT_specification, cu))
13799 {
13800 struct dwarf2_cu *spec_cu = cu;
13801 struct die_info *spec_die = die_specification (die, &spec_cu);
13802
13803 while (spec_die)
13804 {
13805 child_die = spec_die->child;
13806 while (child_die && child_die->tag)
13807 {
13808 if (child_die->tag == DW_TAG_imported_module)
13809 process_die (child_die, spec_cu);
13810 child_die = sibling_die (child_die);
13811 }
13812
13813 /* In some cases, GCC generates specification DIEs that
13814 themselves contain DW_AT_specification attributes. */
13815 spec_die = die_specification (spec_die, &spec_cu);
13816 }
13817 }
13818
13819 newobj = pop_context ();
13820 /* Make a block for the local symbols within. */
13821 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13822 newobj->static_link, lowpc, highpc);
13823
13824 /* For C++, set the block's scope. */
13825 if ((cu->language == language_cplus
13826 || cu->language == language_fortran
13827 || cu->language == language_d
13828 || cu->language == language_rust)
13829 && cu->processing_has_namespace_info)
13830 block_set_scope (block, determine_prefix (die, cu),
13831 &objfile->objfile_obstack);
13832
13833 /* If we have address ranges, record them. */
13834 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13835
13836 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13837
13838 /* Attach template arguments to function. */
13839 if (!template_args.empty ())
13840 {
13841 gdb_assert (templ_func != NULL);
13842
13843 templ_func->n_template_arguments = template_args.size ();
13844 templ_func->template_arguments
13845 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13846 templ_func->n_template_arguments);
13847 memcpy (templ_func->template_arguments,
13848 template_args.data (),
13849 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13850 }
13851
13852 /* In C++, we can have functions nested inside functions (e.g., when
13853 a function declares a class that has methods). This means that
13854 when we finish processing a function scope, we may need to go
13855 back to building a containing block's symbol lists. */
13856 local_symbols = newobj->locals;
13857 local_using_directives = newobj->local_using_directives;
13858
13859 /* If we've finished processing a top-level function, subsequent
13860 symbols go in the file symbol list. */
13861 if (outermost_context_p ())
13862 cu->list_in_scope = &file_symbols;
13863 }
13864
13865 /* Process all the DIES contained within a lexical block scope. Start
13866 a new scope, process the dies, and then close the scope. */
13867
13868 static void
13869 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13870 {
13871 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13872 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13873 struct context_stack *newobj;
13874 CORE_ADDR lowpc, highpc;
13875 struct die_info *child_die;
13876 CORE_ADDR baseaddr;
13877
13878 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13879
13880 /* Ignore blocks with missing or invalid low and high pc attributes. */
13881 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13882 as multiple lexical blocks? Handling children in a sane way would
13883 be nasty. Might be easier to properly extend generic blocks to
13884 describe ranges. */
13885 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13886 {
13887 case PC_BOUNDS_NOT_PRESENT:
13888 /* DW_TAG_lexical_block has no attributes, process its children as if
13889 there was no wrapping by that DW_TAG_lexical_block.
13890 GCC does no longer produces such DWARF since GCC r224161. */
13891 for (child_die = die->child;
13892 child_die != NULL && child_die->tag;
13893 child_die = sibling_die (child_die))
13894 process_die (child_die, cu);
13895 return;
13896 case PC_BOUNDS_INVALID:
13897 return;
13898 }
13899 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13900 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13901
13902 push_context (0, lowpc);
13903 if (die->child != NULL)
13904 {
13905 child_die = die->child;
13906 while (child_die && child_die->tag)
13907 {
13908 process_die (child_die, cu);
13909 child_die = sibling_die (child_die);
13910 }
13911 }
13912 inherit_abstract_dies (die, cu);
13913 newobj = pop_context ();
13914
13915 if (local_symbols != NULL || local_using_directives != NULL)
13916 {
13917 struct block *block
13918 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13919 newobj->start_addr, highpc);
13920
13921 /* Note that recording ranges after traversing children, as we
13922 do here, means that recording a parent's ranges entails
13923 walking across all its children's ranges as they appear in
13924 the address map, which is quadratic behavior.
13925
13926 It would be nicer to record the parent's ranges before
13927 traversing its children, simply overriding whatever you find
13928 there. But since we don't even decide whether to create a
13929 block until after we've traversed its children, that's hard
13930 to do. */
13931 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13932 }
13933 local_symbols = newobj->locals;
13934 local_using_directives = newobj->local_using_directives;
13935 }
13936
13937 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13938
13939 static void
13940 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13941 {
13942 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13943 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13944 CORE_ADDR pc, baseaddr;
13945 struct attribute *attr;
13946 struct call_site *call_site, call_site_local;
13947 void **slot;
13948 int nparams;
13949 struct die_info *child_die;
13950
13951 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13952
13953 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13954 if (attr == NULL)
13955 {
13956 /* This was a pre-DWARF-5 GNU extension alias
13957 for DW_AT_call_return_pc. */
13958 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13959 }
13960 if (!attr)
13961 {
13962 complaint (&symfile_complaints,
13963 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
13964 "DIE %s [in module %s]"),
13965 sect_offset_str (die->sect_off), objfile_name (objfile));
13966 return;
13967 }
13968 pc = attr_value_as_address (attr) + baseaddr;
13969 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13970
13971 if (cu->call_site_htab == NULL)
13972 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13973 NULL, &objfile->objfile_obstack,
13974 hashtab_obstack_allocate, NULL);
13975 call_site_local.pc = pc;
13976 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13977 if (*slot != NULL)
13978 {
13979 complaint (&symfile_complaints,
13980 _("Duplicate PC %s for DW_TAG_call_site "
13981 "DIE %s [in module %s]"),
13982 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13983 objfile_name (objfile));
13984 return;
13985 }
13986
13987 /* Count parameters at the caller. */
13988
13989 nparams = 0;
13990 for (child_die = die->child; child_die && child_die->tag;
13991 child_die = sibling_die (child_die))
13992 {
13993 if (child_die->tag != DW_TAG_call_site_parameter
13994 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13995 {
13996 complaint (&symfile_complaints,
13997 _("Tag %d is not DW_TAG_call_site_parameter in "
13998 "DW_TAG_call_site child DIE %s [in module %s]"),
13999 child_die->tag, sect_offset_str (child_die->sect_off),
14000 objfile_name (objfile));
14001 continue;
14002 }
14003
14004 nparams++;
14005 }
14006
14007 call_site
14008 = ((struct call_site *)
14009 obstack_alloc (&objfile->objfile_obstack,
14010 sizeof (*call_site)
14011 + (sizeof (*call_site->parameter) * (nparams - 1))));
14012 *slot = call_site;
14013 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14014 call_site->pc = pc;
14015
14016 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14017 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14018 {
14019 struct die_info *func_die;
14020
14021 /* Skip also over DW_TAG_inlined_subroutine. */
14022 for (func_die = die->parent;
14023 func_die && func_die->tag != DW_TAG_subprogram
14024 && func_die->tag != DW_TAG_subroutine_type;
14025 func_die = func_die->parent);
14026
14027 /* DW_AT_call_all_calls is a superset
14028 of DW_AT_call_all_tail_calls. */
14029 if (func_die
14030 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14031 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14032 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14033 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14034 {
14035 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14036 not complete. But keep CALL_SITE for look ups via call_site_htab,
14037 both the initial caller containing the real return address PC and
14038 the final callee containing the current PC of a chain of tail
14039 calls do not need to have the tail call list complete. But any
14040 function candidate for a virtual tail call frame searched via
14041 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14042 determined unambiguously. */
14043 }
14044 else
14045 {
14046 struct type *func_type = NULL;
14047
14048 if (func_die)
14049 func_type = get_die_type (func_die, cu);
14050 if (func_type != NULL)
14051 {
14052 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14053
14054 /* Enlist this call site to the function. */
14055 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14056 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14057 }
14058 else
14059 complaint (&symfile_complaints,
14060 _("Cannot find function owning DW_TAG_call_site "
14061 "DIE %s [in module %s]"),
14062 sect_offset_str (die->sect_off), objfile_name (objfile));
14063 }
14064 }
14065
14066 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14067 if (attr == NULL)
14068 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14069 if (attr == NULL)
14070 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14071 if (attr == NULL)
14072 {
14073 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14074 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14075 }
14076 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14077 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14078 /* Keep NULL DWARF_BLOCK. */;
14079 else if (attr_form_is_block (attr))
14080 {
14081 struct dwarf2_locexpr_baton *dlbaton;
14082
14083 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14084 dlbaton->data = DW_BLOCK (attr)->data;
14085 dlbaton->size = DW_BLOCK (attr)->size;
14086 dlbaton->per_cu = cu->per_cu;
14087
14088 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14089 }
14090 else if (attr_form_is_ref (attr))
14091 {
14092 struct dwarf2_cu *target_cu = cu;
14093 struct die_info *target_die;
14094
14095 target_die = follow_die_ref (die, attr, &target_cu);
14096 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14097 if (die_is_declaration (target_die, target_cu))
14098 {
14099 const char *target_physname;
14100
14101 /* Prefer the mangled name; otherwise compute the demangled one. */
14102 target_physname = dw2_linkage_name (target_die, target_cu);
14103 if (target_physname == NULL)
14104 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14105 if (target_physname == NULL)
14106 complaint (&symfile_complaints,
14107 _("DW_AT_call_target target DIE has invalid "
14108 "physname, for referencing DIE %s [in module %s]"),
14109 sect_offset_str (die->sect_off), objfile_name (objfile));
14110 else
14111 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14112 }
14113 else
14114 {
14115 CORE_ADDR lowpc;
14116
14117 /* DW_AT_entry_pc should be preferred. */
14118 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14119 <= PC_BOUNDS_INVALID)
14120 complaint (&symfile_complaints,
14121 _("DW_AT_call_target target DIE has invalid "
14122 "low pc, for referencing DIE %s [in module %s]"),
14123 sect_offset_str (die->sect_off), objfile_name (objfile));
14124 else
14125 {
14126 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14127 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14128 }
14129 }
14130 }
14131 else
14132 complaint (&symfile_complaints,
14133 _("DW_TAG_call_site DW_AT_call_target is neither "
14134 "block nor reference, for DIE %s [in module %s]"),
14135 sect_offset_str (die->sect_off), objfile_name (objfile));
14136
14137 call_site->per_cu = cu->per_cu;
14138
14139 for (child_die = die->child;
14140 child_die && child_die->tag;
14141 child_die = sibling_die (child_die))
14142 {
14143 struct call_site_parameter *parameter;
14144 struct attribute *loc, *origin;
14145
14146 if (child_die->tag != DW_TAG_call_site_parameter
14147 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14148 {
14149 /* Already printed the complaint above. */
14150 continue;
14151 }
14152
14153 gdb_assert (call_site->parameter_count < nparams);
14154 parameter = &call_site->parameter[call_site->parameter_count];
14155
14156 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14157 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14158 register is contained in DW_AT_call_value. */
14159
14160 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14161 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14162 if (origin == NULL)
14163 {
14164 /* This was a pre-DWARF-5 GNU extension alias
14165 for DW_AT_call_parameter. */
14166 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14167 }
14168 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14169 {
14170 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14171
14172 sect_offset sect_off
14173 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14174 if (!offset_in_cu_p (&cu->header, sect_off))
14175 {
14176 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14177 binding can be done only inside one CU. Such referenced DIE
14178 therefore cannot be even moved to DW_TAG_partial_unit. */
14179 complaint (&symfile_complaints,
14180 _("DW_AT_call_parameter offset is not in CU for "
14181 "DW_TAG_call_site child DIE %s [in module %s]"),
14182 sect_offset_str (child_die->sect_off),
14183 objfile_name (objfile));
14184 continue;
14185 }
14186 parameter->u.param_cu_off
14187 = (cu_offset) (sect_off - cu->header.sect_off);
14188 }
14189 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14190 {
14191 complaint (&symfile_complaints,
14192 _("No DW_FORM_block* DW_AT_location for "
14193 "DW_TAG_call_site child DIE %s [in module %s]"),
14194 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14195 continue;
14196 }
14197 else
14198 {
14199 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14200 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14201 if (parameter->u.dwarf_reg != -1)
14202 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14203 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14204 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14205 &parameter->u.fb_offset))
14206 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14207 else
14208 {
14209 complaint (&symfile_complaints,
14210 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14211 "for DW_FORM_block* DW_AT_location is supported for "
14212 "DW_TAG_call_site child DIE %s "
14213 "[in module %s]"),
14214 sect_offset_str (child_die->sect_off),
14215 objfile_name (objfile));
14216 continue;
14217 }
14218 }
14219
14220 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14221 if (attr == NULL)
14222 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14223 if (!attr_form_is_block (attr))
14224 {
14225 complaint (&symfile_complaints,
14226 _("No DW_FORM_block* DW_AT_call_value for "
14227 "DW_TAG_call_site child DIE %s [in module %s]"),
14228 sect_offset_str (child_die->sect_off),
14229 objfile_name (objfile));
14230 continue;
14231 }
14232 parameter->value = DW_BLOCK (attr)->data;
14233 parameter->value_size = DW_BLOCK (attr)->size;
14234
14235 /* Parameters are not pre-cleared by memset above. */
14236 parameter->data_value = NULL;
14237 parameter->data_value_size = 0;
14238 call_site->parameter_count++;
14239
14240 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14241 if (attr == NULL)
14242 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14243 if (attr)
14244 {
14245 if (!attr_form_is_block (attr))
14246 complaint (&symfile_complaints,
14247 _("No DW_FORM_block* DW_AT_call_data_value for "
14248 "DW_TAG_call_site child DIE %s [in module %s]"),
14249 sect_offset_str (child_die->sect_off),
14250 objfile_name (objfile));
14251 else
14252 {
14253 parameter->data_value = DW_BLOCK (attr)->data;
14254 parameter->data_value_size = DW_BLOCK (attr)->size;
14255 }
14256 }
14257 }
14258 }
14259
14260 /* Helper function for read_variable. If DIE represents a virtual
14261 table, then return the type of the concrete object that is
14262 associated with the virtual table. Otherwise, return NULL. */
14263
14264 static struct type *
14265 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14266 {
14267 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14268 if (attr == NULL)
14269 return NULL;
14270
14271 /* Find the type DIE. */
14272 struct die_info *type_die = NULL;
14273 struct dwarf2_cu *type_cu = cu;
14274
14275 if (attr_form_is_ref (attr))
14276 type_die = follow_die_ref (die, attr, &type_cu);
14277 if (type_die == NULL)
14278 return NULL;
14279
14280 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14281 return NULL;
14282 return die_containing_type (type_die, type_cu);
14283 }
14284
14285 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14286
14287 static void
14288 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14289 {
14290 struct rust_vtable_symbol *storage = NULL;
14291
14292 if (cu->language == language_rust)
14293 {
14294 struct type *containing_type = rust_containing_type (die, cu);
14295
14296 if (containing_type != NULL)
14297 {
14298 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14299
14300 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14301 struct rust_vtable_symbol);
14302 initialize_objfile_symbol (storage);
14303 storage->concrete_type = containing_type;
14304 storage->subclass = SYMBOL_RUST_VTABLE;
14305 }
14306 }
14307
14308 new_symbol (die, NULL, cu, storage);
14309 }
14310
14311 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14312 reading .debug_rnglists.
14313 Callback's type should be:
14314 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14315 Return true if the attributes are present and valid, otherwise,
14316 return false. */
14317
14318 template <typename Callback>
14319 static bool
14320 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14321 Callback &&callback)
14322 {
14323 struct dwarf2_per_objfile *dwarf2_per_objfile
14324 = cu->per_cu->dwarf2_per_objfile;
14325 struct objfile *objfile = dwarf2_per_objfile->objfile;
14326 bfd *obfd = objfile->obfd;
14327 /* Base address selection entry. */
14328 CORE_ADDR base;
14329 int found_base;
14330 const gdb_byte *buffer;
14331 CORE_ADDR baseaddr;
14332 bool overflow = false;
14333
14334 found_base = cu->base_known;
14335 base = cu->base_address;
14336
14337 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14338 if (offset >= dwarf2_per_objfile->rnglists.size)
14339 {
14340 complaint (&symfile_complaints,
14341 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14342 offset);
14343 return false;
14344 }
14345 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14346
14347 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14348
14349 while (1)
14350 {
14351 /* Initialize it due to a false compiler warning. */
14352 CORE_ADDR range_beginning = 0, range_end = 0;
14353 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14354 + dwarf2_per_objfile->rnglists.size);
14355 unsigned int bytes_read;
14356
14357 if (buffer == buf_end)
14358 {
14359 overflow = true;
14360 break;
14361 }
14362 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14363 switch (rlet)
14364 {
14365 case DW_RLE_end_of_list:
14366 break;
14367 case DW_RLE_base_address:
14368 if (buffer + cu->header.addr_size > buf_end)
14369 {
14370 overflow = true;
14371 break;
14372 }
14373 base = read_address (obfd, buffer, cu, &bytes_read);
14374 found_base = 1;
14375 buffer += bytes_read;
14376 break;
14377 case DW_RLE_start_length:
14378 if (buffer + cu->header.addr_size > buf_end)
14379 {
14380 overflow = true;
14381 break;
14382 }
14383 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14384 buffer += bytes_read;
14385 range_end = (range_beginning
14386 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14387 buffer += bytes_read;
14388 if (buffer > buf_end)
14389 {
14390 overflow = true;
14391 break;
14392 }
14393 break;
14394 case DW_RLE_offset_pair:
14395 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14396 buffer += bytes_read;
14397 if (buffer > buf_end)
14398 {
14399 overflow = true;
14400 break;
14401 }
14402 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14403 buffer += bytes_read;
14404 if (buffer > buf_end)
14405 {
14406 overflow = true;
14407 break;
14408 }
14409 break;
14410 case DW_RLE_start_end:
14411 if (buffer + 2 * cu->header.addr_size > buf_end)
14412 {
14413 overflow = true;
14414 break;
14415 }
14416 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14417 buffer += bytes_read;
14418 range_end = read_address (obfd, buffer, cu, &bytes_read);
14419 buffer += bytes_read;
14420 break;
14421 default:
14422 complaint (&symfile_complaints,
14423 _("Invalid .debug_rnglists data (no base address)"));
14424 return false;
14425 }
14426 if (rlet == DW_RLE_end_of_list || overflow)
14427 break;
14428 if (rlet == DW_RLE_base_address)
14429 continue;
14430
14431 if (!found_base)
14432 {
14433 /* We have no valid base address for the ranges
14434 data. */
14435 complaint (&symfile_complaints,
14436 _("Invalid .debug_rnglists data (no base address)"));
14437 return false;
14438 }
14439
14440 if (range_beginning > range_end)
14441 {
14442 /* Inverted range entries are invalid. */
14443 complaint (&symfile_complaints,
14444 _("Invalid .debug_rnglists data (inverted range)"));
14445 return false;
14446 }
14447
14448 /* Empty range entries have no effect. */
14449 if (range_beginning == range_end)
14450 continue;
14451
14452 range_beginning += base;
14453 range_end += base;
14454
14455 /* A not-uncommon case of bad debug info.
14456 Don't pollute the addrmap with bad data. */
14457 if (range_beginning + baseaddr == 0
14458 && !dwarf2_per_objfile->has_section_at_zero)
14459 {
14460 complaint (&symfile_complaints,
14461 _(".debug_rnglists entry has start address of zero"
14462 " [in module %s]"), objfile_name (objfile));
14463 continue;
14464 }
14465
14466 callback (range_beginning, range_end);
14467 }
14468
14469 if (overflow)
14470 {
14471 complaint (&symfile_complaints,
14472 _("Offset %d is not terminated "
14473 "for DW_AT_ranges attribute"),
14474 offset);
14475 return false;
14476 }
14477
14478 return true;
14479 }
14480
14481 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14482 Callback's type should be:
14483 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14484 Return 1 if the attributes are present and valid, otherwise, return 0. */
14485
14486 template <typename Callback>
14487 static int
14488 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14489 Callback &&callback)
14490 {
14491 struct dwarf2_per_objfile *dwarf2_per_objfile
14492 = cu->per_cu->dwarf2_per_objfile;
14493 struct objfile *objfile = dwarf2_per_objfile->objfile;
14494 struct comp_unit_head *cu_header = &cu->header;
14495 bfd *obfd = objfile->obfd;
14496 unsigned int addr_size = cu_header->addr_size;
14497 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14498 /* Base address selection entry. */
14499 CORE_ADDR base;
14500 int found_base;
14501 unsigned int dummy;
14502 const gdb_byte *buffer;
14503 CORE_ADDR baseaddr;
14504
14505 if (cu_header->version >= 5)
14506 return dwarf2_rnglists_process (offset, cu, callback);
14507
14508 found_base = cu->base_known;
14509 base = cu->base_address;
14510
14511 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14512 if (offset >= dwarf2_per_objfile->ranges.size)
14513 {
14514 complaint (&symfile_complaints,
14515 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14516 offset);
14517 return 0;
14518 }
14519 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14520
14521 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14522
14523 while (1)
14524 {
14525 CORE_ADDR range_beginning, range_end;
14526
14527 range_beginning = read_address (obfd, buffer, cu, &dummy);
14528 buffer += addr_size;
14529 range_end = read_address (obfd, buffer, cu, &dummy);
14530 buffer += addr_size;
14531 offset += 2 * addr_size;
14532
14533 /* An end of list marker is a pair of zero addresses. */
14534 if (range_beginning == 0 && range_end == 0)
14535 /* Found the end of list entry. */
14536 break;
14537
14538 /* Each base address selection entry is a pair of 2 values.
14539 The first is the largest possible address, the second is
14540 the base address. Check for a base address here. */
14541 if ((range_beginning & mask) == mask)
14542 {
14543 /* If we found the largest possible address, then we already
14544 have the base address in range_end. */
14545 base = range_end;
14546 found_base = 1;
14547 continue;
14548 }
14549
14550 if (!found_base)
14551 {
14552 /* We have no valid base address for the ranges
14553 data. */
14554 complaint (&symfile_complaints,
14555 _("Invalid .debug_ranges data (no base address)"));
14556 return 0;
14557 }
14558
14559 if (range_beginning > range_end)
14560 {
14561 /* Inverted range entries are invalid. */
14562 complaint (&symfile_complaints,
14563 _("Invalid .debug_ranges data (inverted range)"));
14564 return 0;
14565 }
14566
14567 /* Empty range entries have no effect. */
14568 if (range_beginning == range_end)
14569 continue;
14570
14571 range_beginning += base;
14572 range_end += base;
14573
14574 /* A not-uncommon case of bad debug info.
14575 Don't pollute the addrmap with bad data. */
14576 if (range_beginning + baseaddr == 0
14577 && !dwarf2_per_objfile->has_section_at_zero)
14578 {
14579 complaint (&symfile_complaints,
14580 _(".debug_ranges entry has start address of zero"
14581 " [in module %s]"), objfile_name (objfile));
14582 continue;
14583 }
14584
14585 callback (range_beginning, range_end);
14586 }
14587
14588 return 1;
14589 }
14590
14591 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14592 Return 1 if the attributes are present and valid, otherwise, return 0.
14593 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14594
14595 static int
14596 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14597 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14598 struct partial_symtab *ranges_pst)
14599 {
14600 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14601 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14602 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14603 SECT_OFF_TEXT (objfile));
14604 int low_set = 0;
14605 CORE_ADDR low = 0;
14606 CORE_ADDR high = 0;
14607 int retval;
14608
14609 retval = dwarf2_ranges_process (offset, cu,
14610 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14611 {
14612 if (ranges_pst != NULL)
14613 {
14614 CORE_ADDR lowpc;
14615 CORE_ADDR highpc;
14616
14617 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14618 range_beginning + baseaddr);
14619 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14620 range_end + baseaddr);
14621 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14622 ranges_pst);
14623 }
14624
14625 /* FIXME: This is recording everything as a low-high
14626 segment of consecutive addresses. We should have a
14627 data structure for discontiguous block ranges
14628 instead. */
14629 if (! low_set)
14630 {
14631 low = range_beginning;
14632 high = range_end;
14633 low_set = 1;
14634 }
14635 else
14636 {
14637 if (range_beginning < low)
14638 low = range_beginning;
14639 if (range_end > high)
14640 high = range_end;
14641 }
14642 });
14643 if (!retval)
14644 return 0;
14645
14646 if (! low_set)
14647 /* If the first entry is an end-of-list marker, the range
14648 describes an empty scope, i.e. no instructions. */
14649 return 0;
14650
14651 if (low_return)
14652 *low_return = low;
14653 if (high_return)
14654 *high_return = high;
14655 return 1;
14656 }
14657
14658 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14659 definition for the return value. *LOWPC and *HIGHPC are set iff
14660 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14661
14662 static enum pc_bounds_kind
14663 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14664 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14665 struct partial_symtab *pst)
14666 {
14667 struct dwarf2_per_objfile *dwarf2_per_objfile
14668 = cu->per_cu->dwarf2_per_objfile;
14669 struct attribute *attr;
14670 struct attribute *attr_high;
14671 CORE_ADDR low = 0;
14672 CORE_ADDR high = 0;
14673 enum pc_bounds_kind ret;
14674
14675 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14676 if (attr_high)
14677 {
14678 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14679 if (attr)
14680 {
14681 low = attr_value_as_address (attr);
14682 high = attr_value_as_address (attr_high);
14683 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14684 high += low;
14685 }
14686 else
14687 /* Found high w/o low attribute. */
14688 return PC_BOUNDS_INVALID;
14689
14690 /* Found consecutive range of addresses. */
14691 ret = PC_BOUNDS_HIGH_LOW;
14692 }
14693 else
14694 {
14695 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14696 if (attr != NULL)
14697 {
14698 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14699 We take advantage of the fact that DW_AT_ranges does not appear
14700 in DW_TAG_compile_unit of DWO files. */
14701 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14702 unsigned int ranges_offset = (DW_UNSND (attr)
14703 + (need_ranges_base
14704 ? cu->ranges_base
14705 : 0));
14706
14707 /* Value of the DW_AT_ranges attribute is the offset in the
14708 .debug_ranges section. */
14709 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14710 return PC_BOUNDS_INVALID;
14711 /* Found discontinuous range of addresses. */
14712 ret = PC_BOUNDS_RANGES;
14713 }
14714 else
14715 return PC_BOUNDS_NOT_PRESENT;
14716 }
14717
14718 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14719 if (high <= low)
14720 return PC_BOUNDS_INVALID;
14721
14722 /* When using the GNU linker, .gnu.linkonce. sections are used to
14723 eliminate duplicate copies of functions and vtables and such.
14724 The linker will arbitrarily choose one and discard the others.
14725 The AT_*_pc values for such functions refer to local labels in
14726 these sections. If the section from that file was discarded, the
14727 labels are not in the output, so the relocs get a value of 0.
14728 If this is a discarded function, mark the pc bounds as invalid,
14729 so that GDB will ignore it. */
14730 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14731 return PC_BOUNDS_INVALID;
14732
14733 *lowpc = low;
14734 if (highpc)
14735 *highpc = high;
14736 return ret;
14737 }
14738
14739 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14740 its low and high PC addresses. Do nothing if these addresses could not
14741 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14742 and HIGHPC to the high address if greater than HIGHPC. */
14743
14744 static void
14745 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14746 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14747 struct dwarf2_cu *cu)
14748 {
14749 CORE_ADDR low, high;
14750 struct die_info *child = die->child;
14751
14752 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14753 {
14754 *lowpc = std::min (*lowpc, low);
14755 *highpc = std::max (*highpc, high);
14756 }
14757
14758 /* If the language does not allow nested subprograms (either inside
14759 subprograms or lexical blocks), we're done. */
14760 if (cu->language != language_ada)
14761 return;
14762
14763 /* Check all the children of the given DIE. If it contains nested
14764 subprograms, then check their pc bounds. Likewise, we need to
14765 check lexical blocks as well, as they may also contain subprogram
14766 definitions. */
14767 while (child && child->tag)
14768 {
14769 if (child->tag == DW_TAG_subprogram
14770 || child->tag == DW_TAG_lexical_block)
14771 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14772 child = sibling_die (child);
14773 }
14774 }
14775
14776 /* Get the low and high pc's represented by the scope DIE, and store
14777 them in *LOWPC and *HIGHPC. If the correct values can't be
14778 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14779
14780 static void
14781 get_scope_pc_bounds (struct die_info *die,
14782 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14783 struct dwarf2_cu *cu)
14784 {
14785 CORE_ADDR best_low = (CORE_ADDR) -1;
14786 CORE_ADDR best_high = (CORE_ADDR) 0;
14787 CORE_ADDR current_low, current_high;
14788
14789 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14790 >= PC_BOUNDS_RANGES)
14791 {
14792 best_low = current_low;
14793 best_high = current_high;
14794 }
14795 else
14796 {
14797 struct die_info *child = die->child;
14798
14799 while (child && child->tag)
14800 {
14801 switch (child->tag) {
14802 case DW_TAG_subprogram:
14803 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14804 break;
14805 case DW_TAG_namespace:
14806 case DW_TAG_module:
14807 /* FIXME: carlton/2004-01-16: Should we do this for
14808 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14809 that current GCC's always emit the DIEs corresponding
14810 to definitions of methods of classes as children of a
14811 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14812 the DIEs giving the declarations, which could be
14813 anywhere). But I don't see any reason why the
14814 standards says that they have to be there. */
14815 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14816
14817 if (current_low != ((CORE_ADDR) -1))
14818 {
14819 best_low = std::min (best_low, current_low);
14820 best_high = std::max (best_high, current_high);
14821 }
14822 break;
14823 default:
14824 /* Ignore. */
14825 break;
14826 }
14827
14828 child = sibling_die (child);
14829 }
14830 }
14831
14832 *lowpc = best_low;
14833 *highpc = best_high;
14834 }
14835
14836 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14837 in DIE. */
14838
14839 static void
14840 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14841 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14842 {
14843 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14844 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14845 struct attribute *attr;
14846 struct attribute *attr_high;
14847
14848 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14849 if (attr_high)
14850 {
14851 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14852 if (attr)
14853 {
14854 CORE_ADDR low = attr_value_as_address (attr);
14855 CORE_ADDR high = attr_value_as_address (attr_high);
14856
14857 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14858 high += low;
14859
14860 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14861 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14862 record_block_range (block, low, high - 1);
14863 }
14864 }
14865
14866 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14867 if (attr)
14868 {
14869 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14870 We take advantage of the fact that DW_AT_ranges does not appear
14871 in DW_TAG_compile_unit of DWO files. */
14872 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14873
14874 /* The value of the DW_AT_ranges attribute is the offset of the
14875 address range list in the .debug_ranges section. */
14876 unsigned long offset = (DW_UNSND (attr)
14877 + (need_ranges_base ? cu->ranges_base : 0));
14878
14879 dwarf2_ranges_process (offset, cu,
14880 [&] (CORE_ADDR start, CORE_ADDR end)
14881 {
14882 start += baseaddr;
14883 end += baseaddr;
14884 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14885 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14886 record_block_range (block, start, end - 1);
14887 });
14888 }
14889 }
14890
14891 /* Check whether the producer field indicates either of GCC < 4.6, or the
14892 Intel C/C++ compiler, and cache the result in CU. */
14893
14894 static void
14895 check_producer (struct dwarf2_cu *cu)
14896 {
14897 int major, minor;
14898
14899 if (cu->producer == NULL)
14900 {
14901 /* For unknown compilers expect their behavior is DWARF version
14902 compliant.
14903
14904 GCC started to support .debug_types sections by -gdwarf-4 since
14905 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14906 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14907 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14908 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14909 }
14910 else if (producer_is_gcc (cu->producer, &major, &minor))
14911 {
14912 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14913 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14914 }
14915 else if (producer_is_icc (cu->producer, &major, &minor))
14916 cu->producer_is_icc_lt_14 = major < 14;
14917 else
14918 {
14919 /* For other non-GCC compilers, expect their behavior is DWARF version
14920 compliant. */
14921 }
14922
14923 cu->checked_producer = 1;
14924 }
14925
14926 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14927 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14928 during 4.6.0 experimental. */
14929
14930 static int
14931 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14932 {
14933 if (!cu->checked_producer)
14934 check_producer (cu);
14935
14936 return cu->producer_is_gxx_lt_4_6;
14937 }
14938
14939 /* Return the default accessibility type if it is not overriden by
14940 DW_AT_accessibility. */
14941
14942 static enum dwarf_access_attribute
14943 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14944 {
14945 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14946 {
14947 /* The default DWARF 2 accessibility for members is public, the default
14948 accessibility for inheritance is private. */
14949
14950 if (die->tag != DW_TAG_inheritance)
14951 return DW_ACCESS_public;
14952 else
14953 return DW_ACCESS_private;
14954 }
14955 else
14956 {
14957 /* DWARF 3+ defines the default accessibility a different way. The same
14958 rules apply now for DW_TAG_inheritance as for the members and it only
14959 depends on the container kind. */
14960
14961 if (die->parent->tag == DW_TAG_class_type)
14962 return DW_ACCESS_private;
14963 else
14964 return DW_ACCESS_public;
14965 }
14966 }
14967
14968 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14969 offset. If the attribute was not found return 0, otherwise return
14970 1. If it was found but could not properly be handled, set *OFFSET
14971 to 0. */
14972
14973 static int
14974 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14975 LONGEST *offset)
14976 {
14977 struct attribute *attr;
14978
14979 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14980 if (attr != NULL)
14981 {
14982 *offset = 0;
14983
14984 /* Note that we do not check for a section offset first here.
14985 This is because DW_AT_data_member_location is new in DWARF 4,
14986 so if we see it, we can assume that a constant form is really
14987 a constant and not a section offset. */
14988 if (attr_form_is_constant (attr))
14989 *offset = dwarf2_get_attr_constant_value (attr, 0);
14990 else if (attr_form_is_section_offset (attr))
14991 dwarf2_complex_location_expr_complaint ();
14992 else if (attr_form_is_block (attr))
14993 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14994 else
14995 dwarf2_complex_location_expr_complaint ();
14996
14997 return 1;
14998 }
14999
15000 return 0;
15001 }
15002
15003 /* Add an aggregate field to the field list. */
15004
15005 static void
15006 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15007 struct dwarf2_cu *cu)
15008 {
15009 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15010 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15011 struct nextfield *new_field;
15012 struct attribute *attr;
15013 struct field *fp;
15014 const char *fieldname = "";
15015
15016 if (die->tag == DW_TAG_inheritance)
15017 {
15018 fip->baseclasses.emplace_back ();
15019 new_field = &fip->baseclasses.back ();
15020 }
15021 else
15022 {
15023 fip->fields.emplace_back ();
15024 new_field = &fip->fields.back ();
15025 }
15026
15027 fip->nfields++;
15028
15029 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15030 if (attr)
15031 new_field->accessibility = DW_UNSND (attr);
15032 else
15033 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15034 if (new_field->accessibility != DW_ACCESS_public)
15035 fip->non_public_fields = 1;
15036
15037 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15038 if (attr)
15039 new_field->virtuality = DW_UNSND (attr);
15040 else
15041 new_field->virtuality = DW_VIRTUALITY_none;
15042
15043 fp = &new_field->field;
15044
15045 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15046 {
15047 LONGEST offset;
15048
15049 /* Data member other than a C++ static data member. */
15050
15051 /* Get type of field. */
15052 fp->type = die_type (die, cu);
15053
15054 SET_FIELD_BITPOS (*fp, 0);
15055
15056 /* Get bit size of field (zero if none). */
15057 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15058 if (attr)
15059 {
15060 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15061 }
15062 else
15063 {
15064 FIELD_BITSIZE (*fp) = 0;
15065 }
15066
15067 /* Get bit offset of field. */
15068 if (handle_data_member_location (die, cu, &offset))
15069 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15070 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15071 if (attr)
15072 {
15073 if (gdbarch_bits_big_endian (gdbarch))
15074 {
15075 /* For big endian bits, the DW_AT_bit_offset gives the
15076 additional bit offset from the MSB of the containing
15077 anonymous object to the MSB of the field. We don't
15078 have to do anything special since we don't need to
15079 know the size of the anonymous object. */
15080 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15081 }
15082 else
15083 {
15084 /* For little endian bits, compute the bit offset to the
15085 MSB of the anonymous object, subtract off the number of
15086 bits from the MSB of the field to the MSB of the
15087 object, and then subtract off the number of bits of
15088 the field itself. The result is the bit offset of
15089 the LSB of the field. */
15090 int anonymous_size;
15091 int bit_offset = DW_UNSND (attr);
15092
15093 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15094 if (attr)
15095 {
15096 /* The size of the anonymous object containing
15097 the bit field is explicit, so use the
15098 indicated size (in bytes). */
15099 anonymous_size = DW_UNSND (attr);
15100 }
15101 else
15102 {
15103 /* The size of the anonymous object containing
15104 the bit field must be inferred from the type
15105 attribute of the data member containing the
15106 bit field. */
15107 anonymous_size = TYPE_LENGTH (fp->type);
15108 }
15109 SET_FIELD_BITPOS (*fp,
15110 (FIELD_BITPOS (*fp)
15111 + anonymous_size * bits_per_byte
15112 - bit_offset - FIELD_BITSIZE (*fp)));
15113 }
15114 }
15115 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15116 if (attr != NULL)
15117 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15118 + dwarf2_get_attr_constant_value (attr, 0)));
15119
15120 /* Get name of field. */
15121 fieldname = dwarf2_name (die, cu);
15122 if (fieldname == NULL)
15123 fieldname = "";
15124
15125 /* The name is already allocated along with this objfile, so we don't
15126 need to duplicate it for the type. */
15127 fp->name = fieldname;
15128
15129 /* Change accessibility for artificial fields (e.g. virtual table
15130 pointer or virtual base class pointer) to private. */
15131 if (dwarf2_attr (die, DW_AT_artificial, cu))
15132 {
15133 FIELD_ARTIFICIAL (*fp) = 1;
15134 new_field->accessibility = DW_ACCESS_private;
15135 fip->non_public_fields = 1;
15136 }
15137 }
15138 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15139 {
15140 /* C++ static member. */
15141
15142 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15143 is a declaration, but all versions of G++ as of this writing
15144 (so through at least 3.2.1) incorrectly generate
15145 DW_TAG_variable tags. */
15146
15147 const char *physname;
15148
15149 /* Get name of field. */
15150 fieldname = dwarf2_name (die, cu);
15151 if (fieldname == NULL)
15152 return;
15153
15154 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15155 if (attr
15156 /* Only create a symbol if this is an external value.
15157 new_symbol checks this and puts the value in the global symbol
15158 table, which we want. If it is not external, new_symbol
15159 will try to put the value in cu->list_in_scope which is wrong. */
15160 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15161 {
15162 /* A static const member, not much different than an enum as far as
15163 we're concerned, except that we can support more types. */
15164 new_symbol (die, NULL, cu);
15165 }
15166
15167 /* Get physical name. */
15168 physname = dwarf2_physname (fieldname, die, cu);
15169
15170 /* The name is already allocated along with this objfile, so we don't
15171 need to duplicate it for the type. */
15172 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15173 FIELD_TYPE (*fp) = die_type (die, cu);
15174 FIELD_NAME (*fp) = fieldname;
15175 }
15176 else if (die->tag == DW_TAG_inheritance)
15177 {
15178 LONGEST offset;
15179
15180 /* C++ base class field. */
15181 if (handle_data_member_location (die, cu, &offset))
15182 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15183 FIELD_BITSIZE (*fp) = 0;
15184 FIELD_TYPE (*fp) = die_type (die, cu);
15185 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15186 }
15187 else if (die->tag == DW_TAG_variant_part)
15188 {
15189 /* process_structure_scope will treat this DIE as a union. */
15190 process_structure_scope (die, cu);
15191
15192 /* The variant part is relative to the start of the enclosing
15193 structure. */
15194 SET_FIELD_BITPOS (*fp, 0);
15195 fp->type = get_die_type (die, cu);
15196 fp->artificial = 1;
15197 fp->name = "<<variant>>";
15198 }
15199 else
15200 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15201 }
15202
15203 /* Can the type given by DIE define another type? */
15204
15205 static bool
15206 type_can_define_types (const struct die_info *die)
15207 {
15208 switch (die->tag)
15209 {
15210 case DW_TAG_typedef:
15211 case DW_TAG_class_type:
15212 case DW_TAG_structure_type:
15213 case DW_TAG_union_type:
15214 case DW_TAG_enumeration_type:
15215 return true;
15216
15217 default:
15218 return false;
15219 }
15220 }
15221
15222 /* Add a type definition defined in the scope of the FIP's class. */
15223
15224 static void
15225 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15226 struct dwarf2_cu *cu)
15227 {
15228 struct decl_field fp;
15229 memset (&fp, 0, sizeof (fp));
15230
15231 gdb_assert (type_can_define_types (die));
15232
15233 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15234 fp.name = dwarf2_name (die, cu);
15235 fp.type = read_type_die (die, cu);
15236
15237 /* Save accessibility. */
15238 enum dwarf_access_attribute accessibility;
15239 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15240 if (attr != NULL)
15241 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15242 else
15243 accessibility = dwarf2_default_access_attribute (die, cu);
15244 switch (accessibility)
15245 {
15246 case DW_ACCESS_public:
15247 /* The assumed value if neither private nor protected. */
15248 break;
15249 case DW_ACCESS_private:
15250 fp.is_private = 1;
15251 break;
15252 case DW_ACCESS_protected:
15253 fp.is_protected = 1;
15254 break;
15255 default:
15256 complaint (&symfile_complaints,
15257 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15258 }
15259
15260 if (die->tag == DW_TAG_typedef)
15261 fip->typedef_field_list.push_back (fp);
15262 else
15263 fip->nested_types_list.push_back (fp);
15264 }
15265
15266 /* Create the vector of fields, and attach it to the type. */
15267
15268 static void
15269 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15270 struct dwarf2_cu *cu)
15271 {
15272 int nfields = fip->nfields;
15273
15274 /* Record the field count, allocate space for the array of fields,
15275 and create blank accessibility bitfields if necessary. */
15276 TYPE_NFIELDS (type) = nfields;
15277 TYPE_FIELDS (type) = (struct field *)
15278 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15279
15280 if (fip->non_public_fields && cu->language != language_ada)
15281 {
15282 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15283
15284 TYPE_FIELD_PRIVATE_BITS (type) =
15285 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15286 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15287
15288 TYPE_FIELD_PROTECTED_BITS (type) =
15289 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15290 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15291
15292 TYPE_FIELD_IGNORE_BITS (type) =
15293 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15294 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15295 }
15296
15297 /* If the type has baseclasses, allocate and clear a bit vector for
15298 TYPE_FIELD_VIRTUAL_BITS. */
15299 if (!fip->baseclasses.empty () && cu->language != language_ada)
15300 {
15301 int num_bytes = B_BYTES (fip->baseclasses.size ());
15302 unsigned char *pointer;
15303
15304 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15305 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15306 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15307 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15308 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15309 }
15310
15311 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15312 {
15313 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15314
15315 for (int index = 0; index < nfields; ++index)
15316 {
15317 struct nextfield &field = fip->fields[index];
15318
15319 if (field.variant.is_discriminant)
15320 di->discriminant_index = index;
15321 else if (field.variant.default_branch)
15322 di->default_index = index;
15323 else
15324 di->discriminants[index] = field.variant.discriminant_value;
15325 }
15326 }
15327
15328 /* Copy the saved-up fields into the field vector. */
15329 for (int i = 0; i < nfields; ++i)
15330 {
15331 struct nextfield &field
15332 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15333 : fip->fields[i - fip->baseclasses.size ()]);
15334
15335 TYPE_FIELD (type, i) = field.field;
15336 switch (field.accessibility)
15337 {
15338 case DW_ACCESS_private:
15339 if (cu->language != language_ada)
15340 SET_TYPE_FIELD_PRIVATE (type, i);
15341 break;
15342
15343 case DW_ACCESS_protected:
15344 if (cu->language != language_ada)
15345 SET_TYPE_FIELD_PROTECTED (type, i);
15346 break;
15347
15348 case DW_ACCESS_public:
15349 break;
15350
15351 default:
15352 /* Unknown accessibility. Complain and treat it as public. */
15353 {
15354 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15355 field.accessibility);
15356 }
15357 break;
15358 }
15359 if (i < fip->baseclasses.size ())
15360 {
15361 switch (field.virtuality)
15362 {
15363 case DW_VIRTUALITY_virtual:
15364 case DW_VIRTUALITY_pure_virtual:
15365 if (cu->language == language_ada)
15366 error (_("unexpected virtuality in component of Ada type"));
15367 SET_TYPE_FIELD_VIRTUAL (type, i);
15368 break;
15369 }
15370 }
15371 }
15372 }
15373
15374 /* Return true if this member function is a constructor, false
15375 otherwise. */
15376
15377 static int
15378 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15379 {
15380 const char *fieldname;
15381 const char *type_name;
15382 int len;
15383
15384 if (die->parent == NULL)
15385 return 0;
15386
15387 if (die->parent->tag != DW_TAG_structure_type
15388 && die->parent->tag != DW_TAG_union_type
15389 && die->parent->tag != DW_TAG_class_type)
15390 return 0;
15391
15392 fieldname = dwarf2_name (die, cu);
15393 type_name = dwarf2_name (die->parent, cu);
15394 if (fieldname == NULL || type_name == NULL)
15395 return 0;
15396
15397 len = strlen (fieldname);
15398 return (strncmp (fieldname, type_name, len) == 0
15399 && (type_name[len] == '\0' || type_name[len] == '<'));
15400 }
15401
15402 /* Add a member function to the proper fieldlist. */
15403
15404 static void
15405 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15406 struct type *type, struct dwarf2_cu *cu)
15407 {
15408 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15409 struct attribute *attr;
15410 int i;
15411 struct fnfieldlist *flp = nullptr;
15412 struct fn_field *fnp;
15413 const char *fieldname;
15414 struct type *this_type;
15415 enum dwarf_access_attribute accessibility;
15416
15417 if (cu->language == language_ada)
15418 error (_("unexpected member function in Ada type"));
15419
15420 /* Get name of member function. */
15421 fieldname = dwarf2_name (die, cu);
15422 if (fieldname == NULL)
15423 return;
15424
15425 /* Look up member function name in fieldlist. */
15426 for (i = 0; i < fip->fnfieldlists.size (); i++)
15427 {
15428 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15429 {
15430 flp = &fip->fnfieldlists[i];
15431 break;
15432 }
15433 }
15434
15435 /* Create a new fnfieldlist if necessary. */
15436 if (flp == nullptr)
15437 {
15438 fip->fnfieldlists.emplace_back ();
15439 flp = &fip->fnfieldlists.back ();
15440 flp->name = fieldname;
15441 i = fip->fnfieldlists.size () - 1;
15442 }
15443
15444 /* Create a new member function field and add it to the vector of
15445 fnfieldlists. */
15446 flp->fnfields.emplace_back ();
15447 fnp = &flp->fnfields.back ();
15448
15449 /* Delay processing of the physname until later. */
15450 if (cu->language == language_cplus)
15451 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15452 die, cu);
15453 else
15454 {
15455 const char *physname = dwarf2_physname (fieldname, die, cu);
15456 fnp->physname = physname ? physname : "";
15457 }
15458
15459 fnp->type = alloc_type (objfile);
15460 this_type = read_type_die (die, cu);
15461 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15462 {
15463 int nparams = TYPE_NFIELDS (this_type);
15464
15465 /* TYPE is the domain of this method, and THIS_TYPE is the type
15466 of the method itself (TYPE_CODE_METHOD). */
15467 smash_to_method_type (fnp->type, type,
15468 TYPE_TARGET_TYPE (this_type),
15469 TYPE_FIELDS (this_type),
15470 TYPE_NFIELDS (this_type),
15471 TYPE_VARARGS (this_type));
15472
15473 /* Handle static member functions.
15474 Dwarf2 has no clean way to discern C++ static and non-static
15475 member functions. G++ helps GDB by marking the first
15476 parameter for non-static member functions (which is the this
15477 pointer) as artificial. We obtain this information from
15478 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15479 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15480 fnp->voffset = VOFFSET_STATIC;
15481 }
15482 else
15483 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15484 dwarf2_full_name (fieldname, die, cu));
15485
15486 /* Get fcontext from DW_AT_containing_type if present. */
15487 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15488 fnp->fcontext = die_containing_type (die, cu);
15489
15490 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15491 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15492
15493 /* Get accessibility. */
15494 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15495 if (attr)
15496 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15497 else
15498 accessibility = dwarf2_default_access_attribute (die, cu);
15499 switch (accessibility)
15500 {
15501 case DW_ACCESS_private:
15502 fnp->is_private = 1;
15503 break;
15504 case DW_ACCESS_protected:
15505 fnp->is_protected = 1;
15506 break;
15507 }
15508
15509 /* Check for artificial methods. */
15510 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15511 if (attr && DW_UNSND (attr) != 0)
15512 fnp->is_artificial = 1;
15513
15514 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15515
15516 /* Get index in virtual function table if it is a virtual member
15517 function. For older versions of GCC, this is an offset in the
15518 appropriate virtual table, as specified by DW_AT_containing_type.
15519 For everyone else, it is an expression to be evaluated relative
15520 to the object address. */
15521
15522 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15523 if (attr)
15524 {
15525 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15526 {
15527 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15528 {
15529 /* Old-style GCC. */
15530 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15531 }
15532 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15533 || (DW_BLOCK (attr)->size > 1
15534 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15535 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15536 {
15537 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15538 if ((fnp->voffset % cu->header.addr_size) != 0)
15539 dwarf2_complex_location_expr_complaint ();
15540 else
15541 fnp->voffset /= cu->header.addr_size;
15542 fnp->voffset += 2;
15543 }
15544 else
15545 dwarf2_complex_location_expr_complaint ();
15546
15547 if (!fnp->fcontext)
15548 {
15549 /* If there is no `this' field and no DW_AT_containing_type,
15550 we cannot actually find a base class context for the
15551 vtable! */
15552 if (TYPE_NFIELDS (this_type) == 0
15553 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15554 {
15555 complaint (&symfile_complaints,
15556 _("cannot determine context for virtual member "
15557 "function \"%s\" (offset %s)"),
15558 fieldname, sect_offset_str (die->sect_off));
15559 }
15560 else
15561 {
15562 fnp->fcontext
15563 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15564 }
15565 }
15566 }
15567 else if (attr_form_is_section_offset (attr))
15568 {
15569 dwarf2_complex_location_expr_complaint ();
15570 }
15571 else
15572 {
15573 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15574 fieldname);
15575 }
15576 }
15577 else
15578 {
15579 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15580 if (attr && DW_UNSND (attr))
15581 {
15582 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15583 complaint (&symfile_complaints,
15584 _("Member function \"%s\" (offset %s) is virtual "
15585 "but the vtable offset is not specified"),
15586 fieldname, sect_offset_str (die->sect_off));
15587 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15588 TYPE_CPLUS_DYNAMIC (type) = 1;
15589 }
15590 }
15591 }
15592
15593 /* Create the vector of member function fields, and attach it to the type. */
15594
15595 static void
15596 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15597 struct dwarf2_cu *cu)
15598 {
15599 if (cu->language == language_ada)
15600 error (_("unexpected member functions in Ada type"));
15601
15602 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15603 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15604 TYPE_ALLOC (type,
15605 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15606
15607 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15608 {
15609 struct fnfieldlist &nf = fip->fnfieldlists[i];
15610 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15611
15612 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15613 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15614 fn_flp->fn_fields = (struct fn_field *)
15615 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15616
15617 for (int k = 0; k < nf.fnfields.size (); ++k)
15618 fn_flp->fn_fields[k] = nf.fnfields[k];
15619 }
15620
15621 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15622 }
15623
15624 /* Returns non-zero if NAME is the name of a vtable member in CU's
15625 language, zero otherwise. */
15626 static int
15627 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15628 {
15629 static const char vptr[] = "_vptr";
15630
15631 /* Look for the C++ form of the vtable. */
15632 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15633 return 1;
15634
15635 return 0;
15636 }
15637
15638 /* GCC outputs unnamed structures that are really pointers to member
15639 functions, with the ABI-specified layout. If TYPE describes
15640 such a structure, smash it into a member function type.
15641
15642 GCC shouldn't do this; it should just output pointer to member DIEs.
15643 This is GCC PR debug/28767. */
15644
15645 static void
15646 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15647 {
15648 struct type *pfn_type, *self_type, *new_type;
15649
15650 /* Check for a structure with no name and two children. */
15651 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15652 return;
15653
15654 /* Check for __pfn and __delta members. */
15655 if (TYPE_FIELD_NAME (type, 0) == NULL
15656 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15657 || TYPE_FIELD_NAME (type, 1) == NULL
15658 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15659 return;
15660
15661 /* Find the type of the method. */
15662 pfn_type = TYPE_FIELD_TYPE (type, 0);
15663 if (pfn_type == NULL
15664 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15665 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15666 return;
15667
15668 /* Look for the "this" argument. */
15669 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15670 if (TYPE_NFIELDS (pfn_type) == 0
15671 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15672 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15673 return;
15674
15675 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15676 new_type = alloc_type (objfile);
15677 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15678 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15679 TYPE_VARARGS (pfn_type));
15680 smash_to_methodptr_type (type, new_type);
15681 }
15682
15683
15684 /* Called when we find the DIE that starts a structure or union scope
15685 (definition) to create a type for the structure or union. Fill in
15686 the type's name and general properties; the members will not be
15687 processed until process_structure_scope. A symbol table entry for
15688 the type will also not be done until process_structure_scope (assuming
15689 the type has a name).
15690
15691 NOTE: we need to call these functions regardless of whether or not the
15692 DIE has a DW_AT_name attribute, since it might be an anonymous
15693 structure or union. This gets the type entered into our set of
15694 user defined types. */
15695
15696 static struct type *
15697 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15698 {
15699 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15700 struct type *type;
15701 struct attribute *attr;
15702 const char *name;
15703
15704 /* If the definition of this type lives in .debug_types, read that type.
15705 Don't follow DW_AT_specification though, that will take us back up
15706 the chain and we want to go down. */
15707 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15708 if (attr)
15709 {
15710 type = get_DW_AT_signature_type (die, attr, cu);
15711
15712 /* The type's CU may not be the same as CU.
15713 Ensure TYPE is recorded with CU in die_type_hash. */
15714 return set_die_type (die, type, cu);
15715 }
15716
15717 type = alloc_type (objfile);
15718 INIT_CPLUS_SPECIFIC (type);
15719
15720 name = dwarf2_name (die, cu);
15721 if (name != NULL)
15722 {
15723 if (cu->language == language_cplus
15724 || cu->language == language_d
15725 || cu->language == language_rust)
15726 {
15727 const char *full_name = dwarf2_full_name (name, die, cu);
15728
15729 /* dwarf2_full_name might have already finished building the DIE's
15730 type. If so, there is no need to continue. */
15731 if (get_die_type (die, cu) != NULL)
15732 return get_die_type (die, cu);
15733
15734 TYPE_TAG_NAME (type) = full_name;
15735 if (die->tag == DW_TAG_structure_type
15736 || die->tag == DW_TAG_class_type)
15737 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15738 }
15739 else
15740 {
15741 /* The name is already allocated along with this objfile, so
15742 we don't need to duplicate it for the type. */
15743 TYPE_TAG_NAME (type) = name;
15744 if (die->tag == DW_TAG_class_type)
15745 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15746 }
15747 }
15748
15749 if (die->tag == DW_TAG_structure_type)
15750 {
15751 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15752 }
15753 else if (die->tag == DW_TAG_union_type)
15754 {
15755 TYPE_CODE (type) = TYPE_CODE_UNION;
15756 }
15757 else if (die->tag == DW_TAG_variant_part)
15758 {
15759 TYPE_CODE (type) = TYPE_CODE_UNION;
15760 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15761 }
15762 else
15763 {
15764 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15765 }
15766
15767 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15768 TYPE_DECLARED_CLASS (type) = 1;
15769
15770 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15771 if (attr)
15772 {
15773 if (attr_form_is_constant (attr))
15774 TYPE_LENGTH (type) = DW_UNSND (attr);
15775 else
15776 {
15777 /* For the moment, dynamic type sizes are not supported
15778 by GDB's struct type. The actual size is determined
15779 on-demand when resolving the type of a given object,
15780 so set the type's length to zero for now. Otherwise,
15781 we record an expression as the length, and that expression
15782 could lead to a very large value, which could eventually
15783 lead to us trying to allocate that much memory when creating
15784 a value of that type. */
15785 TYPE_LENGTH (type) = 0;
15786 }
15787 }
15788 else
15789 {
15790 TYPE_LENGTH (type) = 0;
15791 }
15792
15793 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15794 {
15795 /* ICC<14 does not output the required DW_AT_declaration on
15796 incomplete types, but gives them a size of zero. */
15797 TYPE_STUB (type) = 1;
15798 }
15799 else
15800 TYPE_STUB_SUPPORTED (type) = 1;
15801
15802 if (die_is_declaration (die, cu))
15803 TYPE_STUB (type) = 1;
15804 else if (attr == NULL && die->child == NULL
15805 && producer_is_realview (cu->producer))
15806 /* RealView does not output the required DW_AT_declaration
15807 on incomplete types. */
15808 TYPE_STUB (type) = 1;
15809
15810 /* We need to add the type field to the die immediately so we don't
15811 infinitely recurse when dealing with pointers to the structure
15812 type within the structure itself. */
15813 set_die_type (die, type, cu);
15814
15815 /* set_die_type should be already done. */
15816 set_descriptive_type (type, die, cu);
15817
15818 return type;
15819 }
15820
15821 /* A helper for process_structure_scope that handles a single member
15822 DIE. */
15823
15824 static void
15825 handle_struct_member_die (struct die_info *child_die, struct type *type,
15826 struct field_info *fi,
15827 std::vector<struct symbol *> *template_args,
15828 struct dwarf2_cu *cu)
15829 {
15830 if (child_die->tag == DW_TAG_member
15831 || child_die->tag == DW_TAG_variable
15832 || child_die->tag == DW_TAG_variant_part)
15833 {
15834 /* NOTE: carlton/2002-11-05: A C++ static data member
15835 should be a DW_TAG_member that is a declaration, but
15836 all versions of G++ as of this writing (so through at
15837 least 3.2.1) incorrectly generate DW_TAG_variable
15838 tags for them instead. */
15839 dwarf2_add_field (fi, child_die, cu);
15840 }
15841 else if (child_die->tag == DW_TAG_subprogram)
15842 {
15843 /* Rust doesn't have member functions in the C++ sense.
15844 However, it does emit ordinary functions as children
15845 of a struct DIE. */
15846 if (cu->language == language_rust)
15847 read_func_scope (child_die, cu);
15848 else
15849 {
15850 /* C++ member function. */
15851 dwarf2_add_member_fn (fi, child_die, type, cu);
15852 }
15853 }
15854 else if (child_die->tag == DW_TAG_inheritance)
15855 {
15856 /* C++ base class field. */
15857 dwarf2_add_field (fi, child_die, cu);
15858 }
15859 else if (type_can_define_types (child_die))
15860 dwarf2_add_type_defn (fi, child_die, cu);
15861 else if (child_die->tag == DW_TAG_template_type_param
15862 || child_die->tag == DW_TAG_template_value_param)
15863 {
15864 struct symbol *arg = new_symbol (child_die, NULL, cu);
15865
15866 if (arg != NULL)
15867 template_args->push_back (arg);
15868 }
15869 else if (child_die->tag == DW_TAG_variant)
15870 {
15871 /* In a variant we want to get the discriminant and also add a
15872 field for our sole member child. */
15873 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15874
15875 for (struct die_info *variant_child = child_die->child;
15876 variant_child != NULL;
15877 variant_child = sibling_die (variant_child))
15878 {
15879 if (variant_child->tag == DW_TAG_member)
15880 {
15881 handle_struct_member_die (variant_child, type, fi,
15882 template_args, cu);
15883 /* Only handle the one. */
15884 break;
15885 }
15886 }
15887
15888 /* We don't handle this but we might as well report it if we see
15889 it. */
15890 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15891 complaint (&symfile_complaints,
15892 _("DW_AT_discr_list is not supported yet"
15893 " - DIE at %s [in module %s]"),
15894 sect_offset_str (child_die->sect_off),
15895 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15896
15897 /* The first field was just added, so we can stash the
15898 discriminant there. */
15899 gdb_assert (!fi->fields.empty ());
15900 if (discr == NULL)
15901 fi->fields.back ().variant.default_branch = true;
15902 else
15903 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15904 }
15905 }
15906
15907 /* Finish creating a structure or union type, including filling in
15908 its members and creating a symbol for it. */
15909
15910 static void
15911 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15912 {
15913 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15914 struct die_info *child_die;
15915 struct type *type;
15916
15917 type = get_die_type (die, cu);
15918 if (type == NULL)
15919 type = read_structure_type (die, cu);
15920
15921 /* When reading a DW_TAG_variant_part, we need to notice when we
15922 read the discriminant member, so we can record it later in the
15923 discriminant_info. */
15924 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15925 sect_offset discr_offset;
15926
15927 if (is_variant_part)
15928 {
15929 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15930 if (discr == NULL)
15931 {
15932 /* Maybe it's a univariant form, an extension we support.
15933 In this case arrange not to check the offset. */
15934 is_variant_part = false;
15935 }
15936 else if (attr_form_is_ref (discr))
15937 {
15938 struct dwarf2_cu *target_cu = cu;
15939 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15940
15941 discr_offset = target_die->sect_off;
15942 }
15943 else
15944 {
15945 complaint (&symfile_complaints,
15946 _("DW_AT_discr does not have DIE reference form"
15947 " - DIE at %s [in module %s]"),
15948 sect_offset_str (die->sect_off),
15949 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15950 is_variant_part = false;
15951 }
15952 }
15953
15954 if (die->child != NULL && ! die_is_declaration (die, cu))
15955 {
15956 struct field_info fi;
15957 std::vector<struct symbol *> template_args;
15958
15959 child_die = die->child;
15960
15961 while (child_die && child_die->tag)
15962 {
15963 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15964
15965 if (is_variant_part && discr_offset == child_die->sect_off)
15966 fi.fields.back ().variant.is_discriminant = true;
15967
15968 child_die = sibling_die (child_die);
15969 }
15970
15971 /* Attach template arguments to type. */
15972 if (!template_args.empty ())
15973 {
15974 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15975 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15976 TYPE_TEMPLATE_ARGUMENTS (type)
15977 = XOBNEWVEC (&objfile->objfile_obstack,
15978 struct symbol *,
15979 TYPE_N_TEMPLATE_ARGUMENTS (type));
15980 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15981 template_args.data (),
15982 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15983 * sizeof (struct symbol *)));
15984 }
15985
15986 /* Attach fields and member functions to the type. */
15987 if (fi.nfields)
15988 dwarf2_attach_fields_to_type (&fi, type, cu);
15989 if (!fi.fnfieldlists.empty ())
15990 {
15991 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15992
15993 /* Get the type which refers to the base class (possibly this
15994 class itself) which contains the vtable pointer for the current
15995 class from the DW_AT_containing_type attribute. This use of
15996 DW_AT_containing_type is a GNU extension. */
15997
15998 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15999 {
16000 struct type *t = die_containing_type (die, cu);
16001
16002 set_type_vptr_basetype (type, t);
16003 if (type == t)
16004 {
16005 int i;
16006
16007 /* Our own class provides vtbl ptr. */
16008 for (i = TYPE_NFIELDS (t) - 1;
16009 i >= TYPE_N_BASECLASSES (t);
16010 --i)
16011 {
16012 const char *fieldname = TYPE_FIELD_NAME (t, i);
16013
16014 if (is_vtable_name (fieldname, cu))
16015 {
16016 set_type_vptr_fieldno (type, i);
16017 break;
16018 }
16019 }
16020
16021 /* Complain if virtual function table field not found. */
16022 if (i < TYPE_N_BASECLASSES (t))
16023 complaint (&symfile_complaints,
16024 _("virtual function table pointer "
16025 "not found when defining class '%s'"),
16026 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16027 "");
16028 }
16029 else
16030 {
16031 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16032 }
16033 }
16034 else if (cu->producer
16035 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16036 {
16037 /* The IBM XLC compiler does not provide direct indication
16038 of the containing type, but the vtable pointer is
16039 always named __vfp. */
16040
16041 int i;
16042
16043 for (i = TYPE_NFIELDS (type) - 1;
16044 i >= TYPE_N_BASECLASSES (type);
16045 --i)
16046 {
16047 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16048 {
16049 set_type_vptr_fieldno (type, i);
16050 set_type_vptr_basetype (type, type);
16051 break;
16052 }
16053 }
16054 }
16055 }
16056
16057 /* Copy fi.typedef_field_list linked list elements content into the
16058 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16059 if (!fi.typedef_field_list.empty ())
16060 {
16061 int count = fi.typedef_field_list.size ();
16062
16063 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16064 TYPE_TYPEDEF_FIELD_ARRAY (type)
16065 = ((struct decl_field *)
16066 TYPE_ALLOC (type,
16067 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16068 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16069
16070 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16071 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16072 }
16073
16074 /* Copy fi.nested_types_list linked list elements content into the
16075 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16076 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16077 {
16078 int count = fi.nested_types_list.size ();
16079
16080 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16081 TYPE_NESTED_TYPES_ARRAY (type)
16082 = ((struct decl_field *)
16083 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16084 TYPE_NESTED_TYPES_COUNT (type) = count;
16085
16086 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16087 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16088 }
16089 }
16090
16091 quirk_gcc_member_function_pointer (type, objfile);
16092 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16093 cu->rust_unions.push_back (type);
16094
16095 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16096 snapshots) has been known to create a die giving a declaration
16097 for a class that has, as a child, a die giving a definition for a
16098 nested class. So we have to process our children even if the
16099 current die is a declaration. Normally, of course, a declaration
16100 won't have any children at all. */
16101
16102 child_die = die->child;
16103
16104 while (child_die != NULL && child_die->tag)
16105 {
16106 if (child_die->tag == DW_TAG_member
16107 || child_die->tag == DW_TAG_variable
16108 || child_die->tag == DW_TAG_inheritance
16109 || child_die->tag == DW_TAG_template_value_param
16110 || child_die->tag == DW_TAG_template_type_param)
16111 {
16112 /* Do nothing. */
16113 }
16114 else
16115 process_die (child_die, cu);
16116
16117 child_die = sibling_die (child_die);
16118 }
16119
16120 /* Do not consider external references. According to the DWARF standard,
16121 these DIEs are identified by the fact that they have no byte_size
16122 attribute, and a declaration attribute. */
16123 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16124 || !die_is_declaration (die, cu))
16125 new_symbol (die, type, cu);
16126 }
16127
16128 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16129 update TYPE using some information only available in DIE's children. */
16130
16131 static void
16132 update_enumeration_type_from_children (struct die_info *die,
16133 struct type *type,
16134 struct dwarf2_cu *cu)
16135 {
16136 struct die_info *child_die;
16137 int unsigned_enum = 1;
16138 int flag_enum = 1;
16139 ULONGEST mask = 0;
16140
16141 auto_obstack obstack;
16142
16143 for (child_die = die->child;
16144 child_die != NULL && child_die->tag;
16145 child_die = sibling_die (child_die))
16146 {
16147 struct attribute *attr;
16148 LONGEST value;
16149 const gdb_byte *bytes;
16150 struct dwarf2_locexpr_baton *baton;
16151 const char *name;
16152
16153 if (child_die->tag != DW_TAG_enumerator)
16154 continue;
16155
16156 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16157 if (attr == NULL)
16158 continue;
16159
16160 name = dwarf2_name (child_die, cu);
16161 if (name == NULL)
16162 name = "<anonymous enumerator>";
16163
16164 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16165 &value, &bytes, &baton);
16166 if (value < 0)
16167 {
16168 unsigned_enum = 0;
16169 flag_enum = 0;
16170 }
16171 else if ((mask & value) != 0)
16172 flag_enum = 0;
16173 else
16174 mask |= value;
16175
16176 /* If we already know that the enum type is neither unsigned, nor
16177 a flag type, no need to look at the rest of the enumerates. */
16178 if (!unsigned_enum && !flag_enum)
16179 break;
16180 }
16181
16182 if (unsigned_enum)
16183 TYPE_UNSIGNED (type) = 1;
16184 if (flag_enum)
16185 TYPE_FLAG_ENUM (type) = 1;
16186 }
16187
16188 /* Given a DW_AT_enumeration_type die, set its type. We do not
16189 complete the type's fields yet, or create any symbols. */
16190
16191 static struct type *
16192 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16193 {
16194 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16195 struct type *type;
16196 struct attribute *attr;
16197 const char *name;
16198
16199 /* If the definition of this type lives in .debug_types, read that type.
16200 Don't follow DW_AT_specification though, that will take us back up
16201 the chain and we want to go down. */
16202 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16203 if (attr)
16204 {
16205 type = get_DW_AT_signature_type (die, attr, cu);
16206
16207 /* The type's CU may not be the same as CU.
16208 Ensure TYPE is recorded with CU in die_type_hash. */
16209 return set_die_type (die, type, cu);
16210 }
16211
16212 type = alloc_type (objfile);
16213
16214 TYPE_CODE (type) = TYPE_CODE_ENUM;
16215 name = dwarf2_full_name (NULL, die, cu);
16216 if (name != NULL)
16217 TYPE_TAG_NAME (type) = name;
16218
16219 attr = dwarf2_attr (die, DW_AT_type, cu);
16220 if (attr != NULL)
16221 {
16222 struct type *underlying_type = die_type (die, cu);
16223
16224 TYPE_TARGET_TYPE (type) = underlying_type;
16225 }
16226
16227 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16228 if (attr)
16229 {
16230 TYPE_LENGTH (type) = DW_UNSND (attr);
16231 }
16232 else
16233 {
16234 TYPE_LENGTH (type) = 0;
16235 }
16236
16237 /* The enumeration DIE can be incomplete. In Ada, any type can be
16238 declared as private in the package spec, and then defined only
16239 inside the package body. Such types are known as Taft Amendment
16240 Types. When another package uses such a type, an incomplete DIE
16241 may be generated by the compiler. */
16242 if (die_is_declaration (die, cu))
16243 TYPE_STUB (type) = 1;
16244
16245 /* Finish the creation of this type by using the enum's children.
16246 We must call this even when the underlying type has been provided
16247 so that we can determine if we're looking at a "flag" enum. */
16248 update_enumeration_type_from_children (die, type, cu);
16249
16250 /* If this type has an underlying type that is not a stub, then we
16251 may use its attributes. We always use the "unsigned" attribute
16252 in this situation, because ordinarily we guess whether the type
16253 is unsigned -- but the guess can be wrong and the underlying type
16254 can tell us the reality. However, we defer to a local size
16255 attribute if one exists, because this lets the compiler override
16256 the underlying type if needed. */
16257 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16258 {
16259 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16260 if (TYPE_LENGTH (type) == 0)
16261 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16262 }
16263
16264 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16265
16266 return set_die_type (die, type, cu);
16267 }
16268
16269 /* Given a pointer to a die which begins an enumeration, process all
16270 the dies that define the members of the enumeration, and create the
16271 symbol for the enumeration type.
16272
16273 NOTE: We reverse the order of the element list. */
16274
16275 static void
16276 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16277 {
16278 struct type *this_type;
16279
16280 this_type = get_die_type (die, cu);
16281 if (this_type == NULL)
16282 this_type = read_enumeration_type (die, cu);
16283
16284 if (die->child != NULL)
16285 {
16286 struct die_info *child_die;
16287 struct symbol *sym;
16288 struct field *fields = NULL;
16289 int num_fields = 0;
16290 const char *name;
16291
16292 child_die = die->child;
16293 while (child_die && child_die->tag)
16294 {
16295 if (child_die->tag != DW_TAG_enumerator)
16296 {
16297 process_die (child_die, cu);
16298 }
16299 else
16300 {
16301 name = dwarf2_name (child_die, cu);
16302 if (name)
16303 {
16304 sym = new_symbol (child_die, this_type, cu);
16305
16306 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16307 {
16308 fields = (struct field *)
16309 xrealloc (fields,
16310 (num_fields + DW_FIELD_ALLOC_CHUNK)
16311 * sizeof (struct field));
16312 }
16313
16314 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16315 FIELD_TYPE (fields[num_fields]) = NULL;
16316 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16317 FIELD_BITSIZE (fields[num_fields]) = 0;
16318
16319 num_fields++;
16320 }
16321 }
16322
16323 child_die = sibling_die (child_die);
16324 }
16325
16326 if (num_fields)
16327 {
16328 TYPE_NFIELDS (this_type) = num_fields;
16329 TYPE_FIELDS (this_type) = (struct field *)
16330 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16331 memcpy (TYPE_FIELDS (this_type), fields,
16332 sizeof (struct field) * num_fields);
16333 xfree (fields);
16334 }
16335 }
16336
16337 /* If we are reading an enum from a .debug_types unit, and the enum
16338 is a declaration, and the enum is not the signatured type in the
16339 unit, then we do not want to add a symbol for it. Adding a
16340 symbol would in some cases obscure the true definition of the
16341 enum, giving users an incomplete type when the definition is
16342 actually available. Note that we do not want to do this for all
16343 enums which are just declarations, because C++0x allows forward
16344 enum declarations. */
16345 if (cu->per_cu->is_debug_types
16346 && die_is_declaration (die, cu))
16347 {
16348 struct signatured_type *sig_type;
16349
16350 sig_type = (struct signatured_type *) cu->per_cu;
16351 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16352 if (sig_type->type_offset_in_section != die->sect_off)
16353 return;
16354 }
16355
16356 new_symbol (die, this_type, cu);
16357 }
16358
16359 /* Extract all information from a DW_TAG_array_type DIE and put it in
16360 the DIE's type field. For now, this only handles one dimensional
16361 arrays. */
16362
16363 static struct type *
16364 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16365 {
16366 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16367 struct die_info *child_die;
16368 struct type *type;
16369 struct type *element_type, *range_type, *index_type;
16370 struct attribute *attr;
16371 const char *name;
16372 struct dynamic_prop *byte_stride_prop = NULL;
16373 unsigned int bit_stride = 0;
16374
16375 element_type = die_type (die, cu);
16376
16377 /* The die_type call above may have already set the type for this DIE. */
16378 type = get_die_type (die, cu);
16379 if (type)
16380 return type;
16381
16382 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16383 if (attr != NULL)
16384 {
16385 int stride_ok;
16386
16387 byte_stride_prop
16388 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16389 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16390 if (!stride_ok)
16391 {
16392 complaint (&symfile_complaints,
16393 _("unable to read array DW_AT_byte_stride "
16394 " - DIE at %s [in module %s]"),
16395 sect_offset_str (die->sect_off),
16396 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16397 /* Ignore this attribute. We will likely not be able to print
16398 arrays of this type correctly, but there is little we can do
16399 to help if we cannot read the attribute's value. */
16400 byte_stride_prop = NULL;
16401 }
16402 }
16403
16404 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16405 if (attr != NULL)
16406 bit_stride = DW_UNSND (attr);
16407
16408 /* Irix 6.2 native cc creates array types without children for
16409 arrays with unspecified length. */
16410 if (die->child == NULL)
16411 {
16412 index_type = objfile_type (objfile)->builtin_int;
16413 range_type = create_static_range_type (NULL, index_type, 0, -1);
16414 type = create_array_type_with_stride (NULL, element_type, range_type,
16415 byte_stride_prop, bit_stride);
16416 return set_die_type (die, type, cu);
16417 }
16418
16419 std::vector<struct type *> range_types;
16420 child_die = die->child;
16421 while (child_die && child_die->tag)
16422 {
16423 if (child_die->tag == DW_TAG_subrange_type)
16424 {
16425 struct type *child_type = read_type_die (child_die, cu);
16426
16427 if (child_type != NULL)
16428 {
16429 /* The range type was succesfully read. Save it for the
16430 array type creation. */
16431 range_types.push_back (child_type);
16432 }
16433 }
16434 child_die = sibling_die (child_die);
16435 }
16436
16437 /* Dwarf2 dimensions are output from left to right, create the
16438 necessary array types in backwards order. */
16439
16440 type = element_type;
16441
16442 if (read_array_order (die, cu) == DW_ORD_col_major)
16443 {
16444 int i = 0;
16445
16446 while (i < range_types.size ())
16447 type = create_array_type_with_stride (NULL, type, range_types[i++],
16448 byte_stride_prop, bit_stride);
16449 }
16450 else
16451 {
16452 size_t ndim = range_types.size ();
16453 while (ndim-- > 0)
16454 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16455 byte_stride_prop, bit_stride);
16456 }
16457
16458 /* Understand Dwarf2 support for vector types (like they occur on
16459 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16460 array type. This is not part of the Dwarf2/3 standard yet, but a
16461 custom vendor extension. The main difference between a regular
16462 array and the vector variant is that vectors are passed by value
16463 to functions. */
16464 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16465 if (attr)
16466 make_vector_type (type);
16467
16468 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16469 implementation may choose to implement triple vectors using this
16470 attribute. */
16471 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16472 if (attr)
16473 {
16474 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16475 TYPE_LENGTH (type) = DW_UNSND (attr);
16476 else
16477 complaint (&symfile_complaints,
16478 _("DW_AT_byte_size for array type smaller "
16479 "than the total size of elements"));
16480 }
16481
16482 name = dwarf2_name (die, cu);
16483 if (name)
16484 TYPE_NAME (type) = name;
16485
16486 /* Install the type in the die. */
16487 set_die_type (die, type, cu);
16488
16489 /* set_die_type should be already done. */
16490 set_descriptive_type (type, die, cu);
16491
16492 return type;
16493 }
16494
16495 static enum dwarf_array_dim_ordering
16496 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16497 {
16498 struct attribute *attr;
16499
16500 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16501
16502 if (attr)
16503 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16504
16505 /* GNU F77 is a special case, as at 08/2004 array type info is the
16506 opposite order to the dwarf2 specification, but data is still
16507 laid out as per normal fortran.
16508
16509 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16510 version checking. */
16511
16512 if (cu->language == language_fortran
16513 && cu->producer && strstr (cu->producer, "GNU F77"))
16514 {
16515 return DW_ORD_row_major;
16516 }
16517
16518 switch (cu->language_defn->la_array_ordering)
16519 {
16520 case array_column_major:
16521 return DW_ORD_col_major;
16522 case array_row_major:
16523 default:
16524 return DW_ORD_row_major;
16525 };
16526 }
16527
16528 /* Extract all information from a DW_TAG_set_type DIE and put it in
16529 the DIE's type field. */
16530
16531 static struct type *
16532 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16533 {
16534 struct type *domain_type, *set_type;
16535 struct attribute *attr;
16536
16537 domain_type = die_type (die, cu);
16538
16539 /* The die_type call above may have already set the type for this DIE. */
16540 set_type = get_die_type (die, cu);
16541 if (set_type)
16542 return set_type;
16543
16544 set_type = create_set_type (NULL, domain_type);
16545
16546 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16547 if (attr)
16548 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16549
16550 return set_die_type (die, set_type, cu);
16551 }
16552
16553 /* A helper for read_common_block that creates a locexpr baton.
16554 SYM is the symbol which we are marking as computed.
16555 COMMON_DIE is the DIE for the common block.
16556 COMMON_LOC is the location expression attribute for the common
16557 block itself.
16558 MEMBER_LOC is the location expression attribute for the particular
16559 member of the common block that we are processing.
16560 CU is the CU from which the above come. */
16561
16562 static void
16563 mark_common_block_symbol_computed (struct symbol *sym,
16564 struct die_info *common_die,
16565 struct attribute *common_loc,
16566 struct attribute *member_loc,
16567 struct dwarf2_cu *cu)
16568 {
16569 struct dwarf2_per_objfile *dwarf2_per_objfile
16570 = cu->per_cu->dwarf2_per_objfile;
16571 struct objfile *objfile = dwarf2_per_objfile->objfile;
16572 struct dwarf2_locexpr_baton *baton;
16573 gdb_byte *ptr;
16574 unsigned int cu_off;
16575 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16576 LONGEST offset = 0;
16577
16578 gdb_assert (common_loc && member_loc);
16579 gdb_assert (attr_form_is_block (common_loc));
16580 gdb_assert (attr_form_is_block (member_loc)
16581 || attr_form_is_constant (member_loc));
16582
16583 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16584 baton->per_cu = cu->per_cu;
16585 gdb_assert (baton->per_cu);
16586
16587 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16588
16589 if (attr_form_is_constant (member_loc))
16590 {
16591 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16592 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16593 }
16594 else
16595 baton->size += DW_BLOCK (member_loc)->size;
16596
16597 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16598 baton->data = ptr;
16599
16600 *ptr++ = DW_OP_call4;
16601 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16602 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16603 ptr += 4;
16604
16605 if (attr_form_is_constant (member_loc))
16606 {
16607 *ptr++ = DW_OP_addr;
16608 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16609 ptr += cu->header.addr_size;
16610 }
16611 else
16612 {
16613 /* We have to copy the data here, because DW_OP_call4 will only
16614 use a DW_AT_location attribute. */
16615 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16616 ptr += DW_BLOCK (member_loc)->size;
16617 }
16618
16619 *ptr++ = DW_OP_plus;
16620 gdb_assert (ptr - baton->data == baton->size);
16621
16622 SYMBOL_LOCATION_BATON (sym) = baton;
16623 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16624 }
16625
16626 /* Create appropriate locally-scoped variables for all the
16627 DW_TAG_common_block entries. Also create a struct common_block
16628 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16629 is used to sepate the common blocks name namespace from regular
16630 variable names. */
16631
16632 static void
16633 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16634 {
16635 struct attribute *attr;
16636
16637 attr = dwarf2_attr (die, DW_AT_location, cu);
16638 if (attr)
16639 {
16640 /* Support the .debug_loc offsets. */
16641 if (attr_form_is_block (attr))
16642 {
16643 /* Ok. */
16644 }
16645 else if (attr_form_is_section_offset (attr))
16646 {
16647 dwarf2_complex_location_expr_complaint ();
16648 attr = NULL;
16649 }
16650 else
16651 {
16652 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16653 "common block member");
16654 attr = NULL;
16655 }
16656 }
16657
16658 if (die->child != NULL)
16659 {
16660 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16661 struct die_info *child_die;
16662 size_t n_entries = 0, size;
16663 struct common_block *common_block;
16664 struct symbol *sym;
16665
16666 for (child_die = die->child;
16667 child_die && child_die->tag;
16668 child_die = sibling_die (child_die))
16669 ++n_entries;
16670
16671 size = (sizeof (struct common_block)
16672 + (n_entries - 1) * sizeof (struct symbol *));
16673 common_block
16674 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16675 size);
16676 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16677 common_block->n_entries = 0;
16678
16679 for (child_die = die->child;
16680 child_die && child_die->tag;
16681 child_die = sibling_die (child_die))
16682 {
16683 /* Create the symbol in the DW_TAG_common_block block in the current
16684 symbol scope. */
16685 sym = new_symbol (child_die, NULL, cu);
16686 if (sym != NULL)
16687 {
16688 struct attribute *member_loc;
16689
16690 common_block->contents[common_block->n_entries++] = sym;
16691
16692 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16693 cu);
16694 if (member_loc)
16695 {
16696 /* GDB has handled this for a long time, but it is
16697 not specified by DWARF. It seems to have been
16698 emitted by gfortran at least as recently as:
16699 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16700 complaint (&symfile_complaints,
16701 _("Variable in common block has "
16702 "DW_AT_data_member_location "
16703 "- DIE at %s [in module %s]"),
16704 sect_offset_str (child_die->sect_off),
16705 objfile_name (objfile));
16706
16707 if (attr_form_is_section_offset (member_loc))
16708 dwarf2_complex_location_expr_complaint ();
16709 else if (attr_form_is_constant (member_loc)
16710 || attr_form_is_block (member_loc))
16711 {
16712 if (attr)
16713 mark_common_block_symbol_computed (sym, die, attr,
16714 member_loc, cu);
16715 }
16716 else
16717 dwarf2_complex_location_expr_complaint ();
16718 }
16719 }
16720 }
16721
16722 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16723 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16724 }
16725 }
16726
16727 /* Create a type for a C++ namespace. */
16728
16729 static struct type *
16730 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16731 {
16732 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16733 const char *previous_prefix, *name;
16734 int is_anonymous;
16735 struct type *type;
16736
16737 /* For extensions, reuse the type of the original namespace. */
16738 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16739 {
16740 struct die_info *ext_die;
16741 struct dwarf2_cu *ext_cu = cu;
16742
16743 ext_die = dwarf2_extension (die, &ext_cu);
16744 type = read_type_die (ext_die, ext_cu);
16745
16746 /* EXT_CU may not be the same as CU.
16747 Ensure TYPE is recorded with CU in die_type_hash. */
16748 return set_die_type (die, type, cu);
16749 }
16750
16751 name = namespace_name (die, &is_anonymous, cu);
16752
16753 /* Now build the name of the current namespace. */
16754
16755 previous_prefix = determine_prefix (die, cu);
16756 if (previous_prefix[0] != '\0')
16757 name = typename_concat (&objfile->objfile_obstack,
16758 previous_prefix, name, 0, cu);
16759
16760 /* Create the type. */
16761 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16762 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16763
16764 return set_die_type (die, type, cu);
16765 }
16766
16767 /* Read a namespace scope. */
16768
16769 static void
16770 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16771 {
16772 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16773 int is_anonymous;
16774
16775 /* Add a symbol associated to this if we haven't seen the namespace
16776 before. Also, add a using directive if it's an anonymous
16777 namespace. */
16778
16779 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16780 {
16781 struct type *type;
16782
16783 type = read_type_die (die, cu);
16784 new_symbol (die, type, cu);
16785
16786 namespace_name (die, &is_anonymous, cu);
16787 if (is_anonymous)
16788 {
16789 const char *previous_prefix = determine_prefix (die, cu);
16790
16791 std::vector<const char *> excludes;
16792 add_using_directive (using_directives (cu->language),
16793 previous_prefix, TYPE_NAME (type), NULL,
16794 NULL, excludes, 0, &objfile->objfile_obstack);
16795 }
16796 }
16797
16798 if (die->child != NULL)
16799 {
16800 struct die_info *child_die = die->child;
16801
16802 while (child_die && child_die->tag)
16803 {
16804 process_die (child_die, cu);
16805 child_die = sibling_die (child_die);
16806 }
16807 }
16808 }
16809
16810 /* Read a Fortran module as type. This DIE can be only a declaration used for
16811 imported module. Still we need that type as local Fortran "use ... only"
16812 declaration imports depend on the created type in determine_prefix. */
16813
16814 static struct type *
16815 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16816 {
16817 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16818 const char *module_name;
16819 struct type *type;
16820
16821 module_name = dwarf2_name (die, cu);
16822 if (!module_name)
16823 complaint (&symfile_complaints,
16824 _("DW_TAG_module has no name, offset %s"),
16825 sect_offset_str (die->sect_off));
16826 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16827
16828 /* determine_prefix uses TYPE_TAG_NAME. */
16829 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16830
16831 return set_die_type (die, type, cu);
16832 }
16833
16834 /* Read a Fortran module. */
16835
16836 static void
16837 read_module (struct die_info *die, struct dwarf2_cu *cu)
16838 {
16839 struct die_info *child_die = die->child;
16840 struct type *type;
16841
16842 type = read_type_die (die, cu);
16843 new_symbol (die, type, cu);
16844
16845 while (child_die && child_die->tag)
16846 {
16847 process_die (child_die, cu);
16848 child_die = sibling_die (child_die);
16849 }
16850 }
16851
16852 /* Return the name of the namespace represented by DIE. Set
16853 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16854 namespace. */
16855
16856 static const char *
16857 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16858 {
16859 struct die_info *current_die;
16860 const char *name = NULL;
16861
16862 /* Loop through the extensions until we find a name. */
16863
16864 for (current_die = die;
16865 current_die != NULL;
16866 current_die = dwarf2_extension (die, &cu))
16867 {
16868 /* We don't use dwarf2_name here so that we can detect the absence
16869 of a name -> anonymous namespace. */
16870 name = dwarf2_string_attr (die, DW_AT_name, cu);
16871
16872 if (name != NULL)
16873 break;
16874 }
16875
16876 /* Is it an anonymous namespace? */
16877
16878 *is_anonymous = (name == NULL);
16879 if (*is_anonymous)
16880 name = CP_ANONYMOUS_NAMESPACE_STR;
16881
16882 return name;
16883 }
16884
16885 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16886 the user defined type vector. */
16887
16888 static struct type *
16889 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16890 {
16891 struct gdbarch *gdbarch
16892 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16893 struct comp_unit_head *cu_header = &cu->header;
16894 struct type *type;
16895 struct attribute *attr_byte_size;
16896 struct attribute *attr_address_class;
16897 int byte_size, addr_class;
16898 struct type *target_type;
16899
16900 target_type = die_type (die, cu);
16901
16902 /* The die_type call above may have already set the type for this DIE. */
16903 type = get_die_type (die, cu);
16904 if (type)
16905 return type;
16906
16907 type = lookup_pointer_type (target_type);
16908
16909 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16910 if (attr_byte_size)
16911 byte_size = DW_UNSND (attr_byte_size);
16912 else
16913 byte_size = cu_header->addr_size;
16914
16915 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16916 if (attr_address_class)
16917 addr_class = DW_UNSND (attr_address_class);
16918 else
16919 addr_class = DW_ADDR_none;
16920
16921 /* If the pointer size or address class is different than the
16922 default, create a type variant marked as such and set the
16923 length accordingly. */
16924 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
16925 {
16926 if (gdbarch_address_class_type_flags_p (gdbarch))
16927 {
16928 int type_flags;
16929
16930 type_flags = gdbarch_address_class_type_flags
16931 (gdbarch, byte_size, addr_class);
16932 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16933 == 0);
16934 type = make_type_with_address_space (type, type_flags);
16935 }
16936 else if (TYPE_LENGTH (type) != byte_size)
16937 {
16938 complaint (&symfile_complaints,
16939 _("invalid pointer size %d"), byte_size);
16940 }
16941 else
16942 {
16943 /* Should we also complain about unhandled address classes? */
16944 }
16945 }
16946
16947 TYPE_LENGTH (type) = byte_size;
16948 return set_die_type (die, type, cu);
16949 }
16950
16951 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16952 the user defined type vector. */
16953
16954 static struct type *
16955 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16956 {
16957 struct type *type;
16958 struct type *to_type;
16959 struct type *domain;
16960
16961 to_type = die_type (die, cu);
16962 domain = die_containing_type (die, cu);
16963
16964 /* The calls above may have already set the type for this DIE. */
16965 type = get_die_type (die, cu);
16966 if (type)
16967 return type;
16968
16969 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16970 type = lookup_methodptr_type (to_type);
16971 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16972 {
16973 struct type *new_type
16974 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16975
16976 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16977 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16978 TYPE_VARARGS (to_type));
16979 type = lookup_methodptr_type (new_type);
16980 }
16981 else
16982 type = lookup_memberptr_type (to_type, domain);
16983
16984 return set_die_type (die, type, cu);
16985 }
16986
16987 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16988 the user defined type vector. */
16989
16990 static struct type *
16991 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16992 enum type_code refcode)
16993 {
16994 struct comp_unit_head *cu_header = &cu->header;
16995 struct type *type, *target_type;
16996 struct attribute *attr;
16997
16998 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16999
17000 target_type = die_type (die, cu);
17001
17002 /* The die_type call above may have already set the type for this DIE. */
17003 type = get_die_type (die, cu);
17004 if (type)
17005 return type;
17006
17007 type = lookup_reference_type (target_type, refcode);
17008 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17009 if (attr)
17010 {
17011 TYPE_LENGTH (type) = DW_UNSND (attr);
17012 }
17013 else
17014 {
17015 TYPE_LENGTH (type) = cu_header->addr_size;
17016 }
17017 return set_die_type (die, type, cu);
17018 }
17019
17020 /* Add the given cv-qualifiers to the element type of the array. GCC
17021 outputs DWARF type qualifiers that apply to an array, not the
17022 element type. But GDB relies on the array element type to carry
17023 the cv-qualifiers. This mimics section 6.7.3 of the C99
17024 specification. */
17025
17026 static struct type *
17027 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17028 struct type *base_type, int cnst, int voltl)
17029 {
17030 struct type *el_type, *inner_array;
17031
17032 base_type = copy_type (base_type);
17033 inner_array = base_type;
17034
17035 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17036 {
17037 TYPE_TARGET_TYPE (inner_array) =
17038 copy_type (TYPE_TARGET_TYPE (inner_array));
17039 inner_array = TYPE_TARGET_TYPE (inner_array);
17040 }
17041
17042 el_type = TYPE_TARGET_TYPE (inner_array);
17043 cnst |= TYPE_CONST (el_type);
17044 voltl |= TYPE_VOLATILE (el_type);
17045 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17046
17047 return set_die_type (die, base_type, cu);
17048 }
17049
17050 static struct type *
17051 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17052 {
17053 struct type *base_type, *cv_type;
17054
17055 base_type = die_type (die, cu);
17056
17057 /* The die_type call above may have already set the type for this DIE. */
17058 cv_type = get_die_type (die, cu);
17059 if (cv_type)
17060 return cv_type;
17061
17062 /* In case the const qualifier is applied to an array type, the element type
17063 is so qualified, not the array type (section 6.7.3 of C99). */
17064 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17065 return add_array_cv_type (die, cu, base_type, 1, 0);
17066
17067 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17068 return set_die_type (die, cv_type, cu);
17069 }
17070
17071 static struct type *
17072 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17073 {
17074 struct type *base_type, *cv_type;
17075
17076 base_type = die_type (die, cu);
17077
17078 /* The die_type call above may have already set the type for this DIE. */
17079 cv_type = get_die_type (die, cu);
17080 if (cv_type)
17081 return cv_type;
17082
17083 /* In case the volatile qualifier is applied to an array type, the
17084 element type is so qualified, not the array type (section 6.7.3
17085 of C99). */
17086 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17087 return add_array_cv_type (die, cu, base_type, 0, 1);
17088
17089 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17090 return set_die_type (die, cv_type, cu);
17091 }
17092
17093 /* Handle DW_TAG_restrict_type. */
17094
17095 static struct type *
17096 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17097 {
17098 struct type *base_type, *cv_type;
17099
17100 base_type = die_type (die, cu);
17101
17102 /* The die_type call above may have already set the type for this DIE. */
17103 cv_type = get_die_type (die, cu);
17104 if (cv_type)
17105 return cv_type;
17106
17107 cv_type = make_restrict_type (base_type);
17108 return set_die_type (die, cv_type, cu);
17109 }
17110
17111 /* Handle DW_TAG_atomic_type. */
17112
17113 static struct type *
17114 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17115 {
17116 struct type *base_type, *cv_type;
17117
17118 base_type = die_type (die, cu);
17119
17120 /* The die_type call above may have already set the type for this DIE. */
17121 cv_type = get_die_type (die, cu);
17122 if (cv_type)
17123 return cv_type;
17124
17125 cv_type = make_atomic_type (base_type);
17126 return set_die_type (die, cv_type, cu);
17127 }
17128
17129 /* Extract all information from a DW_TAG_string_type DIE and add to
17130 the user defined type vector. It isn't really a user defined type,
17131 but it behaves like one, with other DIE's using an AT_user_def_type
17132 attribute to reference it. */
17133
17134 static struct type *
17135 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17136 {
17137 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17138 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17139 struct type *type, *range_type, *index_type, *char_type;
17140 struct attribute *attr;
17141 unsigned int length;
17142
17143 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17144 if (attr)
17145 {
17146 length = DW_UNSND (attr);
17147 }
17148 else
17149 {
17150 /* Check for the DW_AT_byte_size attribute. */
17151 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17152 if (attr)
17153 {
17154 length = DW_UNSND (attr);
17155 }
17156 else
17157 {
17158 length = 1;
17159 }
17160 }
17161
17162 index_type = objfile_type (objfile)->builtin_int;
17163 range_type = create_static_range_type (NULL, index_type, 1, length);
17164 char_type = language_string_char_type (cu->language_defn, gdbarch);
17165 type = create_string_type (NULL, char_type, range_type);
17166
17167 return set_die_type (die, type, cu);
17168 }
17169
17170 /* Assuming that DIE corresponds to a function, returns nonzero
17171 if the function is prototyped. */
17172
17173 static int
17174 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17175 {
17176 struct attribute *attr;
17177
17178 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17179 if (attr && (DW_UNSND (attr) != 0))
17180 return 1;
17181
17182 /* The DWARF standard implies that the DW_AT_prototyped attribute
17183 is only meaninful for C, but the concept also extends to other
17184 languages that allow unprototyped functions (Eg: Objective C).
17185 For all other languages, assume that functions are always
17186 prototyped. */
17187 if (cu->language != language_c
17188 && cu->language != language_objc
17189 && cu->language != language_opencl)
17190 return 1;
17191
17192 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17193 prototyped and unprototyped functions; default to prototyped,
17194 since that is more common in modern code (and RealView warns
17195 about unprototyped functions). */
17196 if (producer_is_realview (cu->producer))
17197 return 1;
17198
17199 return 0;
17200 }
17201
17202 /* Handle DIES due to C code like:
17203
17204 struct foo
17205 {
17206 int (*funcp)(int a, long l);
17207 int b;
17208 };
17209
17210 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17211
17212 static struct type *
17213 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17214 {
17215 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17216 struct type *type; /* Type that this function returns. */
17217 struct type *ftype; /* Function that returns above type. */
17218 struct attribute *attr;
17219
17220 type = die_type (die, cu);
17221
17222 /* The die_type call above may have already set the type for this DIE. */
17223 ftype = get_die_type (die, cu);
17224 if (ftype)
17225 return ftype;
17226
17227 ftype = lookup_function_type (type);
17228
17229 if (prototyped_function_p (die, cu))
17230 TYPE_PROTOTYPED (ftype) = 1;
17231
17232 /* Store the calling convention in the type if it's available in
17233 the subroutine die. Otherwise set the calling convention to
17234 the default value DW_CC_normal. */
17235 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17236 if (attr)
17237 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17238 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17239 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17240 else
17241 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17242
17243 /* Record whether the function returns normally to its caller or not
17244 if the DWARF producer set that information. */
17245 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17246 if (attr && (DW_UNSND (attr) != 0))
17247 TYPE_NO_RETURN (ftype) = 1;
17248
17249 /* We need to add the subroutine type to the die immediately so
17250 we don't infinitely recurse when dealing with parameters
17251 declared as the same subroutine type. */
17252 set_die_type (die, ftype, cu);
17253
17254 if (die->child != NULL)
17255 {
17256 struct type *void_type = objfile_type (objfile)->builtin_void;
17257 struct die_info *child_die;
17258 int nparams, iparams;
17259
17260 /* Count the number of parameters.
17261 FIXME: GDB currently ignores vararg functions, but knows about
17262 vararg member functions. */
17263 nparams = 0;
17264 child_die = die->child;
17265 while (child_die && child_die->tag)
17266 {
17267 if (child_die->tag == DW_TAG_formal_parameter)
17268 nparams++;
17269 else if (child_die->tag == DW_TAG_unspecified_parameters)
17270 TYPE_VARARGS (ftype) = 1;
17271 child_die = sibling_die (child_die);
17272 }
17273
17274 /* Allocate storage for parameters and fill them in. */
17275 TYPE_NFIELDS (ftype) = nparams;
17276 TYPE_FIELDS (ftype) = (struct field *)
17277 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17278
17279 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17280 even if we error out during the parameters reading below. */
17281 for (iparams = 0; iparams < nparams; iparams++)
17282 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17283
17284 iparams = 0;
17285 child_die = die->child;
17286 while (child_die && child_die->tag)
17287 {
17288 if (child_die->tag == DW_TAG_formal_parameter)
17289 {
17290 struct type *arg_type;
17291
17292 /* DWARF version 2 has no clean way to discern C++
17293 static and non-static member functions. G++ helps
17294 GDB by marking the first parameter for non-static
17295 member functions (which is the this pointer) as
17296 artificial. We pass this information to
17297 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17298
17299 DWARF version 3 added DW_AT_object_pointer, which GCC
17300 4.5 does not yet generate. */
17301 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17302 if (attr)
17303 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17304 else
17305 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17306 arg_type = die_type (child_die, cu);
17307
17308 /* RealView does not mark THIS as const, which the testsuite
17309 expects. GCC marks THIS as const in method definitions,
17310 but not in the class specifications (GCC PR 43053). */
17311 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17312 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17313 {
17314 int is_this = 0;
17315 struct dwarf2_cu *arg_cu = cu;
17316 const char *name = dwarf2_name (child_die, cu);
17317
17318 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17319 if (attr)
17320 {
17321 /* If the compiler emits this, use it. */
17322 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17323 is_this = 1;
17324 }
17325 else if (name && strcmp (name, "this") == 0)
17326 /* Function definitions will have the argument names. */
17327 is_this = 1;
17328 else if (name == NULL && iparams == 0)
17329 /* Declarations may not have the names, so like
17330 elsewhere in GDB, assume an artificial first
17331 argument is "this". */
17332 is_this = 1;
17333
17334 if (is_this)
17335 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17336 arg_type, 0);
17337 }
17338
17339 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17340 iparams++;
17341 }
17342 child_die = sibling_die (child_die);
17343 }
17344 }
17345
17346 return ftype;
17347 }
17348
17349 static struct type *
17350 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17351 {
17352 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17353 const char *name = NULL;
17354 struct type *this_type, *target_type;
17355
17356 name = dwarf2_full_name (NULL, die, cu);
17357 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17358 TYPE_TARGET_STUB (this_type) = 1;
17359 set_die_type (die, this_type, cu);
17360 target_type = die_type (die, cu);
17361 if (target_type != this_type)
17362 TYPE_TARGET_TYPE (this_type) = target_type;
17363 else
17364 {
17365 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17366 spec and cause infinite loops in GDB. */
17367 complaint (&symfile_complaints,
17368 _("Self-referential DW_TAG_typedef "
17369 "- DIE at %s [in module %s]"),
17370 sect_offset_str (die->sect_off), objfile_name (objfile));
17371 TYPE_TARGET_TYPE (this_type) = NULL;
17372 }
17373 return this_type;
17374 }
17375
17376 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17377 (which may be different from NAME) to the architecture back-end to allow
17378 it to guess the correct format if necessary. */
17379
17380 static struct type *
17381 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17382 const char *name_hint)
17383 {
17384 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17385 const struct floatformat **format;
17386 struct type *type;
17387
17388 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17389 if (format)
17390 type = init_float_type (objfile, bits, name, format);
17391 else
17392 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17393
17394 return type;
17395 }
17396
17397 /* Find a representation of a given base type and install
17398 it in the TYPE field of the die. */
17399
17400 static struct type *
17401 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17402 {
17403 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17404 struct type *type;
17405 struct attribute *attr;
17406 int encoding = 0, bits = 0;
17407 const char *name;
17408
17409 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17410 if (attr)
17411 {
17412 encoding = DW_UNSND (attr);
17413 }
17414 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17415 if (attr)
17416 {
17417 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17418 }
17419 name = dwarf2_name (die, cu);
17420 if (!name)
17421 {
17422 complaint (&symfile_complaints,
17423 _("DW_AT_name missing from DW_TAG_base_type"));
17424 }
17425
17426 switch (encoding)
17427 {
17428 case DW_ATE_address:
17429 /* Turn DW_ATE_address into a void * pointer. */
17430 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17431 type = init_pointer_type (objfile, bits, name, type);
17432 break;
17433 case DW_ATE_boolean:
17434 type = init_boolean_type (objfile, bits, 1, name);
17435 break;
17436 case DW_ATE_complex_float:
17437 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17438 type = init_complex_type (objfile, name, type);
17439 break;
17440 case DW_ATE_decimal_float:
17441 type = init_decfloat_type (objfile, bits, name);
17442 break;
17443 case DW_ATE_float:
17444 type = dwarf2_init_float_type (objfile, bits, name, name);
17445 break;
17446 case DW_ATE_signed:
17447 type = init_integer_type (objfile, bits, 0, name);
17448 break;
17449 case DW_ATE_unsigned:
17450 if (cu->language == language_fortran
17451 && name
17452 && startswith (name, "character("))
17453 type = init_character_type (objfile, bits, 1, name);
17454 else
17455 type = init_integer_type (objfile, bits, 1, name);
17456 break;
17457 case DW_ATE_signed_char:
17458 if (cu->language == language_ada || cu->language == language_m2
17459 || cu->language == language_pascal
17460 || cu->language == language_fortran)
17461 type = init_character_type (objfile, bits, 0, name);
17462 else
17463 type = init_integer_type (objfile, bits, 0, name);
17464 break;
17465 case DW_ATE_unsigned_char:
17466 if (cu->language == language_ada || cu->language == language_m2
17467 || cu->language == language_pascal
17468 || cu->language == language_fortran
17469 || cu->language == language_rust)
17470 type = init_character_type (objfile, bits, 1, name);
17471 else
17472 type = init_integer_type (objfile, bits, 1, name);
17473 break;
17474 case DW_ATE_UTF:
17475 {
17476 gdbarch *arch = get_objfile_arch (objfile);
17477
17478 if (bits == 16)
17479 type = builtin_type (arch)->builtin_char16;
17480 else if (bits == 32)
17481 type = builtin_type (arch)->builtin_char32;
17482 else
17483 {
17484 complaint (&symfile_complaints,
17485 _("unsupported DW_ATE_UTF bit size: '%d'"),
17486 bits);
17487 type = init_integer_type (objfile, bits, 1, name);
17488 }
17489 return set_die_type (die, type, cu);
17490 }
17491 break;
17492
17493 default:
17494 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17495 dwarf_type_encoding_name (encoding));
17496 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17497 break;
17498 }
17499
17500 if (name && strcmp (name, "char") == 0)
17501 TYPE_NOSIGN (type) = 1;
17502
17503 return set_die_type (die, type, cu);
17504 }
17505
17506 /* Parse dwarf attribute if it's a block, reference or constant and put the
17507 resulting value of the attribute into struct bound_prop.
17508 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17509
17510 static int
17511 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17512 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17513 {
17514 struct dwarf2_property_baton *baton;
17515 struct obstack *obstack
17516 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17517
17518 if (attr == NULL || prop == NULL)
17519 return 0;
17520
17521 if (attr_form_is_block (attr))
17522 {
17523 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17524 baton->referenced_type = NULL;
17525 baton->locexpr.per_cu = cu->per_cu;
17526 baton->locexpr.size = DW_BLOCK (attr)->size;
17527 baton->locexpr.data = DW_BLOCK (attr)->data;
17528 prop->data.baton = baton;
17529 prop->kind = PROP_LOCEXPR;
17530 gdb_assert (prop->data.baton != NULL);
17531 }
17532 else if (attr_form_is_ref (attr))
17533 {
17534 struct dwarf2_cu *target_cu = cu;
17535 struct die_info *target_die;
17536 struct attribute *target_attr;
17537
17538 target_die = follow_die_ref (die, attr, &target_cu);
17539 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17540 if (target_attr == NULL)
17541 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17542 target_cu);
17543 if (target_attr == NULL)
17544 return 0;
17545
17546 switch (target_attr->name)
17547 {
17548 case DW_AT_location:
17549 if (attr_form_is_section_offset (target_attr))
17550 {
17551 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17552 baton->referenced_type = die_type (target_die, target_cu);
17553 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17554 prop->data.baton = baton;
17555 prop->kind = PROP_LOCLIST;
17556 gdb_assert (prop->data.baton != NULL);
17557 }
17558 else if (attr_form_is_block (target_attr))
17559 {
17560 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17561 baton->referenced_type = die_type (target_die, target_cu);
17562 baton->locexpr.per_cu = cu->per_cu;
17563 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17564 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17565 prop->data.baton = baton;
17566 prop->kind = PROP_LOCEXPR;
17567 gdb_assert (prop->data.baton != NULL);
17568 }
17569 else
17570 {
17571 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17572 "dynamic property");
17573 return 0;
17574 }
17575 break;
17576 case DW_AT_data_member_location:
17577 {
17578 LONGEST offset;
17579
17580 if (!handle_data_member_location (target_die, target_cu,
17581 &offset))
17582 return 0;
17583
17584 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17585 baton->referenced_type = read_type_die (target_die->parent,
17586 target_cu);
17587 baton->offset_info.offset = offset;
17588 baton->offset_info.type = die_type (target_die, target_cu);
17589 prop->data.baton = baton;
17590 prop->kind = PROP_ADDR_OFFSET;
17591 break;
17592 }
17593 }
17594 }
17595 else if (attr_form_is_constant (attr))
17596 {
17597 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17598 prop->kind = PROP_CONST;
17599 }
17600 else
17601 {
17602 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17603 dwarf2_name (die, cu));
17604 return 0;
17605 }
17606
17607 return 1;
17608 }
17609
17610 /* Read the given DW_AT_subrange DIE. */
17611
17612 static struct type *
17613 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17614 {
17615 struct type *base_type, *orig_base_type;
17616 struct type *range_type;
17617 struct attribute *attr;
17618 struct dynamic_prop low, high;
17619 int low_default_is_valid;
17620 int high_bound_is_count = 0;
17621 const char *name;
17622 LONGEST negative_mask;
17623
17624 orig_base_type = die_type (die, cu);
17625 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17626 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17627 creating the range type, but we use the result of check_typedef
17628 when examining properties of the type. */
17629 base_type = check_typedef (orig_base_type);
17630
17631 /* The die_type call above may have already set the type for this DIE. */
17632 range_type = get_die_type (die, cu);
17633 if (range_type)
17634 return range_type;
17635
17636 low.kind = PROP_CONST;
17637 high.kind = PROP_CONST;
17638 high.data.const_val = 0;
17639
17640 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17641 omitting DW_AT_lower_bound. */
17642 switch (cu->language)
17643 {
17644 case language_c:
17645 case language_cplus:
17646 low.data.const_val = 0;
17647 low_default_is_valid = 1;
17648 break;
17649 case language_fortran:
17650 low.data.const_val = 1;
17651 low_default_is_valid = 1;
17652 break;
17653 case language_d:
17654 case language_objc:
17655 case language_rust:
17656 low.data.const_val = 0;
17657 low_default_is_valid = (cu->header.version >= 4);
17658 break;
17659 case language_ada:
17660 case language_m2:
17661 case language_pascal:
17662 low.data.const_val = 1;
17663 low_default_is_valid = (cu->header.version >= 4);
17664 break;
17665 default:
17666 low.data.const_val = 0;
17667 low_default_is_valid = 0;
17668 break;
17669 }
17670
17671 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17672 if (attr)
17673 attr_to_dynamic_prop (attr, die, cu, &low);
17674 else if (!low_default_is_valid)
17675 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17676 "- DIE at %s [in module %s]"),
17677 sect_offset_str (die->sect_off),
17678 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17679
17680 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17681 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17682 {
17683 attr = dwarf2_attr (die, DW_AT_count, cu);
17684 if (attr_to_dynamic_prop (attr, die, cu, &high))
17685 {
17686 /* If bounds are constant do the final calculation here. */
17687 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17688 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17689 else
17690 high_bound_is_count = 1;
17691 }
17692 }
17693
17694 /* Dwarf-2 specifications explicitly allows to create subrange types
17695 without specifying a base type.
17696 In that case, the base type must be set to the type of
17697 the lower bound, upper bound or count, in that order, if any of these
17698 three attributes references an object that has a type.
17699 If no base type is found, the Dwarf-2 specifications say that
17700 a signed integer type of size equal to the size of an address should
17701 be used.
17702 For the following C code: `extern char gdb_int [];'
17703 GCC produces an empty range DIE.
17704 FIXME: muller/2010-05-28: Possible references to object for low bound,
17705 high bound or count are not yet handled by this code. */
17706 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17707 {
17708 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17709 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17710 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17711 struct type *int_type = objfile_type (objfile)->builtin_int;
17712
17713 /* Test "int", "long int", and "long long int" objfile types,
17714 and select the first one having a size above or equal to the
17715 architecture address size. */
17716 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17717 base_type = int_type;
17718 else
17719 {
17720 int_type = objfile_type (objfile)->builtin_long;
17721 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17722 base_type = int_type;
17723 else
17724 {
17725 int_type = objfile_type (objfile)->builtin_long_long;
17726 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17727 base_type = int_type;
17728 }
17729 }
17730 }
17731
17732 /* Normally, the DWARF producers are expected to use a signed
17733 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17734 But this is unfortunately not always the case, as witnessed
17735 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17736 is used instead. To work around that ambiguity, we treat
17737 the bounds as signed, and thus sign-extend their values, when
17738 the base type is signed. */
17739 negative_mask =
17740 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17741 if (low.kind == PROP_CONST
17742 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17743 low.data.const_val |= negative_mask;
17744 if (high.kind == PROP_CONST
17745 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17746 high.data.const_val |= negative_mask;
17747
17748 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17749
17750 if (high_bound_is_count)
17751 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17752
17753 /* Ada expects an empty array on no boundary attributes. */
17754 if (attr == NULL && cu->language != language_ada)
17755 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17756
17757 name = dwarf2_name (die, cu);
17758 if (name)
17759 TYPE_NAME (range_type) = name;
17760
17761 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17762 if (attr)
17763 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17764
17765 set_die_type (die, range_type, cu);
17766
17767 /* set_die_type should be already done. */
17768 set_descriptive_type (range_type, die, cu);
17769
17770 return range_type;
17771 }
17772
17773 static struct type *
17774 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17775 {
17776 struct type *type;
17777
17778 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17779 NULL);
17780 TYPE_NAME (type) = dwarf2_name (die, cu);
17781
17782 /* In Ada, an unspecified type is typically used when the description
17783 of the type is defered to a different unit. When encountering
17784 such a type, we treat it as a stub, and try to resolve it later on,
17785 when needed. */
17786 if (cu->language == language_ada)
17787 TYPE_STUB (type) = 1;
17788
17789 return set_die_type (die, type, cu);
17790 }
17791
17792 /* Read a single die and all its descendents. Set the die's sibling
17793 field to NULL; set other fields in the die correctly, and set all
17794 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17795 location of the info_ptr after reading all of those dies. PARENT
17796 is the parent of the die in question. */
17797
17798 static struct die_info *
17799 read_die_and_children (const struct die_reader_specs *reader,
17800 const gdb_byte *info_ptr,
17801 const gdb_byte **new_info_ptr,
17802 struct die_info *parent)
17803 {
17804 struct die_info *die;
17805 const gdb_byte *cur_ptr;
17806 int has_children;
17807
17808 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17809 if (die == NULL)
17810 {
17811 *new_info_ptr = cur_ptr;
17812 return NULL;
17813 }
17814 store_in_ref_table (die, reader->cu);
17815
17816 if (has_children)
17817 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17818 else
17819 {
17820 die->child = NULL;
17821 *new_info_ptr = cur_ptr;
17822 }
17823
17824 die->sibling = NULL;
17825 die->parent = parent;
17826 return die;
17827 }
17828
17829 /* Read a die, all of its descendents, and all of its siblings; set
17830 all of the fields of all of the dies correctly. Arguments are as
17831 in read_die_and_children. */
17832
17833 static struct die_info *
17834 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17835 const gdb_byte *info_ptr,
17836 const gdb_byte **new_info_ptr,
17837 struct die_info *parent)
17838 {
17839 struct die_info *first_die, *last_sibling;
17840 const gdb_byte *cur_ptr;
17841
17842 cur_ptr = info_ptr;
17843 first_die = last_sibling = NULL;
17844
17845 while (1)
17846 {
17847 struct die_info *die
17848 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17849
17850 if (die == NULL)
17851 {
17852 *new_info_ptr = cur_ptr;
17853 return first_die;
17854 }
17855
17856 if (!first_die)
17857 first_die = die;
17858 else
17859 last_sibling->sibling = die;
17860
17861 last_sibling = die;
17862 }
17863 }
17864
17865 /* Read a die, all of its descendents, and all of its siblings; set
17866 all of the fields of all of the dies correctly. Arguments are as
17867 in read_die_and_children.
17868 This the main entry point for reading a DIE and all its children. */
17869
17870 static struct die_info *
17871 read_die_and_siblings (const struct die_reader_specs *reader,
17872 const gdb_byte *info_ptr,
17873 const gdb_byte **new_info_ptr,
17874 struct die_info *parent)
17875 {
17876 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17877 new_info_ptr, parent);
17878
17879 if (dwarf_die_debug)
17880 {
17881 fprintf_unfiltered (gdb_stdlog,
17882 "Read die from %s@0x%x of %s:\n",
17883 get_section_name (reader->die_section),
17884 (unsigned) (info_ptr - reader->die_section->buffer),
17885 bfd_get_filename (reader->abfd));
17886 dump_die (die, dwarf_die_debug);
17887 }
17888
17889 return die;
17890 }
17891
17892 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17893 attributes.
17894 The caller is responsible for filling in the extra attributes
17895 and updating (*DIEP)->num_attrs.
17896 Set DIEP to point to a newly allocated die with its information,
17897 except for its child, sibling, and parent fields.
17898 Set HAS_CHILDREN to tell whether the die has children or not. */
17899
17900 static const gdb_byte *
17901 read_full_die_1 (const struct die_reader_specs *reader,
17902 struct die_info **diep, const gdb_byte *info_ptr,
17903 int *has_children, int num_extra_attrs)
17904 {
17905 unsigned int abbrev_number, bytes_read, i;
17906 struct abbrev_info *abbrev;
17907 struct die_info *die;
17908 struct dwarf2_cu *cu = reader->cu;
17909 bfd *abfd = reader->abfd;
17910
17911 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17912 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17913 info_ptr += bytes_read;
17914 if (!abbrev_number)
17915 {
17916 *diep = NULL;
17917 *has_children = 0;
17918 return info_ptr;
17919 }
17920
17921 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17922 if (!abbrev)
17923 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17924 abbrev_number,
17925 bfd_get_filename (abfd));
17926
17927 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17928 die->sect_off = sect_off;
17929 die->tag = abbrev->tag;
17930 die->abbrev = abbrev_number;
17931
17932 /* Make the result usable.
17933 The caller needs to update num_attrs after adding the extra
17934 attributes. */
17935 die->num_attrs = abbrev->num_attrs;
17936
17937 for (i = 0; i < abbrev->num_attrs; ++i)
17938 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17939 info_ptr);
17940
17941 *diep = die;
17942 *has_children = abbrev->has_children;
17943 return info_ptr;
17944 }
17945
17946 /* Read a die and all its attributes.
17947 Set DIEP to point to a newly allocated die with its information,
17948 except for its child, sibling, and parent fields.
17949 Set HAS_CHILDREN to tell whether the die has children or not. */
17950
17951 static const gdb_byte *
17952 read_full_die (const struct die_reader_specs *reader,
17953 struct die_info **diep, const gdb_byte *info_ptr,
17954 int *has_children)
17955 {
17956 const gdb_byte *result;
17957
17958 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17959
17960 if (dwarf_die_debug)
17961 {
17962 fprintf_unfiltered (gdb_stdlog,
17963 "Read die from %s@0x%x of %s:\n",
17964 get_section_name (reader->die_section),
17965 (unsigned) (info_ptr - reader->die_section->buffer),
17966 bfd_get_filename (reader->abfd));
17967 dump_die (*diep, dwarf_die_debug);
17968 }
17969
17970 return result;
17971 }
17972 \f
17973 /* Abbreviation tables.
17974
17975 In DWARF version 2, the description of the debugging information is
17976 stored in a separate .debug_abbrev section. Before we read any
17977 dies from a section we read in all abbreviations and install them
17978 in a hash table. */
17979
17980 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17981
17982 struct abbrev_info *
17983 abbrev_table::alloc_abbrev ()
17984 {
17985 struct abbrev_info *abbrev;
17986
17987 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
17988 memset (abbrev, 0, sizeof (struct abbrev_info));
17989
17990 return abbrev;
17991 }
17992
17993 /* Add an abbreviation to the table. */
17994
17995 void
17996 abbrev_table::add_abbrev (unsigned int abbrev_number,
17997 struct abbrev_info *abbrev)
17998 {
17999 unsigned int hash_number;
18000
18001 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18002 abbrev->next = m_abbrevs[hash_number];
18003 m_abbrevs[hash_number] = abbrev;
18004 }
18005
18006 /* Look up an abbrev in the table.
18007 Returns NULL if the abbrev is not found. */
18008
18009 struct abbrev_info *
18010 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18011 {
18012 unsigned int hash_number;
18013 struct abbrev_info *abbrev;
18014
18015 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18016 abbrev = m_abbrevs[hash_number];
18017
18018 while (abbrev)
18019 {
18020 if (abbrev->number == abbrev_number)
18021 return abbrev;
18022 abbrev = abbrev->next;
18023 }
18024 return NULL;
18025 }
18026
18027 /* Read in an abbrev table. */
18028
18029 static abbrev_table_up
18030 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18031 struct dwarf2_section_info *section,
18032 sect_offset sect_off)
18033 {
18034 struct objfile *objfile = dwarf2_per_objfile->objfile;
18035 bfd *abfd = get_section_bfd_owner (section);
18036 const gdb_byte *abbrev_ptr;
18037 struct abbrev_info *cur_abbrev;
18038 unsigned int abbrev_number, bytes_read, abbrev_name;
18039 unsigned int abbrev_form;
18040 struct attr_abbrev *cur_attrs;
18041 unsigned int allocated_attrs;
18042
18043 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18044
18045 dwarf2_read_section (objfile, section);
18046 abbrev_ptr = section->buffer + to_underlying (sect_off);
18047 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18048 abbrev_ptr += bytes_read;
18049
18050 allocated_attrs = ATTR_ALLOC_CHUNK;
18051 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18052
18053 /* Loop until we reach an abbrev number of 0. */
18054 while (abbrev_number)
18055 {
18056 cur_abbrev = abbrev_table->alloc_abbrev ();
18057
18058 /* read in abbrev header */
18059 cur_abbrev->number = abbrev_number;
18060 cur_abbrev->tag
18061 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18062 abbrev_ptr += bytes_read;
18063 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18064 abbrev_ptr += 1;
18065
18066 /* now read in declarations */
18067 for (;;)
18068 {
18069 LONGEST implicit_const;
18070
18071 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18072 abbrev_ptr += bytes_read;
18073 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18074 abbrev_ptr += bytes_read;
18075 if (abbrev_form == DW_FORM_implicit_const)
18076 {
18077 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18078 &bytes_read);
18079 abbrev_ptr += bytes_read;
18080 }
18081 else
18082 {
18083 /* Initialize it due to a false compiler warning. */
18084 implicit_const = -1;
18085 }
18086
18087 if (abbrev_name == 0)
18088 break;
18089
18090 if (cur_abbrev->num_attrs == allocated_attrs)
18091 {
18092 allocated_attrs += ATTR_ALLOC_CHUNK;
18093 cur_attrs
18094 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18095 }
18096
18097 cur_attrs[cur_abbrev->num_attrs].name
18098 = (enum dwarf_attribute) abbrev_name;
18099 cur_attrs[cur_abbrev->num_attrs].form
18100 = (enum dwarf_form) abbrev_form;
18101 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18102 ++cur_abbrev->num_attrs;
18103 }
18104
18105 cur_abbrev->attrs =
18106 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18107 cur_abbrev->num_attrs);
18108 memcpy (cur_abbrev->attrs, cur_attrs,
18109 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18110
18111 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18112
18113 /* Get next abbreviation.
18114 Under Irix6 the abbreviations for a compilation unit are not
18115 always properly terminated with an abbrev number of 0.
18116 Exit loop if we encounter an abbreviation which we have
18117 already read (which means we are about to read the abbreviations
18118 for the next compile unit) or if the end of the abbreviation
18119 table is reached. */
18120 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18121 break;
18122 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18123 abbrev_ptr += bytes_read;
18124 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18125 break;
18126 }
18127
18128 xfree (cur_attrs);
18129 return abbrev_table;
18130 }
18131
18132 /* Returns nonzero if TAG represents a type that we might generate a partial
18133 symbol for. */
18134
18135 static int
18136 is_type_tag_for_partial (int tag)
18137 {
18138 switch (tag)
18139 {
18140 #if 0
18141 /* Some types that would be reasonable to generate partial symbols for,
18142 that we don't at present. */
18143 case DW_TAG_array_type:
18144 case DW_TAG_file_type:
18145 case DW_TAG_ptr_to_member_type:
18146 case DW_TAG_set_type:
18147 case DW_TAG_string_type:
18148 case DW_TAG_subroutine_type:
18149 #endif
18150 case DW_TAG_base_type:
18151 case DW_TAG_class_type:
18152 case DW_TAG_interface_type:
18153 case DW_TAG_enumeration_type:
18154 case DW_TAG_structure_type:
18155 case DW_TAG_subrange_type:
18156 case DW_TAG_typedef:
18157 case DW_TAG_union_type:
18158 return 1;
18159 default:
18160 return 0;
18161 }
18162 }
18163
18164 /* Load all DIEs that are interesting for partial symbols into memory. */
18165
18166 static struct partial_die_info *
18167 load_partial_dies (const struct die_reader_specs *reader,
18168 const gdb_byte *info_ptr, int building_psymtab)
18169 {
18170 struct dwarf2_cu *cu = reader->cu;
18171 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18172 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18173 unsigned int bytes_read;
18174 unsigned int load_all = 0;
18175 int nesting_level = 1;
18176
18177 parent_die = NULL;
18178 last_die = NULL;
18179
18180 gdb_assert (cu->per_cu != NULL);
18181 if (cu->per_cu->load_all_dies)
18182 load_all = 1;
18183
18184 cu->partial_dies
18185 = htab_create_alloc_ex (cu->header.length / 12,
18186 partial_die_hash,
18187 partial_die_eq,
18188 NULL,
18189 &cu->comp_unit_obstack,
18190 hashtab_obstack_allocate,
18191 dummy_obstack_deallocate);
18192
18193 while (1)
18194 {
18195 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18196
18197 /* A NULL abbrev means the end of a series of children. */
18198 if (abbrev == NULL)
18199 {
18200 if (--nesting_level == 0)
18201 return first_die;
18202
18203 info_ptr += bytes_read;
18204 last_die = parent_die;
18205 parent_die = parent_die->die_parent;
18206 continue;
18207 }
18208
18209 /* Check for template arguments. We never save these; if
18210 they're seen, we just mark the parent, and go on our way. */
18211 if (parent_die != NULL
18212 && cu->language == language_cplus
18213 && (abbrev->tag == DW_TAG_template_type_param
18214 || abbrev->tag == DW_TAG_template_value_param))
18215 {
18216 parent_die->has_template_arguments = 1;
18217
18218 if (!load_all)
18219 {
18220 /* We don't need a partial DIE for the template argument. */
18221 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18222 continue;
18223 }
18224 }
18225
18226 /* We only recurse into c++ subprograms looking for template arguments.
18227 Skip their other children. */
18228 if (!load_all
18229 && cu->language == language_cplus
18230 && parent_die != NULL
18231 && parent_die->tag == DW_TAG_subprogram)
18232 {
18233 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18234 continue;
18235 }
18236
18237 /* Check whether this DIE is interesting enough to save. Normally
18238 we would not be interested in members here, but there may be
18239 later variables referencing them via DW_AT_specification (for
18240 static members). */
18241 if (!load_all
18242 && !is_type_tag_for_partial (abbrev->tag)
18243 && abbrev->tag != DW_TAG_constant
18244 && abbrev->tag != DW_TAG_enumerator
18245 && abbrev->tag != DW_TAG_subprogram
18246 && abbrev->tag != DW_TAG_inlined_subroutine
18247 && abbrev->tag != DW_TAG_lexical_block
18248 && abbrev->tag != DW_TAG_variable
18249 && abbrev->tag != DW_TAG_namespace
18250 && abbrev->tag != DW_TAG_module
18251 && abbrev->tag != DW_TAG_member
18252 && abbrev->tag != DW_TAG_imported_unit
18253 && abbrev->tag != DW_TAG_imported_declaration)
18254 {
18255 /* Otherwise we skip to the next sibling, if any. */
18256 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18257 continue;
18258 }
18259
18260 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18261 abbrev);
18262
18263 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18264
18265 /* This two-pass algorithm for processing partial symbols has a
18266 high cost in cache pressure. Thus, handle some simple cases
18267 here which cover the majority of C partial symbols. DIEs
18268 which neither have specification tags in them, nor could have
18269 specification tags elsewhere pointing at them, can simply be
18270 processed and discarded.
18271
18272 This segment is also optional; scan_partial_symbols and
18273 add_partial_symbol will handle these DIEs if we chain
18274 them in normally. When compilers which do not emit large
18275 quantities of duplicate debug information are more common,
18276 this code can probably be removed. */
18277
18278 /* Any complete simple types at the top level (pretty much all
18279 of them, for a language without namespaces), can be processed
18280 directly. */
18281 if (parent_die == NULL
18282 && pdi.has_specification == 0
18283 && pdi.is_declaration == 0
18284 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18285 || pdi.tag == DW_TAG_base_type
18286 || pdi.tag == DW_TAG_subrange_type))
18287 {
18288 if (building_psymtab && pdi.name != NULL)
18289 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18290 VAR_DOMAIN, LOC_TYPEDEF,
18291 &objfile->static_psymbols,
18292 0, cu->language, objfile);
18293 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18294 continue;
18295 }
18296
18297 /* The exception for DW_TAG_typedef with has_children above is
18298 a workaround of GCC PR debug/47510. In the case of this complaint
18299 type_name_no_tag_or_error will error on such types later.
18300
18301 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18302 it could not find the child DIEs referenced later, this is checked
18303 above. In correct DWARF DW_TAG_typedef should have no children. */
18304
18305 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18306 complaint (&symfile_complaints,
18307 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18308 "- DIE at %s [in module %s]"),
18309 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18310
18311 /* If we're at the second level, and we're an enumerator, and
18312 our parent has no specification (meaning possibly lives in a
18313 namespace elsewhere), then we can add the partial symbol now
18314 instead of queueing it. */
18315 if (pdi.tag == DW_TAG_enumerator
18316 && parent_die != NULL
18317 && parent_die->die_parent == NULL
18318 && parent_die->tag == DW_TAG_enumeration_type
18319 && parent_die->has_specification == 0)
18320 {
18321 if (pdi.name == NULL)
18322 complaint (&symfile_complaints,
18323 _("malformed enumerator DIE ignored"));
18324 else if (building_psymtab)
18325 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18326 VAR_DOMAIN, LOC_CONST,
18327 cu->language == language_cplus
18328 ? &objfile->global_psymbols
18329 : &objfile->static_psymbols,
18330 0, cu->language, objfile);
18331
18332 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18333 continue;
18334 }
18335
18336 struct partial_die_info *part_die
18337 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18338
18339 /* We'll save this DIE so link it in. */
18340 part_die->die_parent = parent_die;
18341 part_die->die_sibling = NULL;
18342 part_die->die_child = NULL;
18343
18344 if (last_die && last_die == parent_die)
18345 last_die->die_child = part_die;
18346 else if (last_die)
18347 last_die->die_sibling = part_die;
18348
18349 last_die = part_die;
18350
18351 if (first_die == NULL)
18352 first_die = part_die;
18353
18354 /* Maybe add the DIE to the hash table. Not all DIEs that we
18355 find interesting need to be in the hash table, because we
18356 also have the parent/sibling/child chains; only those that we
18357 might refer to by offset later during partial symbol reading.
18358
18359 For now this means things that might have be the target of a
18360 DW_AT_specification, DW_AT_abstract_origin, or
18361 DW_AT_extension. DW_AT_extension will refer only to
18362 namespaces; DW_AT_abstract_origin refers to functions (and
18363 many things under the function DIE, but we do not recurse
18364 into function DIEs during partial symbol reading) and
18365 possibly variables as well; DW_AT_specification refers to
18366 declarations. Declarations ought to have the DW_AT_declaration
18367 flag. It happens that GCC forgets to put it in sometimes, but
18368 only for functions, not for types.
18369
18370 Adding more things than necessary to the hash table is harmless
18371 except for the performance cost. Adding too few will result in
18372 wasted time in find_partial_die, when we reread the compilation
18373 unit with load_all_dies set. */
18374
18375 if (load_all
18376 || abbrev->tag == DW_TAG_constant
18377 || abbrev->tag == DW_TAG_subprogram
18378 || abbrev->tag == DW_TAG_variable
18379 || abbrev->tag == DW_TAG_namespace
18380 || part_die->is_declaration)
18381 {
18382 void **slot;
18383
18384 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18385 to_underlying (part_die->sect_off),
18386 INSERT);
18387 *slot = part_die;
18388 }
18389
18390 /* For some DIEs we want to follow their children (if any). For C
18391 we have no reason to follow the children of structures; for other
18392 languages we have to, so that we can get at method physnames
18393 to infer fully qualified class names, for DW_AT_specification,
18394 and for C++ template arguments. For C++, we also look one level
18395 inside functions to find template arguments (if the name of the
18396 function does not already contain the template arguments).
18397
18398 For Ada, we need to scan the children of subprograms and lexical
18399 blocks as well because Ada allows the definition of nested
18400 entities that could be interesting for the debugger, such as
18401 nested subprograms for instance. */
18402 if (last_die->has_children
18403 && (load_all
18404 || last_die->tag == DW_TAG_namespace
18405 || last_die->tag == DW_TAG_module
18406 || last_die->tag == DW_TAG_enumeration_type
18407 || (cu->language == language_cplus
18408 && last_die->tag == DW_TAG_subprogram
18409 && (last_die->name == NULL
18410 || strchr (last_die->name, '<') == NULL))
18411 || (cu->language != language_c
18412 && (last_die->tag == DW_TAG_class_type
18413 || last_die->tag == DW_TAG_interface_type
18414 || last_die->tag == DW_TAG_structure_type
18415 || last_die->tag == DW_TAG_union_type))
18416 || (cu->language == language_ada
18417 && (last_die->tag == DW_TAG_subprogram
18418 || last_die->tag == DW_TAG_lexical_block))))
18419 {
18420 nesting_level++;
18421 parent_die = last_die;
18422 continue;
18423 }
18424
18425 /* Otherwise we skip to the next sibling, if any. */
18426 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18427
18428 /* Back to the top, do it again. */
18429 }
18430 }
18431
18432 partial_die_info::partial_die_info (sect_offset sect_off_,
18433 struct abbrev_info *abbrev)
18434 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18435 {
18436 }
18437
18438 /* Read a minimal amount of information into the minimal die structure.
18439 INFO_PTR should point just after the initial uleb128 of a DIE. */
18440
18441 const gdb_byte *
18442 partial_die_info::read (const struct die_reader_specs *reader,
18443 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18444 {
18445 struct dwarf2_cu *cu = reader->cu;
18446 struct dwarf2_per_objfile *dwarf2_per_objfile
18447 = cu->per_cu->dwarf2_per_objfile;
18448 unsigned int i;
18449 int has_low_pc_attr = 0;
18450 int has_high_pc_attr = 0;
18451 int high_pc_relative = 0;
18452
18453 for (i = 0; i < abbrev.num_attrs; ++i)
18454 {
18455 struct attribute attr;
18456
18457 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18458
18459 /* Store the data if it is of an attribute we want to keep in a
18460 partial symbol table. */
18461 switch (attr.name)
18462 {
18463 case DW_AT_name:
18464 switch (tag)
18465 {
18466 case DW_TAG_compile_unit:
18467 case DW_TAG_partial_unit:
18468 case DW_TAG_type_unit:
18469 /* Compilation units have a DW_AT_name that is a filename, not
18470 a source language identifier. */
18471 case DW_TAG_enumeration_type:
18472 case DW_TAG_enumerator:
18473 /* These tags always have simple identifiers already; no need
18474 to canonicalize them. */
18475 name = DW_STRING (&attr);
18476 break;
18477 default:
18478 {
18479 struct objfile *objfile = dwarf2_per_objfile->objfile;
18480
18481 name
18482 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18483 &objfile->per_bfd->storage_obstack);
18484 }
18485 break;
18486 }
18487 break;
18488 case DW_AT_linkage_name:
18489 case DW_AT_MIPS_linkage_name:
18490 /* Note that both forms of linkage name might appear. We
18491 assume they will be the same, and we only store the last
18492 one we see. */
18493 if (cu->language == language_ada)
18494 name = DW_STRING (&attr);
18495 linkage_name = DW_STRING (&attr);
18496 break;
18497 case DW_AT_low_pc:
18498 has_low_pc_attr = 1;
18499 lowpc = attr_value_as_address (&attr);
18500 break;
18501 case DW_AT_high_pc:
18502 has_high_pc_attr = 1;
18503 highpc = attr_value_as_address (&attr);
18504 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18505 high_pc_relative = 1;
18506 break;
18507 case DW_AT_location:
18508 /* Support the .debug_loc offsets. */
18509 if (attr_form_is_block (&attr))
18510 {
18511 d.locdesc = DW_BLOCK (&attr);
18512 }
18513 else if (attr_form_is_section_offset (&attr))
18514 {
18515 dwarf2_complex_location_expr_complaint ();
18516 }
18517 else
18518 {
18519 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18520 "partial symbol information");
18521 }
18522 break;
18523 case DW_AT_external:
18524 is_external = DW_UNSND (&attr);
18525 break;
18526 case DW_AT_declaration:
18527 is_declaration = DW_UNSND (&attr);
18528 break;
18529 case DW_AT_type:
18530 has_type = 1;
18531 break;
18532 case DW_AT_abstract_origin:
18533 case DW_AT_specification:
18534 case DW_AT_extension:
18535 has_specification = 1;
18536 spec_offset = dwarf2_get_ref_die_offset (&attr);
18537 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18538 || cu->per_cu->is_dwz);
18539 break;
18540 case DW_AT_sibling:
18541 /* Ignore absolute siblings, they might point outside of
18542 the current compile unit. */
18543 if (attr.form == DW_FORM_ref_addr)
18544 complaint (&symfile_complaints,
18545 _("ignoring absolute DW_AT_sibling"));
18546 else
18547 {
18548 const gdb_byte *buffer = reader->buffer;
18549 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18550 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18551
18552 if (sibling_ptr < info_ptr)
18553 complaint (&symfile_complaints,
18554 _("DW_AT_sibling points backwards"));
18555 else if (sibling_ptr > reader->buffer_end)
18556 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18557 else
18558 sibling = sibling_ptr;
18559 }
18560 break;
18561 case DW_AT_byte_size:
18562 has_byte_size = 1;
18563 break;
18564 case DW_AT_const_value:
18565 has_const_value = 1;
18566 break;
18567 case DW_AT_calling_convention:
18568 /* DWARF doesn't provide a way to identify a program's source-level
18569 entry point. DW_AT_calling_convention attributes are only meant
18570 to describe functions' calling conventions.
18571
18572 However, because it's a necessary piece of information in
18573 Fortran, and before DWARF 4 DW_CC_program was the only
18574 piece of debugging information whose definition refers to
18575 a 'main program' at all, several compilers marked Fortran
18576 main programs with DW_CC_program --- even when those
18577 functions use the standard calling conventions.
18578
18579 Although DWARF now specifies a way to provide this
18580 information, we support this practice for backward
18581 compatibility. */
18582 if (DW_UNSND (&attr) == DW_CC_program
18583 && cu->language == language_fortran)
18584 main_subprogram = 1;
18585 break;
18586 case DW_AT_inline:
18587 if (DW_UNSND (&attr) == DW_INL_inlined
18588 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18589 may_be_inlined = 1;
18590 break;
18591
18592 case DW_AT_import:
18593 if (tag == DW_TAG_imported_unit)
18594 {
18595 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18596 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18597 || cu->per_cu->is_dwz);
18598 }
18599 break;
18600
18601 case DW_AT_main_subprogram:
18602 main_subprogram = DW_UNSND (&attr);
18603 break;
18604
18605 default:
18606 break;
18607 }
18608 }
18609
18610 if (high_pc_relative)
18611 highpc += lowpc;
18612
18613 if (has_low_pc_attr && has_high_pc_attr)
18614 {
18615 /* When using the GNU linker, .gnu.linkonce. sections are used to
18616 eliminate duplicate copies of functions and vtables and such.
18617 The linker will arbitrarily choose one and discard the others.
18618 The AT_*_pc values for such functions refer to local labels in
18619 these sections. If the section from that file was discarded, the
18620 labels are not in the output, so the relocs get a value of 0.
18621 If this is a discarded function, mark the pc bounds as invalid,
18622 so that GDB will ignore it. */
18623 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18624 {
18625 struct objfile *objfile = dwarf2_per_objfile->objfile;
18626 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18627
18628 complaint (&symfile_complaints,
18629 _("DW_AT_low_pc %s is zero "
18630 "for DIE at %s [in module %s]"),
18631 paddress (gdbarch, lowpc),
18632 sect_offset_str (sect_off),
18633 objfile_name (objfile));
18634 }
18635 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18636 else if (lowpc >= highpc)
18637 {
18638 struct objfile *objfile = dwarf2_per_objfile->objfile;
18639 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18640
18641 complaint (&symfile_complaints,
18642 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18643 "for DIE at %s [in module %s]"),
18644 paddress (gdbarch, lowpc),
18645 paddress (gdbarch, highpc),
18646 sect_offset_str (sect_off),
18647 objfile_name (objfile));
18648 }
18649 else
18650 has_pc_info = 1;
18651 }
18652
18653 return info_ptr;
18654 }
18655
18656 /* Find a cached partial DIE at OFFSET in CU. */
18657
18658 struct partial_die_info *
18659 dwarf2_cu::find_partial_die (sect_offset sect_off)
18660 {
18661 struct partial_die_info *lookup_die = NULL;
18662 struct partial_die_info part_die (sect_off);
18663
18664 lookup_die = ((struct partial_die_info *)
18665 htab_find_with_hash (partial_dies, &part_die,
18666 to_underlying (sect_off)));
18667
18668 return lookup_die;
18669 }
18670
18671 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18672 except in the case of .debug_types DIEs which do not reference
18673 outside their CU (they do however referencing other types via
18674 DW_FORM_ref_sig8). */
18675
18676 static struct partial_die_info *
18677 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18678 {
18679 struct dwarf2_per_objfile *dwarf2_per_objfile
18680 = cu->per_cu->dwarf2_per_objfile;
18681 struct objfile *objfile = dwarf2_per_objfile->objfile;
18682 struct dwarf2_per_cu_data *per_cu = NULL;
18683 struct partial_die_info *pd = NULL;
18684
18685 if (offset_in_dwz == cu->per_cu->is_dwz
18686 && offset_in_cu_p (&cu->header, sect_off))
18687 {
18688 pd = cu->find_partial_die (sect_off);
18689 if (pd != NULL)
18690 return pd;
18691 /* We missed recording what we needed.
18692 Load all dies and try again. */
18693 per_cu = cu->per_cu;
18694 }
18695 else
18696 {
18697 /* TUs don't reference other CUs/TUs (except via type signatures). */
18698 if (cu->per_cu->is_debug_types)
18699 {
18700 error (_("Dwarf Error: Type Unit at offset %s contains"
18701 " external reference to offset %s [in module %s].\n"),
18702 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18703 bfd_get_filename (objfile->obfd));
18704 }
18705 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18706 dwarf2_per_objfile);
18707
18708 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18709 load_partial_comp_unit (per_cu);
18710
18711 per_cu->cu->last_used = 0;
18712 pd = per_cu->cu->find_partial_die (sect_off);
18713 }
18714
18715 /* If we didn't find it, and not all dies have been loaded,
18716 load them all and try again. */
18717
18718 if (pd == NULL && per_cu->load_all_dies == 0)
18719 {
18720 per_cu->load_all_dies = 1;
18721
18722 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18723 THIS_CU->cu may already be in use. So we can't just free it and
18724 replace its DIEs with the ones we read in. Instead, we leave those
18725 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18726 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18727 set. */
18728 load_partial_comp_unit (per_cu);
18729
18730 pd = per_cu->cu->find_partial_die (sect_off);
18731 }
18732
18733 if (pd == NULL)
18734 internal_error (__FILE__, __LINE__,
18735 _("could not find partial DIE %s "
18736 "in cache [from module %s]\n"),
18737 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18738 return pd;
18739 }
18740
18741 /* See if we can figure out if the class lives in a namespace. We do
18742 this by looking for a member function; its demangled name will
18743 contain namespace info, if there is any. */
18744
18745 static void
18746 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18747 struct dwarf2_cu *cu)
18748 {
18749 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18750 what template types look like, because the demangler
18751 frequently doesn't give the same name as the debug info. We
18752 could fix this by only using the demangled name to get the
18753 prefix (but see comment in read_structure_type). */
18754
18755 struct partial_die_info *real_pdi;
18756 struct partial_die_info *child_pdi;
18757
18758 /* If this DIE (this DIE's specification, if any) has a parent, then
18759 we should not do this. We'll prepend the parent's fully qualified
18760 name when we create the partial symbol. */
18761
18762 real_pdi = struct_pdi;
18763 while (real_pdi->has_specification)
18764 real_pdi = find_partial_die (real_pdi->spec_offset,
18765 real_pdi->spec_is_dwz, cu);
18766
18767 if (real_pdi->die_parent != NULL)
18768 return;
18769
18770 for (child_pdi = struct_pdi->die_child;
18771 child_pdi != NULL;
18772 child_pdi = child_pdi->die_sibling)
18773 {
18774 if (child_pdi->tag == DW_TAG_subprogram
18775 && child_pdi->linkage_name != NULL)
18776 {
18777 char *actual_class_name
18778 = language_class_name_from_physname (cu->language_defn,
18779 child_pdi->linkage_name);
18780 if (actual_class_name != NULL)
18781 {
18782 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18783 struct_pdi->name
18784 = ((const char *)
18785 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18786 actual_class_name,
18787 strlen (actual_class_name)));
18788 xfree (actual_class_name);
18789 }
18790 break;
18791 }
18792 }
18793 }
18794
18795 void
18796 partial_die_info::fixup (struct dwarf2_cu *cu)
18797 {
18798 /* Once we've fixed up a die, there's no point in doing so again.
18799 This also avoids a memory leak if we were to call
18800 guess_partial_die_structure_name multiple times. */
18801 if (fixup_called)
18802 return;
18803
18804 /* If we found a reference attribute and the DIE has no name, try
18805 to find a name in the referred to DIE. */
18806
18807 if (name == NULL && has_specification)
18808 {
18809 struct partial_die_info *spec_die;
18810
18811 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
18812
18813 spec_die->fixup (cu);
18814
18815 if (spec_die->name)
18816 {
18817 name = spec_die->name;
18818
18819 /* Copy DW_AT_external attribute if it is set. */
18820 if (spec_die->is_external)
18821 is_external = spec_die->is_external;
18822 }
18823 }
18824
18825 /* Set default names for some unnamed DIEs. */
18826
18827 if (name == NULL && tag == DW_TAG_namespace)
18828 name = CP_ANONYMOUS_NAMESPACE_STR;
18829
18830 /* If there is no parent die to provide a namespace, and there are
18831 children, see if we can determine the namespace from their linkage
18832 name. */
18833 if (cu->language == language_cplus
18834 && !VEC_empty (dwarf2_section_info_def,
18835 cu->per_cu->dwarf2_per_objfile->types)
18836 && die_parent == NULL
18837 && has_children
18838 && (tag == DW_TAG_class_type
18839 || tag == DW_TAG_structure_type
18840 || tag == DW_TAG_union_type))
18841 guess_partial_die_structure_name (this, cu);
18842
18843 /* GCC might emit a nameless struct or union that has a linkage
18844 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18845 if (name == NULL
18846 && (tag == DW_TAG_class_type
18847 || tag == DW_TAG_interface_type
18848 || tag == DW_TAG_structure_type
18849 || tag == DW_TAG_union_type)
18850 && linkage_name != NULL)
18851 {
18852 char *demangled;
18853
18854 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
18855 if (demangled)
18856 {
18857 const char *base;
18858
18859 /* Strip any leading namespaces/classes, keep only the base name.
18860 DW_AT_name for named DIEs does not contain the prefixes. */
18861 base = strrchr (demangled, ':');
18862 if (base && base > demangled && base[-1] == ':')
18863 base++;
18864 else
18865 base = demangled;
18866
18867 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18868 name
18869 = ((const char *)
18870 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18871 base, strlen (base)));
18872 xfree (demangled);
18873 }
18874 }
18875
18876 fixup_called = 1;
18877 }
18878
18879 /* Read an attribute value described by an attribute form. */
18880
18881 static const gdb_byte *
18882 read_attribute_value (const struct die_reader_specs *reader,
18883 struct attribute *attr, unsigned form,
18884 LONGEST implicit_const, const gdb_byte *info_ptr)
18885 {
18886 struct dwarf2_cu *cu = reader->cu;
18887 struct dwarf2_per_objfile *dwarf2_per_objfile
18888 = cu->per_cu->dwarf2_per_objfile;
18889 struct objfile *objfile = dwarf2_per_objfile->objfile;
18890 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18891 bfd *abfd = reader->abfd;
18892 struct comp_unit_head *cu_header = &cu->header;
18893 unsigned int bytes_read;
18894 struct dwarf_block *blk;
18895
18896 attr->form = (enum dwarf_form) form;
18897 switch (form)
18898 {
18899 case DW_FORM_ref_addr:
18900 if (cu->header.version == 2)
18901 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18902 else
18903 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18904 &cu->header, &bytes_read);
18905 info_ptr += bytes_read;
18906 break;
18907 case DW_FORM_GNU_ref_alt:
18908 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18909 info_ptr += bytes_read;
18910 break;
18911 case DW_FORM_addr:
18912 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18913 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18914 info_ptr += bytes_read;
18915 break;
18916 case DW_FORM_block2:
18917 blk = dwarf_alloc_block (cu);
18918 blk->size = read_2_bytes (abfd, info_ptr);
18919 info_ptr += 2;
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_block4:
18925 blk = dwarf_alloc_block (cu);
18926 blk->size = read_4_bytes (abfd, info_ptr);
18927 info_ptr += 4;
18928 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18929 info_ptr += blk->size;
18930 DW_BLOCK (attr) = blk;
18931 break;
18932 case DW_FORM_data2:
18933 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18934 info_ptr += 2;
18935 break;
18936 case DW_FORM_data4:
18937 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18938 info_ptr += 4;
18939 break;
18940 case DW_FORM_data8:
18941 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18942 info_ptr += 8;
18943 break;
18944 case DW_FORM_data16:
18945 blk = dwarf_alloc_block (cu);
18946 blk->size = 16;
18947 blk->data = read_n_bytes (abfd, info_ptr, 16);
18948 info_ptr += 16;
18949 DW_BLOCK (attr) = blk;
18950 break;
18951 case DW_FORM_sec_offset:
18952 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18953 info_ptr += bytes_read;
18954 break;
18955 case DW_FORM_string:
18956 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18957 DW_STRING_IS_CANONICAL (attr) = 0;
18958 info_ptr += bytes_read;
18959 break;
18960 case DW_FORM_strp:
18961 if (!cu->per_cu->is_dwz)
18962 {
18963 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18964 abfd, info_ptr, cu_header,
18965 &bytes_read);
18966 DW_STRING_IS_CANONICAL (attr) = 0;
18967 info_ptr += bytes_read;
18968 break;
18969 }
18970 /* FALLTHROUGH */
18971 case DW_FORM_line_strp:
18972 if (!cu->per_cu->is_dwz)
18973 {
18974 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18975 abfd, info_ptr,
18976 cu_header, &bytes_read);
18977 DW_STRING_IS_CANONICAL (attr) = 0;
18978 info_ptr += bytes_read;
18979 break;
18980 }
18981 /* FALLTHROUGH */
18982 case DW_FORM_GNU_strp_alt:
18983 {
18984 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18985 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18986 &bytes_read);
18987
18988 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18989 dwz, str_offset);
18990 DW_STRING_IS_CANONICAL (attr) = 0;
18991 info_ptr += bytes_read;
18992 }
18993 break;
18994 case DW_FORM_exprloc:
18995 case DW_FORM_block:
18996 blk = dwarf_alloc_block (cu);
18997 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18998 info_ptr += bytes_read;
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_block1:
19004 blk = dwarf_alloc_block (cu);
19005 blk->size = read_1_byte (abfd, info_ptr);
19006 info_ptr += 1;
19007 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19008 info_ptr += blk->size;
19009 DW_BLOCK (attr) = blk;
19010 break;
19011 case DW_FORM_data1:
19012 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19013 info_ptr += 1;
19014 break;
19015 case DW_FORM_flag:
19016 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19017 info_ptr += 1;
19018 break;
19019 case DW_FORM_flag_present:
19020 DW_UNSND (attr) = 1;
19021 break;
19022 case DW_FORM_sdata:
19023 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19024 info_ptr += bytes_read;
19025 break;
19026 case DW_FORM_udata:
19027 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19028 info_ptr += bytes_read;
19029 break;
19030 case DW_FORM_ref1:
19031 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19032 + read_1_byte (abfd, info_ptr));
19033 info_ptr += 1;
19034 break;
19035 case DW_FORM_ref2:
19036 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19037 + read_2_bytes (abfd, info_ptr));
19038 info_ptr += 2;
19039 break;
19040 case DW_FORM_ref4:
19041 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19042 + read_4_bytes (abfd, info_ptr));
19043 info_ptr += 4;
19044 break;
19045 case DW_FORM_ref8:
19046 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19047 + read_8_bytes (abfd, info_ptr));
19048 info_ptr += 8;
19049 break;
19050 case DW_FORM_ref_sig8:
19051 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19052 info_ptr += 8;
19053 break;
19054 case DW_FORM_ref_udata:
19055 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19056 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19057 info_ptr += bytes_read;
19058 break;
19059 case DW_FORM_indirect:
19060 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19061 info_ptr += bytes_read;
19062 if (form == DW_FORM_implicit_const)
19063 {
19064 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19065 info_ptr += bytes_read;
19066 }
19067 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19068 info_ptr);
19069 break;
19070 case DW_FORM_implicit_const:
19071 DW_SND (attr) = implicit_const;
19072 break;
19073 case DW_FORM_GNU_addr_index:
19074 if (reader->dwo_file == NULL)
19075 {
19076 /* For now flag a hard error.
19077 Later we can turn this into a complaint. */
19078 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19079 dwarf_form_name (form),
19080 bfd_get_filename (abfd));
19081 }
19082 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19083 info_ptr += bytes_read;
19084 break;
19085 case DW_FORM_GNU_str_index:
19086 if (reader->dwo_file == NULL)
19087 {
19088 /* For now flag a hard error.
19089 Later we can turn this into a complaint if warranted. */
19090 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19091 dwarf_form_name (form),
19092 bfd_get_filename (abfd));
19093 }
19094 {
19095 ULONGEST str_index =
19096 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19097
19098 DW_STRING (attr) = read_str_index (reader, str_index);
19099 DW_STRING_IS_CANONICAL (attr) = 0;
19100 info_ptr += bytes_read;
19101 }
19102 break;
19103 default:
19104 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19105 dwarf_form_name (form),
19106 bfd_get_filename (abfd));
19107 }
19108
19109 /* Super hack. */
19110 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19111 attr->form = DW_FORM_GNU_ref_alt;
19112
19113 /* We have seen instances where the compiler tried to emit a byte
19114 size attribute of -1 which ended up being encoded as an unsigned
19115 0xffffffff. Although 0xffffffff is technically a valid size value,
19116 an object of this size seems pretty unlikely so we can relatively
19117 safely treat these cases as if the size attribute was invalid and
19118 treat them as zero by default. */
19119 if (attr->name == DW_AT_byte_size
19120 && form == DW_FORM_data4
19121 && DW_UNSND (attr) >= 0xffffffff)
19122 {
19123 complaint
19124 (&symfile_complaints,
19125 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19126 hex_string (DW_UNSND (attr)));
19127 DW_UNSND (attr) = 0;
19128 }
19129
19130 return info_ptr;
19131 }
19132
19133 /* Read an attribute described by an abbreviated attribute. */
19134
19135 static const gdb_byte *
19136 read_attribute (const struct die_reader_specs *reader,
19137 struct attribute *attr, struct attr_abbrev *abbrev,
19138 const gdb_byte *info_ptr)
19139 {
19140 attr->name = abbrev->name;
19141 return read_attribute_value (reader, attr, abbrev->form,
19142 abbrev->implicit_const, info_ptr);
19143 }
19144
19145 /* Read dwarf information from a buffer. */
19146
19147 static unsigned int
19148 read_1_byte (bfd *abfd, const gdb_byte *buf)
19149 {
19150 return bfd_get_8 (abfd, buf);
19151 }
19152
19153 static int
19154 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19155 {
19156 return bfd_get_signed_8 (abfd, buf);
19157 }
19158
19159 static unsigned int
19160 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19161 {
19162 return bfd_get_16 (abfd, buf);
19163 }
19164
19165 static int
19166 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19167 {
19168 return bfd_get_signed_16 (abfd, buf);
19169 }
19170
19171 static unsigned int
19172 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19173 {
19174 return bfd_get_32 (abfd, buf);
19175 }
19176
19177 static int
19178 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19179 {
19180 return bfd_get_signed_32 (abfd, buf);
19181 }
19182
19183 static ULONGEST
19184 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19185 {
19186 return bfd_get_64 (abfd, buf);
19187 }
19188
19189 static CORE_ADDR
19190 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19191 unsigned int *bytes_read)
19192 {
19193 struct comp_unit_head *cu_header = &cu->header;
19194 CORE_ADDR retval = 0;
19195
19196 if (cu_header->signed_addr_p)
19197 {
19198 switch (cu_header->addr_size)
19199 {
19200 case 2:
19201 retval = bfd_get_signed_16 (abfd, buf);
19202 break;
19203 case 4:
19204 retval = bfd_get_signed_32 (abfd, buf);
19205 break;
19206 case 8:
19207 retval = bfd_get_signed_64 (abfd, buf);
19208 break;
19209 default:
19210 internal_error (__FILE__, __LINE__,
19211 _("read_address: bad switch, signed [in module %s]"),
19212 bfd_get_filename (abfd));
19213 }
19214 }
19215 else
19216 {
19217 switch (cu_header->addr_size)
19218 {
19219 case 2:
19220 retval = bfd_get_16 (abfd, buf);
19221 break;
19222 case 4:
19223 retval = bfd_get_32 (abfd, buf);
19224 break;
19225 case 8:
19226 retval = bfd_get_64 (abfd, buf);
19227 break;
19228 default:
19229 internal_error (__FILE__, __LINE__,
19230 _("read_address: bad switch, "
19231 "unsigned [in module %s]"),
19232 bfd_get_filename (abfd));
19233 }
19234 }
19235
19236 *bytes_read = cu_header->addr_size;
19237 return retval;
19238 }
19239
19240 /* Read the initial length from a section. The (draft) DWARF 3
19241 specification allows the initial length to take up either 4 bytes
19242 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19243 bytes describe the length and all offsets will be 8 bytes in length
19244 instead of 4.
19245
19246 An older, non-standard 64-bit format is also handled by this
19247 function. The older format in question stores the initial length
19248 as an 8-byte quantity without an escape value. Lengths greater
19249 than 2^32 aren't very common which means that the initial 4 bytes
19250 is almost always zero. Since a length value of zero doesn't make
19251 sense for the 32-bit format, this initial zero can be considered to
19252 be an escape value which indicates the presence of the older 64-bit
19253 format. As written, the code can't detect (old format) lengths
19254 greater than 4GB. If it becomes necessary to handle lengths
19255 somewhat larger than 4GB, we could allow other small values (such
19256 as the non-sensical values of 1, 2, and 3) to also be used as
19257 escape values indicating the presence of the old format.
19258
19259 The value returned via bytes_read should be used to increment the
19260 relevant pointer after calling read_initial_length().
19261
19262 [ Note: read_initial_length() and read_offset() are based on the
19263 document entitled "DWARF Debugging Information Format", revision
19264 3, draft 8, dated November 19, 2001. This document was obtained
19265 from:
19266
19267 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19268
19269 This document is only a draft and is subject to change. (So beware.)
19270
19271 Details regarding the older, non-standard 64-bit format were
19272 determined empirically by examining 64-bit ELF files produced by
19273 the SGI toolchain on an IRIX 6.5 machine.
19274
19275 - Kevin, July 16, 2002
19276 ] */
19277
19278 static LONGEST
19279 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19280 {
19281 LONGEST length = bfd_get_32 (abfd, buf);
19282
19283 if (length == 0xffffffff)
19284 {
19285 length = bfd_get_64 (abfd, buf + 4);
19286 *bytes_read = 12;
19287 }
19288 else if (length == 0)
19289 {
19290 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19291 length = bfd_get_64 (abfd, buf);
19292 *bytes_read = 8;
19293 }
19294 else
19295 {
19296 *bytes_read = 4;
19297 }
19298
19299 return length;
19300 }
19301
19302 /* Cover function for read_initial_length.
19303 Returns the length of the object at BUF, and stores the size of the
19304 initial length in *BYTES_READ and stores the size that offsets will be in
19305 *OFFSET_SIZE.
19306 If the initial length size is not equivalent to that specified in
19307 CU_HEADER then issue a complaint.
19308 This is useful when reading non-comp-unit headers. */
19309
19310 static LONGEST
19311 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19312 const struct comp_unit_head *cu_header,
19313 unsigned int *bytes_read,
19314 unsigned int *offset_size)
19315 {
19316 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19317
19318 gdb_assert (cu_header->initial_length_size == 4
19319 || cu_header->initial_length_size == 8
19320 || cu_header->initial_length_size == 12);
19321
19322 if (cu_header->initial_length_size != *bytes_read)
19323 complaint (&symfile_complaints,
19324 _("intermixed 32-bit and 64-bit DWARF sections"));
19325
19326 *offset_size = (*bytes_read == 4) ? 4 : 8;
19327 return length;
19328 }
19329
19330 /* Read an offset from the data stream. The size of the offset is
19331 given by cu_header->offset_size. */
19332
19333 static LONGEST
19334 read_offset (bfd *abfd, const gdb_byte *buf,
19335 const struct comp_unit_head *cu_header,
19336 unsigned int *bytes_read)
19337 {
19338 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19339
19340 *bytes_read = cu_header->offset_size;
19341 return offset;
19342 }
19343
19344 /* Read an offset from the data stream. */
19345
19346 static LONGEST
19347 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19348 {
19349 LONGEST retval = 0;
19350
19351 switch (offset_size)
19352 {
19353 case 4:
19354 retval = bfd_get_32 (abfd, buf);
19355 break;
19356 case 8:
19357 retval = bfd_get_64 (abfd, buf);
19358 break;
19359 default:
19360 internal_error (__FILE__, __LINE__,
19361 _("read_offset_1: bad switch [in module %s]"),
19362 bfd_get_filename (abfd));
19363 }
19364
19365 return retval;
19366 }
19367
19368 static const gdb_byte *
19369 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19370 {
19371 /* If the size of a host char is 8 bits, we can return a pointer
19372 to the buffer, otherwise we have to copy the data to a buffer
19373 allocated on the temporary obstack. */
19374 gdb_assert (HOST_CHAR_BIT == 8);
19375 return buf;
19376 }
19377
19378 static const char *
19379 read_direct_string (bfd *abfd, const gdb_byte *buf,
19380 unsigned int *bytes_read_ptr)
19381 {
19382 /* If the size of a host char is 8 bits, we can return a pointer
19383 to the string, otherwise we have to copy the string to a buffer
19384 allocated on the temporary obstack. */
19385 gdb_assert (HOST_CHAR_BIT == 8);
19386 if (*buf == '\0')
19387 {
19388 *bytes_read_ptr = 1;
19389 return NULL;
19390 }
19391 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19392 return (const char *) buf;
19393 }
19394
19395 /* Return pointer to string at section SECT offset STR_OFFSET with error
19396 reporting strings FORM_NAME and SECT_NAME. */
19397
19398 static const char *
19399 read_indirect_string_at_offset_from (struct objfile *objfile,
19400 bfd *abfd, LONGEST str_offset,
19401 struct dwarf2_section_info *sect,
19402 const char *form_name,
19403 const char *sect_name)
19404 {
19405 dwarf2_read_section (objfile, sect);
19406 if (sect->buffer == NULL)
19407 error (_("%s used without %s section [in module %s]"),
19408 form_name, sect_name, bfd_get_filename (abfd));
19409 if (str_offset >= sect->size)
19410 error (_("%s pointing outside of %s section [in module %s]"),
19411 form_name, sect_name, bfd_get_filename (abfd));
19412 gdb_assert (HOST_CHAR_BIT == 8);
19413 if (sect->buffer[str_offset] == '\0')
19414 return NULL;
19415 return (const char *) (sect->buffer + str_offset);
19416 }
19417
19418 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19419
19420 static const char *
19421 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19422 bfd *abfd, LONGEST str_offset)
19423 {
19424 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19425 abfd, str_offset,
19426 &dwarf2_per_objfile->str,
19427 "DW_FORM_strp", ".debug_str");
19428 }
19429
19430 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19431
19432 static const char *
19433 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19434 bfd *abfd, LONGEST str_offset)
19435 {
19436 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19437 abfd, str_offset,
19438 &dwarf2_per_objfile->line_str,
19439 "DW_FORM_line_strp",
19440 ".debug_line_str");
19441 }
19442
19443 /* Read a string at offset STR_OFFSET in the .debug_str section from
19444 the .dwz file DWZ. Throw an error if the offset is too large. If
19445 the string consists of a single NUL byte, return NULL; otherwise
19446 return a pointer to the string. */
19447
19448 static const char *
19449 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19450 LONGEST str_offset)
19451 {
19452 dwarf2_read_section (objfile, &dwz->str);
19453
19454 if (dwz->str.buffer == NULL)
19455 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19456 "section [in module %s]"),
19457 bfd_get_filename (dwz->dwz_bfd));
19458 if (str_offset >= dwz->str.size)
19459 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19460 ".debug_str section [in module %s]"),
19461 bfd_get_filename (dwz->dwz_bfd));
19462 gdb_assert (HOST_CHAR_BIT == 8);
19463 if (dwz->str.buffer[str_offset] == '\0')
19464 return NULL;
19465 return (const char *) (dwz->str.buffer + str_offset);
19466 }
19467
19468 /* Return pointer to string at .debug_str offset as read from BUF.
19469 BUF is assumed to be in a compilation unit described by CU_HEADER.
19470 Return *BYTES_READ_PTR count of bytes read from BUF. */
19471
19472 static const char *
19473 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19474 const gdb_byte *buf,
19475 const struct comp_unit_head *cu_header,
19476 unsigned int *bytes_read_ptr)
19477 {
19478 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19479
19480 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19481 }
19482
19483 /* Return pointer to string at .debug_line_str offset as read from BUF.
19484 BUF is assumed to be in a compilation unit described by CU_HEADER.
19485 Return *BYTES_READ_PTR count of bytes read from BUF. */
19486
19487 static const char *
19488 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19489 bfd *abfd, const gdb_byte *buf,
19490 const struct comp_unit_head *cu_header,
19491 unsigned int *bytes_read_ptr)
19492 {
19493 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19494
19495 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19496 str_offset);
19497 }
19498
19499 ULONGEST
19500 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19501 unsigned int *bytes_read_ptr)
19502 {
19503 ULONGEST result;
19504 unsigned int num_read;
19505 int shift;
19506 unsigned char byte;
19507
19508 result = 0;
19509 shift = 0;
19510 num_read = 0;
19511 while (1)
19512 {
19513 byte = bfd_get_8 (abfd, buf);
19514 buf++;
19515 num_read++;
19516 result |= ((ULONGEST) (byte & 127) << shift);
19517 if ((byte & 128) == 0)
19518 {
19519 break;
19520 }
19521 shift += 7;
19522 }
19523 *bytes_read_ptr = num_read;
19524 return result;
19525 }
19526
19527 static LONGEST
19528 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19529 unsigned int *bytes_read_ptr)
19530 {
19531 LONGEST result;
19532 int shift, num_read;
19533 unsigned char byte;
19534
19535 result = 0;
19536 shift = 0;
19537 num_read = 0;
19538 while (1)
19539 {
19540 byte = bfd_get_8 (abfd, buf);
19541 buf++;
19542 num_read++;
19543 result |= ((LONGEST) (byte & 127) << shift);
19544 shift += 7;
19545 if ((byte & 128) == 0)
19546 {
19547 break;
19548 }
19549 }
19550 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19551 result |= -(((LONGEST) 1) << shift);
19552 *bytes_read_ptr = num_read;
19553 return result;
19554 }
19555
19556 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19557 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19558 ADDR_SIZE is the size of addresses from the CU header. */
19559
19560 static CORE_ADDR
19561 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19562 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19563 {
19564 struct objfile *objfile = dwarf2_per_objfile->objfile;
19565 bfd *abfd = objfile->obfd;
19566 const gdb_byte *info_ptr;
19567
19568 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19569 if (dwarf2_per_objfile->addr.buffer == NULL)
19570 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19571 objfile_name (objfile));
19572 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19573 error (_("DW_FORM_addr_index pointing outside of "
19574 ".debug_addr section [in module %s]"),
19575 objfile_name (objfile));
19576 info_ptr = (dwarf2_per_objfile->addr.buffer
19577 + addr_base + addr_index * addr_size);
19578 if (addr_size == 4)
19579 return bfd_get_32 (abfd, info_ptr);
19580 else
19581 return bfd_get_64 (abfd, info_ptr);
19582 }
19583
19584 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19585
19586 static CORE_ADDR
19587 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19588 {
19589 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19590 cu->addr_base, cu->header.addr_size);
19591 }
19592
19593 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19594
19595 static CORE_ADDR
19596 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19597 unsigned int *bytes_read)
19598 {
19599 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19600 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19601
19602 return read_addr_index (cu, addr_index);
19603 }
19604
19605 /* Data structure to pass results from dwarf2_read_addr_index_reader
19606 back to dwarf2_read_addr_index. */
19607
19608 struct dwarf2_read_addr_index_data
19609 {
19610 ULONGEST addr_base;
19611 int addr_size;
19612 };
19613
19614 /* die_reader_func for dwarf2_read_addr_index. */
19615
19616 static void
19617 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19618 const gdb_byte *info_ptr,
19619 struct die_info *comp_unit_die,
19620 int has_children,
19621 void *data)
19622 {
19623 struct dwarf2_cu *cu = reader->cu;
19624 struct dwarf2_read_addr_index_data *aidata =
19625 (struct dwarf2_read_addr_index_data *) data;
19626
19627 aidata->addr_base = cu->addr_base;
19628 aidata->addr_size = cu->header.addr_size;
19629 }
19630
19631 /* Given an index in .debug_addr, fetch the value.
19632 NOTE: This can be called during dwarf expression evaluation,
19633 long after the debug information has been read, and thus per_cu->cu
19634 may no longer exist. */
19635
19636 CORE_ADDR
19637 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19638 unsigned int addr_index)
19639 {
19640 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19641 struct dwarf2_cu *cu = per_cu->cu;
19642 ULONGEST addr_base;
19643 int addr_size;
19644
19645 /* We need addr_base and addr_size.
19646 If we don't have PER_CU->cu, we have to get it.
19647 Nasty, but the alternative is storing the needed info in PER_CU,
19648 which at this point doesn't seem justified: it's not clear how frequently
19649 it would get used and it would increase the size of every PER_CU.
19650 Entry points like dwarf2_per_cu_addr_size do a similar thing
19651 so we're not in uncharted territory here.
19652 Alas we need to be a bit more complicated as addr_base is contained
19653 in the DIE.
19654
19655 We don't need to read the entire CU(/TU).
19656 We just need the header and top level die.
19657
19658 IWBN to use the aging mechanism to let us lazily later discard the CU.
19659 For now we skip this optimization. */
19660
19661 if (cu != NULL)
19662 {
19663 addr_base = cu->addr_base;
19664 addr_size = cu->header.addr_size;
19665 }
19666 else
19667 {
19668 struct dwarf2_read_addr_index_data aidata;
19669
19670 /* Note: We can't use init_cutu_and_read_dies_simple here,
19671 we need addr_base. */
19672 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19673 dwarf2_read_addr_index_reader, &aidata);
19674 addr_base = aidata.addr_base;
19675 addr_size = aidata.addr_size;
19676 }
19677
19678 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19679 addr_size);
19680 }
19681
19682 /* Given a DW_FORM_GNU_str_index, fetch the string.
19683 This is only used by the Fission support. */
19684
19685 static const char *
19686 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19687 {
19688 struct dwarf2_cu *cu = reader->cu;
19689 struct dwarf2_per_objfile *dwarf2_per_objfile
19690 = cu->per_cu->dwarf2_per_objfile;
19691 struct objfile *objfile = dwarf2_per_objfile->objfile;
19692 const char *objf_name = objfile_name (objfile);
19693 bfd *abfd = objfile->obfd;
19694 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19695 struct dwarf2_section_info *str_offsets_section =
19696 &reader->dwo_file->sections.str_offsets;
19697 const gdb_byte *info_ptr;
19698 ULONGEST str_offset;
19699 static const char form_name[] = "DW_FORM_GNU_str_index";
19700
19701 dwarf2_read_section (objfile, str_section);
19702 dwarf2_read_section (objfile, str_offsets_section);
19703 if (str_section->buffer == NULL)
19704 error (_("%s used without .debug_str.dwo section"
19705 " in CU at offset %s [in module %s]"),
19706 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19707 if (str_offsets_section->buffer == NULL)
19708 error (_("%s used without .debug_str_offsets.dwo section"
19709 " in CU at offset %s [in module %s]"),
19710 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19711 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19712 error (_("%s pointing outside of .debug_str_offsets.dwo"
19713 " section in CU at offset %s [in module %s]"),
19714 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19715 info_ptr = (str_offsets_section->buffer
19716 + str_index * cu->header.offset_size);
19717 if (cu->header.offset_size == 4)
19718 str_offset = bfd_get_32 (abfd, info_ptr);
19719 else
19720 str_offset = bfd_get_64 (abfd, info_ptr);
19721 if (str_offset >= str_section->size)
19722 error (_("Offset from %s pointing outside of"
19723 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19724 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19725 return (const char *) (str_section->buffer + str_offset);
19726 }
19727
19728 /* Return the length of an LEB128 number in BUF. */
19729
19730 static int
19731 leb128_size (const gdb_byte *buf)
19732 {
19733 const gdb_byte *begin = buf;
19734 gdb_byte byte;
19735
19736 while (1)
19737 {
19738 byte = *buf++;
19739 if ((byte & 128) == 0)
19740 return buf - begin;
19741 }
19742 }
19743
19744 static void
19745 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19746 {
19747 switch (lang)
19748 {
19749 case DW_LANG_C89:
19750 case DW_LANG_C99:
19751 case DW_LANG_C11:
19752 case DW_LANG_C:
19753 case DW_LANG_UPC:
19754 cu->language = language_c;
19755 break;
19756 case DW_LANG_Java:
19757 case DW_LANG_C_plus_plus:
19758 case DW_LANG_C_plus_plus_11:
19759 case DW_LANG_C_plus_plus_14:
19760 cu->language = language_cplus;
19761 break;
19762 case DW_LANG_D:
19763 cu->language = language_d;
19764 break;
19765 case DW_LANG_Fortran77:
19766 case DW_LANG_Fortran90:
19767 case DW_LANG_Fortran95:
19768 case DW_LANG_Fortran03:
19769 case DW_LANG_Fortran08:
19770 cu->language = language_fortran;
19771 break;
19772 case DW_LANG_Go:
19773 cu->language = language_go;
19774 break;
19775 case DW_LANG_Mips_Assembler:
19776 cu->language = language_asm;
19777 break;
19778 case DW_LANG_Ada83:
19779 case DW_LANG_Ada95:
19780 cu->language = language_ada;
19781 break;
19782 case DW_LANG_Modula2:
19783 cu->language = language_m2;
19784 break;
19785 case DW_LANG_Pascal83:
19786 cu->language = language_pascal;
19787 break;
19788 case DW_LANG_ObjC:
19789 cu->language = language_objc;
19790 break;
19791 case DW_LANG_Rust:
19792 case DW_LANG_Rust_old:
19793 cu->language = language_rust;
19794 break;
19795 case DW_LANG_Cobol74:
19796 case DW_LANG_Cobol85:
19797 default:
19798 cu->language = language_minimal;
19799 break;
19800 }
19801 cu->language_defn = language_def (cu->language);
19802 }
19803
19804 /* Return the named attribute or NULL if not there. */
19805
19806 static struct attribute *
19807 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19808 {
19809 for (;;)
19810 {
19811 unsigned int i;
19812 struct attribute *spec = NULL;
19813
19814 for (i = 0; i < die->num_attrs; ++i)
19815 {
19816 if (die->attrs[i].name == name)
19817 return &die->attrs[i];
19818 if (die->attrs[i].name == DW_AT_specification
19819 || die->attrs[i].name == DW_AT_abstract_origin)
19820 spec = &die->attrs[i];
19821 }
19822
19823 if (!spec)
19824 break;
19825
19826 die = follow_die_ref (die, spec, &cu);
19827 }
19828
19829 return NULL;
19830 }
19831
19832 /* Return the named attribute or NULL if not there,
19833 but do not follow DW_AT_specification, etc.
19834 This is for use in contexts where we're reading .debug_types dies.
19835 Following DW_AT_specification, DW_AT_abstract_origin will take us
19836 back up the chain, and we want to go down. */
19837
19838 static struct attribute *
19839 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19840 {
19841 unsigned int i;
19842
19843 for (i = 0; i < die->num_attrs; ++i)
19844 if (die->attrs[i].name == name)
19845 return &die->attrs[i];
19846
19847 return NULL;
19848 }
19849
19850 /* Return the string associated with a string-typed attribute, or NULL if it
19851 is either not found or is of an incorrect type. */
19852
19853 static const char *
19854 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19855 {
19856 struct attribute *attr;
19857 const char *str = NULL;
19858
19859 attr = dwarf2_attr (die, name, cu);
19860
19861 if (attr != NULL)
19862 {
19863 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19864 || attr->form == DW_FORM_string
19865 || attr->form == DW_FORM_GNU_str_index
19866 || attr->form == DW_FORM_GNU_strp_alt)
19867 str = DW_STRING (attr);
19868 else
19869 complaint (&symfile_complaints,
19870 _("string type expected for attribute %s for "
19871 "DIE at %s in module %s"),
19872 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19873 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19874 }
19875
19876 return str;
19877 }
19878
19879 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19880 and holds a non-zero value. This function should only be used for
19881 DW_FORM_flag or DW_FORM_flag_present attributes. */
19882
19883 static int
19884 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19885 {
19886 struct attribute *attr = dwarf2_attr (die, name, cu);
19887
19888 return (attr && DW_UNSND (attr));
19889 }
19890
19891 static int
19892 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19893 {
19894 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19895 which value is non-zero. However, we have to be careful with
19896 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19897 (via dwarf2_flag_true_p) follows this attribute. So we may
19898 end up accidently finding a declaration attribute that belongs
19899 to a different DIE referenced by the specification attribute,
19900 even though the given DIE does not have a declaration attribute. */
19901 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19902 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19903 }
19904
19905 /* Return the die giving the specification for DIE, if there is
19906 one. *SPEC_CU is the CU containing DIE on input, and the CU
19907 containing the return value on output. If there is no
19908 specification, but there is an abstract origin, that is
19909 returned. */
19910
19911 static struct die_info *
19912 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19913 {
19914 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19915 *spec_cu);
19916
19917 if (spec_attr == NULL)
19918 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19919
19920 if (spec_attr == NULL)
19921 return NULL;
19922 else
19923 return follow_die_ref (die, spec_attr, spec_cu);
19924 }
19925
19926 /* Stub for free_line_header to match void * callback types. */
19927
19928 static void
19929 free_line_header_voidp (void *arg)
19930 {
19931 struct line_header *lh = (struct line_header *) arg;
19932
19933 delete lh;
19934 }
19935
19936 void
19937 line_header::add_include_dir (const char *include_dir)
19938 {
19939 if (dwarf_line_debug >= 2)
19940 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19941 include_dirs.size () + 1, include_dir);
19942
19943 include_dirs.push_back (include_dir);
19944 }
19945
19946 void
19947 line_header::add_file_name (const char *name,
19948 dir_index d_index,
19949 unsigned int mod_time,
19950 unsigned int length)
19951 {
19952 if (dwarf_line_debug >= 2)
19953 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19954 (unsigned) file_names.size () + 1, name);
19955
19956 file_names.emplace_back (name, d_index, mod_time, length);
19957 }
19958
19959 /* A convenience function to find the proper .debug_line section for a CU. */
19960
19961 static struct dwarf2_section_info *
19962 get_debug_line_section (struct dwarf2_cu *cu)
19963 {
19964 struct dwarf2_section_info *section;
19965 struct dwarf2_per_objfile *dwarf2_per_objfile
19966 = cu->per_cu->dwarf2_per_objfile;
19967
19968 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19969 DWO file. */
19970 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19971 section = &cu->dwo_unit->dwo_file->sections.line;
19972 else if (cu->per_cu->is_dwz)
19973 {
19974 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19975
19976 section = &dwz->line;
19977 }
19978 else
19979 section = &dwarf2_per_objfile->line;
19980
19981 return section;
19982 }
19983
19984 /* Read directory or file name entry format, starting with byte of
19985 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19986 entries count and the entries themselves in the described entry
19987 format. */
19988
19989 static void
19990 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19991 bfd *abfd, const gdb_byte **bufp,
19992 struct line_header *lh,
19993 const struct comp_unit_head *cu_header,
19994 void (*callback) (struct line_header *lh,
19995 const char *name,
19996 dir_index d_index,
19997 unsigned int mod_time,
19998 unsigned int length))
19999 {
20000 gdb_byte format_count, formati;
20001 ULONGEST data_count, datai;
20002 const gdb_byte *buf = *bufp;
20003 const gdb_byte *format_header_data;
20004 unsigned int bytes_read;
20005
20006 format_count = read_1_byte (abfd, buf);
20007 buf += 1;
20008 format_header_data = buf;
20009 for (formati = 0; formati < format_count; formati++)
20010 {
20011 read_unsigned_leb128 (abfd, buf, &bytes_read);
20012 buf += bytes_read;
20013 read_unsigned_leb128 (abfd, buf, &bytes_read);
20014 buf += bytes_read;
20015 }
20016
20017 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20018 buf += bytes_read;
20019 for (datai = 0; datai < data_count; datai++)
20020 {
20021 const gdb_byte *format = format_header_data;
20022 struct file_entry fe;
20023
20024 for (formati = 0; formati < format_count; formati++)
20025 {
20026 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20027 format += bytes_read;
20028
20029 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20030 format += bytes_read;
20031
20032 gdb::optional<const char *> string;
20033 gdb::optional<unsigned int> uint;
20034
20035 switch (form)
20036 {
20037 case DW_FORM_string:
20038 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20039 buf += bytes_read;
20040 break;
20041
20042 case DW_FORM_line_strp:
20043 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20044 abfd, buf,
20045 cu_header,
20046 &bytes_read));
20047 buf += bytes_read;
20048 break;
20049
20050 case DW_FORM_data1:
20051 uint.emplace (read_1_byte (abfd, buf));
20052 buf += 1;
20053 break;
20054
20055 case DW_FORM_data2:
20056 uint.emplace (read_2_bytes (abfd, buf));
20057 buf += 2;
20058 break;
20059
20060 case DW_FORM_data4:
20061 uint.emplace (read_4_bytes (abfd, buf));
20062 buf += 4;
20063 break;
20064
20065 case DW_FORM_data8:
20066 uint.emplace (read_8_bytes (abfd, buf));
20067 buf += 8;
20068 break;
20069
20070 case DW_FORM_udata:
20071 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20072 buf += bytes_read;
20073 break;
20074
20075 case DW_FORM_block:
20076 /* It is valid only for DW_LNCT_timestamp which is ignored by
20077 current GDB. */
20078 break;
20079 }
20080
20081 switch (content_type)
20082 {
20083 case DW_LNCT_path:
20084 if (string.has_value ())
20085 fe.name = *string;
20086 break;
20087 case DW_LNCT_directory_index:
20088 if (uint.has_value ())
20089 fe.d_index = (dir_index) *uint;
20090 break;
20091 case DW_LNCT_timestamp:
20092 if (uint.has_value ())
20093 fe.mod_time = *uint;
20094 break;
20095 case DW_LNCT_size:
20096 if (uint.has_value ())
20097 fe.length = *uint;
20098 break;
20099 case DW_LNCT_MD5:
20100 break;
20101 default:
20102 complaint (&symfile_complaints,
20103 _("Unknown format content type %s"),
20104 pulongest (content_type));
20105 }
20106 }
20107
20108 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20109 }
20110
20111 *bufp = buf;
20112 }
20113
20114 /* Read the statement program header starting at OFFSET in
20115 .debug_line, or .debug_line.dwo. Return a pointer
20116 to a struct line_header, allocated using xmalloc.
20117 Returns NULL if there is a problem reading the header, e.g., if it
20118 has a version we don't understand.
20119
20120 NOTE: the strings in the include directory and file name tables of
20121 the returned object point into the dwarf line section buffer,
20122 and must not be freed. */
20123
20124 static line_header_up
20125 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20126 {
20127 const gdb_byte *line_ptr;
20128 unsigned int bytes_read, offset_size;
20129 int i;
20130 const char *cur_dir, *cur_file;
20131 struct dwarf2_section_info *section;
20132 bfd *abfd;
20133 struct dwarf2_per_objfile *dwarf2_per_objfile
20134 = cu->per_cu->dwarf2_per_objfile;
20135
20136 section = get_debug_line_section (cu);
20137 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20138 if (section->buffer == NULL)
20139 {
20140 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20141 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20142 else
20143 complaint (&symfile_complaints, _("missing .debug_line section"));
20144 return 0;
20145 }
20146
20147 /* We can't do this until we know the section is non-empty.
20148 Only then do we know we have such a section. */
20149 abfd = get_section_bfd_owner (section);
20150
20151 /* Make sure that at least there's room for the total_length field.
20152 That could be 12 bytes long, but we're just going to fudge that. */
20153 if (to_underlying (sect_off) + 4 >= section->size)
20154 {
20155 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20156 return 0;
20157 }
20158
20159 line_header_up lh (new line_header ());
20160
20161 lh->sect_off = sect_off;
20162 lh->offset_in_dwz = cu->per_cu->is_dwz;
20163
20164 line_ptr = section->buffer + to_underlying (sect_off);
20165
20166 /* Read in the header. */
20167 lh->total_length =
20168 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20169 &bytes_read, &offset_size);
20170 line_ptr += bytes_read;
20171 if (line_ptr + lh->total_length > (section->buffer + section->size))
20172 {
20173 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20174 return 0;
20175 }
20176 lh->statement_program_end = line_ptr + lh->total_length;
20177 lh->version = read_2_bytes (abfd, line_ptr);
20178 line_ptr += 2;
20179 if (lh->version > 5)
20180 {
20181 /* This is a version we don't understand. The format could have
20182 changed in ways we don't handle properly so just punt. */
20183 complaint (&symfile_complaints,
20184 _("unsupported version in .debug_line section"));
20185 return NULL;
20186 }
20187 if (lh->version >= 5)
20188 {
20189 gdb_byte segment_selector_size;
20190
20191 /* Skip address size. */
20192 read_1_byte (abfd, line_ptr);
20193 line_ptr += 1;
20194
20195 segment_selector_size = read_1_byte (abfd, line_ptr);
20196 line_ptr += 1;
20197 if (segment_selector_size != 0)
20198 {
20199 complaint (&symfile_complaints,
20200 _("unsupported segment selector size %u "
20201 "in .debug_line section"),
20202 segment_selector_size);
20203 return NULL;
20204 }
20205 }
20206 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20207 line_ptr += offset_size;
20208 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20209 line_ptr += 1;
20210 if (lh->version >= 4)
20211 {
20212 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20213 line_ptr += 1;
20214 }
20215 else
20216 lh->maximum_ops_per_instruction = 1;
20217
20218 if (lh->maximum_ops_per_instruction == 0)
20219 {
20220 lh->maximum_ops_per_instruction = 1;
20221 complaint (&symfile_complaints,
20222 _("invalid maximum_ops_per_instruction "
20223 "in `.debug_line' section"));
20224 }
20225
20226 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20227 line_ptr += 1;
20228 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20229 line_ptr += 1;
20230 lh->line_range = read_1_byte (abfd, line_ptr);
20231 line_ptr += 1;
20232 lh->opcode_base = read_1_byte (abfd, line_ptr);
20233 line_ptr += 1;
20234 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20235
20236 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20237 for (i = 1; i < lh->opcode_base; ++i)
20238 {
20239 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20240 line_ptr += 1;
20241 }
20242
20243 if (lh->version >= 5)
20244 {
20245 /* Read directory table. */
20246 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20247 &cu->header,
20248 [] (struct line_header *lh, const char *name,
20249 dir_index d_index, unsigned int mod_time,
20250 unsigned int length)
20251 {
20252 lh->add_include_dir (name);
20253 });
20254
20255 /* Read file name table. */
20256 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20257 &cu->header,
20258 [] (struct line_header *lh, const char *name,
20259 dir_index d_index, unsigned int mod_time,
20260 unsigned int length)
20261 {
20262 lh->add_file_name (name, d_index, mod_time, length);
20263 });
20264 }
20265 else
20266 {
20267 /* Read directory table. */
20268 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20269 {
20270 line_ptr += bytes_read;
20271 lh->add_include_dir (cur_dir);
20272 }
20273 line_ptr += bytes_read;
20274
20275 /* Read file name table. */
20276 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20277 {
20278 unsigned int mod_time, length;
20279 dir_index d_index;
20280
20281 line_ptr += bytes_read;
20282 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20283 line_ptr += bytes_read;
20284 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20285 line_ptr += bytes_read;
20286 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20287 line_ptr += bytes_read;
20288
20289 lh->add_file_name (cur_file, d_index, mod_time, length);
20290 }
20291 line_ptr += bytes_read;
20292 }
20293 lh->statement_program_start = line_ptr;
20294
20295 if (line_ptr > (section->buffer + section->size))
20296 complaint (&symfile_complaints,
20297 _("line number info header doesn't "
20298 "fit in `.debug_line' section"));
20299
20300 return lh;
20301 }
20302
20303 /* Subroutine of dwarf_decode_lines to simplify it.
20304 Return the file name of the psymtab for included file FILE_INDEX
20305 in line header LH of PST.
20306 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20307 If space for the result is malloc'd, *NAME_HOLDER will be set.
20308 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20309
20310 static const char *
20311 psymtab_include_file_name (const struct line_header *lh, int file_index,
20312 const struct partial_symtab *pst,
20313 const char *comp_dir,
20314 gdb::unique_xmalloc_ptr<char> *name_holder)
20315 {
20316 const file_entry &fe = lh->file_names[file_index];
20317 const char *include_name = fe.name;
20318 const char *include_name_to_compare = include_name;
20319 const char *pst_filename;
20320 int file_is_pst;
20321
20322 const char *dir_name = fe.include_dir (lh);
20323
20324 gdb::unique_xmalloc_ptr<char> hold_compare;
20325 if (!IS_ABSOLUTE_PATH (include_name)
20326 && (dir_name != NULL || comp_dir != NULL))
20327 {
20328 /* Avoid creating a duplicate psymtab for PST.
20329 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20330 Before we do the comparison, however, we need to account
20331 for DIR_NAME and COMP_DIR.
20332 First prepend dir_name (if non-NULL). If we still don't
20333 have an absolute path prepend comp_dir (if non-NULL).
20334 However, the directory we record in the include-file's
20335 psymtab does not contain COMP_DIR (to match the
20336 corresponding symtab(s)).
20337
20338 Example:
20339
20340 bash$ cd /tmp
20341 bash$ gcc -g ./hello.c
20342 include_name = "hello.c"
20343 dir_name = "."
20344 DW_AT_comp_dir = comp_dir = "/tmp"
20345 DW_AT_name = "./hello.c"
20346
20347 */
20348
20349 if (dir_name != NULL)
20350 {
20351 name_holder->reset (concat (dir_name, SLASH_STRING,
20352 include_name, (char *) NULL));
20353 include_name = name_holder->get ();
20354 include_name_to_compare = include_name;
20355 }
20356 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20357 {
20358 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20359 include_name, (char *) NULL));
20360 include_name_to_compare = hold_compare.get ();
20361 }
20362 }
20363
20364 pst_filename = pst->filename;
20365 gdb::unique_xmalloc_ptr<char> copied_name;
20366 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20367 {
20368 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20369 pst_filename, (char *) NULL));
20370 pst_filename = copied_name.get ();
20371 }
20372
20373 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20374
20375 if (file_is_pst)
20376 return NULL;
20377 return include_name;
20378 }
20379
20380 /* State machine to track the state of the line number program. */
20381
20382 class lnp_state_machine
20383 {
20384 public:
20385 /* Initialize a machine state for the start of a line number
20386 program. */
20387 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20388
20389 file_entry *current_file ()
20390 {
20391 /* lh->file_names is 0-based, but the file name numbers in the
20392 statement program are 1-based. */
20393 return m_line_header->file_name_at (m_file);
20394 }
20395
20396 /* Record the line in the state machine. END_SEQUENCE is true if
20397 we're processing the end of a sequence. */
20398 void record_line (bool end_sequence);
20399
20400 /* Check address and if invalid nop-out the rest of the lines in this
20401 sequence. */
20402 void check_line_address (struct dwarf2_cu *cu,
20403 const gdb_byte *line_ptr,
20404 CORE_ADDR lowpc, CORE_ADDR address);
20405
20406 void handle_set_discriminator (unsigned int discriminator)
20407 {
20408 m_discriminator = discriminator;
20409 m_line_has_non_zero_discriminator |= discriminator != 0;
20410 }
20411
20412 /* Handle DW_LNE_set_address. */
20413 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20414 {
20415 m_op_index = 0;
20416 address += baseaddr;
20417 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20418 }
20419
20420 /* Handle DW_LNS_advance_pc. */
20421 void handle_advance_pc (CORE_ADDR adjust);
20422
20423 /* Handle a special opcode. */
20424 void handle_special_opcode (unsigned char op_code);
20425
20426 /* Handle DW_LNS_advance_line. */
20427 void handle_advance_line (int line_delta)
20428 {
20429 advance_line (line_delta);
20430 }
20431
20432 /* Handle DW_LNS_set_file. */
20433 void handle_set_file (file_name_index file);
20434
20435 /* Handle DW_LNS_negate_stmt. */
20436 void handle_negate_stmt ()
20437 {
20438 m_is_stmt = !m_is_stmt;
20439 }
20440
20441 /* Handle DW_LNS_const_add_pc. */
20442 void handle_const_add_pc ();
20443
20444 /* Handle DW_LNS_fixed_advance_pc. */
20445 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20446 {
20447 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20448 m_op_index = 0;
20449 }
20450
20451 /* Handle DW_LNS_copy. */
20452 void handle_copy ()
20453 {
20454 record_line (false);
20455 m_discriminator = 0;
20456 }
20457
20458 /* Handle DW_LNE_end_sequence. */
20459 void handle_end_sequence ()
20460 {
20461 m_record_line_callback = ::record_line;
20462 }
20463
20464 private:
20465 /* Advance the line by LINE_DELTA. */
20466 void advance_line (int line_delta)
20467 {
20468 m_line += line_delta;
20469
20470 if (line_delta != 0)
20471 m_line_has_non_zero_discriminator = m_discriminator != 0;
20472 }
20473
20474 gdbarch *m_gdbarch;
20475
20476 /* True if we're recording lines.
20477 Otherwise we're building partial symtabs and are just interested in
20478 finding include files mentioned by the line number program. */
20479 bool m_record_lines_p;
20480
20481 /* The line number header. */
20482 line_header *m_line_header;
20483
20484 /* These are part of the standard DWARF line number state machine,
20485 and initialized according to the DWARF spec. */
20486
20487 unsigned char m_op_index = 0;
20488 /* The line table index (1-based) of the current file. */
20489 file_name_index m_file = (file_name_index) 1;
20490 unsigned int m_line = 1;
20491
20492 /* These are initialized in the constructor. */
20493
20494 CORE_ADDR m_address;
20495 bool m_is_stmt;
20496 unsigned int m_discriminator;
20497
20498 /* Additional bits of state we need to track. */
20499
20500 /* The last file that we called dwarf2_start_subfile for.
20501 This is only used for TLLs. */
20502 unsigned int m_last_file = 0;
20503 /* The last file a line number was recorded for. */
20504 struct subfile *m_last_subfile = NULL;
20505
20506 /* The function to call to record a line. */
20507 record_line_ftype *m_record_line_callback = NULL;
20508
20509 /* The last line number that was recorded, used to coalesce
20510 consecutive entries for the same line. This can happen, for
20511 example, when discriminators are present. PR 17276. */
20512 unsigned int m_last_line = 0;
20513 bool m_line_has_non_zero_discriminator = false;
20514 };
20515
20516 void
20517 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20518 {
20519 CORE_ADDR addr_adj = (((m_op_index + adjust)
20520 / m_line_header->maximum_ops_per_instruction)
20521 * m_line_header->minimum_instruction_length);
20522 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20523 m_op_index = ((m_op_index + adjust)
20524 % m_line_header->maximum_ops_per_instruction);
20525 }
20526
20527 void
20528 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20529 {
20530 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20531 CORE_ADDR addr_adj = (((m_op_index
20532 + (adj_opcode / m_line_header->line_range))
20533 / m_line_header->maximum_ops_per_instruction)
20534 * m_line_header->minimum_instruction_length);
20535 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20536 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20537 % m_line_header->maximum_ops_per_instruction);
20538
20539 int line_delta = (m_line_header->line_base
20540 + (adj_opcode % m_line_header->line_range));
20541 advance_line (line_delta);
20542 record_line (false);
20543 m_discriminator = 0;
20544 }
20545
20546 void
20547 lnp_state_machine::handle_set_file (file_name_index file)
20548 {
20549 m_file = file;
20550
20551 const file_entry *fe = current_file ();
20552 if (fe == NULL)
20553 dwarf2_debug_line_missing_file_complaint ();
20554 else if (m_record_lines_p)
20555 {
20556 const char *dir = fe->include_dir (m_line_header);
20557
20558 m_last_subfile = current_subfile;
20559 m_line_has_non_zero_discriminator = m_discriminator != 0;
20560 dwarf2_start_subfile (fe->name, dir);
20561 }
20562 }
20563
20564 void
20565 lnp_state_machine::handle_const_add_pc ()
20566 {
20567 CORE_ADDR adjust
20568 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20569
20570 CORE_ADDR addr_adj
20571 = (((m_op_index + adjust)
20572 / m_line_header->maximum_ops_per_instruction)
20573 * m_line_header->minimum_instruction_length);
20574
20575 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20576 m_op_index = ((m_op_index + adjust)
20577 % m_line_header->maximum_ops_per_instruction);
20578 }
20579
20580 /* Ignore this record_line request. */
20581
20582 static void
20583 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20584 {
20585 return;
20586 }
20587
20588 /* Return non-zero if we should add LINE to the line number table.
20589 LINE is the line to add, LAST_LINE is the last line that was added,
20590 LAST_SUBFILE is the subfile for LAST_LINE.
20591 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20592 had a non-zero discriminator.
20593
20594 We have to be careful in the presence of discriminators.
20595 E.g., for this line:
20596
20597 for (i = 0; i < 100000; i++);
20598
20599 clang can emit four line number entries for that one line,
20600 each with a different discriminator.
20601 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20602
20603 However, we want gdb to coalesce all four entries into one.
20604 Otherwise the user could stepi into the middle of the line and
20605 gdb would get confused about whether the pc really was in the
20606 middle of the line.
20607
20608 Things are further complicated by the fact that two consecutive
20609 line number entries for the same line is a heuristic used by gcc
20610 to denote the end of the prologue. So we can't just discard duplicate
20611 entries, we have to be selective about it. The heuristic we use is
20612 that we only collapse consecutive entries for the same line if at least
20613 one of those entries has a non-zero discriminator. PR 17276.
20614
20615 Note: Addresses in the line number state machine can never go backwards
20616 within one sequence, thus this coalescing is ok. */
20617
20618 static int
20619 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20620 int line_has_non_zero_discriminator,
20621 struct subfile *last_subfile)
20622 {
20623 if (current_subfile != last_subfile)
20624 return 1;
20625 if (line != last_line)
20626 return 1;
20627 /* Same line for the same file that we've seen already.
20628 As a last check, for pr 17276, only record the line if the line
20629 has never had a non-zero discriminator. */
20630 if (!line_has_non_zero_discriminator)
20631 return 1;
20632 return 0;
20633 }
20634
20635 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20636 in the line table of subfile SUBFILE. */
20637
20638 static void
20639 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20640 unsigned int line, CORE_ADDR address,
20641 record_line_ftype p_record_line)
20642 {
20643 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20644
20645 if (dwarf_line_debug)
20646 {
20647 fprintf_unfiltered (gdb_stdlog,
20648 "Recording line %u, file %s, address %s\n",
20649 line, lbasename (subfile->name),
20650 paddress (gdbarch, address));
20651 }
20652
20653 (*p_record_line) (subfile, line, addr);
20654 }
20655
20656 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20657 Mark the end of a set of line number records.
20658 The arguments are the same as for dwarf_record_line_1.
20659 If SUBFILE is NULL the request is ignored. */
20660
20661 static void
20662 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20663 CORE_ADDR address, record_line_ftype p_record_line)
20664 {
20665 if (subfile == NULL)
20666 return;
20667
20668 if (dwarf_line_debug)
20669 {
20670 fprintf_unfiltered (gdb_stdlog,
20671 "Finishing current line, file %s, address %s\n",
20672 lbasename (subfile->name),
20673 paddress (gdbarch, address));
20674 }
20675
20676 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20677 }
20678
20679 void
20680 lnp_state_machine::record_line (bool end_sequence)
20681 {
20682 if (dwarf_line_debug)
20683 {
20684 fprintf_unfiltered (gdb_stdlog,
20685 "Processing actual line %u: file %u,"
20686 " address %s, is_stmt %u, discrim %u\n",
20687 m_line, to_underlying (m_file),
20688 paddress (m_gdbarch, m_address),
20689 m_is_stmt, m_discriminator);
20690 }
20691
20692 file_entry *fe = current_file ();
20693
20694 if (fe == NULL)
20695 dwarf2_debug_line_missing_file_complaint ();
20696 /* For now we ignore lines not starting on an instruction boundary.
20697 But not when processing end_sequence for compatibility with the
20698 previous version of the code. */
20699 else if (m_op_index == 0 || end_sequence)
20700 {
20701 fe->included_p = 1;
20702 if (m_record_lines_p && m_is_stmt)
20703 {
20704 if (m_last_subfile != current_subfile || end_sequence)
20705 {
20706 dwarf_finish_line (m_gdbarch, m_last_subfile,
20707 m_address, m_record_line_callback);
20708 }
20709
20710 if (!end_sequence)
20711 {
20712 if (dwarf_record_line_p (m_line, m_last_line,
20713 m_line_has_non_zero_discriminator,
20714 m_last_subfile))
20715 {
20716 dwarf_record_line_1 (m_gdbarch, current_subfile,
20717 m_line, m_address,
20718 m_record_line_callback);
20719 }
20720 m_last_subfile = current_subfile;
20721 m_last_line = m_line;
20722 }
20723 }
20724 }
20725 }
20726
20727 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20728 bool record_lines_p)
20729 {
20730 m_gdbarch = arch;
20731 m_record_lines_p = record_lines_p;
20732 m_line_header = lh;
20733
20734 m_record_line_callback = ::record_line;
20735
20736 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20737 was a line entry for it so that the backend has a chance to adjust it
20738 and also record it in case it needs it. This is currently used by MIPS
20739 code, cf. `mips_adjust_dwarf2_line'. */
20740 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20741 m_is_stmt = lh->default_is_stmt;
20742 m_discriminator = 0;
20743 }
20744
20745 void
20746 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20747 const gdb_byte *line_ptr,
20748 CORE_ADDR lowpc, CORE_ADDR address)
20749 {
20750 /* If address < lowpc then it's not a usable value, it's outside the
20751 pc range of the CU. However, we restrict the test to only address
20752 values of zero to preserve GDB's previous behaviour which is to
20753 handle the specific case of a function being GC'd by the linker. */
20754
20755 if (address == 0 && address < lowpc)
20756 {
20757 /* This line table is for a function which has been
20758 GCd by the linker. Ignore it. PR gdb/12528 */
20759
20760 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20761 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20762
20763 complaint (&symfile_complaints,
20764 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20765 line_offset, objfile_name (objfile));
20766 m_record_line_callback = noop_record_line;
20767 /* Note: record_line_callback is left as noop_record_line until
20768 we see DW_LNE_end_sequence. */
20769 }
20770 }
20771
20772 /* Subroutine of dwarf_decode_lines to simplify it.
20773 Process the line number information in LH.
20774 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20775 program in order to set included_p for every referenced header. */
20776
20777 static void
20778 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20779 const int decode_for_pst_p, CORE_ADDR lowpc)
20780 {
20781 const gdb_byte *line_ptr, *extended_end;
20782 const gdb_byte *line_end;
20783 unsigned int bytes_read, extended_len;
20784 unsigned char op_code, extended_op;
20785 CORE_ADDR baseaddr;
20786 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20787 bfd *abfd = objfile->obfd;
20788 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20789 /* True if we're recording line info (as opposed to building partial
20790 symtabs and just interested in finding include files mentioned by
20791 the line number program). */
20792 bool record_lines_p = !decode_for_pst_p;
20793
20794 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20795
20796 line_ptr = lh->statement_program_start;
20797 line_end = lh->statement_program_end;
20798
20799 /* Read the statement sequences until there's nothing left. */
20800 while (line_ptr < line_end)
20801 {
20802 /* The DWARF line number program state machine. Reset the state
20803 machine at the start of each sequence. */
20804 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20805 bool end_sequence = false;
20806
20807 if (record_lines_p)
20808 {
20809 /* Start a subfile for the current file of the state
20810 machine. */
20811 const file_entry *fe = state_machine.current_file ();
20812
20813 if (fe != NULL)
20814 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20815 }
20816
20817 /* Decode the table. */
20818 while (line_ptr < line_end && !end_sequence)
20819 {
20820 op_code = read_1_byte (abfd, line_ptr);
20821 line_ptr += 1;
20822
20823 if (op_code >= lh->opcode_base)
20824 {
20825 /* Special opcode. */
20826 state_machine.handle_special_opcode (op_code);
20827 }
20828 else switch (op_code)
20829 {
20830 case DW_LNS_extended_op:
20831 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20832 &bytes_read);
20833 line_ptr += bytes_read;
20834 extended_end = line_ptr + extended_len;
20835 extended_op = read_1_byte (abfd, line_ptr);
20836 line_ptr += 1;
20837 switch (extended_op)
20838 {
20839 case DW_LNE_end_sequence:
20840 state_machine.handle_end_sequence ();
20841 end_sequence = true;
20842 break;
20843 case DW_LNE_set_address:
20844 {
20845 CORE_ADDR address
20846 = read_address (abfd, line_ptr, cu, &bytes_read);
20847 line_ptr += bytes_read;
20848
20849 state_machine.check_line_address (cu, line_ptr,
20850 lowpc, address);
20851 state_machine.handle_set_address (baseaddr, address);
20852 }
20853 break;
20854 case DW_LNE_define_file:
20855 {
20856 const char *cur_file;
20857 unsigned int mod_time, length;
20858 dir_index dindex;
20859
20860 cur_file = read_direct_string (abfd, line_ptr,
20861 &bytes_read);
20862 line_ptr += bytes_read;
20863 dindex = (dir_index)
20864 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20865 line_ptr += bytes_read;
20866 mod_time =
20867 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20868 line_ptr += bytes_read;
20869 length =
20870 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20871 line_ptr += bytes_read;
20872 lh->add_file_name (cur_file, dindex, mod_time, length);
20873 }
20874 break;
20875 case DW_LNE_set_discriminator:
20876 {
20877 /* The discriminator is not interesting to the
20878 debugger; just ignore it. We still need to
20879 check its value though:
20880 if there are consecutive entries for the same
20881 (non-prologue) line we want to coalesce them.
20882 PR 17276. */
20883 unsigned int discr
20884 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20885 line_ptr += bytes_read;
20886
20887 state_machine.handle_set_discriminator (discr);
20888 }
20889 break;
20890 default:
20891 complaint (&symfile_complaints,
20892 _("mangled .debug_line section"));
20893 return;
20894 }
20895 /* Make sure that we parsed the extended op correctly. If e.g.
20896 we expected a different address size than the producer used,
20897 we may have read the wrong number of bytes. */
20898 if (line_ptr != extended_end)
20899 {
20900 complaint (&symfile_complaints,
20901 _("mangled .debug_line section"));
20902 return;
20903 }
20904 break;
20905 case DW_LNS_copy:
20906 state_machine.handle_copy ();
20907 break;
20908 case DW_LNS_advance_pc:
20909 {
20910 CORE_ADDR adjust
20911 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20912 line_ptr += bytes_read;
20913
20914 state_machine.handle_advance_pc (adjust);
20915 }
20916 break;
20917 case DW_LNS_advance_line:
20918 {
20919 int line_delta
20920 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20921 line_ptr += bytes_read;
20922
20923 state_machine.handle_advance_line (line_delta);
20924 }
20925 break;
20926 case DW_LNS_set_file:
20927 {
20928 file_name_index file
20929 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20930 &bytes_read);
20931 line_ptr += bytes_read;
20932
20933 state_machine.handle_set_file (file);
20934 }
20935 break;
20936 case DW_LNS_set_column:
20937 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20938 line_ptr += bytes_read;
20939 break;
20940 case DW_LNS_negate_stmt:
20941 state_machine.handle_negate_stmt ();
20942 break;
20943 case DW_LNS_set_basic_block:
20944 break;
20945 /* Add to the address register of the state machine the
20946 address increment value corresponding to special opcode
20947 255. I.e., this value is scaled by the minimum
20948 instruction length since special opcode 255 would have
20949 scaled the increment. */
20950 case DW_LNS_const_add_pc:
20951 state_machine.handle_const_add_pc ();
20952 break;
20953 case DW_LNS_fixed_advance_pc:
20954 {
20955 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20956 line_ptr += 2;
20957
20958 state_machine.handle_fixed_advance_pc (addr_adj);
20959 }
20960 break;
20961 default:
20962 {
20963 /* Unknown standard opcode, ignore it. */
20964 int i;
20965
20966 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20967 {
20968 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20969 line_ptr += bytes_read;
20970 }
20971 }
20972 }
20973 }
20974
20975 if (!end_sequence)
20976 dwarf2_debug_line_missing_end_sequence_complaint ();
20977
20978 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20979 in which case we still finish recording the last line). */
20980 state_machine.record_line (true);
20981 }
20982 }
20983
20984 /* Decode the Line Number Program (LNP) for the given line_header
20985 structure and CU. The actual information extracted and the type
20986 of structures created from the LNP depends on the value of PST.
20987
20988 1. If PST is NULL, then this procedure uses the data from the program
20989 to create all necessary symbol tables, and their linetables.
20990
20991 2. If PST is not NULL, this procedure reads the program to determine
20992 the list of files included by the unit represented by PST, and
20993 builds all the associated partial symbol tables.
20994
20995 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20996 It is used for relative paths in the line table.
20997 NOTE: When processing partial symtabs (pst != NULL),
20998 comp_dir == pst->dirname.
20999
21000 NOTE: It is important that psymtabs have the same file name (via strcmp)
21001 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21002 symtab we don't use it in the name of the psymtabs we create.
21003 E.g. expand_line_sal requires this when finding psymtabs to expand.
21004 A good testcase for this is mb-inline.exp.
21005
21006 LOWPC is the lowest address in CU (or 0 if not known).
21007
21008 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21009 for its PC<->lines mapping information. Otherwise only the filename
21010 table is read in. */
21011
21012 static void
21013 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21014 struct dwarf2_cu *cu, struct partial_symtab *pst,
21015 CORE_ADDR lowpc, int decode_mapping)
21016 {
21017 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21018 const int decode_for_pst_p = (pst != NULL);
21019
21020 if (decode_mapping)
21021 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21022
21023 if (decode_for_pst_p)
21024 {
21025 int file_index;
21026
21027 /* Now that we're done scanning the Line Header Program, we can
21028 create the psymtab of each included file. */
21029 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21030 if (lh->file_names[file_index].included_p == 1)
21031 {
21032 gdb::unique_xmalloc_ptr<char> name_holder;
21033 const char *include_name =
21034 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21035 &name_holder);
21036 if (include_name != NULL)
21037 dwarf2_create_include_psymtab (include_name, pst, objfile);
21038 }
21039 }
21040 else
21041 {
21042 /* Make sure a symtab is created for every file, even files
21043 which contain only variables (i.e. no code with associated
21044 line numbers). */
21045 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21046 int i;
21047
21048 for (i = 0; i < lh->file_names.size (); i++)
21049 {
21050 file_entry &fe = lh->file_names[i];
21051
21052 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21053
21054 if (current_subfile->symtab == NULL)
21055 {
21056 current_subfile->symtab
21057 = allocate_symtab (cust, current_subfile->name);
21058 }
21059 fe.symtab = current_subfile->symtab;
21060 }
21061 }
21062 }
21063
21064 /* Start a subfile for DWARF. FILENAME is the name of the file and
21065 DIRNAME the name of the source directory which contains FILENAME
21066 or NULL if not known.
21067 This routine tries to keep line numbers from identical absolute and
21068 relative file names in a common subfile.
21069
21070 Using the `list' example from the GDB testsuite, which resides in
21071 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21072 of /srcdir/list0.c yields the following debugging information for list0.c:
21073
21074 DW_AT_name: /srcdir/list0.c
21075 DW_AT_comp_dir: /compdir
21076 files.files[0].name: list0.h
21077 files.files[0].dir: /srcdir
21078 files.files[1].name: list0.c
21079 files.files[1].dir: /srcdir
21080
21081 The line number information for list0.c has to end up in a single
21082 subfile, so that `break /srcdir/list0.c:1' works as expected.
21083 start_subfile will ensure that this happens provided that we pass the
21084 concatenation of files.files[1].dir and files.files[1].name as the
21085 subfile's name. */
21086
21087 static void
21088 dwarf2_start_subfile (const char *filename, const char *dirname)
21089 {
21090 char *copy = NULL;
21091
21092 /* In order not to lose the line information directory,
21093 we concatenate it to the filename when it makes sense.
21094 Note that the Dwarf3 standard says (speaking of filenames in line
21095 information): ``The directory index is ignored for file names
21096 that represent full path names''. Thus ignoring dirname in the
21097 `else' branch below isn't an issue. */
21098
21099 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21100 {
21101 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21102 filename = copy;
21103 }
21104
21105 start_subfile (filename);
21106
21107 if (copy != NULL)
21108 xfree (copy);
21109 }
21110
21111 /* Start a symtab for DWARF.
21112 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21113
21114 static struct compunit_symtab *
21115 dwarf2_start_symtab (struct dwarf2_cu *cu,
21116 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21117 {
21118 struct compunit_symtab *cust
21119 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21120 low_pc, cu->language);
21121
21122 record_debugformat ("DWARF 2");
21123 record_producer (cu->producer);
21124
21125 /* We assume that we're processing GCC output. */
21126 processing_gcc_compilation = 2;
21127
21128 cu->processing_has_namespace_info = 0;
21129
21130 return cust;
21131 }
21132
21133 static void
21134 var_decode_location (struct attribute *attr, struct symbol *sym,
21135 struct dwarf2_cu *cu)
21136 {
21137 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21138 struct comp_unit_head *cu_header = &cu->header;
21139
21140 /* NOTE drow/2003-01-30: There used to be a comment and some special
21141 code here to turn a symbol with DW_AT_external and a
21142 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21143 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21144 with some versions of binutils) where shared libraries could have
21145 relocations against symbols in their debug information - the
21146 minimal symbol would have the right address, but the debug info
21147 would not. It's no longer necessary, because we will explicitly
21148 apply relocations when we read in the debug information now. */
21149
21150 /* A DW_AT_location attribute with no contents indicates that a
21151 variable has been optimized away. */
21152 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21153 {
21154 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21155 return;
21156 }
21157
21158 /* Handle one degenerate form of location expression specially, to
21159 preserve GDB's previous behavior when section offsets are
21160 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21161 then mark this symbol as LOC_STATIC. */
21162
21163 if (attr_form_is_block (attr)
21164 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21165 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21166 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21167 && (DW_BLOCK (attr)->size
21168 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21169 {
21170 unsigned int dummy;
21171
21172 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21173 SYMBOL_VALUE_ADDRESS (sym) =
21174 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21175 else
21176 SYMBOL_VALUE_ADDRESS (sym) =
21177 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21178 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21179 fixup_symbol_section (sym, objfile);
21180 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21181 SYMBOL_SECTION (sym));
21182 return;
21183 }
21184
21185 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21186 expression evaluator, and use LOC_COMPUTED only when necessary
21187 (i.e. when the value of a register or memory location is
21188 referenced, or a thread-local block, etc.). Then again, it might
21189 not be worthwhile. I'm assuming that it isn't unless performance
21190 or memory numbers show me otherwise. */
21191
21192 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21193
21194 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21195 cu->has_loclist = 1;
21196 }
21197
21198 /* Given a pointer to a DWARF information entry, figure out if we need
21199 to make a symbol table entry for it, and if so, create a new entry
21200 and return a pointer to it.
21201 If TYPE is NULL, determine symbol type from the die, otherwise
21202 used the passed type.
21203 If SPACE is not NULL, use it to hold the new symbol. If it is
21204 NULL, allocate a new symbol on the objfile's obstack. */
21205
21206 static struct symbol *
21207 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21208 struct symbol *space)
21209 {
21210 struct dwarf2_per_objfile *dwarf2_per_objfile
21211 = cu->per_cu->dwarf2_per_objfile;
21212 struct objfile *objfile = dwarf2_per_objfile->objfile;
21213 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21214 struct symbol *sym = NULL;
21215 const char *name;
21216 struct attribute *attr = NULL;
21217 struct attribute *attr2 = NULL;
21218 CORE_ADDR baseaddr;
21219 struct pending **list_to_add = NULL;
21220
21221 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21222
21223 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21224
21225 name = dwarf2_name (die, cu);
21226 if (name)
21227 {
21228 const char *linkagename;
21229 int suppress_add = 0;
21230
21231 if (space)
21232 sym = space;
21233 else
21234 sym = allocate_symbol (objfile);
21235 OBJSTAT (objfile, n_syms++);
21236
21237 /* Cache this symbol's name and the name's demangled form (if any). */
21238 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21239 linkagename = dwarf2_physname (name, die, cu);
21240 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21241
21242 /* Fortran does not have mangling standard and the mangling does differ
21243 between gfortran, iFort etc. */
21244 if (cu->language == language_fortran
21245 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21246 symbol_set_demangled_name (&(sym->ginfo),
21247 dwarf2_full_name (name, die, cu),
21248 NULL);
21249
21250 /* Default assumptions.
21251 Use the passed type or decode it from the die. */
21252 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21253 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21254 if (type != NULL)
21255 SYMBOL_TYPE (sym) = type;
21256 else
21257 SYMBOL_TYPE (sym) = die_type (die, cu);
21258 attr = dwarf2_attr (die,
21259 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21260 cu);
21261 if (attr)
21262 {
21263 SYMBOL_LINE (sym) = DW_UNSND (attr);
21264 }
21265
21266 attr = dwarf2_attr (die,
21267 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21268 cu);
21269 if (attr)
21270 {
21271 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21272 struct file_entry *fe;
21273
21274 if (cu->line_header != NULL)
21275 fe = cu->line_header->file_name_at (file_index);
21276 else
21277 fe = NULL;
21278
21279 if (fe == NULL)
21280 complaint (&symfile_complaints,
21281 _("file index out of range"));
21282 else
21283 symbol_set_symtab (sym, fe->symtab);
21284 }
21285
21286 switch (die->tag)
21287 {
21288 case DW_TAG_label:
21289 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21290 if (attr)
21291 {
21292 CORE_ADDR addr;
21293
21294 addr = attr_value_as_address (attr);
21295 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21296 SYMBOL_VALUE_ADDRESS (sym) = addr;
21297 }
21298 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21299 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21300 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21301 add_symbol_to_list (sym, cu->list_in_scope);
21302 break;
21303 case DW_TAG_subprogram:
21304 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21305 finish_block. */
21306 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21307 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21308 if ((attr2 && (DW_UNSND (attr2) != 0))
21309 || cu->language == language_ada)
21310 {
21311 /* Subprograms marked external are stored as a global symbol.
21312 Ada subprograms, whether marked external or not, are always
21313 stored as a global symbol, because we want to be able to
21314 access them globally. For instance, we want to be able
21315 to break on a nested subprogram without having to
21316 specify the context. */
21317 list_to_add = &global_symbols;
21318 }
21319 else
21320 {
21321 list_to_add = cu->list_in_scope;
21322 }
21323 break;
21324 case DW_TAG_inlined_subroutine:
21325 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21326 finish_block. */
21327 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21328 SYMBOL_INLINED (sym) = 1;
21329 list_to_add = cu->list_in_scope;
21330 break;
21331 case DW_TAG_template_value_param:
21332 suppress_add = 1;
21333 /* Fall through. */
21334 case DW_TAG_constant:
21335 case DW_TAG_variable:
21336 case DW_TAG_member:
21337 /* Compilation with minimal debug info may result in
21338 variables with missing type entries. Change the
21339 misleading `void' type to something sensible. */
21340 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21341 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21342
21343 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21344 /* In the case of DW_TAG_member, we should only be called for
21345 static const members. */
21346 if (die->tag == DW_TAG_member)
21347 {
21348 /* dwarf2_add_field uses die_is_declaration,
21349 so we do the same. */
21350 gdb_assert (die_is_declaration (die, cu));
21351 gdb_assert (attr);
21352 }
21353 if (attr)
21354 {
21355 dwarf2_const_value (attr, sym, cu);
21356 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21357 if (!suppress_add)
21358 {
21359 if (attr2 && (DW_UNSND (attr2) != 0))
21360 list_to_add = &global_symbols;
21361 else
21362 list_to_add = cu->list_in_scope;
21363 }
21364 break;
21365 }
21366 attr = dwarf2_attr (die, DW_AT_location, cu);
21367 if (attr)
21368 {
21369 var_decode_location (attr, sym, cu);
21370 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21371
21372 /* Fortran explicitly imports any global symbols to the local
21373 scope by DW_TAG_common_block. */
21374 if (cu->language == language_fortran && die->parent
21375 && die->parent->tag == DW_TAG_common_block)
21376 attr2 = NULL;
21377
21378 if (SYMBOL_CLASS (sym) == LOC_STATIC
21379 && SYMBOL_VALUE_ADDRESS (sym) == 0
21380 && !dwarf2_per_objfile->has_section_at_zero)
21381 {
21382 /* When a static variable is eliminated by the linker,
21383 the corresponding debug information is not stripped
21384 out, but the variable address is set to null;
21385 do not add such variables into symbol table. */
21386 }
21387 else if (attr2 && (DW_UNSND (attr2) != 0))
21388 {
21389 /* Workaround gfortran PR debug/40040 - it uses
21390 DW_AT_location for variables in -fPIC libraries which may
21391 get overriden by other libraries/executable and get
21392 a different address. Resolve it by the minimal symbol
21393 which may come from inferior's executable using copy
21394 relocation. Make this workaround only for gfortran as for
21395 other compilers GDB cannot guess the minimal symbol
21396 Fortran mangling kind. */
21397 if (cu->language == language_fortran && die->parent
21398 && die->parent->tag == DW_TAG_module
21399 && cu->producer
21400 && startswith (cu->producer, "GNU Fortran"))
21401 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21402
21403 /* A variable with DW_AT_external is never static,
21404 but it may be block-scoped. */
21405 list_to_add = (cu->list_in_scope == &file_symbols
21406 ? &global_symbols : cu->list_in_scope);
21407 }
21408 else
21409 list_to_add = cu->list_in_scope;
21410 }
21411 else
21412 {
21413 /* We do not know the address of this symbol.
21414 If it is an external symbol and we have type information
21415 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21416 The address of the variable will then be determined from
21417 the minimal symbol table whenever the variable is
21418 referenced. */
21419 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21420
21421 /* Fortran explicitly imports any global symbols to the local
21422 scope by DW_TAG_common_block. */
21423 if (cu->language == language_fortran && die->parent
21424 && die->parent->tag == DW_TAG_common_block)
21425 {
21426 /* SYMBOL_CLASS doesn't matter here because
21427 read_common_block is going to reset it. */
21428 if (!suppress_add)
21429 list_to_add = cu->list_in_scope;
21430 }
21431 else if (attr2 && (DW_UNSND (attr2) != 0)
21432 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21433 {
21434 /* A variable with DW_AT_external is never static, but it
21435 may be block-scoped. */
21436 list_to_add = (cu->list_in_scope == &file_symbols
21437 ? &global_symbols : cu->list_in_scope);
21438
21439 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21440 }
21441 else if (!die_is_declaration (die, cu))
21442 {
21443 /* Use the default LOC_OPTIMIZED_OUT class. */
21444 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21445 if (!suppress_add)
21446 list_to_add = cu->list_in_scope;
21447 }
21448 }
21449 break;
21450 case DW_TAG_formal_parameter:
21451 /* If we are inside a function, mark this as an argument. If
21452 not, we might be looking at an argument to an inlined function
21453 when we do not have enough information to show inlined frames;
21454 pretend it's a local variable in that case so that the user can
21455 still see it. */
21456 if (context_stack_depth > 0
21457 && context_stack[context_stack_depth - 1].name != NULL)
21458 SYMBOL_IS_ARGUMENT (sym) = 1;
21459 attr = dwarf2_attr (die, DW_AT_location, cu);
21460 if (attr)
21461 {
21462 var_decode_location (attr, sym, cu);
21463 }
21464 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21465 if (attr)
21466 {
21467 dwarf2_const_value (attr, sym, cu);
21468 }
21469
21470 list_to_add = cu->list_in_scope;
21471 break;
21472 case DW_TAG_unspecified_parameters:
21473 /* From varargs functions; gdb doesn't seem to have any
21474 interest in this information, so just ignore it for now.
21475 (FIXME?) */
21476 break;
21477 case DW_TAG_template_type_param:
21478 suppress_add = 1;
21479 /* Fall through. */
21480 case DW_TAG_class_type:
21481 case DW_TAG_interface_type:
21482 case DW_TAG_structure_type:
21483 case DW_TAG_union_type:
21484 case DW_TAG_set_type:
21485 case DW_TAG_enumeration_type:
21486 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21487 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21488
21489 {
21490 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21491 really ever be static objects: otherwise, if you try
21492 to, say, break of a class's method and you're in a file
21493 which doesn't mention that class, it won't work unless
21494 the check for all static symbols in lookup_symbol_aux
21495 saves you. See the OtherFileClass tests in
21496 gdb.c++/namespace.exp. */
21497
21498 if (!suppress_add)
21499 {
21500 list_to_add = (cu->list_in_scope == &file_symbols
21501 && cu->language == language_cplus
21502 ? &global_symbols : cu->list_in_scope);
21503
21504 /* The semantics of C++ state that "struct foo {
21505 ... }" also defines a typedef for "foo". */
21506 if (cu->language == language_cplus
21507 || cu->language == language_ada
21508 || cu->language == language_d
21509 || cu->language == language_rust)
21510 {
21511 /* The symbol's name is already allocated along
21512 with this objfile, so we don't need to
21513 duplicate it for the type. */
21514 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21515 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21516 }
21517 }
21518 }
21519 break;
21520 case DW_TAG_typedef:
21521 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21522 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21523 list_to_add = cu->list_in_scope;
21524 break;
21525 case DW_TAG_base_type:
21526 case DW_TAG_subrange_type:
21527 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21528 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21529 list_to_add = cu->list_in_scope;
21530 break;
21531 case DW_TAG_enumerator:
21532 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21533 if (attr)
21534 {
21535 dwarf2_const_value (attr, sym, cu);
21536 }
21537 {
21538 /* NOTE: carlton/2003-11-10: See comment above in the
21539 DW_TAG_class_type, etc. block. */
21540
21541 list_to_add = (cu->list_in_scope == &file_symbols
21542 && cu->language == language_cplus
21543 ? &global_symbols : cu->list_in_scope);
21544 }
21545 break;
21546 case DW_TAG_imported_declaration:
21547 case DW_TAG_namespace:
21548 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21549 list_to_add = &global_symbols;
21550 break;
21551 case DW_TAG_module:
21552 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21553 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21554 list_to_add = &global_symbols;
21555 break;
21556 case DW_TAG_common_block:
21557 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21558 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21559 add_symbol_to_list (sym, cu->list_in_scope);
21560 break;
21561 default:
21562 /* Not a tag we recognize. Hopefully we aren't processing
21563 trash data, but since we must specifically ignore things
21564 we don't recognize, there is nothing else we should do at
21565 this point. */
21566 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21567 dwarf_tag_name (die->tag));
21568 break;
21569 }
21570
21571 if (suppress_add)
21572 {
21573 sym->hash_next = objfile->template_symbols;
21574 objfile->template_symbols = sym;
21575 list_to_add = NULL;
21576 }
21577
21578 if (list_to_add != NULL)
21579 add_symbol_to_list (sym, list_to_add);
21580
21581 /* For the benefit of old versions of GCC, check for anonymous
21582 namespaces based on the demangled name. */
21583 if (!cu->processing_has_namespace_info
21584 && cu->language == language_cplus)
21585 cp_scan_for_anonymous_namespaces (sym, objfile);
21586 }
21587 return (sym);
21588 }
21589
21590 /* Given an attr with a DW_FORM_dataN value in host byte order,
21591 zero-extend it as appropriate for the symbol's type. The DWARF
21592 standard (v4) is not entirely clear about the meaning of using
21593 DW_FORM_dataN for a constant with a signed type, where the type is
21594 wider than the data. The conclusion of a discussion on the DWARF
21595 list was that this is unspecified. We choose to always zero-extend
21596 because that is the interpretation long in use by GCC. */
21597
21598 static gdb_byte *
21599 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21600 struct dwarf2_cu *cu, LONGEST *value, int bits)
21601 {
21602 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21603 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21604 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21605 LONGEST l = DW_UNSND (attr);
21606
21607 if (bits < sizeof (*value) * 8)
21608 {
21609 l &= ((LONGEST) 1 << bits) - 1;
21610 *value = l;
21611 }
21612 else if (bits == sizeof (*value) * 8)
21613 *value = l;
21614 else
21615 {
21616 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21617 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21618 return bytes;
21619 }
21620
21621 return NULL;
21622 }
21623
21624 /* Read a constant value from an attribute. Either set *VALUE, or if
21625 the value does not fit in *VALUE, set *BYTES - either already
21626 allocated on the objfile obstack, or newly allocated on OBSTACK,
21627 or, set *BATON, if we translated the constant to a location
21628 expression. */
21629
21630 static void
21631 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21632 const char *name, struct obstack *obstack,
21633 struct dwarf2_cu *cu,
21634 LONGEST *value, const gdb_byte **bytes,
21635 struct dwarf2_locexpr_baton **baton)
21636 {
21637 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21638 struct comp_unit_head *cu_header = &cu->header;
21639 struct dwarf_block *blk;
21640 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21641 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21642
21643 *value = 0;
21644 *bytes = NULL;
21645 *baton = NULL;
21646
21647 switch (attr->form)
21648 {
21649 case DW_FORM_addr:
21650 case DW_FORM_GNU_addr_index:
21651 {
21652 gdb_byte *data;
21653
21654 if (TYPE_LENGTH (type) != cu_header->addr_size)
21655 dwarf2_const_value_length_mismatch_complaint (name,
21656 cu_header->addr_size,
21657 TYPE_LENGTH (type));
21658 /* Symbols of this form are reasonably rare, so we just
21659 piggyback on the existing location code rather than writing
21660 a new implementation of symbol_computed_ops. */
21661 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21662 (*baton)->per_cu = cu->per_cu;
21663 gdb_assert ((*baton)->per_cu);
21664
21665 (*baton)->size = 2 + cu_header->addr_size;
21666 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21667 (*baton)->data = data;
21668
21669 data[0] = DW_OP_addr;
21670 store_unsigned_integer (&data[1], cu_header->addr_size,
21671 byte_order, DW_ADDR (attr));
21672 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21673 }
21674 break;
21675 case DW_FORM_string:
21676 case DW_FORM_strp:
21677 case DW_FORM_GNU_str_index:
21678 case DW_FORM_GNU_strp_alt:
21679 /* DW_STRING is already allocated on the objfile obstack, point
21680 directly to it. */
21681 *bytes = (const gdb_byte *) DW_STRING (attr);
21682 break;
21683 case DW_FORM_block1:
21684 case DW_FORM_block2:
21685 case DW_FORM_block4:
21686 case DW_FORM_block:
21687 case DW_FORM_exprloc:
21688 case DW_FORM_data16:
21689 blk = DW_BLOCK (attr);
21690 if (TYPE_LENGTH (type) != blk->size)
21691 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21692 TYPE_LENGTH (type));
21693 *bytes = blk->data;
21694 break;
21695
21696 /* The DW_AT_const_value attributes are supposed to carry the
21697 symbol's value "represented as it would be on the target
21698 architecture." By the time we get here, it's already been
21699 converted to host endianness, so we just need to sign- or
21700 zero-extend it as appropriate. */
21701 case DW_FORM_data1:
21702 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21703 break;
21704 case DW_FORM_data2:
21705 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21706 break;
21707 case DW_FORM_data4:
21708 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21709 break;
21710 case DW_FORM_data8:
21711 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21712 break;
21713
21714 case DW_FORM_sdata:
21715 case DW_FORM_implicit_const:
21716 *value = DW_SND (attr);
21717 break;
21718
21719 case DW_FORM_udata:
21720 *value = DW_UNSND (attr);
21721 break;
21722
21723 default:
21724 complaint (&symfile_complaints,
21725 _("unsupported const value attribute form: '%s'"),
21726 dwarf_form_name (attr->form));
21727 *value = 0;
21728 break;
21729 }
21730 }
21731
21732
21733 /* Copy constant value from an attribute to a symbol. */
21734
21735 static void
21736 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21737 struct dwarf2_cu *cu)
21738 {
21739 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21740 LONGEST value;
21741 const gdb_byte *bytes;
21742 struct dwarf2_locexpr_baton *baton;
21743
21744 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21745 SYMBOL_PRINT_NAME (sym),
21746 &objfile->objfile_obstack, cu,
21747 &value, &bytes, &baton);
21748
21749 if (baton != NULL)
21750 {
21751 SYMBOL_LOCATION_BATON (sym) = baton;
21752 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21753 }
21754 else if (bytes != NULL)
21755 {
21756 SYMBOL_VALUE_BYTES (sym) = bytes;
21757 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21758 }
21759 else
21760 {
21761 SYMBOL_VALUE (sym) = value;
21762 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21763 }
21764 }
21765
21766 /* Return the type of the die in question using its DW_AT_type attribute. */
21767
21768 static struct type *
21769 die_type (struct die_info *die, struct dwarf2_cu *cu)
21770 {
21771 struct attribute *type_attr;
21772
21773 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21774 if (!type_attr)
21775 {
21776 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21777 /* A missing DW_AT_type represents a void type. */
21778 return objfile_type (objfile)->builtin_void;
21779 }
21780
21781 return lookup_die_type (die, type_attr, cu);
21782 }
21783
21784 /* True iff CU's producer generates GNAT Ada auxiliary information
21785 that allows to find parallel types through that information instead
21786 of having to do expensive parallel lookups by type name. */
21787
21788 static int
21789 need_gnat_info (struct dwarf2_cu *cu)
21790 {
21791 /* Assume that the Ada compiler was GNAT, which always produces
21792 the auxiliary information. */
21793 return (cu->language == language_ada);
21794 }
21795
21796 /* Return the auxiliary type of the die in question using its
21797 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21798 attribute is not present. */
21799
21800 static struct type *
21801 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21802 {
21803 struct attribute *type_attr;
21804
21805 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21806 if (!type_attr)
21807 return NULL;
21808
21809 return lookup_die_type (die, type_attr, cu);
21810 }
21811
21812 /* If DIE has a descriptive_type attribute, then set the TYPE's
21813 descriptive type accordingly. */
21814
21815 static void
21816 set_descriptive_type (struct type *type, struct die_info *die,
21817 struct dwarf2_cu *cu)
21818 {
21819 struct type *descriptive_type = die_descriptive_type (die, cu);
21820
21821 if (descriptive_type)
21822 {
21823 ALLOCATE_GNAT_AUX_TYPE (type);
21824 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21825 }
21826 }
21827
21828 /* Return the containing type of the die in question using its
21829 DW_AT_containing_type attribute. */
21830
21831 static struct type *
21832 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21833 {
21834 struct attribute *type_attr;
21835 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21836
21837 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21838 if (!type_attr)
21839 error (_("Dwarf Error: Problem turning containing type into gdb type "
21840 "[in module %s]"), objfile_name (objfile));
21841
21842 return lookup_die_type (die, type_attr, cu);
21843 }
21844
21845 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21846
21847 static struct type *
21848 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21849 {
21850 struct dwarf2_per_objfile *dwarf2_per_objfile
21851 = cu->per_cu->dwarf2_per_objfile;
21852 struct objfile *objfile = dwarf2_per_objfile->objfile;
21853 char *message, *saved;
21854
21855 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
21856 objfile_name (objfile),
21857 sect_offset_str (cu->header.sect_off),
21858 sect_offset_str (die->sect_off));
21859 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21860 message, strlen (message));
21861 xfree (message);
21862
21863 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21864 }
21865
21866 /* Look up the type of DIE in CU using its type attribute ATTR.
21867 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21868 DW_AT_containing_type.
21869 If there is no type substitute an error marker. */
21870
21871 static struct type *
21872 lookup_die_type (struct die_info *die, const struct attribute *attr,
21873 struct dwarf2_cu *cu)
21874 {
21875 struct dwarf2_per_objfile *dwarf2_per_objfile
21876 = cu->per_cu->dwarf2_per_objfile;
21877 struct objfile *objfile = dwarf2_per_objfile->objfile;
21878 struct type *this_type;
21879
21880 gdb_assert (attr->name == DW_AT_type
21881 || attr->name == DW_AT_GNAT_descriptive_type
21882 || attr->name == DW_AT_containing_type);
21883
21884 /* First see if we have it cached. */
21885
21886 if (attr->form == DW_FORM_GNU_ref_alt)
21887 {
21888 struct dwarf2_per_cu_data *per_cu;
21889 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21890
21891 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21892 dwarf2_per_objfile);
21893 this_type = get_die_type_at_offset (sect_off, per_cu);
21894 }
21895 else if (attr_form_is_ref (attr))
21896 {
21897 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21898
21899 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21900 }
21901 else if (attr->form == DW_FORM_ref_sig8)
21902 {
21903 ULONGEST signature = DW_SIGNATURE (attr);
21904
21905 return get_signatured_type (die, signature, cu);
21906 }
21907 else
21908 {
21909 complaint (&symfile_complaints,
21910 _("Dwarf Error: Bad type attribute %s in DIE"
21911 " at %s [in module %s]"),
21912 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21913 objfile_name (objfile));
21914 return build_error_marker_type (cu, die);
21915 }
21916
21917 /* If not cached we need to read it in. */
21918
21919 if (this_type == NULL)
21920 {
21921 struct die_info *type_die = NULL;
21922 struct dwarf2_cu *type_cu = cu;
21923
21924 if (attr_form_is_ref (attr))
21925 type_die = follow_die_ref (die, attr, &type_cu);
21926 if (type_die == NULL)
21927 return build_error_marker_type (cu, die);
21928 /* If we find the type now, it's probably because the type came
21929 from an inter-CU reference and the type's CU got expanded before
21930 ours. */
21931 this_type = read_type_die (type_die, type_cu);
21932 }
21933
21934 /* If we still don't have a type use an error marker. */
21935
21936 if (this_type == NULL)
21937 return build_error_marker_type (cu, die);
21938
21939 return this_type;
21940 }
21941
21942 /* Return the type in DIE, CU.
21943 Returns NULL for invalid types.
21944
21945 This first does a lookup in die_type_hash,
21946 and only reads the die in if necessary.
21947
21948 NOTE: This can be called when reading in partial or full symbols. */
21949
21950 static struct type *
21951 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21952 {
21953 struct type *this_type;
21954
21955 this_type = get_die_type (die, cu);
21956 if (this_type)
21957 return this_type;
21958
21959 return read_type_die_1 (die, cu);
21960 }
21961
21962 /* Read the type in DIE, CU.
21963 Returns NULL for invalid types. */
21964
21965 static struct type *
21966 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21967 {
21968 struct type *this_type = NULL;
21969
21970 switch (die->tag)
21971 {
21972 case DW_TAG_class_type:
21973 case DW_TAG_interface_type:
21974 case DW_TAG_structure_type:
21975 case DW_TAG_union_type:
21976 this_type = read_structure_type (die, cu);
21977 break;
21978 case DW_TAG_enumeration_type:
21979 this_type = read_enumeration_type (die, cu);
21980 break;
21981 case DW_TAG_subprogram:
21982 case DW_TAG_subroutine_type:
21983 case DW_TAG_inlined_subroutine:
21984 this_type = read_subroutine_type (die, cu);
21985 break;
21986 case DW_TAG_array_type:
21987 this_type = read_array_type (die, cu);
21988 break;
21989 case DW_TAG_set_type:
21990 this_type = read_set_type (die, cu);
21991 break;
21992 case DW_TAG_pointer_type:
21993 this_type = read_tag_pointer_type (die, cu);
21994 break;
21995 case DW_TAG_ptr_to_member_type:
21996 this_type = read_tag_ptr_to_member_type (die, cu);
21997 break;
21998 case DW_TAG_reference_type:
21999 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22000 break;
22001 case DW_TAG_rvalue_reference_type:
22002 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22003 break;
22004 case DW_TAG_const_type:
22005 this_type = read_tag_const_type (die, cu);
22006 break;
22007 case DW_TAG_volatile_type:
22008 this_type = read_tag_volatile_type (die, cu);
22009 break;
22010 case DW_TAG_restrict_type:
22011 this_type = read_tag_restrict_type (die, cu);
22012 break;
22013 case DW_TAG_string_type:
22014 this_type = read_tag_string_type (die, cu);
22015 break;
22016 case DW_TAG_typedef:
22017 this_type = read_typedef (die, cu);
22018 break;
22019 case DW_TAG_subrange_type:
22020 this_type = read_subrange_type (die, cu);
22021 break;
22022 case DW_TAG_base_type:
22023 this_type = read_base_type (die, cu);
22024 break;
22025 case DW_TAG_unspecified_type:
22026 this_type = read_unspecified_type (die, cu);
22027 break;
22028 case DW_TAG_namespace:
22029 this_type = read_namespace_type (die, cu);
22030 break;
22031 case DW_TAG_module:
22032 this_type = read_module_type (die, cu);
22033 break;
22034 case DW_TAG_atomic_type:
22035 this_type = read_tag_atomic_type (die, cu);
22036 break;
22037 default:
22038 complaint (&symfile_complaints,
22039 _("unexpected tag in read_type_die: '%s'"),
22040 dwarf_tag_name (die->tag));
22041 break;
22042 }
22043
22044 return this_type;
22045 }
22046
22047 /* See if we can figure out if the class lives in a namespace. We do
22048 this by looking for a member function; its demangled name will
22049 contain namespace info, if there is any.
22050 Return the computed name or NULL.
22051 Space for the result is allocated on the objfile's obstack.
22052 This is the full-die version of guess_partial_die_structure_name.
22053 In this case we know DIE has no useful parent. */
22054
22055 static char *
22056 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22057 {
22058 struct die_info *spec_die;
22059 struct dwarf2_cu *spec_cu;
22060 struct die_info *child;
22061 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22062
22063 spec_cu = cu;
22064 spec_die = die_specification (die, &spec_cu);
22065 if (spec_die != NULL)
22066 {
22067 die = spec_die;
22068 cu = spec_cu;
22069 }
22070
22071 for (child = die->child;
22072 child != NULL;
22073 child = child->sibling)
22074 {
22075 if (child->tag == DW_TAG_subprogram)
22076 {
22077 const char *linkage_name = dw2_linkage_name (child, cu);
22078
22079 if (linkage_name != NULL)
22080 {
22081 char *actual_name
22082 = language_class_name_from_physname (cu->language_defn,
22083 linkage_name);
22084 char *name = NULL;
22085
22086 if (actual_name != NULL)
22087 {
22088 const char *die_name = dwarf2_name (die, cu);
22089
22090 if (die_name != NULL
22091 && strcmp (die_name, actual_name) != 0)
22092 {
22093 /* Strip off the class name from the full name.
22094 We want the prefix. */
22095 int die_name_len = strlen (die_name);
22096 int actual_name_len = strlen (actual_name);
22097
22098 /* Test for '::' as a sanity check. */
22099 if (actual_name_len > die_name_len + 2
22100 && actual_name[actual_name_len
22101 - die_name_len - 1] == ':')
22102 name = (char *) obstack_copy0 (
22103 &objfile->per_bfd->storage_obstack,
22104 actual_name, actual_name_len - die_name_len - 2);
22105 }
22106 }
22107 xfree (actual_name);
22108 return name;
22109 }
22110 }
22111 }
22112
22113 return NULL;
22114 }
22115
22116 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22117 prefix part in such case. See
22118 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22119
22120 static const char *
22121 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22122 {
22123 struct attribute *attr;
22124 const char *base;
22125
22126 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22127 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22128 return NULL;
22129
22130 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22131 return NULL;
22132
22133 attr = dw2_linkage_name_attr (die, cu);
22134 if (attr == NULL || DW_STRING (attr) == NULL)
22135 return NULL;
22136
22137 /* dwarf2_name had to be already called. */
22138 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22139
22140 /* Strip the base name, keep any leading namespaces/classes. */
22141 base = strrchr (DW_STRING (attr), ':');
22142 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22143 return "";
22144
22145 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22146 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22147 DW_STRING (attr),
22148 &base[-1] - DW_STRING (attr));
22149 }
22150
22151 /* Return the name of the namespace/class that DIE is defined within,
22152 or "" if we can't tell. The caller should not xfree the result.
22153
22154 For example, if we're within the method foo() in the following
22155 code:
22156
22157 namespace N {
22158 class C {
22159 void foo () {
22160 }
22161 };
22162 }
22163
22164 then determine_prefix on foo's die will return "N::C". */
22165
22166 static const char *
22167 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22168 {
22169 struct dwarf2_per_objfile *dwarf2_per_objfile
22170 = cu->per_cu->dwarf2_per_objfile;
22171 struct die_info *parent, *spec_die;
22172 struct dwarf2_cu *spec_cu;
22173 struct type *parent_type;
22174 const char *retval;
22175
22176 if (cu->language != language_cplus
22177 && cu->language != language_fortran && cu->language != language_d
22178 && cu->language != language_rust)
22179 return "";
22180
22181 retval = anonymous_struct_prefix (die, cu);
22182 if (retval)
22183 return retval;
22184
22185 /* We have to be careful in the presence of DW_AT_specification.
22186 For example, with GCC 3.4, given the code
22187
22188 namespace N {
22189 void foo() {
22190 // Definition of N::foo.
22191 }
22192 }
22193
22194 then we'll have a tree of DIEs like this:
22195
22196 1: DW_TAG_compile_unit
22197 2: DW_TAG_namespace // N
22198 3: DW_TAG_subprogram // declaration of N::foo
22199 4: DW_TAG_subprogram // definition of N::foo
22200 DW_AT_specification // refers to die #3
22201
22202 Thus, when processing die #4, we have to pretend that we're in
22203 the context of its DW_AT_specification, namely the contex of die
22204 #3. */
22205 spec_cu = cu;
22206 spec_die = die_specification (die, &spec_cu);
22207 if (spec_die == NULL)
22208 parent = die->parent;
22209 else
22210 {
22211 parent = spec_die->parent;
22212 cu = spec_cu;
22213 }
22214
22215 if (parent == NULL)
22216 return "";
22217 else if (parent->building_fullname)
22218 {
22219 const char *name;
22220 const char *parent_name;
22221
22222 /* It has been seen on RealView 2.2 built binaries,
22223 DW_TAG_template_type_param types actually _defined_ as
22224 children of the parent class:
22225
22226 enum E {};
22227 template class <class Enum> Class{};
22228 Class<enum E> class_e;
22229
22230 1: DW_TAG_class_type (Class)
22231 2: DW_TAG_enumeration_type (E)
22232 3: DW_TAG_enumerator (enum1:0)
22233 3: DW_TAG_enumerator (enum2:1)
22234 ...
22235 2: DW_TAG_template_type_param
22236 DW_AT_type DW_FORM_ref_udata (E)
22237
22238 Besides being broken debug info, it can put GDB into an
22239 infinite loop. Consider:
22240
22241 When we're building the full name for Class<E>, we'll start
22242 at Class, and go look over its template type parameters,
22243 finding E. We'll then try to build the full name of E, and
22244 reach here. We're now trying to build the full name of E,
22245 and look over the parent DIE for containing scope. In the
22246 broken case, if we followed the parent DIE of E, we'd again
22247 find Class, and once again go look at its template type
22248 arguments, etc., etc. Simply don't consider such parent die
22249 as source-level parent of this die (it can't be, the language
22250 doesn't allow it), and break the loop here. */
22251 name = dwarf2_name (die, cu);
22252 parent_name = dwarf2_name (parent, cu);
22253 complaint (&symfile_complaints,
22254 _("template param type '%s' defined within parent '%s'"),
22255 name ? name : "<unknown>",
22256 parent_name ? parent_name : "<unknown>");
22257 return "";
22258 }
22259 else
22260 switch (parent->tag)
22261 {
22262 case DW_TAG_namespace:
22263 parent_type = read_type_die (parent, cu);
22264 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22265 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22266 Work around this problem here. */
22267 if (cu->language == language_cplus
22268 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22269 return "";
22270 /* We give a name to even anonymous namespaces. */
22271 return TYPE_TAG_NAME (parent_type);
22272 case DW_TAG_class_type:
22273 case DW_TAG_interface_type:
22274 case DW_TAG_structure_type:
22275 case DW_TAG_union_type:
22276 case DW_TAG_module:
22277 parent_type = read_type_die (parent, cu);
22278 if (TYPE_TAG_NAME (parent_type) != NULL)
22279 return TYPE_TAG_NAME (parent_type);
22280 else
22281 /* An anonymous structure is only allowed non-static data
22282 members; no typedefs, no member functions, et cetera.
22283 So it does not need a prefix. */
22284 return "";
22285 case DW_TAG_compile_unit:
22286 case DW_TAG_partial_unit:
22287 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22288 if (cu->language == language_cplus
22289 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22290 && die->child != NULL
22291 && (die->tag == DW_TAG_class_type
22292 || die->tag == DW_TAG_structure_type
22293 || die->tag == DW_TAG_union_type))
22294 {
22295 char *name = guess_full_die_structure_name (die, cu);
22296 if (name != NULL)
22297 return name;
22298 }
22299 return "";
22300 case DW_TAG_enumeration_type:
22301 parent_type = read_type_die (parent, cu);
22302 if (TYPE_DECLARED_CLASS (parent_type))
22303 {
22304 if (TYPE_TAG_NAME (parent_type) != NULL)
22305 return TYPE_TAG_NAME (parent_type);
22306 return "";
22307 }
22308 /* Fall through. */
22309 default:
22310 return determine_prefix (parent, cu);
22311 }
22312 }
22313
22314 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22315 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22316 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22317 an obconcat, otherwise allocate storage for the result. The CU argument is
22318 used to determine the language and hence, the appropriate separator. */
22319
22320 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22321
22322 static char *
22323 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22324 int physname, struct dwarf2_cu *cu)
22325 {
22326 const char *lead = "";
22327 const char *sep;
22328
22329 if (suffix == NULL || suffix[0] == '\0'
22330 || prefix == NULL || prefix[0] == '\0')
22331 sep = "";
22332 else if (cu->language == language_d)
22333 {
22334 /* For D, the 'main' function could be defined in any module, but it
22335 should never be prefixed. */
22336 if (strcmp (suffix, "D main") == 0)
22337 {
22338 prefix = "";
22339 sep = "";
22340 }
22341 else
22342 sep = ".";
22343 }
22344 else if (cu->language == language_fortran && physname)
22345 {
22346 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22347 DW_AT_MIPS_linkage_name is preferred and used instead. */
22348
22349 lead = "__";
22350 sep = "_MOD_";
22351 }
22352 else
22353 sep = "::";
22354
22355 if (prefix == NULL)
22356 prefix = "";
22357 if (suffix == NULL)
22358 suffix = "";
22359
22360 if (obs == NULL)
22361 {
22362 char *retval
22363 = ((char *)
22364 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22365
22366 strcpy (retval, lead);
22367 strcat (retval, prefix);
22368 strcat (retval, sep);
22369 strcat (retval, suffix);
22370 return retval;
22371 }
22372 else
22373 {
22374 /* We have an obstack. */
22375 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22376 }
22377 }
22378
22379 /* Return sibling of die, NULL if no sibling. */
22380
22381 static struct die_info *
22382 sibling_die (struct die_info *die)
22383 {
22384 return die->sibling;
22385 }
22386
22387 /* Get name of a die, return NULL if not found. */
22388
22389 static const char *
22390 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22391 struct obstack *obstack)
22392 {
22393 if (name && cu->language == language_cplus)
22394 {
22395 std::string canon_name = cp_canonicalize_string (name);
22396
22397 if (!canon_name.empty ())
22398 {
22399 if (canon_name != name)
22400 name = (const char *) obstack_copy0 (obstack,
22401 canon_name.c_str (),
22402 canon_name.length ());
22403 }
22404 }
22405
22406 return name;
22407 }
22408
22409 /* Get name of a die, return NULL if not found.
22410 Anonymous namespaces are converted to their magic string. */
22411
22412 static const char *
22413 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22414 {
22415 struct attribute *attr;
22416 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22417
22418 attr = dwarf2_attr (die, DW_AT_name, cu);
22419 if ((!attr || !DW_STRING (attr))
22420 && die->tag != DW_TAG_namespace
22421 && die->tag != DW_TAG_class_type
22422 && die->tag != DW_TAG_interface_type
22423 && die->tag != DW_TAG_structure_type
22424 && die->tag != DW_TAG_union_type)
22425 return NULL;
22426
22427 switch (die->tag)
22428 {
22429 case DW_TAG_compile_unit:
22430 case DW_TAG_partial_unit:
22431 /* Compilation units have a DW_AT_name that is a filename, not
22432 a source language identifier. */
22433 case DW_TAG_enumeration_type:
22434 case DW_TAG_enumerator:
22435 /* These tags always have simple identifiers already; no need
22436 to canonicalize them. */
22437 return DW_STRING (attr);
22438
22439 case DW_TAG_namespace:
22440 if (attr != NULL && DW_STRING (attr) != NULL)
22441 return DW_STRING (attr);
22442 return CP_ANONYMOUS_NAMESPACE_STR;
22443
22444 case DW_TAG_class_type:
22445 case DW_TAG_interface_type:
22446 case DW_TAG_structure_type:
22447 case DW_TAG_union_type:
22448 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22449 structures or unions. These were of the form "._%d" in GCC 4.1,
22450 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22451 and GCC 4.4. We work around this problem by ignoring these. */
22452 if (attr && DW_STRING (attr)
22453 && (startswith (DW_STRING (attr), "._")
22454 || startswith (DW_STRING (attr), "<anonymous")))
22455 return NULL;
22456
22457 /* GCC might emit a nameless typedef that has a linkage name. See
22458 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22459 if (!attr || DW_STRING (attr) == NULL)
22460 {
22461 char *demangled = NULL;
22462
22463 attr = dw2_linkage_name_attr (die, cu);
22464 if (attr == NULL || DW_STRING (attr) == NULL)
22465 return NULL;
22466
22467 /* Avoid demangling DW_STRING (attr) the second time on a second
22468 call for the same DIE. */
22469 if (!DW_STRING_IS_CANONICAL (attr))
22470 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22471
22472 if (demangled)
22473 {
22474 const char *base;
22475
22476 /* FIXME: we already did this for the partial symbol... */
22477 DW_STRING (attr)
22478 = ((const char *)
22479 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22480 demangled, strlen (demangled)));
22481 DW_STRING_IS_CANONICAL (attr) = 1;
22482 xfree (demangled);
22483
22484 /* Strip any leading namespaces/classes, keep only the base name.
22485 DW_AT_name for named DIEs does not contain the prefixes. */
22486 base = strrchr (DW_STRING (attr), ':');
22487 if (base && base > DW_STRING (attr) && base[-1] == ':')
22488 return &base[1];
22489 else
22490 return DW_STRING (attr);
22491 }
22492 }
22493 break;
22494
22495 default:
22496 break;
22497 }
22498
22499 if (!DW_STRING_IS_CANONICAL (attr))
22500 {
22501 DW_STRING (attr)
22502 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22503 &objfile->per_bfd->storage_obstack);
22504 DW_STRING_IS_CANONICAL (attr) = 1;
22505 }
22506 return DW_STRING (attr);
22507 }
22508
22509 /* Return the die that this die in an extension of, or NULL if there
22510 is none. *EXT_CU is the CU containing DIE on input, and the CU
22511 containing the return value on output. */
22512
22513 static struct die_info *
22514 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22515 {
22516 struct attribute *attr;
22517
22518 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22519 if (attr == NULL)
22520 return NULL;
22521
22522 return follow_die_ref (die, attr, ext_cu);
22523 }
22524
22525 /* Convert a DIE tag into its string name. */
22526
22527 static const char *
22528 dwarf_tag_name (unsigned tag)
22529 {
22530 const char *name = get_DW_TAG_name (tag);
22531
22532 if (name == NULL)
22533 return "DW_TAG_<unknown>";
22534
22535 return name;
22536 }
22537
22538 /* Convert a DWARF attribute code into its string name. */
22539
22540 static const char *
22541 dwarf_attr_name (unsigned attr)
22542 {
22543 const char *name;
22544
22545 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22546 if (attr == DW_AT_MIPS_fde)
22547 return "DW_AT_MIPS_fde";
22548 #else
22549 if (attr == DW_AT_HP_block_index)
22550 return "DW_AT_HP_block_index";
22551 #endif
22552
22553 name = get_DW_AT_name (attr);
22554
22555 if (name == NULL)
22556 return "DW_AT_<unknown>";
22557
22558 return name;
22559 }
22560
22561 /* Convert a DWARF value form code into its string name. */
22562
22563 static const char *
22564 dwarf_form_name (unsigned form)
22565 {
22566 const char *name = get_DW_FORM_name (form);
22567
22568 if (name == NULL)
22569 return "DW_FORM_<unknown>";
22570
22571 return name;
22572 }
22573
22574 static const char *
22575 dwarf_bool_name (unsigned mybool)
22576 {
22577 if (mybool)
22578 return "TRUE";
22579 else
22580 return "FALSE";
22581 }
22582
22583 /* Convert a DWARF type code into its string name. */
22584
22585 static const char *
22586 dwarf_type_encoding_name (unsigned enc)
22587 {
22588 const char *name = get_DW_ATE_name (enc);
22589
22590 if (name == NULL)
22591 return "DW_ATE_<unknown>";
22592
22593 return name;
22594 }
22595
22596 static void
22597 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22598 {
22599 unsigned int i;
22600
22601 print_spaces (indent, f);
22602 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22603 dwarf_tag_name (die->tag), die->abbrev,
22604 sect_offset_str (die->sect_off));
22605
22606 if (die->parent != NULL)
22607 {
22608 print_spaces (indent, f);
22609 fprintf_unfiltered (f, " parent at offset: %s\n",
22610 sect_offset_str (die->parent->sect_off));
22611 }
22612
22613 print_spaces (indent, f);
22614 fprintf_unfiltered (f, " has children: %s\n",
22615 dwarf_bool_name (die->child != NULL));
22616
22617 print_spaces (indent, f);
22618 fprintf_unfiltered (f, " attributes:\n");
22619
22620 for (i = 0; i < die->num_attrs; ++i)
22621 {
22622 print_spaces (indent, f);
22623 fprintf_unfiltered (f, " %s (%s) ",
22624 dwarf_attr_name (die->attrs[i].name),
22625 dwarf_form_name (die->attrs[i].form));
22626
22627 switch (die->attrs[i].form)
22628 {
22629 case DW_FORM_addr:
22630 case DW_FORM_GNU_addr_index:
22631 fprintf_unfiltered (f, "address: ");
22632 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22633 break;
22634 case DW_FORM_block2:
22635 case DW_FORM_block4:
22636 case DW_FORM_block:
22637 case DW_FORM_block1:
22638 fprintf_unfiltered (f, "block: size %s",
22639 pulongest (DW_BLOCK (&die->attrs[i])->size));
22640 break;
22641 case DW_FORM_exprloc:
22642 fprintf_unfiltered (f, "expression: size %s",
22643 pulongest (DW_BLOCK (&die->attrs[i])->size));
22644 break;
22645 case DW_FORM_data16:
22646 fprintf_unfiltered (f, "constant of 16 bytes");
22647 break;
22648 case DW_FORM_ref_addr:
22649 fprintf_unfiltered (f, "ref address: ");
22650 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22651 break;
22652 case DW_FORM_GNU_ref_alt:
22653 fprintf_unfiltered (f, "alt ref address: ");
22654 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22655 break;
22656 case DW_FORM_ref1:
22657 case DW_FORM_ref2:
22658 case DW_FORM_ref4:
22659 case DW_FORM_ref8:
22660 case DW_FORM_ref_udata:
22661 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22662 (long) (DW_UNSND (&die->attrs[i])));
22663 break;
22664 case DW_FORM_data1:
22665 case DW_FORM_data2:
22666 case DW_FORM_data4:
22667 case DW_FORM_data8:
22668 case DW_FORM_udata:
22669 case DW_FORM_sdata:
22670 fprintf_unfiltered (f, "constant: %s",
22671 pulongest (DW_UNSND (&die->attrs[i])));
22672 break;
22673 case DW_FORM_sec_offset:
22674 fprintf_unfiltered (f, "section offset: %s",
22675 pulongest (DW_UNSND (&die->attrs[i])));
22676 break;
22677 case DW_FORM_ref_sig8:
22678 fprintf_unfiltered (f, "signature: %s",
22679 hex_string (DW_SIGNATURE (&die->attrs[i])));
22680 break;
22681 case DW_FORM_string:
22682 case DW_FORM_strp:
22683 case DW_FORM_line_strp:
22684 case DW_FORM_GNU_str_index:
22685 case DW_FORM_GNU_strp_alt:
22686 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22687 DW_STRING (&die->attrs[i])
22688 ? DW_STRING (&die->attrs[i]) : "",
22689 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22690 break;
22691 case DW_FORM_flag:
22692 if (DW_UNSND (&die->attrs[i]))
22693 fprintf_unfiltered (f, "flag: TRUE");
22694 else
22695 fprintf_unfiltered (f, "flag: FALSE");
22696 break;
22697 case DW_FORM_flag_present:
22698 fprintf_unfiltered (f, "flag: TRUE");
22699 break;
22700 case DW_FORM_indirect:
22701 /* The reader will have reduced the indirect form to
22702 the "base form" so this form should not occur. */
22703 fprintf_unfiltered (f,
22704 "unexpected attribute form: DW_FORM_indirect");
22705 break;
22706 case DW_FORM_implicit_const:
22707 fprintf_unfiltered (f, "constant: %s",
22708 plongest (DW_SND (&die->attrs[i])));
22709 break;
22710 default:
22711 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22712 die->attrs[i].form);
22713 break;
22714 }
22715 fprintf_unfiltered (f, "\n");
22716 }
22717 }
22718
22719 static void
22720 dump_die_for_error (struct die_info *die)
22721 {
22722 dump_die_shallow (gdb_stderr, 0, die);
22723 }
22724
22725 static void
22726 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22727 {
22728 int indent = level * 4;
22729
22730 gdb_assert (die != NULL);
22731
22732 if (level >= max_level)
22733 return;
22734
22735 dump_die_shallow (f, indent, die);
22736
22737 if (die->child != NULL)
22738 {
22739 print_spaces (indent, f);
22740 fprintf_unfiltered (f, " Children:");
22741 if (level + 1 < max_level)
22742 {
22743 fprintf_unfiltered (f, "\n");
22744 dump_die_1 (f, level + 1, max_level, die->child);
22745 }
22746 else
22747 {
22748 fprintf_unfiltered (f,
22749 " [not printed, max nesting level reached]\n");
22750 }
22751 }
22752
22753 if (die->sibling != NULL && level > 0)
22754 {
22755 dump_die_1 (f, level, max_level, die->sibling);
22756 }
22757 }
22758
22759 /* This is called from the pdie macro in gdbinit.in.
22760 It's not static so gcc will keep a copy callable from gdb. */
22761
22762 void
22763 dump_die (struct die_info *die, int max_level)
22764 {
22765 dump_die_1 (gdb_stdlog, 0, max_level, die);
22766 }
22767
22768 static void
22769 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22770 {
22771 void **slot;
22772
22773 slot = htab_find_slot_with_hash (cu->die_hash, die,
22774 to_underlying (die->sect_off),
22775 INSERT);
22776
22777 *slot = die;
22778 }
22779
22780 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22781 required kind. */
22782
22783 static sect_offset
22784 dwarf2_get_ref_die_offset (const struct attribute *attr)
22785 {
22786 if (attr_form_is_ref (attr))
22787 return (sect_offset) DW_UNSND (attr);
22788
22789 complaint (&symfile_complaints,
22790 _("unsupported die ref attribute form: '%s'"),
22791 dwarf_form_name (attr->form));
22792 return {};
22793 }
22794
22795 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22796 * the value held by the attribute is not constant. */
22797
22798 static LONGEST
22799 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22800 {
22801 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22802 return DW_SND (attr);
22803 else if (attr->form == DW_FORM_udata
22804 || attr->form == DW_FORM_data1
22805 || attr->form == DW_FORM_data2
22806 || attr->form == DW_FORM_data4
22807 || attr->form == DW_FORM_data8)
22808 return DW_UNSND (attr);
22809 else
22810 {
22811 /* For DW_FORM_data16 see attr_form_is_constant. */
22812 complaint (&symfile_complaints,
22813 _("Attribute value is not a constant (%s)"),
22814 dwarf_form_name (attr->form));
22815 return default_value;
22816 }
22817 }
22818
22819 /* Follow reference or signature attribute ATTR of SRC_DIE.
22820 On entry *REF_CU is the CU of SRC_DIE.
22821 On exit *REF_CU is the CU of the result. */
22822
22823 static struct die_info *
22824 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22825 struct dwarf2_cu **ref_cu)
22826 {
22827 struct die_info *die;
22828
22829 if (attr_form_is_ref (attr))
22830 die = follow_die_ref (src_die, attr, ref_cu);
22831 else if (attr->form == DW_FORM_ref_sig8)
22832 die = follow_die_sig (src_die, attr, ref_cu);
22833 else
22834 {
22835 dump_die_for_error (src_die);
22836 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22837 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22838 }
22839
22840 return die;
22841 }
22842
22843 /* Follow reference OFFSET.
22844 On entry *REF_CU is the CU of the source die referencing OFFSET.
22845 On exit *REF_CU is the CU of the result.
22846 Returns NULL if OFFSET is invalid. */
22847
22848 static struct die_info *
22849 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22850 struct dwarf2_cu **ref_cu)
22851 {
22852 struct die_info temp_die;
22853 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22854 struct dwarf2_per_objfile *dwarf2_per_objfile
22855 = cu->per_cu->dwarf2_per_objfile;
22856
22857 gdb_assert (cu->per_cu != NULL);
22858
22859 target_cu = cu;
22860
22861 if (cu->per_cu->is_debug_types)
22862 {
22863 /* .debug_types CUs cannot reference anything outside their CU.
22864 If they need to, they have to reference a signatured type via
22865 DW_FORM_ref_sig8. */
22866 if (!offset_in_cu_p (&cu->header, sect_off))
22867 return NULL;
22868 }
22869 else if (offset_in_dwz != cu->per_cu->is_dwz
22870 || !offset_in_cu_p (&cu->header, sect_off))
22871 {
22872 struct dwarf2_per_cu_data *per_cu;
22873
22874 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22875 dwarf2_per_objfile);
22876
22877 /* If necessary, add it to the queue and load its DIEs. */
22878 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22879 load_full_comp_unit (per_cu, cu->language);
22880
22881 target_cu = per_cu->cu;
22882 }
22883 else if (cu->dies == NULL)
22884 {
22885 /* We're loading full DIEs during partial symbol reading. */
22886 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22887 load_full_comp_unit (cu->per_cu, language_minimal);
22888 }
22889
22890 *ref_cu = target_cu;
22891 temp_die.sect_off = sect_off;
22892 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22893 &temp_die,
22894 to_underlying (sect_off));
22895 }
22896
22897 /* Follow reference attribute ATTR of SRC_DIE.
22898 On entry *REF_CU is the CU of SRC_DIE.
22899 On exit *REF_CU is the CU of the result. */
22900
22901 static struct die_info *
22902 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22903 struct dwarf2_cu **ref_cu)
22904 {
22905 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22906 struct dwarf2_cu *cu = *ref_cu;
22907 struct die_info *die;
22908
22909 die = follow_die_offset (sect_off,
22910 (attr->form == DW_FORM_GNU_ref_alt
22911 || cu->per_cu->is_dwz),
22912 ref_cu);
22913 if (!die)
22914 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22915 "at %s [in module %s]"),
22916 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22917 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22918
22919 return die;
22920 }
22921
22922 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22923 Returned value is intended for DW_OP_call*. Returned
22924 dwarf2_locexpr_baton->data has lifetime of
22925 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22926
22927 struct dwarf2_locexpr_baton
22928 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22929 struct dwarf2_per_cu_data *per_cu,
22930 CORE_ADDR (*get_frame_pc) (void *baton),
22931 void *baton)
22932 {
22933 struct dwarf2_cu *cu;
22934 struct die_info *die;
22935 struct attribute *attr;
22936 struct dwarf2_locexpr_baton retval;
22937 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22938 struct dwarf2_per_objfile *dwarf2_per_objfile
22939 = get_dwarf2_per_objfile (objfile);
22940
22941 if (per_cu->cu == NULL)
22942 load_cu (per_cu);
22943 cu = per_cu->cu;
22944 if (cu == NULL)
22945 {
22946 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22947 Instead just throw an error, not much else we can do. */
22948 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22949 sect_offset_str (sect_off), objfile_name (objfile));
22950 }
22951
22952 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22953 if (!die)
22954 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22955 sect_offset_str (sect_off), objfile_name (objfile));
22956
22957 attr = dwarf2_attr (die, DW_AT_location, cu);
22958 if (!attr)
22959 {
22960 /* DWARF: "If there is no such attribute, then there is no effect.".
22961 DATA is ignored if SIZE is 0. */
22962
22963 retval.data = NULL;
22964 retval.size = 0;
22965 }
22966 else if (attr_form_is_section_offset (attr))
22967 {
22968 struct dwarf2_loclist_baton loclist_baton;
22969 CORE_ADDR pc = (*get_frame_pc) (baton);
22970 size_t size;
22971
22972 fill_in_loclist_baton (cu, &loclist_baton, attr);
22973
22974 retval.data = dwarf2_find_location_expression (&loclist_baton,
22975 &size, pc);
22976 retval.size = size;
22977 }
22978 else
22979 {
22980 if (!attr_form_is_block (attr))
22981 error (_("Dwarf Error: DIE at %s referenced in module %s "
22982 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22983 sect_offset_str (sect_off), objfile_name (objfile));
22984
22985 retval.data = DW_BLOCK (attr)->data;
22986 retval.size = DW_BLOCK (attr)->size;
22987 }
22988 retval.per_cu = cu->per_cu;
22989
22990 age_cached_comp_units (dwarf2_per_objfile);
22991
22992 return retval;
22993 }
22994
22995 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22996 offset. */
22997
22998 struct dwarf2_locexpr_baton
22999 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23000 struct dwarf2_per_cu_data *per_cu,
23001 CORE_ADDR (*get_frame_pc) (void *baton),
23002 void *baton)
23003 {
23004 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23005
23006 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23007 }
23008
23009 /* Write a constant of a given type as target-ordered bytes into
23010 OBSTACK. */
23011
23012 static const gdb_byte *
23013 write_constant_as_bytes (struct obstack *obstack,
23014 enum bfd_endian byte_order,
23015 struct type *type,
23016 ULONGEST value,
23017 LONGEST *len)
23018 {
23019 gdb_byte *result;
23020
23021 *len = TYPE_LENGTH (type);
23022 result = (gdb_byte *) obstack_alloc (obstack, *len);
23023 store_unsigned_integer (result, *len, byte_order, value);
23024
23025 return result;
23026 }
23027
23028 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23029 pointer to the constant bytes and set LEN to the length of the
23030 data. If memory is needed, allocate it on OBSTACK. If the DIE
23031 does not have a DW_AT_const_value, return NULL. */
23032
23033 const gdb_byte *
23034 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23035 struct dwarf2_per_cu_data *per_cu,
23036 struct obstack *obstack,
23037 LONGEST *len)
23038 {
23039 struct dwarf2_cu *cu;
23040 struct die_info *die;
23041 struct attribute *attr;
23042 const gdb_byte *result = NULL;
23043 struct type *type;
23044 LONGEST value;
23045 enum bfd_endian byte_order;
23046 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23047
23048 if (per_cu->cu == NULL)
23049 load_cu (per_cu);
23050 cu = per_cu->cu;
23051 if (cu == NULL)
23052 {
23053 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23054 Instead just throw an error, not much else we can do. */
23055 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23056 sect_offset_str (sect_off), objfile_name (objfile));
23057 }
23058
23059 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23060 if (!die)
23061 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23062 sect_offset_str (sect_off), objfile_name (objfile));
23063
23064 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23065 if (attr == NULL)
23066 return NULL;
23067
23068 byte_order = (bfd_big_endian (objfile->obfd)
23069 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23070
23071 switch (attr->form)
23072 {
23073 case DW_FORM_addr:
23074 case DW_FORM_GNU_addr_index:
23075 {
23076 gdb_byte *tem;
23077
23078 *len = cu->header.addr_size;
23079 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23080 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23081 result = tem;
23082 }
23083 break;
23084 case DW_FORM_string:
23085 case DW_FORM_strp:
23086 case DW_FORM_GNU_str_index:
23087 case DW_FORM_GNU_strp_alt:
23088 /* DW_STRING is already allocated on the objfile obstack, point
23089 directly to it. */
23090 result = (const gdb_byte *) DW_STRING (attr);
23091 *len = strlen (DW_STRING (attr));
23092 break;
23093 case DW_FORM_block1:
23094 case DW_FORM_block2:
23095 case DW_FORM_block4:
23096 case DW_FORM_block:
23097 case DW_FORM_exprloc:
23098 case DW_FORM_data16:
23099 result = DW_BLOCK (attr)->data;
23100 *len = DW_BLOCK (attr)->size;
23101 break;
23102
23103 /* The DW_AT_const_value attributes are supposed to carry the
23104 symbol's value "represented as it would be on the target
23105 architecture." By the time we get here, it's already been
23106 converted to host endianness, so we just need to sign- or
23107 zero-extend it as appropriate. */
23108 case DW_FORM_data1:
23109 type = die_type (die, cu);
23110 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23111 if (result == NULL)
23112 result = write_constant_as_bytes (obstack, byte_order,
23113 type, value, len);
23114 break;
23115 case DW_FORM_data2:
23116 type = die_type (die, cu);
23117 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23118 if (result == NULL)
23119 result = write_constant_as_bytes (obstack, byte_order,
23120 type, value, len);
23121 break;
23122 case DW_FORM_data4:
23123 type = die_type (die, cu);
23124 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23125 if (result == NULL)
23126 result = write_constant_as_bytes (obstack, byte_order,
23127 type, value, len);
23128 break;
23129 case DW_FORM_data8:
23130 type = die_type (die, cu);
23131 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23132 if (result == NULL)
23133 result = write_constant_as_bytes (obstack, byte_order,
23134 type, value, len);
23135 break;
23136
23137 case DW_FORM_sdata:
23138 case DW_FORM_implicit_const:
23139 type = die_type (die, cu);
23140 result = write_constant_as_bytes (obstack, byte_order,
23141 type, DW_SND (attr), len);
23142 break;
23143
23144 case DW_FORM_udata:
23145 type = die_type (die, cu);
23146 result = write_constant_as_bytes (obstack, byte_order,
23147 type, DW_UNSND (attr), len);
23148 break;
23149
23150 default:
23151 complaint (&symfile_complaints,
23152 _("unsupported const value attribute form: '%s'"),
23153 dwarf_form_name (attr->form));
23154 break;
23155 }
23156
23157 return result;
23158 }
23159
23160 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23161 valid type for this die is found. */
23162
23163 struct type *
23164 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23165 struct dwarf2_per_cu_data *per_cu)
23166 {
23167 struct dwarf2_cu *cu;
23168 struct die_info *die;
23169
23170 if (per_cu->cu == NULL)
23171 load_cu (per_cu);
23172 cu = per_cu->cu;
23173 if (!cu)
23174 return NULL;
23175
23176 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23177 if (!die)
23178 return NULL;
23179
23180 return die_type (die, cu);
23181 }
23182
23183 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23184 PER_CU. */
23185
23186 struct type *
23187 dwarf2_get_die_type (cu_offset die_offset,
23188 struct dwarf2_per_cu_data *per_cu)
23189 {
23190 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23191 return get_die_type_at_offset (die_offset_sect, per_cu);
23192 }
23193
23194 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23195 On entry *REF_CU is the CU of SRC_DIE.
23196 On exit *REF_CU is the CU of the result.
23197 Returns NULL if the referenced DIE isn't found. */
23198
23199 static struct die_info *
23200 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23201 struct dwarf2_cu **ref_cu)
23202 {
23203 struct die_info temp_die;
23204 struct dwarf2_cu *sig_cu;
23205 struct die_info *die;
23206
23207 /* While it might be nice to assert sig_type->type == NULL here,
23208 we can get here for DW_AT_imported_declaration where we need
23209 the DIE not the type. */
23210
23211 /* If necessary, add it to the queue and load its DIEs. */
23212
23213 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23214 read_signatured_type (sig_type);
23215
23216 sig_cu = sig_type->per_cu.cu;
23217 gdb_assert (sig_cu != NULL);
23218 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23219 temp_die.sect_off = sig_type->type_offset_in_section;
23220 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23221 to_underlying (temp_die.sect_off));
23222 if (die)
23223 {
23224 struct dwarf2_per_objfile *dwarf2_per_objfile
23225 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23226
23227 /* For .gdb_index version 7 keep track of included TUs.
23228 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23229 if (dwarf2_per_objfile->index_table != NULL
23230 && dwarf2_per_objfile->index_table->version <= 7)
23231 {
23232 VEC_safe_push (dwarf2_per_cu_ptr,
23233 (*ref_cu)->per_cu->imported_symtabs,
23234 sig_cu->per_cu);
23235 }
23236
23237 *ref_cu = sig_cu;
23238 return die;
23239 }
23240
23241 return NULL;
23242 }
23243
23244 /* Follow signatured type referenced by ATTR in SRC_DIE.
23245 On entry *REF_CU is the CU of SRC_DIE.
23246 On exit *REF_CU is the CU of the result.
23247 The result is the DIE of the type.
23248 If the referenced type cannot be found an error is thrown. */
23249
23250 static struct die_info *
23251 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23252 struct dwarf2_cu **ref_cu)
23253 {
23254 ULONGEST signature = DW_SIGNATURE (attr);
23255 struct signatured_type *sig_type;
23256 struct die_info *die;
23257
23258 gdb_assert (attr->form == DW_FORM_ref_sig8);
23259
23260 sig_type = lookup_signatured_type (*ref_cu, signature);
23261 /* sig_type will be NULL if the signatured type is missing from
23262 the debug info. */
23263 if (sig_type == NULL)
23264 {
23265 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23266 " from DIE at %s [in module %s]"),
23267 hex_string (signature), sect_offset_str (src_die->sect_off),
23268 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23269 }
23270
23271 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23272 if (die == NULL)
23273 {
23274 dump_die_for_error (src_die);
23275 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23276 " from DIE at %s [in module %s]"),
23277 hex_string (signature), sect_offset_str (src_die->sect_off),
23278 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23279 }
23280
23281 return die;
23282 }
23283
23284 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23285 reading in and processing the type unit if necessary. */
23286
23287 static struct type *
23288 get_signatured_type (struct die_info *die, ULONGEST signature,
23289 struct dwarf2_cu *cu)
23290 {
23291 struct dwarf2_per_objfile *dwarf2_per_objfile
23292 = cu->per_cu->dwarf2_per_objfile;
23293 struct signatured_type *sig_type;
23294 struct dwarf2_cu *type_cu;
23295 struct die_info *type_die;
23296 struct type *type;
23297
23298 sig_type = lookup_signatured_type (cu, signature);
23299 /* sig_type will be NULL if the signatured type is missing from
23300 the debug info. */
23301 if (sig_type == NULL)
23302 {
23303 complaint (&symfile_complaints,
23304 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23305 " from DIE at %s [in module %s]"),
23306 hex_string (signature), sect_offset_str (die->sect_off),
23307 objfile_name (dwarf2_per_objfile->objfile));
23308 return build_error_marker_type (cu, die);
23309 }
23310
23311 /* If we already know the type we're done. */
23312 if (sig_type->type != NULL)
23313 return sig_type->type;
23314
23315 type_cu = cu;
23316 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23317 if (type_die != NULL)
23318 {
23319 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23320 is created. This is important, for example, because for c++ classes
23321 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23322 type = read_type_die (type_die, type_cu);
23323 if (type == NULL)
23324 {
23325 complaint (&symfile_complaints,
23326 _("Dwarf Error: Cannot build signatured type %s"
23327 " referenced from DIE at %s [in module %s]"),
23328 hex_string (signature), sect_offset_str (die->sect_off),
23329 objfile_name (dwarf2_per_objfile->objfile));
23330 type = build_error_marker_type (cu, die);
23331 }
23332 }
23333 else
23334 {
23335 complaint (&symfile_complaints,
23336 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23337 " from DIE at %s [in module %s]"),
23338 hex_string (signature), sect_offset_str (die->sect_off),
23339 objfile_name (dwarf2_per_objfile->objfile));
23340 type = build_error_marker_type (cu, die);
23341 }
23342 sig_type->type = type;
23343
23344 return type;
23345 }
23346
23347 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23348 reading in and processing the type unit if necessary. */
23349
23350 static struct type *
23351 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23352 struct dwarf2_cu *cu) /* ARI: editCase function */
23353 {
23354 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23355 if (attr_form_is_ref (attr))
23356 {
23357 struct dwarf2_cu *type_cu = cu;
23358 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23359
23360 return read_type_die (type_die, type_cu);
23361 }
23362 else if (attr->form == DW_FORM_ref_sig8)
23363 {
23364 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23365 }
23366 else
23367 {
23368 struct dwarf2_per_objfile *dwarf2_per_objfile
23369 = cu->per_cu->dwarf2_per_objfile;
23370
23371 complaint (&symfile_complaints,
23372 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23373 " at %s [in module %s]"),
23374 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23375 objfile_name (dwarf2_per_objfile->objfile));
23376 return build_error_marker_type (cu, die);
23377 }
23378 }
23379
23380 /* Load the DIEs associated with type unit PER_CU into memory. */
23381
23382 static void
23383 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23384 {
23385 struct signatured_type *sig_type;
23386
23387 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23388 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23389
23390 /* We have the per_cu, but we need the signatured_type.
23391 Fortunately this is an easy translation. */
23392 gdb_assert (per_cu->is_debug_types);
23393 sig_type = (struct signatured_type *) per_cu;
23394
23395 gdb_assert (per_cu->cu == NULL);
23396
23397 read_signatured_type (sig_type);
23398
23399 gdb_assert (per_cu->cu != NULL);
23400 }
23401
23402 /* die_reader_func for read_signatured_type.
23403 This is identical to load_full_comp_unit_reader,
23404 but is kept separate for now. */
23405
23406 static void
23407 read_signatured_type_reader (const struct die_reader_specs *reader,
23408 const gdb_byte *info_ptr,
23409 struct die_info *comp_unit_die,
23410 int has_children,
23411 void *data)
23412 {
23413 struct dwarf2_cu *cu = reader->cu;
23414
23415 gdb_assert (cu->die_hash == NULL);
23416 cu->die_hash =
23417 htab_create_alloc_ex (cu->header.length / 12,
23418 die_hash,
23419 die_eq,
23420 NULL,
23421 &cu->comp_unit_obstack,
23422 hashtab_obstack_allocate,
23423 dummy_obstack_deallocate);
23424
23425 if (has_children)
23426 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23427 &info_ptr, comp_unit_die);
23428 cu->dies = comp_unit_die;
23429 /* comp_unit_die is not stored in die_hash, no need. */
23430
23431 /* We try not to read any attributes in this function, because not
23432 all CUs needed for references have been loaded yet, and symbol
23433 table processing isn't initialized. But we have to set the CU language,
23434 or we won't be able to build types correctly.
23435 Similarly, if we do not read the producer, we can not apply
23436 producer-specific interpretation. */
23437 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23438 }
23439
23440 /* Read in a signatured type and build its CU and DIEs.
23441 If the type is a stub for the real type in a DWO file,
23442 read in the real type from the DWO file as well. */
23443
23444 static void
23445 read_signatured_type (struct signatured_type *sig_type)
23446 {
23447 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23448
23449 gdb_assert (per_cu->is_debug_types);
23450 gdb_assert (per_cu->cu == NULL);
23451
23452 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23453 read_signatured_type_reader, NULL);
23454 sig_type->per_cu.tu_read = 1;
23455 }
23456
23457 /* Decode simple location descriptions.
23458 Given a pointer to a dwarf block that defines a location, compute
23459 the location and return the value.
23460
23461 NOTE drow/2003-11-18: This function is called in two situations
23462 now: for the address of static or global variables (partial symbols
23463 only) and for offsets into structures which are expected to be
23464 (more or less) constant. The partial symbol case should go away,
23465 and only the constant case should remain. That will let this
23466 function complain more accurately. A few special modes are allowed
23467 without complaint for global variables (for instance, global
23468 register values and thread-local values).
23469
23470 A location description containing no operations indicates that the
23471 object is optimized out. The return value is 0 for that case.
23472 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23473 callers will only want a very basic result and this can become a
23474 complaint.
23475
23476 Note that stack[0] is unused except as a default error return. */
23477
23478 static CORE_ADDR
23479 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23480 {
23481 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23482 size_t i;
23483 size_t size = blk->size;
23484 const gdb_byte *data = blk->data;
23485 CORE_ADDR stack[64];
23486 int stacki;
23487 unsigned int bytes_read, unsnd;
23488 gdb_byte op;
23489
23490 i = 0;
23491 stacki = 0;
23492 stack[stacki] = 0;
23493 stack[++stacki] = 0;
23494
23495 while (i < size)
23496 {
23497 op = data[i++];
23498 switch (op)
23499 {
23500 case DW_OP_lit0:
23501 case DW_OP_lit1:
23502 case DW_OP_lit2:
23503 case DW_OP_lit3:
23504 case DW_OP_lit4:
23505 case DW_OP_lit5:
23506 case DW_OP_lit6:
23507 case DW_OP_lit7:
23508 case DW_OP_lit8:
23509 case DW_OP_lit9:
23510 case DW_OP_lit10:
23511 case DW_OP_lit11:
23512 case DW_OP_lit12:
23513 case DW_OP_lit13:
23514 case DW_OP_lit14:
23515 case DW_OP_lit15:
23516 case DW_OP_lit16:
23517 case DW_OP_lit17:
23518 case DW_OP_lit18:
23519 case DW_OP_lit19:
23520 case DW_OP_lit20:
23521 case DW_OP_lit21:
23522 case DW_OP_lit22:
23523 case DW_OP_lit23:
23524 case DW_OP_lit24:
23525 case DW_OP_lit25:
23526 case DW_OP_lit26:
23527 case DW_OP_lit27:
23528 case DW_OP_lit28:
23529 case DW_OP_lit29:
23530 case DW_OP_lit30:
23531 case DW_OP_lit31:
23532 stack[++stacki] = op - DW_OP_lit0;
23533 break;
23534
23535 case DW_OP_reg0:
23536 case DW_OP_reg1:
23537 case DW_OP_reg2:
23538 case DW_OP_reg3:
23539 case DW_OP_reg4:
23540 case DW_OP_reg5:
23541 case DW_OP_reg6:
23542 case DW_OP_reg7:
23543 case DW_OP_reg8:
23544 case DW_OP_reg9:
23545 case DW_OP_reg10:
23546 case DW_OP_reg11:
23547 case DW_OP_reg12:
23548 case DW_OP_reg13:
23549 case DW_OP_reg14:
23550 case DW_OP_reg15:
23551 case DW_OP_reg16:
23552 case DW_OP_reg17:
23553 case DW_OP_reg18:
23554 case DW_OP_reg19:
23555 case DW_OP_reg20:
23556 case DW_OP_reg21:
23557 case DW_OP_reg22:
23558 case DW_OP_reg23:
23559 case DW_OP_reg24:
23560 case DW_OP_reg25:
23561 case DW_OP_reg26:
23562 case DW_OP_reg27:
23563 case DW_OP_reg28:
23564 case DW_OP_reg29:
23565 case DW_OP_reg30:
23566 case DW_OP_reg31:
23567 stack[++stacki] = op - DW_OP_reg0;
23568 if (i < size)
23569 dwarf2_complex_location_expr_complaint ();
23570 break;
23571
23572 case DW_OP_regx:
23573 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23574 i += bytes_read;
23575 stack[++stacki] = unsnd;
23576 if (i < size)
23577 dwarf2_complex_location_expr_complaint ();
23578 break;
23579
23580 case DW_OP_addr:
23581 stack[++stacki] = read_address (objfile->obfd, &data[i],
23582 cu, &bytes_read);
23583 i += bytes_read;
23584 break;
23585
23586 case DW_OP_const1u:
23587 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23588 i += 1;
23589 break;
23590
23591 case DW_OP_const1s:
23592 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23593 i += 1;
23594 break;
23595
23596 case DW_OP_const2u:
23597 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23598 i += 2;
23599 break;
23600
23601 case DW_OP_const2s:
23602 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23603 i += 2;
23604 break;
23605
23606 case DW_OP_const4u:
23607 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23608 i += 4;
23609 break;
23610
23611 case DW_OP_const4s:
23612 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23613 i += 4;
23614 break;
23615
23616 case DW_OP_const8u:
23617 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23618 i += 8;
23619 break;
23620
23621 case DW_OP_constu:
23622 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23623 &bytes_read);
23624 i += bytes_read;
23625 break;
23626
23627 case DW_OP_consts:
23628 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23629 i += bytes_read;
23630 break;
23631
23632 case DW_OP_dup:
23633 stack[stacki + 1] = stack[stacki];
23634 stacki++;
23635 break;
23636
23637 case DW_OP_plus:
23638 stack[stacki - 1] += stack[stacki];
23639 stacki--;
23640 break;
23641
23642 case DW_OP_plus_uconst:
23643 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23644 &bytes_read);
23645 i += bytes_read;
23646 break;
23647
23648 case DW_OP_minus:
23649 stack[stacki - 1] -= stack[stacki];
23650 stacki--;
23651 break;
23652
23653 case DW_OP_deref:
23654 /* If we're not the last op, then we definitely can't encode
23655 this using GDB's address_class enum. This is valid for partial
23656 global symbols, although the variable's address will be bogus
23657 in the psymtab. */
23658 if (i < size)
23659 dwarf2_complex_location_expr_complaint ();
23660 break;
23661
23662 case DW_OP_GNU_push_tls_address:
23663 case DW_OP_form_tls_address:
23664 /* The top of the stack has the offset from the beginning
23665 of the thread control block at which the variable is located. */
23666 /* Nothing should follow this operator, so the top of stack would
23667 be returned. */
23668 /* This is valid for partial global symbols, but the variable's
23669 address will be bogus in the psymtab. Make it always at least
23670 non-zero to not look as a variable garbage collected by linker
23671 which have DW_OP_addr 0. */
23672 if (i < size)
23673 dwarf2_complex_location_expr_complaint ();
23674 stack[stacki]++;
23675 break;
23676
23677 case DW_OP_GNU_uninit:
23678 break;
23679
23680 case DW_OP_GNU_addr_index:
23681 case DW_OP_GNU_const_index:
23682 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23683 &bytes_read);
23684 i += bytes_read;
23685 break;
23686
23687 default:
23688 {
23689 const char *name = get_DW_OP_name (op);
23690
23691 if (name)
23692 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23693 name);
23694 else
23695 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23696 op);
23697 }
23698
23699 return (stack[stacki]);
23700 }
23701
23702 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23703 outside of the allocated space. Also enforce minimum>0. */
23704 if (stacki >= ARRAY_SIZE (stack) - 1)
23705 {
23706 complaint (&symfile_complaints,
23707 _("location description stack overflow"));
23708 return 0;
23709 }
23710
23711 if (stacki <= 0)
23712 {
23713 complaint (&symfile_complaints,
23714 _("location description stack underflow"));
23715 return 0;
23716 }
23717 }
23718 return (stack[stacki]);
23719 }
23720
23721 /* memory allocation interface */
23722
23723 static struct dwarf_block *
23724 dwarf_alloc_block (struct dwarf2_cu *cu)
23725 {
23726 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23727 }
23728
23729 static struct die_info *
23730 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23731 {
23732 struct die_info *die;
23733 size_t size = sizeof (struct die_info);
23734
23735 if (num_attrs > 1)
23736 size += (num_attrs - 1) * sizeof (struct attribute);
23737
23738 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23739 memset (die, 0, sizeof (struct die_info));
23740 return (die);
23741 }
23742
23743 \f
23744 /* Macro support. */
23745
23746 /* Return file name relative to the compilation directory of file number I in
23747 *LH's file name table. The result is allocated using xmalloc; the caller is
23748 responsible for freeing it. */
23749
23750 static char *
23751 file_file_name (int file, struct line_header *lh)
23752 {
23753 /* Is the file number a valid index into the line header's file name
23754 table? Remember that file numbers start with one, not zero. */
23755 if (1 <= file && file <= lh->file_names.size ())
23756 {
23757 const file_entry &fe = lh->file_names[file - 1];
23758
23759 if (!IS_ABSOLUTE_PATH (fe.name))
23760 {
23761 const char *dir = fe.include_dir (lh);
23762 if (dir != NULL)
23763 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23764 }
23765 return xstrdup (fe.name);
23766 }
23767 else
23768 {
23769 /* The compiler produced a bogus file number. We can at least
23770 record the macro definitions made in the file, even if we
23771 won't be able to find the file by name. */
23772 char fake_name[80];
23773
23774 xsnprintf (fake_name, sizeof (fake_name),
23775 "<bad macro file number %d>", file);
23776
23777 complaint (&symfile_complaints,
23778 _("bad file number in macro information (%d)"),
23779 file);
23780
23781 return xstrdup (fake_name);
23782 }
23783 }
23784
23785 /* Return the full name of file number I in *LH's file name table.
23786 Use COMP_DIR as the name of the current directory of the
23787 compilation. The result is allocated using xmalloc; the caller is
23788 responsible for freeing it. */
23789 static char *
23790 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23791 {
23792 /* Is the file number a valid index into the line header's file name
23793 table? Remember that file numbers start with one, not zero. */
23794 if (1 <= file && file <= lh->file_names.size ())
23795 {
23796 char *relative = file_file_name (file, lh);
23797
23798 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23799 return relative;
23800 return reconcat (relative, comp_dir, SLASH_STRING,
23801 relative, (char *) NULL);
23802 }
23803 else
23804 return file_file_name (file, lh);
23805 }
23806
23807
23808 static struct macro_source_file *
23809 macro_start_file (int file, int line,
23810 struct macro_source_file *current_file,
23811 struct line_header *lh)
23812 {
23813 /* File name relative to the compilation directory of this source file. */
23814 char *file_name = file_file_name (file, lh);
23815
23816 if (! current_file)
23817 {
23818 /* Note: We don't create a macro table for this compilation unit
23819 at all until we actually get a filename. */
23820 struct macro_table *macro_table = get_macro_table ();
23821
23822 /* If we have no current file, then this must be the start_file
23823 directive for the compilation unit's main source file. */
23824 current_file = macro_set_main (macro_table, file_name);
23825 macro_define_special (macro_table);
23826 }
23827 else
23828 current_file = macro_include (current_file, line, file_name);
23829
23830 xfree (file_name);
23831
23832 return current_file;
23833 }
23834
23835 static const char *
23836 consume_improper_spaces (const char *p, const char *body)
23837 {
23838 if (*p == ' ')
23839 {
23840 complaint (&symfile_complaints,
23841 _("macro definition contains spaces "
23842 "in formal argument list:\n`%s'"),
23843 body);
23844
23845 while (*p == ' ')
23846 p++;
23847 }
23848
23849 return p;
23850 }
23851
23852
23853 static void
23854 parse_macro_definition (struct macro_source_file *file, int line,
23855 const char *body)
23856 {
23857 const char *p;
23858
23859 /* The body string takes one of two forms. For object-like macro
23860 definitions, it should be:
23861
23862 <macro name> " " <definition>
23863
23864 For function-like macro definitions, it should be:
23865
23866 <macro name> "() " <definition>
23867 or
23868 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23869
23870 Spaces may appear only where explicitly indicated, and in the
23871 <definition>.
23872
23873 The Dwarf 2 spec says that an object-like macro's name is always
23874 followed by a space, but versions of GCC around March 2002 omit
23875 the space when the macro's definition is the empty string.
23876
23877 The Dwarf 2 spec says that there should be no spaces between the
23878 formal arguments in a function-like macro's formal argument list,
23879 but versions of GCC around March 2002 include spaces after the
23880 commas. */
23881
23882
23883 /* Find the extent of the macro name. The macro name is terminated
23884 by either a space or null character (for an object-like macro) or
23885 an opening paren (for a function-like macro). */
23886 for (p = body; *p; p++)
23887 if (*p == ' ' || *p == '(')
23888 break;
23889
23890 if (*p == ' ' || *p == '\0')
23891 {
23892 /* It's an object-like macro. */
23893 int name_len = p - body;
23894 char *name = savestring (body, name_len);
23895 const char *replacement;
23896
23897 if (*p == ' ')
23898 replacement = body + name_len + 1;
23899 else
23900 {
23901 dwarf2_macro_malformed_definition_complaint (body);
23902 replacement = body + name_len;
23903 }
23904
23905 macro_define_object (file, line, name, replacement);
23906
23907 xfree (name);
23908 }
23909 else if (*p == '(')
23910 {
23911 /* It's a function-like macro. */
23912 char *name = savestring (body, p - body);
23913 int argc = 0;
23914 int argv_size = 1;
23915 char **argv = XNEWVEC (char *, argv_size);
23916
23917 p++;
23918
23919 p = consume_improper_spaces (p, body);
23920
23921 /* Parse the formal argument list. */
23922 while (*p && *p != ')')
23923 {
23924 /* Find the extent of the current argument name. */
23925 const char *arg_start = p;
23926
23927 while (*p && *p != ',' && *p != ')' && *p != ' ')
23928 p++;
23929
23930 if (! *p || p == arg_start)
23931 dwarf2_macro_malformed_definition_complaint (body);
23932 else
23933 {
23934 /* Make sure argv has room for the new argument. */
23935 if (argc >= argv_size)
23936 {
23937 argv_size *= 2;
23938 argv = XRESIZEVEC (char *, argv, argv_size);
23939 }
23940
23941 argv[argc++] = savestring (arg_start, p - arg_start);
23942 }
23943
23944 p = consume_improper_spaces (p, body);
23945
23946 /* Consume the comma, if present. */
23947 if (*p == ',')
23948 {
23949 p++;
23950
23951 p = consume_improper_spaces (p, body);
23952 }
23953 }
23954
23955 if (*p == ')')
23956 {
23957 p++;
23958
23959 if (*p == ' ')
23960 /* Perfectly formed definition, no complaints. */
23961 macro_define_function (file, line, name,
23962 argc, (const char **) argv,
23963 p + 1);
23964 else if (*p == '\0')
23965 {
23966 /* Complain, but do define it. */
23967 dwarf2_macro_malformed_definition_complaint (body);
23968 macro_define_function (file, line, name,
23969 argc, (const char **) argv,
23970 p);
23971 }
23972 else
23973 /* Just complain. */
23974 dwarf2_macro_malformed_definition_complaint (body);
23975 }
23976 else
23977 /* Just complain. */
23978 dwarf2_macro_malformed_definition_complaint (body);
23979
23980 xfree (name);
23981 {
23982 int i;
23983
23984 for (i = 0; i < argc; i++)
23985 xfree (argv[i]);
23986 }
23987 xfree (argv);
23988 }
23989 else
23990 dwarf2_macro_malformed_definition_complaint (body);
23991 }
23992
23993 /* Skip some bytes from BYTES according to the form given in FORM.
23994 Returns the new pointer. */
23995
23996 static const gdb_byte *
23997 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23998 enum dwarf_form form,
23999 unsigned int offset_size,
24000 struct dwarf2_section_info *section)
24001 {
24002 unsigned int bytes_read;
24003
24004 switch (form)
24005 {
24006 case DW_FORM_data1:
24007 case DW_FORM_flag:
24008 ++bytes;
24009 break;
24010
24011 case DW_FORM_data2:
24012 bytes += 2;
24013 break;
24014
24015 case DW_FORM_data4:
24016 bytes += 4;
24017 break;
24018
24019 case DW_FORM_data8:
24020 bytes += 8;
24021 break;
24022
24023 case DW_FORM_data16:
24024 bytes += 16;
24025 break;
24026
24027 case DW_FORM_string:
24028 read_direct_string (abfd, bytes, &bytes_read);
24029 bytes += bytes_read;
24030 break;
24031
24032 case DW_FORM_sec_offset:
24033 case DW_FORM_strp:
24034 case DW_FORM_GNU_strp_alt:
24035 bytes += offset_size;
24036 break;
24037
24038 case DW_FORM_block:
24039 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24040 bytes += bytes_read;
24041 break;
24042
24043 case DW_FORM_block1:
24044 bytes += 1 + read_1_byte (abfd, bytes);
24045 break;
24046 case DW_FORM_block2:
24047 bytes += 2 + read_2_bytes (abfd, bytes);
24048 break;
24049 case DW_FORM_block4:
24050 bytes += 4 + read_4_bytes (abfd, bytes);
24051 break;
24052
24053 case DW_FORM_sdata:
24054 case DW_FORM_udata:
24055 case DW_FORM_GNU_addr_index:
24056 case DW_FORM_GNU_str_index:
24057 bytes = gdb_skip_leb128 (bytes, buffer_end);
24058 if (bytes == NULL)
24059 {
24060 dwarf2_section_buffer_overflow_complaint (section);
24061 return NULL;
24062 }
24063 break;
24064
24065 case DW_FORM_implicit_const:
24066 break;
24067
24068 default:
24069 {
24070 complaint (&symfile_complaints,
24071 _("invalid form 0x%x in `%s'"),
24072 form, get_section_name (section));
24073 return NULL;
24074 }
24075 }
24076
24077 return bytes;
24078 }
24079
24080 /* A helper for dwarf_decode_macros that handles skipping an unknown
24081 opcode. Returns an updated pointer to the macro data buffer; or,
24082 on error, issues a complaint and returns NULL. */
24083
24084 static const gdb_byte *
24085 skip_unknown_opcode (unsigned int opcode,
24086 const gdb_byte **opcode_definitions,
24087 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24088 bfd *abfd,
24089 unsigned int offset_size,
24090 struct dwarf2_section_info *section)
24091 {
24092 unsigned int bytes_read, i;
24093 unsigned long arg;
24094 const gdb_byte *defn;
24095
24096 if (opcode_definitions[opcode] == NULL)
24097 {
24098 complaint (&symfile_complaints,
24099 _("unrecognized DW_MACFINO opcode 0x%x"),
24100 opcode);
24101 return NULL;
24102 }
24103
24104 defn = opcode_definitions[opcode];
24105 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24106 defn += bytes_read;
24107
24108 for (i = 0; i < arg; ++i)
24109 {
24110 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24111 (enum dwarf_form) defn[i], offset_size,
24112 section);
24113 if (mac_ptr == NULL)
24114 {
24115 /* skip_form_bytes already issued the complaint. */
24116 return NULL;
24117 }
24118 }
24119
24120 return mac_ptr;
24121 }
24122
24123 /* A helper function which parses the header of a macro section.
24124 If the macro section is the extended (for now called "GNU") type,
24125 then this updates *OFFSET_SIZE. Returns a pointer to just after
24126 the header, or issues a complaint and returns NULL on error. */
24127
24128 static const gdb_byte *
24129 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24130 bfd *abfd,
24131 const gdb_byte *mac_ptr,
24132 unsigned int *offset_size,
24133 int section_is_gnu)
24134 {
24135 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24136
24137 if (section_is_gnu)
24138 {
24139 unsigned int version, flags;
24140
24141 version = read_2_bytes (abfd, mac_ptr);
24142 if (version != 4 && version != 5)
24143 {
24144 complaint (&symfile_complaints,
24145 _("unrecognized version `%d' in .debug_macro section"),
24146 version);
24147 return NULL;
24148 }
24149 mac_ptr += 2;
24150
24151 flags = read_1_byte (abfd, mac_ptr);
24152 ++mac_ptr;
24153 *offset_size = (flags & 1) ? 8 : 4;
24154
24155 if ((flags & 2) != 0)
24156 /* We don't need the line table offset. */
24157 mac_ptr += *offset_size;
24158
24159 /* Vendor opcode descriptions. */
24160 if ((flags & 4) != 0)
24161 {
24162 unsigned int i, count;
24163
24164 count = read_1_byte (abfd, mac_ptr);
24165 ++mac_ptr;
24166 for (i = 0; i < count; ++i)
24167 {
24168 unsigned int opcode, bytes_read;
24169 unsigned long arg;
24170
24171 opcode = read_1_byte (abfd, mac_ptr);
24172 ++mac_ptr;
24173 opcode_definitions[opcode] = mac_ptr;
24174 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24175 mac_ptr += bytes_read;
24176 mac_ptr += arg;
24177 }
24178 }
24179 }
24180
24181 return mac_ptr;
24182 }
24183
24184 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24185 including DW_MACRO_import. */
24186
24187 static void
24188 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24189 bfd *abfd,
24190 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24191 struct macro_source_file *current_file,
24192 struct line_header *lh,
24193 struct dwarf2_section_info *section,
24194 int section_is_gnu, int section_is_dwz,
24195 unsigned int offset_size,
24196 htab_t include_hash)
24197 {
24198 struct objfile *objfile = dwarf2_per_objfile->objfile;
24199 enum dwarf_macro_record_type macinfo_type;
24200 int at_commandline;
24201 const gdb_byte *opcode_definitions[256];
24202
24203 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24204 &offset_size, section_is_gnu);
24205 if (mac_ptr == NULL)
24206 {
24207 /* We already issued a complaint. */
24208 return;
24209 }
24210
24211 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24212 GDB is still reading the definitions from command line. First
24213 DW_MACINFO_start_file will need to be ignored as it was already executed
24214 to create CURRENT_FILE for the main source holding also the command line
24215 definitions. On first met DW_MACINFO_start_file this flag is reset to
24216 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24217
24218 at_commandline = 1;
24219
24220 do
24221 {
24222 /* Do we at least have room for a macinfo type byte? */
24223 if (mac_ptr >= mac_end)
24224 {
24225 dwarf2_section_buffer_overflow_complaint (section);
24226 break;
24227 }
24228
24229 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24230 mac_ptr++;
24231
24232 /* Note that we rely on the fact that the corresponding GNU and
24233 DWARF constants are the same. */
24234 DIAGNOSTIC_PUSH
24235 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24236 switch (macinfo_type)
24237 {
24238 /* A zero macinfo type indicates the end of the macro
24239 information. */
24240 case 0:
24241 break;
24242
24243 case DW_MACRO_define:
24244 case DW_MACRO_undef:
24245 case DW_MACRO_define_strp:
24246 case DW_MACRO_undef_strp:
24247 case DW_MACRO_define_sup:
24248 case DW_MACRO_undef_sup:
24249 {
24250 unsigned int bytes_read;
24251 int line;
24252 const char *body;
24253 int is_define;
24254
24255 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24256 mac_ptr += bytes_read;
24257
24258 if (macinfo_type == DW_MACRO_define
24259 || macinfo_type == DW_MACRO_undef)
24260 {
24261 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24262 mac_ptr += bytes_read;
24263 }
24264 else
24265 {
24266 LONGEST str_offset;
24267
24268 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24269 mac_ptr += offset_size;
24270
24271 if (macinfo_type == DW_MACRO_define_sup
24272 || macinfo_type == DW_MACRO_undef_sup
24273 || section_is_dwz)
24274 {
24275 struct dwz_file *dwz
24276 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24277
24278 body = read_indirect_string_from_dwz (objfile,
24279 dwz, str_offset);
24280 }
24281 else
24282 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24283 abfd, str_offset);
24284 }
24285
24286 is_define = (macinfo_type == DW_MACRO_define
24287 || macinfo_type == DW_MACRO_define_strp
24288 || macinfo_type == DW_MACRO_define_sup);
24289 if (! current_file)
24290 {
24291 /* DWARF violation as no main source is present. */
24292 complaint (&symfile_complaints,
24293 _("debug info with no main source gives macro %s "
24294 "on line %d: %s"),
24295 is_define ? _("definition") : _("undefinition"),
24296 line, body);
24297 break;
24298 }
24299 if ((line == 0 && !at_commandline)
24300 || (line != 0 && at_commandline))
24301 complaint (&symfile_complaints,
24302 _("debug info gives %s macro %s with %s line %d: %s"),
24303 at_commandline ? _("command-line") : _("in-file"),
24304 is_define ? _("definition") : _("undefinition"),
24305 line == 0 ? _("zero") : _("non-zero"), line, body);
24306
24307 if (is_define)
24308 parse_macro_definition (current_file, line, body);
24309 else
24310 {
24311 gdb_assert (macinfo_type == DW_MACRO_undef
24312 || macinfo_type == DW_MACRO_undef_strp
24313 || macinfo_type == DW_MACRO_undef_sup);
24314 macro_undef (current_file, line, body);
24315 }
24316 }
24317 break;
24318
24319 case DW_MACRO_start_file:
24320 {
24321 unsigned int bytes_read;
24322 int line, file;
24323
24324 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24325 mac_ptr += bytes_read;
24326 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24327 mac_ptr += bytes_read;
24328
24329 if ((line == 0 && !at_commandline)
24330 || (line != 0 && at_commandline))
24331 complaint (&symfile_complaints,
24332 _("debug info gives source %d included "
24333 "from %s at %s line %d"),
24334 file, at_commandline ? _("command-line") : _("file"),
24335 line == 0 ? _("zero") : _("non-zero"), line);
24336
24337 if (at_commandline)
24338 {
24339 /* This DW_MACRO_start_file was executed in the
24340 pass one. */
24341 at_commandline = 0;
24342 }
24343 else
24344 current_file = macro_start_file (file, line, current_file, lh);
24345 }
24346 break;
24347
24348 case DW_MACRO_end_file:
24349 if (! current_file)
24350 complaint (&symfile_complaints,
24351 _("macro debug info has an unmatched "
24352 "`close_file' directive"));
24353 else
24354 {
24355 current_file = current_file->included_by;
24356 if (! current_file)
24357 {
24358 enum dwarf_macro_record_type next_type;
24359
24360 /* GCC circa March 2002 doesn't produce the zero
24361 type byte marking the end of the compilation
24362 unit. Complain if it's not there, but exit no
24363 matter what. */
24364
24365 /* Do we at least have room for a macinfo type byte? */
24366 if (mac_ptr >= mac_end)
24367 {
24368 dwarf2_section_buffer_overflow_complaint (section);
24369 return;
24370 }
24371
24372 /* We don't increment mac_ptr here, so this is just
24373 a look-ahead. */
24374 next_type
24375 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24376 mac_ptr);
24377 if (next_type != 0)
24378 complaint (&symfile_complaints,
24379 _("no terminating 0-type entry for "
24380 "macros in `.debug_macinfo' section"));
24381
24382 return;
24383 }
24384 }
24385 break;
24386
24387 case DW_MACRO_import:
24388 case DW_MACRO_import_sup:
24389 {
24390 LONGEST offset;
24391 void **slot;
24392 bfd *include_bfd = abfd;
24393 struct dwarf2_section_info *include_section = section;
24394 const gdb_byte *include_mac_end = mac_end;
24395 int is_dwz = section_is_dwz;
24396 const gdb_byte *new_mac_ptr;
24397
24398 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24399 mac_ptr += offset_size;
24400
24401 if (macinfo_type == DW_MACRO_import_sup)
24402 {
24403 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24404
24405 dwarf2_read_section (objfile, &dwz->macro);
24406
24407 include_section = &dwz->macro;
24408 include_bfd = get_section_bfd_owner (include_section);
24409 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24410 is_dwz = 1;
24411 }
24412
24413 new_mac_ptr = include_section->buffer + offset;
24414 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24415
24416 if (*slot != NULL)
24417 {
24418 /* This has actually happened; see
24419 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24420 complaint (&symfile_complaints,
24421 _("recursive DW_MACRO_import in "
24422 ".debug_macro section"));
24423 }
24424 else
24425 {
24426 *slot = (void *) new_mac_ptr;
24427
24428 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24429 include_bfd, new_mac_ptr,
24430 include_mac_end, current_file, lh,
24431 section, section_is_gnu, is_dwz,
24432 offset_size, include_hash);
24433
24434 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24435 }
24436 }
24437 break;
24438
24439 case DW_MACINFO_vendor_ext:
24440 if (!section_is_gnu)
24441 {
24442 unsigned int bytes_read;
24443
24444 /* This reads the constant, but since we don't recognize
24445 any vendor extensions, we ignore it. */
24446 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24447 mac_ptr += bytes_read;
24448 read_direct_string (abfd, mac_ptr, &bytes_read);
24449 mac_ptr += bytes_read;
24450
24451 /* We don't recognize any vendor extensions. */
24452 break;
24453 }
24454 /* FALLTHROUGH */
24455
24456 default:
24457 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24458 mac_ptr, mac_end, abfd, offset_size,
24459 section);
24460 if (mac_ptr == NULL)
24461 return;
24462 break;
24463 }
24464 DIAGNOSTIC_POP
24465 } while (macinfo_type != 0);
24466 }
24467
24468 static void
24469 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24470 int section_is_gnu)
24471 {
24472 struct dwarf2_per_objfile *dwarf2_per_objfile
24473 = cu->per_cu->dwarf2_per_objfile;
24474 struct objfile *objfile = dwarf2_per_objfile->objfile;
24475 struct line_header *lh = cu->line_header;
24476 bfd *abfd;
24477 const gdb_byte *mac_ptr, *mac_end;
24478 struct macro_source_file *current_file = 0;
24479 enum dwarf_macro_record_type macinfo_type;
24480 unsigned int offset_size = cu->header.offset_size;
24481 const gdb_byte *opcode_definitions[256];
24482 void **slot;
24483 struct dwarf2_section_info *section;
24484 const char *section_name;
24485
24486 if (cu->dwo_unit != NULL)
24487 {
24488 if (section_is_gnu)
24489 {
24490 section = &cu->dwo_unit->dwo_file->sections.macro;
24491 section_name = ".debug_macro.dwo";
24492 }
24493 else
24494 {
24495 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24496 section_name = ".debug_macinfo.dwo";
24497 }
24498 }
24499 else
24500 {
24501 if (section_is_gnu)
24502 {
24503 section = &dwarf2_per_objfile->macro;
24504 section_name = ".debug_macro";
24505 }
24506 else
24507 {
24508 section = &dwarf2_per_objfile->macinfo;
24509 section_name = ".debug_macinfo";
24510 }
24511 }
24512
24513 dwarf2_read_section (objfile, section);
24514 if (section->buffer == NULL)
24515 {
24516 complaint (&symfile_complaints, _("missing %s section"), section_name);
24517 return;
24518 }
24519 abfd = get_section_bfd_owner (section);
24520
24521 /* First pass: Find the name of the base filename.
24522 This filename is needed in order to process all macros whose definition
24523 (or undefinition) comes from the command line. These macros are defined
24524 before the first DW_MACINFO_start_file entry, and yet still need to be
24525 associated to the base file.
24526
24527 To determine the base file name, we scan the macro definitions until we
24528 reach the first DW_MACINFO_start_file entry. We then initialize
24529 CURRENT_FILE accordingly so that any macro definition found before the
24530 first DW_MACINFO_start_file can still be associated to the base file. */
24531
24532 mac_ptr = section->buffer + offset;
24533 mac_end = section->buffer + section->size;
24534
24535 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24536 &offset_size, section_is_gnu);
24537 if (mac_ptr == NULL)
24538 {
24539 /* We already issued a complaint. */
24540 return;
24541 }
24542
24543 do
24544 {
24545 /* Do we at least have room for a macinfo type byte? */
24546 if (mac_ptr >= mac_end)
24547 {
24548 /* Complaint is printed during the second pass as GDB will probably
24549 stop the first pass earlier upon finding
24550 DW_MACINFO_start_file. */
24551 break;
24552 }
24553
24554 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24555 mac_ptr++;
24556
24557 /* Note that we rely on the fact that the corresponding GNU and
24558 DWARF constants are the same. */
24559 DIAGNOSTIC_PUSH
24560 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24561 switch (macinfo_type)
24562 {
24563 /* A zero macinfo type indicates the end of the macro
24564 information. */
24565 case 0:
24566 break;
24567
24568 case DW_MACRO_define:
24569 case DW_MACRO_undef:
24570 /* Only skip the data by MAC_PTR. */
24571 {
24572 unsigned int bytes_read;
24573
24574 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24575 mac_ptr += bytes_read;
24576 read_direct_string (abfd, mac_ptr, &bytes_read);
24577 mac_ptr += bytes_read;
24578 }
24579 break;
24580
24581 case DW_MACRO_start_file:
24582 {
24583 unsigned int bytes_read;
24584 int line, file;
24585
24586 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24587 mac_ptr += bytes_read;
24588 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24589 mac_ptr += bytes_read;
24590
24591 current_file = macro_start_file (file, line, current_file, lh);
24592 }
24593 break;
24594
24595 case DW_MACRO_end_file:
24596 /* No data to skip by MAC_PTR. */
24597 break;
24598
24599 case DW_MACRO_define_strp:
24600 case DW_MACRO_undef_strp:
24601 case DW_MACRO_define_sup:
24602 case DW_MACRO_undef_sup:
24603 {
24604 unsigned int bytes_read;
24605
24606 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24607 mac_ptr += bytes_read;
24608 mac_ptr += offset_size;
24609 }
24610 break;
24611
24612 case DW_MACRO_import:
24613 case DW_MACRO_import_sup:
24614 /* Note that, according to the spec, a transparent include
24615 chain cannot call DW_MACRO_start_file. So, we can just
24616 skip this opcode. */
24617 mac_ptr += offset_size;
24618 break;
24619
24620 case DW_MACINFO_vendor_ext:
24621 /* Only skip the data by MAC_PTR. */
24622 if (!section_is_gnu)
24623 {
24624 unsigned int bytes_read;
24625
24626 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24627 mac_ptr += bytes_read;
24628 read_direct_string (abfd, mac_ptr, &bytes_read);
24629 mac_ptr += bytes_read;
24630 }
24631 /* FALLTHROUGH */
24632
24633 default:
24634 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24635 mac_ptr, mac_end, abfd, offset_size,
24636 section);
24637 if (mac_ptr == NULL)
24638 return;
24639 break;
24640 }
24641 DIAGNOSTIC_POP
24642 } while (macinfo_type != 0 && current_file == NULL);
24643
24644 /* Second pass: Process all entries.
24645
24646 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24647 command-line macro definitions/undefinitions. This flag is unset when we
24648 reach the first DW_MACINFO_start_file entry. */
24649
24650 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24651 htab_eq_pointer,
24652 NULL, xcalloc, xfree));
24653 mac_ptr = section->buffer + offset;
24654 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24655 *slot = (void *) mac_ptr;
24656 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24657 abfd, mac_ptr, mac_end,
24658 current_file, lh, section,
24659 section_is_gnu, 0, offset_size,
24660 include_hash.get ());
24661 }
24662
24663 /* Check if the attribute's form is a DW_FORM_block*
24664 if so return true else false. */
24665
24666 static int
24667 attr_form_is_block (const struct attribute *attr)
24668 {
24669 return (attr == NULL ? 0 :
24670 attr->form == DW_FORM_block1
24671 || attr->form == DW_FORM_block2
24672 || attr->form == DW_FORM_block4
24673 || attr->form == DW_FORM_block
24674 || attr->form == DW_FORM_exprloc);
24675 }
24676
24677 /* Return non-zero if ATTR's value is a section offset --- classes
24678 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24679 You may use DW_UNSND (attr) to retrieve such offsets.
24680
24681 Section 7.5.4, "Attribute Encodings", explains that no attribute
24682 may have a value that belongs to more than one of these classes; it
24683 would be ambiguous if we did, because we use the same forms for all
24684 of them. */
24685
24686 static int
24687 attr_form_is_section_offset (const struct attribute *attr)
24688 {
24689 return (attr->form == DW_FORM_data4
24690 || attr->form == DW_FORM_data8
24691 || attr->form == DW_FORM_sec_offset);
24692 }
24693
24694 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24695 zero otherwise. When this function returns true, you can apply
24696 dwarf2_get_attr_constant_value to it.
24697
24698 However, note that for some attributes you must check
24699 attr_form_is_section_offset before using this test. DW_FORM_data4
24700 and DW_FORM_data8 are members of both the constant class, and of
24701 the classes that contain offsets into other debug sections
24702 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24703 that, if an attribute's can be either a constant or one of the
24704 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24705 taken as section offsets, not constants.
24706
24707 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24708 cannot handle that. */
24709
24710 static int
24711 attr_form_is_constant (const struct attribute *attr)
24712 {
24713 switch (attr->form)
24714 {
24715 case DW_FORM_sdata:
24716 case DW_FORM_udata:
24717 case DW_FORM_data1:
24718 case DW_FORM_data2:
24719 case DW_FORM_data4:
24720 case DW_FORM_data8:
24721 case DW_FORM_implicit_const:
24722 return 1;
24723 default:
24724 return 0;
24725 }
24726 }
24727
24728
24729 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24730 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24731
24732 static int
24733 attr_form_is_ref (const struct attribute *attr)
24734 {
24735 switch (attr->form)
24736 {
24737 case DW_FORM_ref_addr:
24738 case DW_FORM_ref1:
24739 case DW_FORM_ref2:
24740 case DW_FORM_ref4:
24741 case DW_FORM_ref8:
24742 case DW_FORM_ref_udata:
24743 case DW_FORM_GNU_ref_alt:
24744 return 1;
24745 default:
24746 return 0;
24747 }
24748 }
24749
24750 /* Return the .debug_loc section to use for CU.
24751 For DWO files use .debug_loc.dwo. */
24752
24753 static struct dwarf2_section_info *
24754 cu_debug_loc_section (struct dwarf2_cu *cu)
24755 {
24756 struct dwarf2_per_objfile *dwarf2_per_objfile
24757 = cu->per_cu->dwarf2_per_objfile;
24758
24759 if (cu->dwo_unit)
24760 {
24761 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24762
24763 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24764 }
24765 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24766 : &dwarf2_per_objfile->loc);
24767 }
24768
24769 /* A helper function that fills in a dwarf2_loclist_baton. */
24770
24771 static void
24772 fill_in_loclist_baton (struct dwarf2_cu *cu,
24773 struct dwarf2_loclist_baton *baton,
24774 const struct attribute *attr)
24775 {
24776 struct dwarf2_per_objfile *dwarf2_per_objfile
24777 = cu->per_cu->dwarf2_per_objfile;
24778 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24779
24780 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24781
24782 baton->per_cu = cu->per_cu;
24783 gdb_assert (baton->per_cu);
24784 /* We don't know how long the location list is, but make sure we
24785 don't run off the edge of the section. */
24786 baton->size = section->size - DW_UNSND (attr);
24787 baton->data = section->buffer + DW_UNSND (attr);
24788 baton->base_address = cu->base_address;
24789 baton->from_dwo = cu->dwo_unit != NULL;
24790 }
24791
24792 static void
24793 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24794 struct dwarf2_cu *cu, int is_block)
24795 {
24796 struct dwarf2_per_objfile *dwarf2_per_objfile
24797 = cu->per_cu->dwarf2_per_objfile;
24798 struct objfile *objfile = dwarf2_per_objfile->objfile;
24799 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24800
24801 if (attr_form_is_section_offset (attr)
24802 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24803 the section. If so, fall through to the complaint in the
24804 other branch. */
24805 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24806 {
24807 struct dwarf2_loclist_baton *baton;
24808
24809 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24810
24811 fill_in_loclist_baton (cu, baton, attr);
24812
24813 if (cu->base_known == 0)
24814 complaint (&symfile_complaints,
24815 _("Location list used without "
24816 "specifying the CU base address."));
24817
24818 SYMBOL_ACLASS_INDEX (sym) = (is_block
24819 ? dwarf2_loclist_block_index
24820 : dwarf2_loclist_index);
24821 SYMBOL_LOCATION_BATON (sym) = baton;
24822 }
24823 else
24824 {
24825 struct dwarf2_locexpr_baton *baton;
24826
24827 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24828 baton->per_cu = cu->per_cu;
24829 gdb_assert (baton->per_cu);
24830
24831 if (attr_form_is_block (attr))
24832 {
24833 /* Note that we're just copying the block's data pointer
24834 here, not the actual data. We're still pointing into the
24835 info_buffer for SYM's objfile; right now we never release
24836 that buffer, but when we do clean up properly this may
24837 need to change. */
24838 baton->size = DW_BLOCK (attr)->size;
24839 baton->data = DW_BLOCK (attr)->data;
24840 }
24841 else
24842 {
24843 dwarf2_invalid_attrib_class_complaint ("location description",
24844 SYMBOL_NATURAL_NAME (sym));
24845 baton->size = 0;
24846 }
24847
24848 SYMBOL_ACLASS_INDEX (sym) = (is_block
24849 ? dwarf2_locexpr_block_index
24850 : dwarf2_locexpr_index);
24851 SYMBOL_LOCATION_BATON (sym) = baton;
24852 }
24853 }
24854
24855 /* Return the OBJFILE associated with the compilation unit CU. If CU
24856 came from a separate debuginfo file, then the master objfile is
24857 returned. */
24858
24859 struct objfile *
24860 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24861 {
24862 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24863
24864 /* Return the master objfile, so that we can report and look up the
24865 correct file containing this variable. */
24866 if (objfile->separate_debug_objfile_backlink)
24867 objfile = objfile->separate_debug_objfile_backlink;
24868
24869 return objfile;
24870 }
24871
24872 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24873 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24874 CU_HEADERP first. */
24875
24876 static const struct comp_unit_head *
24877 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24878 struct dwarf2_per_cu_data *per_cu)
24879 {
24880 const gdb_byte *info_ptr;
24881
24882 if (per_cu->cu)
24883 return &per_cu->cu->header;
24884
24885 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24886
24887 memset (cu_headerp, 0, sizeof (*cu_headerp));
24888 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24889 rcuh_kind::COMPILE);
24890
24891 return cu_headerp;
24892 }
24893
24894 /* Return the address size given in the compilation unit header for CU. */
24895
24896 int
24897 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24898 {
24899 struct comp_unit_head cu_header_local;
24900 const struct comp_unit_head *cu_headerp;
24901
24902 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24903
24904 return cu_headerp->addr_size;
24905 }
24906
24907 /* Return the offset size given in the compilation unit header for CU. */
24908
24909 int
24910 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24911 {
24912 struct comp_unit_head cu_header_local;
24913 const struct comp_unit_head *cu_headerp;
24914
24915 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24916
24917 return cu_headerp->offset_size;
24918 }
24919
24920 /* See its dwarf2loc.h declaration. */
24921
24922 int
24923 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24924 {
24925 struct comp_unit_head cu_header_local;
24926 const struct comp_unit_head *cu_headerp;
24927
24928 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24929
24930 if (cu_headerp->version == 2)
24931 return cu_headerp->addr_size;
24932 else
24933 return cu_headerp->offset_size;
24934 }
24935
24936 /* Return the text offset of the CU. The returned offset comes from
24937 this CU's objfile. If this objfile came from a separate debuginfo
24938 file, then the offset may be different from the corresponding
24939 offset in the parent objfile. */
24940
24941 CORE_ADDR
24942 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24943 {
24944 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24945
24946 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24947 }
24948
24949 /* Return DWARF version number of PER_CU. */
24950
24951 short
24952 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24953 {
24954 return per_cu->dwarf_version;
24955 }
24956
24957 /* Locate the .debug_info compilation unit from CU's objfile which contains
24958 the DIE at OFFSET. Raises an error on failure. */
24959
24960 static struct dwarf2_per_cu_data *
24961 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24962 unsigned int offset_in_dwz,
24963 struct dwarf2_per_objfile *dwarf2_per_objfile)
24964 {
24965 struct dwarf2_per_cu_data *this_cu;
24966 int low, high;
24967 const sect_offset *cu_off;
24968
24969 low = 0;
24970 high = dwarf2_per_objfile->n_comp_units - 1;
24971 while (high > low)
24972 {
24973 struct dwarf2_per_cu_data *mid_cu;
24974 int mid = low + (high - low) / 2;
24975
24976 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24977 cu_off = &mid_cu->sect_off;
24978 if (mid_cu->is_dwz > offset_in_dwz
24979 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
24980 high = mid;
24981 else
24982 low = mid + 1;
24983 }
24984 gdb_assert (low == high);
24985 this_cu = dwarf2_per_objfile->all_comp_units[low];
24986 cu_off = &this_cu->sect_off;
24987 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
24988 {
24989 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24990 error (_("Dwarf Error: could not find partial DIE containing "
24991 "offset %s [in module %s]"),
24992 sect_offset_str (sect_off),
24993 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24994
24995 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24996 <= sect_off);
24997 return dwarf2_per_objfile->all_comp_units[low-1];
24998 }
24999 else
25000 {
25001 this_cu = dwarf2_per_objfile->all_comp_units[low];
25002 if (low == dwarf2_per_objfile->n_comp_units - 1
25003 && sect_off >= this_cu->sect_off + this_cu->length)
25004 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25005 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25006 return this_cu;
25007 }
25008 }
25009
25010 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25011
25012 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25013 : per_cu (per_cu_),
25014 mark (0),
25015 has_loclist (0),
25016 checked_producer (0),
25017 producer_is_gxx_lt_4_6 (0),
25018 producer_is_gcc_lt_4_3 (0),
25019 producer_is_icc_lt_14 (0),
25020 processing_has_namespace_info (0)
25021 {
25022 per_cu->cu = this;
25023 }
25024
25025 /* Destroy a dwarf2_cu. */
25026
25027 dwarf2_cu::~dwarf2_cu ()
25028 {
25029 per_cu->cu = NULL;
25030 }
25031
25032 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25033
25034 static void
25035 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25036 enum language pretend_language)
25037 {
25038 struct attribute *attr;
25039
25040 /* Set the language we're debugging. */
25041 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25042 if (attr)
25043 set_cu_language (DW_UNSND (attr), cu);
25044 else
25045 {
25046 cu->language = pretend_language;
25047 cu->language_defn = language_def (cu->language);
25048 }
25049
25050 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25051 }
25052
25053 /* Increase the age counter on each cached compilation unit, and free
25054 any that are too old. */
25055
25056 static void
25057 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25058 {
25059 struct dwarf2_per_cu_data *per_cu, **last_chain;
25060
25061 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25062 per_cu = dwarf2_per_objfile->read_in_chain;
25063 while (per_cu != NULL)
25064 {
25065 per_cu->cu->last_used ++;
25066 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25067 dwarf2_mark (per_cu->cu);
25068 per_cu = per_cu->cu->read_in_chain;
25069 }
25070
25071 per_cu = dwarf2_per_objfile->read_in_chain;
25072 last_chain = &dwarf2_per_objfile->read_in_chain;
25073 while (per_cu != NULL)
25074 {
25075 struct dwarf2_per_cu_data *next_cu;
25076
25077 next_cu = per_cu->cu->read_in_chain;
25078
25079 if (!per_cu->cu->mark)
25080 {
25081 delete per_cu->cu;
25082 *last_chain = next_cu;
25083 }
25084 else
25085 last_chain = &per_cu->cu->read_in_chain;
25086
25087 per_cu = next_cu;
25088 }
25089 }
25090
25091 /* Remove a single compilation unit from the cache. */
25092
25093 static void
25094 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25095 {
25096 struct dwarf2_per_cu_data *per_cu, **last_chain;
25097 struct dwarf2_per_objfile *dwarf2_per_objfile
25098 = target_per_cu->dwarf2_per_objfile;
25099
25100 per_cu = dwarf2_per_objfile->read_in_chain;
25101 last_chain = &dwarf2_per_objfile->read_in_chain;
25102 while (per_cu != NULL)
25103 {
25104 struct dwarf2_per_cu_data *next_cu;
25105
25106 next_cu = per_cu->cu->read_in_chain;
25107
25108 if (per_cu == target_per_cu)
25109 {
25110 delete per_cu->cu;
25111 per_cu->cu = NULL;
25112 *last_chain = next_cu;
25113 break;
25114 }
25115 else
25116 last_chain = &per_cu->cu->read_in_chain;
25117
25118 per_cu = next_cu;
25119 }
25120 }
25121
25122 /* Release all extra memory associated with OBJFILE. */
25123
25124 void
25125 dwarf2_free_objfile (struct objfile *objfile)
25126 {
25127 struct dwarf2_per_objfile *dwarf2_per_objfile
25128 = get_dwarf2_per_objfile (objfile);
25129
25130 delete dwarf2_per_objfile;
25131 }
25132
25133 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25134 We store these in a hash table separate from the DIEs, and preserve them
25135 when the DIEs are flushed out of cache.
25136
25137 The CU "per_cu" pointer is needed because offset alone is not enough to
25138 uniquely identify the type. A file may have multiple .debug_types sections,
25139 or the type may come from a DWO file. Furthermore, while it's more logical
25140 to use per_cu->section+offset, with Fission the section with the data is in
25141 the DWO file but we don't know that section at the point we need it.
25142 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25143 because we can enter the lookup routine, get_die_type_at_offset, from
25144 outside this file, and thus won't necessarily have PER_CU->cu.
25145 Fortunately, PER_CU is stable for the life of the objfile. */
25146
25147 struct dwarf2_per_cu_offset_and_type
25148 {
25149 const struct dwarf2_per_cu_data *per_cu;
25150 sect_offset sect_off;
25151 struct type *type;
25152 };
25153
25154 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25155
25156 static hashval_t
25157 per_cu_offset_and_type_hash (const void *item)
25158 {
25159 const struct dwarf2_per_cu_offset_and_type *ofs
25160 = (const struct dwarf2_per_cu_offset_and_type *) item;
25161
25162 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25163 }
25164
25165 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25166
25167 static int
25168 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25169 {
25170 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25171 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25172 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25173 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25174
25175 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25176 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25177 }
25178
25179 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25180 table if necessary. For convenience, return TYPE.
25181
25182 The DIEs reading must have careful ordering to:
25183 * Not cause infite loops trying to read in DIEs as a prerequisite for
25184 reading current DIE.
25185 * Not trying to dereference contents of still incompletely read in types
25186 while reading in other DIEs.
25187 * Enable referencing still incompletely read in types just by a pointer to
25188 the type without accessing its fields.
25189
25190 Therefore caller should follow these rules:
25191 * Try to fetch any prerequisite types we may need to build this DIE type
25192 before building the type and calling set_die_type.
25193 * After building type call set_die_type for current DIE as soon as
25194 possible before fetching more types to complete the current type.
25195 * Make the type as complete as possible before fetching more types. */
25196
25197 static struct type *
25198 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25199 {
25200 struct dwarf2_per_objfile *dwarf2_per_objfile
25201 = cu->per_cu->dwarf2_per_objfile;
25202 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25203 struct objfile *objfile = dwarf2_per_objfile->objfile;
25204 struct attribute *attr;
25205 struct dynamic_prop prop;
25206
25207 /* For Ada types, make sure that the gnat-specific data is always
25208 initialized (if not already set). There are a few types where
25209 we should not be doing so, because the type-specific area is
25210 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25211 where the type-specific area is used to store the floatformat).
25212 But this is not a problem, because the gnat-specific information
25213 is actually not needed for these types. */
25214 if (need_gnat_info (cu)
25215 && TYPE_CODE (type) != TYPE_CODE_FUNC
25216 && TYPE_CODE (type) != TYPE_CODE_FLT
25217 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25218 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25219 && TYPE_CODE (type) != TYPE_CODE_METHOD
25220 && !HAVE_GNAT_AUX_INFO (type))
25221 INIT_GNAT_SPECIFIC (type);
25222
25223 /* Read DW_AT_allocated and set in type. */
25224 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25225 if (attr_form_is_block (attr))
25226 {
25227 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25228 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25229 }
25230 else if (attr != NULL)
25231 {
25232 complaint (&symfile_complaints,
25233 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25234 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25235 sect_offset_str (die->sect_off));
25236 }
25237
25238 /* Read DW_AT_associated and set in type. */
25239 attr = dwarf2_attr (die, DW_AT_associated, cu);
25240 if (attr_form_is_block (attr))
25241 {
25242 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25243 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25244 }
25245 else if (attr != NULL)
25246 {
25247 complaint (&symfile_complaints,
25248 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25249 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25250 sect_offset_str (die->sect_off));
25251 }
25252
25253 /* Read DW_AT_data_location and set in type. */
25254 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25255 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25256 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25257
25258 if (dwarf2_per_objfile->die_type_hash == NULL)
25259 {
25260 dwarf2_per_objfile->die_type_hash =
25261 htab_create_alloc_ex (127,
25262 per_cu_offset_and_type_hash,
25263 per_cu_offset_and_type_eq,
25264 NULL,
25265 &objfile->objfile_obstack,
25266 hashtab_obstack_allocate,
25267 dummy_obstack_deallocate);
25268 }
25269
25270 ofs.per_cu = cu->per_cu;
25271 ofs.sect_off = die->sect_off;
25272 ofs.type = type;
25273 slot = (struct dwarf2_per_cu_offset_and_type **)
25274 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25275 if (*slot)
25276 complaint (&symfile_complaints,
25277 _("A problem internal to GDB: DIE %s has type already set"),
25278 sect_offset_str (die->sect_off));
25279 *slot = XOBNEW (&objfile->objfile_obstack,
25280 struct dwarf2_per_cu_offset_and_type);
25281 **slot = ofs;
25282 return type;
25283 }
25284
25285 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25286 or return NULL if the die does not have a saved type. */
25287
25288 static struct type *
25289 get_die_type_at_offset (sect_offset sect_off,
25290 struct dwarf2_per_cu_data *per_cu)
25291 {
25292 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25293 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25294
25295 if (dwarf2_per_objfile->die_type_hash == NULL)
25296 return NULL;
25297
25298 ofs.per_cu = per_cu;
25299 ofs.sect_off = sect_off;
25300 slot = ((struct dwarf2_per_cu_offset_and_type *)
25301 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25302 if (slot)
25303 return slot->type;
25304 else
25305 return NULL;
25306 }
25307
25308 /* Look up the type for DIE in CU in die_type_hash,
25309 or return NULL if DIE does not have a saved type. */
25310
25311 static struct type *
25312 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25313 {
25314 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25315 }
25316
25317 /* Add a dependence relationship from CU to REF_PER_CU. */
25318
25319 static void
25320 dwarf2_add_dependence (struct dwarf2_cu *cu,
25321 struct dwarf2_per_cu_data *ref_per_cu)
25322 {
25323 void **slot;
25324
25325 if (cu->dependencies == NULL)
25326 cu->dependencies
25327 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25328 NULL, &cu->comp_unit_obstack,
25329 hashtab_obstack_allocate,
25330 dummy_obstack_deallocate);
25331
25332 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25333 if (*slot == NULL)
25334 *slot = ref_per_cu;
25335 }
25336
25337 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25338 Set the mark field in every compilation unit in the
25339 cache that we must keep because we are keeping CU. */
25340
25341 static int
25342 dwarf2_mark_helper (void **slot, void *data)
25343 {
25344 struct dwarf2_per_cu_data *per_cu;
25345
25346 per_cu = (struct dwarf2_per_cu_data *) *slot;
25347
25348 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25349 reading of the chain. As such dependencies remain valid it is not much
25350 useful to track and undo them during QUIT cleanups. */
25351 if (per_cu->cu == NULL)
25352 return 1;
25353
25354 if (per_cu->cu->mark)
25355 return 1;
25356 per_cu->cu->mark = 1;
25357
25358 if (per_cu->cu->dependencies != NULL)
25359 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25360
25361 return 1;
25362 }
25363
25364 /* Set the mark field in CU and in every other compilation unit in the
25365 cache that we must keep because we are keeping CU. */
25366
25367 static void
25368 dwarf2_mark (struct dwarf2_cu *cu)
25369 {
25370 if (cu->mark)
25371 return;
25372 cu->mark = 1;
25373 if (cu->dependencies != NULL)
25374 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25375 }
25376
25377 static void
25378 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25379 {
25380 while (per_cu)
25381 {
25382 per_cu->cu->mark = 0;
25383 per_cu = per_cu->cu->read_in_chain;
25384 }
25385 }
25386
25387 /* Trivial hash function for partial_die_info: the hash value of a DIE
25388 is its offset in .debug_info for this objfile. */
25389
25390 static hashval_t
25391 partial_die_hash (const void *item)
25392 {
25393 const struct partial_die_info *part_die
25394 = (const struct partial_die_info *) item;
25395
25396 return to_underlying (part_die->sect_off);
25397 }
25398
25399 /* Trivial comparison function for partial_die_info structures: two DIEs
25400 are equal if they have the same offset. */
25401
25402 static int
25403 partial_die_eq (const void *item_lhs, const void *item_rhs)
25404 {
25405 const struct partial_die_info *part_die_lhs
25406 = (const struct partial_die_info *) item_lhs;
25407 const struct partial_die_info *part_die_rhs
25408 = (const struct partial_die_info *) item_rhs;
25409
25410 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25411 }
25412
25413 static struct cmd_list_element *set_dwarf_cmdlist;
25414 static struct cmd_list_element *show_dwarf_cmdlist;
25415
25416 static void
25417 set_dwarf_cmd (const char *args, int from_tty)
25418 {
25419 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25420 gdb_stdout);
25421 }
25422
25423 static void
25424 show_dwarf_cmd (const char *args, int from_tty)
25425 {
25426 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25427 }
25428
25429 int dwarf_always_disassemble;
25430
25431 static void
25432 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25433 struct cmd_list_element *c, const char *value)
25434 {
25435 fprintf_filtered (file,
25436 _("Whether to always disassemble "
25437 "DWARF expressions is %s.\n"),
25438 value);
25439 }
25440
25441 static void
25442 show_check_physname (struct ui_file *file, int from_tty,
25443 struct cmd_list_element *c, const char *value)
25444 {
25445 fprintf_filtered (file,
25446 _("Whether to check \"physname\" is %s.\n"),
25447 value);
25448 }
25449
25450 void
25451 _initialize_dwarf2_read (void)
25452 {
25453
25454 dwarf2_objfile_data_key = register_objfile_data ();
25455
25456 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25457 Set DWARF specific variables.\n\
25458 Configure DWARF variables such as the cache size"),
25459 &set_dwarf_cmdlist, "maintenance set dwarf ",
25460 0/*allow-unknown*/, &maintenance_set_cmdlist);
25461
25462 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25463 Show DWARF specific variables\n\
25464 Show DWARF variables such as the cache size"),
25465 &show_dwarf_cmdlist, "maintenance show dwarf ",
25466 0/*allow-unknown*/, &maintenance_show_cmdlist);
25467
25468 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25469 &dwarf_max_cache_age, _("\
25470 Set the upper bound on the age of cached DWARF compilation units."), _("\
25471 Show the upper bound on the age of cached DWARF compilation units."), _("\
25472 A higher limit means that cached compilation units will be stored\n\
25473 in memory longer, and more total memory will be used. Zero disables\n\
25474 caching, which can slow down startup."),
25475 NULL,
25476 show_dwarf_max_cache_age,
25477 &set_dwarf_cmdlist,
25478 &show_dwarf_cmdlist);
25479
25480 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25481 &dwarf_always_disassemble, _("\
25482 Set whether `info address' always disassembles DWARF expressions."), _("\
25483 Show whether `info address' always disassembles DWARF expressions."), _("\
25484 When enabled, DWARF expressions are always printed in an assembly-like\n\
25485 syntax. When disabled, expressions will be printed in a more\n\
25486 conversational style, when possible."),
25487 NULL,
25488 show_dwarf_always_disassemble,
25489 &set_dwarf_cmdlist,
25490 &show_dwarf_cmdlist);
25491
25492 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25493 Set debugging of the DWARF reader."), _("\
25494 Show debugging of the DWARF reader."), _("\
25495 When enabled (non-zero), debugging messages are printed during DWARF\n\
25496 reading and symtab expansion. A value of 1 (one) provides basic\n\
25497 information. A value greater than 1 provides more verbose information."),
25498 NULL,
25499 NULL,
25500 &setdebuglist, &showdebuglist);
25501
25502 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25503 Set debugging of the DWARF DIE reader."), _("\
25504 Show debugging of the DWARF DIE reader."), _("\
25505 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25506 The value is the maximum depth to print."),
25507 NULL,
25508 NULL,
25509 &setdebuglist, &showdebuglist);
25510
25511 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25512 Set debugging of the dwarf line reader."), _("\
25513 Show debugging of the dwarf line reader."), _("\
25514 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25515 A value of 1 (one) provides basic information.\n\
25516 A value greater than 1 provides more verbose information."),
25517 NULL,
25518 NULL,
25519 &setdebuglist, &showdebuglist);
25520
25521 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25522 Set cross-checking of \"physname\" code against demangler."), _("\
25523 Show cross-checking of \"physname\" code against demangler."), _("\
25524 When enabled, GDB's internal \"physname\" code is checked against\n\
25525 the demangler."),
25526 NULL, show_check_physname,
25527 &setdebuglist, &showdebuglist);
25528
25529 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25530 no_class, &use_deprecated_index_sections, _("\
25531 Set whether to use deprecated gdb_index sections."), _("\
25532 Show whether to use deprecated gdb_index sections."), _("\
25533 When enabled, deprecated .gdb_index sections are used anyway.\n\
25534 Normally they are ignored either because of a missing feature or\n\
25535 performance issue.\n\
25536 Warning: This option must be enabled before gdb reads the file."),
25537 NULL,
25538 NULL,
25539 &setlist, &showlist);
25540
25541 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25542 &dwarf2_locexpr_funcs);
25543 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25544 &dwarf2_loclist_funcs);
25545
25546 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25547 &dwarf2_block_frame_base_locexpr_funcs);
25548 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25549 &dwarf2_block_frame_base_loclist_funcs);
25550
25551 #if GDB_SELF_TEST
25552 selftests::register_test ("dw2_expand_symtabs_matching",
25553 selftests::dw2_expand_symtabs_matching::run_test);
25554 #endif
25555 }
This page took 0.533585 seconds and 5 git commands to generate.